usb_uart.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. static void usb_uart_vcp_init(UsbUart* usb_uart, uint8_t vcp_ch) {
  41. furi_hal_usb_unlock();
  42. Cli* cli = furi_record_open(RECORD_CLI);
  43. cli_session_close(cli);
  44. if(vcp_ch == 0) {
  45. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  46. } else {
  47. furi_check(furi_hal_usb_set_config(&usb_cdc_dual, NULL) == true);
  48. cli_session_open(cli, &cli_vcp);
  49. }
  50. furi_record_close(RECORD_CLI);
  51. furi_hal_cdc_set_callbacks(vcp_ch, (CdcCallbacks*)&cdc_cb, usb_uart);
  52. }
  53. static void usb_uart_vcp_deinit(UsbUart* usb_uart, uint8_t vcp_ch) {
  54. UNUSED(usb_uart);
  55. furi_hal_cdc_set_callbacks(vcp_ch, NULL, NULL);
  56. if(vcp_ch != 0) {
  57. Cli* cli = furi_record_open(RECORD_CLI);
  58. cli_session_close(cli);
  59. furi_record_close(RECORD_CLI);
  60. }
  61. }
  62. bool usb_uart_tx_data(UsbUart* usb_uart, uint8_t* data, size_t length) {
  63. uint32_t pos = 0;
  64. while(pos < length) {
  65. size_t pkt_size = length - pos;
  66. if(pkt_size > USB_CDC_PKT_LEN) {
  67. pkt_size = USB_CDC_PKT_LEN;
  68. }
  69. if(furi_semaphore_acquire(usb_uart->tx_sem, 100) != FuriStatusOk) {
  70. return false;
  71. }
  72. if(furi_mutex_acquire(usb_uart->usb_mutex, 100) != FuriStatusOk) {
  73. furi_semaphore_release(usb_uart->tx_sem);
  74. return false;
  75. }
  76. furi_hal_cdc_send(usb_uart->cfg.vcp_ch, &data[pos], pkt_size);
  77. furi_mutex_release(usb_uart->usb_mutex);
  78. usb_uart->st.tx_cnt += pkt_size;
  79. pos += pkt_size;
  80. }
  81. return true;
  82. }
  83. static int32_t usb_uart_worker(void* context) {
  84. UsbUart* usb_uart = (UsbUart*)context;
  85. memcpy(&usb_uart->cfg, &usb_uart->cfg_new, sizeof(UsbUartConfig));
  86. usb_uart->tx_sem = furi_semaphore_alloc(1, 1);
  87. usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  88. usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
  89. uint8_t data[2 * USB_CDC_PKT_LEN];
  90. size_t remain = 0;
  91. while(1) {
  92. uint32_t events =
  93. furi_thread_flags_wait(WORKER_ALL_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  94. furi_check(!(events & FuriFlagError));
  95. if(events & WorkerEvtStop) {
  96. break;
  97. }
  98. if(events & WorkerEvtCdcRx) {
  99. size_t len = 0;
  100. if(furi_mutex_acquire(usb_uart->usb_mutex, 100) == FuriStatusOk) {
  101. len = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, &data[remain], USB_CDC_PKT_LEN);
  102. furi_mutex_release(usb_uart->usb_mutex);
  103. }
  104. if(len > 0) {
  105. usb_uart->st.rx_cnt += len;
  106. remain += len;
  107. size_t handled = usb_uart->cfg.rx_data(usb_uart->cfg.rx_data_ctx, data, remain);
  108. memcpy(data, &data[handled], remain - handled);
  109. remain -= handled;
  110. }
  111. }
  112. if(events & WorkerEvtCfgChange) {
  113. if(usb_uart->cfg.vcp_ch != usb_uart->cfg_new.vcp_ch) {
  114. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  115. usb_uart_vcp_init(usb_uart, usb_uart->cfg_new.vcp_ch);
  116. usb_uart->cfg.vcp_ch = usb_uart->cfg_new.vcp_ch;
  117. }
  118. api_lock_unlock(usb_uart->cfg_lock);
  119. }
  120. }
  121. usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
  122. furi_mutex_free(usb_uart->usb_mutex);
  123. furi_semaphore_free(usb_uart->tx_sem);
  124. furi_hal_usb_unlock();
  125. furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
  126. Cli* cli = furi_record_open(RECORD_CLI);
  127. cli_session_open(cli, &cli_vcp);
  128. furi_record_close(RECORD_CLI);
  129. return 0;
  130. }
  131. /* VCP callbacks */
  132. static void vcp_on_cdc_tx_complete(void* context) {
  133. UsbUart* usb_uart = (UsbUart*)context;
  134. furi_semaphore_release(usb_uart->tx_sem);
  135. }
  136. static void vcp_on_cdc_rx(void* context) {
  137. UsbUart* usb_uart = (UsbUart*)context;
  138. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCdcRx);
  139. }
  140. static void vcp_state_callback(void* context, uint8_t state) {
  141. UNUSED(context);
  142. UNUSED(state);
  143. }
  144. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  145. UNUSED(context);
  146. UNUSED(state);
  147. }
  148. static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config) {
  149. UNUSED(context);
  150. UNUSED(config);
  151. }
  152. UsbUart* usb_uart_enable(UsbUartConfig* cfg) {
  153. UsbUart* usb_uart = malloc(sizeof(UsbUart));
  154. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  155. usb_uart->thread = furi_thread_alloc_ex("UsbUartWorker", 1024, usb_uart_worker, usb_uart);
  156. furi_thread_start(usb_uart->thread);
  157. return usb_uart;
  158. }
  159. void usb_uart_disable(UsbUart* usb_uart) {
  160. furi_assert(usb_uart);
  161. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtStop);
  162. furi_thread_join(usb_uart->thread);
  163. furi_thread_free(usb_uart->thread);
  164. free(usb_uart);
  165. }
  166. void usb_uart_set_config(UsbUart* usb_uart, UsbUartConfig* cfg) {
  167. furi_assert(usb_uart);
  168. furi_assert(cfg);
  169. usb_uart->cfg_lock = api_lock_alloc_locked();
  170. memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
  171. furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCfgChange);
  172. api_lock_wait_unlock_and_free(usb_uart->cfg_lock);
  173. }
  174. void usb_uart_get_config(UsbUart* usb_uart, UsbUartConfig* cfg) {
  175. furi_assert(usb_uart);
  176. furi_assert(cfg);
  177. memcpy(cfg, &(usb_uart->cfg_new), sizeof(UsbUartConfig));
  178. }
  179. void usb_uart_get_state(UsbUart* usb_uart, UsbUartState* st) {
  180. furi_assert(usb_uart);
  181. furi_assert(st);
  182. memcpy(st, &(usb_uart->st), sizeof(UsbUartState));
  183. }