Loading...
 
Skip to main content

System Workbench for STM32


Receiving Characters From UART Corrupts Memory

I have an alternate solution for your inputprocess, which may clear up the problem and not require the UART to receive one character at a time.

If you set up the UART with a DMA channel on the RX side in circular buffer mode it will keep recieveing characters regardless how you check and process them. You will then need to implement your own routines to get a character, and perhaps to detect when a character is present (kbhit()?) by looking at the DMA count values in the DMA handle block to index into the receive buffer when characters are received. You need to keep your own index pointing to the next character to be recieved when you extract a character. Make sure you wrap your index the same as the DMA counter does.

By setting it up this way, the UART will recieve characters continuously, and as long as you take them out of the buffer before the DMA wraps around you will never have a problem.

I generally use a buffersize of 256 bytes but you can change this depending on your traffic.

Somewhere in the startup of your program, you need to do one call to the HAL routine to start a recieve DMA transfer, pointing to the buffer where you want the DMA to store data. It will then keep filling the circular buffer and you can take characters out, check for your terminating characters, etc and always be ready for the next message.