usb_uart_bridge.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "usb_uart_bridge.h"
  2. #include "furi-hal.h"
  3. #include <stream_buffer.h>
  4. #include <furi-hal-usb-cdc_i.h>
  5. #include "usb_cdc.h"
  6. #define USB_PKT_LEN CDC_DATA_SZ
  7. #define USB_UART_RX_BUF_SIZE (USB_PKT_LEN * 3)
  8. #define USB_UART_TX_BUF_SIZE (USB_PKT_LEN * 3)
  9. typedef enum {
  10. WorkerEvtStop = (1 << 0),
  11. WorkerEvtRxReady = (1 << 1),
  12. WorkerEvtTxStop = (1 << 2),
  13. WorkerEvtTxReady = (1 << 3),
  14. WorkerEvtSof = (1 << 4),
  15. } WorkerEvtFlags;
  16. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxReady)
  17. #define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtTxReady)
  18. typedef struct {
  19. UsbUartConfig cfg;
  20. FuriThread* thread;
  21. FuriThread* tx_thread;
  22. osEventFlagsId_t events;
  23. StreamBufferHandle_t rx_stream;
  24. StreamBufferHandle_t tx_stream;
  25. uint8_t rx_buf[USB_PKT_LEN];
  26. uint8_t tx_buf[USB_PKT_LEN];
  27. bool buf_full;
  28. } UsbUartParams;
  29. static UsbUartParams* usb_uart;
  30. static bool running = false;
  31. static void vcp_on_cdc_tx_complete();
  32. static void vcp_on_cdc_rx();
  33. static void vcp_state_callback(uint8_t state);
  34. static void vcp_on_cdc_control_line(uint8_t state);
  35. static void vcp_on_line_config(struct usb_cdc_line_coding* config);
  36. static CdcCallbacks cdc_cb = {
  37. vcp_on_cdc_tx_complete,
  38. vcp_on_cdc_rx,
  39. vcp_state_callback,
  40. vcp_on_cdc_control_line,
  41. vcp_on_line_config,
  42. };
  43. /* USB UART worker */
  44. static int32_t usb_uart_tx_thread(void* context);
  45. static void usb_uart_on_irq_cb(UartIrqEvent ev, uint8_t data) {
  46. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  47. if(ev == UartIrqEventRXNE) {
  48. xStreamBufferSendFromISR(usb_uart->rx_stream, &data, 1, &xHigherPriorityTaskWoken);
  49. size_t ret = xStreamBufferBytesAvailable(usb_uart->rx_stream);
  50. if(ret > USB_PKT_LEN) osEventFlagsSet(usb_uart->events, WorkerEvtRxReady);
  51. } else if(ev == UartIrqEventIDLE) {
  52. osEventFlagsSet(usb_uart->events, WorkerEvtRxReady);
  53. }
  54. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  55. }
  56. static int32_t usb_uart_worker(void* context) {
  57. memcpy(&usb_uart->cfg, context, sizeof(UsbUartConfig));
  58. usb_uart->rx_stream = xStreamBufferCreate(USB_UART_RX_BUF_SIZE, 1);
  59. usb_uart->tx_stream = xStreamBufferCreate(USB_UART_TX_BUF_SIZE, 1);
  60. usb_uart->tx_thread = furi_thread_alloc();
  61. furi_thread_set_name(usb_uart->tx_thread, "usb_uart_tx");
  62. furi_thread_set_stack_size(usb_uart->tx_thread, 512);
  63. furi_thread_set_context(usb_uart->tx_thread, NULL);
  64. furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
  65. UsbMode usb_mode_prev = furi_hal_usb_get_config();
  66. if(usb_uart->cfg.vcp_ch == 0) {
  67. furi_hal_usb_set_config(UsbModeVcpSingle);
  68. furi_hal_vcp_disable();
  69. osEventFlagsSet(usb_uart->events, WorkerEvtSof);
  70. } else {
  71. furi_hal_usb_set_config(UsbModeVcpDual);
  72. }
  73. if(usb_uart->cfg.uart_ch == FuriHalUartIdUSART1) {
  74. furi_hal_console_disable();
  75. } else if(usb_uart->cfg.uart_ch == FuriHalUartIdLPUART1) {
  76. furi_hal_uart_init(usb_uart->cfg.uart_ch, 115200);
  77. furi_hal_uart_set_irq_cb(usb_uart->cfg.uart_ch, usb_uart_on_irq_cb);
  78. }
  79. furi_hal_uart_set_irq_cb(usb_uart->cfg.uart_ch, usb_uart_on_irq_cb);
  80. if(usb_uart->cfg.baudrate != 0)
  81. furi_hal_uart_set_br(usb_uart->cfg.uart_ch, usb_uart->cfg.baudrate);
  82. else
  83. vcp_on_line_config(furi_hal_cdc_get_port_settings(usb_uart->cfg.vcp_ch));
  84. furi_hal_cdc_set_callbacks(usb_uart->cfg.vcp_ch, &cdc_cb);
  85. furi_thread_start(usb_uart->tx_thread);
  86. while(1) {
  87. uint32_t events = osEventFlagsWait(
  88. usb_uart->events, WORKER_ALL_RX_EVENTS, osFlagsWaitAny, osWaitForever);
  89. furi_check((events & osFlagsError) == 0);
  90. if(events & WorkerEvtStop) break;
  91. if(events & WorkerEvtRxReady) {
  92. size_t len = 0;
  93. do {
  94. len = xStreamBufferReceive(usb_uart->rx_stream, usb_uart->rx_buf, USB_PKT_LEN, 0);
  95. if(len > 0) {
  96. if((osEventFlagsWait(usb_uart->events, WorkerEvtSof, osFlagsWaitAny, 100) &
  97. osFlagsError) == 0)
  98. furi_hal_cdc_send(usb_uart->cfg.vcp_ch, usb_uart->rx_buf, len);
  99. else
  100. xStreamBufferReset(usb_uart->rx_stream);
  101. }
  102. } while(len > 0);
  103. }
  104. }
  105. osEventFlagsSet(usb_uart->events, WorkerEvtTxStop);
  106. furi_thread_join(usb_uart->tx_thread);
  107. furi_thread_free(usb_uart->tx_thread);
  108. if(usb_uart->cfg.uart_ch == FuriHalUartIdUSART1)
  109. furi_hal_console_enable();
  110. else if(usb_uart->cfg.uart_ch == FuriHalUartIdLPUART1)
  111. furi_hal_uart_deinit(usb_uart->cfg.uart_ch);
  112. furi_hal_cdc_set_callbacks(usb_uart->cfg.vcp_ch, NULL);
  113. furi_hal_usb_set_config(usb_mode_prev);
  114. if(usb_uart->cfg.vcp_ch == 0) furi_hal_vcp_enable();
  115. vStreamBufferDelete(usb_uart->rx_stream);
  116. vStreamBufferDelete(usb_uart->tx_stream);
  117. return 0;
  118. }
  119. static int32_t usb_uart_tx_thread(void* context) {
  120. uint8_t data[USB_PKT_LEN];
  121. while(1) {
  122. uint32_t events = osEventFlagsWait(
  123. usb_uart->events, WORKER_ALL_TX_EVENTS, osFlagsWaitAny, osWaitForever);
  124. furi_check((events & osFlagsError) == 0);
  125. if(events & WorkerEvtTxStop) break;
  126. if(events & WorkerEvtTxReady) {
  127. size_t len = 0;
  128. do {
  129. len = xStreamBufferReceive(usb_uart->tx_stream, &data, 1, 0);
  130. if(len > 0) {
  131. furi_hal_uart_tx(usb_uart->cfg.uart_ch, data, len);
  132. }
  133. if((usb_uart->buf_full == true) &&
  134. (xStreamBufferBytesAvailable(usb_uart->tx_stream) == 0)) {
  135. // Stream buffer was overflown, but now is free. Reading USB buffer to resume USB transfers
  136. usb_uart->buf_full = false;
  137. int32_t size = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, data, USB_PKT_LEN);
  138. if(size > 0) {
  139. furi_hal_uart_tx(usb_uart->cfg.uart_ch, data, size);
  140. }
  141. }
  142. } while(len > 0);
  143. }
  144. }
  145. return 0;
  146. }
  147. /* VCP callbacks */
  148. static void vcp_on_cdc_tx_complete() {
  149. osEventFlagsSet(usb_uart->events, WorkerEvtSof);
  150. }
  151. static void vcp_on_cdc_rx() {
  152. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  153. uint16_t max_len = xStreamBufferSpacesAvailable(usb_uart->tx_stream);
  154. if(max_len >= USB_PKT_LEN) {
  155. //if(max_len > USB_PKT_LEN) max_len = USB_PKT_LEN;
  156. int32_t size = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, usb_uart->tx_buf, USB_PKT_LEN);
  157. if(size > 0) {
  158. size_t ret = xStreamBufferSendFromISR(
  159. usb_uart->tx_stream, usb_uart->tx_buf, size, &xHigherPriorityTaskWoken);
  160. furi_check(ret == size);
  161. }
  162. } else {
  163. usb_uart->buf_full = true;
  164. }
  165. osEventFlagsSet(usb_uart->events, WorkerEvtTxReady);
  166. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  167. }
  168. static void vcp_state_callback(uint8_t state) {
  169. }
  170. static void vcp_on_cdc_control_line(uint8_t state) {
  171. }
  172. static void vcp_on_line_config(struct usb_cdc_line_coding* config) {
  173. if((usb_uart->cfg.baudrate == 0) && (config->dwDTERate != 0))
  174. furi_hal_uart_set_br(usb_uart->cfg.uart_ch, config->dwDTERate);
  175. }
  176. void usb_uart_enable(UsbUartConfig* cfg) {
  177. if(running == false) {
  178. running = true;
  179. usb_uart = furi_alloc(sizeof(UsbUartParams));
  180. usb_uart->thread = furi_thread_alloc();
  181. furi_thread_set_name(usb_uart->thread, "usb_uart");
  182. furi_thread_set_stack_size(usb_uart->thread, 1024);
  183. furi_thread_set_context(usb_uart->thread, cfg);
  184. furi_thread_set_callback(usb_uart->thread, usb_uart_worker);
  185. usb_uart->events = osEventFlagsNew(NULL);
  186. furi_thread_start(usb_uart->thread);
  187. }
  188. }
  189. void usb_uart_disable() {
  190. if(running == true) {
  191. osEventFlagsSet(usb_uart->events, WorkerEvtStop);
  192. furi_thread_join(usb_uart->thread);
  193. furi_thread_free(usb_uart->thread);
  194. osEventFlagsDelete(usb_uart->events);
  195. free(usb_uart);
  196. running = false;
  197. }
  198. }