Loading...
 

System Workbench for STM32


STM32F4 USART DMA RECEIVE

Hello everyone.

I’m just starting stm32f4. I’m try to use USART DMA, with Transmit it’s ok. But with Receive, when I send characters from Terminal to Board first, for example ‘aaa’, it’s ok. But the second characters I send, for example ‘bbb’, so the characters result is ‘aaabbb’ but I want the characters just ‘bbb’. I’m try to clear the buffer but it’s doesn’t work.
In case of Receive, I’m using flag IDLE to nodify that the characters was done.
I think the best way is return the address of memory address from current address to start address, but I’m not sure how to return it.

Here is my code.

Pleas help me. Thank you so much.

int main(void)

{

USART_Config();


if (USART_GetFlagStatus(USART3, USART_FLAG_IDLE)==RESET)

{

/* clear buffer */

for (i=0; iAHB1ENR = RCC_AHB1ENR_GPIOCEN;



RCC->APB1ENR = RCC_APB1ENR_USART3EN;



RCC->AHB1ENR = RCC_AHB1ENR_DMA1EN;



/* USART3 GPIO configuration -------------------*/

/* Connect USART pins to AF7 */

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);



/* Configure USART Tx and Rx as alternate function push-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 = GPIO_Pin_10;

GPIO_Init(GPIOC, &GPIO_InitStructure);



GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_Init(GPIOC, &GPIO_InitStructure);



USART_InitStructure.USART_BaudRate = 56000;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

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(USART3, &USART_InitStructure);



/* Configure DMA Initialization Structure */

DMA_InitStructure.DMA_BufferSize = 100;

DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;

DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;

DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;


DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(USART3->DR)) ;

DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_Priority = DMA_Priority_High;

/* Configure TX DMA */

DMA_InitStructure.DMA_Channel = DMA_Channel_4 ;

DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;

DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aTxBuffer ;

DMA_Init(DMA1_Stream4,&DMA_InitStructure);

/* Configure RX DMA */

DMA_InitStructure.DMA_Channel = DMA_Channel_4 ;

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;

DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)&(aRxBuffer);

DMA_Init(DMA1_Stream1,&DMA_InitStructure);

}