Skip to content

Commit c73ca9b

Browse files
committed
hal/shell: Add shell commands for hal
This adds few commands to shell to manipulate gpio and nvregs. Signed-off-by: Jerzy Kasenberg <jerzy@apache.org>
1 parent a52f357 commit c73ca9b

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

hw/hal/shell_cmds/pkg.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: hw/hal/shell_cmds
21+
pkg.description: Hardware Abstraction Layer shell commands
22+
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
23+
pkg.homepage: "http://mynewt.apache.org/"
24+
pkg.keywords:
25+
26+
pkg.whole_archive: true
27+
28+
pkg.deps:
29+
- "@apache-mynewt-core/hw/hal"
30+
- "@apache-mynewt-core/sys/shell"
31+
32+
pkg.link_tables:
33+
- hal_commands
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <errno.h>
21+
#include <string.h>
22+
#include <os/mynewt.h>
23+
#include <console/console.h>
24+
#include <shell/shell.h>
25+
#include <parse/parse.h>
26+
#include <hal/hal_gpio.h>
27+
#include <hal/hal_nvreg.h>
28+
29+
int
30+
nvreg_dump_func(const struct shell_cmd *cmd, int argc, char *argv[],
31+
struct streamer *streamer)
32+
{
33+
int i;
34+
uint32_t val;
35+
int n = hal_nvreg_get_num_regs();
36+
37+
for (i = 0; i < n; ++i) {
38+
val = hal_nvreg_read(i);
39+
streamer_printf(streamer, "reg[%d] = %" PRIu32 " (0x%" PRIX32 ")\n", i, val, val);
40+
}
41+
return 0;
42+
}
43+
44+
int
45+
nvreg_read_func(const struct shell_cmd *cmd, int argc, char *argv[],
46+
struct streamer *streamer)
47+
{
48+
int reg = -1;
49+
uint32_t val;
50+
int rc;
51+
52+
if (argc > 1) {
53+
reg = parse_ll_bounds(argv[1], 0, 256, &rc);
54+
}
55+
if (reg >=0) {
56+
val = hal_nvreg_read(reg);
57+
streamer_printf(streamer, "reg[%d] = %" PRIu32 " (0x%" PRIX32 ")\n", reg, val, val);
58+
}
59+
return 0;
60+
}
61+
62+
int
63+
nvreg_write_func(const struct shell_cmd *cmd, int argc, char *argv[],
64+
struct streamer *streamer)
65+
{
66+
int reg = -1;
67+
uint32_t val = 0;
68+
int rc;
69+
70+
if (argc > 2) {
71+
reg = parse_ll_bounds(argv[1], 0, 256, &rc);
72+
if (rc == 0) {
73+
val = parse_ll_bounds(argv[2], 0, 0xFFFFFFFF, &rc);
74+
}
75+
}
76+
if (argc >= 3 && rc == 0) {
77+
hal_nvreg_write(reg, val);
78+
} else {
79+
streamer_printf(streamer, "%s <reg> <val>\n", cmd->sc_cmd);
80+
}
81+
return 0;
82+
}
83+
84+
int
85+
gpio_init_out_func(const struct shell_cmd *cmd, int argc, char *argv[],
86+
struct streamer *streamer)
87+
{
88+
int pin = -1;
89+
int val = -1;
90+
int rc;
91+
92+
if (argc > 1) {
93+
pin = parse_ll_bounds(argv[1], 0, 512, &rc);
94+
}
95+
if (argc > 2) {
96+
val = parse_ll_bounds(argv[2], 0, 1, &rc);
97+
} else {
98+
val = 0;
99+
}
100+
if (pin >=0 && val >= 0) {
101+
hal_gpio_init_out(pin, val);
102+
}
103+
return 0;
104+
}
105+
106+
int
107+
gpio_write_func(const struct shell_cmd *cmd, int argc, char *argv[],
108+
struct streamer *streamer)
109+
{
110+
int pin = -1;
111+
int val = -1;
112+
int rc;
113+
114+
if (argc < 3) {
115+
streamer_printf(streamer, "%s <pin> 0 | 1\n", cmd->sc_cmd);
116+
} else {
117+
pin = parse_ll_bounds(argv[1], 0, 512, &rc);
118+
val = parse_ll_bounds(argv[2], 0, 1, &rc);
119+
}
120+
if (pin >=0 && val >= 0) {
121+
hal_gpio_write(pin, val);
122+
}
123+
return 0;
124+
}
125+
126+
int
127+
gpio_toggle_func(const struct shell_cmd *cmd, int argc, char *argv[],
128+
struct streamer *streamer)
129+
{
130+
int pin = -1;
131+
int rc;
132+
133+
if (argc < 2) {
134+
streamer_printf(streamer, "%s <pin>\n", cmd->sc_cmd);
135+
} else {
136+
pin = parse_ll_bounds(argv[1], 0, 512, &rc);
137+
}
138+
if (pin >=0) {
139+
hal_gpio_toggle(pin);
140+
}
141+
return 0;
142+
}
143+
144+
SHELL_MODULE_EXT_CMD(hal, nvreg_dump, nvreg_dump_func, NULL);
145+
SHELL_MODULE_EXT_CMD(hal, nvreg_read, nvreg_read_func, NULL);
146+
SHELL_MODULE_EXT_CMD(hal, nvreg_write, nvreg_write_func, NULL);
147+
SHELL_MODULE_EXT_CMD(hal, gpio_init_out, gpio_init_out_func, NULL);
148+
SHELL_MODULE_EXT_CMD(hal, gpio_write, gpio_write_func, NULL);
149+
SHELL_MODULE_EXT_CMD(hal, gpio_toggle, gpio_toggle_func, NULL);
150+
151+
SHELL_MODULE_WITH_LINK_TABLE(hal);
152+

0 commit comments

Comments
 (0)