furi-hal-vcp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <furi-hal-usb-cdc_i.h>
  2. #include <furi.h>
  3. #include <stream_buffer.h>
  4. #define USB_CDC_PKT_LEN CDC_DATA_SZ
  5. #define VCP_RX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  6. #define VCP_TX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
  7. #define VCP_IF_NUM 0
  8. typedef enum {
  9. VcpEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
  10. VcpEvtConnect = (1 << 1),
  11. VcpEvtDisconnect = (1 << 2),
  12. VcpEvtEnable = (1 << 3),
  13. VcpEvtDisable = (1 << 4),
  14. VcpEvtRx = (1 << 5),
  15. VcpEvtTx = (1 << 6),
  16. VcpEvtRxDone = (1 << 7),
  17. VcpEvtTxDone = (1 << 8),
  18. } WorkerEvtFlags;
  19. #define VCP_THREAD_FLAG_ALL (VcpEvtConnect | VcpEvtDisconnect | VcpEvtEnable | VcpEvtDisable | VcpEvtRx | VcpEvtTx | VcpEvtRxDone | VcpEvtTxDone)
  20. typedef struct {
  21. FuriThread* thread;
  22. StreamBufferHandle_t tx_stream;
  23. StreamBufferHandle_t rx_stream;
  24. volatile bool connected;
  25. uint8_t data_buffer[USB_CDC_PKT_LEN];
  26. } FuriHalVcp;
  27. static int32_t vcp_worker(void* context);
  28. static void vcp_on_cdc_tx_complete();
  29. static void vcp_on_cdc_rx();
  30. static void vcp_state_callback(uint8_t state);
  31. static void vcp_on_cdc_control_line(uint8_t state);
  32. static CdcCallbacks cdc_cb = {
  33. vcp_on_cdc_tx_complete,
  34. vcp_on_cdc_rx,
  35. vcp_state_callback,
  36. vcp_on_cdc_control_line,
  37. NULL,
  38. };
  39. static FuriHalVcp* vcp = NULL;
  40. static const uint8_t ascii_soh = 0x01;
  41. static const uint8_t ascii_eot = 0x04;
  42. void furi_hal_vcp_init() {
  43. vcp = furi_alloc(sizeof(FuriHalVcp));
  44. vcp->connected = false;
  45. vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
  46. vcp->rx_stream = xStreamBufferCreate(VCP_RX_BUF_SIZE, 1);
  47. vcp->thread = furi_thread_alloc();
  48. furi_thread_set_name(vcp->thread, "VcpWorker");
  49. furi_thread_set_stack_size(vcp->thread, 512);
  50. furi_thread_set_callback(vcp->thread, vcp_worker);
  51. furi_thread_start(vcp->thread);
  52. FURI_LOG_I("FuriHalVcp", "Init OK");
  53. }
  54. static int32_t vcp_worker(void* context) {
  55. bool enabled = true;
  56. bool tx_idle = false;
  57. bool rx_pending = false;
  58. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
  59. while (1) {
  60. uint32_t flags = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
  61. furi_assert((flags & osFlagsError) == 0);
  62. // New data received
  63. if((flags & VcpEvtRxDone) && enabled) {
  64. if (xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  65. size_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  66. if (len > 0)
  67. xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever);
  68. else
  69. rx_pending = false;
  70. } else
  71. rx_pending = true; // Buffer is full, retry later
  72. }
  73. // Rx buffer was read, maybe there is enough space for new data?
  74. if((flags & VcpEvtRx) && rx_pending) {
  75. if (xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
  76. size_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
  77. if (len > 0)
  78. xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever);
  79. else
  80. rx_pending = false;
  81. }
  82. }
  83. // New data in Tx buffer
  84. if((flags & VcpEvtTx) && enabled) {
  85. if (tx_idle) {
  86. size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  87. if (len > 0) {
  88. tx_idle = false;
  89. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  90. }
  91. }
  92. }
  93. // CDC write transfer done
  94. if((flags & VcpEvtTxDone) && enabled) {
  95. size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
  96. if (len > 0) { // Some data left in Tx buffer. Sending it now
  97. tx_idle = false;
  98. furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
  99. } else { // There is nothing to send. Set flag to start next transfer instantly
  100. tx_idle = true;
  101. }
  102. }
  103. // VCP session opened
  104. if((flags & VcpEvtConnect) && enabled) {
  105. if (vcp->connected == false) {
  106. vcp->connected = true;
  107. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  108. }
  109. }
  110. // VCP session closed
  111. if((flags & VcpEvtDisconnect) && enabled) {
  112. if (vcp->connected == true) {
  113. vcp->connected = false;
  114. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  115. }
  116. }
  117. // VCP enabled
  118. if((flags & VcpEvtEnable) && !enabled){
  119. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
  120. enabled = true;
  121. furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN); // flush Rx buffer
  122. if (furi_hal_cdc_get_ctrl_line_state(VCP_IF_NUM) & (1 << 0)) {
  123. vcp->connected = true;
  124. xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
  125. }
  126. }
  127. // VCP disabled
  128. if((flags & VcpEvtDisable) && enabled) {
  129. enabled = false;
  130. vcp->connected = false;
  131. xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
  132. }
  133. }
  134. return 0;
  135. }
  136. void furi_hal_vcp_enable() {
  137. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtEnable);
  138. }
  139. void furi_hal_vcp_disable() {
  140. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisable);
  141. }
  142. size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
  143. furi_assert(vcp);
  144. furi_assert(buffer);
  145. size_t rx_cnt = 0;
  146. while (size > 0) {
  147. size_t batch_size = size;
  148. if (batch_size > VCP_RX_BUF_SIZE)
  149. batch_size = VCP_RX_BUF_SIZE;
  150. size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
  151. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
  152. if (len == 0)
  153. break;
  154. size -= len;
  155. buffer += len;
  156. rx_cnt += len;
  157. }
  158. return rx_cnt;
  159. }
  160. size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
  161. furi_assert(vcp);
  162. return furi_hal_vcp_rx_with_timeout(buffer, size, osWaitForever);
  163. }
  164. void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
  165. furi_assert(vcp);
  166. furi_assert(buffer);
  167. while (size > 0) {
  168. size_t batch_size = size;
  169. if (batch_size > VCP_TX_BUF_SIZE)
  170. batch_size = VCP_TX_BUF_SIZE;
  171. xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
  172. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
  173. size -= batch_size;
  174. buffer += batch_size;
  175. }
  176. }
  177. static void vcp_state_callback(uint8_t state) {
  178. if (state == 0) {
  179. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  180. }
  181. }
  182. static void vcp_on_cdc_control_line(uint8_t state) {
  183. // bit 0: DTR state, bit 1: RTS state
  184. bool dtr = state & (1 << 0);
  185. if (dtr == true) {
  186. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
  187. } else {
  188. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
  189. }
  190. }
  191. static void vcp_on_cdc_rx() {
  192. uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRxDone);
  193. furi_assert((ret & osFlagsError) == 0);
  194. }
  195. static void vcp_on_cdc_tx_complete() {
  196. osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTxDone);
  197. }
  198. bool furi_hal_vcp_is_connected(void) {
  199. furi_assert(vcp);
  200. return vcp->connected;
  201. }