cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART DMA RECEIVE

phuocnguyenhuu96
Associate III

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.

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; i<100; i++)

{

aRxBuffer[i]=0;

}

USART_Cmd(USART3, ENABLE);

  DMA_Cmd(DMA1_Stream1, ENABLE); 

  USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);

}

else if (USART_GetFlagStatus(USART3, USART_FLAG_IDLE)==SET)

{

 USART_Cmd(USART3, DISABLE);

  DMA_Cmd(DMA1_Stream1, DISABLE); 

USART_DMACmd(USART3, USART_DMAReq_Rx, DISABLE);

}

 

 while (1)

 { 

 }

}

static void USART_Config(void)

{

 USART_InitTypeDef USART_InitStructure;

 GPIO_InitTypeDef GPIO_InitStructure;

RCC->AHB1ENR = 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);

}

3 REPLIES 3
phuocnguyenhuu96
Associate III

I have solved the problem.

How?

phuocnguyenhuu96
Associate III

I use the interrupt IDLE to disable DMA and delete buffer when transfer again.