|
| 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] = %u (0x%X)\n", i, |
| 40 | + (unsigned int)val, (unsigned int)val); |
| 41 | + } |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +int |
| 46 | +nvreg_read_func(const struct shell_cmd *cmd, int argc, char *argv[], |
| 47 | + struct streamer *streamer) |
| 48 | +{ |
| 49 | + int reg = -1; |
| 50 | + uint32_t val; |
| 51 | + int rc; |
| 52 | + |
| 53 | + if (argc > 1) { |
| 54 | + reg = parse_ll_bounds(argv[1], 0, 256, &rc); |
| 55 | + } |
| 56 | + if (reg >= 0) { |
| 57 | + val = hal_nvreg_read(reg); |
| 58 | + streamer_printf(streamer, "reg[%d] = %u (0x%X)\n", reg, |
| 59 | + (unsigned int)val, (unsigned int)val); |
| 60 | + } |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +int |
| 65 | +nvreg_write_func(const struct shell_cmd *cmd, int argc, char *argv[], |
| 66 | + struct streamer *streamer) |
| 67 | +{ |
| 68 | + int reg = -1; |
| 69 | + uint32_t val = 0; |
| 70 | + int rc; |
| 71 | + |
| 72 | + if (argc > 2) { |
| 73 | + reg = parse_ll_bounds(argv[1], 0, 256, &rc); |
| 74 | + if (rc == 0) { |
| 75 | + val = parse_ll_bounds(argv[2], 0, 0xFFFFFFFF, &rc); |
| 76 | + } |
| 77 | + } |
| 78 | + if (argc >= 3 && rc == 0) { |
| 79 | + hal_nvreg_write(reg, val); |
| 80 | + } else { |
| 81 | + streamer_printf(streamer, "%s <reg> <val>\n", cmd->sc_cmd); |
| 82 | + } |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +int |
| 87 | +gpio_init_out_func(const struct shell_cmd *cmd, int argc, char *argv[], |
| 88 | + struct streamer *streamer) |
| 89 | +{ |
| 90 | + int pin = -1; |
| 91 | + int val = -1; |
| 92 | + int rc; |
| 93 | + |
| 94 | + if (argc > 1) { |
| 95 | + pin = parse_ll_bounds(argv[1], 0, 512, &rc); |
| 96 | + } |
| 97 | + if (argc > 2) { |
| 98 | + val = parse_ll_bounds(argv[2], 0, 1, &rc); |
| 99 | + } else { |
| 100 | + val = 0; |
| 101 | + } |
| 102 | + if (pin >= 0 && val >= 0) { |
| 103 | + hal_gpio_init_out(pin, val); |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +int |
| 109 | +gpio_write_func(const struct shell_cmd *cmd, int argc, char *argv[], |
| 110 | + struct streamer *streamer) |
| 111 | +{ |
| 112 | + int pin = -1; |
| 113 | + int val = -1; |
| 114 | + int rc; |
| 115 | + |
| 116 | + if (argc < 3) { |
| 117 | + streamer_printf(streamer, "%s <pin> 0 | 1\n", cmd->sc_cmd); |
| 118 | + } else { |
| 119 | + pin = parse_ll_bounds(argv[1], 0, 512, &rc); |
| 120 | + val = parse_ll_bounds(argv[2], 0, 1, &rc); |
| 121 | + } |
| 122 | + if (pin >= 0 && val >= 0) { |
| 123 | + hal_gpio_write(pin, val); |
| 124 | + } |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +int |
| 129 | +gpio_toggle_func(const struct shell_cmd *cmd, int argc, char *argv[], |
| 130 | + struct streamer *streamer) |
| 131 | +{ |
| 132 | + int pin = -1; |
| 133 | + int rc; |
| 134 | + |
| 135 | + if (argc < 2) { |
| 136 | + streamer_printf(streamer, "%s <pin>\n", cmd->sc_cmd); |
| 137 | + } else { |
| 138 | + pin = parse_ll_bounds(argv[1], 0, 512, &rc); |
| 139 | + } |
| 140 | + if (pin >= 0) { |
| 141 | + hal_gpio_toggle(pin); |
| 142 | + } |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +SHELL_MODULE_EXT_CMD(hal, nvreg_dump, nvreg_dump_func, NULL); |
| 147 | +SHELL_MODULE_EXT_CMD(hal, nvreg_read, nvreg_read_func, NULL); |
| 148 | +SHELL_MODULE_EXT_CMD(hal, nvreg_write, nvreg_write_func, NULL); |
| 149 | +SHELL_MODULE_EXT_CMD(hal, gpio_init_out, gpio_init_out_func, NULL); |
| 150 | +SHELL_MODULE_EXT_CMD(hal, gpio_write, gpio_write_func, NULL); |
| 151 | +SHELL_MODULE_EXT_CMD(hal, gpio_toggle, gpio_toggle_func, NULL); |
| 152 | + |
| 153 | +SHELL_MODULE_WITH_LINK_TABLE(hal); |
0 commit comments