usb_uart.c 6.8 KB

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