官方给出的下面drv_usart.c驱动代码中函数stm32_dma_config()的L0系列的宏定义预编译SOC_SERIES_STM32L0中是没有DMA_Handle->Init.Request参数的,但是实际上L0系列的DMA传输需要配置这个值,否则会造成数据接收不到的问题
#if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32L0)
DMA_Handle->Instance = dma_config->Instance;
#elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
DMA_Handle->Instance = dma_config->Instance;
DMA_Handle->Init.Channel = dma_config->channel;
#elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
DMA_Handle->Instance = dma_config->Instance;
DMA_Handle->Init.Request = dma_config->request;
#endif
需要改成如下,将SOC_SERIES_STM32L0宏定义单独作为一项,并添加DMA_Handle->Init.Request = dma_config->request;代码段
#if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0)
DMA_Handle->Instance = dma_config->Instance;
#elif defined(SOC_SERIES_STM32L0)
DMA_Handle->Instance = dma_config->Instance;
DMA_Handle->Init.Request = dma_config->request;
#elif defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
DMA_Handle->Instance = dma_config->Instance;
DMA_Handle->Init.Channel = dma_config->channel;
#elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0)
DMA_Handle->Instance = dma_config->Instance;
DMA_Handle->Init.Request = dma_config->request;
#endif
为了配合上述代码,还需要在dma_config添加,同理,如果有其它通道的串口DMA,也需要做此处理
#define UART1_RX_DMA_REQUEST DMA_REQUEST_3
#if defined(BSP_UART1_RX_USING_DMA) && !defined(UART1_RX_DMA_INSTANCE)
#define UART1_DMA_RX_IRQHandler DMA1_Channel4_5_6_7_IRQHandler
#define UART1_RX_DMA_RCC RCC_AHBENR_DMA1EN
#define UART1_RX_DMA_INSTANCE DMA1_Channel5
#define UART1_RX_DMA_REQUEST DMA_REQUEST_3
#define UART1_RX_DMA_IRQ DMA1_Channel4_5_6_7_IRQn
#endif
另外,在uart_config.h文件中的串口DMA结构体初始化定义中,也需要添加request字段
#if defined(BSP_UART1_RX_USING_DMA)
#ifndef UART1_DMA_RX_CONFIG
#define UART1_DMA_RX_CONFIG \
{ \
.Instance = UART1_RX_DMA_INSTANCE, \
.dma_rcc = UART1_RX_DMA_RCC, \
.dma_irq = UART1_RX_DMA_IRQ, \
.request = UART1_RX_DMA_REQUEST, \
}
#endif /* UART1_DMA_RX_CONFIG */
#endif /* BSP_UART1_RX_USING_DMA */
这里也吐槽一下rt-thread中这部分代码的作者,既然没有验证过就别轻易放上去,这是个很明显的bug了。