ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
tlm_uart.c
Go to the documentation of this file.
1
5#include "tlm_uart.h"
6#include "cmsis_os2.h"
7#include <string.h>
8
9#define TLM_UART_BUF 256u
10
11static UART_HandleTypeDef *s_huart;
12static osSemaphoreId_t s_txFree;
13static uint8_t s_dmaBuf[TLM_UART_BUF]; /* must stay valid for the whole DMA */
14static volatile uint32_t s_dropped;
15
16int tlm_uart_init(UART_HandleTypeDef *huart)
17{
18 s_huart = huart;
19 s_txFree = osSemaphoreNew(1, 1, NULL);
20 return s_txFree ? 0 : -1;
21}
22
23int tlm_uart_send(const uint8_t *buf, uint16_t len, uint32_t timeout_ms)
24{
25 if (!s_huart || len > TLM_UART_BUF) return -1;
26 if (osSemaphoreAcquire(s_txFree, timeout_ms) != osOK) { s_dropped++; return -1; }
27 memcpy(s_dmaBuf, buf, len);
28 if (HAL_UART_Transmit_DMA(s_huart, s_dmaBuf, len) != HAL_OK) {
29 osSemaphoreRelease(s_txFree);
30 s_dropped++;
31 return -1;
32 }
33 return 0;
34}
35
36void tlm_uart_tx_cplt_isr(UART_HandleTypeDef *huart)
37{
38 if (huart == s_huart) (void)osSemaphoreRelease(s_txFree); /* ISR-safe in CMSIS-RTOS2 */
39}
40
41uint32_t tlm_uart_dropped(void) { return s_dropped; }
static volatile uint32_t s_dropped
Definition tlm_uart.c:14
#define TLM_UART_BUF
Definition tlm_uart.c:9
static uint8_t s_dmaBuf[TLM_UART_BUF]
Definition tlm_uart.c:13
int tlm_uart_send(const uint8_t *buf, uint16_t len, uint32_t timeout_ms)
Non-blocking beyond timeout_ms: if the previous DMA has not finished, the frame is dropped.
Definition tlm_uart.c:23
static osSemaphoreId_t s_txFree
Definition tlm_uart.c:12
uint32_t tlm_uart_dropped(void)
Definition tlm_uart.c:41
int tlm_uart_init(UART_HandleTypeDef *huart)
Definition tlm_uart.c:16
void tlm_uart_tx_cplt_isr(UART_HandleTypeDef *huart)
To be called from HAL_UART_TxCpltCallback() (see 03 - Docs/firmware/build.md)
Definition tlm_uart.c:36
static UART_HandleTypeDef * s_huart
Definition tlm_uart.c:11
USB transport: UART -> ST-LINK Virtual COM Port (USART1 on the NUCLEO-WB55).