furi-hal-vcp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <furi-hal-vcp_i.h>
  2. #include <furi-hal-usb-cdc_i.h>
  3. #include <furi.h>
  4. #include <stream_buffer.h>
  5. #define APP_RX_DATA_SIZE CDC_DATA_SZ
  6. #define APP_TX_DATA_SIZE CDC_DATA_SZ
  7. #define FURI_HAL_VCP_RX_BUFFER_SIZE (APP_RX_DATA_SIZE * 16)
  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 FuriHalVcp* furi_hal_vcp = NULL;
  15. static const uint8_t ascii_soh = 0x01;
  16. static const uint8_t ascii_eot = 0x04;
  17. static uint8_t* vcp_rx_buf;
  18. void furi_hal_vcp_init() {
  19. furi_hal_vcp = furi_alloc(sizeof(FuriHalVcp));
  20. vcp_rx_buf = furi_alloc(APP_RX_DATA_SIZE);
  21. furi_hal_vcp->connected = false;
  22. furi_hal_vcp->rx_stream = xStreamBufferCreate(FURI_HAL_VCP_RX_BUFFER_SIZE, 1);
  23. furi_hal_vcp->rx_stream_full = false;
  24. furi_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
  25. FURI_LOG_I("FuriHalVcp", "Init OK");
  26. }
  27. size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
  28. furi_assert(furi_hal_vcp);
  29. size_t received = xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
  30. if(furi_hal_vcp->rx_stream_full
  31. && xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
  32. furi_hal_vcp->rx_stream_full = false;
  33. }
  34. return received;
  35. }
  36. size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
  37. furi_assert(furi_hal_vcp);
  38. return xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, timeout);
  39. }
  40. void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
  41. furi_assert(furi_hal_vcp);
  42. while (size > 0 && furi_hal_vcp->connected) {
  43. furi_check(osSemaphoreAcquire(furi_hal_vcp->tx_semaphore, osWaitForever) == osOK);
  44. if (!furi_hal_vcp->connected)
  45. break;
  46. size_t batch_size = size;
  47. if (batch_size > APP_TX_DATA_SIZE) {
  48. batch_size = APP_TX_DATA_SIZE;
  49. }
  50. furi_hal_cdc_send(0, (uint8_t*)buffer, batch_size);
  51. size -= batch_size;
  52. buffer += batch_size;
  53. }
  54. }
  55. void furi_hal_vcp_on_usb_resume() {
  56. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  57. }
  58. void furi_hal_vcp_on_usb_suspend() {
  59. if (furi_hal_vcp->connected) {
  60. furi_hal_vcp->connected = false;
  61. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  62. }
  63. }
  64. void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
  65. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  66. // bit 0: DTR state, bit 1: RTS state
  67. // bool dtr = state & 0b01;
  68. bool dtr = state & 0b1;
  69. if (dtr) {
  70. if (!furi_hal_vcp->connected) {
  71. furi_hal_vcp->connected = true;
  72. xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_soh, 1, &xHigherPriorityTaskWoken); // SOH
  73. }
  74. } else {
  75. if (furi_hal_vcp->connected) {
  76. xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_eot, 1, &xHigherPriorityTaskWoken); // EOT
  77. furi_hal_vcp->connected = false;
  78. }
  79. }
  80. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  81. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  82. }
  83. void furi_hal_vcp_on_cdc_rx(uint8_t if_num) {
  84. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  85. if (if_num == 0) {
  86. uint16_t max_len = xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream);
  87. if (max_len > 0) {
  88. if (max_len > APP_RX_DATA_SIZE)
  89. max_len = APP_RX_DATA_SIZE;
  90. int32_t size = furi_hal_cdc_receive(0, vcp_rx_buf, max_len);
  91. if (size > 0) {
  92. size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, vcp_rx_buf, size, &xHigherPriorityTaskWoken);
  93. furi_check(ret == size);
  94. }
  95. } else {
  96. furi_hal_vcp->rx_stream_full = true;
  97. };
  98. }
  99. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  100. }
  101. void furi_hal_vcp_on_cdc_tx_complete(uint8_t if_num) {
  102. if (if_num == 0)
  103. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  104. }