cancel
Showing results for 
Search instead for 
Did you mean: 

Unexpected data change in snprintf output with STM32H743 UART DMA

Umer454
Associate II

Hello,

I am using an STM32H743 microcontroller. UART8 is configured at a baud rate of 460800 with DMA enabled. The UART output is logged by a USB logger which creates a FAT32 text file.

The transmitted data contains mixed data types such as integers, characters, strings, floats, and doubles. To combine the data, I format it into a string using snprintf.

I am facing a strange issue:

When I format the string using a comma after the first integer, and the second integer variable is zero, the value recorded in the USB log becomes 8 instead of 0.

When I replace the comma with a semicolon, the value is recorded correctly as 0.

Additional observations:

This issue only occurs when the data is generated by the STM32.

If the same formatted data is sent from a computer or another hardware source, the issue does not occur.

If I log the STM32 UART output directly on a computer terminal, the data is correct.

The problem only appears when using the USB logger with STM32 UART DMA output.

Could you please advise what could cause this behavior? Could it be related to DMA operation, cache coherency, buffer handling, or string formatting on STM32H7?

Any guidance would be appreciated.

Regards,

9 REPLIES 9
TDK
Super User

Show the relevant code. Probably a code bug.

Be sure not to send variables in the local scope such as the snprintf buffer to a DMA function and let the scope be deleted (i.e. local variables are deleted when the function returns).

DMA can't change the value of memory. There is no mechanism for this. It's just sending what is there--issue is elsewhere.

It could be a cache issue, in which case it would be pretty easy to debug by disabling the cache. Are you managing cache correctly? Are buffers cache-aligned?

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Super User

@Umer454 wrote:

If I log the STM32 UART output directly on a computer terminal, the data is correct.

The problem only appears when using the USB logger with STM32 UART DMA output.


Sounds like there may be a fault with the "USB logger", then?

Can you give more details of that logger, and what exactly you mean by "directly on a computer terminal" ?

How to write your question to maximize your chances to find a solution

 

Do you have some independent way to verify what the stm32 is actually transmitting; eg, a logic analyser?

Have you checked the accuracy of the baud rate, and the integrity of the UART signal on the wires?

Does this only happen when the STM32 is using DMA?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Ozone
Principal III

While I'm not sure about your particular case, such problems are usually related to the locale settings in C.
The interpretation (scanf() and the like) of e.g. dates, numerical number or or monetary value depends on the country settings, which can be modified to any other (installed) locale.

> When I format the string using a comma after the first integer, and the second integer variable is zero, the value recorded in the USB log becomes 8 instead of 0.

I would try to check (output) the received string and the USB log for comparison.

> The UART output is logged by a USB logger which creates a FAT32 text file.

You didn't specifiythe device, so it's unknown if it can be reconfigured in this context.

However, you could try the setlocale() POSIX function (https://www.man7.org/linux/man-pages/man3/setlocale.3.html) to match the "expectations" of the USB logger.

 

Pavel A.
Super User

From all mentioned possibilities cache coherency looks likely. Do you enable the D cache on the memory touched by the DMA? Use the MPU? Tried to disable the cache?

 

 

Thank you for comments, I have shared my init code with you, can you verify please?

Hi, in your code there isn't anything that deals with MPU and cache flushing. So the questions remain.

In function Logger_PrepData(): the sprintf has a lot of arguments including float/double. This may be OK in a "normal" C app, but in a small embedded system with newlib-nano runtime library it lights red light to me. 

- Try to sniff the serial data between STM32 and the logger or connect to a PC and use some terminal. Will you see the 8's there (it is real or artifact of the logger)?

- Try to format the string with sequence of lower level functions (fcvt, gcvt ...) instead of the huge sprintf.

 

 

Hello,

I am sending you two code examples. Kindly review them and let me know which approach is better for the STM32H743.

I have successfully transmitted a 1 KB buffer using simple UART (polling-based), and the data is being recorded correctly. Now I want to transmit the same 1 KB buffer using DMA at 10Hz.

Please advise which implementation is more suitable and reliable for this purpose.

Best regards,
Umer Daraz

Pavel A.
Super User

When sending your original logs (with floats) to a PC with any terminal app and save the data to a file, do you still see  8 instead of 0, or other anomaly?

The code with DMA and cache looks OK, but it sends different data than in the original post.

 

 

 

Umer454
Associate II

If I send a constant string, the data is written correctly.
But when I send variable data, it becomes garbage.

myuart1_15_01_2026_1609.c this code is working fine and data logger works fine

not_working.c, when logging through this code. logged data garbage... why??

Thank you for your time and guidance.

Umer