Using DMA to send data through UART

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Hello !

I wanted to ask 3 questions connected to tutorials I've watched :
Source :


- I was wondering when here the UART is sending data from TX to anywhere ;>
1700336262473.png

- Why UART need to be set in DMA mode or rather why there is this type of mode ? I know that data is sent from memory to UART TX register but why it needs a mode that DMA is sending it to him ? I know the DMA is set in the IOC/graphical interface, and there is a handler created hdma_usart2_tx. So why there is this mode for UART ?

-When is used HUART handler and HDMA_USART handler ?
 
Last edited:

MrChips

Joined Oct 2, 2009
30,467
You are missing the point of the video. First and foremost, this is a tutorial that introduces the following concepts:

  1. How to use STM32CubeIDE
  2. A simple example of how to read the ADC and send data to the UART
  3. A quick example of using IRQ
  4. How to setup and use Callbacks
  5. An introduction to DMA
  6. How to set up the ADC for DMA and achieve 5Msps sampling rate
  7. How to send data from ADC to UART using DMA
  8. Examples of using HAL functions

You do not have to use DMA with the UART. This is only a demonstration.
To repeat. This is a tutorial. At the very end of the tutorial he says that this is not a practical example.

His demonstration and delivery is very rapid. However, what he says and demonstrates is very in-depth and clear. You will need to view this video repeatedly in order to catch the tiny details that are important but which you may not capture on first or subsequent viewing.

The way to really benefit from this tutorial is to attempt a project to suit your needs. For example, digitize sound, store it, and play it back through a loudspeaker.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Hello !


You are missing the point of the video. First and foremost, this is a tutorial that introduces the following concepts:

  1. How to use STM32CubeIDE
  2. A simple example of how to read the ADC and send data to the UART
  3. A quick example of using IRQ
  4. How to setup and use Callbacks
  5. An introduction to DMA
  6. How to set up the ADC for DMA and achieve 5Msps sampling rate
  7. How to send data from ADC to UART using DMA
  8. Examples of using HAL functions

You do not have to use DMA with the UART. This is only a demonstration.
To repeat. This is a tutorial. At the very end of the tutorial he says that this is not a practical example.
Perhaps it is not practical, I've used ADC as well before in more simple example without DMA.

I was also learning how DMA works but I couldn't understand how it worked or rather how the commands worked. Because it's hard to understand the concept if I don't understand the code itself.

So I wondered how UART started transmiting. Usually for this I used HAL_UART_Transmit, so this command was sending data from Uart to other peripheral.
Although I am slowly rewatching the video and catching some stuff. But this video also made me look more into these UART functions or UART_DMA functions.

Like here for normal UART_Transmit :

1700355684415.png
So there is this WaitOnFlag so it waits for he flag to send another data as I understood and increments the data buffer and decrement the counter.

But in UART_Transmit_DMA I couldn't find those things.
1700356022598.png

There is no checking the flag of TX if it sent the data or not, rather I see this HAL_DMA_Start_IT that sends data from buffer to UART TX register but when UART sends data from TX register out ? Or the counter that counts down to 0.

I've also noticed the huart2.Instance->CR3 which I didn't understand. It enables the DMA transfer for transmit. What does it mean ?
 

geekoftheweek

Joined Oct 6, 2013
1,183
- Why UART need to be set in DMA mode or rather why there is this type of mode ?
In a nutshell the DMA controller handles the functions the processor normally would. Instead of the processor reading a byte from memory and writing it to the UART each time the DMA controller handles it instead. The DMA controller then alerts the processor when all bytes are transferred instead of each individual byte.
 

MrChips

Joined Oct 2, 2009
30,467
Don't use DMA with the UART. This is not a practical example.

If you want to experiment and learn how to use DMA, fill up a buffer with a sinewave signal (or even a saw-tooth wave). Now try to send this buffer continuously to a DAC using DMA.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Don't use DMA with the UART. This is not a practical example.

If you want to experiment and learn how to use DMA, fill up a buffer with a sinewave signal (or even a saw-tooth wave). Now try to send this buffer continuously to a DAC using DMA.
I want to understand what I've asked ;>
Why it is not practical ? How it handles the stuff etc.

I've understood the beggining of the video which the author used the ADC Hal commands and the UART
But the DMA not really.
 

MrChips

Joined Oct 2, 2009
30,467
You need to understand what is DMA.

DMA stands for Direct Memory Access.
In the context of the STM32 DMA, DMA is not restricted to memory, you can transfer data from peripheral to peripheral.

What do you need to perform DMA?

1. You need a source address or device.
2. You need a destination address or device.
3. You need to specify the number of data to be transferred.

In general, if you are transferring data to or from memory, you might want to increment the address after every data item is transferred. If it is a peripheral device, it is more likely that data is being transferred to or from a device register. In this case, the address does not change.

Once the necessary information is given, the DMA process is started. The process is stopped when all the data has been transferred.

If you ask for continuous DMA transfer, the process does not stop. It will start again from the first memory address (assuming it is to or from memory). This is known as a circular buffer.

You can also have dual buffers. You can have DMA transfer into one buffer while you process the data in another buffer.

DMA enables the computer system to move data around very rapidly without using CPU cycles.
DMA is essential for most common processes you encounter right now, video, music streaming, internet access, file access, etc.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
495
What do you need to perform DMA?

1. You need a source address or device.
2. You need a destination address or device.
3. You need to specify the number of data to be transferred.
Oki doki so in this case from the video it was :

1. Source was the buffer with some data (char message)
2. Destination is UART TX register
3. The number is specified by the length of that char message

Now there is a problem because TX register takes 8 bit of data (or so as I remember), but the message can be bigger, not UART has to send the data and send the info that it can take next data from DMA. How ?


Once the necessary information is given, the DMA process is started. The process is stopped when all the data has been transferred.

If you ask for continuous DMA transfer, the process does not stop. It will start again from the first memory address (assuming it is to or from memory). This is known as a circular buffer.
A yea there is this Normal mode and Circular mode. I assume that Circular mode is the continuous DMA.


You need to understand what is DMA.

DMA stands for Direct Memory Access.
In the context of the STM32 DMA, DMA is not restricted to memory, you can transfer data from peripheral to peripheral.

What do you need to perform DMA?

1. You need a source address or device.
2. You need a destination address or device.
3. You need to specify the number of data to be transferred.

In general, if you are transferring data to or from memory, you might want to increment the address after every data item is transferred. If it is a peripheral device, it is more likely that data is being transferred to or from a device register. In this case, the address does not change.

Once the necessary information is given, the DMA process is started. The process is stopped when all the data has been transferred.

If you ask for continuous DMA transfer, the process does not stop. It will start again from the first memory address (assuming it is to or from memory). This is known as a circular buffer.

You can also have dual buffers. You can have DMA transfer into one buffer while you process the data in another buffer.

DMA enables the computer system to move data around very rapidly without using CPU cycles.
DMA is essential for most common processes you encounter right now, video, music streaming, internet access, file access, etc.
I can see the pros of using DMA which is that we send data to DMA and instead of waiting till the data is sent to peripheral the DMA takes control of this so the CPU can do other operations.

Like taking data from ADC using DMA and store it into buffer, instead of CPU that would have to wait for the buffer to be fully stored it can now using the DMA do other stuff like turning the LED on (yea I know turning LED is not a big deal), or send different data.


It still didn't answer my question on how it works in code, how DMA works using the codes, how DMA know stuff that aren't in the code. How UART is sending data when in UART_Transmit_DMA it only sets the DMA to send data from memory to TX register and there is nothing about UART sending data or receving flag like TXE.

Understanding the overall concept is essential, understanding the code is also essential I think.
In which I couldn't understand the UART_Transmit_DMA. Because DMA was meant to send data from memory to UART Tx register but it also makes UART start and sends data. But I couldn't find where it starts in the code. Or those CR3 registers that turned on something.
 

MrChips

Joined Oct 2, 2009
30,467
Now there is a problem because TX register takes 8 bit of data (or so as I remember), but the message can be bigger, not UART has to send the data and send the info that it can take next data from DMA. How ?
Let us say there are 8000 bytes sitting in the buffer. UART TX buffer can only take one 8-bit byte at a time.
DMA hardware sends one byte to UART TX buffer. After the buffer is empty, DMA hardware sends the next byte, and so on, until all 8000 bytes have been sent. Then DMA hardware sets a flag to say, hey, I am done.

It still didn't answer my question on how it works in code, how DMA works using the codes, how DMA know stuff that aren't in the code. How UART is sending data when in UART_Transmit_DMA it only sets the DMA to send data from memory to TX register and there is nothing about UART sending data or receving flag like TXE.
DMA is performed by hardware. There is no code.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Let us say there are 8000 bytes sitting in the buffer. UART TX buffer can only take one 8-bit byte at a time.
DMA hardware sends one byte to UART TX buffer. After the buffer is empty, DMA hardware sends the next byte, and so on, until all 8000 bytes have been sent. Then DMA hardware sets a flag to say, hey, I am done.
So then the DMA know which flag from UART says it is empty, which is TXE, how does he know about this flag ?


DMA is performed by hardware. There is no code.
So it must be set then how DMA must work.
Then how does he know which flag he must to wait for to send another 8bit,16 bit, 32 bit data ? Before he don't receive the flag he won't send another pack. For Uart it is TXE, for other devices I don't know. So in the code there should be info about the flag he has to receive to send another data. Where is it ?

What is huart2.Instance->CR3 ? I see the comment what it is but I don't understand.

HAL_DMA_Start as I understand starts the DMA but where he gets the info about which flag he should be waiting for ?
1700364772042.png

Can't see it here.
 

MrChips

Joined Oct 2, 2009
30,467
Remember, all MCU peripherals are built with hardware, clocks, counters, registers, flags, etc.
Program code is used to communicate with the hardware.

DMA does not need code. It is hardware that communicates with hardware using hard-wired connections.
The DMA hardware loads the UART TX register, starts transmission and waits for the hardware to tell it when it is ready to accept another byte. There is no code needed to do this.

(Complex computer hardware architectures are designed and built using what is known as micro-code. You don't need to know this because it is just a lower level of hardware complexity.)
 

nsaspook

Joined Aug 27, 2009
12,778
Xenon02 said:
- Why UART need to be set in DMA mode or rather why there is this type of mode ?

Speed with low CPU processing loads and hardware based multitasking is why, in exchange for hardware and some software complexity. The DMA controller uses fast hardware logic to read things like the TXE flag that would normally also go to the interrupt controller to trigger an interrupt for software code processing on a non-dma transfer. Everything that's needed to cycle data is hard-coded (after register configuration of the DMA controller) for standalone operation. What's also important is memory speed, because we are not wasting memory bandwidth on CPU instructions during these transfers all of the usable memory speed is used (and sometimes hogged). That's why some processors have dedicated DMA memory buffers/banks for some controllers so another concurrent task that's using CPU instructions and data memory can also process at full memory speed.

You can design kernel protocol drivers for systems the RPi that can move RX/TX SPI data at 64MHz using DMA on dedicated cores. It configures the DMA and starts the process, after that it's close to 100% hardware for most bulk transfers. Most processors have several DMA channels so using DMA on other I/O channels like the UART helps with concurrency and multi-task code efficiency even if you don't need the MAX speed it can deliver.
https://forum.allaboutcircuits.com/threads/fifo-logic-block-meta-stability.136284/post-1143241

You can do some strange stuff with DMA.
https://people.ece.cornell.edu/land/courses/ece4760/PIC32/index_DMA_weird_machine.html
 
Last edited:

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Remember, all MCU peripherals are built with hardware, clocks, counters, registers, flags, etc.
Program code is used to communicate with the hardware.

DMA does not need code. It is hardware that communicates with hardware using hard-wired connections.
The DMA hardware loads the UART TX register, starts transmission and waits for the hardware to tell it when it is ready to accept another byte. There is no code needed to do this.
I found out what this huart2.Instance->CR3 exactly means. In CR3 there is a bit that activates in UART option to send a request that he (the UART) can take new data.

Also I've found something like this :
1700366393481.png

So I understand that DMAT is only activating this AND gate and TXE is connected to this and gate which tells the DMA whether to send next data or not.


And about code,
1700366474094.png

The callbacks for half transfer complete or full transfer complete aren't from DMA or for DMA ?


Remember, all MCU peripherals are built with hardware, clocks, counters, registers, flags, etc.
Program code is used to communicate with the hardware.

DMA does not need code. It is hardware that communicates with hardware using hard-wired connections.
I understand that peripherals are built with hardware, clock, counters etc.
DMA doesn't need a code to communicate/to be configured ? What is HAL_DMA_Start ? If it's not a code for DMA then for what ?

@nsaspook
Nice new profile picture :>
 

MrChips

Joined Oct 2, 2009
30,467
I understand that peripherals are built with hardware, clock, counters etc.
DMA doesn't need a code to communicate/to be configured ? What is HAL_DMA_Start ? If it's not a code for DMA then for what ?
You need a key to start the motor of your car, correct?
The key has nothing to do with the running of the motor.

HAL_DMA_Start is a key to start the DMA. This has nothing to do with the actual process of transferring data from memory to the UART TX register repeatedly.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Ok but DMA then needs a code to even start. You've mentioned it doesn't. So I can see in HAL_DMA_Start that most of the things Is that it modifies UART dma Handler. After that there are some specific for DMA commands like configuring HAL_DMA_Configure.
There are also some interupts settings and they are DMA but uses Usart handler so I guess they are not from DMA device ? So it is a setting for UART ?

These handlers are confusing to the points that I don't know what is set for what.
 

Thread Starter

Xenon02

Joined Feb 24, 2021
495
Here's a hint. Unless you know what you're talking about, try not to make points with people that do know what they are talking about.
My bad here.
I just noticed something and it didn't match with the answer. I am usually confused from answer to the code or something I see.
(of course this is a good reminder, because I have found the answer for the 2x16 LCD I was looking for so I'll add it later).

So I've read a bit in RM from the STM32 Nucleo-L073RZ how the DMA works, and I've found out that DMA transmits the data after it receives a flag DMA request like TX, so that's why the UART must share the TX flag so in register CR3 there is a bit that enables to send the TX flag to the DMA, without it the DMA won't send data, it needs a request from peripheral.

I then decided to get more deep into it which was how it finds the data (I've learned about chips, R/W bits. Still thinking how chips memory works when it's off I guess the output is nor 1 or 0 so it's probably HZ but if every chip is HZ then the input that uses the the output memory must have pull up or pull down resistor).
Yup I know I think to much, but it makes me nervous that I don't understand it but everybody does in more specific way, and I think that when I work with it must understand it fully and I cannot change that in me ;D

So I am now stuck with how the DMA transfer the data from memory to peripheral (in RM there is no info that DMA has register to contain data to be transferred, usually there are registers to set the DMA). And was thinking how UART sets flags, in simple explanation it shows only RX/TX flags, and data bus and there is bit talk with the registers, so I wondered how the logic gates would look like (of course there is no info in RM because it is an intellectual property stuff understandable :) ). Or how register output can work as read or write (i mean the same 8 lines are used to read or write in register), maybe they work as memory so maybe that's how.

And saw that DMA must be configured so CPU has a code to access the DMA registers to configure it (that's what I thought reading RM).
This was a thing I thought when I read that DMA doesn't need a code. I might have wrongly interpreted, that it doesn't need code to controll the transffer, but I thought that the code is needed to start DMA and to configure it the direction, addresses etc.



Just saying, I found myself being to buggy in stuff or going into deep ocean and usually I am asked why I need this type of specific info. Maybe it will be usefull in FPGA (started in with Xilinx), but of course understanding how it works using logic gates might not be usefull in STM.
 

nsaspook

Joined Aug 27, 2009
12,778
My bad here.
I just noticed something and it didn't match with the answer. I am usually confused from answer to the code or something I see.
(of course this is a good reminder, because I have found the answer for the 2x16 LCD I was looking for so I'll add it later).

So I've read a bit in RM from the STM32 Nucleo-L073RZ how the DMA works, and I've found out that DMA transmits the data after it receives a flag DMA request like TX, so that's why the UART must share the TX flag so in register CR3 there is a bit that enables to send the TX flag to the DMA, without it the DMA won't send data, it needs a request from peripheral.

I then decided to get more deep into it which was how it finds the data (I've learned about chips, R/W bits. Still thinking how chips memory works when it's off I guess the output is nor 1 or 0 so it's probably HZ but if every chip is HZ then the input that uses the the output memory must have pull up or pull down resistor).
Yup I know I think to much, but it makes me nervous that I don't understand it but everybody does in more specific way ;D

So I am now stuck with how the DMA transfer the data from memory to peripheral (in RM there is no info that DMA has register to contain data to be transferred, usually there are registers to set the DMA). And was thinking how UART sets flags, in simple explanation it shows only RX/TX flags, and data bus and there is bit talk with the registers, so I wondered how the logic gates would look like (of course there is no info in RM because it is an intellectual property stuff understandable :) ). Or how register output can work as read or write (i mean the same 8 lines are used to read or write in register), maybe they work as memory so maybe that's how.

And saw that DMA must be configured so CPU has a code to access the DMA registers to configure it (that's what I thought reading RM).
This was a thing I thought when I read that DMA doesn't need a code. I might have wrongly interpreted, that it doesn't need code to controll the transffer, but I thought that the code is needed to start DMA and to configure it the direction, addresses etc.



Just saying, I found myself being to buggy in stuff or going into deep ocean and usually I am asked why I need this type of specific info. Maybe it will be usefull in FPGA (started in with Xilinx), but of course understanding how it works using logic gates might not be usefull in STM.
Reading about something can only get you so far if you don't have the foundational theory of why it's needed already in your head. The DMA controller in a typical 32-bit controller is a complex beast that requires an understanding of computer hardware architecture to see things at the logic level. It works, it's cool and eventually you will understand if you build things in software on hardware at the embedded hardware level.
 
Top