furi-hal-vcp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <furi-hal-usb-cdc_i.h>
  2. #include <furi.h>
  3. #include <stream_buffer.h>
  4. #define APP_RX_DATA_SIZE CDC_DATA_SZ
  5. #define APP_TX_DATA_SIZE CDC_DATA_SZ
  6. #define FURI_HAL_VCP_RX_BUFFER_SIZE (APP_RX_DATA_SIZE * 16)
  7. #define VCP_IF_NUM 0
  8. typedef struct {
  9. volatile bool connected;
  10. StreamBufferHandle_t rx_stream;
  11. volatile bool rx_stream_full;
  12. osSemaphoreId_t tx_semaphore;
  13. } FuriHalVcp;
  14. static void vcp_on_cdc_tx_complete();
  15. static void vcp_on_cdc_rx();
  16. static void vcp_state_callback(uint8_t state);
  17. static void vcp_on_cdc_control_line(uint8_t state);
  18. static CdcCallbacks cdc_cb = {
  19. vcp_on_cdc_tx_complete,
  20. vcp_on_cdc_rx,
  21. vcp_state_callback,
  22. vcp_on_cdc_control_line,
  23. NULL,
  24. };
  25. static FuriHalVcp* furi_hal_vcp = NULL;
  26. static const uint8_t ascii_soh = 0x01;
  27. static const uint8_t ascii_eot = 0x04;
  28. static uint8_t* vcp_rx_buf;
  29. void furi_hal_vcp_init() {
  30. furi_hal_vcp = furi_alloc(sizeof(FuriHalVcp));
  31. vcp_rx_buf = furi_alloc(APP_RX_DATA_SIZE);
  32. furi_hal_vcp->connected = false;
  33. furi_hal_vcp->rx_stream = xStreamBufferCreate(FURI_HAL_VCP_RX_BUFFER_SIZE, 1);
  34. furi_hal_vcp->rx_stream_full = false;
  35. furi_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
  36. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
  37. FURI_LOG_I("FuriHalVcp", "Init OK");
  38. }
  39. void furi_hal_vcp_enable() {
  40. furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
  41. furi_hal_vcp->connected = true;
  42. }
  43. void furi_hal_vcp_disable() {
  44. furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL);
  45. furi_hal_vcp->connected = false;
  46. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  47. }
  48. size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
  49. furi_assert(furi_hal_vcp);
  50. size_t received = xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
  51. if(furi_hal_vcp->rx_stream_full
  52. && xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
  53. furi_hal_vcp->rx_stream_full = false;
  54. }
  55. return received;
  56. }
  57. size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
  58. furi_assert(furi_hal_vcp);
  59. return xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, timeout);
  60. }
  61. void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
  62. furi_assert(furi_hal_vcp);
  63. while (size > 0 && furi_hal_vcp->connected) {
  64. furi_check(osSemaphoreAcquire(furi_hal_vcp->tx_semaphore, osWaitForever) == osOK);
  65. if (!furi_hal_vcp->connected)
  66. break;
  67. size_t batch_size = size;
  68. if (batch_size > APP_TX_DATA_SIZE) {
  69. batch_size = APP_TX_DATA_SIZE;
  70. }
  71. furi_hal_cdc_send(VCP_IF_NUM, (uint8_t*)buffer, batch_size);
  72. size -= batch_size;
  73. buffer += batch_size;
  74. }
  75. }
  76. static void vcp_state_callback(uint8_t state) {
  77. if (state == 1)
  78. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  79. else if (furi_hal_vcp->connected) {
  80. furi_hal_vcp->connected = false;
  81. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  82. }
  83. }
  84. static void vcp_on_cdc_control_line(uint8_t state) {
  85. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  86. // bit 0: DTR state, bit 1: RTS state
  87. // bool dtr = state & 0b01;
  88. bool dtr = state & 0b1;
  89. if (dtr) {
  90. if (!furi_hal_vcp->connected) {
  91. furi_hal_vcp->connected = true;
  92. xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_soh, 1, &xHigherPriorityTaskWoken); // SOH
  93. }
  94. } else {
  95. if (furi_hal_vcp->connected) {
  96. xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_eot, 1, &xHigherPriorityTaskWoken); // EOT
  97. furi_hal_vcp->connected = false;
  98. }
  99. }
  100. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  101. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  102. }
  103. static void vcp_on_cdc_rx() {
  104. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  105. uint16_t max_len = xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream);
  106. if (max_len > 0) {
  107. if (max_len > APP_RX_DATA_SIZE)
  108. max_len = APP_RX_DATA_SIZE;
  109. int32_t size = furi_hal_cdc_receive(VCP_IF_NUM, vcp_rx_buf, max_len);
  110. if (size > 0) {
  111. size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, vcp_rx_buf, size, &xHigherPriorityTaskWoken);
  112. furi_check(ret == size);
  113. }
  114. } else {
  115. furi_hal_vcp->rx_stream_full = true;
  116. };
  117. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  118. }
  119. static void vcp_on_cdc_tx_complete() {
  120. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  121. }
  122. bool furi_hal_vcp_is_connected(void) {
  123. return furi_hal_vcp->connected;
  124. }