usb_uart_bridge.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #define USB_CDC_BIT_DTR (1 << 0)
  9. #define USB_CDC_BIT_RTS (1 << 1)
  10. static const GpioPin* flow_pins[][2] = {
  11. {&gpio_ext_pa7, &gpio_ext_pa6}, // 2, 3
  12. {&gpio_ext_pb2, &gpio_ext_pc3}, // 6, 7
  13. };
  14. typedef enum {
  15. WorkerEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
  16. WorkerEvtStop = (1 << 1),
  17. WorkerEvtRxDone = (1 << 2),
  18. WorkerEvtTxStop = (1 << 3),
  19. WorkerEvtCdcRx = (1 << 4),
  20. WorkerEvtCfgChange = (1 << 5),
  21. WorkerEvtLineCfgSet = (1 << 6),
  22. WorkerEvtCtrlLineSet = (1 << 7),
  23. } WorkerEvtFlags;
  24. #define WORKER_ALL_RX_EVENTS \
  25. (WorkerEvtStop | WorkerEvtRxDone | WorkerEvtCfgChange | WorkerEvtLineCfgSet | \
  26. WorkerEvtCtrlLineSet)
  27. #define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtCdcRx)
  28. struct UsbUartBridge {
  29. UsbUartConfig cfg;
  30. UsbUartConfig cfg_new;
  31. FuriThread* thread;
  32. FuriThread* tx_thread;
  33. StreamBufferHandle_t rx_stream;
  34. osMutexId_t usb_mutex;
  35. osSemaphoreId_t tx_sem;
  36. UsbUartState st;
  37. uint8_t rx_buf[USB_CDC_PKT_LEN];
  38. };
  39. static void vcp_on_cdc_tx_complete(void* context);
  40. static void vcp_on_cdc_rx(void* context);
  41. static void vcp_state_callback(void* context, uint8_t state);
  42. static void vcp_on_cdc_control_line(void* context, uint8_t state);
  43. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config);
  44. static const CdcCallbacks cdc_cb = {
  45. vcp_on_cdc_tx_complete,
  46. vcp_on_cdc_rx,
  47. vcp_state_callback,
  48. vcp_on_cdc_control_line,
  49. vcp_on_line_config,
  50. };
  51. /* USB UART worker */
  52. static int32_t usb_uart_tx_thread(void* context);
  53. static void usb_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  54. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  55. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  56. if(ev == UartIrqEventRXNE) {
  57. xStreamBufferSendFromISR(usb_uart->rx_stream, &data, 1, &xHigherPriorityTaskWoken);
  58. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtRxDone);
  59. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  60. }
  61. }
  62. static void usb_uart_vcp_init(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
  63. furi_hal_usb_unlock();
  64. if(vcp_ch == 0) {
  65. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  66. furi_hal_vcp_disable();
  67. } else {
  68. furi_check(furi_hal_usb_set_config(&usb_cdc_dual, NULL) == true);
  69. }
  70. furi_hal_cdc_set_callbacks(vcp_ch, (CdcCallbacks*)&cdc_cb, usb_uart);
  71. }
  72. static void usb_uart_vcp_deinit(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
  73. furi_hal_cdc_set_callbacks(vcp_ch, NULL, NULL);
  74. if(vcp_ch == 0) furi_hal_vcp_enable();
  75. }
  76. static void usb_uart_serial_init(UsbUartBridge* usb_uart, uint8_t uart_ch) {
  77. if(uart_ch == FuriHalUartIdUSART1) {
  78. furi_hal_console_disable();
  79. } else if(uart_ch == FuriHalUartIdLPUART1) {
  80. furi_hal_uart_init(uart_ch, 115200);
  81. }
  82. furi_hal_uart_set_irq_cb(uart_ch, usb_uart_on_irq_cb, usb_uart);
  83. }
  84. static void usb_uart_serial_deinit(UsbUartBridge* usb_uart, uint8_t uart_ch) {
  85. furi_hal_uart_set_irq_cb(uart_ch, NULL, NULL);
  86. if(uart_ch == FuriHalUartIdUSART1)
  87. furi_hal_console_enable();
  88. else if(uart_ch == FuriHalUartIdLPUART1)
  89. furi_hal_uart_deinit(uart_ch);
  90. }
  91. static void usb_uart_set_baudrate(UsbUartBridge* usb_uart, uint32_t baudrate) {
  92. if(baudrate != 0) {
  93. furi_hal_uart_set_br(usb_uart->cfg.uart_ch, baudrate);
  94. usb_uart->st.baudrate_cur = baudrate;
  95. } else {
  96. struct usb_cdc_line_coding* line_cfg =
  97. furi_hal_cdc_get_port_settings(usb_uart->cfg.vcp_ch);
  98. if(line_cfg->dwDTERate > 0) {
  99. furi_hal_uart_set_br(usb_uart->cfg.uart_ch, line_cfg->dwDTERate);
  100. usb_uart->st.baudrate_cur = line_cfg->dwDTERate;
  101. }
  102. }
  103. }
  104. static void usb_uart_update_ctrl_lines(UsbUartBridge* usb_uart) {
  105. if(usb_uart->cfg.flow_pins != 0) {
  106. furi_assert((usb_uart->cfg.flow_pins - 1) < (sizeof(flow_pins) / sizeof(flow_pins[0])));
  107. uint8_t state = furi_hal_cdc_get_ctrl_line_state(usb_uart->cfg.vcp_ch);
  108. hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][0], !(state & USB_CDC_BIT_RTS));
  109. hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][1], !(state & USB_CDC_BIT_DTR));
  110. }
  111. }
  112. static int32_t usb_uart_worker(void* context) {
  113. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  114. memcpy(&usb_uart->cfg, &usb_uart->cfg_new, sizeof(UsbUartConfig));
  115. usb_uart->rx_stream = xStreamBufferCreate(USB_UART_RX_BUF_SIZE, 1);
  116. usb_uart->tx_sem = osSemaphoreNew(1, 1, NULL);
  117. usb_uart->usb_mutex = osMutexNew(NULL);
  118. usb_uart->tx_thread = furi_thread_alloc();
  119. furi_thread_set_name(usb_uart->tx_thread, "UsbUartTxWorker");
  120. furi_thread_set_stack_size(usb_uart->tx_thread, 512);
  121. furi_thread_set_context(usb_uart->tx_thread, usb_uart);
  122. furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
  123. FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
  124. usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
  125. usb_uart_serial_init(usb_uart, usb_uart->cfg.uart_ch);
  126. usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
  127. if(usb_uart->cfg.flow_pins != 0) {
  128. furi_assert((usb_uart->cfg.flow_pins - 1) < (sizeof(flow_pins) / sizeof(flow_pins[0])));
  129. hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeOutputPushPull);
  130. hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeOutputPushPull);
  131. usb_uart_update_ctrl_lines(usb_uart);
  132. }
  133. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtCdcRx);
  134. furi_thread_start(usb_uart->tx_thread);
  135. while(1) {
  136. uint32_t events = osThreadFlagsWait(WORKER_ALL_RX_EVENTS, osFlagsWaitAny, osWaitForever);
  137. furi_check((events & osFlagsError) == 0);
  138. if(events & WorkerEvtStop) break;
  139. if(events & WorkerEvtRxDone) {
  140. size_t len =
  141. xStreamBufferReceive(usb_uart->rx_stream, usb_uart->rx_buf, USB_CDC_PKT_LEN, 0);
  142. if(len > 0) {
  143. if(osSemaphoreAcquire(usb_uart->tx_sem, 100) == osOK) {
  144. usb_uart->st.rx_cnt += len;
  145. furi_check(osMutexAcquire(usb_uart->usb_mutex, osWaitForever) == osOK);
  146. furi_hal_cdc_send(usb_uart->cfg.vcp_ch, usb_uart->rx_buf, len);
  147. furi_check(osMutexRelease(usb_uart->usb_mutex) == osOK);
  148. } else {
  149. xStreamBufferReset(usb_uart->rx_stream);
  150. }
  151. }
  152. }
  153. if(events & WorkerEvtCfgChange) {
  154. if(usb_uart->cfg.vcp_ch != usb_uart->cfg_new.vcp_ch) {
  155. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtTxStop);
  156. furi_thread_join(usb_uart->tx_thread);
  157. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  158. usb_uart_vcp_init(usb_uart, usb_uart->cfg_new.vcp_ch);
  159. usb_uart->cfg.vcp_ch = usb_uart->cfg_new.vcp_ch;
  160. furi_thread_start(usb_uart->tx_thread);
  161. events |= WorkerEvtCtrlLineSet;
  162. events |= WorkerEvtLineCfgSet;
  163. }
  164. if(usb_uart->cfg.uart_ch != usb_uart->cfg_new.uart_ch) {
  165. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtTxStop);
  166. furi_thread_join(usb_uart->tx_thread);
  167. usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
  168. usb_uart_serial_init(usb_uart, usb_uart->cfg_new.uart_ch);
  169. usb_uart->cfg.uart_ch = usb_uart->cfg_new.uart_ch;
  170. usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
  171. furi_thread_start(usb_uart->tx_thread);
  172. }
  173. if(usb_uart->cfg.baudrate != usb_uart->cfg_new.baudrate) {
  174. usb_uart_set_baudrate(usb_uart, usb_uart->cfg_new.baudrate);
  175. usb_uart->cfg.baudrate = usb_uart->cfg_new.baudrate;
  176. }
  177. if(usb_uart->cfg.flow_pins != usb_uart->cfg_new.flow_pins) {
  178. if(usb_uart->cfg.flow_pins != 0) {
  179. hal_gpio_init_simple(
  180. flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
  181. hal_gpio_init_simple(
  182. flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
  183. }
  184. if(usb_uart->cfg_new.flow_pins != 0) {
  185. furi_assert(
  186. (usb_uart->cfg_new.flow_pins - 1) <
  187. (sizeof(flow_pins) / sizeof(flow_pins[0])));
  188. hal_gpio_init_simple(
  189. flow_pins[usb_uart->cfg_new.flow_pins - 1][0], GpioModeOutputPushPull);
  190. hal_gpio_init_simple(
  191. flow_pins[usb_uart->cfg_new.flow_pins - 1][1], GpioModeOutputPushPull);
  192. }
  193. usb_uart->cfg.flow_pins = usb_uart->cfg_new.flow_pins;
  194. events |= WorkerEvtCtrlLineSet;
  195. }
  196. }
  197. if(events & WorkerEvtLineCfgSet) {
  198. if(usb_uart->cfg.baudrate == 0)
  199. usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
  200. }
  201. if(events & WorkerEvtCtrlLineSet) {
  202. usb_uart_update_ctrl_lines(usb_uart);
  203. }
  204. }
  205. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  206. usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
  207. furi_hal_usb_unlock();
  208. furi_hal_usb_set_config(usb_mode_prev, NULL);
  209. if(usb_uart->cfg.flow_pins != 0) {
  210. hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
  211. hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
  212. }
  213. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtTxStop);
  214. furi_thread_join(usb_uart->tx_thread);
  215. furi_thread_free(usb_uart->tx_thread);
  216. vStreamBufferDelete(usb_uart->rx_stream);
  217. osMutexDelete(usb_uart->usb_mutex);
  218. osSemaphoreDelete(usb_uart->tx_sem);
  219. return 0;
  220. }
  221. static int32_t usb_uart_tx_thread(void* context) {
  222. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  223. uint8_t data[USB_CDC_PKT_LEN];
  224. while(1) {
  225. uint32_t events = osThreadFlagsWait(WORKER_ALL_TX_EVENTS, osFlagsWaitAny, osWaitForever);
  226. furi_check((events & osFlagsError) == 0);
  227. if(events & WorkerEvtTxStop) break;
  228. if(events & WorkerEvtCdcRx) {
  229. furi_check(osMutexAcquire(usb_uart->usb_mutex, osWaitForever) == osOK);
  230. size_t len = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, data, USB_CDC_PKT_LEN);
  231. furi_check(osMutexRelease(usb_uart->usb_mutex) == osOK);
  232. if(len > 0) {
  233. usb_uart->st.tx_cnt += len;
  234. furi_hal_uart_tx(usb_uart->cfg.uart_ch, data, len);
  235. }
  236. }
  237. }
  238. return 0;
  239. }
  240. /* VCP callbacks */
  241. static void vcp_on_cdc_tx_complete(void* context) {
  242. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  243. osSemaphoreRelease(usb_uart->tx_sem);
  244. }
  245. static void vcp_on_cdc_rx(void* context) {
  246. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  247. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->tx_thread), WorkerEvtCdcRx);
  248. }
  249. static void vcp_state_callback(void* context, uint8_t state) {
  250. }
  251. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  252. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  253. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtCtrlLineSet);
  254. }
  255. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config) {
  256. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  257. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtLineCfgSet);
  258. }
  259. UsbUartBridge* usb_uart_enable(UsbUartConfig* cfg) {
  260. UsbUartBridge* usb_uart = malloc(sizeof(UsbUartBridge));
  261. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  262. usb_uart->thread = furi_thread_alloc();
  263. furi_thread_set_name(usb_uart->thread, "UsbUartWorker");
  264. furi_thread_set_stack_size(usb_uart->thread, 1024);
  265. furi_thread_set_context(usb_uart->thread, usb_uart);
  266. furi_thread_set_callback(usb_uart->thread, usb_uart_worker);
  267. furi_thread_start(usb_uart->thread);
  268. return usb_uart;
  269. }
  270. void usb_uart_disable(UsbUartBridge* usb_uart) {
  271. furi_assert(usb_uart);
  272. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtStop);
  273. furi_thread_join(usb_uart->thread);
  274. furi_thread_free(usb_uart->thread);
  275. free(usb_uart);
  276. }
  277. void usb_uart_set_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
  278. furi_assert(usb_uart);
  279. furi_assert(cfg);
  280. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  281. osThreadFlagsSet(furi_thread_get_thread_id(usb_uart->thread), WorkerEvtCfgChange);
  282. }
  283. void usb_uart_get_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
  284. furi_assert(usb_uart);
  285. furi_assert(cfg);
  286. memcpy(cfg, &(usb_uart->cfg_new), sizeof(UsbUartConfig));
  287. }
  288. void usb_uart_get_state(UsbUartBridge* usb_uart, UsbUartState* st) {
  289. furi_assert(usb_uart);
  290. furi_assert(st);
  291. memcpy(st, &(usb_uart->st), sizeof(UsbUartState));
  292. }