2021-10-26 18:41:56 +00:00
|
|
|
#include "usb_uart_bridge.h"
|
2022-01-05 16:10:18 +00:00
|
|
|
#include "furi_hal.h"
|
2021-10-26 18:41:56 +00:00
|
|
|
#include <stream_buffer.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_usb_cdc_i.h>
|
2021-10-26 18:41:56 +00:00
|
|
|
#include "usb_cdc.h"
|
2022-04-29 12:02:17 +00:00
|
|
|
#include "cli/cli_vcp.h"
|
|
|
|
#include "cli/cli.h"
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-04 19:33:28 +00:00
|
|
|
#define USB_CDC_PKT_LEN CDC_DATA_SZ
|
|
|
|
#define USB_UART_RX_BUF_SIZE (USB_CDC_PKT_LEN * 5)
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
#define USB_CDC_BIT_DTR (1 << 0)
|
|
|
|
#define USB_CDC_BIT_RTS (1 << 1)
|
|
|
|
|
|
|
|
static const GpioPin* flow_pins[][2] = {
|
|
|
|
{&gpio_ext_pa7, &gpio_ext_pa6}, // 2, 3
|
|
|
|
{&gpio_ext_pb2, &gpio_ext_pc3}, // 6, 7
|
|
|
|
};
|
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
typedef enum {
|
2022-04-29 12:02:17 +00:00
|
|
|
WorkerEvtStop = (1 << 0),
|
|
|
|
WorkerEvtRxDone = (1 << 1),
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
WorkerEvtTxStop = (1 << 2),
|
|
|
|
WorkerEvtCdcRx = (1 << 3),
|
2021-11-21 15:17:43 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
WorkerEvtCfgChange = (1 << 4),
|
2021-11-21 15:17:43 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
WorkerEvtLineCfgSet = (1 << 5),
|
|
|
|
WorkerEvtCtrlLineSet = (1 << 6),
|
2021-11-21 15:17:43 +00:00
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
} WorkerEvtFlags;
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
#define WORKER_ALL_RX_EVENTS \
|
|
|
|
(WorkerEvtStop | WorkerEvtRxDone | WorkerEvtCfgChange | WorkerEvtLineCfgSet | \
|
|
|
|
WorkerEvtCtrlLineSet)
|
2021-11-04 19:33:28 +00:00
|
|
|
#define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtCdcRx)
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
struct UsbUartBridge {
|
2021-10-26 18:41:56 +00:00
|
|
|
UsbUartConfig cfg;
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartConfig cfg_new;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
FuriThread* thread;
|
|
|
|
FuriThread* tx_thread;
|
|
|
|
|
|
|
|
StreamBufferHandle_t rx_stream;
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMutex* usb_mutex;
|
2021-11-04 19:33:28 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriSemaphore* tx_sem;
|
2021-11-04 19:33:28 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartState st;
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
uint8_t rx_buf[USB_CDC_PKT_LEN];
|
|
|
|
};
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00: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);
|
|
|
|
static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config);
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
static const CdcCallbacks cdc_cb = {
|
2021-10-26 18:41:56 +00:00
|
|
|
vcp_on_cdc_tx_complete,
|
|
|
|
vcp_on_cdc_rx,
|
|
|
|
vcp_state_callback,
|
|
|
|
vcp_on_cdc_control_line,
|
|
|
|
vcp_on_line_config,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* USB UART worker */
|
|
|
|
|
|
|
|
static int32_t usb_uart_tx_thread(void* context);
|
|
|
|
|
2021-11-19 22:19:31 +00:00
|
|
|
static void usb_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
2021-10-26 18:41:56 +00:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
|
|
|
|
if(ev == UartIrqEventRXNE) {
|
|
|
|
xStreamBufferSendFromISR(usb_uart->rx_stream, &data, 1, &xHigherPriorityTaskWoken);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtRxDone);
|
2021-11-21 15:17:43 +00:00
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_uart_vcp_init(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
|
2022-03-24 15:45:03 +00:00
|
|
|
furi_hal_usb_unlock();
|
2021-11-21 15:17:43 +00:00
|
|
|
if(vcp_ch == 0) {
|
2022-04-29 12:02:17 +00:00
|
|
|
Cli* cli = furi_record_open("cli");
|
|
|
|
cli_session_close(cli);
|
|
|
|
furi_record_close("cli");
|
2022-03-24 15:45:03 +00:00
|
|
|
furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
|
2021-11-21 15:17:43 +00:00
|
|
|
} else {
|
2022-03-24 15:45:03 +00:00
|
|
|
furi_check(furi_hal_usb_set_config(&usb_cdc_dual, NULL) == true);
|
2022-04-29 12:02:17 +00:00
|
|
|
Cli* cli = furi_record_open("cli");
|
|
|
|
cli_session_open(cli, &cli_vcp);
|
|
|
|
furi_record_close("cli");
|
2021-11-21 15:17:43 +00:00
|
|
|
}
|
|
|
|
furi_hal_cdc_set_callbacks(vcp_ch, (CdcCallbacks*)&cdc_cb, usb_uart);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_uart_vcp_deinit(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(usb_uart);
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_hal_cdc_set_callbacks(vcp_ch, NULL, NULL);
|
2022-04-29 12:02:17 +00:00
|
|
|
if(vcp_ch != 0) {
|
|
|
|
Cli* cli = furi_record_open("cli");
|
|
|
|
cli_session_close(cli);
|
|
|
|
furi_record_close("cli");
|
|
|
|
}
|
2021-11-21 15:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_uart_serial_init(UsbUartBridge* usb_uart, uint8_t uart_ch) {
|
|
|
|
if(uart_ch == FuriHalUartIdUSART1) {
|
|
|
|
furi_hal_console_disable();
|
|
|
|
} else if(uart_ch == FuriHalUartIdLPUART1) {
|
|
|
|
furi_hal_uart_init(uart_ch, 115200);
|
|
|
|
}
|
|
|
|
furi_hal_uart_set_irq_cb(uart_ch, usb_uart_on_irq_cb, usb_uart);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_uart_serial_deinit(UsbUartBridge* usb_uart, uint8_t uart_ch) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(usb_uart);
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_hal_uart_set_irq_cb(uart_ch, NULL, NULL);
|
|
|
|
if(uart_ch == FuriHalUartIdUSART1)
|
|
|
|
furi_hal_console_enable();
|
|
|
|
else if(uart_ch == FuriHalUartIdLPUART1)
|
|
|
|
furi_hal_uart_deinit(uart_ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_uart_set_baudrate(UsbUartBridge* usb_uart, uint32_t baudrate) {
|
|
|
|
if(baudrate != 0) {
|
|
|
|
furi_hal_uart_set_br(usb_uart->cfg.uart_ch, baudrate);
|
|
|
|
usb_uart->st.baudrate_cur = baudrate;
|
|
|
|
} else {
|
|
|
|
struct usb_cdc_line_coding* line_cfg =
|
|
|
|
furi_hal_cdc_get_port_settings(usb_uart->cfg.vcp_ch);
|
|
|
|
if(line_cfg->dwDTERate > 0) {
|
|
|
|
furi_hal_uart_set_br(usb_uart->cfg.uart_ch, line_cfg->dwDTERate);
|
|
|
|
usb_uart->st.baudrate_cur = line_cfg->dwDTERate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_uart_update_ctrl_lines(UsbUartBridge* usb_uart) {
|
|
|
|
if(usb_uart->cfg.flow_pins != 0) {
|
2022-05-06 13:37:10 +00:00
|
|
|
furi_assert((size_t)(usb_uart->cfg.flow_pins - 1) < COUNT_OF(flow_pins));
|
2021-11-21 15:17:43 +00:00
|
|
|
uint8_t state = furi_hal_cdc_get_ctrl_line_state(usb_uart->cfg.vcp_ch);
|
|
|
|
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][0], !(state & USB_CDC_BIT_RTS));
|
|
|
|
furi_hal_gpio_write(flow_pins[usb_uart->cfg.flow_pins - 1][1], !(state & USB_CDC_BIT_DTR));
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t usb_uart_worker(void* context) {
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
|
|
|
|
|
|
|
memcpy(&usb_uart->cfg, &usb_uart->cfg_new, sizeof(UsbUartConfig));
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
usb_uart->rx_stream = xStreamBufferCreate(USB_UART_RX_BUF_SIZE, 1);
|
2021-11-04 19:33:28 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
usb_uart->tx_sem = furi_semaphore_alloc(1, 1);
|
|
|
|
usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
usb_uart->tx_thread = furi_thread_alloc();
|
2021-11-11 16:17:50 +00:00
|
|
|
furi_thread_set_name(usb_uart->tx_thread, "UsbUartTxWorker");
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_thread_set_stack_size(usb_uart->tx_thread, 512);
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_thread_set_context(usb_uart->tx_thread, usb_uart);
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
|
|
|
|
usb_uart_serial_init(usb_uart, usb_uart->cfg.uart_ch);
|
|
|
|
usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
|
|
|
|
if(usb_uart->cfg.flow_pins != 0) {
|
2022-05-06 13:37:10 +00:00
|
|
|
furi_assert((size_t)(usb_uart->cfg.flow_pins - 1) < COUNT_OF(flow_pins));
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_simple(
|
|
|
|
flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeOutputPushPull);
|
|
|
|
furi_hal_gpio_init_simple(
|
|
|
|
flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeOutputPushPull);
|
2021-11-21 15:17:43 +00:00
|
|
|
usb_uart_update_ctrl_lines(usb_uart);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtCdcRx);
|
2021-10-26 18:41:56 +00:00
|
|
|
|
|
|
|
furi_thread_start(usb_uart->tx_thread);
|
|
|
|
|
|
|
|
while(1) {
|
2022-06-20 14:54:48 +00:00
|
|
|
uint32_t events =
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
|
|
|
|
furi_check((events & FuriFlagError) == 0);
|
2021-10-26 18:41:56 +00:00
|
|
|
if(events & WorkerEvtStop) break;
|
2021-11-04 19:33:28 +00:00
|
|
|
if(events & WorkerEvtRxDone) {
|
|
|
|
size_t len =
|
|
|
|
xStreamBufferReceive(usb_uart->rx_stream, usb_uart->rx_buf, USB_CDC_PKT_LEN, 0);
|
|
|
|
if(len > 0) {
|
2022-07-20 10:56:33 +00:00
|
|
|
if(furi_semaphore_acquire(usb_uart->tx_sem, 100) == FuriStatusOk) {
|
2021-11-21 15:17:43 +00:00
|
|
|
usb_uart->st.rx_cnt += len;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_mutex_acquire(usb_uart->usb_mutex, FuriWaitForever) == FuriStatusOk);
|
2021-11-04 19:33:28 +00:00
|
|
|
furi_hal_cdc_send(usb_uart->cfg.vcp_ch, usb_uart->rx_buf, len);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(usb_uart->usb_mutex) == FuriStatusOk);
|
2021-11-04 19:33:28 +00:00
|
|
|
} else {
|
|
|
|
xStreamBufferReset(usb_uart->rx_stream);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
2021-11-04 19:33:28 +00:00
|
|
|
}
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
2021-11-21 15:17:43 +00:00
|
|
|
if(events & WorkerEvtCfgChange) {
|
|
|
|
if(usb_uart->cfg.vcp_ch != usb_uart->cfg_new.vcp_ch) {
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_thread_join(usb_uart->tx_thread);
|
|
|
|
|
|
|
|
usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
|
|
|
|
usb_uart_vcp_init(usb_uart, usb_uart->cfg_new.vcp_ch);
|
|
|
|
|
|
|
|
usb_uart->cfg.vcp_ch = usb_uart->cfg_new.vcp_ch;
|
|
|
|
furi_thread_start(usb_uart->tx_thread);
|
|
|
|
events |= WorkerEvtCtrlLineSet;
|
|
|
|
events |= WorkerEvtLineCfgSet;
|
|
|
|
}
|
|
|
|
if(usb_uart->cfg.uart_ch != usb_uart->cfg_new.uart_ch) {
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_thread_join(usb_uart->tx_thread);
|
|
|
|
|
|
|
|
usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
|
|
|
|
usb_uart_serial_init(usb_uart, usb_uart->cfg_new.uart_ch);
|
|
|
|
|
|
|
|
usb_uart->cfg.uart_ch = usb_uart->cfg_new.uart_ch;
|
|
|
|
usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
|
|
|
|
|
|
|
|
furi_thread_start(usb_uart->tx_thread);
|
|
|
|
}
|
|
|
|
if(usb_uart->cfg.baudrate != usb_uart->cfg_new.baudrate) {
|
|
|
|
usb_uart_set_baudrate(usb_uart, usb_uart->cfg_new.baudrate);
|
|
|
|
usb_uart->cfg.baudrate = usb_uart->cfg_new.baudrate;
|
|
|
|
}
|
|
|
|
if(usb_uart->cfg.flow_pins != usb_uart->cfg_new.flow_pins) {
|
|
|
|
if(usb_uart->cfg.flow_pins != 0) {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_simple(
|
2021-11-21 15:17:43 +00:00
|
|
|
flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_simple(
|
2021-11-21 15:17:43 +00:00
|
|
|
flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
|
|
|
|
}
|
|
|
|
if(usb_uart->cfg_new.flow_pins != 0) {
|
2022-05-06 13:37:10 +00:00
|
|
|
furi_assert((size_t)(usb_uart->cfg_new.flow_pins - 1) < COUNT_OF(flow_pins));
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_simple(
|
2021-11-21 15:17:43 +00:00
|
|
|
flow_pins[usb_uart->cfg_new.flow_pins - 1][0], GpioModeOutputPushPull);
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_simple(
|
2021-11-21 15:17:43 +00:00
|
|
|
flow_pins[usb_uart->cfg_new.flow_pins - 1][1], GpioModeOutputPushPull);
|
|
|
|
}
|
|
|
|
usb_uart->cfg.flow_pins = usb_uart->cfg_new.flow_pins;
|
|
|
|
events |= WorkerEvtCtrlLineSet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(events & WorkerEvtLineCfgSet) {
|
|
|
|
if(usb_uart->cfg.baudrate == 0)
|
|
|
|
usb_uart_set_baudrate(usb_uart, usb_uart->cfg.baudrate);
|
|
|
|
}
|
|
|
|
if(events & WorkerEvtCtrlLineSet) {
|
|
|
|
usb_uart_update_ctrl_lines(usb_uart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usb_uart_vcp_deinit(usb_uart, usb_uart->cfg.vcp_ch);
|
|
|
|
usb_uart_serial_deinit(usb_uart, usb_uart->cfg.uart_ch);
|
2022-04-29 12:02:17 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
if(usb_uart->cfg.flow_pins != 0) {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][0], GpioModeAnalog);
|
|
|
|
furi_hal_gpio_init_simple(flow_pins[usb_uart->cfg.flow_pins - 1][1], GpioModeAnalog);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtTxStop);
|
2021-10-26 18:41:56 +00:00
|
|
|
furi_thread_join(usb_uart->tx_thread);
|
|
|
|
furi_thread_free(usb_uart->tx_thread);
|
|
|
|
|
|
|
|
vStreamBufferDelete(usb_uart->rx_stream);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_free(usb_uart->usb_mutex);
|
|
|
|
furi_semaphore_free(usb_uart->tx_sem);
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2022-04-29 12:02:17 +00:00
|
|
|
furi_hal_usb_unlock();
|
|
|
|
furi_check(furi_hal_usb_set_config(&usb_cdc_single, NULL) == true);
|
|
|
|
Cli* cli = furi_record_open("cli");
|
|
|
|
cli_session_open(cli, &cli_vcp);
|
|
|
|
furi_record_close("cli");
|
|
|
|
|
2021-10-26 18:41:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t usb_uart_tx_thread(void* context) {
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
|
|
|
|
2021-11-04 19:33:28 +00:00
|
|
|
uint8_t data[USB_CDC_PKT_LEN];
|
2021-10-26 18:41:56 +00:00
|
|
|
while(1) {
|
2022-06-20 14:54:48 +00:00
|
|
|
uint32_t events =
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_thread_flags_wait(WORKER_ALL_TX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
|
|
|
|
furi_check((events & FuriFlagError) == 0);
|
2021-10-26 18:41:56 +00:00
|
|
|
if(events & WorkerEvtTxStop) break;
|
2021-11-04 19:33:28 +00:00
|
|
|
if(events & WorkerEvtCdcRx) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(usb_uart->usb_mutex, FuriWaitForever) == FuriStatusOk);
|
2021-11-21 15:17:43 +00:00
|
|
|
size_t len = furi_hal_cdc_receive(usb_uart->cfg.vcp_ch, data, USB_CDC_PKT_LEN);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(usb_uart->usb_mutex) == FuriStatusOk);
|
2021-11-04 19:33:28 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
if(len > 0) {
|
|
|
|
usb_uart->st.tx_cnt += len;
|
|
|
|
furi_hal_uart_tx(usb_uart->cfg.uart_ch, data, len);
|
2021-11-04 19:33:28 +00:00
|
|
|
}
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VCP callbacks */
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
static void vcp_on_cdc_tx_complete(void* context) {
|
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_semaphore_release(usb_uart->tx_sem);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
static void vcp_on_cdc_rx(void* context) {
|
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->tx_thread), WorkerEvtCdcRx);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
static void vcp_state_callback(void* context, uint8_t state) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(context);
|
|
|
|
UNUSED(state);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
static void vcp_on_cdc_control_line(void* context, uint8_t state) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(state);
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCtrlLineSet);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
static void vcp_on_line_config(void* context, struct usb_cdc_line_coding* config) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(config);
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtLineCfgSet);
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
UsbUartBridge* usb_uart_enable(UsbUartConfig* cfg) {
|
2022-02-18 19:53:46 +00:00
|
|
|
UsbUartBridge* usb_uart = malloc(sizeof(UsbUartBridge));
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
|
2021-10-26 18:41:56 +00:00
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
usb_uart->thread = furi_thread_alloc();
|
|
|
|
furi_thread_set_name(usb_uart->thread, "UsbUartWorker");
|
|
|
|
furi_thread_set_stack_size(usb_uart->thread, 1024);
|
|
|
|
furi_thread_set_context(usb_uart->thread, usb_uart);
|
|
|
|
furi_thread_set_callback(usb_uart->thread, usb_uart_worker);
|
|
|
|
|
|
|
|
furi_thread_start(usb_uart->thread);
|
|
|
|
return usb_uart;
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:17:43 +00:00
|
|
|
void usb_uart_disable(UsbUartBridge* usb_uart) {
|
2021-11-26 13:53:51 +00:00
|
|
|
furi_assert(usb_uart);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtStop);
|
2021-11-21 15:17:43 +00:00
|
|
|
furi_thread_join(usb_uart->thread);
|
|
|
|
furi_thread_free(usb_uart->thread);
|
|
|
|
free(usb_uart);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_uart_set_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
|
|
|
|
furi_assert(usb_uart);
|
|
|
|
furi_assert(cfg);
|
|
|
|
memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCfgChange);
|
2021-11-21 15:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_uart_get_config(UsbUartBridge* usb_uart, UsbUartConfig* cfg) {
|
|
|
|
furi_assert(usb_uart);
|
|
|
|
furi_assert(cfg);
|
|
|
|
memcpy(cfg, &(usb_uart->cfg_new), sizeof(UsbUartConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_uart_get_state(UsbUartBridge* usb_uart, UsbUartState* st) {
|
|
|
|
furi_assert(usb_uart);
|
|
|
|
furi_assert(st);
|
|
|
|
memcpy(st, &(usb_uart->st), sizeof(UsbUartState));
|
2021-10-26 18:41:56 +00:00
|
|
|
}
|