2021-08-22 10:43:45 +00:00
|
|
|
#include <furi-hal-vcp_i.h>
|
2021-10-06 09:24:09 +00:00
|
|
|
#include <furi-hal-usb-cdc_i.h>
|
2021-08-22 10:43:45 +00:00
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <stream_buffer.h>
|
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
#define APP_RX_DATA_SIZE CDC_DATA_SZ
|
|
|
|
#define APP_TX_DATA_SIZE CDC_DATA_SZ
|
|
|
|
#define FURI_HAL_VCP_RX_BUFFER_SIZE (APP_RX_DATA_SIZE * 16)
|
2021-08-08 18:03:25 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2021-08-22 10:43:45 +00:00
|
|
|
volatile bool connected;
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
StreamBufferHandle_t rx_stream;
|
2021-08-22 10:43:45 +00:00
|
|
|
volatile bool rx_stream_full;
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
osSemaphoreId_t tx_semaphore;
|
|
|
|
} FuriHalVcp;
|
|
|
|
|
|
|
|
static FuriHalVcp* furi_hal_vcp = NULL;
|
|
|
|
|
|
|
|
static const uint8_t ascii_soh = 0x01;
|
|
|
|
static const uint8_t ascii_eot = 0x04;
|
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
static uint8_t* vcp_rx_buf;
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_vcp_init() {
|
|
|
|
furi_hal_vcp = furi_alloc(sizeof(FuriHalVcp));
|
2021-10-06 09:24:09 +00:00
|
|
|
vcp_rx_buf = furi_alloc(APP_RX_DATA_SIZE);
|
2021-08-22 10:43:45 +00:00
|
|
|
furi_hal_vcp->connected = false;
|
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_vcp->rx_stream = xStreamBufferCreate(FURI_HAL_VCP_RX_BUFFER_SIZE, 1);
|
2021-08-22 10:43:45 +00:00
|
|
|
furi_hal_vcp->rx_stream_full = false;
|
2021-08-08 18:03:25 +00:00
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
furi_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
|
2021-08-08 18:03:25 +00:00
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
FURI_LOG_I("FuriHalVcp", "Init OK");
|
2021-08-08 18:03:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
|
|
|
|
furi_assert(furi_hal_vcp);
|
2021-08-08 18:03:25 +00:00
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
size_t received = xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
|
2021-08-08 18:03:25 +00:00
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
if(furi_hal_vcp->rx_stream_full
|
2021-10-06 09:24:09 +00:00
|
|
|
&& xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
|
2021-08-22 10:43:45 +00:00
|
|
|
furi_hal_vcp->rx_stream_full = false;
|
2021-08-08 18:03:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
return received;
|
2021-08-08 18:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
|
|
|
|
furi_assert(furi_hal_vcp);
|
|
|
|
return xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
|
|
|
furi_assert(furi_hal_vcp);
|
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
while (size > 0 && furi_hal_vcp->connected) {
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_check(osSemaphoreAcquire(furi_hal_vcp->tx_semaphore, osWaitForever) == osOK);
|
2021-08-22 10:43:45 +00:00
|
|
|
if (!furi_hal_vcp->connected)
|
|
|
|
break;
|
2021-08-08 18:03:25 +00:00
|
|
|
|
|
|
|
size_t batch_size = size;
|
|
|
|
if (batch_size > APP_TX_DATA_SIZE) {
|
|
|
|
batch_size = APP_TX_DATA_SIZE;
|
|
|
|
}
|
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
furi_hal_cdc_send(0, (uint8_t*)buffer, batch_size);
|
|
|
|
size -= batch_size;
|
|
|
|
buffer += batch_size;
|
2021-08-22 10:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_vcp_on_usb_resume() {
|
|
|
|
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_vcp_on_usb_suspend() {
|
|
|
|
if (furi_hal_vcp->connected) {
|
|
|
|
furi_hal_vcp->connected = false;
|
|
|
|
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
|
2021-10-06 09:24:09 +00:00
|
|
|
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2021-08-22 10:43:45 +00:00
|
|
|
// bit 0: DTR state, bit 1: RTS state
|
|
|
|
// bool dtr = state & 0b01;
|
|
|
|
bool dtr = state & 0b1;
|
|
|
|
|
|
|
|
if (dtr) {
|
|
|
|
if (!furi_hal_vcp->connected) {
|
|
|
|
furi_hal_vcp->connected = true;
|
2021-10-06 09:24:09 +00:00
|
|
|
xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_soh, 1, &xHigherPriorityTaskWoken); // SOH
|
|
|
|
|
2021-08-22 10:43:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (furi_hal_vcp->connected) {
|
2021-10-06 09:24:09 +00:00
|
|
|
xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_eot, 1, &xHigherPriorityTaskWoken); // EOT
|
2021-08-22 10:43:45 +00:00
|
|
|
furi_hal_vcp->connected = false;
|
2021-08-08 18:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-22 10:43:45 +00:00
|
|
|
|
|
|
|
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
2021-10-06 09:24:09 +00:00
|
|
|
|
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
2021-08-08 18:03:25 +00:00
|
|
|
}
|
2021-08-22 10:43:45 +00:00
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
void furi_hal_vcp_on_cdc_rx(uint8_t if_num) {
|
2021-08-22 10:43:45 +00:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2021-10-06 09:24:09 +00:00
|
|
|
|
|
|
|
if (if_num == 0) {
|
|
|
|
uint16_t max_len = xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream);
|
|
|
|
if (max_len > 0) {
|
|
|
|
if (max_len > APP_RX_DATA_SIZE)
|
|
|
|
max_len = APP_RX_DATA_SIZE;
|
|
|
|
int32_t size = furi_hal_cdc_receive(0, vcp_rx_buf, max_len);
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, vcp_rx_buf, size, &xHigherPriorityTaskWoken);
|
|
|
|
furi_check(ret == size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
furi_hal_vcp->rx_stream_full = true;
|
|
|
|
};
|
2021-08-22 10:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
|
2021-10-06 09:24:09 +00:00
|
|
|
void furi_hal_vcp_on_cdc_tx_complete(uint8_t if_num) {
|
|
|
|
if (if_num == 0)
|
|
|
|
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
2021-08-22 10:43:45 +00:00
|
|
|
}
|
|
|
|
|