Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uart receiving data via dma and selecting speed and UART for logging output #3078

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e332b46
furi_hal: add selecting speed and USART for logging output
Skorpionm Sep 14, 2023
5cb1954
furi_hal: fix syntax
Skorpionm Sep 14, 2023
d58adbd
Furi_Hal: add DMA RX in LPUART
Skorpionm Sep 16, 2023
5ed59d9
Furi_Hal: add LPUART receiving data via dma
Skorpionm Sep 19, 2023
94efc86
Furi_Hal: add USART1 receiving data via dma
Skorpionm Sep 20, 2023
bd9c5bb
usb_uart_bridge: add Rx DMA
Skorpionm Sep 21, 2023
7d8aa41
furi_hal_usart: delete comments
Skorpionm Sep 21, 2023
65a16d9
API: api_symbols F18
Skorpionm Sep 21, 2023
83927af
Merge branch 'dev' into skorp/furi_hal_usart_console
Skorpionm Sep 21, 2023
52dd414
furi_hal_uart: fix syntax
Skorpionm Sep 21, 2023
0112169
Merge branch 'skorp/furi_hal_usart_console' of github.com:Flipper-Zer…
Skorpionm Sep 21, 2023
ee79a30
Merge branch 'dev' into skorp/furi_hal_usart_console
Skorpionm Sep 21, 2023
0e0cbc7
API: fix api_symbols version
Skorpionm Sep 21, 2023
334e869
Merge branch 'dev' into skorp/furi_hal_usart_console
Skorpionm Sep 21, 2023
83d6496
furi_hal_uart: add description of functions
Skorpionm Sep 21, 2023
a85aa9e
free-dap: delete submodule
Skorpionm Sep 21, 2023
99b6556
furi_hal: fix uart callback
Skorpionm Sep 24, 2023
e2be364
Merge branch 'dev' into skorp/furi_hal_usart_console
skotopes Oct 6, 2023
4ed9b97
FuriHal: remove unused includes
skotopes Oct 6, 2023
bb430bc
FuriHal: cleanup UART API naming
skotopes Oct 6, 2023
6e88845
Format sources and fix uart api usae by usb uart bridge
skotopes Oct 6, 2023
8bc91ef
FuriHal: more naming chnages in uart api
skotopes Oct 6, 2023
bc22992
FuriHal: fix comments
Skorpionm Oct 7, 2023
6c9cfbc
Merge remote-tracking branch 'origin/dev' into skorp/furi_hal_usart_c…
skotopes Oct 9, 2023
6bee333
UsbUartBridge: slightly more readable usb_uart_on_irq_rx_dma_cb routi…
skotopes Oct 9, 2023
a706d21
FuriHal: cleaup uart and console code
skotopes Oct 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions applications/main/gpio/usb_uart_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ typedef enum {

WorkerEvtTxStop = (1 << 2),
WorkerEvtCdcRx = (1 << 3),
WorkerEvtCdcTxComplete = (1 << 4),

WorkerEvtCfgChange = (1 << 4),
WorkerEvtCfgChange = (1 << 5),

WorkerEvtLineCfgSet = (1 << 5),
WorkerEvtCtrlLineSet = (1 << 6),
WorkerEvtLineCfgSet = (1 << 6),
WorkerEvtCtrlLineSet = (1 << 7),

} WorkerEvtFlags;

#define WORKER_ALL_RX_EVENTS \
(WorkerEvtStop | WorkerEvtRxDone | WorkerEvtCfgChange | WorkerEvtLineCfgSet | \
WorkerEvtCtrlLineSet)
WorkerEvtCtrlLineSet | WorkerEvtCdcTxComplete)
#define WORKER_ALL_TX_EVENTS (WorkerEvtTxStop | WorkerEvtCdcRx)

struct UsbUartBridge {
Expand Down Expand Up @@ -75,13 +76,18 @@ static const CdcCallbacks cdc_cb = {

static int32_t usb_uart_tx_thread(void* context);

static void usb_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
static void usb_uart_on_irq_rx_dma_cb(
FuriHalUartDmaEvent ev,
FuriHalUartId id_uart,
size_t data_len,
void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;

if(ev == UartIrqEventRXNE) {
furi_stream_buffer_send(usb_uart->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtRxDone);
}
UNUSED(ev);
uint8_t data[data_len];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't allocate data on stack with variable passed from outside

Copy link
Member Author

@Skorpionm Skorpionm Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

furi_hal_uart_dma_rx(id_uart, data, data_len);
furi_stream_buffer_send(usb_uart->rx_stream, data, data_len, 100);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no timeouts in interrupts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtRxDone);
}

static void usb_uart_vcp_init(UsbUartBridge* usb_uart, uint8_t vcp_ch) {
Expand Down Expand Up @@ -112,20 +118,17 @@ static void usb_uart_vcp_deinit(UsbUartBridge* usb_uart, uint8_t vcp_ch) {

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_console_deinit();
}
furi_hal_uart_set_irq_cb(uart_ch, usb_uart_on_irq_cb, usb_uart);
furi_hal_uart_init(uart_ch, 115200);
furi_hal_uart_dma_start(uart_ch, usb_uart_on_irq_rx_dma_cb, usb_uart);
}

static void usb_uart_serial_deinit(UsbUartBridge* usb_uart, uint8_t uart_ch) {
UNUSED(usb_uart);
furi_hal_uart_set_irq_cb(uart_ch, NULL, NULL);
furi_hal_uart_deinit(uart_ch);
if(uart_ch == FuriHalUartIdUSART1)
furi_hal_console_enable();
else if(uart_ch == FuriHalUartIdLPUART1)
furi_hal_uart_deinit(uart_ch);
furi_hal_console_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
}

static void usb_uart_set_baudrate(UsbUartBridge* usb_uart, uint32_t baudrate) {
Expand Down Expand Up @@ -186,7 +189,7 @@ static int32_t usb_uart_worker(void* context) {
furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
furi_check(!(events & FuriFlagError));
if(events & WorkerEvtStop) break;
if(events & WorkerEvtRxDone) {
if(events & WorkerEvtRxDone || events & WorkerEvtCdcTxComplete) {
size_t len = furi_stream_buffer_receive(
usb_uart->rx_stream, usb_uart->rx_buf, USB_CDC_PKT_LEN, 0);
if(len > 0) {
Expand All @@ -196,8 +199,6 @@ static int32_t usb_uart_worker(void* context) {
furi_mutex_acquire(usb_uart->usb_mutex, FuriWaitForever) == FuriStatusOk);
furi_hal_cdc_send(usb_uart->cfg.vcp_ch, usb_uart->rx_buf, len);
furi_check(furi_mutex_release(usb_uart->usb_mutex) == FuriStatusOk);
} else {
furi_stream_buffer_reset(usb_uart->rx_stream);
}
}
}
Expand Down Expand Up @@ -310,6 +311,7 @@ static int32_t usb_uart_tx_thread(void* context) {
static void vcp_on_cdc_tx_complete(void* context) {
UsbUartBridge* usb_uart = (UsbUartBridge*)context;
furi_semaphore_release(usb_uart->tx_sem);
furi_thread_flags_set(furi_thread_get_id(usb_uart->thread), WorkerEvtCdcTxComplete);
}

static void vcp_on_cdc_rx(void* context) {
Expand Down
4 changes: 2 additions & 2 deletions documentation/FuriHalBus.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ Below is the list of DMA channels and their usage by the system.
| -- | 3 | | |
| -- | 4 | yes | pulse reader |
| -- | 5 | | |
| -- | 6 | | |
| -- | 7 | | |
| -- | 6 | yes | USART1_Rx |
| -- | 7 | yes | LPUART1_Rx |
| DMA2 | 1 | yes | infrared, lfrfid, subghz, |
| -- | 2 | yes | -- |
| -- | 3 | yes | cc1101_ext |
Expand Down
11 changes: 8 additions & 3 deletions firmware/targets/f18/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,39.1,,
Version,+,40.0,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
Expand Down Expand Up @@ -1025,13 +1025,14 @@ Function,+,furi_hal_clock_mco_disable,void,
Function,+,furi_hal_clock_mco_enable,void,"FuriHalClockMcoSourceId, FuriHalClockMcoDivisorId"
Function,-,furi_hal_clock_resume_tick,void,
Function,-,furi_hal_clock_suspend_tick,void,
Function,+,furi_hal_console_deinit,void,
Function,-,furi_hal_clock_switch_hse2hsi,void,
Function,-,furi_hal_clock_switch_hse2pll,_Bool,
Function,-,furi_hal_clock_switch_hsi2hse,void,
Function,-,furi_hal_clock_switch_pll2hse,_Bool,
Function,+,furi_hal_console_disable,void,
Function,+,furi_hal_console_enable,void,
Function,+,furi_hal_console_init,void,
Function,+,furi_hal_console_init,void,"FuriHalUartId, uint32_t"
Function,+,furi_hal_console_printf,void,"const char[], ..."
Function,+,furi_hal_console_puts,void,const char*
Function,+,furi_hal_console_set_tx_callback,void,"FuriHalConsoleTxCallback, void*"
Expand Down Expand Up @@ -1265,12 +1266,16 @@ Function,-,furi_hal_spi_dma_init,void,
Function,+,furi_hal_spi_release,void,FuriHalSpiBusHandle*
Function,+,furi_hal_switch,void,void*
Function,+,furi_hal_uart_deinit,void,FuriHalUartId
Function,+,furi_hal_uart_dma_bytes_available,size_t,FuriHalUartId
Function,+,furi_hal_uart_init,void,"FuriHalUartId, uint32_t"
Function,+,furi_hal_uart_resume,void,FuriHalUartId
Function,+,furi_hal_uart_dma_rx,size_t,"FuriHalUartId, uint8_t*, size_t"
Function,+,furi_hal_uart_set_br,void,"FuriHalUartId, uint32_t"
Function,+,furi_hal_uart_set_irq_cb,void,"FuriHalUartId, void (*)(UartIrqEvent, uint8_t, void*), void*"
Function,+,furi_hal_uart_dma_start,void,"FuriHalUartId, FuriHalUartDmaRxCallback, void*"
Function,+,furi_hal_uart_set_irq_cb,void,"FuriHalUartId, FuriHalUartRxByteCallback, void*"
Function,+,furi_hal_uart_suspend,void,FuriHalUartId
Function,+,furi_hal_uart_tx,void,"FuriHalUartId, uint8_t*, size_t"
Function,+,furi_hal_uart_wait_tx_complete,void,FuriHalUartId
Function,+,furi_hal_usb_disable,void,
Function,+,furi_hal_usb_enable,void,
Function,+,furi_hal_usb_get_config,FuriHalUsbInterface*,
Expand Down
2 changes: 1 addition & 1 deletion firmware/targets/f18/furi_hal/furi_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void furi_hal_init() {
furi_hal_mpu_init();
furi_hal_clock_init();
furi_hal_random_init();
furi_hal_console_init();
furi_hal_console_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
furi_hal_rtc_init();
furi_hal_interrupt_init();
furi_hal_flash_init();
Expand Down
11 changes: 8 additions & 3 deletions firmware/targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,39.1,,
Version,+,40.0,,
Header,+,applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Expand Down Expand Up @@ -1096,13 +1096,14 @@ Function,+,furi_hal_clock_mco_disable,void,
Function,+,furi_hal_clock_mco_enable,void,"FuriHalClockMcoSourceId, FuriHalClockMcoDivisorId"
Function,-,furi_hal_clock_resume_tick,void,
Function,-,furi_hal_clock_suspend_tick,void,
Function,+,furi_hal_console_deinit,void,
Function,-,furi_hal_clock_switch_hse2hsi,void,
Function,-,furi_hal_clock_switch_hse2pll,_Bool,
Function,-,furi_hal_clock_switch_hsi2hse,void,
Function,-,furi_hal_clock_switch_pll2hse,_Bool,
Function,+,furi_hal_console_disable,void,
Function,+,furi_hal_console_enable,void,
Function,+,furi_hal_console_init,void,
Function,+,furi_hal_console_init,void,"FuriHalUartId, uint32_t"
Function,+,furi_hal_console_printf,void,"const char[], ..."
Function,+,furi_hal_console_puts,void,const char*
Function,+,furi_hal_console_set_tx_callback,void,"FuriHalConsoleTxCallback, void*"
Expand Down Expand Up @@ -1436,12 +1437,16 @@ Function,+,furi_hal_subghz_tx,_Bool,
Function,+,furi_hal_subghz_write_packet,void,"const uint8_t*, uint8_t"
Function,+,furi_hal_switch,void,void*
Function,+,furi_hal_uart_deinit,void,FuriHalUartId
Function,+,furi_hal_uart_dma_bytes_available,size_t,FuriHalUartId
Function,+,furi_hal_uart_init,void,"FuriHalUartId, uint32_t"
Function,+,furi_hal_uart_resume,void,FuriHalUartId
Function,+,furi_hal_uart_dma_rx,size_t,"FuriHalUartId, uint8_t*, size_t"
Function,+,furi_hal_uart_set_br,void,"FuriHalUartId, uint32_t"
Function,+,furi_hal_uart_set_irq_cb,void,"FuriHalUartId, void (*)(UartIrqEvent, uint8_t, void*), void*"
Function,+,furi_hal_uart_dma_start,void,"FuriHalUartId, FuriHalUartDmaRxCallback, void*"
Function,+,furi_hal_uart_set_irq_cb,void,"FuriHalUartId, FuriHalUartRxByteCallback, void*"
Function,+,furi_hal_uart_suspend,void,FuriHalUartId
Function,+,furi_hal_uart_tx,void,"FuriHalUartId, uint8_t*, size_t"
Function,+,furi_hal_uart_wait_tx_complete,void,FuriHalUartId
Function,+,furi_hal_usb_disable,void,
Function,+,furi_hal_usb_enable,void,
Function,+,furi_hal_usb_get_config,FuriHalUsbInterface*,
Expand Down
2 changes: 1 addition & 1 deletion firmware/targets/f7/furi_hal/furi_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void furi_hal_init() {
furi_hal_mpu_init();
furi_hal_clock_init();
furi_hal_random_init();
furi_hal_console_init();
furi_hal_console_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
furi_hal_rtc_init();
furi_hal_interrupt_init();
furi_hal_flash_init();
Expand Down
51 changes: 32 additions & 19 deletions firmware/targets/f7/furi_hal/furi_hal_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,55 @@

#define TAG "FuriHalConsole"

#ifdef HEAP_PRINT_DEBUG
#define CONSOLE_BAUDRATE 1843200
#else
#define CONSOLE_BAUDRATE 230400
#endif

typedef struct {
bool alive;
bool initialized;
FuriHalUartId channel;
uint32_t baudrate;
FuriHalConsoleTxCallback tx_callback;
void* tx_callback_context;
} FuriHalConsole;

FuriHalConsole furi_hal_console = {
.initialized = false,
.alive = false,
.channel = FuriHalUartIdUSART1,
.tx_callback = NULL,
.baudrate = CONSOLE_BAUDRATE,
.tx_callback_context = NULL,
};

void furi_hal_console_init() {
furi_hal_uart_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
void furi_hal_console_init(FuriHalUartId channel, uint32_t baudrate) {
furi_check(!furi_hal_console.initialized);

furi_hal_console.channel = channel;
furi_hal_console.baudrate = baudrate;
furi_hal_uart_init(channel, baudrate);
furi_hal_console.alive = true;
furi_hal_console.initialized = true;
}

void furi_hal_console_deinit() {
furi_check(furi_hal_console.initialized);
furi_hal_uart_deinit(furi_hal_console.channel);
furi_hal_console.channel = FuriHalUartIdUSART1;
furi_hal_console.baudrate = CONSOLE_BAUDRATE;
furi_hal_console.alive = false;
furi_hal_console.initialized = false;
}

void furi_hal_console_enable() {
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
while(!LL_USART_IsActiveFlag_TC(USART1))
;
furi_hal_uart_set_br(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
furi_check(furi_hal_console.initialized);
furi_hal_uart_set_irq_cb(furi_hal_console.channel, NULL, NULL);
furi_hal_uart_wait_tx_complete(furi_hal_console.channel);

furi_hal_uart_set_br(furi_hal_console.channel, furi_hal_console.baudrate);
furi_hal_console.alive = true;
}

void furi_hal_console_disable() {
while(!LL_USART_IsActiveFlag_TC(USART1))
;
furi_check(furi_hal_console.initialized);
furi_hal_uart_wait_tx_complete(furi_hal_console.channel);
furi_hal_console.alive = false;
}

Expand All @@ -63,10 +78,9 @@ void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
furi_hal_console.tx_callback(buffer, buffer_size, furi_hal_console.tx_callback_context);
}

furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)buffer, buffer_size);
furi_hal_uart_tx(furi_hal_console.channel, (uint8_t*)buffer, buffer_size);
// Wait for TC flag to be raised for last char
while(!LL_USART_IsActiveFlag_TC(USART1))
;
furi_hal_uart_wait_tx_complete(furi_hal_console.channel);
FURI_CRITICAL_EXIT();
}

Expand All @@ -79,8 +93,7 @@ void furi_hal_console_tx_with_new_line(const uint8_t* buffer, size_t buffer_size
// Transmit new line symbols
furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)"\r\n", 2);
// Wait for TC flag to be raised for last char
while(!LL_USART_IsActiveFlag_TC(USART1))
;
furi_hal_uart_wait_tx_complete(furi_hal_console.channel);
FURI_CRITICAL_EXIT();
}

Expand Down
12 changes: 11 additions & 1 deletion firmware/targets/f7/furi_hal/furi_hal_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
#include <stdint.h>
#include <stdio.h>

#include <furi_hal_uart.h>

#ifdef HEAP_PRINT_DEBUG
#define CONSOLE_BAUDRATE 1843200
#else
#define CONSOLE_BAUDRATE 230400
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef void (*FuriHalConsoleTxCallback)(const uint8_t* buffer, size_t size, void* context);

void furi_hal_console_init();
void furi_hal_console_init(FuriHalUartId channel, uint32_t baudrate);

void furi_hal_console_deinit();

void furi_hal_console_enable();

Expand Down
14 changes: 14 additions & 0 deletions firmware/targets/f7/furi_hal/furi_hal_interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const IRQn_Type furi_hal_interrupt_irqn[FuriHalInterruptIdMax] = {
// LPTIMx
[FuriHalInterruptIdLpTim1] = LPTIM1_IRQn,
[FuriHalInterruptIdLpTim2] = LPTIM2_IRQn,

// UARTx
[FuriHalInterruptIdUart1] = USART1_IRQn,

// LPUARTx
[FuriHalInterruptIdLpUart1] = LPUART1_IRQn,
};

__attribute__((always_inline)) static inline void
Expand Down Expand Up @@ -328,3 +334,11 @@ void LPTIM1_IRQHandler() {
void LPTIM2_IRQHandler() {
furi_hal_interrupt_call(FuriHalInterruptIdLpTim2);
}

void USART1_IRQHandler(void) {
furi_hal_interrupt_call(FuriHalInterruptIdUart1);
}

void LPUART1_IRQHandler(void) {
furi_hal_interrupt_call(FuriHalInterruptIdLpUart1);
}
6 changes: 6 additions & 0 deletions firmware/targets/f7/furi_hal/furi_hal_interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ typedef enum {
FuriHalInterruptIdLpTim1,
FuriHalInterruptIdLpTim2,

//UARTx
FuriHalInterruptIdUart1,

//LPUARTx
FuriHalInterruptIdLpUart1,

// Service value
FuriHalInterruptIdMax,
} FuriHalInterruptId;
Expand Down
Loading