使用STM32F205的串口工作在DMA模式时接受异常怎么办?
扫描二维码
随时随地手机看文章
1 前言
客户反馈在使用STM32F205的串口工作在DMA模式时,有时能够接收数据,有时完全没有数据,但如果换成中断模式来接收又能100%正常收到数据。
2 复现现象2.1 问题背景
与客户沟通,客户使用的是STM32F2标准库V1.1.0,串口波特率为1.408Mbps,不经过串口RS232,直接连接主CPU和从MCU(STM32F205)的串口发送和接收引脚,如下图所示:
2.2 尝试重现问题
由于客户使用的是主从架构,实验采用两块STM3220G-EVAL评估板来重现现象。一块用来不间断发送串口数据,另一块采用串口DMA进行接收,直接通过杜邦线连接串口PIN脚并共地,不使用评估板上的RS232收发器。接收端使用STM32F2xx_StdPeriph_Examples USARTUSART_TwoBoards的示例代码。代码片段如下:
intmain(void){...USART_Config();...while(1){/*ClearBuffers*/Fill_Buffer(RxBuffer,TXBUFFERSIZE);Fill_Buffer(CmdBuffer,2);DMA_DeInit(USARTx_RX_DMA_STREAM);DMA_InitStructure.DMA_Channel=USARTx_RX_DMA_CHANNEL;DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralToMemory;/*************USARTwillreceivethethetransactiondata****************//*Transactiondata(lengthdefinedbyCmdBuffer[1]variable)*/DMA_InitStructure.DMA_Memory0BaseAddr=(uint32_t)RxBuffer;DMA_InitStructure.DMA_BufferSize=10;//(uint16_t)CmdBuffer[1];DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;//DMA_Mode_Circular;DMA_Init(USARTx_RX_DMA_STREAM,&DMA_InitStructure);NVIC_InitStructure.NVIC_IRQChannel=DMA1_Stream1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);/*EnableDMAStreamTransferCompleteinterrupt*/DMA_ITConfig(USARTx_RX_DMA_STREAM,DMA_IT_TE"DMA_IT_DME|DMA_IT_FE,ENABLE);/*EnabletheDMAStream*/DMA_Cmd(USARTx_RX_DMA_STREAM,ENABLE);/*EnabletheUSARTRxDMArequests*/USART_DMACmd(USARTx,USART_DMAReq_Rx,ENABLE);//USART_Cmd(USARTx,ENABLE);//while(SET==USART_GetFlagStatus(USARTx,USART_FLAG_ORE))//{//Tmp=USART_ReceiveData(USARTx);//}while((DMA_GetFlagStatus(USARTx_RX_DMA_STREAM,USARTx_RX_DMA_FLAG_TCIF)==RESET){}/*ClearallDMAStreamsflags*/DMA_ClearFlag(USARTx_RX_DMA_STREAM,USARTx_RX_DMA_FLAG_HTIF|USARTx_RX_DMA_FLAG_TCIF);/*DisabletheDMAStream*/DMA_Cmd(USARTx_RX_DMA_STREAM,DISABLE);/*DisabletheUSARTRxDMArequests*/USART_DMACmd(USARTx,USART_DMAReq_Rx,DISABLE);//handletheRxBufferdata...//...}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
USART_Config()函数如下:
staticvoidUSART_Config(void){USART_InitTypeDefUSART_InitStructure;GPIO_InitTypeDefGPIO_InitStructure;/*PeripheralClockEnable-------------------------------------------------*//*EnableGPIOclock*/RCC_AHB1PeriphClockCmd(USARTx_TX_GPIO_CLK|USARTx_RX_GPIO_CLK,ENABLE);/*EnableUSARTclock*/USARTx_CLK_INIT(USARTx_CLK,ENABLE);/*EnabletheDMAclock*/RCC_AHB1PeriphClockCmd(USARTx_DMAx_CLK,ENABLE);/*USARTxGPIOconfiguration-----------------------------------------------*//*ConnectUSARTpinstoAF7*/GPIO_PinAFConfig(USARTx_TX_GPIO_PORT,USARTx_TX_SOURCE,USARTx_TX_AF);GPIO_PinAFConfig(USARTx_RX_GPIO_PORT,USARTx_RX_SOURCE,USARTx_RX_AF);/*ConfigureUSARTTxandRxasalternatefunctionpush-pull*/GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;GPIO_InitStructure.GPIO_Pin=USARTx_TX_PIN;GPIO_Init(USARTx_TX_GPIO_PORT,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=USARTx_RX_PIN;GPIO_Init(USARTx_RX_GPIO_PORT,&GPIO_InitStructure);/*USARTxconfiguration----------------------------------------------------*//*EnabletheUSARTOverSamplingby8*/USART_OverSampling8Cmd(USARTx,ENABLE);USART_InitStructure.USART_BaudRate=1408000;//3750000;USART_InitStructure.USART_WordLength=USART_WordLength_8b;USART_InitStructure.USART_StopBits=USART_StopBits_1;/*WhenusingParitythewordlengthmustbeconfiguredto9bits*/USART_InitStructure.USART_Parity=USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;USART_Init(USARTx,&USART_InitStructure);/*ConfigureDMAcontrollertomanageUSARTTXandRXDMArequest----------*/DMA_InitStructure.DMA_PeripheralBaseAddr=USARTx_DR_ADDRESS;DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;DMA_InitStructure.DMA_Priority=DMA_Priority_VeryHigh;DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Enable;DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;/*HereonlytheunchangedparametersoftheDMAinitializationstructureareconfigured.Duringtheprogramoperation,theDMAwillbeconfiguredwithdifferentparametersaccordingtotheoperationphase*//*EnableUSART*/USART_Cmd(USARTx,ENABLE);}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364651234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
按如上代码,有如下现象:
1. 代码不做修改,若先启动接收端MCU再启动发送端MCU,接收端MCU的串口能正常接收。
2. 代码不做修改,若先启动发送端MCU再启动接收端MCU,接收端MCU的串口100%接收异常。
3. 修改发送端代码,改为发送端MCU串口每1秒间隔发送一次,则无论启动顺序如何,接收端MCU的串口都能正常。
3 程序分析
由上述代码可知,程序是先在USART_Config()函数函数内初始化串口并使能,然后再在接下来的main函数的while循环内初始化DMA并使能。这个是标准库内附带的示例代码,咋一看没什么问题,但仔细一想,针对用户的使用场景,这里就会产生一个问题:由于用户的主CPU有可能在从MCU启动之前就已经有可能启动,那么在这种情况下,在初始化完串口并使能后,到DMA使能之前这段时间内,若主CPU向从MCU发送串口数据,从MCU是否能正确接收?
从上述测试