[FL-1885] USB-UART bridge (#778)
* [FL-1885] USB-UART: app and worker * [FL-1885] USB-UART: UART on CDC0 Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <furi-hal-console.h>
|
||||
#include <furi-hal-lpuart.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stm32wbxx_ll_gpio.h>
|
||||
@@ -7,8 +8,12 @@
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#define CONSOLE_BAUDRATE 230400
|
||||
|
||||
volatile bool furi_hal_console_alive = false;
|
||||
|
||||
static void (*irq_cb)(uint8_t ev, uint8_t data);
|
||||
|
||||
void furi_hal_console_init() {
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
|
||||
@@ -21,11 +26,11 @@ void furi_hal_console_init() {
|
||||
|
||||
LL_USART_InitTypeDef USART_InitStruct = {0};
|
||||
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
|
||||
USART_InitStruct.BaudRate = 230400;
|
||||
USART_InitStruct.BaudRate = CONSOLE_BAUDRATE;
|
||||
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
||||
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
||||
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
||||
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;
|
||||
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
||||
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
||||
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
||||
LL_USART_Init(USART1, &USART_InitStruct);
|
||||
@@ -36,12 +41,41 @@ void furi_hal_console_init() {
|
||||
LL_USART_Enable(USART1);
|
||||
|
||||
while(!LL_USART_IsActiveFlag_TEACK(USART1)) ;
|
||||
|
||||
LL_USART_EnableIT_RXNE_RXFNE(USART1);
|
||||
LL_USART_EnableIT_IDLE(USART1);
|
||||
HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
|
||||
|
||||
furi_hal_console_alive = true;
|
||||
|
||||
FURI_LOG_I("FuriHalConsole", "Init OK");
|
||||
}
|
||||
|
||||
static void furi_hal_console_uart_tx(const uint8_t* buffer, size_t buffer_size) {
|
||||
void furi_hal_usart_init() {
|
||||
furi_hal_console_alive = false;
|
||||
}
|
||||
|
||||
void furi_hal_usart_set_br(uint32_t baud) {
|
||||
if (LL_USART_IsEnabled(USART1)) {
|
||||
// Wait for transfer complete flag
|
||||
while (!LL_USART_IsActiveFlag_TC(USART1));
|
||||
LL_USART_Disable(USART1);
|
||||
uint32_t uartclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
|
||||
LL_USART_SetBaudRate(USART1, uartclk, LL_USART_PRESCALER_DIV1, LL_USART_OVERSAMPLING_16, baud);
|
||||
LL_USART_Enable(USART1);
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_usart_deinit() {
|
||||
while (!LL_USART_IsActiveFlag_TC(USART1));
|
||||
furi_hal_usart_set_br(CONSOLE_BAUDRATE);
|
||||
furi_hal_console_alive = true;
|
||||
}
|
||||
|
||||
void furi_hal_usart_tx(const uint8_t* buffer, size_t buffer_size) {
|
||||
if (LL_USART_IsEnabled(USART1) == 0)
|
||||
return;
|
||||
|
||||
while(buffer_size > 0) {
|
||||
while (!LL_USART_IsActiveFlag_TXE(USART1));
|
||||
|
||||
@@ -52,12 +86,32 @@ static void furi_hal_console_uart_tx(const uint8_t* buffer, size_t buffer_size)
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_usart_set_irq_cb(void (*cb)(UartIrqEvent ev, uint8_t data)) {
|
||||
irq_cb = cb;
|
||||
if (irq_cb == NULL)
|
||||
NVIC_DisableIRQ(USART1_IRQn);
|
||||
else
|
||||
NVIC_EnableIRQ(USART1_IRQn);
|
||||
}
|
||||
|
||||
void USART1_IRQHandler(void) {
|
||||
if (LL_USART_IsActiveFlag_RXNE_RXFNE(USART1)) {
|
||||
uint8_t data = LL_USART_ReceiveData8(USART1);
|
||||
irq_cb(UartIrqEventRXNE, data);
|
||||
} else if (LL_USART_IsActiveFlag_IDLE(USART1)) {
|
||||
irq_cb(UartIrqEventIDLE, 0);
|
||||
LL_USART_ClearFlag_IDLE(USART1);
|
||||
}
|
||||
|
||||
//TODO: more events
|
||||
}
|
||||
|
||||
void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
|
||||
if (!furi_hal_console_alive)
|
||||
return;
|
||||
|
||||
// Transmit data
|
||||
furi_hal_console_uart_tx(buffer, buffer_size);
|
||||
furi_hal_usart_tx(buffer, buffer_size);
|
||||
// Wait for TC flag to be raised for last char
|
||||
while (!LL_USART_IsActiveFlag_TC(USART1));
|
||||
}
|
||||
@@ -67,9 +121,9 @@ void furi_hal_console_tx_with_new_line(const uint8_t* buffer, size_t buffer_size
|
||||
return;
|
||||
|
||||
// Transmit data
|
||||
furi_hal_console_uart_tx(buffer, buffer_size);
|
||||
furi_hal_usart_tx(buffer, buffer_size);
|
||||
// Transmit new line symbols
|
||||
furi_hal_console_uart_tx((const uint8_t*)"\r\n", 2);
|
||||
furi_hal_usart_tx((const uint8_t*)"\r\n", 2);
|
||||
// Wait for TC flag to be raised for last char
|
||||
while (!LL_USART_IsActiveFlag_TC(USART1));
|
||||
}
|
||||
|
@@ -7,6 +7,12 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
UartIrqEventRXNE,
|
||||
UartIrqEventIDLE,
|
||||
//TODO: more events
|
||||
} UartIrqEvent;
|
||||
|
||||
void furi_hal_console_init();
|
||||
|
||||
void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size);
|
||||
@@ -23,6 +29,18 @@ void furi_hal_console_printf(const char format[], ...);
|
||||
|
||||
void furi_hal_console_puts(const char* data);
|
||||
|
||||
|
||||
void furi_hal_usart_init();
|
||||
|
||||
void furi_hal_usart_deinit();
|
||||
|
||||
void furi_hal_usart_set_br(uint32_t baud);
|
||||
|
||||
void furi_hal_usart_tx(const uint8_t* buffer, size_t buffer_size);
|
||||
|
||||
void furi_hal_usart_set_irq_cb(void (*cb)(UartIrqEvent ev, uint8_t data));
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
105
firmware/targets/f6/furi-hal/furi-hal-lpuart.c
Normal file
105
firmware/targets/f6/furi-hal/furi-hal-lpuart.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <furi-hal-lpuart.h>
|
||||
#include <stdbool.h>
|
||||
#include <stm32wbxx_ll_gpio.h>
|
||||
#include <stm32wbxx_ll_lpuart.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
static void (*irq_cb)(uint8_t ev, uint8_t data);
|
||||
|
||||
void furi_hal_lpuart_init() {
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = PC0_Pin|PC1_Pin;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_8;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
LL_RCC_SetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE_PCLK1);
|
||||
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPUART1);
|
||||
|
||||
LL_LPUART_InitTypeDef LPUART_InitStruct = {0};
|
||||
LPUART_InitStruct.PrescalerValue = LL_LPUART_PRESCALER_DIV1;
|
||||
LPUART_InitStruct.BaudRate = 115200;
|
||||
LPUART_InitStruct.DataWidth = LL_LPUART_DATAWIDTH_8B;
|
||||
LPUART_InitStruct.StopBits = LL_LPUART_STOPBITS_1;
|
||||
LPUART_InitStruct.Parity = LL_LPUART_PARITY_NONE;
|
||||
LPUART_InitStruct.TransferDirection = LL_LPUART_DIRECTION_TX_RX;
|
||||
LPUART_InitStruct.HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
|
||||
LL_LPUART_Init(LPUART1, &LPUART_InitStruct);
|
||||
LL_LPUART_SetTXFIFOThreshold(LPUART1, LL_LPUART_FIFOTHRESHOLD_1_8);
|
||||
LL_LPUART_SetRXFIFOThreshold(LPUART1, LL_LPUART_FIFOTHRESHOLD_1_8);
|
||||
LL_LPUART_EnableFIFO(LPUART1);
|
||||
|
||||
LL_LPUART_Enable(LPUART1);
|
||||
|
||||
while((!(LL_LPUART_IsActiveFlag_TEACK(LPUART1))) || (!(LL_LPUART_IsActiveFlag_REACK(LPUART1))));
|
||||
|
||||
LL_LPUART_EnableIT_RXNE_RXFNE(LPUART1);
|
||||
LL_LPUART_EnableIT_IDLE(LPUART1);
|
||||
HAL_NVIC_SetPriority(LPUART1_IRQn, 5, 0);
|
||||
|
||||
FURI_LOG_I("FuriHalLpUart", "Init OK");
|
||||
}
|
||||
|
||||
void furi_hal_lpuart_set_br(uint32_t baud) {
|
||||
if (LL_LPUART_IsEnabled(LPUART1)) {
|
||||
// Wait for transfer complete flag
|
||||
while (!LL_LPUART_IsActiveFlag_TC(LPUART1));
|
||||
LL_LPUART_Disable(LPUART1);
|
||||
uint32_t uartclk = LL_RCC_GetLPUARTClockFreq(LL_RCC_GetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE_PCLK1));
|
||||
if (uartclk/baud > 4095) {
|
||||
LL_LPUART_SetPrescaler(LPUART1, LL_LPUART_PRESCALER_DIV32);
|
||||
LL_LPUART_SetBaudRate(LPUART1, uartclk, LL_LPUART_PRESCALER_DIV32, baud);
|
||||
} else {
|
||||
LL_LPUART_SetPrescaler(LPUART1, LL_LPUART_PRESCALER_DIV1);
|
||||
LL_LPUART_SetBaudRate(LPUART1, uartclk, LL_LPUART_PRESCALER_DIV1, baud);
|
||||
}
|
||||
|
||||
LL_LPUART_Enable(LPUART1);
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_lpuart_deinit() {
|
||||
furi_hal_lpuart_set_irq_cb(NULL);
|
||||
LL_GPIO_SetPinMode(GPIOC, PC0_Pin, LL_GPIO_MODE_ANALOG);
|
||||
LL_GPIO_SetPinMode(GPIOC, PC1_Pin, LL_GPIO_MODE_ANALOG);
|
||||
LL_LPUART_Disable(LPUART1);
|
||||
LL_APB1_GRP2_DisableClock(LL_APB1_GRP2_PERIPH_LPUART1);
|
||||
}
|
||||
|
||||
void furi_hal_lpuart_tx(const uint8_t* buffer, size_t buffer_size) {
|
||||
if (LL_LPUART_IsEnabled(LPUART1) == 0)
|
||||
return;
|
||||
|
||||
while(buffer_size > 0) {
|
||||
while (!LL_LPUART_IsActiveFlag_TXE(LPUART1));
|
||||
|
||||
LL_LPUART_TransmitData8(LPUART1, *buffer);
|
||||
|
||||
buffer++;
|
||||
buffer_size--;
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_lpuart_set_irq_cb(void (*cb)(UartIrqEvent ev, uint8_t data)) {
|
||||
irq_cb = cb;
|
||||
if (irq_cb == NULL)
|
||||
NVIC_DisableIRQ(LPUART1_IRQn);
|
||||
else
|
||||
NVIC_EnableIRQ(LPUART1_IRQn);
|
||||
}
|
||||
|
||||
void LPUART1_IRQHandler(void) {
|
||||
if (LL_LPUART_IsActiveFlag_RXNE_RXFNE(LPUART1)) {
|
||||
uint8_t data = LL_LPUART_ReceiveData8(LPUART1);
|
||||
irq_cb(UartIrqEventRXNE, data);
|
||||
} else if (LL_LPUART_IsActiveFlag_IDLE(LPUART1)) {
|
||||
irq_cb(UartIrqEventIDLE, 0);
|
||||
LL_LPUART_ClearFlag_IDLE(LPUART1);
|
||||
}
|
||||
|
||||
//TODO: more events
|
||||
}
|
24
firmware/targets/f6/furi-hal/furi-hal-lpuart.h
Normal file
24
firmware/targets/f6/furi-hal/furi-hal-lpuart.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "furi-hal-console.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
void furi_hal_lpuart_init();
|
||||
|
||||
void furi_hal_lpuart_deinit();
|
||||
|
||||
void furi_hal_lpuart_set_br(uint32_t baud);
|
||||
|
||||
void furi_hal_lpuart_tx(const uint8_t* buffer, size_t buffer_size);
|
||||
|
||||
void furi_hal_lpuart_set_irq_cb(void (*cb)(UartIrqEvent ev, uint8_t data));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -1,6 +1,5 @@
|
||||
#include "furi-hal-version.h"
|
||||
#include "furi-hal-usb_i.h"
|
||||
#include "furi-hal-vcp_i.h"
|
||||
#include "furi-hal-usb-cdc_i.h"
|
||||
#include <furi.h>
|
||||
|
||||
@@ -17,6 +16,8 @@
|
||||
|
||||
#define CDC_NTF_SZ 0x08
|
||||
|
||||
#define IF_NUM_MAX 2
|
||||
|
||||
struct CdcIadDescriptor {
|
||||
struct usb_iad_descriptor comm_iad;
|
||||
struct usb_interface_descriptor comm;
|
||||
@@ -343,12 +344,8 @@ static const struct CdcConfigDescriptorDual cdc_cfg_desc_dual = {
|
||||
},
|
||||
};
|
||||
|
||||
static struct usb_cdc_line_coding cdc_line = {
|
||||
.dwDTERate = 38400,
|
||||
.bCharFormat = USB_CDC_1_STOP_BITS,
|
||||
.bParityType = USB_CDC_NO_PARITY,
|
||||
.bDataBits = 8,
|
||||
};
|
||||
static struct usb_cdc_line_coding cdc_config[IF_NUM_MAX] = {};
|
||||
|
||||
static void cdc_init(usbd_device* dev, struct UsbInterface* intf);
|
||||
static void cdc_deinit(usbd_device *dev);
|
||||
static void cdc_on_wakeup(usbd_device *dev);
|
||||
@@ -358,6 +355,7 @@ static usbd_respond cdc_ep_config (usbd_device *dev, uint8_t cfg);
|
||||
static usbd_respond cdc_control (usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback);
|
||||
static usbd_device* usb_dev;
|
||||
static struct UsbInterface* cdc_if_cur = NULL;
|
||||
static CdcCallbacks* callbacks[IF_NUM_MAX] = {NULL};
|
||||
|
||||
struct UsbInterface usb_cdc_single = {
|
||||
.init = cdc_init,
|
||||
@@ -429,6 +427,17 @@ static void cdc_deinit(usbd_device *dev) {
|
||||
cdc_if_cur = NULL;
|
||||
}
|
||||
|
||||
void furi_hal_cdc_set_callbacks(uint8_t if_num, CdcCallbacks* cb) {
|
||||
if (if_num < 2)
|
||||
callbacks[if_num] = cb;
|
||||
}
|
||||
|
||||
struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num) {
|
||||
if (if_num < 2)
|
||||
return &cdc_config[if_num];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -444,25 +453,47 @@ int32_t furi_hal_cdc_receive(uint8_t if_num, uint8_t* buf, uint16_t max_len) {
|
||||
}
|
||||
|
||||
static void cdc_on_wakeup(usbd_device *dev) {
|
||||
furi_hal_vcp_on_usb_resume();
|
||||
for (uint8_t i = 0; i < IF_NUM_MAX; i++) {
|
||||
if (callbacks[i] != NULL) {
|
||||
if (callbacks[i]->state_callback != NULL)
|
||||
callbacks[i]->state_callback(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void cdc_on_suspend(usbd_device *dev) {
|
||||
furi_hal_vcp_on_usb_suspend();
|
||||
for (uint8_t i = 0; i < IF_NUM_MAX; i++) {
|
||||
if (callbacks[i] != NULL) {
|
||||
if (callbacks[i]->state_callback != NULL)
|
||||
callbacks[i]->state_callback(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void cdc_rx_ep_callback (usbd_device *dev, uint8_t event, uint8_t ep) {
|
||||
uint8_t if_num = 0;
|
||||
if (ep == CDC0_RXD_EP)
|
||||
furi_hal_vcp_on_cdc_rx(0);
|
||||
if_num = 0;
|
||||
else
|
||||
furi_hal_vcp_on_cdc_rx(1);
|
||||
if_num = 1;
|
||||
|
||||
if (callbacks[if_num] != NULL) {
|
||||
if (callbacks[if_num]->rx_ep_callback != NULL)
|
||||
callbacks[if_num]->rx_ep_callback();
|
||||
}
|
||||
}
|
||||
|
||||
static void cdc_tx_ep_callback (usbd_device *dev, uint8_t event, uint8_t ep) {
|
||||
uint8_t if_num = 0;
|
||||
if (ep == CDC0_TXD_EP)
|
||||
furi_hal_vcp_on_cdc_tx_complete(0);
|
||||
if_num = 0;
|
||||
else
|
||||
furi_hal_vcp_on_cdc_tx_complete(1);
|
||||
if_num = 1;
|
||||
|
||||
if (callbacks[if_num] != NULL) {
|
||||
if (callbacks[if_num]->tx_ep_callback != NULL)
|
||||
callbacks[if_num]->tx_ep_callback();
|
||||
}
|
||||
}
|
||||
|
||||
static void cdc_txrx_ep_callback (usbd_device *dev, uint8_t event, uint8_t ep) {
|
||||
@@ -494,13 +525,15 @@ static usbd_respond cdc_ep_config (usbd_device *dev, uint8_t cfg) {
|
||||
return usbd_ack;
|
||||
case 1:
|
||||
/* configuring device */
|
||||
if ((CDC0_TXD_EP & 0x7F) != (CDC0_RXD_EP & 0x7F)) {
|
||||
if ((CDC0_TXD_EP & 0x7F) != (CDC0_RXD_EP & 0x7F)) {
|
||||
// 2x unidirectional endpoint mode with dualbuf
|
||||
usbd_ep_config(dev, CDC0_RXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
|
||||
usbd_ep_config(dev, CDC0_TXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
|
||||
usbd_ep_config(dev, CDC0_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
|
||||
usbd_reg_endpoint(dev, CDC0_RXD_EP, cdc_rx_ep_callback);
|
||||
usbd_reg_endpoint(dev, CDC0_TXD_EP, cdc_tx_ep_callback);
|
||||
} else {
|
||||
} else {
|
||||
// 1x bidirectional endpoint mode
|
||||
usbd_ep_config(dev, CDC0_RXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
|
||||
usbd_ep_config(dev, CDC0_TXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
|
||||
usbd_ep_config(dev, CDC0_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
|
||||
@@ -532,20 +565,33 @@ static usbd_respond cdc_ep_config (usbd_device *dev, uint8_t cfg) {
|
||||
}
|
||||
|
||||
/* Control requests handler */
|
||||
static usbd_respond cdc_control (usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback) {
|
||||
static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
|
||||
/* CDC control requests */
|
||||
if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) == (USB_REQ_INTERFACE | USB_REQ_CLASS)
|
||||
&& req->wIndex == 0 ) {
|
||||
switch (req->bRequest) {
|
||||
uint8_t if_num = 0;
|
||||
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) == (USB_REQ_INTERFACE | USB_REQ_CLASS)
|
||||
&& (req->wIndex == 0 || req->wIndex == 2)) {
|
||||
if (req->wIndex == 0)
|
||||
if_num = 0;
|
||||
else
|
||||
if_num = 1;
|
||||
|
||||
switch(req->bRequest) {
|
||||
case USB_CDC_SET_CONTROL_LINE_STATE:
|
||||
furi_hal_vcp_on_cdc_control_line(req->wValue);
|
||||
if (callbacks[if_num] != NULL) {
|
||||
if (callbacks[if_num]->ctrl_line_callback != NULL)
|
||||
callbacks[if_num]->ctrl_line_callback(req->wValue);
|
||||
}
|
||||
return usbd_ack;
|
||||
case USB_CDC_SET_LINE_CODING:
|
||||
memcpy(&cdc_line, req->data, sizeof(cdc_line));
|
||||
memcpy(&cdc_config[if_num], req->data, sizeof(cdc_config[0]));
|
||||
if (callbacks[if_num] != NULL) {
|
||||
if (callbacks[if_num]->config_callback != NULL)
|
||||
callbacks[if_num]->config_callback(&cdc_config[if_num]);
|
||||
}
|
||||
return usbd_ack;
|
||||
case USB_CDC_GET_LINE_CODING:
|
||||
dev->status.data_ptr = &cdc_line;
|
||||
dev->status.data_count = sizeof(cdc_line);
|
||||
dev->status.data_ptr = &cdc_config[if_num];
|
||||
dev->status.data_count = sizeof(cdc_config[0]);
|
||||
return usbd_ack;
|
||||
default:
|
||||
return usbd_fail;
|
||||
|
@@ -1,6 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#define CDC_DATA_SZ 0x40
|
||||
#include <stdint.h>
|
||||
#include "usb_cdc.h"
|
||||
|
||||
#define CDC_DATA_SZ 64
|
||||
|
||||
typedef struct {
|
||||
void (*tx_ep_callback)(void);
|
||||
void (*rx_ep_callback)(void);
|
||||
void (*state_callback)(uint8_t state);
|
||||
void (*ctrl_line_callback)(uint8_t state);
|
||||
void (*config_callback)(struct usb_cdc_line_coding* config);
|
||||
} CdcCallbacks;
|
||||
|
||||
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);
|
||||
|
||||
void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len);
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
#include "furi-hal-version.h"
|
||||
#include "furi-hal-usb_i.h"
|
||||
#include "furi-hal-usb.h"
|
||||
#include "furi-hal-vcp_i.h"
|
||||
#include <furi.h>
|
||||
|
||||
#include "usb.h"
|
||||
@@ -34,6 +33,7 @@ struct UsbCfg{
|
||||
UsbMode mode_cur;
|
||||
UsbMode mode_next;
|
||||
bool enabled;
|
||||
bool connected;
|
||||
} usb_config;
|
||||
|
||||
static void furi_hal_usb_tmr_cb(void* context);
|
||||
@@ -158,11 +158,15 @@ static usbd_respond usb_descriptor_get (usbd_ctlreq *req, void **address, uint16
|
||||
}
|
||||
|
||||
static void susp_evt(usbd_device *dev, uint8_t event, uint8_t ep) {
|
||||
if (usb_if_modes[usb_config.mode_cur] != NULL)
|
||||
if ((usb_if_modes[usb_config.mode_cur] != NULL) && (usb_config.connected == true)) {
|
||||
usb_config.connected = false;
|
||||
usb_if_modes[usb_config.mode_cur]->suspend(&udev);
|
||||
}
|
||||
}
|
||||
|
||||
static void wkup_evt(usbd_device *dev, uint8_t event, uint8_t ep) {
|
||||
if (usb_if_modes[usb_config.mode_cur] != NULL)
|
||||
if ((usb_if_modes[usb_config.mode_cur] != NULL) && (usb_config.connected == false)) {
|
||||
usb_config.connected = true;
|
||||
usb_if_modes[usb_config.mode_cur]->wakeup(&udev);
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,3 @@
|
||||
#include <furi-hal-vcp_i.h>
|
||||
#include <furi-hal-usb-cdc_i.h>
|
||||
|
||||
#include <furi.h>
|
||||
@@ -7,6 +6,7 @@
|
||||
#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)
|
||||
#define VCP_IF_NUM 0
|
||||
|
||||
typedef struct {
|
||||
volatile bool connected;
|
||||
@@ -17,6 +17,19 @@ typedef struct {
|
||||
osSemaphoreId_t tx_semaphore;
|
||||
} FuriHalVcp;
|
||||
|
||||
static void vcp_on_cdc_tx_complete();
|
||||
static void vcp_on_cdc_rx();
|
||||
static void vcp_state_callback(uint8_t state);
|
||||
static void vcp_on_cdc_control_line(uint8_t state);
|
||||
|
||||
static CdcCallbacks cdc_cb = {
|
||||
vcp_on_cdc_tx_complete,
|
||||
vcp_on_cdc_rx,
|
||||
vcp_state_callback,
|
||||
vcp_on_cdc_control_line,
|
||||
NULL,
|
||||
};
|
||||
|
||||
static FuriHalVcp* furi_hal_vcp = NULL;
|
||||
|
||||
static const uint8_t ascii_soh = 0x01;
|
||||
@@ -34,9 +47,22 @@ void furi_hal_vcp_init() {
|
||||
|
||||
furi_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
|
||||
|
||||
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
|
||||
|
||||
FURI_LOG_I("FuriHalVcp", "Init OK");
|
||||
}
|
||||
|
||||
void furi_hal_vcp_enable() {
|
||||
furi_hal_cdc_set_callbacks(VCP_IF_NUM, &cdc_cb);
|
||||
furi_hal_vcp->connected = true;
|
||||
}
|
||||
|
||||
void furi_hal_vcp_disable() {
|
||||
furi_hal_cdc_set_callbacks(VCP_IF_NUM, NULL);
|
||||
furi_hal_vcp->connected = false;
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
|
||||
furi_assert(furi_hal_vcp);
|
||||
|
||||
@@ -68,24 +94,22 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
||||
batch_size = APP_TX_DATA_SIZE;
|
||||
}
|
||||
|
||||
furi_hal_cdc_send(0, (uint8_t*)buffer, batch_size);
|
||||
furi_hal_cdc_send(VCP_IF_NUM, (uint8_t*)buffer, batch_size);
|
||||
size -= batch_size;
|
||||
buffer += batch_size;
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_usb_resume() {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_usb_suspend() {
|
||||
if (furi_hal_vcp->connected) {
|
||||
static void vcp_state_callback(uint8_t state) {
|
||||
if (state == 1)
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
else if (furi_hal_vcp->connected) {
|
||||
furi_hal_vcp->connected = false;
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
|
||||
static void vcp_on_cdc_control_line(uint8_t state) {
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
// bit 0: DTR state, bit 1: RTS state
|
||||
@@ -110,30 +134,26 @@ void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_rx(uint8_t if_num) {
|
||||
static void vcp_on_cdc_rx() {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
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);
|
||||
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(VCP_IF_NUM, 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;
|
||||
};
|
||||
}
|
||||
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(uint8_t if_num) {
|
||||
if (if_num == 0)
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
static void vcp_on_cdc_tx_complete() {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
|
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi-hal-vcp.h>
|
||||
|
||||
void furi_hal_vcp_on_usb_resume();
|
||||
|
||||
void furi_hal_vcp_on_usb_suspend();
|
||||
|
||||
void furi_hal_vcp_on_cdc_control_line(uint8_t state);
|
||||
|
||||
void furi_hal_vcp_on_cdc_rx(uint8_t if_num);
|
||||
|
||||
void furi_hal_vcp_on_cdc_tx_complete(uint8_t if_num);
|
@@ -33,9 +33,9 @@ void furi_hal_init() {
|
||||
furi_hal_crypto_init();
|
||||
|
||||
// VCP + USB
|
||||
furi_hal_vcp_init();
|
||||
furi_hal_usb_init();
|
||||
furi_hal_usb_set_config(UsbModeVcpSingle);
|
||||
furi_hal_vcp_init();
|
||||
FURI_LOG_I("HAL", "USB OK");
|
||||
|
||||
furi_hal_i2c_init();
|
||||
|
@@ -72,6 +72,7 @@ C_SOURCES += \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_spi.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_tim.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_usart.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_lpuart.c \
|
||||
$(CUBE_DIR)/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_utils.c
|
||||
|
||||
# FreeRTOS
|
||||
|
Reference in New Issue
Block a user