cancel
Showing results for 
Search instead for 
Did you mean: 

Configuration of DMA for USART-RX buffer

Greusard Alexandre
Associate II
Posted on August 14, 2017 at 17:23

Hey there!

I'm working on stm32f051 MCU for a little project and a I  have to receive and process data from its USART port 1.

My issu is that

I don't have any interuption from the DMA

, and if I check directly on the buffer selected for the memory transfert there is also nothing in it.

But

I do receive informations

when I work with the USART directly ( from the USART1_IRQHandler() )

And I make DMA working fine with ADC.

I'm completly lost and going mad about this for to much time now.

I'd be glad to have any answer, and please tell me if you need any informations mere!

Here is my code:

#define USART_RX             GPIO_Pin_10

#define USART_TX             GPIO_Pin_9

#define USART_source_RX         GPIO_PinSource10

#define USART_source_TX         GPIO_PinSource9

#define USART_PERIPH                    USART1

#define USART_BAUDRATE                  115200

#define MSG_SIZE_MAX 50

static char msg[MSG_SIZE_MAX];

#define DMA_RX_BUFFER_SIZE          4

static uint8_t DMA_RX_Buffer[DMA_RX_BUFFER_SIZE];

static uint32_t Send_Byte (uint8_t c)

{

    while (USART_GetFlagStatus(USART_PERIPH, USART_FLAG_TXE) == RESET);

    USART_SendData(USART_PERIPH, c);

  return 0;

}

void SendPacket(uint8_t *data, uint16_t length)

{

  uint16_t i;

  i = 0;

  while (i < length)

  {

    Send_Byte(data[i]);

    i++;

  }

}

LeGreuch

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on August 14, 2017 at 17:57

You enable the DMA1 clock someplace?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3
Greusard Alexandre
Associate II
Posted on August 14, 2017 at 17:26

Sory didn't finish my job, here is the rest of my code:

void Setup_UART_DMA()

{

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

        

    GPIO_InitTypeDef GPIO_InitStructure;

    USART_InitTypeDef USART_InitStructure;

    GPIO_PinAFConfig(GPIOA, USART_source_TX, GPIO_AF_1);

    GPIO_PinAFConfig(GPIOA, USART_source_RX, GPIO_AF_1);

    //Configure USART1 pins: Rx (PA2) and Tx (PA3)

    GPIO_InitStructure.GPIO_Pin = USART_RX | USART_TX;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

        

        

    //Configure USART2 setting: ----------------------------

    USART_InitStructure.USART_BaudRate = USART_BAUDRATE;

    USART_InitStructure.USART_WordLength = USART_WordLength_9b;//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(USART_PERIPH, &USART_InitStructure);

    

        // Enable RXNE interrupt

        //USART_ITConfig(USART_PERIPH, USART_IT_RXNE, ENABLE);

        

        // Enable USART1 global interrupt

        //NVIC_EnableIRQ(USART1_IRQn);

        

        DMA_DeInit(DMA1_Channel3);

          /* Configure DMA for USART RX, DMA1, Stream5, Channel4 */

        DMA_InitTypeDef DMA_InitStruct;

        DMA_StructInit(&DMA_InitStruct);

        DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART1->RDR;

        DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)DMA_RX_Buffer;

        DMA_InitStruct.DMA_BufferSize = DMA_RX_BUFFER_SIZE;

        DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; // ou DMA_DIR_PeripheralDST ?

        DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

        DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//DMA_PeripheralDataSize_Byte;

        DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

        DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

        DMA_Init(DMA1_Channel3, &DMA_InitStruct);

        

        USART_DMACmd(USART_PERIPH,USART_DMAReq_Rx,ENABLE);

        

        USART_Cmd(USART_PERIPH,ENABLE);

        

        DMA_Cmd(DMA1_Channel3, ENABLE);

        

        DMA_ITConfig(DMA1_Channel3, DMA_IT_TC,ENABLE);

        

        /* Enable global interrupts for DMA stream */

        NVIC_InitTypeDef NVIC_InitStruct;

        NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel2_3_IRQn;

        NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

        NVIC_InitStruct.NVIC_IRQChannelPriority = 0;

        NVIC_Init(&NVIC_InitStruct);

        

        

        NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);

}

void DMA1_Channel2_3_IRQHandler(void)

{

    if(DMA_GetITStatus(DMA_ISR_TCIF3) != RESET)

    {

      while (USART_GetFlagStatus(USART_PERIPH, USART_FLAG_TXE) == RESET);

      sprintf(msg,'Transfer complete\r\n');

      SendPacket(msg, strlen(msg));

      DMA_ClearITPendingBit(DMA1_IT_GL3);

    }

    //while(USART_GetFlagStatus(USART_PERIPH, USART_FLAG_TXE) == RESET);  

}/**/
Posted on August 14, 2017 at 17:57

You enable the DMA1 clock someplace?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 16, 2017 at 10:11

Nice thank you Clive One!! Now it beginning to work

:)