usb_uart_bridge.c 14 KB

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