gpio_scene_usb_uart_config.c 5.9 KB

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