usb_uart_bridge.c 14 KB

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