[FL-2010] furi-hal-vcp rework (#812)

* [FL-2010] furi-hal-vcp rework
* Fix connect state change on vcp enable
* New thread naming scheme and stack size adjustment.
* Applications: rename worker threads to match new naming scheme.

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Nikolay Minaylov 2021-11-11 19:17:50 +03:00 committed by GitHub
parent ac8b1457f2
commit b564e8eb38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 364 additions and 263 deletions

View file

@ -65,55 +65,55 @@ extern int32_t power_settings_app(void* p);
const FlipperApplication FLIPPER_SERVICES[] = {
/* Services */
#ifdef SRV_RPC
{.app = rpc_srv, .name = "RPC", .stack_size = 1024 * 4, .icon = NULL},
{.app = rpc_srv, .name = "RpcSrv", .stack_size = 1024 * 4, .icon = NULL},
#endif
#ifdef SRV_BT
{.app = bt_srv, .name = "BT", .stack_size = 1024, .icon = NULL},
{.app = bt_srv, .name = "BtSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_CLI
{.app = cli_srv, .name = "Cli", .stack_size = 4096, .icon = NULL},
{.app = cli_srv, .name = "CliSrv", .stack_size = 4096, .icon = NULL},
#endif
#ifdef SRV_DIALOGS
{.app = dialogs_srv, .name = "Dialogs", .stack_size = 1024, .icon = NULL},
{.app = dialogs_srv, .name = "DialogsSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_DOLPHIN
{.app = dolphin_srv, .name = "Dolphin", .stack_size = 1024, .icon = NULL},
{.app = dolphin_srv, .name = "DolphinSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_DESKTOP
{.app = desktop_srv, .name = "Desktop", .stack_size = 1024, .icon = NULL},
{.app = desktop_srv, .name = "DesktopSrv", .stack_size = 2048, .icon = NULL},
#endif
#ifdef SRV_GUI
{.app = gui_srv, .name = "Gui", .stack_size = 8192, .icon = NULL},
{.app = gui_srv, .name = "GuiSrv", .stack_size = 2048, .icon = NULL},
#endif
#ifdef SRV_INPUT
{.app = input_srv, .name = "Input", .stack_size = 1024, .icon = NULL},
{.app = input_srv, .name = "InputSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_LOADER
{.app = loader_srv, .name = "Loader", .stack_size = 1024, .icon = NULL},
{.app = loader_srv, .name = "LoaderSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_NOTIFICATION
{.app = notification_srv, .name = "Notification", .stack_size = 1024, .icon = NULL},
{.app = notification_srv, .name = "NotificationSrv", .stack_size = 1536, .icon = NULL},
#endif
#ifdef SRV_POWER
{.app = power_srv, .name = "Power", .stack_size = 1024, .icon = NULL},
{.app = power_srv, .name = "PowerSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_POWER_OBSERVER
{.app = power_observer_srv, .name = "PowerObserver", .stack_size = 1024, .icon = NULL},
{.app = power_observer_srv, .name = "PowerAuditSrv", .stack_size = 1024, .icon = NULL},
#endif
#ifdef SRV_STORAGE
{.app = storage_srv, .name = "Storage", .stack_size = 4096, .icon = NULL},
{.app = storage_srv, .name = "StorageSrv", .stack_size = 3072, .icon = NULL},
#endif
};

View file

@ -8,11 +8,12 @@
#define USB_UART_RX_BUF_SIZE (USB_CDC_PKT_LEN * 5)
typedef enum {
WorkerEvtStop = (1 << 0),
WorkerEvtRxDone = (1 << 1),
WorkerEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
WorkerEvtStop = (1 << 1),
WorkerEvtRxDone = (1 << 2),
WorkerEvtTxStop = (1 << 2),
WorkerEvtCdcRx = (1 << 3),
WorkerEvtTxStop = (1 << 3),
WorkerEvtCdcRx = (1 << 4),
} WorkerEvtFlags;
#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
@ -75,7 +76,7 @@ static int32_t usb_uart_worker(void* context) {
usb_uart->usb_mutex = osMutexNew(NULL);
usb_uart->tx_thread = furi_thread_alloc();
furi_thread_set_name(usb_uart->tx_thread, "usb_uart_tx");
furi_thread_set_name(usb_uart->tx_thread, "UsbUartTxWorker");
furi_thread_set_stack_size(usb_uart->tx_thread, 512);
furi_thread_set_context(usb_uart->tx_thread, NULL);
furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
@ -191,7 +192,7 @@ void usb_uart_enable(UsbUartConfig* cfg) {
usb_uart = furi_alloc(sizeof(UsbUartParams));
usb_uart->thread = furi_thread_alloc();
furi_thread_set_name(usb_uart->thread, "usb_uart");
furi_thread_set_name(usb_uart->thread, "UsbUartWorker");
furi_thread_set_stack_size(usb_uart->thread, 1024);
furi_thread_set_context(usb_uart->thread, cfg);
furi_thread_set_callback(usb_uart->thread, usb_uart_worker);

View file

@ -10,7 +10,7 @@
NfcWorker* nfc_worker_alloc() {
NfcWorker* nfc_worker = furi_alloc(sizeof(NfcWorker));
// Worker thread attributes
nfc_worker->thread_attr.name = "nfc_worker";
nfc_worker->thread_attr.name = "NfcWorker";
nfc_worker->thread_attr.stack_size = 8192;
nfc_worker->callback = NULL;
nfc_worker->context = NULL;

View file

@ -145,7 +145,7 @@ SubGhzFrequencyAnalyzerWorker* subghz_frequency_analyzer_worker_alloc() {
SubGhzFrequencyAnalyzerWorker* instance = furi_alloc(sizeof(SubGhzFrequencyAnalyzerWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "subghz_frequency_analyzer_worker");
furi_thread_set_name(instance->thread, "SubghzFAWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, subghz_frequency_analyzer_worker_thread);

View file

@ -101,6 +101,9 @@
#define configTIMER_TASK_PRIORITY ( 2 )
#define configTIMER_QUEUE_LENGTH 32
#define configTIMER_TASK_STACK_DEPTH 256
#define configTIMER_SERVICE_TASK_NAME "TimersSrv"
#define configIDLE_TASK_NAME "(-_-)"
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */

View file

@ -33,7 +33,7 @@ bool ble_app_init() {
ble_app->hci_mtx = osMutexNew(NULL);
ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
// HCI transport layer thread to handle user asynch events
ble_app->hci_thread_attr.name = "ble hci";
ble_app->hci_thread_attr.name = "BleHciWorker";
ble_app->hci_thread_attr.stack_size = 1024;
ble_app->hci_thread_id = osThreadNew(ble_app_hci_thread, NULL, &ble_app->hci_thread_attr);

View file

@ -50,7 +50,7 @@ void ble_glue_set_key_storage_changed_callback(BleGlueKeyStorageChangedCallback
void ble_glue_init() {
ble_glue = furi_alloc(sizeof(BleGlue));
ble_glue->status = BleGlueStatusStartup;
ble_glue->shci_user_event_thread_attr.name = "ble_shci_evt";
ble_glue->shci_user_event_thread_attr.name = "BleShciWorker";
ble_glue->shci_user_event_thread_attr.stack_size = 1024;
// Configure the system Power Mode

View file

@ -393,7 +393,7 @@ bool gap_init(BleEventCallback on_event_cb, void* context) {
gap->enable_adv = true;
// Thread configuration
gap->thread_attr.name = "BLE advertising";
gap->thread_attr.name = "BleGapWorker";
gap->thread_attr.stack_size = 1024;
gap->thread_id = osThreadNew(gap_app, NULL, &gap->thread_attr);

View file

@ -345,6 +345,7 @@ static const struct CdcConfigDescriptorDual cdc_cfg_desc_dual = {
};
static struct usb_cdc_line_coding cdc_config[IF_NUM_MAX] = {};
static uint8_t cdc_ctrl_line_state[IF_NUM_MAX];
static void cdc_init(usbd_device* dev, struct UsbInterface* intf);
static void cdc_deinit(usbd_device *dev);
@ -438,6 +439,12 @@ struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num) {
return NULL;
}
uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num) {
if (if_num < 2)
return cdc_ctrl_line_state[if_num];
return 0;
}
void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len) {
if (if_num == 0)
usbd_ep_write(usb_dev, CDC0_TXD_EP, buf, len);
@ -465,6 +472,7 @@ static void cdc_on_wakeup(usbd_device *dev) {
static void cdc_on_suspend(usbd_device *dev) {
for (uint8_t i = 0; i < IF_NUM_MAX; i++) {
cdc_ctrl_line_state[i] = 0;
if (callbacks[i] != NULL) {
if (callbacks[i]->state_callback != NULL)
callbacks[i]->state_callback(0);
@ -580,8 +588,9 @@ static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_cal
switch(req->bRequest) {
case USB_CDC_SET_CONTROL_LINE_STATE:
if (callbacks[if_num] != NULL) {
cdc_ctrl_line_state[if_num] = req->wValue;
if (callbacks[if_num]->ctrl_line_callback != NULL)
callbacks[if_num]->ctrl_line_callback(req->wValue);
callbacks[if_num]->ctrl_line_callback(cdc_ctrl_line_state[if_num]);
}
return usbd_ack;
case USB_CDC_SET_LINE_CODING:

View file

@ -17,6 +17,8 @@ void furi_hal_cdc_set_callbacks(uint8_t if_num, CdcCallbacks* cb);
struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num);
uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num);
void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len);
int32_t furi_hal_cdc_receive(uint8_t if_num, uint8_t* buf, uint16_t max_len);

View file

@ -4,29 +4,37 @@
#include <stream_buffer.h>
#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)
#define VCP_IF_NUM 0
typedef enum {
VcpConnect,
VcpDisconnect,
} VcpEvent;
VcpEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
VcpEvtConnect = (1 << 1),
VcpEvtDisconnect = (1 << 2),
VcpEvtEnable = (1 << 3),
VcpEvtDisable = (1 << 4),
VcpEvtRx = (1 << 5),
VcpEvtTx = (1 << 6),
VcpEvtRxDone = (1 << 7),
VcpEvtTxDone = (1 << 8),
} WorkerEvtFlags;
#define VCP_THREAD_FLAG_ALL (VcpEvtConnect | VcpEvtDisconnect | VcpEvtEnable | VcpEvtDisable | VcpEvtRx | VcpEvtTx | VcpEvtRxDone | VcpEvtTxDone)
typedef struct {
FuriThread* thread;
StreamBufferHandle_t tx_stream;
StreamBufferHandle_t rx_stream;
volatile bool connected;
uint8_t rx_buf[USB_CDC_PKT_LEN];
uint8_t rx_buf_start;
uint8_t rx_buf_len;
osMessageQueueId_t event_queue;
osMutexId_t usb_mutex;
osSemaphoreId_t tx_sem;
osSemaphoreId_t rx_sem;
uint8_t data_buffer[USB_CDC_PKT_LEN];
} FuriHalVcp;
static int32_t vcp_worker(void* context);
static void vcp_on_cdc_tx_complete();
static void vcp_on_cdc_rx();
static void vcp_state_callback(uint8_t state);
@ -49,34 +57,117 @@ void furi_hal_vcp_init() {
vcp = furi_alloc(sizeof(FuriHalVcp));
vcp->connected = false;
vcp->usb_mutex = osMutexNew(NULL);
vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
vcp->rx_stream = xStreamBufferCreate(VCP_RX_BUF_SIZE, 1);
vcp->tx_sem = osSemaphoreNew(1, 1, NULL);
vcp->rx_sem = osSemaphoreNew(1, 0, NULL);
vcp->event_queue = osMessageQueueNew(8, sizeof(VcpEvent), NULL);
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
vcp->thread = furi_thread_alloc();
furi_thread_set_name(vcp->thread, "VcpWorker");
furi_thread_set_stack_size(vcp->thread, 512);
furi_thread_set_callback(vcp->thread, vcp_worker);
furi_thread_start(vcp->thread);
FURI_LOG_I("FuriHalVcp", "Init OK");
}
void furi_hal_vcp_enable() {
static int32_t vcp_worker(void* context) {
bool enabled = true;
bool tx_idle = false;
bool rx_pending = false;
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
VcpEvent evt = VcpConnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
vcp->connected = true;
osSemaphoreRelease(vcp->tx_sem);
osSemaphoreRelease(vcp->rx_sem);
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 (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
}
// Rx buffer was read, maybe there is enough space for new data?
if((flags & VcpEvtRx) && rx_pending) {
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;
}
}
// New data in Tx buffer
if((flags & VcpEvtTx) && enabled) {
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);
}
}
}
// CDC write transfer done
if((flags & VcpEvtTxDone) && enabled) {
size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
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);
} else { // There is nothing to send. Set flag to start next transfer instantly
tx_idle = true;
}
}
// VCP session opened
if((flags & VcpEvtConnect) && enabled) {
if (vcp->connected == false) {
vcp->connected = true;
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
}
}
// VCP session closed
if((flags & VcpEvtDisconnect) && enabled) {
if (vcp->connected == true) {
vcp->connected = false;
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
}
}
// VCP enabled
if((flags & VcpEvtEnable) && !enabled){
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
if (furi_hal_cdc_get_ctrl_line_state(VCP_IF_NUM) & (1 << 0)) {
vcp->connected = true;
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
}
}
// VCP disabled
if((flags & VcpEvtDisable) && enabled) {
enabled = false;
vcp->connected = false;
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
}
}
return 0;
}
void furi_hal_vcp_enable() {
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtEnable);
}
void furi_hal_vcp_disable() {
furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL);
VcpEvent evt = VcpDisconnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
vcp->connected = false;
osSemaphoreRelease(vcp->tx_sem);
osSemaphoreRelease(vcp->rx_sem);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisable);
}
size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
@ -85,47 +176,17 @@ size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeo
size_t rx_cnt = 0;
VcpEvent evt = VcpDisconnect;
while (size > 0) {
size_t batch_size = size;
if (batch_size > VCP_RX_BUF_SIZE)
batch_size = VCP_RX_BUF_SIZE;
if (vcp->rx_buf_len > 0) {
size_t len = (vcp->rx_buf_len > size) ? (size) : (vcp->rx_buf_len);
memcpy(&buffer[rx_cnt], &vcp->rx_buf[vcp->rx_buf_start], len);
vcp->rx_buf_len -= len;
vcp->rx_buf_start += len;
rx_cnt += len;
}
while (rx_cnt < size) {
if (osMessageQueueGet(vcp->event_queue, &evt, NULL, 0) == osOK) {
if (evt == VcpConnect)
buffer[rx_cnt] = ascii_soh;
else {
buffer[rx_cnt] = ascii_eot;
vcp->rx_buf_len = 0;
}
rx_cnt++;
return rx_cnt;
}
if (osSemaphoreAcquire(vcp->rx_sem, timeout) == osErrorTimeout)
return rx_cnt;
furi_check(osMutexAcquire(vcp->usb_mutex, osWaitForever) == osOK);
size_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->rx_buf, USB_CDC_PKT_LEN);
furi_check(osMutexRelease(vcp->usb_mutex) == osOK);
vcp->rx_buf_len = len;
vcp->rx_buf_start = 0;
if (vcp->rx_buf_len > (size - rx_cnt)) {
len = size - rx_cnt;
memcpy(&buffer[rx_cnt], vcp->rx_buf, len);
vcp->rx_buf_len -= len;
vcp->rx_buf_start += len;
} else {
memcpy(&buffer[rx_cnt], vcp->rx_buf, vcp->rx_buf_len);
vcp->rx_buf_len = 0;
}
size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
if (len == 0)
break;
size -= len;
buffer += len;
rx_cnt += len;
}
return rx_cnt;
@ -133,26 +194,20 @@ size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeo
size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
furi_assert(vcp);
return furi_hal_vcp_rx_with_timeout(buffer, size, portMAX_DELAY);
return furi_hal_vcp_rx_with_timeout(buffer, size, osWaitForever);
}
void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
furi_assert(vcp);
furi_assert(buffer);
while (size > 0 && vcp->connected) {
furi_check(osSemaphoreAcquire(vcp->tx_sem, osWaitForever) == osOK);
if (!vcp->connected)
break;
while (size > 0) {
size_t batch_size = size;
if (batch_size > USB_CDC_PKT_LEN) {
batch_size = USB_CDC_PKT_LEN;
}
if (batch_size > VCP_TX_BUF_SIZE)
batch_size = VCP_TX_BUF_SIZE;
furi_check(osMutexAcquire(vcp->usb_mutex, osWaitForever) == osOK);
furi_hal_cdc_send(VCP_IF_NUM, (uint8_t*)buffer, batch_size);
furi_check(osMutexRelease(vcp->usb_mutex) == osOK);
xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
size -= batch_size;
buffer += batch_size;
@ -160,52 +215,33 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
}
static void vcp_state_callback(uint8_t state) {
if (state == 1) {
osSemaphoreRelease(vcp->rx_sem);
//osSemaphoreRelease(vcp->tx_sem);
}
else if (vcp->connected) {
vcp->connected = false;
osSemaphoreRelease(vcp->rx_sem);
VcpEvent evt = VcpDisconnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
//osSemaphoreRelease(vcp->tx_sem);
if (state == 0) {
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
}
}
static void vcp_on_cdc_control_line(uint8_t state) {
// bit 0: DTR state, bit 1: RTS state
bool dtr = state & 0b1;
bool dtr = state & (1 << 0);
if (dtr) {
if (!vcp->connected) {
vcp->connected = true;
VcpEvent evt = VcpConnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
}
if (dtr == true) {
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
} else {
if (vcp->connected) {
VcpEvent evt = VcpDisconnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
vcp->connected = false;
}
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
}
osSemaphoreRelease(vcp->tx_sem);
osSemaphoreRelease(vcp->rx_sem);
}
static void vcp_on_cdc_rx() {
if (vcp->connected == false)
return;
osSemaphoreRelease(vcp->rx_sem);
uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRxDone);
furi_assert((ret & osFlagsError) == 0);
}
static void vcp_on_cdc_tx_complete() {
osSemaphoreRelease(vcp->tx_sem);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTxDone);
}
bool furi_hal_vcp_is_connected(void) {
furi_assert(vcp);
return vcp->connected;
}

View file

@ -101,6 +101,9 @@
#define configTIMER_TASK_PRIORITY ( 2 )
#define configTIMER_QUEUE_LENGTH 32
#define configTIMER_TASK_STACK_DEPTH 256
#define configTIMER_SERVICE_TASK_NAME "TimersSrv"
#define configIDLE_TASK_NAME "(-_-)"
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */

View file

@ -33,7 +33,7 @@ bool ble_app_init() {
ble_app->hci_mtx = osMutexNew(NULL);
ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
// HCI transport layer thread to handle user asynch events
ble_app->hci_thread_attr.name = "ble hci";
ble_app->hci_thread_attr.name = "BleHciWorker";
ble_app->hci_thread_attr.stack_size = 1024;
ble_app->hci_thread_id = osThreadNew(ble_app_hci_thread, NULL, &ble_app->hci_thread_attr);

View file

@ -50,7 +50,7 @@ void ble_glue_set_key_storage_changed_callback(BleGlueKeyStorageChangedCallback
void ble_glue_init() {
ble_glue = furi_alloc(sizeof(BleGlue));
ble_glue->status = BleGlueStatusStartup;
ble_glue->shci_user_event_thread_attr.name = "ble_shci_evt";
ble_glue->shci_user_event_thread_attr.name = "BleShciWorker";
ble_glue->shci_user_event_thread_attr.stack_size = 1024;
// Configure the system Power Mode

View file

@ -393,7 +393,7 @@ bool gap_init(BleEventCallback on_event_cb, void* context) {
gap->enable_adv = true;
// Thread configuration
gap->thread_attr.name = "BLE advertising";
gap->thread_attr.name = "BleGapWorker";
gap->thread_attr.stack_size = 1024;
gap->thread_id = osThreadNew(gap_app, NULL, &gap->thread_attr);

View file

@ -345,6 +345,7 @@ static const struct CdcConfigDescriptorDual cdc_cfg_desc_dual = {
};
static struct usb_cdc_line_coding cdc_config[IF_NUM_MAX] = {};
static uint8_t cdc_ctrl_line_state[IF_NUM_MAX];
static void cdc_init(usbd_device* dev, struct UsbInterface* intf);
static void cdc_deinit(usbd_device *dev);
@ -438,6 +439,12 @@ struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num) {
return NULL;
}
uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num) {
if (if_num < 2)
return cdc_ctrl_line_state[if_num];
return 0;
}
void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len) {
if (if_num == 0)
usbd_ep_write(usb_dev, CDC0_TXD_EP, buf, len);
@ -465,6 +472,7 @@ static void cdc_on_wakeup(usbd_device *dev) {
static void cdc_on_suspend(usbd_device *dev) {
for (uint8_t i = 0; i < IF_NUM_MAX; i++) {
cdc_ctrl_line_state[i] = 0;
if (callbacks[i] != NULL) {
if (callbacks[i]->state_callback != NULL)
callbacks[i]->state_callback(0);
@ -580,8 +588,9 @@ static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_cal
switch(req->bRequest) {
case USB_CDC_SET_CONTROL_LINE_STATE:
if (callbacks[if_num] != NULL) {
cdc_ctrl_line_state[if_num] = req->wValue;
if (callbacks[if_num]->ctrl_line_callback != NULL)
callbacks[if_num]->ctrl_line_callback(req->wValue);
callbacks[if_num]->ctrl_line_callback(cdc_ctrl_line_state[if_num]);
}
return usbd_ack;
case USB_CDC_SET_LINE_CODING:

View file

@ -17,6 +17,8 @@ void furi_hal_cdc_set_callbacks(uint8_t if_num, CdcCallbacks* cb);
struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num);
uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num);
void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len);
int32_t furi_hal_cdc_receive(uint8_t if_num, uint8_t* buf, uint16_t max_len);

View file

@ -4,29 +4,37 @@
#include <stream_buffer.h>
#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)
#define VCP_IF_NUM 0
typedef enum {
VcpConnect,
VcpDisconnect,
} VcpEvent;
VcpEvtReserved = (1 << 0), // Reserved for StreamBuffer internal event
VcpEvtConnect = (1 << 1),
VcpEvtDisconnect = (1 << 2),
VcpEvtEnable = (1 << 3),
VcpEvtDisable = (1 << 4),
VcpEvtRx = (1 << 5),
VcpEvtTx = (1 << 6),
VcpEvtRxDone = (1 << 7),
VcpEvtTxDone = (1 << 8),
} WorkerEvtFlags;
#define VCP_THREAD_FLAG_ALL (VcpEvtConnect | VcpEvtDisconnect | VcpEvtEnable | VcpEvtDisable | VcpEvtRx | VcpEvtTx | VcpEvtRxDone | VcpEvtTxDone)
typedef struct {
FuriThread* thread;
StreamBufferHandle_t tx_stream;
StreamBufferHandle_t rx_stream;
volatile bool connected;
uint8_t rx_buf[USB_CDC_PKT_LEN];
uint8_t rx_buf_start;
uint8_t rx_buf_len;
osMessageQueueId_t event_queue;
osMutexId_t usb_mutex;
osSemaphoreId_t tx_sem;
osSemaphoreId_t rx_sem;
uint8_t data_buffer[USB_CDC_PKT_LEN];
} FuriHalVcp;
static int32_t vcp_worker(void* context);
static void vcp_on_cdc_tx_complete();
static void vcp_on_cdc_rx();
static void vcp_state_callback(uint8_t state);
@ -49,34 +57,117 @@ void furi_hal_vcp_init() {
vcp = furi_alloc(sizeof(FuriHalVcp));
vcp->connected = false;
vcp->usb_mutex = osMutexNew(NULL);
vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
vcp->rx_stream = xStreamBufferCreate(VCP_RX_BUF_SIZE, 1);
vcp->tx_sem = osSemaphoreNew(1, 1, NULL);
vcp->rx_sem = osSemaphoreNew(1, 0, NULL);
vcp->event_queue = osMessageQueueNew(8, sizeof(VcpEvent), NULL);
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
vcp->thread = furi_thread_alloc();
furi_thread_set_name(vcp->thread, "VcpWorker");
furi_thread_set_stack_size(vcp->thread, 512);
furi_thread_set_callback(vcp->thread, vcp_worker);
furi_thread_start(vcp->thread);
FURI_LOG_I("FuriHalVcp", "Init OK");
}
void furi_hal_vcp_enable() {
static int32_t vcp_worker(void* context) {
bool enabled = true;
bool tx_idle = false;
bool rx_pending = false;
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
VcpEvent evt = VcpConnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
vcp->connected = true;
osSemaphoreRelease(vcp->tx_sem);
osSemaphoreRelease(vcp->rx_sem);
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 (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
}
// Rx buffer was read, maybe there is enough space for new data?
if((flags & VcpEvtRx) && rx_pending) {
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;
}
}
// New data in Tx buffer
if((flags & VcpEvtTx) && enabled) {
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);
}
}
}
// CDC write transfer done
if((flags & VcpEvtTxDone) && enabled) {
size_t len = xStreamBufferReceive(vcp->tx_stream, vcp->data_buffer, USB_CDC_PKT_LEN, 0);
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);
} else { // There is nothing to send. Set flag to start next transfer instantly
tx_idle = true;
}
}
// VCP session opened
if((flags & VcpEvtConnect) && enabled) {
if (vcp->connected == false) {
vcp->connected = true;
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
}
}
// VCP session closed
if((flags & VcpEvtDisconnect) && enabled) {
if (vcp->connected == true) {
vcp->connected = false;
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
}
}
// VCP enabled
if((flags & VcpEvtEnable) && !enabled){
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
if (furi_hal_cdc_get_ctrl_line_state(VCP_IF_NUM) & (1 << 0)) {
vcp->connected = true;
xStreamBufferSend(vcp->rx_stream, &ascii_soh, 1, osWaitForever);
}
}
// VCP disabled
if((flags & VcpEvtDisable) && enabled) {
enabled = false;
vcp->connected = false;
xStreamBufferSend(vcp->rx_stream, &ascii_eot, 1, osWaitForever);
}
}
return 0;
}
void furi_hal_vcp_enable() {
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtEnable);
}
void furi_hal_vcp_disable() {
furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL);
VcpEvent evt = VcpDisconnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
vcp->connected = false;
osSemaphoreRelease(vcp->tx_sem);
osSemaphoreRelease(vcp->rx_sem);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisable);
}
size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
@ -85,47 +176,17 @@ size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeo
size_t rx_cnt = 0;
VcpEvent evt = VcpDisconnect;
while (size > 0) {
size_t batch_size = size;
if (batch_size > VCP_RX_BUF_SIZE)
batch_size = VCP_RX_BUF_SIZE;
if (vcp->rx_buf_len > 0) {
size_t len = (vcp->rx_buf_len > size) ? (size) : (vcp->rx_buf_len);
memcpy(&buffer[rx_cnt], &vcp->rx_buf[vcp->rx_buf_start], len);
vcp->rx_buf_len -= len;
vcp->rx_buf_start += len;
rx_cnt += len;
}
while (rx_cnt < size) {
if (osMessageQueueGet(vcp->event_queue, &evt, NULL, 0) == osOK) {
if (evt == VcpConnect)
buffer[rx_cnt] = ascii_soh;
else {
buffer[rx_cnt] = ascii_eot;
vcp->rx_buf_len = 0;
}
rx_cnt++;
return rx_cnt;
}
if (osSemaphoreAcquire(vcp->rx_sem, timeout) == osErrorTimeout)
return rx_cnt;
furi_check(osMutexAcquire(vcp->usb_mutex, osWaitForever) == osOK);
size_t len = furi_hal_cdc_receive(VCP_IF_NUM, vcp->rx_buf, USB_CDC_PKT_LEN);
furi_check(osMutexRelease(vcp->usb_mutex) == osOK);
vcp->rx_buf_len = len;
vcp->rx_buf_start = 0;
if (vcp->rx_buf_len > (size - rx_cnt)) {
len = size - rx_cnt;
memcpy(&buffer[rx_cnt], vcp->rx_buf, len);
vcp->rx_buf_len -= len;
vcp->rx_buf_start += len;
} else {
memcpy(&buffer[rx_cnt], vcp->rx_buf, vcp->rx_buf_len);
vcp->rx_buf_len = 0;
}
size_t len = xStreamBufferReceive(vcp->rx_stream, buffer, batch_size, timeout);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRx);
if (len == 0)
break;
size -= len;
buffer += len;
rx_cnt += len;
}
return rx_cnt;
@ -133,26 +194,20 @@ size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeo
size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
furi_assert(vcp);
return furi_hal_vcp_rx_with_timeout(buffer, size, portMAX_DELAY);
return furi_hal_vcp_rx_with_timeout(buffer, size, osWaitForever);
}
void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
furi_assert(vcp);
furi_assert(buffer);
while (size > 0 && vcp->connected) {
furi_check(osSemaphoreAcquire(vcp->tx_sem, osWaitForever) == osOK);
if (!vcp->connected)
break;
while (size > 0) {
size_t batch_size = size;
if (batch_size > USB_CDC_PKT_LEN) {
batch_size = USB_CDC_PKT_LEN;
}
if (batch_size > VCP_TX_BUF_SIZE)
batch_size = VCP_TX_BUF_SIZE;
furi_check(osMutexAcquire(vcp->usb_mutex, osWaitForever) == osOK);
furi_hal_cdc_send(VCP_IF_NUM, (uint8_t*)buffer, batch_size);
furi_check(osMutexRelease(vcp->usb_mutex) == osOK);
xStreamBufferSend(vcp->tx_stream, buffer, batch_size, osWaitForever);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTx);
size -= batch_size;
buffer += batch_size;
@ -160,52 +215,33 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
}
static void vcp_state_callback(uint8_t state) {
if (state == 1) {
osSemaphoreRelease(vcp->rx_sem);
//osSemaphoreRelease(vcp->tx_sem);
}
else if (vcp->connected) {
vcp->connected = false;
osSemaphoreRelease(vcp->rx_sem);
VcpEvent evt = VcpDisconnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
//osSemaphoreRelease(vcp->tx_sem);
if (state == 0) {
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
}
}
static void vcp_on_cdc_control_line(uint8_t state) {
// bit 0: DTR state, bit 1: RTS state
bool dtr = state & 0b1;
bool dtr = state & (1 << 0);
if (dtr) {
if (!vcp->connected) {
vcp->connected = true;
VcpEvent evt = VcpConnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
}
if (dtr == true) {
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtConnect);
} else {
if (vcp->connected) {
VcpEvent evt = VcpDisconnect;
osMessageQueuePut(vcp->event_queue, &evt, 0, 0);
vcp->connected = false;
}
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtDisconnect);
}
osSemaphoreRelease(vcp->tx_sem);
osSemaphoreRelease(vcp->rx_sem);
}
static void vcp_on_cdc_rx() {
if (vcp->connected == false)
return;
osSemaphoreRelease(vcp->rx_sem);
uint32_t ret = osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtRxDone);
furi_assert((ret & osFlagsError) == 0);
}
static void vcp_on_cdc_tx_complete() {
osSemaphoreRelease(vcp->tx_sem);
osThreadFlagsSet(furi_thread_get_thread_id(vcp->thread), VcpEvtTxDone);
}
bool furi_hal_vcp_is_connected(void) {
furi_assert(vcp);
return vcp->connected;
}

View file

@ -38,7 +38,7 @@ void platformDisableIrqCallback() {
void platformSetIrqCallback(PlatformIrqCallback callback) {
platform_irq_callback = callback;
platform_irq_thread_attr.name = "rfal_irq_worker";
platform_irq_thread_attr.name = "RfalIrqWorker";
platform_irq_thread_attr.stack_size = 1024;
platform_irq_thread_attr.priority = osPriorityISR;
platform_irq_thread_id = osThreadNew(platformIrqWorker, NULL, &platform_irq_thread_attr);

View file

@ -216,7 +216,7 @@ IrdaWorker* irda_worker_alloc() {
IrdaWorker* instance = furi_alloc(sizeof(IrdaWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "irda_worker");
furi_thread_set_name(instance->thread, "IrdaWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);

View file

@ -163,7 +163,7 @@ SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
SubGhzFileEncoderWorker* instance = furi_alloc(sizeof(SubGhzFileEncoderWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "subghz_file_encoder_worker");
furi_thread_set_name(instance->thread, "SubghzFEWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);

View file

@ -90,7 +90,7 @@ SubGhzWorker* subghz_worker_alloc() {
SubGhzWorker* instance = furi_alloc(sizeof(SubGhzWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "subghz_worker");
furi_thread_set_name(instance->thread, "SubghzWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, subghz_worker_thread_callback);