0

I just started working with STM32F407GZ and I for the start I wanted to turn on/off LED's on-board. I am using CubeMX to generate initialization code. I can receive and transmit data but it doesn't gets correctly the data recived. Also, the leds don't turn on until I click the reset button. When I click the reset button the leds turn on but I stop receiving and transmiting data.

This is the code:

 MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART3_UART_Init();

  HAL_UART_Receive_DMA(&huart3,receive,20);
    
  while (1)
  {
        HAL_Delay(100);
        HAL_UART_Receive_DMA(&huart3,receive,20);
        
        HAL_UART_Transmit_DMA(&huart3,receive,20);
        
        HAL_Delay(100);
        HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOF,GPIO_PIN_6,GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOF,GPIO_PIN_7,GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_SET);
        
        HAL_Delay(500);
        
  }
salaotitu
  • 21
  • 1

2 Answers2

1

As 0___________ said, DMA is non blocking receive. It automatically reads data from the bus and move the data to the memory location you specified (which is receive in your case).
Here is the simple solution using blocking functions:

  HAL_UART_Receive(&huart3, receive, 20, 1000);
  HAL_UART_Transmit(&huart3, receive, 20, 1000);

HAL_UART_Receive() returns when either the 20 bytes of data is received or 1000 ms has been passed. HAL_UART_Transmit() returns when either the 20 bytes of data is transmitted or 1000 ms has been passed. These two functions make sure that the execution of receiving and transmitting follows the order of statements.

This isn't the thing that you are asking, but I think this might help you. The part of turning on led can be reduced to one line:

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_SET);

You can see the definition of GPIO_PIN_6 or other pins and you will get the idea.

Quence
  • 38
  • 6
0
        HAL_UART_Receive_DMA(&huart3,receive,20);
        
        HAL_UART_Transmit_DMA(&huart3,receive,20);

This is wrong. DMA functions are not blocking. They do not wait until the operation finishes. They return immediately. You instruct UART to receive 20 bytes and at the same moment, you want to send the data from the same buffer (and nothing has been received yet).

0___________
  • 60,014
  • 4
  • 34
  • 74