mothercup/mcm-ddc-ble/inc/bsp_usart.h
evan.liu 8a7cebcf77 V1.00.26: UART baud 115200 -> 460800
- firmware BSP_USART_BAUDRATE 460800 (divider error +0.03%); uartrst/
  uartinfo follow the macro
- RX DMA ring 2KB -> 3KB: at 460800 2KB only covers 44ms, less than the
  ~45ms flash-erase interrupt-off window; stack top now 0x2000BDE8
  (guard warns <1KB from the 0x2000C000 cliff, by design)
- tools: ble_ota_update.py UartTransport and uart_cap.py default baud
- Verified: stream OTA 55832B in 2.6s @23kB/s (one lost frame recovered
  by go-back-N), PASS after reboot; text CLI fine at 460800
- docs: ble_protocol.md, design spec, AGENTS.md, dev log section 48
2026-09-05 10:01:23 +08:00

70 lines
2.4 KiB
C

/**
* @file bsp_usart.h
* @brief USART1 support: printf retarget (polling TX), DMA TX, RX DMA
* circular ring + IDLE interrupt. TX=PB6, RX=PB7, 460800 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 460800
/* 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
/* USART1 RX DMA channel: circular mode into a 2KB ring, drained by
* bsp_usart_read_byte(). DMA keeps sampling while interrupts are off
* (flash erase windows during UART OTA streaming). */
#define BSP_USART_RX_DMA_CH DMA_CH2
#define BSP_USART_RX_DMA_REMAP DMA_REMAP_USART1_RX
#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 received byte. Source order: the inject queue (BLE
* downlink feeding the CLI) first, then the DMA RX ring. Returns 1 on
* success, 0 on timeout. */
int bsp_usart_read_byte(uint8_t* ch, uint32_t timeout_ms);
/* RX: inject bytes from task context (BLE downlink feeds the CLI through
* this). Bytes are dropped when the inject 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__ */