[FL-1857] New USB stack (#735)
* libusb_stm32 USB stack test * USB: Add dual CDC mode * USB HID demo, added libusb_stm32 as submodule * Target F6/F7: remomve unused ll_usb Co-authored-by: n.minaylov <n.minaylov@flipperdevices.com> Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
#include <furi-hal-vcp_i.h>
|
||||
#include <furi-hal-usb-cdc_i.h>
|
||||
|
||||
#include <furi.h>
|
||||
#include <usbd_cdc_if.h>
|
||||
#include <stream_buffer.h>
|
||||
|
||||
#define FURI_HAL_VCP_RX_BUFFER_SIZE (APP_RX_DATA_SIZE * 5)
|
||||
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
#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)
|
||||
|
||||
typedef struct {
|
||||
volatile bool connected;
|
||||
@@ -22,8 +22,11 @@ static FuriHalVcp* furi_hal_vcp = NULL;
|
||||
static const uint8_t ascii_soh = 0x01;
|
||||
static const uint8_t ascii_eot = 0x04;
|
||||
|
||||
static uint8_t* vcp_rx_buf;
|
||||
|
||||
void furi_hal_vcp_init() {
|
||||
furi_hal_vcp = furi_alloc(sizeof(FuriHalVcp));
|
||||
vcp_rx_buf = furi_alloc(APP_RX_DATA_SIZE);
|
||||
furi_hal_vcp->connected = false;
|
||||
|
||||
furi_hal_vcp->rx_stream = xStreamBufferCreate(FURI_HAL_VCP_RX_BUFFER_SIZE, 1);
|
||||
@@ -40,10 +43,8 @@ size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
|
||||
size_t received = xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
|
||||
|
||||
if(furi_hal_vcp->rx_stream_full
|
||||
&&xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
|
||||
&& xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
|
||||
furi_hal_vcp->rx_stream_full = false;
|
||||
// data accepted, start waiting for next packet
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
}
|
||||
|
||||
return received;
|
||||
@@ -67,13 +68,9 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
||||
batch_size = APP_TX_DATA_SIZE;
|
||||
}
|
||||
|
||||
if (CDC_Transmit_FS((uint8_t*)buffer, batch_size) == USBD_OK) {
|
||||
size -= batch_size;
|
||||
buffer += batch_size;
|
||||
} else {
|
||||
FURI_LOG_E("FuriHalVcp", "CDC_Transmit_FS failed");
|
||||
osDelay(50);
|
||||
}
|
||||
furi_hal_cdc_send(0, (uint8_t*)buffer, batch_size);
|
||||
size -= batch_size;
|
||||
buffer += batch_size;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +86,8 @@ void furi_hal_vcp_on_usb_suspend() {
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
// bit 0: DTR state, bit 1: RTS state
|
||||
// bool dtr = state & 0b01;
|
||||
bool dtr = state & 0b1;
|
||||
@@ -96,33 +95,45 @@ void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
|
||||
if (dtr) {
|
||||
if (!furi_hal_vcp->connected) {
|
||||
furi_hal_vcp->connected = true;
|
||||
furi_hal_vcp_on_cdc_rx(&ascii_soh, 1); // SOH
|
||||
xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_soh, 1, &xHigherPriorityTaskWoken); // SOH
|
||||
|
||||
}
|
||||
} else {
|
||||
if (furi_hal_vcp->connected) {
|
||||
furi_hal_vcp_on_cdc_rx(&ascii_eot, 1); // EOT
|
||||
xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, &ascii_eot, 1, &xHigherPriorityTaskWoken); // EOT
|
||||
furi_hal_vcp->connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_rx(const uint8_t* buffer, size_t size) {
|
||||
void furi_hal_vcp_on_cdc_rx(uint8_t if_num) {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, buffer, size, &xHigherPriorityTaskWoken);
|
||||
furi_check(ret == size);
|
||||
|
||||
if (xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
} else {
|
||||
furi_hal_vcp->rx_stream_full = true;
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_tx_complete(size_t size) {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
void furi_hal_vcp_on_cdc_tx_complete(uint8_t if_num) {
|
||||
if (if_num == 0)
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user