furi-hal-vcp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <furi-hal-usb-cdc_i.h>
  2. #include <furi-hal-console.h>
  3. #include <furi.h>
  4. #include <stream_buffer.h>
  5. #define TAG "FuriHalVcp"
  6. #define USB_CDC_PKT_LEN CDC_DATA_SZ
  7. #define VCP_RX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  8. #define VCP_TX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  9. #define VCP_IF_NUM 0
  10. typedef enum {
  11. VcpEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
  12. VcpEvtConnect = (1 << 1),
  13. VcpEvtDisconnect = (1 << 2),
  14. VcpEvtEnable = (1 << 3),
  15. VcpEvtDisable = (1 << 4),
  16. VcpEvtRx = (1 << 5),
  17. VcpEvtTx = (1 << 6),
  18. VcpEvtStreamRx = (1 << 7),
  19. VcpEvtStreamTx = (1 << 8),
  20. } WorkerEvtFlags;
  21. #define VCP_THREAD_FLAG_ALL (VcpEvtConnect | VcpEvtDisconnect | VcpEvtEnable | VcpEvtDisable | VcpEvtRx | VcpEvtTx | VcpEvtStreamRx | VcpEvtStreamTx)
  22. typedef struct {
  23. FuriThread* thread;
  24. StreamBufferHandle_t tx_stream;
  25. StreamBufferHandle_t rx_stream;
  26. volatile bool connected;
  27. uint8_t data_buffer[USB_CDC_PKT_LEN];
  28. } FuriHalVcp;
  29. static int32_t vcp_worker(void* context);
  30. static void vcp_on_cdc_tx_complete();
  31. static void vcp_on_cdc_rx();
  32. static void vcp_state_callback(uint8_t state);
  33. static void vcp_on_cdc_control_line(uint8_t state);
  34. static CdcCallbacks cdc_cb = {
  35. vcp_on_cdc_tx_complete,
  36. vcp_on_cdc_rx,
  37. vcp_state_callback,
  38. vcp_on_cdc_control_line,
  39. NULL,
  40. };
  41. static FuriHalVcp* vcp = NULL;
  42. static const uint8_t ascii_soh = 0x01;
  43. static const uint8_t ascii_eot = 0x04;
  44. void furi_hal_vcp_init() {
  45. vcp = furi_alloc(sizeof(FuriHalVcp));
  46. vcp->connected = false;
  47. vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
  48. vcp->rx_stream = xStreamBufferCreate(VCP_RX_BUF_SIZE, 1);
  49. vcp->thread = furi_thread_alloc();
  50. furi_thread_set_name(vcp->thread, "VcpWorker");
  51. furi_thread_set_stack_size(vcp->thread, 1024);
  52. furi_thread_set_callback(vcp->thread, vcp_worker);
  53. furi_thread_start(vcp->thread);
  54. FURI_LOG_I(TAG, "Init OK");
  55. }
  56. static int32_t vcp_worker(void* context) {
  57. bool enabled = true;
  58. bool tx_idle = false;
  59. size_t missed_rx = 0;
  60. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
  61. while (1) {
  62. uint32_t flags = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  63. furi_assert((flags & osFlagsError) == 0);
  64. // New data received
  65. if((flags & VcpEvtStreamRx) && enabled && missed_rx > 0) {
  66. #ifdef FURI_HAL_USB_VCP_DEBUG
  67. furi_hal_console_puts("VCP StreamRx\r\n");
  68. #endif
  69. if (xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  70. flags |= VcpEvtRx;
  71. missed_rx--;
  72. }
  73. }
  74. // Rx buffer was read, maybe there is enough space for new data?
  75. if((flags & VcpEvtRx)) {
  76. if (xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  77. int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  78. #ifdef FURI_HAL_USB_VCP_DEBUG
  79. furi_hal_console_printf("VCP Rx %d\r\n", len);
  80. #endif
  81. if (len > 0) {
  82. furi_check(xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever) == len);
  83. }
  84. } else {
  85. #ifdef FURI_HAL_USB_VCP_DEBUG
  86. furi_hal_console_puts("VCP Rx missed\r\n");
  87. #endif
  88. missed_rx++;
  89. }
  90. }
  91. // New data in Tx buffer
  92. if((flags & VcpEvtStreamTx) && enabled) {
  93. #ifdef FURI_HAL_USB_VCP_DEBUG
  94. furi_hal_console_puts("VCP StreamTx\r\n");
  95. #endif
  96. if (tx_idle) {
  97. flags |= VcpEvtTx;
  98. }
  99. }
  100. // CDC write transfer done
  101. if((flags & VcpEvtTx) && enabled) {
  102. size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  103. #ifdef FURI_HAL_USB_VCP_DEBUG
  104. furi_hal_console_printf("VCP Tx %d\r\n", len);
  105. #endif
  106. if (len > 0) { // Some data left in Tx buffer. Sending it now
  107. tx_idle = false;
  108. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  109. } else { // There is nothing to send. Set flag to start next transfer instantly
  110. tx_idle = true;
  111. }
  112. }
  113. // VCP session opened
  114. if((flags & VcpEvtConnect) && enabled) {
  115. #ifdef FURI_HAL_USB_VCP_DEBUG
  116. furi_hal_console_puts("VCP Connect\r\n");
  117. #endif
  118. if (vcp->connected == false) {
  119. vcp->connected = true;
  120. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  121. }
  122. }
  123. // VCP session closed
  124. if((flags & VcpEvtDisconnect) && enabled) {
  125. #ifdef FURI_HAL_USB_VCP_DEBUG
  126. furi_hal_console_puts("VCP Disconnect\r\n");
  127. #endif
  128. if (vcp->connected == true) {
  129. vcp->connected = false;
  130. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  131. }
  132. }
  133. // VCP enabled
  134. if((flags & VcpEvtEnable) && !enabled){
  135. #ifdef FURI_HAL_USB_VCP_DEBUG
  136. furi_hal_console_puts("VCP Enable\r\n");
  137. #endif
  138. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
  139. enabled = true;
  140. furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN); // flush Rx buffer
  141. if (furi_hal_cdc_get_ctrl_line_state(VCP_IF_NUM) & (1 << 0)) {
  142. vcp->connected = true;
  143. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  144. }
  145. }
  146. // VCP disabled
  147. if((flags & VcpEvtDisable) && enabled) {
  148. #ifdef FURI_HAL_USB_VCP_DEBUG
  149. furi_hal_console_puts("VCP Disable\r\n");
  150. #endif
  151. enabled = false;
  152. vcp->connected = false;
  153. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  154. }
  155. }
  156. return 0;
  157. }
  158. void furi_hal_vcp_enable() {
  159. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtEnable);
  160. }
  161. void furi_hal_vcp_disable() {
  162. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisable);
  163. }
  164. size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
  165. furi_assert(vcp);
  166. furi_assert(buffer);
  167. size_t rx_cnt = 0;
  168. while (size > 0) {
  169. size_t batch_size = size;
  170. if (batch_size > VCP_RX_BUF_SIZE)
  171. batch_size = VCP_RX_BUF_SIZE;
  172. size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
  173. if (len == 0)
  174. break;
  175. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamRx);
  176. size -= len;
  177. buffer += len;
  178. rx_cnt += len;
  179. }
  180. return rx_cnt;
  181. }
  182. size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
  183. furi_assert(vcp);
  184. return furi_hal_vcp_rx_with_timeout(buffer, size, osWaitForever);
  185. }
  186. void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
  187. furi_assert(vcp);
  188. furi_assert(buffer);
  189. while (size > 0) {
  190. size_t batch_size = size;
  191. if (batch_size > VCP_TX_BUF_SIZE)
  192. batch_size = VCP_TX_BUF_SIZE;
  193. xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
  194. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamTx);
  195. size -= batch_size;
  196. buffer += batch_size;
  197. }
  198. }
  199. static void vcp_state_callback(uint8_t state) {
  200. #ifdef FURI_HAL_USB_VCP_DEBUG
  201. furi_hal_console_puts("VCP State\r\n");
  202. #endif
  203. if (state == 0) {
  204. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  205. }
  206. }
  207. static void vcp_on_cdc_control_line(uint8_t state) {
  208. // bit 0: DTR state, bit 1: RTS state
  209. bool dtr = state & (1 << 0);
  210. #ifdef FURI_HAL_USB_VCP_DEBUG
  211. furi_hal_console_puts("VCP CtrlLine\r\n");
  212. #endif
  213. if (dtr == true) {
  214. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
  215. } else {
  216. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  217. }
  218. }
  219. static void vcp_on_cdc_rx() {
  220. uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
  221. furi_assert((ret & osFlagsError) == 0);
  222. }
  223. static void vcp_on_cdc_tx_complete() {
  224. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
  225. }
  226. bool furi_hal_vcp_is_connected(void) {
  227. furi_assert(vcp);
  228. return vcp->connected;
  229. }