furi-hal-vcp.c 9.0 KB

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