usb_uart.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <stdlib.h>
  2. #include "usb_uart.h"
  3. #include "furi_hal.h"
  4. #include <furi_hal_usb_cdc.h>
  5. #include "usb_cdc.h"
  6. #include "cli/cli_vcp.h"
  7. #include <toolbox/api_lock.h>
  8. #include <cli/cli.h>
  9. #define USB_CDC_PKT_LEN CDC_DATA_SZ
  10. #define USB_UART_RX_BUF_SIZE (USB_CDC_PKT_LEN * 5)
  11. #define USB_CDC_BIT_DTR (1 << 0)
  12. #define USB_CDC_BIT_RTS (1 << 1)
  13. typedef enum {
  14. WorkerEvtStop = (1 << 0),
  15. WorkerEvtCdcRx = (1 << 1),
  16. WorkerEvtCfgChange = (1 << 2)
  17. } WorkerEvtFlags;
  18. #define WORKER_ALL_EVENTS (WorkerEvtStop | WorkerEvtCfgChange | WorkerEvtCdcRx)
  19. struct UsbUart {
  20. UsbUartConfig cfg;
  21. UsbUartConfig cfg_new;
  22. FuriThread* thread;
  23. FuriMutex* usb_mutex;
  24. FuriSemaphore* tx_sem;
  25. UsbUartState st;
  26. FuriApiLock cfg_lock;
  27. uint8_t rx_buf[USB_CDC_PKT_LEN];
  28. };
  29. static void vcp_on_cdc_tx_complete(void* context);
  30. static void vcp_on_cdc_rx(void* context);
  31. static void vcp_state_callback(void* context, uint8_t state);
  32. static void vcp_on_cdc_control_line(void* context, uint8_t state);
  33. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config);
  34. static const CdcCallbacks cdc_cb = {
  35. .tx_ep_callback = &vcp_on_cdc_tx_complete,
  36. .rx_ep_callback = &vcp_on_cdc_rx,
  37. .state_callback = &vcp_state_callback,
  38. .ctrl_line_callback = &vcp_on_cdc_control_line,
  39. .config_callback = &vcp_on_line_config,
  40. };
  41. static void usb_uart_vcp_init(UsbUart* usb_uart, uint8_t vcp_ch) {
  42. furi_hal_usb_unlock();
  43. CliVcp* cli_vcp = furi_record_open(RECORD_CLI_VCP);
  44. cli_vcp_disable(cli_vcp);
  45. if(vcp_ch == 0) {
  46. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  47. } else {
  48. furi_check(furi_hal_usb_set_config(&usb_cdc_dual, NULL) == true);
  49. cli_vcp_enable(cli_vcp);
  50. }
  51. furi_record_close(RECORD_CLI_VCP);
  52. furi_hal_cdc_set_callbacks(vcp_ch, (CdcCallbacks*)&cdc_cb, usb_uart);
  53. }
  54. static void usb_uart_vcp_deinit(UsbUart* usb_uart, uint8_t vcp_ch) {
  55. UNUSED(usb_uart);
  56. furi_hal_cdc_set_callbacks(vcp_ch, NULL, NULL);
  57. if(vcp_ch != 0) {
  58. CliVcp* cli_vcp = furi_record_open(RECORD_CLI_VCP);
  59. cli_vcp_disable(cli_vcp);
  60. furi_record_close(RECORD_CLI_VCP);
  61. }
  62. }
  63. bool usb_uart_tx_data(UsbUart* usb_uart, uint8_t* data, size_t length) {
  64. uint32_t pos = 0;
  65. while(pos < length) {
  66. size_t pkt_size = length - pos;
  67. if(pkt_size > USB_CDC_PKT_LEN) {
  68. pkt_size = USB_CDC_PKT_LEN;
  69. }
  70. if(furi_semaphore_acquire(usb_uart->tx_sem, 100) != FuriStatusOk) {
  71. return false;
  72. }
  73. if(furi_mutex_acquire(usb_uart->usb_mutex, 100) != FuriStatusOk) {
  74. furi_semaphore_release(usb_uart->tx_sem);
  75. return false;
  76. }
  77. furi_hal_cdc_send(usb_uart->cfg.vcp_ch, &data[pos], pkt_size);
  78. furi_mutex_release(usb_uart->usb_mutex);
  79. usb_uart->st.tx_cnt += pkt_size;
  80. pos += pkt_size;
  81. }
  82. return true;
  83. }
  84. static int32_t usb_uart_worker(void* context) {
  85. UsbUart* usb_uart = (UsbUart*)context;
  86. memcpy(&usb_uart->cfg, &usb_uart->cfg_new, sizeof(UsbUartConfig));
  87. usb_uart->tx_sem = furi_semaphore_alloc(1, 1);
  88. usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  89. usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
  90. uint8_t data[2 * USB_CDC_PKT_LEN];
  91. size_t remain = 0;
  92. while(1) {
  93. uint32_t events =
  94. furi_thread_flags_wait(WORKER_ALL_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  95. furi_check(!(events & FuriFlagError));
  96. if(events & WorkerEvtStop) {
  97. break;
  98. }
  99. if(events & WorkerEvtCdcRx) {
  100. size_t len = 0;
  101. if(furi_mutex_acquire(usb_uart->usb_mutex, 100) == FuriStatusOk) {
  102. len = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, &data[remain], USB_CDC_PKT_LEN);
  103. furi_mutex_release(usb_uart->usb_mutex);
  104. }
  105. if(len > 0) {
  106. usb_uart->st.rx_cnt += len;
  107. remain += len;
  108. size_t handled = usb_uart->cfg.rx_data(usb_uart->cfg.rx_data_ctx, data, remain);
  109. memcpy(data, &data[handled], remain - handled);
  110. remain -= handled;
  111. }
  112. }
  113. if(events & WorkerEvtCfgChange) {
  114. if(usb_uart->cfg.vcp_ch != usb_uart->cfg_new.vcp_ch) {
  115. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  116. usb_uart_vcp_init(usb_uart, usb_uart->cfg_new.vcp_ch);
  117. usb_uart->cfg.vcp_ch = usb_uart->cfg_new.vcp_ch;
  118. }
  119. api_lock_unlock(usb_uart->cfg_lock);
  120. }
  121. }
  122. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  123. furi_mutex_free(usb_uart->usb_mutex);
  124. furi_semaphore_free(usb_uart->tx_sem);
  125. furi_hal_usb_unlock();
  126. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  127. CliVcp* cli_vcp = furi_record_open(RECORD_CLI_VCP);
  128. cli_vcp_enable(cli_vcp);
  129. furi_record_close(RECORD_CLI_VCP);
  130. return 0;
  131. }
  132. /* VCP callbacks */
  133. static void vcp_on_cdc_tx_complete(void* context) {
  134. UsbUart* usb_uart = (UsbUart*)context;
  135. furi_semaphore_release(usb_uart->tx_sem);
  136. }
  137. static void vcp_on_cdc_rx(void* context) {
  138. UsbUart* usb_uart = (UsbUart*)context;
  139. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCdcRx);
  140. }
  141. static void vcp_state_callback(void* context, uint8_t state) {
  142. UNUSED(context);
  143. UNUSED(state);
  144. }
  145. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  146. UNUSED(context);
  147. UNUSED(state);
  148. }
  149. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config) {
  150. UNUSED(context);
  151. UNUSED(config);
  152. }
  153. UsbUart* usb_uart_enable(UsbUartConfig* cfg) {
  154. UsbUart* usb_uart = malloc(sizeof(UsbUart));
  155. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  156. usb_uart->thread = furi_thread_alloc_ex("UsbUartWorker", 1024, usb_uart_worker, usb_uart);
  157. furi_thread_start(usb_uart->thread);
  158. return usb_uart;
  159. }
  160. void usb_uart_disable(UsbUart* usb_uart) {
  161. furi_assert(usb_uart);
  162. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtStop);
  163. furi_thread_join(usb_uart->thread);
  164. furi_thread_free(usb_uart->thread);
  165. free(usb_uart);
  166. }
  167. void usb_uart_set_config(UsbUart* usb_uart, UsbUartConfig* cfg) {
  168. furi_assert(usb_uart);
  169. furi_assert(cfg);
  170. usb_uart->cfg_lock = api_lock_alloc_locked();
  171. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  172. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCfgChange);
  173. api_lock_wait_unlock_and_free(usb_uart->cfg_lock);
  174. }
  175. void usb_uart_get_config(UsbUart* usb_uart, UsbUartConfig* cfg) {
  176. furi_assert(usb_uart);
  177. furi_assert(cfg);
  178. memcpy(cfg, &(usb_uart->cfg_new), sizeof(UsbUartConfig));
  179. }
  180. void usb_uart_get_state(UsbUart* usb_uart, UsbUartState* st) {
  181. furi_assert(usb_uart);
  182. furi_assert(st);
  183. memcpy(st, &(usb_uart->st), sizeof(UsbUartState));
  184. }