RPC: Add Virtual Display & Unify log tags (#814)
* RPC: Update protobuf sources * RPC: Add Virtual Display * Unify log tags * RPC: Virtual Display placeholder * Rpc: clear frame buffer callback before confirm. * Firmware: full assert for hal, move fatfs initialization to furi hal. * FuriHal: VCP optimizations, thread safe console. Rpc: adjust buffer sizes. Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#include <furi-hal-usb-cdc_i.h>
|
||||
|
||||
#include <furi-hal-console.h>
|
||||
#include <furi.h>
|
||||
#include <stream_buffer.h>
|
||||
|
||||
#define TAG "FuriHalVcp"
|
||||
|
||||
#define USB_CDC_PKT_LEN CDC_DATA_SZ
|
||||
#define VCP_RX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
|
||||
#define VCP_TX_BUF_SIZE (USB_CDC_PKT_LEN * 3)
|
||||
@@ -17,11 +19,11 @@ typedef enum {
|
||||
VcpEvtDisable = (1 << 4),
|
||||
VcpEvtRx = (1 << 5),
|
||||
VcpEvtTx = (1 << 6),
|
||||
VcpEvtRxDone = (1 << 7),
|
||||
VcpEvtTxDone = (1 << 8),
|
||||
VcpEvtStreamRx = (1 << 7),
|
||||
VcpEvtStreamTx = (1 << 8),
|
||||
} WorkerEvtFlags;
|
||||
|
||||
#define VCP_THREAD_FLAG_ALL (VcpEvtConnect | VcpEvtDisconnect | VcpEvtEnable | VcpEvtDisable | VcpEvtRx | VcpEvtTx | VcpEvtRxDone | VcpEvtTxDone)
|
||||
#define VCP_THREAD_FLAG_ALL (VcpEvtConnect | VcpEvtDisconnect | VcpEvtEnable | VcpEvtDisable | VcpEvtRx | VcpEvtTx | VcpEvtStreamRx | VcpEvtStreamTx)
|
||||
|
||||
typedef struct {
|
||||
FuriThread* thread;
|
||||
@@ -62,61 +64,69 @@ void furi_hal_vcp_init() {
|
||||
|
||||
vcp->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(vcp->thread, "VcpWorker");
|
||||
furi_thread_set_stack_size(vcp->thread, 512);
|
||||
furi_thread_set_stack_size(vcp->thread, 1024);
|
||||
furi_thread_set_callback(vcp->thread, vcp_worker);
|
||||
furi_thread_start(vcp->thread);
|
||||
|
||||
FURI_LOG_I("FuriHalVcp", "Init OK");
|
||||
FURI_LOG_I(TAG, "Init OK");
|
||||
}
|
||||
|
||||
static int32_t vcp_worker(void* context) {
|
||||
bool enabled = true;
|
||||
bool tx_idle = false;
|
||||
bool rx_pending = false;
|
||||
size_t missed_rx = 0;
|
||||
|
||||
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
|
||||
|
||||
|
||||
while (1) {
|
||||
uint32_t flags = osThreadFlagsWait(VCP_THREAD_FLAG_ALL, osFlagsWaitAny, osWaitForever);
|
||||
furi_assert((flags & osFlagsError) == 0);
|
||||
|
||||
// New data received
|
||||
if((flags & VcpEvtRxDone) && enabled) {
|
||||
if((flags & VcpEvtStreamRx) && enabled && missed_rx > 0) {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP StreamRx\r\n");
|
||||
#endif
|
||||
if (xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
|
||||
size_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
|
||||
if (len > 0)
|
||||
xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever);
|
||||
else
|
||||
rx_pending = false;
|
||||
} else
|
||||
rx_pending = true; // Buffer is full, retry later
|
||||
flags |= VcpEvtRx;
|
||||
missed_rx--;
|
||||
}
|
||||
}
|
||||
|
||||
// Rx buffer was read, maybe there is enough space for new data?
|
||||
if((flags & VcpEvtRx) && rx_pending) {
|
||||
if((flags & VcpEvtRx)) {
|
||||
if (xStreamBufferSpacesAvailable(vcp->rx_stream) >= USB_CDC_PKT_LEN) {
|
||||
size_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
|
||||
if (len > 0)
|
||||
xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever);
|
||||
else
|
||||
rx_pending = false;
|
||||
int32_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN);
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_printf("VCP Rx %d\r\n", len);
|
||||
#endif
|
||||
if (len > 0) {
|
||||
furi_check(xStreamBufferSend(vcp->rx_stream, vcp->data_buffer, len, osWaitForever) == len);
|
||||
}
|
||||
} else {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP Rx missed\r\n");
|
||||
#endif
|
||||
missed_rx++;
|
||||
}
|
||||
}
|
||||
|
||||
// New data in Tx buffer
|
||||
if((flags & VcpEvtTx) && enabled) {
|
||||
if((flags & VcpEvtStreamTx) && enabled) {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP StreamTx\r\n");
|
||||
#endif
|
||||
if (tx_idle) {
|
||||
size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
|
||||
if (len > 0) {
|
||||
tx_idle = false;
|
||||
furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
|
||||
}
|
||||
flags |= VcpEvtTx;
|
||||
}
|
||||
}
|
||||
|
||||
// CDC write transfer done
|
||||
if((flags & VcpEvtTxDone) && enabled) {
|
||||
if((flags & VcpEvtTx) && enabled) {
|
||||
size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_printf("VCP Tx %d\r\n", len);
|
||||
#endif
|
||||
if (len > 0) { // Some data left in Tx buffer. Sending it now
|
||||
tx_idle = false;
|
||||
furi_hal_cdc_send(VCP_IF_NUM, vcp->data_buffer, len);
|
||||
@@ -127,6 +137,9 @@ static int32_t vcp_worker(void* context) {
|
||||
|
||||
// VCP session opened
|
||||
if((flags & VcpEvtConnect) && enabled) {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP Connect\r\n");
|
||||
#endif
|
||||
if (vcp->connected == false) {
|
||||
vcp->connected = true;
|
||||
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
|
||||
@@ -135,6 +148,9 @@ static int32_t vcp_worker(void* context) {
|
||||
|
||||
// VCP session closed
|
||||
if((flags & VcpEvtDisconnect) && enabled) {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP Disconnect\r\n");
|
||||
#endif
|
||||
if (vcp->connected == true) {
|
||||
vcp->connected = false;
|
||||
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
|
||||
@@ -143,6 +159,9 @@ static int32_t vcp_worker(void* context) {
|
||||
|
||||
// VCP enabled
|
||||
if((flags & VcpEvtEnable) && !enabled){
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP Enable\r\n");
|
||||
#endif
|
||||
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
|
||||
enabled = true;
|
||||
furi_hal_cdc_receive(VCP_IF_NUM, vcp->data_buffer, USB_CDC_PKT_LEN); // flush Rx buffer
|
||||
@@ -154,6 +173,9 @@ static int32_t vcp_worker(void* context) {
|
||||
|
||||
// VCP disabled
|
||||
if((flags & VcpEvtDisable) && enabled) {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP Disable\r\n");
|
||||
#endif
|
||||
enabled = false;
|
||||
vcp->connected = false;
|
||||
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
|
||||
@@ -182,9 +204,9 @@ size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeo
|
||||
batch_size = VCP_RX_BUF_SIZE;
|
||||
|
||||
size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
|
||||
if (len == 0)
|
||||
break;
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamRx);
|
||||
size -= len;
|
||||
buffer += len;
|
||||
rx_cnt += len;
|
||||
@@ -207,7 +229,7 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
||||
batch_size = VCP_TX_BUF_SIZE;
|
||||
|
||||
xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtStreamTx);
|
||||
|
||||
size -= batch_size;
|
||||
buffer += batch_size;
|
||||
@@ -215,6 +237,9 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
||||
}
|
||||
|
||||
static void vcp_state_callback(uint8_t state) {
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP State\r\n");
|
||||
#endif
|
||||
if (state == 0) {
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
|
||||
}
|
||||
@@ -223,7 +248,9 @@ static void vcp_state_callback(uint8_t state) {
|
||||
static void vcp_on_cdc_control_line(uint8_t state) {
|
||||
// bit 0: DTR state, bit 1: RTS state
|
||||
bool dtr = state & (1 << 0);
|
||||
|
||||
#ifdef FURI_HAL_USB_VCP_DEBUG
|
||||
furi_hal_console_puts("VCP CtrlLine\r\n");
|
||||
#endif
|
||||
if (dtr == true) {
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
|
||||
} else {
|
||||
@@ -232,12 +259,12 @@ static void vcp_on_cdc_control_line(uint8_t state) {
|
||||
}
|
||||
|
||||
static void vcp_on_cdc_rx() {
|
||||
uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRxDone);
|
||||
uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
|
||||
furi_assert((ret & osFlagsError) == 0);
|
||||
}
|
||||
|
||||
static void vcp_on_cdc_tx_complete() {
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTxDone);
|
||||
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
|
||||
}
|
||||
|
||||
bool furi_hal_vcp_is_connected(void) {
|
||||
|
Reference in New Issue
Block a user