2022-01-05 19:10:18 +03:00
|
|
|
#include <furi_hal_usb_cdc_i.h>
|
2022-02-16 20:52:34 +03:00
|
|
|
#include <furi_hal.h>
|
2021-09-10 05:19:02 +03:00
|
|
|
#include <furi.h>
|
|
|
|
#include <stream_buffer.h>
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "FuriHalVcp"
|
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
#define USB_CDC_PKT_LEN CDC_DATA_SZ
|
2021-11-11 19:17:50 +03:00
|
|
|
#define VCP_RX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
|
|
|
|
#define VCP_TX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
|
|
|
|
|
2021-10-21 21:12:20 +03:00
|
|
|
#define VCP_IF_NUM 0
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
typedef enum {
|
2022-01-05 19:10:18 +03:00
|
|
|
VcpEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
|
|
|
|
VcpEvtEnable = (1 << 1),
|
|
|
|
VcpEvtDisable = (1 << 2),
|
|
|
|
VcpEvtConnect = (1 << 3),
|
|
|
|
VcpEvtDisconnect = (1 << 4),
|
|
|
|
VcpEvtStreamRx = (1 << 5),
|
|
|
|
VcpEvtRx = (1 << 6),
|
|
|
|
VcpEvtStreamTx = (1 << 7),
|
|
|
|
VcpEvtTx = (1 << 8),
|
2021-11-11 19:17:50 +03:00
|
|
|
} WorkerEvtFlags;
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
#define VCP_THREAD_FLAG_ALL \
|
|
|
|
(VcpEvtEnable | VcpEvtDisable | VcpEvtConnect | VcpEvtDisconnect | VcpEvtRx | VcpEvtTx | \
|
|
|
|
VcpEvtStreamRx | VcpEvtStreamTx)
|
2021-11-04 22:33:28 +03:00
|
|
|
|
2021-09-10 05:19:02 +03:00
|
|
|
typedef struct {
|
2021-11-11 19:17:50 +03:00
|
|
|
FuriThread* thread;
|
2021-11-04 22:33:28 +03:00
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
StreamBufferHandle_t tx_stream;
|
|
|
|
StreamBufferHandle_t rx_stream;
|
2021-11-04 22:33:28 +03:00
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
volatile bool connected;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
uint8_t data_buffer[USB_CDC_PKT_LEN];
|
2021-09-10 05:19:02 +03:00
|
|
|
} FuriHalVcp;
|
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
static int32_t vcp_worker(void* context);
|
2021-11-21 18:17:43 +03:00
|
|
|
static void vcp_on_cdc_tx_complete(void* context);
|
|
|
|
static void vcp_on_cdc_rx(void* context);
|
|
|
|
static void vcp_state_callback(void* context, uint8_t state);
|
|
|
|
static void vcp_on_cdc_control_line(void* context, uint8_t state);
|
2021-10-21 21:12:20 +03:00
|
|
|
|
|
|
|
static CdcCallbacks cdc_cb = {
|
|
|
|
vcp_on_cdc_tx_complete,
|
|
|
|
vcp_on_cdc_rx,
|
|
|
|
vcp_state_callback,
|
|
|
|
vcp_on_cdc_control_line,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
static FuriHalVcp* vcp = NULL;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
|
|
|
static const uint8_t ascii_soh = 0x01;
|
|
|
|
static const uint8_t ascii_eot = 0x04;
|
|
|
|
|
|
|
|
void furi_hal_vcp_init() {
|
2022-02-18 22:53:46 +03:00
|
|
|
vcp = malloc(sizeof(FuriHalVcp));
|
2021-11-04 22:33:28 +03:00
|
|
|
vcp->connected = false;
|
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
|
|
|
|
vcp->rx_stream = xStreamBufferCreate(VCP_RX_BUF_SIZE, 1);
|
|
|
|
|
|
|
|
vcp->thread = furi_thread_alloc();
|
2022-02-16 20:52:34 +03:00
|
|
|
furi_thread_set_name(vcp->thread, "VcpDriver");
|
2021-11-12 16:04:35 +03:00
|
|
|
furi_thread_set_stack_size(vcp->thread, 1024);
|
2021-11-11 19:17:50 +03:00
|
|
|
furi_thread_set_callback(vcp->thread, vcp_worker);
|
|
|
|
furi_thread_start(vcp->thread);
|
2021-11-04 22:33:28 +03:00
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_I(TAG, "Init OK");
|
2021-11-11 19:17:50 +03:00
|
|
|
}
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
static int32_t vcp_worker(void* context) {
|
|
|
|
bool enabled = true;
|
|
|
|
bool tx_idle = false;
|
2021-11-12 16:04:35 +03:00
|
|
|
size_t missed_rx = 0;
|
2022-02-14 13:29:30 +03:00
|
|
|
uint8_t last_tx_pkt_len = 0;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2022-03-23 16:35:25 +03:00
|
|
|
furi_hal_usb_set_config(&usb_cdc_single, NULL);
|
2021-11-21 18:17:43 +03:00
|
|
|
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb, NULL);
|
2021-11-12 16:04:35 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
while(1) {
|
2021-11-11 19:17:50 +03:00
|
|
|
uint32_t flags = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
|
|
|
|
furi_assert((flags & osFlagsError) == 0);
|
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
// VCP enabled
|
2022-01-05 19:10:18 +03:00
|
|
|
if((flags & VcpEvtEnable) && !enabled) {
|
2021-11-21 18:17:43 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "Enable");
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-21 18:17:43 +03:00
|
|
|
flags |= VcpEvtTx;
|
|
|
|
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb, NULL);
|
|
|
|
enabled = true;
|
|
|
|
furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN); // flush Rx buffer
|
2022-01-05 19:10:18 +03:00
|
|
|
if(furi_hal_cdc_get_ctrl_line_state(VCP_IF_NUM) & (1 << 0)) {
|
2021-11-21 18:17:43 +03:00
|
|
|
vcp->connected = true;
|
|
|
|
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VCP disabled
|
|
|
|
if((flags & VcpEvtDisable) && enabled) {
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "Disable");
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-21 18:17:43 +03:00
|
|
|
enabled = false;
|
|
|
|
vcp->connected = false;
|
|
|
|
xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
|
|
|
|
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
|
|
|
|
}
|
|
|
|
|
|
|
|
// VCP session opened
|
|
|
|
if((flags & VcpEvtConnect) && enabled) {
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "Connect");
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
|
|
|
if(vcp->connected == false) {
|
2021-11-21 18:17:43 +03:00
|
|
|
vcp->connected = true;
|
|
|
|
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VCP session closed
|
|
|
|
if((flags & VcpEvtDisconnect) && enabled) {
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "Disconnect");
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
|
|
|
if(vcp->connected == true) {
|
2021-11-21 18:17:43 +03:00
|
|
|
vcp->connected = false;
|
|
|
|
xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
|
|
|
|
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rx buffer was read, maybe there is enough space for new data?
|
2021-11-12 16:04:35 +03:00
|
|
|
if((flags & VcpEvtStreamRx) && enabled && missed_rx > 0) {
|
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "StreamRx");
|
2021-11-12 16:04:35 +03:00
|
|
|
#endif
|
2022-01-05 19:10:18 +03:00
|
|
|
if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
|
2021-11-12 16:04:35 +03:00
|
|
|
flags |= VcpEvtRx;
|
|
|
|
missed_rx--;
|
|
|
|
}
|
2021-11-11 19:17:50 +03:00
|
|
|
}
|
2021-10-21 21:12:20 +03:00
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
// New data received
|
2021-11-12 16:04:35 +03:00
|
|
|
if((flags & VcpEvtRx)) {
|
2022-01-05 19:10:18 +03:00
|
|
|
if(xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
|
2021-11-12 16:04:35 +03:00
|
|
|
int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "Rx %d", len);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
|
|
|
if(len > 0) {
|
|
|
|
furi_check(
|
|
|
|
xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever) ==
|
|
|
|
len);
|
2021-11-12 16:04:35 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "Rx missed");
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-12 16:04:35 +03:00
|
|
|
missed_rx++;
|
2021-11-11 19:17:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New data in Tx buffer
|
2021-11-12 16:04:35 +03:00
|
|
|
if((flags & VcpEvtStreamTx) && enabled) {
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "StreamTx");
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
|
|
|
if(tx_idle) {
|
2021-11-12 16:04:35 +03:00
|
|
|
flags |= VcpEvtTx;
|
2021-11-11 19:17:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CDC write transfer done
|
2021-11-12 16:04:35 +03:00
|
|
|
if((flags & VcpEvtTx) && enabled) {
|
2022-01-05 19:10:18 +03:00
|
|
|
size_t len =
|
|
|
|
xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
|
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "Tx %d", len);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
|
|
|
if(len > 0) { // Some data left in Tx buffer. Sending it now
|
2021-11-11 19:17:50 +03:00
|
|
|
tx_idle = false;
|
|
|
|
furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
|
2022-02-14 13:29:30 +03:00
|
|
|
last_tx_pkt_len = len;
|
|
|
|
} else { // There is nothing to send.
|
|
|
|
if(last_tx_pkt_len == 64) {
|
|
|
|
// Send extra zero-length packet if last packet len is 64 to indicate transfer end
|
|
|
|
furi_hal_cdc_send(VCP_IF_NUM, NULL, 0);
|
|
|
|
} else {
|
|
|
|
// Set flag to start next transfer instantly
|
|
|
|
tx_idle = true;
|
|
|
|
}
|
|
|
|
last_tx_pkt_len = 0;
|
2021-11-11 19:17:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 21:12:20 +03:00
|
|
|
void furi_hal_vcp_enable() {
|
2021-11-11 19:17:50 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtEnable);
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_vcp_disable() {
|
2021-11-11 19:17:50 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisable);
|
2021-10-21 21:12:20 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
|
|
|
|
furi_assert(vcp);
|
|
|
|
furi_assert(buffer);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "rx %u start", size);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-21 18:17:43 +03:00
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
size_t rx_cnt = 0;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
while(size > 0) {
|
2021-11-11 19:17:50 +03:00
|
|
|
size_t batch_size = size;
|
2022-01-05 19:10:18 +03:00
|
|
|
if(batch_size > VCP_RX_BUF_SIZE) batch_size = VCP_RX_BUF_SIZE;
|
2021-11-04 22:33:28 +03:00
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
|
2022-01-05 19:10:18 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2022-02-13 22:24:03 +03:00
|
|
|
FURI_LOG_D(TAG, "rx %u ", batch_size);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
|
|
|
if(len == 0) break;
|
2021-11-12 16:04:35 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamRx);
|
2021-11-11 19:17:50 +03:00
|
|
|
size -= len;
|
|
|
|
buffer += len;
|
2021-11-04 22:33:28 +03:00
|
|
|
rx_cnt += len;
|
|
|
|
}
|
2021-11-21 18:17:43 +03:00
|
|
|
|
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "rx %u end", size);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-04 22:33:28 +03:00
|
|
|
return rx_cnt;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:33:28 +03:00
|
|
|
size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
|
|
|
|
furi_assert(vcp);
|
2021-11-11 19:17:50 +03:00
|
|
|
return furi_hal_vcp_rx_with_timeout(buffer, size, osWaitForever);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
2021-11-04 22:33:28 +03:00
|
|
|
furi_assert(vcp);
|
2021-11-11 19:17:50 +03:00
|
|
|
furi_assert(buffer);
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "tx %u start", size);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-21 18:17:43 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
while(size > 0 && vcp->connected) {
|
2021-09-10 05:19:02 +03:00
|
|
|
size_t batch_size = size;
|
2022-01-05 19:10:18 +03:00
|
|
|
if(batch_size > USB_CDC_PKT_LEN) batch_size = USB_CDC_PKT_LEN;
|
2021-09-10 05:19:02 +03:00
|
|
|
|
2021-11-11 19:17:50 +03:00
|
|
|
xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
|
2021-11-12 16:04:35 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamTx);
|
2021-11-21 18:17:43 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2022-02-13 22:24:03 +03:00
|
|
|
FURI_LOG_D(TAG, "tx %u", batch_size);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-04 22:33:28 +03:00
|
|
|
|
2021-10-06 12:24:09 +03:00
|
|
|
size -= batch_size;
|
|
|
|
buffer += batch_size;
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#ifdef FURI_HAL_USB_VCP_DEBUG
|
2021-11-21 18:17:43 +03:00
|
|
|
FURI_LOG_D(TAG, "tx %u end", size);
|
2022-01-05 19:10:18 +03:00
|
|
|
#endif
|
2021-11-21 18:17:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vcp_state_callback(void* context, uint8_t state) {
|
2022-01-05 19:10:18 +03:00
|
|
|
if(state == 0) {
|
2021-11-11 19:17:50 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
static void vcp_on_cdc_control_line(void* context, uint8_t state) {
|
2021-09-10 05:19:02 +03:00
|
|
|
// bit 0: DTR state, bit 1: RTS state
|
2021-11-11 19:17:50 +03:00
|
|
|
bool dtr = state & (1 << 0);
|
2021-11-21 18:17:43 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
if(dtr == true) {
|
2021-11-11 19:17:50 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
|
2021-09-10 05:19:02 +03:00
|
|
|
} else {
|
2021-11-11 19:17:50 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
static void vcp_on_cdc_rx(void* context) {
|
2021-11-12 16:04:35 +03:00
|
|
|
uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
|
2021-11-28 21:28:19 +03:00
|
|
|
furi_check((ret & osFlagsError) == 0);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
2021-11-21 18:17:43 +03:00
|
|
|
static void vcp_on_cdc_tx_complete(void* context) {
|
2021-11-12 16:04:35 +03:00
|
|
|
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
|
2021-09-10 05:19:02 +03:00
|
|
|
}
|
2021-10-26 19:05:28 +03:00
|
|
|
|
|
|
|
bool furi_hal_vcp_is_connected(void) {
|
2021-11-11 19:17:50 +03:00
|
|
|
furi_assert(vcp);
|
2021-11-04 22:33:28 +03:00
|
|
|
return vcp->connected;
|
2021-10-26 19:05:28 +03:00
|
|
|
}
|