2022-06-09 15:04:36 +00:00
|
|
|
#include "cli_command_gpio.h"
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
#include "core/string.h"
|
2022-06-09 15:04:36 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <furi_hal.h>
|
|
|
|
#include <lib/toolbox/args.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const GpioPin* pin;
|
|
|
|
const char* name;
|
|
|
|
const bool debug;
|
|
|
|
} CliCommandGpio;
|
|
|
|
|
|
|
|
const CliCommandGpio cli_command_gpio_pins[] = {
|
|
|
|
{.pin = &gpio_ext_pc0, .name = "PC0", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pc1, .name = "PC1", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pc3, .name = "PC3", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pb2, .name = "PB2", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pb3, .name = "PB3", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pa4, .name = "PA4", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pa6, .name = "PA6", .debug = false},
|
|
|
|
{.pin = &gpio_ext_pa7, .name = "PA7", .debug = false},
|
|
|
|
/* Dangerous pins, may damage hardware */
|
|
|
|
{.pin = &gpio_infrared_rx, .name = "PA0", .debug = true},
|
|
|
|
{.pin = &gpio_usart_rx, .name = "PB7", .debug = true},
|
|
|
|
{.pin = &gpio_speaker, .name = "PB8", .debug = true},
|
|
|
|
{.pin = &gpio_infrared_tx, .name = "PB9", .debug = true},
|
|
|
|
};
|
|
|
|
|
|
|
|
void cli_command_gpio_print_usage() {
|
|
|
|
printf("Usage:\r\n");
|
|
|
|
printf("gpio <cmd> <args>\r\n");
|
|
|
|
printf("Cmd list:\r\n");
|
|
|
|
printf("\tmode <pin_name> <0|1>\t - Set gpio mode: 0 - input, 1 - output\r\n");
|
|
|
|
printf("\tset <pin_name> <0|1>\t - Set gpio value\r\n");
|
|
|
|
printf("\tread <pin_name>\t - Read gpio value\r\n");
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool pin_name_to_int(FuriString* pin_name, size_t* result) {
|
2022-12-26 12:13:30 +00:00
|
|
|
bool is_debug_mode = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
|
2022-06-09 15:04:36 +00:00
|
|
|
for(size_t i = 0; i < COUNT_OF(cli_command_gpio_pins); i++) {
|
2022-12-26 12:13:30 +00:00
|
|
|
if(furi_string_equal(pin_name, cli_command_gpio_pins[i].name)) {
|
|
|
|
if(!cli_command_gpio_pins[i].debug || is_debug_mode) {
|
2022-06-09 15:04:36 +00:00
|
|
|
*result = i;
|
2022-12-26 12:13:30 +00:00
|
|
|
return true;
|
2022-06-09 15:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
return false;
|
2022-06-09 15:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gpio_print_pins(void) {
|
|
|
|
printf("Wrong pin name. Available pins: ");
|
2022-12-26 12:13:30 +00:00
|
|
|
bool is_debug_mode = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
|
2022-06-09 15:04:36 +00:00
|
|
|
for(size_t i = 0; i < COUNT_OF(cli_command_gpio_pins); i++) {
|
2022-12-26 12:13:30 +00:00
|
|
|
if(!cli_command_gpio_pins[i].debug || is_debug_mode) {
|
2022-06-09 15:04:36 +00:00
|
|
|
printf("%s ", cli_command_gpio_pins[i].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
typedef enum {
|
|
|
|
GpioParseReturnOk,
|
|
|
|
GpioParseReturnCmdSyntaxError,
|
|
|
|
GpioParseReturnPinError,
|
|
|
|
GpioParseReturnValueError
|
|
|
|
} GpioParseReturn;
|
|
|
|
|
|
|
|
static GpioParseReturn gpio_command_parse(FuriString* args, size_t* pin_num, uint8_t* value) {
|
2022-12-26 12:13:30 +00:00
|
|
|
GpioParseReturn ret = GpioParseReturnOk;
|
|
|
|
FuriString* pin_name = furi_string_alloc();
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(!args_read_string_and_trim(args, pin_name)) {
|
|
|
|
ret = GpioParseReturnCmdSyntaxError;
|
|
|
|
break;
|
|
|
|
} else if(!pin_name_to_int(pin_name, pin_num)) {
|
|
|
|
ret = GpioParseReturnPinError;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-09 15:04:36 +00:00
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
int pin_mode; //-V779
|
|
|
|
if(!args_read_int_and_trim(args, &pin_mode) || pin_mode < 0 || pin_mode > 1) {
|
|
|
|
ret = GpioParseReturnValueError;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-09 15:04:36 +00:00
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
*value = pin_mode;
|
|
|
|
} while(false);
|
2022-06-09 15:04:36 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(pin_name);
|
2022-12-26 12:13:30 +00:00
|
|
|
return ret;
|
2022-06-09 15:04:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void cli_command_gpio_mode(Cli* cli, FuriString* args, void* context) {
|
2022-06-09 15:04:36 +00:00
|
|
|
UNUSED(cli);
|
|
|
|
UNUSED(context);
|
|
|
|
|
|
|
|
size_t num = 0;
|
|
|
|
uint8_t value = 255;
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
GpioParseReturn err = gpio_command_parse(args, &num, &value);
|
2022-06-09 15:04:36 +00:00
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(err == GpioParseReturnCmdSyntaxError) {
|
2022-10-05 15:15:23 +00:00
|
|
|
cli_print_usage("gpio mode", "<pin_name> <0|1>", furi_string_get_cstr(args));
|
2022-06-09 15:04:36 +00:00
|
|
|
return;
|
2022-12-26 12:13:30 +00:00
|
|
|
} else if(err == GpioParseReturnPinError) { //-V547
|
2022-06-09 15:04:36 +00:00
|
|
|
gpio_print_pins();
|
|
|
|
return;
|
2022-11-05 15:07:24 +00:00
|
|
|
} else if(err == GpioParseReturnValueError) {
|
2022-06-09 15:04:36 +00:00
|
|
|
printf("Value is invalid. Enter 1 for input or 0 for output");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
if(cli_command_gpio_pins[num].debug) { //-V779
|
2022-06-09 15:04:36 +00:00
|
|
|
printf(
|
2022-11-23 13:15:08 +00:00
|
|
|
"Changing this pin mode may damage hardware. Are you sure you want to continue? (y/n)?\r\n");
|
2022-06-09 15:04:36 +00:00
|
|
|
char c = cli_getc(cli);
|
|
|
|
if(c != 'y' && c != 'Y') {
|
|
|
|
printf("Cancelled.\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(value == 1) { // output
|
|
|
|
furi_hal_gpio_write(cli_command_gpio_pins[num].pin, false);
|
|
|
|
furi_hal_gpio_init_simple(cli_command_gpio_pins[num].pin, GpioModeOutputPushPull);
|
|
|
|
printf("Pin %s is now an output (low)", cli_command_gpio_pins[num].name);
|
|
|
|
} else { // input
|
|
|
|
furi_hal_gpio_init_simple(cli_command_gpio_pins[num].pin, GpioModeInput);
|
|
|
|
printf("Pin %s is now an input", cli_command_gpio_pins[num].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void cli_command_gpio_read(Cli* cli, FuriString* args, void* context) {
|
2022-06-09 15:04:36 +00:00
|
|
|
UNUSED(cli);
|
|
|
|
UNUSED(context);
|
|
|
|
|
|
|
|
size_t num = 0;
|
|
|
|
if(!pin_name_to_int(args, &num)) {
|
|
|
|
gpio_print_pins();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
if(LL_GPIO_MODE_INPUT != //-V779
|
2022-06-09 15:04:36 +00:00
|
|
|
LL_GPIO_GetPinMode(
|
|
|
|
cli_command_gpio_pins[num].pin->port, cli_command_gpio_pins[num].pin->pin)) {
|
|
|
|
printf("Err: pin %s is not set as an input.", cli_command_gpio_pins[num].name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t val = !!furi_hal_gpio_read(cli_command_gpio_pins[num].pin);
|
|
|
|
|
|
|
|
printf("Pin %s <= %u", cli_command_gpio_pins[num].name, val);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void cli_command_gpio_set(Cli* cli, FuriString* args, void* context) {
|
2022-06-09 15:04:36 +00:00
|
|
|
UNUSED(context);
|
|
|
|
|
|
|
|
size_t num = 0;
|
|
|
|
uint8_t value = 0;
|
2022-11-05 15:07:24 +00:00
|
|
|
GpioParseReturn err = gpio_command_parse(args, &num, &value);
|
2022-06-09 15:04:36 +00:00
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(err == GpioParseReturnCmdSyntaxError) {
|
2022-10-05 15:15:23 +00:00
|
|
|
cli_print_usage("gpio set", "<pin_name> <0|1>", furi_string_get_cstr(args));
|
2022-06-09 15:04:36 +00:00
|
|
|
return;
|
2022-12-26 12:13:30 +00:00
|
|
|
} else if(err == GpioParseReturnPinError) { //-V547
|
2022-06-09 15:04:36 +00:00
|
|
|
gpio_print_pins();
|
|
|
|
return;
|
2022-11-05 15:07:24 +00:00
|
|
|
} else if(err == GpioParseReturnValueError) {
|
2022-06-09 15:04:36 +00:00
|
|
|
printf("Value is invalid. Enter 1 for high or 0 for low");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
if(LL_GPIO_MODE_OUTPUT != //-V779
|
2022-06-09 15:04:36 +00:00
|
|
|
LL_GPIO_GetPinMode(
|
|
|
|
cli_command_gpio_pins[num].pin->port, cli_command_gpio_pins[num].pin->pin)) {
|
|
|
|
printf("Err: pin %s is not set as an output.", cli_command_gpio_pins[num].name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extra check if debug pins used
|
|
|
|
if(cli_command_gpio_pins[num].debug) {
|
|
|
|
printf(
|
|
|
|
"Setting this pin may damage hardware. Are you sure you want to continue? (y/n)?\r\n");
|
|
|
|
char c = cli_getc(cli);
|
|
|
|
if(c != 'y' && c != 'Y') {
|
|
|
|
printf("Cancelled.\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_gpio_write(cli_command_gpio_pins[num].pin, !!value);
|
|
|
|
printf("Pin %s => %u", cli_command_gpio_pins[num].name, !!value);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void cli_command_gpio(Cli* cli, FuriString* args, void* context) {
|
|
|
|
FuriString* cmd;
|
|
|
|
cmd = furi_string_alloc();
|
2022-06-09 15:04:36 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
if(!args_read_string_and_trim(args, cmd)) {
|
|
|
|
cli_command_gpio_print_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_cmp_str(cmd, "mode") == 0) {
|
2022-06-09 15:04:36 +00:00
|
|
|
cli_command_gpio_mode(cli, args, context);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_cmp_str(cmd, "set") == 0) {
|
2022-06-09 15:04:36 +00:00
|
|
|
cli_command_gpio_set(cli, args, context);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_cmp_str(cmd, "read") == 0) {
|
2022-06-09 15:04:36 +00:00
|
|
|
cli_command_gpio_read(cli, args, context);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cli_command_gpio_print_usage();
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(cmd);
|
2022-06-09 15:04:36 +00:00
|
|
|
}
|