mothercup/mcm-ddc-ble/inc/bsp_usart.h

63 lines
2.0 KiB
C

/**
* @file bsp_usart.h
* @brief USART1 support: printf retarget (polling TX), DMA TX, RX interrupt queue.
* TX=PB6, RX=PB7, 115200 8N1.
*/
#ifndef __BSP_USART_H__
#define __BSP_USART_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "n32wb03x.h"
#define BSP_USARTx USART1
#define BSP_USART_CLK RCC_APB2_PERIPH_USART1
#define BSP_USART_GPIO GPIOB
#define BSP_USART_GPIO_CLK RCC_APB2_PERIPH_GPIOB
#define BSP_USART_TX_PIN GPIO_PIN_6
#define BSP_USART_RX_PIN GPIO_PIN_7
#define BSP_USART_TX_GPIO_AF GPIO_AF4_USART1
#define BSP_USART_RX_GPIO_AF GPIO_AF4_USART1
#define BSP_USART_BAUDRATE 115200
/* USART1 TX DMA channel (remap required on this chip) */
#define BSP_USART_TX_DMA_CH DMA_CH1
#define BSP_USART_TX_DMA_TC DMA_FLAG_TC1
#define BSP_USART_TX_DMA_REMAP DMA_REMAP_USART1_TX
#define BSP_USART_DAT_ADDR (USART1_BASE + 0x04)
/* Pass to bsp_usart_read_byte() to block without timeout */
#define BSP_USART_WAIT_FOREVER 0xFFFFFFFFu
void bsp_usart_init(void);
/* Re-apply the baud rate at runtime (keeps RX interrupt/DMA setup).
* Used by the "uartrst" CLI command after a baud parameter change. */
void bsp_usart_set_baud(uint32_t baud);
/* TX: send a buffer through DMA (polled completion) */
void bsp_usart_write_dma(const uint8_t* data, uint16_t len);
/* RX: fetch one byte received by the RXDNE interrupt; returns 1 on success */
int bsp_usart_read_byte(uint8_t* ch, uint32_t timeout_ms);
/* RX: inject bytes from task context into the same RX queue (BLE downlink
* feeds the CLI through this). Bytes are dropped when the queue is full. */
void bsp_usart_rx_inject(const uint8_t* data, uint16_t len);
/* TX mutex so that DMA bursts and polling printf lines do not interleave.
* No-ops while the scheduler is not running. */
void bsp_usart_tx_lock(void);
void bsp_usart_tx_unlock(void);
/* Called from USART1_IRQHandler (see n32wb03x_it.c) */
void bsp_usart_rx_isr_handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __BSP_USART_H__ */