# UART：USART1 上发生溢出错误，数据丢失

- **ID:** `embedded/uart-overrun-error`
- **领域:** embedded
- **类别:** runtime_error
- **错误码:** `USART_SR_ORE`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

UART 接收缓冲区溢出，因为 CPU 未能在下一个字节到达前读取数据，通常由于高中断延迟或 DMA 缓冲不足。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| STM32Cube_FW_F4 v1.27.0 | active | — | — |
| FreeRTOS v10.4.6 | active | — | — |
| GCC ARM Embedded 12.2.1 | active | — | — |

## 解决方案

1. ```
   启用 UART DMA 进行接收并使用环形缓冲区：配置 DMA 为外设到内存传输，设置缓冲区大小为 256 字节，并使用半传输/全传输中断处理数据。
   ```
2. ```
   增加 UART FIFO 阈值（如果硬件支持）：设置 USART_CR3_DMAT 启用 DMA，或配置接收超时（RTO）定期刷新缓冲区。
   ```
3. ```
   降低其他外设的中断优先级，确保 UART ISR 及时执行：使用 NVIC_SetPriority(USART1_IRQn, 0) 设置最高优先级。
   ```

## 无效尝试

- **Increase UART baud rate to reduce data arrival rate** — Higher baud rate increases data rate, worsening the overrun condition. (95% 失败率)
- **Disable UART interrupts and poll manually** — Polling increases CPU load and may miss data entirely, leading to more overruns. (80% 失败率)
- **Add a delay in the UART ISR** — Delays in ISR increase interrupt latency, causing more overruns. (90% 失败率)
