Skip to main content
Hugues Libotte
Associate II
September 7, 2023
Question

First byte of USART data received through DMA is not the right one

  • September 7, 2023
  • 5 replies
  • 6310 views

Hello.

I am using the STM32F030 for a sensor with a RS485 interface.

In my tests, I am sending a 8-bits message (constant value) to the STM32 in a loop. When I look at the data received, the last byte is at the first place, always. There is never any other or additional shift.

I can understand it is byte remaining from the previous comm and missed out from the previous reading but why is it so stable (always one byte and always the correct one).

Any idea about the reason of this ? And more usefull, do you have any method to clear the DMA buffer before starting reading ? Would you recommand the use of a circular buffer for the DMA ?

On one side (transmitter)

message_TX[0] = 0x02; ...; message_TX[6]= 0xDE ; message_TX[7] = 0xAC;

HAL_UART_Transmit_DMA(&huart1, (uint8_t*)message_TX, 8);

On the other side (receiver)

HAL_UART_Receive_DMA(&huart1, (uint8_t *)message_RX , 8);

And I get the following data in message_RX[0] = 0xAC (always) and message_RX[7] =0xDE (always)

Any suggestion for me ?

 

This topic has been closed for replies.

5 replies

Hugues Libotte
Associate II
September 7, 2023

In addition... if I read 9 bytes, I was expecting a progressive shift in the position of this first byte but it is not the case. I read the expected sequence from byte 1 to 8 and byte 0 is still containing the value from the previous comm (as I understand it)...

Issamos
Lead III
September 7, 2023

Hello @Hugues Libotte 

Maybe one of those 3 errata is the source of your problem.

Screenshot_2023-09-07-14-40-23-94_e2d5b3f32b79de1d45acd1fad96fbb0f.jpg

Screenshot_2023-09-07-14-39-30-13_e2d5b3f32b79de1d45acd1fad96fbb0f.jpg

Screenshot_2023-09-07-14-40-52-37_e2d5b3f32b79de1d45acd1fad96fbb0f.jpg

if not, i Suggest you to do the send all the 8 bytes byte by byte using a for loop that send the byte 1 juste after receiving an acknowledge that the byte 0 was well received...

Best regards.

II

Hugues Libotte
Associate II
September 7, 2023

Hello. Thanks !

Maybe  the third one but as far as I read, it seems the HAL_UART_TRANSMIT_DMA and HAL_UART_RECEIVE_DMA deals with uint8 and uint16. But what to set up in the CubeMX config panel.

Issamos
Lead III
September 7, 2023

You have to do exactly as sayed in the errata.

Best regards.

II

Tesla DeLorean
Guru
September 7, 2023

I wouldn't receive multiple bytes in this way, real difficult to re-sync. Use a protocol with patterns that allow for stream to be resynced, and integrity checked.

RS485, where receive and transmit connect together? In a loop-back or one wire mode sense?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Hugues Libotte
Associate II
September 7, 2023

Yes, it is for RS485 but in full duplex. I agree about the pattern but, clearing the UART register  content would be easier. I have several 100ms - even more-  between two communications so no hurry to take care of this.

waclawek.jan
Super User
September 8, 2023

Give us more context. Does the sensor transmit data all the time, or only after the STM32 asks for it? Are the DMA set as circular?

At any case, you should do what Clive recommends. DMA is not that useful for UART, and just hanging on some rx-complete callback expecting flawless data to appear automagically in a buffer is not sufficient.

JW

Hugues Libotte
Associate II
September 12, 2023

Hi,

The sensor is a RS485 slave so it answers to the request of my uC. This request occurs every 500 ms (so really not in a hurry). I tested both DMA in circular and not : same issue.

I can agree that DMA is not so mandatory here... but this issue is strange and I would like to get it operationnal for a future use in another project where we would really need this.

Bye

H

 

waclawek.jan
Super User
September 12, 2023

Are the transmitted data echoed? What happens if you make a large Rx buffer (at least sum of both Tx and Rx message) and start Rx DMA *before* Tx?

JW