furi_hal_vcp.c 9.3 KB

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