furi_hal_vcp.c 9.2 KB

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