usb_uart_bridge.c 14 KB

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