furi_hal_vcp.c 9.3 KB

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