[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));
|
||||
}
|
||||
|
Reference in New Issue
Block a user