|
|
@@ -375,7 +375,19 @@ static rt_size_t stm32_transmit(struct rt_serial_device *serial,
|
|
|
|
|
|
if (uart->uart_dma_flag & RT_DEVICE_FLAG_DMA_TX)
|
|
|
{
|
|
|
- HAL_UART_Transmit_DMA(&uart->handle, buf, size);
|
|
|
+ if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) != HAL_OK)
|
|
|
+ {
|
|
|
+ /* 启动失败(典型为 gState 卡在上次传输的 BUSY_TX): 中止残留传输后重试一次 */
|
|
|
+ HAL_UART_AbortTransmit(&uart->handle);
|
|
|
+ if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) != HAL_OK)
|
|
|
+ {
|
|
|
+ /* 仍失败: 主动上报 TX_DMADONE, 防止 serial_v2 框架永久阻塞在 tx_cpt 上
|
|
|
+ * (阻塞期间持有应用层互斥锁, 会导致该串口发送整体瘫痪) */
|
|
|
+ LOG_E("%s tx dma start failed, drop %d bytes", uart->config->name, size);
|
|
|
+ rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DMADONE);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
@@ -395,38 +407,24 @@ static void dma_recv_isr(struct rt_serial_device *serial, rt_uint8_t isr_flag)
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
- recv_len = 0;
|
|
|
counter = __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
|
|
|
|
|
|
- switch (isr_flag)
|
|
|
- {
|
|
|
- case UART_RX_DMA_IT_IDLE_FLAG:
|
|
|
- if (counter <= uart->dma_rx.remaining_cnt)
|
|
|
- recv_len = uart->dma_rx.remaining_cnt - counter;
|
|
|
- else
|
|
|
- recv_len = serial->config.rx_bufsz + uart->dma_rx.remaining_cnt - counter;
|
|
|
- break;
|
|
|
+ /* 统一按环形距离计算新增数据量: (bufsz + 上次计数 - 当前计数) % bufsz
|
|
|
+ * 修复原 TC(回绕) 分支的竞态: ISR 被延迟(如片内 flash 擦写长时间关中断)时
|
|
|
+ * counter 已回绕越过 remaining_cnt, 原判断导致整段数据丢失且索引永久错位/多报重复 */
|
|
|
+ recv_len = (serial->config.rx_bufsz + uart->dma_rx.remaining_cnt - counter)
|
|
|
+ % serial->config.rx_bufsz;
|
|
|
|
|
|
- case UART_RX_DMA_IT_HT_FLAG:
|
|
|
- if (counter < uart->dma_rx.remaining_cnt)
|
|
|
- recv_len = uart->dma_rx.remaining_cnt - counter;
|
|
|
- break;
|
|
|
-
|
|
|
- case UART_RX_DMA_IT_TC_FLAG:
|
|
|
- if(counter >= uart->dma_rx.remaining_cnt)
|
|
|
- recv_len = serial->config.rx_bufsz + uart->dma_rx.remaining_cnt - counter;
|
|
|
-
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
+ /* 无论 recv_len 是否为 0 都必须同步计数, 否则后续事件全部错算 */
|
|
|
+ uart->dma_rx.remaining_cnt = counter;
|
|
|
|
|
|
if (recv_len)
|
|
|
{
|
|
|
- uart->dma_rx.remaining_cnt = counter;
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
|
|
|
}
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
+ UNUSED(isr_flag);
|
|
|
}
|
|
|
#endif /* RT_SERIAL_USING_DMA */
|
|
|
|
|
|
@@ -530,18 +528,8 @@ static void uart_isr(struct rt_serial_device *serial)
|
|
|
{
|
|
|
UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_CTS);
|
|
|
}
|
|
|
- if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
|
|
|
- {
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
|
|
|
- }
|
|
|
- if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
|
|
|
- {
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
- }
|
|
|
- if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
|
|
|
- {
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_RXNE);
|
|
|
- }
|
|
|
+ /* 注意: 不在此清 TXE/TC/RXNE 正常收发标志 —— 极端时序下误清 TC 会导致
|
|
|
+ * DMA 发送完成回调丢失, 发送线程永久阻塞; RXNE 由 DMA/正常分支处理 */
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1087,8 +1075,29 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|
|
{
|
|
|
RT_ASSERT(huart != NULL);
|
|
|
struct stm32_uart *uart = (struct stm32_uart *)huart;
|
|
|
- LOG_D("%s: %s %d\n", __FUNCTION__, uart->config->name, huart->ErrorCode);
|
|
|
- UNUSED(uart);
|
|
|
+ LOG_E("%s uart error: 0x%x", uart->config->name, huart->ErrorCode);
|
|
|
+
|
|
|
+ /* 复位 HAL 状态机: 否则 gState 卡在 BUSY 会导致后续发送全部失败且无任何日志 */
|
|
|
+ __HAL_UNLOCK(huart);
|
|
|
+ huart->gState = HAL_UART_STATE_READY;
|
|
|
+ huart->RxState = HAL_UART_STATE_READY;
|
|
|
+ huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
|
+
|
|
|
+#ifdef RT_SERIAL_USING_DMA
|
|
|
+ /* RX DMA 为循环模式常开: 若被错误中止则重启接收, 保证接收链路自愈 */
|
|
|
+ if (uart->uart_dma_flag & RT_DEVICE_FLAG_DMA_RX)
|
|
|
+ {
|
|
|
+ if (HAL_DMA_GetState(&uart->dma_rx.handle) != HAL_DMA_STATE_BUSY)
|
|
|
+ {
|
|
|
+ struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)uart->serial.serial_rx;
|
|
|
+ if (rx_fifo != RT_NULL &&
|
|
|
+ HAL_UART_Receive_DMA(huart, rx_fifo->buffer, uart->serial.config.rx_bufsz) == HAL_OK)
|
|
|
+ {
|
|
|
+ uart->dma_rx.remaining_cnt = uart->serial.config.rx_bufsz;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
/**
|