An in-depth understanding of DMA: How does DMA alleviate the CPU’s workload?

What is DMA?

DMA, or Direct Memory Access, is a mechanism used in computer systems to achieve high-speed data transfer. DMA allows peripherals to directly access data stored in main memory without the need for the processor to be involved in every transfer, resulting in faster data transfer speed, reduced processor load, and decreased data transfer delays.

In traditional computer systems, data is sent from input devices, such as hard disks or network adapters, to the processor through a constant transfer process between memory and the processor. This process consumes a large amount of time and resources. However, with DMA, once the data transfer is complete, the processor receives an interrupt request and only handles the interrupt instead of involving itself in the entire data transfer process.

Thus, DMA can help computer systems achieve high-speed, low-latency data transfer, improve data transfer efficiency, and can be used for various types of data transfers, such as audio and video transfer or large file transfers.

How does DMA work?

Here’s an example program that sends data through a microcontroller’s serial port:

char a=0xAA;

After connecting a USB cable, data sent from the microcontroller can be received on the computer. What’s the process for serial communication? Let’s take a look at the first line of code:

char a=0xAA;

This line defines a variable a. Where is the variable stored? We can look at the microcontroller’s logic diagram:

In fact, the variable is stored in SRAM, a storage for the microcontroller, with a very fast storage speed. The storage of variables is operated by the microcontroller’s core using the bus.

Now let’s look at the second line of code:


It transfers the data from memory to the serial data register. Once the serial data register receives data, the external peripheral automatically sends the data out. The essence of this line of code is transferring data from memory to the peripheral, which requires operation by the core. If it’s sending a byte of data it can be completed in an instant, about one millisecond. If it’s sending 10,000 of these data, it would take up to 10 seconds to complete. Although it may be just a bit longer than an instant for us, 10 seconds can be extremely long for the microcontroller, causing the CPU’s precious resources to be consumed in data transfer.

To solve the pain point of CPU resource usage during data transfer, engineers designed a module called DMA. DMA stands for direct memory access, meaning direct access to memory. Its primary function is to transfer data, for example, transferring data from memory to an external peripheral or from the peripheral to memory without requiring the core’s involvement. This frees up the core to do other things, such as operating I/O ports or responding to interrupts.

DMA module

Applying the common program as an example:

for ( i =0; i <10000; i ++)
 TXREG = OXAA ;  // Make serial port send 0XAA
LED =1;      // Illuminate LED
    Delay (5);    // Delay for 5 seconds
    LED =0;      // Turn off LED
    Delay (5);    // Delay for 5 seconds

It initially sends 10,000 pieces of serial port data. This process takes 10 seconds, during which the LED cannot blink. Without the use of DMA, it is impossible to send serial port data and blink the LED simultaneously. However, by using DMA, it is possible to send serial port data and blink the LED at the same time.

How does DMA achieve this? Let’s take a look at the program using DMA:

for  ( i =0; i <10000; i ++)
SendData [ i ]= OXAA;
DMA _ PeriAddr   = OXFFFF ;
DMA _ SramAddr  = 0X0000;
DMA _ Direction   = DMA - Peri ;
DMA _ DataSize    =  10000;
DMA _ DataSize    =  10000;
DMA _ Sram +     =  ON;
DMA _ Peri +      =  OFF;
 LED _ shan (10);

Firstly, the DMA initialization program is configured at the beginning of the program. Then, the program starts sending the 10,000 pieces of data without the need for the kernel to be involved. The kernel is then free to control the blinking of the LED. This results in the phenomenon of sending serial port data while the LED is blinking simultaneously.

What configurations does DMA undergo in the beginning? Firstly, the program defines an array:

Each value of the array is 0XAA. The data in this array is the serial port data to be sent, and this data is stored in SRAM memory. Furthermore, the addresses of these data are continuous. For example, if the address of the first data is 0x0000, then the address of the second data is 0x001, and so on.

Next, the DMA is configured:

The first line of the program sets the target register address for the DMA to transfer. This is where the data is moved to. For example, if the address of the serial port data receiver is 0xffff, then this line of the program should be copied as 0xffff.

The second line sets the source address in SRAM memory, which is the address of the first data to be transferred. Since the first address of the array is 0x0000, this should be set as 0x0000.

The third line of the program sets the direction of data transfer. Since we are transferring data from memory to the peripheral, the program should set DMA to the peripheral.

The fourth line sets the size of the data to be transferred. Since this array contains 10,000 pieces of data, this should be set as 10000.

The fifth line of the program increments the memory address. This is because we are sending the 10,000 pieces of data in this array. Setting the memory address to increment means that data from the second address will be sent after data from the first address is sent, and so on.

If the memory address is not incremented, the program will keep sending the first data 10,000 times.

The sixth line of the program sets the peripheral address not to be incremented. This is because we are transferring all 10,000 pieces of data to the same address of the serial port.

After these operations, the DMA is roughly configured. The configuration of DMA requires the involvement of the kernel, but once the DMA is configured, it can operate on its own without the kernel needing to be involved again. At this point, the DMA is responsible for data transfer operations, while the kernel focuses on making the LED blink. Apart from transferring serial port data, there are many other data transfer applications using DMA, such as receiving serial port data, which transfers data from the peripheral to memory. There is also analog-to-digital conversion (ADC), which transfers data from the ADC register to memory. Regardless of the application, the use of the DMA module greatly reduces the workload of the kernel.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top