1 前言
客戶反饋在使用STM32F205的串口工作在DMA模式時,有時能夠接收數(shù)據(jù),有時完全沒有數(shù)據(jù),但如果換成中斷模式來接收又能100%正常收到數(shù)據(jù)。
2 復(fù)現(xiàn)現(xiàn)象2.1 問題背景
與客戶溝通,客戶使用的是STM32F2標(biāo)準(zhǔn)庫V1.1.0,串口波特率為1.408Mbps,不經(jīng)過串口RS232,直接連接主CPU和從MCU(STM32F205)的串口發(fā)送和接收引腳,如下圖所示:
2.2 嘗試重現(xiàn)問題
由于客戶使用的是主從架構(gòu),實驗采用兩塊STM3220G-EVAL評估板來重現(xiàn)現(xiàn)象。一塊用來不間斷發(fā)送串口數(shù)據(jù),另一塊采用串口DMA進(jìn)行接收,直接通過杜邦線連接串口PIN腳并共地,不使用評估板上的RS232收發(fā)器。接收端使用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()函數(shù)如下:
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
按如上代碼,有如下現(xiàn)象:
1. 代碼不做修改,若先啟動接收端MCU再啟動發(fā)送端MCU,接收端MCU的串口能正常接收。
2. 代碼不做修改,若先啟動發(fā)送端MCU再啟動接收端MCU,接收端MCU的串口100%接收異常。
3. 修改發(fā)送端代碼,改為發(fā)送端MCU串口每1秒間隔發(fā)送一次,則無論啟動順序如何,接收端MCU的串口都能正常。
3 程序分析
由上述代碼可知,程序是先在USART_Config()函數(shù)函數(shù)內(nèi)初始化串口并使能,然后再在接下來的main函數(shù)的while循環(huán)內(nèi)初始化DMA并使能。這個是標(biāo)準(zhǔn)庫內(nèi)附帶的示例代碼,咋一看沒什么問題,但仔細(xì)一想,針對用戶的使用場景,這里就會產(chǎn)生一個問題:由于用戶的主CPU有可能在從MCU啟動之前就已經(jīng)有可能啟動,那么在這種情況下,在初始化完串口并使能后,到DMA使能之前這段時間內(nèi),若主CPU向從MCU發(fā)送串口數(shù)據(jù),從MCU是否能正確接收?
從上述測試