CLI: readonly pin BOOT (IO0), strict ordering in gpio_get

This commit is contained in:
DrZlo13 2021-12-07 17:38:15 +10:00
parent e36f6f41c4
commit d96f1dc05e
1 changed files with 72 additions and 18 deletions

View File

@ -7,15 +7,26 @@
typedef struct {
const char* name;
const gpio_num_t gpio_num;
const bool get;
const bool set;
} GPIOItem;
#define MAKE_IO(pin_index) \
{ .name = "IO" #pin_index, .gpio_num = GPIO_NUM_##pin_index }
{ .name = "IO" #pin_index, .gpio_num = GPIO_NUM_##pin_index, .get = true, .set = true }
#define MAKE_IO_NAMED(pin_name, pin_index) \
{ .name = pin_name, .gpio_num = GPIO_NUM_##pin_index }
{ .name = pin_name, .gpio_num = GPIO_NUM_##pin_index, .get = true, .set = true }
const GPIOItem gpio_items[] = {
#define MAKE_IO_READONLY(pin_index) \
{ .name = "IO" #pin_index, .gpio_num = GPIO_NUM_##pin_index, .get = true, .set = false }
#define MAKE_IO_NAMED_READONLY(pin_name, pin_index) \
{ .name = pin_name, .gpio_num = GPIO_NUM_##pin_index, .get = true, .set = false }
static const GPIOItem gpio_items[] = {
//BOOT
MAKE_IO_READONLY(0),
MAKE_IO_NAMED_READONLY("BOOT", 0),
// SWD
MAKE_IO(1),
MAKE_IO_NAMED("SWCLK", 1),
@ -24,6 +35,13 @@ const GPIOItem gpio_items[] = {
// I2C_PULL_UP
MAKE_IO(3),
MAKE_IO_NAMED("I2C_PULL_UP", 3),
// I2C_PULL_UP
MAKE_IO(4),
MAKE_IO_NAMED("LED_BLUE", 4),
MAKE_IO(5),
MAKE_IO_NAMED("LED_GREEN", 5),
MAKE_IO(6),
MAKE_IO_NAMED("LED_RED", 6),
// GPIO
MAKE_IO(7),
MAKE_IO(8),
@ -67,22 +85,46 @@ const GPIOItem gpio_items[] = {
MAKE_IO_NAMED("JTAG_TMS", 41),
};
size_t gpio_items_count = COUNT_OF(gpio_items);
static void cli_gpio_print_list(Cli* cli) {
static void cli_gpio_print_list_set(Cli* cli) {
cli_write_str(cli, "Pin names: ");
for(size_t i = 0; i < gpio_items_count; i++) {
cli_write_str(cli, gpio_items[i].name);
if((i + 1) < gpio_items_count) {
cli_write_str(cli, " ");
for(size_t i = 0; i < COUNT_OF(gpio_items); i++) {
if(gpio_items[i].set) {
cli_write_str(cli, gpio_items[i].name);
if((i + 1) < COUNT_OF(gpio_items)) {
cli_write_str(cli, " ");
}
}
}
}
static gpio_num_t gpio_search(mstring_t* gpio_name) {
static gpio_num_t gpio_search_set(mstring_t* gpio_name) {
gpio_num_t gpio_num = GPIO_NUM_NC;
for(size_t i = 0; i < gpio_items_count; i++) {
if(mstring_cmp_cstr(gpio_name, gpio_items[i].name) == 0) {
for(size_t i = 0; i < COUNT_OF(gpio_items); i++) {
if(mstring_cmp_cstr(gpio_name, gpio_items[i].name) == 0 && gpio_items[i].set) {
gpio_num = gpio_items[i].gpio_num;
break;
}
}
return gpio_num;
}
static void cli_gpio_print_list_get(Cli* cli) {
cli_write_str(cli, "Pin names: ");
for(size_t i = 0; i < COUNT_OF(gpio_items); i++) {
if(gpio_items[i].get) {
cli_write_str(cli, gpio_items[i].name);
if((i + 1) < COUNT_OF(gpio_items)) {
cli_write_str(cli, " ");
}
}
}
}
static gpio_num_t gpio_search_get(mstring_t* gpio_name) {
gpio_num_t gpio_num = GPIO_NUM_NC;
for(size_t i = 0; i < COUNT_OF(gpio_items); i++) {
if(mstring_cmp_cstr(gpio_name, gpio_items[i].name) == 0 && gpio_items[i].get) {
gpio_num = gpio_items[i].gpio_num;
break;
}
@ -94,7 +136,7 @@ static gpio_num_t gpio_search(mstring_t* gpio_name) {
void cli_gpio_set_print_usage(Cli* cli) {
cli_write_str(cli, "Usage: gpio_set <pin_names> <value>");
cli_write_eol(cli);
cli_gpio_print_list(cli);
cli_gpio_print_list_set(cli);
cli_write_eol(cli);
cli_write_str(cli, "Examples:");
cli_write_eol(cli);
@ -106,7 +148,7 @@ void cli_gpio_set_print_usage(Cli* cli) {
void cli_gpio_get_print_usage(Cli* cli) {
cli_write_str(cli, "Usage: gpio_get <pin_names>");
cli_write_eol(cli);
cli_gpio_print_list(cli);
cli_gpio_print_list_get(cli);
cli_write_eol(cli);
cli_write_str(cli, "Examples:");
cli_write_eol(cli);
@ -137,7 +179,7 @@ void cli_gpio_set(Cli* cli, mstring_t* args) {
for(size_t i = 0; i < (argc - 1); i++) {
if(cli_args_read_string_and_trim(args, gpio_name)) {
gpio_num_t gpio_num = gpio_search(gpio_name);
gpio_num_t gpio_num = gpio_search_set(gpio_name);
if(gpio_num == GPIO_NUM_NC) {
cli_printf(cli, "Invalid <pin_name> %s", mstring_get_cstr(gpio_name));
@ -190,10 +232,11 @@ void cli_gpio_get(Cli* cli, mstring_t* args) {
io_conf.pull_up_en = 0;
bool error = false;
gpio_num_t last_gpio_num = GPIO_NUM_NC;
for(size_t i = 0; i < argc; i++) {
if(cli_args_read_string_and_trim(args, gpio_name)) {
gpio_num_t gpio_num = gpio_search(gpio_name);
gpio_num_t gpio_num = gpio_search_get(gpio_name);
if(gpio_num == GPIO_NUM_NC) {
cli_printf(cli, "Invalid <pin_name> %s", mstring_get_cstr(gpio_name));
@ -201,7 +244,18 @@ void cli_gpio_get(Cli* cli, mstring_t* args) {
error = true;
break;
} else {
io_conf.pin_bit_mask |= (1LL << (uint64_t)gpio_num);
if(gpio_num <= last_gpio_num) {
cli_printf(
cli,
"<pin_name> %s is less than or equal to the previous",
mstring_get_cstr(gpio_name));
cli_write_eol(cli);
error = true;
break;
} else {
last_gpio_num = gpio_num;
io_conf.pin_bit_mask |= (1LL << (uint64_t)gpio_num);
}
}
} else {
error = true;