usb_uart_bridge.c 6.8 KB

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