#ifndef DEVICE_USART0_H #define DEVICE_USART0_H #include #include /** * @brief USART0 串口模块。 * @note 负责 GPIO 初始化、外设配置、TX 环形缓冲与中断驱动发送。 */ class Usart0 { public: /** * @brief 配置 USART0 的 GPIO 引脚。 * @param[in] 无。 * @param[out] 无。 * @retval 无返回值。 * @note 当前配置为:PA9=TX(AF_PP)、PA10=RX(IN_FLOATING)。 */ static void configurePins(); /** * @brief 初始化 USART0 外设与 NVIC。 * @param[in] 无。 * @param[out] 无。 * @retval 无返回值。 * @note 使用 8N1 格式,收发使能,同步配置 NVIC 以便 TXE 中断驱动发送。 */ static void init(); /** * @brief 将字符写入 USART0 发送环形缓冲。 * @param[in] ch 待发送字符。 * @param[in] f 标准流指针,实际未使用。 * @param[out] 无。 * @retval int 返回已写入缓冲的字符值。 * @note 新字符写入后自动启动 TXE 中断来推动实际硬件发送。 */ static int putChar(int ch, FILE *f); /** * @brief USART0 中断处理入口。 * @param[in] 无。 * @param[out] 无。 * @retval 无返回值。 * @note 在 TXE 产生时从环形缓冲取下一字节发送;缓冲空后关闭 TXE 中断。 */ static void onInterrupt(); private: /** * @brief 获取环形缓冲下一索引。 * @param[in] idx 当前索引。 * @param[out] 无。 * @retval uint16_t 下一索引值。 */ static uint16_t nextIndex(uint16_t idx); /** * @brief 判断环形发送缓冲是否已满。 * @param[in] 无。 * @param[out] 无。 * @retval bool true 表示已满,false 表示可继续写入。 */ static bool isBufferFull(); /** * @brief 判断环形发送缓冲是否为空。 * @param[in] 无。 * @param[out] 无。 * @retval bool true 表示空,false 表示仍有待发送数据。 */ static bool isBufferEmpty(); static bool putCharNonBlocking(int ch); static constexpr uint16_t kTxBufSize = 1024U; static volatile uint32_t s_dropCount; static uint8_t s_txBuf[kTxBufSize]; static volatile uint16_t s_txHead; static volatile uint16_t s_txTail; }; #endif /* DEVICE_USART0_H */