From bdcaf2ad5633b4bb4231d14189c24a60cc40c0b7 Mon Sep 17 00:00:00 2001 From: wdfk-prog <1425075683@qq.com> Date: Mon, 11 May 2026 10:44:47 +0800 Subject: [PATCH] fix[STM32][SPI]: ignore benign Tx-only OVR after async completion --- .../libraries/HAL_Drivers/drivers/drv_spi.c | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c index 18fad23e57..a38626f186 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c @@ -485,7 +485,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m #endif /* BSP_SPI_USING_DMA */ #ifdef BSP_SPI_USING_INT - rt_bool_t int_eligible = (send_length >= BSP_SPI_INT_TRANS_MIN_LEN); + rt_bool_t int_eligible = (send_length >= BSP_SPI_INT_TRANS_MIN_LEN); #if defined(BSP_SPI_TX_USING_INT) rt_bool_t use_tx_int = int_eligible && spi_drv->spi_dma_flag & RT_DEVICE_FLAG_INT_TX; #else @@ -602,10 +602,29 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m if (spi_handle->ErrorCode != HAL_SPI_ERROR_NONE) { - state = HAL_ERROR; - need_abort = RT_TRUE; - LOG_E("SPI async transfer failed, error code: 0x%08x", spi_handle->ErrorCode); - goto transfer_cleanup; + rt_uint32_t err = spi_handle->ErrorCode; + rt_bool_t tx_only = (message->send_buf != RT_NULL) && (message->recv_buf == RT_NULL); + rt_bool_t ovr_only = ((err & HAL_SPI_ERROR_OVR) != 0U) && ((err & ~(HAL_SPI_ERROR_OVR | HAL_SPI_ERROR_ABORT)) == 0U); + + /** + * Ignore the expected OVR from a completed Tx-only transfer. + * + * In 2-line SPI mode, Tx-only transfer still receives data from + * MISO. Since no RX buffer is provided, the received data is not + * read and HAL may latch OVR after async Tx completion. + */ + if (tx_only && ovr_only && (spi_handle->Init.Direction == SPI_DIRECTION_2LINES) && (spi_handle->TxXferCount == 0U)) + { + __HAL_SPI_CLEAR_OVRFLAG(spi_handle); + spi_handle->ErrorCode = HAL_SPI_ERROR_NONE; + } + else + { + state = HAL_ERROR; + need_abort = RT_TRUE; + LOG_E("SPI async transfer failed, error code: 0x%08x", spi_handle->ErrorCode); + goto transfer_cleanup; + } } } #endif /* BSP_SPI_USING_IRQ */