usb_uart_bridge.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_CDC_PKT_LEN CDC_DATA_SZ
  7. #define USB_UART_RX_BUF_SIZE (USB_CDC_PKT_LEN * 5)
  8. typedef enum {
  9. WorkerEvtStop = (1 << 0),
  10. WorkerEvtRxDone = (1 << 1),
  11. WorkerEvtTxStop = (1 << 2),
  12. WorkerEvtCdcRx = (1 << 3),
  13. } WorkerEvtFlags;
  14. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  15. #define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtCdcRx)
  16. typedef struct {
  17. UsbUartConfig cfg;
  18. FuriThread* thread;
  19. FuriThread* tx_thread;
  20. StreamBufferHandle_t rx_stream;
  21. osMutexId_t usb_mutex;
  22. osSemaphoreId_t tx_sem;
  23. uint8_t rx_buf[USB_CDC_PKT_LEN];
  24. bool buf_full;
  25. } UsbUartParams;
  26. static UsbUartParams* usb_uart;
  27. static bool running = false;
  28. static void vcp_on_cdc_tx_complete();
  29. static void vcp_on_cdc_rx();
  30. static void vcp_state_callback(uint8_t state);
  31. static void vcp_on_cdc_control_line(uint8_t state);
  32. static void vcp_on_line_config(struct usb_cdc_line_coding* config);
  33. static CdcCallbacks cdc_cb = {
  34. vcp_on_cdc_tx_complete,
  35. vcp_on_cdc_rx,
  36. vcp_state_callback,
  37. vcp_on_cdc_control_line,
  38. vcp_on_line_config,
  39. };
  40. /* USB UART worker */
  41. static int32_t usb_uart_tx_thread(void* context);
  42. static void usb_uart_on_irq_cb(UartIrqEvent ev, uint8_t data) {
  43. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  44. if(ev == UartIrqEventRXNE) {
  45. xStreamBufferSendFromISR(usb_uart->rx_stream, &data, 1, &xHigherPriorityTaskWoken);
  46. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  47. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtRxDone);
  48. }
  49. }
  50. static int32_t usb_uart_worker(void* context) {
  51. memcpy(&usb_uart->cfg, context, sizeof(UsbUartConfig));
  52. usb_uart->rx_stream = xStreamBufferCreate(USB_UART_RX_BUF_SIZE, 1);
  53. usb_uart->tx_sem = osSemaphoreNew(1, 1, NULL);
  54. usb_uart->usb_mutex = osMutexNew(NULL);
  55. usb_uart->tx_thread = furi_thread_alloc();
  56. furi_thread_set_name(usb_uart->tx_thread, "usb_uart_tx");
  57. furi_thread_set_stack_size(usb_uart->tx_thread, 512);
  58. furi_thread_set_context(usb_uart->tx_thread, NULL);
  59. furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
  60. UsbMode usb_mode_prev = furi_hal_usb_get_config();
  61. if(usb_uart->cfg.vcp_ch == 0) {
  62. furi_hal_usb_set_config(UsbModeVcpSingle);
  63. furi_hal_vcp_disable();
  64. } else {
  65. furi_hal_usb_set_config(UsbModeVcpDual);
  66. }
  67. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtCdcRx);
  68. if(usb_uart->cfg.uart_ch == FuriHalUartIdUSART1) {
  69. furi_hal_console_disable();
  70. } else if(usb_uart->cfg.uart_ch == FuriHalUartIdLPUART1) {
  71. furi_hal_uart_init(usb_uart->cfg.uart_ch, 115200);
  72. furi_hal_uart_set_irq_cb(usb_uart->cfg.uart_ch, usb_uart_on_irq_cb);
  73. }
  74. furi_hal_uart_set_irq_cb(usb_uart->cfg.uart_ch, usb_uart_on_irq_cb);
  75. if(usb_uart->cfg.baudrate != 0)
  76. furi_hal_uart_set_br(usb_uart->cfg.uart_ch, usb_uart->cfg.baudrate);
  77. else
  78. vcp_on_line_config(furi_hal_cdc_get_port_settings(usb_uart->cfg.vcp_ch));
  79. furi_hal_cdc_set_callbacks(usb_uart->cfg.vcp_ch, &cdc_cb);
  80. furi_thread_start(usb_uart->tx_thread);
  81. while(1) {
  82. uint32_t events = osThreadFlagsWait(WORKER_ALL_RX_EVENTS, osFlagsWaitAny, osWaitForever);
  83. furi_check((events & osFlagsError) == 0);
  84. if(events & WorkerEvtStop) break;
  85. if(events & WorkerEvtRxDone) {
  86. size_t len =
  87. xStreamBufferReceive(usb_uart->rx_stream, usb_uart->rx_buf, USB_CDC_PKT_LEN, 0);
  88. if(len > 0) {
  89. if(osSemaphoreAcquire(usb_uart->tx_sem, 100) == osOK) {
  90. furi_check(osMutexAcquire(usb_uart->usb_mutex, osWaitForever) == osOK);
  91. furi_hal_cdc_send(usb_uart->cfg.vcp_ch, usb_uart->rx_buf, len);
  92. furi_check(osMutexRelease(usb_uart->usb_mutex) == osOK);
  93. } else {
  94. xStreamBufferReset(usb_uart->rx_stream);
  95. }
  96. }
  97. }
  98. }
  99. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtTxStop);
  100. furi_thread_join(usb_uart->tx_thread);
  101. furi_thread_free(usb_uart->tx_thread);
  102. if(usb_uart->cfg.uart_ch == FuriHalUartIdUSART1)
  103. furi_hal_console_enable();
  104. else if(usb_uart->cfg.uart_ch == FuriHalUartIdLPUART1)
  105. furi_hal_uart_deinit(usb_uart->cfg.uart_ch);
  106. furi_hal_cdc_set_callbacks(usb_uart->cfg.vcp_ch, NULL);
  107. furi_hal_usb_set_config(usb_mode_prev);
  108. if(usb_uart->cfg.vcp_ch == 0) furi_hal_vcp_enable();
  109. vStreamBufferDelete(usb_uart->rx_stream);
  110. osMutexDelete(usb_uart->usb_mutex);
  111. osSemaphoreDelete(usb_uart->tx_sem);
  112. return 0;
  113. }
  114. static int32_t usb_uart_tx_thread(void* context) {
  115. uint8_t data[USB_CDC_PKT_LEN];
  116. while(1) {
  117. uint32_t events = osThreadFlagsWait(WORKER_ALL_TX_EVENTS, osFlagsWaitAny, osWaitForever);
  118. furi_check((events & osFlagsError) == 0);
  119. if(events & WorkerEvtTxStop) break;
  120. if(events & WorkerEvtCdcRx) {
  121. furi_check(osMutexAcquire(usb_uart->usb_mutex, osWaitForever) == osOK);
  122. int32_t size = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, data, USB_CDC_PKT_LEN);
  123. furi_check(osMutexRelease(usb_uart->usb_mutex) == osOK);
  124. if(size > 0) {
  125. furi_hal_uart_tx(usb_uart->cfg.uart_ch, data, size);
  126. }
  127. }
  128. }
  129. return 0;
  130. }
  131. /* VCP callbacks */
  132. static void vcp_on_cdc_tx_complete() {
  133. osSemaphoreRelease(usb_uart->tx_sem);
  134. }
  135. static void vcp_on_cdc_rx() {
  136. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtCdcRx);
  137. }
  138. static void vcp_state_callback(uint8_t state) {
  139. }
  140. static void vcp_on_cdc_control_line(uint8_t state) {
  141. }
  142. static void vcp_on_line_config(struct usb_cdc_line_coding* config) {
  143. if((usb_uart->cfg.baudrate == 0) && (config->dwDTERate != 0))
  144. furi_hal_uart_set_br(usb_uart->cfg.uart_ch, config->dwDTERate);
  145. }
  146. void usb_uart_enable(UsbUartConfig* cfg) {
  147. if(running == false) {
  148. running = true;
  149. usb_uart = furi_alloc(sizeof(UsbUartParams));
  150. usb_uart->thread = furi_thread_alloc();
  151. furi_thread_set_name(usb_uart->thread, "usb_uart");
  152. furi_thread_set_stack_size(usb_uart->thread, 1024);
  153. furi_thread_set_context(usb_uart->thread, cfg);
  154. furi_thread_set_callback(usb_uart->thread, usb_uart_worker);
  155. furi_thread_start(usb_uart->thread);
  156. }
  157. }
  158. void usb_uart_disable() {
  159. if(running == true) {
  160. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtStop);
  161. furi_thread_join(usb_uart->thread);
  162. furi_thread_free(usb_uart->thread);
  163. free(usb_uart);
  164. running = false;
  165. }
  166. }