gpio_scene_usb_uart_config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "../usb_uart_bridge.h"
  2. #include "../gpio_app_i.h"
  3. #include <furi_hal.h>
  4. typedef enum {
  5. UsbUartLineIndexVcp,
  6. UsbUartLineIndexBaudrate,
  7. UsbUartLineIndexUart,
  8. UsbUartLineIndexFlow,
  9. } LineIndex;
  10. static const char* vcp_ch[] = {"0 (CLI)", "1"};
  11. static const char* uart_ch[] = {"13,14", "15,16"};
  12. static const char* flow_pins[] = {"None", "2,3", "6,7", "16,15"};
  13. static const char* baudrate_mode[] = {"Host"};
  14. static const uint32_t baudrate_list[] = {
  15. 1200,
  16. 2400,
  17. 4800,
  18. 9600,
  19. 19200,
  20. 28800,
  21. 38400,
  22. 57600,
  23. 115200,
  24. 230400,
  25. 460800,
  26. 921600,
  27. };
  28. bool gpio_scene_usb_uart_cfg_on_event(void* context, SceneManagerEvent event) {
  29. GpioApp* app = context;
  30. furi_assert(app);
  31. if(event.type == SceneManagerEventTypeCustom) {
  32. if(event.event == GpioUsbUartEventConfigSet) {
  33. usb_uart_set_config(app->usb_uart_bridge, app->usb_uart_cfg);
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. void line_ensure_flow_invariant(GpioApp* app) {
  40. // GPIO pins PC0, PC1 (16,15) are unavailable for RTS/DTR when LPUART is
  41. // selected. This function enforces that invariant by resetting flow_pins
  42. // to None if it is configured to 16,15 when LPUART is selected.
  43. uint8_t available_flow_pins = app->usb_uart_cfg->uart_ch == FuriHalUartIdLPUART1 ? 3 : 4;
  44. VariableItem* item = app->var_item_flow;
  45. variable_item_set_values_count(item, available_flow_pins);
  46. if(app->usb_uart_cfg->flow_pins >= available_flow_pins) {
  47. app->usb_uart_cfg->flow_pins = 0;
  48. variable_item_set_current_value_index(item, app->usb_uart_cfg->flow_pins);
  49. variable_item_set_current_value_text(item, flow_pins[app->usb_uart_cfg->flow_pins]);
  50. }
  51. }
  52. static void line_vcp_cb(VariableItem* item) {
  53. GpioApp* app = variable_item_get_context(item);
  54. furi_assert(app);
  55. uint8_t index = variable_item_get_current_value_index(item);
  56. variable_item_set_current_value_text(item, vcp_ch[index]);
  57. app->usb_uart_cfg->vcp_ch = index;
  58. view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
  59. }
  60. static void line_port_cb(VariableItem* item) {
  61. GpioApp* app = variable_item_get_context(item);
  62. furi_assert(app);
  63. uint8_t index = variable_item_get_current_value_index(item);
  64. variable_item_set_current_value_text(item, uart_ch[index]);
  65. if(index == 0)
  66. app->usb_uart_cfg->uart_ch = FuriHalUartIdUSART1;
  67. else if(index == 1)
  68. app->usb_uart_cfg->uart_ch = FuriHalUartIdLPUART1;
  69. line_ensure_flow_invariant(app);
  70. view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
  71. }
  72. static void line_flow_cb(VariableItem* item) {
  73. GpioApp* app = variable_item_get_context(item);
  74. furi_assert(app);
  75. uint8_t index = variable_item_get_current_value_index(item);
  76. variable_item_set_current_value_text(item, flow_pins[index]);
  77. app->usb_uart_cfg->flow_pins = index;
  78. view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
  79. }
  80. static void line_baudrate_cb(VariableItem* item) {
  81. GpioApp* app = variable_item_get_context(item);
  82. furi_assert(app);
  83. uint8_t index = variable_item_get_current_value_index(item);
  84. char br_text[8];
  85. if(index > 0) {
  86. snprintf(br_text, 7, "%lu", baudrate_list[index - 1]);
  87. variable_item_set_current_value_text(item, br_text);
  88. app->usb_uart_cfg->baudrate = baudrate_list[index - 1];
  89. } else {
  90. variable_item_set_current_value_text(item, baudrate_mode[index]);
  91. app->usb_uart_cfg->baudrate = 0;
  92. }
  93. app->usb_uart_cfg->baudrate_mode = index;
  94. view_dispatcher_send_custom_event(app->view_dispatcher, GpioUsbUartEventConfigSet);
  95. }
  96. void gpio_scene_usb_uart_cfg_on_enter(void* context) {
  97. GpioApp* app = context;
  98. furi_assert(app);
  99. VariableItemList* var_item_list = app->var_item_list;
  100. app->usb_uart_cfg = malloc(sizeof(UsbUartConfig));
  101. usb_uart_get_config(app->usb_uart_bridge, app->usb_uart_cfg);
  102. VariableItem* item;
  103. char br_text[8];
  104. item = variable_item_list_add(var_item_list, "USB Channel", 2, line_vcp_cb, app);
  105. variable_item_set_current_value_index(item, app->usb_uart_cfg->vcp_ch);
  106. variable_item_set_current_value_text(item, vcp_ch[app->usb_uart_cfg->vcp_ch]);
  107. item = variable_item_list_add(
  108. var_item_list,
  109. "Baudrate",
  110. sizeof(baudrate_list) / sizeof(baudrate_list[0]) + 1,
  111. line_baudrate_cb,
  112. app);
  113. variable_item_set_current_value_index(item, app->usb_uart_cfg->baudrate_mode);
  114. if(app->usb_uart_cfg->baudrate_mode > 0) {
  115. snprintf(br_text, 7, "%lu", baudrate_list[app->usb_uart_cfg->baudrate_mode - 1]);
  116. variable_item_set_current_value_text(item, br_text);
  117. } else {
  118. variable_item_set_current_value_text(
  119. item, baudrate_mode[app->usb_uart_cfg->baudrate_mode]);
  120. }
  121. item = variable_item_list_add(var_item_list, "UART Pins", 2, line_port_cb, app);
  122. variable_item_set_current_value_index(item, app->usb_uart_cfg->uart_ch);
  123. variable_item_set_current_value_text(item, uart_ch[app->usb_uart_cfg->uart_ch]);
  124. item = variable_item_list_add(
  125. var_item_list, "RTS/DTR Pins", COUNT_OF(flow_pins), line_flow_cb, app);
  126. variable_item_set_current_value_index(item, app->usb_uart_cfg->flow_pins);
  127. variable_item_set_current_value_text(item, flow_pins[app->usb_uart_cfg->flow_pins]);
  128. app->var_item_flow = item;
  129. line_ensure_flow_invariant(app);
  130. variable_item_list_set_selected_item(
  131. var_item_list, scene_manager_get_scene_state(app->scene_manager, GpioAppViewUsbUartCfg));
  132. view_dispatcher_switch_to_view(app->view_dispatcher, GpioAppViewUsbUartCfg);
  133. }
  134. void gpio_scene_usb_uart_cfg_on_exit(void* context) {
  135. GpioApp* app = context;
  136. scene_manager_set_scene_state(
  137. app->scene_manager,
  138. GpioAppViewUsbUartCfg,
  139. variable_item_list_get_selected_item_index(app->var_item_list));
  140. variable_item_list_reset(app->var_item_list);
  141. free(app->usb_uart_cfg);
  142. }