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 <toolbox/api_lock.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. {&gpio_ext_pc0, &gpio_ext_pc1}, // 16, 15
  16. };
  17. typedef enum {
  18. WorkerEvtStop = (1 << 0),
  19. WorkerEvtRxDone = (1 << 1),
  20. WorkerEvtTxStop = (1 << 2),
  21. WorkerEvtCdcRx = (1 << 3),
  22. WorkerEvtCfgChange = (1 << 4),
  23. WorkerEvtLineCfgSet = (1 << 5),
  24. WorkerEvtCtrlLineSet = (1 << 6),
  25. } WorkerEvtFlags;
  26. #define WORKER_ALL_RX_EVENTS \
  27. (WorkerEvtStop | WorkerEvtRxDone | WorkerEvtCfgChange | WorkerEvtLineCfgSet | \
  28. WorkerEvtCtrlLineSet)
  29. #define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtCdcRx)
  30. struct UsbUartBridge {
  31. UsbUartConfig cfg;
  32. UsbUartConfig cfg_new;
  33. FuriThread* thread;
  34. FuriThread* tx_thread;
  35. FuriHalSerialHandle* serial_handle;
  36. FuriStreamBuffer* rx_stream;
  37. FuriMutex* usb_mutex;
  38. FuriSemaphore* tx_sem;
  39. UsbUartState st;
  40. FuriApiLock cfg_lock;
  41. uint8_t rx_buf[USB_CDC_PKT_LEN];
  42. };
  43. static void vcp_on_cdc_tx_complete(void* context);
  44. static void vcp_on_cdc_rx(void* context);
  45. static void vcp_state_callback(void* context, uint8_t state);
  46. static void vcp_on_cdc_control_line(void* context, uint8_t state);
  47. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config);
  48. static const CdcCallbacks cdc_cb = {
  49. vcp_on_cdc_tx_complete,
  50. vcp_on_cdc_rx,
  51. vcp_state_callback,
  52. vcp_on_cdc_control_line,
  53. vcp_on_line_config,
  54. NULL,
  55. };
  56. /* USB UART worker */
  57. static int32_t usb_uart_tx_thread(void* context);
  58. static void
  59. usb_uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
  60. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  61. if(event == FuriHalSerialRxEventData) {
  62. uint8_t data = furi_hal_serial_async_rx(handle);
  63. furi_stream_buffer_send(usb_uart->rx_stream, &data, 1, 0);
  64. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtRxDone);
  65. }
  66. }
  67. static void usb_uart_vcp_init(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
  68. furi_hal_usb_unlock();
  69. if(vcp_ch == 0) {
  70. Cli* cli = furi_record_open(RECORD_CLI);
  71. cli_session_close(cli);
  72. furi_record_close(RECORD_CLI);
  73. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  74. } else {
  75. furi_check(furi_hal_usb_set_config(&usb_cdc_dual, NULL) == true);
  76. Cli* cli = furi_record_open(RECORD_CLI);
  77. cli_session_open(cli, &cli_vcp);
  78. furi_record_close(RECORD_CLI);
  79. }
  80. furi_hal_cdc_set_callbacks(vcp_ch, (CdcCallbacks*)&cdc_cb, usb_uart);
  81. }
  82. static void usb_uart_vcp_deinit(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
  83. UNUSED(usb_uart);
  84. furi_hal_cdc_set_callbacks(vcp_ch, NULL, NULL);
  85. if(vcp_ch != 0) {
  86. Cli* cli = furi_record_open(RECORD_CLI);
  87. cli_session_close(cli);
  88. furi_record_close(RECORD_CLI);
  89. }
  90. }
  91. static void usb_uart_serial_init(UsbUartBridge* usb_uart, uint8_t uart_ch) {
  92. usb_uart->serial_handle = furi_hal_serial_control_acquire(uart_ch);
  93. furi_check(usb_uart->serial_handle);
  94. furi_hal_serial_init(usb_uart->serial_handle, 115200);
  95. furi_hal_serial_async_rx_start(usb_uart->serial_handle, usb_uart_on_irq_cb, usb_uart, false);
  96. }
  97. static void usb_uart_serial_deinit(UsbUartBridge* usb_uart, uint8_t uart_ch) {
  98. UNUSED(uart_ch);
  99. furi_hal_serial_async_rx_stop(usb_uart->serial_handle);
  100. furi_hal_serial_deinit(usb_uart->serial_handle);
  101. furi_hal_serial_control_release(usb_uart->serial_handle);
  102. }
  103. static void usb_uart_set_baudrate(UsbUartBridge* usb_uart, uint32_t baudrate) {
  104. if(baudrate != 0) {
  105. furi_hal_serial_set_br(usb_uart->serial_handle, baudrate);
  106. usb_uart->st.baudrate_cur = baudrate;
  107. } else {
  108. struct usb_cdc_line_coding* line_cfg =
  109. furi_hal_cdc_get_port_settings(usb_uart->cfg.vcp_ch);
  110. if(line_cfg->dwDTERate > 0) {
  111. furi_hal_serial_set_br(usb_uart->serial_handle, baudrate);
  112. usb_uart->st.baudrate_cur = line_cfg->dwDTERate;
  113. }
  114. }
  115. }
  116. static void usb_uart_update_ctrl_lines(UsbUartBridge* usb_uart) {
  117. if(usb_uart->cfg.flow_pins != 0) {
  118. furi_assert((size_t)(usb_uart->cfg.flow_pins - 1) < COUNT_OF(flow_pins));
  119. uint8_t state = furi_hal_cdc_get_ctrl_line_state(usb_uart->cfg.vcp_ch);
  120. furi_hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][0], !(state & USB_CDC_BIT_RTS));
  121. furi_hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][1], !(state & USB_CDC_BIT_DTR));
  122. }
  123. }
  124. static int32_t usb_uart_worker(void* context) {
  125. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  126. memcpy(&usb_uart->cfg, &usb_uart->cfg_new, sizeof(UsbUartConfig));
  127. usb_uart->rx_stream = furi_stream_buffer_alloc(USB_UART_RX_BUF_SIZE, 1);
  128. usb_uart->tx_sem = furi_semaphore_alloc(1, 1);
  129. usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  130. usb_uart->tx_thread =
  131. furi_thread_alloc_ex("UsbUartTxWorker", 512, usb_uart_tx_thread, usb_uart);
  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));
  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. api_lock_unlock(usb_uart->cfg_lock);
  207. }
  208. if(events & WorkerEvtLineCfgSet) {
  209. if(usb_uart->cfg.baudrate == 0)
  210. usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
  211. }
  212. if(events & WorkerEvtCtrlLineSet) {
  213. usb_uart_update_ctrl_lines(usb_uart);
  214. }
  215. }
  216. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  217. usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
  218. if(usb_uart->cfg.flow_pins != 0) {
  219. furi_hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
  220. furi_hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
  221. }
  222. furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
  223. furi_thread_join(usb_uart->tx_thread);
  224. furi_thread_free(usb_uart->tx_thread);
  225. furi_stream_buffer_free(usb_uart->rx_stream);
  226. furi_mutex_free(usb_uart->usb_mutex);
  227. furi_semaphore_free(usb_uart->tx_sem);
  228. furi_hal_usb_unlock();
  229. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  230. Cli* cli = furi_record_open(RECORD_CLI);
  231. cli_session_open(cli, &cli_vcp);
  232. furi_record_close(RECORD_CLI);
  233. return 0;
  234. }
  235. static int32_t usb_uart_tx_thread(void* context) {
  236. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  237. uint8_t data[USB_CDC_PKT_LEN];
  238. while(1) {
  239. uint32_t events =
  240. furi_thread_flags_wait(WORKER_ALL_TX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  241. furi_check(!(events & FuriFlagError));
  242. if(events & WorkerEvtTxStop) break;
  243. if(events & WorkerEvtCdcRx) {
  244. furi_check(furi_mutex_acquire(usb_uart->usb_mutex, FuriWaitForever) == FuriStatusOk);
  245. size_t len = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, data, USB_CDC_PKT_LEN);
  246. furi_check(furi_mutex_release(usb_uart->usb_mutex) == FuriStatusOk);
  247. if(len > 0) {
  248. usb_uart->st.tx_cnt += len;
  249. furi_hal_serial_tx(usb_uart->serial_handle, data, len);
  250. }
  251. }
  252. }
  253. return 0;
  254. }
  255. /* VCP callbacks */
  256. static void vcp_on_cdc_tx_complete(void* context) {
  257. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  258. furi_semaphore_release(usb_uart->tx_sem);
  259. }
  260. static void vcp_on_cdc_rx(void* context) {
  261. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  262. furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtCdcRx);
  263. }
  264. static void vcp_state_callback(void* context, uint8_t state) {
  265. UNUSED(context);
  266. UNUSED(state);
  267. }
  268. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  269. UNUSED(state);
  270. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  271. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCtrlLineSet);
  272. }
  273. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config) {
  274. UNUSED(config);
  275. UsbUartBridge* usb_uart = (UsbUartBridge*)context;
  276. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtLineCfgSet);
  277. }
  278. UsbUartBridge* usb_uart_enable(UsbUartConfig* cfg) {
  279. UsbUartBridge* usb_uart = malloc(sizeof(UsbUartBridge));
  280. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  281. usb_uart->thread = furi_thread_alloc_ex("UsbUartWorker", 1024, usb_uart_worker, usb_uart);
  282. furi_thread_start(usb_uart->thread);
  283. return usb_uart;
  284. }
  285. void usb_uart_disable(UsbUartBridge* usb_uart) {
  286. furi_assert(usb_uart);
  287. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtStop);
  288. furi_thread_join(usb_uart->thread);
  289. furi_thread_free(usb_uart->thread);
  290. free(usb_uart);
  291. }
  292. void usb_uart_set_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
  293. furi_assert(usb_uart);
  294. furi_assert(cfg);
  295. usb_uart->cfg_lock = api_lock_alloc_locked();
  296. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  297. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCfgChange);
  298. api_lock_wait_unlock_and_free(usb_uart->cfg_lock);
  299. }
  300. void usb_uart_get_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
  301. furi_assert(usb_uart);
  302. furi_assert(cfg);
  303. memcpy(cfg, &(usb_uart->cfg_new), sizeof(UsbUartConfig));
  304. }
  305. void usb_uart_get_state(UsbUartBridge* usb_uart, UsbUartState* st) {
  306. furi_assert(usb_uart);
  307. furi_assert(st);
  308. memcpy(st, &(usb_uart->st), sizeof(UsbUartState));
  309. }