furi_hal_vcp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. 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 = furi_alloc(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, "VcpWorker");
  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_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb, NULL);
  64. while(1) {
  65. uint32_t flags = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  66. furi_assert((flags & osFlagsError) == 0);
  67. // VCP enabled
  68. if((flags & VcpEvtEnable) && !enabled) {
  69. #ifdef FURI_HAL_USB_VCP_DEBUG
  70. FURI_LOG_D(TAG, "Enable");
  71. #endif
  72. flags |= VcpEvtTx;
  73. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb, NULL);
  74. enabled = true;
  75. furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN); // flush Rx buffer
  76. if(furi_hal_cdc_get_ctrl_line_state(VCP_IF_NUM) & (1 << 0)) {
  77. vcp->connected = true;
  78. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  79. }
  80. }
  81. // VCP disabled
  82. if((flags & VcpEvtDisable) && enabled) {
  83. #ifdef FURI_HAL_USB_VCP_DEBUG
  84. FURI_LOG_D(TAG, "Disable");
  85. #endif
  86. enabled = false;
  87. vcp->connected = false;
  88. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  89. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  90. }
  91. // VCP session opened
  92. if((flags & VcpEvtConnect) && enabled) {
  93. #ifdef FURI_HAL_USB_VCP_DEBUG
  94. FURI_LOG_D(TAG, "Connect");
  95. #endif
  96. if(vcp->connected == false) {
  97. vcp->connected = true;
  98. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  99. }
  100. }
  101. // VCP session closed
  102. if((flags & VcpEvtDisconnect) && enabled) {
  103. #ifdef FURI_HAL_USB_VCP_DEBUG
  104. FURI_LOG_D(TAG, "Disconnect");
  105. #endif
  106. if(vcp->connected == true) {
  107. vcp->connected = false;
  108. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  109. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  110. }
  111. }
  112. // Rx buffer was read, maybe there is enough space for new data?
  113. if((flags & VcpEvtStreamRx) && enabled && missed_rx > 0) {
  114. #ifdef FURI_HAL_USB_VCP_DEBUG
  115. FURI_LOG_D(TAG, "StreamRx");
  116. #endif
  117. if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  118. flags |= VcpEvtRx;
  119. missed_rx--;
  120. }
  121. }
  122. // New data received
  123. if((flags & VcpEvtRx)) {
  124. if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  125. int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  126. #ifdef FURI_HAL_USB_VCP_DEBUG
  127. FURI_LOG_D(TAG, "Rx %d", len);
  128. #endif
  129. if(len > 0) {
  130. furi_check(
  131. xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever) ==
  132. len);
  133. }
  134. } else {
  135. #ifdef FURI_HAL_USB_VCP_DEBUG
  136. FURI_LOG_D(TAG, "Rx missed");
  137. #endif
  138. missed_rx++;
  139. }
  140. }
  141. // New data in Tx buffer
  142. if((flags & VcpEvtStreamTx) && enabled) {
  143. #ifdef FURI_HAL_USB_VCP_DEBUG
  144. FURI_LOG_D(TAG, "StreamTx");
  145. #endif
  146. if(tx_idle) {
  147. flags |= VcpEvtTx;
  148. }
  149. }
  150. // CDC write transfer done
  151. if((flags & VcpEvtTx) && enabled) {
  152. size_t len =
  153. xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  154. #ifdef FURI_HAL_USB_VCP_DEBUG
  155. FURI_LOG_D(TAG, "Tx %d", len);
  156. #endif
  157. if(len > 0) { // Some data left in Tx buffer. Sending it now
  158. tx_idle = false;
  159. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  160. last_tx_pkt_len = len;
  161. } else { // There is nothing to send.
  162. if(last_tx_pkt_len == 64) {
  163. // Send extra zero-length packet if last packet len is 64 to indicate transfer end
  164. furi_hal_cdc_send(VCP_IF_NUM, NULL, 0);
  165. } else {
  166. // Set flag to start next transfer instantly
  167. tx_idle = true;
  168. }
  169. last_tx_pkt_len = 0;
  170. }
  171. }
  172. }
  173. return 0;
  174. }
  175. void furi_hal_vcp_enable() {
  176. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtEnable);
  177. }
  178. void furi_hal_vcp_disable() {
  179. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisable);
  180. }
  181. size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
  182. furi_assert(vcp);
  183. furi_assert(buffer);
  184. #ifdef FURI_HAL_USB_VCP_DEBUG
  185. FURI_LOG_D(TAG, "rx %u start", size);
  186. #endif
  187. size_t rx_cnt = 0;
  188. while(size > 0) {
  189. size_t batch_size = size;
  190. if(batch_size > VCP_RX_BUF_SIZE) batch_size = VCP_RX_BUF_SIZE;
  191. size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
  192. #ifdef FURI_HAL_USB_VCP_DEBUG
  193. FURI_LOG_D(TAG, "rx %u ", batch_size);
  194. #endif
  195. if(len == 0) break;
  196. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamRx);
  197. size -= len;
  198. buffer += len;
  199. rx_cnt += len;
  200. }
  201. #ifdef FURI_HAL_USB_VCP_DEBUG
  202. FURI_LOG_D(TAG, "rx %u end", size);
  203. #endif
  204. return rx_cnt;
  205. }
  206. size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
  207. furi_assert(vcp);
  208. return furi_hal_vcp_rx_with_timeout(buffer, size, osWaitForever);
  209. }
  210. void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
  211. furi_assert(vcp);
  212. furi_assert(buffer);
  213. #ifdef FURI_HAL_USB_VCP_DEBUG
  214. FURI_LOG_D(TAG, "tx %u start", size);
  215. #endif
  216. while(size > 0 && vcp->connected) {
  217. size_t batch_size = size;
  218. if(batch_size > USB_CDC_PKT_LEN) batch_size = USB_CDC_PKT_LEN;
  219. xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
  220. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamTx);
  221. #ifdef FURI_HAL_USB_VCP_DEBUG
  222. FURI_LOG_D(TAG, "tx %u", batch_size);
  223. #endif
  224. size -= batch_size;
  225. buffer += batch_size;
  226. }
  227. #ifdef FURI_HAL_USB_VCP_DEBUG
  228. FURI_LOG_D(TAG, "tx %u end", size);
  229. #endif
  230. }
  231. static void vcp_state_callback(void* context, uint8_t state) {
  232. if(state == 0) {
  233. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  234. }
  235. }
  236. static void vcp_on_cdc_control_line(void* context, uint8_t state) {
  237. // bit 0: DTR state, bit 1: RTS state
  238. bool dtr = state & (1 << 0);
  239. if(dtr == true) {
  240. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
  241. } else {
  242. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  243. }
  244. }
  245. static void vcp_on_cdc_rx(void* context) {
  246. uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
  247. furi_check((ret & osFlagsError) == 0);
  248. }
  249. static void vcp_on_cdc_tx_complete(void* context) {
  250. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
  251. }
  252. bool furi_hal_vcp_is_connected(void) {
  253. furi_assert(vcp);
  254. return vcp->connected;
  255. }