furi-hal-vcp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <furi-hal-vcp_i.h>
  2. #include <furi.h>
  3. #include <usbd_cdc_if.h>
  4. #include <stream_buffer.h>
  5. #define FURI_HAL_VCP_RX_BUFFER_SIZE (APP_RX_DATA_SIZE * 5)
  6. extern USBD_HandleTypeDef hUsbDeviceFS;
  7. typedef struct {
  8. volatile bool connected;
  9. StreamBufferHandle_t rx_stream;
  10. volatile bool rx_stream_full;
  11. osSemaphoreId_t tx_semaphore;
  12. } FuriHalVcp;
  13. static FuriHalVcp* furi_hal_vcp = NULL;
  14. static const uint8_t ascii_soh = 0x01;
  15. static const uint8_t ascii_eot = 0x04;
  16. void furi_hal_vcp_init() {
  17. furi_hal_vcp = furi_alloc(sizeof(FuriHalVcp));
  18. furi_hal_vcp->connected = false;
  19. furi_hal_vcp->rx_stream = xStreamBufferCreate(FURI_HAL_VCP_RX_BUFFER_SIZE, 1);
  20. furi_hal_vcp->rx_stream_full = false;
  21. furi_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
  22. FURI_LOG_I("FuriHalVcp", "Init OK");
  23. }
  24. size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
  25. furi_assert(furi_hal_vcp);
  26. size_t received = xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
  27. if(furi_hal_vcp->rx_stream_full
  28. &&xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
  29. furi_hal_vcp->rx_stream_full = false;
  30. // data accepted, start waiting for next packet
  31. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  32. }
  33. return received;
  34. }
  35. size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
  36. furi_assert(furi_hal_vcp);
  37. return xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, timeout);
  38. }
  39. void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
  40. furi_assert(furi_hal_vcp);
  41. while (size > 0 && furi_hal_vcp->connected) {
  42. furi_check(osSemaphoreAcquire(furi_hal_vcp->tx_semaphore, osWaitForever) == osOK);
  43. if (!furi_hal_vcp->connected)
  44. break;
  45. size_t batch_size = size;
  46. if (batch_size > APP_TX_DATA_SIZE) {
  47. batch_size = APP_TX_DATA_SIZE;
  48. }
  49. if (CDC_Transmit_FS((uint8_t*)buffer, batch_size) == USBD_OK) {
  50. size -= batch_size;
  51. buffer += batch_size;
  52. } else {
  53. FURI_LOG_E("FuriHalVcp", "CDC_Transmit_FS failed");
  54. osDelay(50);
  55. }
  56. }
  57. }
  58. void furi_hal_vcp_on_usb_resume() {
  59. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  60. }
  61. void furi_hal_vcp_on_usb_suspend() {
  62. if (furi_hal_vcp->connected) {
  63. furi_hal_vcp->connected = false;
  64. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  65. }
  66. }
  67. void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
  68. // bit 0: DTR state, bit 1: RTS state
  69. // bool dtr = state & 0b01;
  70. bool dtr = state & 0b1;
  71. if (dtr) {
  72. if (!furi_hal_vcp->connected) {
  73. furi_hal_vcp->connected = true;
  74. furi_hal_vcp_on_cdc_rx(&ascii_soh, 1); // SOH
  75. }
  76. } else {
  77. if (furi_hal_vcp->connected) {
  78. furi_hal_vcp_on_cdc_rx(&ascii_eot, 1); // EOT
  79. furi_hal_vcp->connected = false;
  80. }
  81. }
  82. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  83. }
  84. void furi_hal_vcp_on_cdc_rx(const uint8_t* buffer, size_t size) {
  85. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  86. size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, buffer, size, &xHigherPriorityTaskWoken);
  87. furi_check(ret == size);
  88. if (xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
  89. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  90. } else {
  91. furi_hal_vcp->rx_stream_full = true;
  92. }
  93. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  94. }
  95. void furi_hal_vcp_on_cdc_tx_complete(size_t size) {
  96. osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
  97. }