furi_hal_vcp.c 8.8 KB

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