cli_command_gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "cli_command_gpio.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <lib/toolbox/args.h>
  5. typedef struct {
  6. const GpioPin* pin;
  7. const char* name;
  8. const bool debug;
  9. } CliCommandGpio;
  10. const CliCommandGpio cli_command_gpio_pins[] = {
  11. {.pin = &gpio_ext_pc0, .name = "PC0", .debug = false},
  12. {.pin = &gpio_ext_pc1, .name = "PC1", .debug = false},
  13. {.pin = &gpio_ext_pc3, .name = "PC3", .debug = false},
  14. {.pin = &gpio_ext_pb2, .name = "PB2", .debug = false},
  15. {.pin = &gpio_ext_pb3, .name = "PB3", .debug = false},
  16. {.pin = &gpio_ext_pa4, .name = "PA4", .debug = false},
  17. {.pin = &gpio_ext_pa6, .name = "PA6", .debug = false},
  18. {.pin = &gpio_ext_pa7, .name = "PA7", .debug = false},
  19. /* Dangerous pins, may damage hardware */
  20. {.pin = &gpio_infrared_rx, .name = "PA0", .debug = true},
  21. {.pin = &gpio_usart_rx, .name = "PB7", .debug = true},
  22. {.pin = &gpio_speaker, .name = "PB8", .debug = true},
  23. {.pin = &gpio_infrared_tx, .name = "PB9", .debug = true},
  24. };
  25. void cli_command_gpio_print_usage() {
  26. printf("Usage:\r\n");
  27. printf("gpio <cmd> <args>\r\n");
  28. printf("Cmd list:\r\n");
  29. printf("\tmode <pin_name> <0|1>\t - Set gpio mode: 0 - input, 1 - output\r\n");
  30. printf("\tset <pin_name> <0|1>\t - Set gpio value\r\n");
  31. printf("\tread <pin_name>\t - Read gpio value\r\n");
  32. }
  33. static bool pin_name_to_int(FuriString* pin_name, size_t* result) {
  34. bool found = false;
  35. bool debug = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
  36. for(size_t i = 0; i < COUNT_OF(cli_command_gpio_pins); i++) {
  37. if(!furi_string_cmp(pin_name, cli_command_gpio_pins[i].name)) {
  38. if(!cli_command_gpio_pins[i].debug || debug) {
  39. *result = i;
  40. found = true;
  41. break;
  42. }
  43. }
  44. }
  45. return found;
  46. }
  47. static void gpio_print_pins(void) {
  48. printf("Wrong pin name. Available pins: ");
  49. bool debug = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
  50. for(size_t i = 0; i < COUNT_OF(cli_command_gpio_pins); i++) {
  51. if(!cli_command_gpio_pins[i].debug || debug) {
  52. printf("%s ", cli_command_gpio_pins[i].name);
  53. }
  54. }
  55. }
  56. typedef enum { OK, ERR_CMD_SYNTAX, ERR_PIN, ERR_VALUE } GpioParseError;
  57. static GpioParseError gpio_command_parse(FuriString* args, size_t* pin_num, uint8_t* value) {
  58. FuriString* pin_name;
  59. pin_name = furi_string_alloc();
  60. size_t ws = furi_string_search_char(args, ' ');
  61. if(ws == FURI_STRING_FAILURE) {
  62. return ERR_CMD_SYNTAX;
  63. }
  64. furi_string_set_n(pin_name, args, 0, ws);
  65. furi_string_right(args, ws);
  66. furi_string_trim(args);
  67. if(!pin_name_to_int(pin_name, pin_num)) {
  68. furi_string_free(pin_name);
  69. return ERR_PIN;
  70. }
  71. furi_string_free(pin_name);
  72. if(!furi_string_cmp(args, "0")) {
  73. *value = 0;
  74. } else if(!furi_string_cmp(args, "1")) {
  75. *value = 1;
  76. } else {
  77. return ERR_VALUE;
  78. }
  79. return OK;
  80. }
  81. void cli_command_gpio_mode(Cli* cli, FuriString* args, void* context) {
  82. UNUSED(cli);
  83. UNUSED(context);
  84. size_t num = 0;
  85. uint8_t value = 255;
  86. GpioParseError err = gpio_command_parse(args, &num, &value);
  87. if(ERR_CMD_SYNTAX == err) {
  88. cli_print_usage("gpio mode", "<pin_name> <0|1>", furi_string_get_cstr(args));
  89. return;
  90. } else if(ERR_PIN == err) {
  91. gpio_print_pins();
  92. return;
  93. } else if(ERR_VALUE == err) {
  94. printf("Value is invalid. Enter 1 for input or 0 for output");
  95. return;
  96. }
  97. if(cli_command_gpio_pins[num].debug) {
  98. printf(
  99. "Changeing this pin mode may damage hardware. Are you sure you want to continue? (y/n)?\r\n");
  100. char c = cli_getc(cli);
  101. if(c != 'y' && c != 'Y') {
  102. printf("Cancelled.\r\n");
  103. return;
  104. }
  105. }
  106. if(value == 1) { // output
  107. furi_hal_gpio_write(cli_command_gpio_pins[num].pin, false);
  108. furi_hal_gpio_init_simple(cli_command_gpio_pins[num].pin, GpioModeOutputPushPull);
  109. printf("Pin %s is now an output (low)", cli_command_gpio_pins[num].name);
  110. } else { // input
  111. furi_hal_gpio_init_simple(cli_command_gpio_pins[num].pin, GpioModeInput);
  112. printf("Pin %s is now an input", cli_command_gpio_pins[num].name);
  113. }
  114. }
  115. void cli_command_gpio_read(Cli* cli, FuriString* args, void* context) {
  116. UNUSED(cli);
  117. UNUSED(context);
  118. size_t num = 0;
  119. if(!pin_name_to_int(args, &num)) {
  120. gpio_print_pins();
  121. return;
  122. }
  123. if(LL_GPIO_MODE_INPUT !=
  124. LL_GPIO_GetPinMode(
  125. cli_command_gpio_pins[num].pin->port, cli_command_gpio_pins[num].pin->pin)) {
  126. printf("Err: pin %s is not set as an input.", cli_command_gpio_pins[num].name);
  127. return;
  128. }
  129. uint8_t val = !!furi_hal_gpio_read(cli_command_gpio_pins[num].pin);
  130. printf("Pin %s <= %u", cli_command_gpio_pins[num].name, val);
  131. }
  132. void cli_command_gpio_set(Cli* cli, FuriString* args, void* context) {
  133. UNUSED(context);
  134. size_t num = 0;
  135. uint8_t value = 0;
  136. GpioParseError err = gpio_command_parse(args, &num, &value);
  137. if(ERR_CMD_SYNTAX == err) {
  138. cli_print_usage("gpio set", "<pin_name> <0|1>", furi_string_get_cstr(args));
  139. return;
  140. } else if(ERR_PIN == err) {
  141. gpio_print_pins();
  142. return;
  143. } else if(ERR_VALUE == err) {
  144. printf("Value is invalid. Enter 1 for high or 0 for low");
  145. return;
  146. }
  147. if(LL_GPIO_MODE_OUTPUT !=
  148. LL_GPIO_GetPinMode(
  149. cli_command_gpio_pins[num].pin->port, cli_command_gpio_pins[num].pin->pin)) {
  150. printf("Err: pin %s is not set as an output.", cli_command_gpio_pins[num].name);
  151. return;
  152. }
  153. // Extra check if debug pins used
  154. if(cli_command_gpio_pins[num].debug) {
  155. printf(
  156. "Setting this pin may damage hardware. Are you sure you want to continue? (y/n)?\r\n");
  157. char c = cli_getc(cli);
  158. if(c != 'y' && c != 'Y') {
  159. printf("Cancelled.\r\n");
  160. return;
  161. }
  162. }
  163. furi_hal_gpio_write(cli_command_gpio_pins[num].pin, !!value);
  164. printf("Pin %s => %u", cli_command_gpio_pins[num].name, !!value);
  165. }
  166. void cli_command_gpio(Cli* cli, FuriString* args, void* context) {
  167. FuriString* cmd;
  168. cmd = furi_string_alloc();
  169. do {
  170. if(!args_read_string_and_trim(args, cmd)) {
  171. cli_command_gpio_print_usage();
  172. break;
  173. }
  174. if(furi_string_cmp_str(cmd, "mode") == 0) {
  175. cli_command_gpio_mode(cli, args, context);
  176. break;
  177. }
  178. if(furi_string_cmp_str(cmd, "set") == 0) {
  179. cli_command_gpio_set(cli, args, context);
  180. break;
  181. }
  182. if(furi_string_cmp_str(cmd, "read") == 0) {
  183. cli_command_gpio_read(cli, args, context);
  184. break;
  185. }
  186. cli_command_gpio_print_usage();
  187. } while(false);
  188. furi_string_free(cmd);
  189. }