A DMA controller helps move data quickly inside a computer without using the main processor. This makes programs run faster and saves the processor for other tasks.
0
0
DMA controller concept in Embedded C
Introduction
When you need to transfer large blocks of data between memory and devices without slowing down the CPU.
When reading data from sensors continuously and storing it in memory automatically.
When sending data to a display or communication port without interrupting the main program.
When you want to improve system efficiency by offloading data movement tasks from the CPU.
Syntax
Embedded C
DMA_Init(); DMA_SetSourceAddress(source_address); DMA_SetDestinationAddress(destination_address); DMA_SetTransferSize(size); DMA_StartTransfer();
This is a general sequence to configure and start a DMA transfer.
Exact function names and parameters depend on the microcontroller and its DMA hardware.
Examples
This example sets up a DMA transfer of 256 bytes from memory address 0x20001000 to 0x40002000.
Embedded C
DMA_Init(); DMA_SetSourceAddress(0x20001000); DMA_SetDestinationAddress(0x40002000); DMA_SetTransferSize(256); DMA_StartTransfer();
This example transfers 128 bytes from an ADC buffer to a memory area using DMA.
Embedded C
DMA_Init();
DMA_SetSourceAddress((uint32_t)&adc_buffer);
DMA_SetDestinationAddress((uint32_t)&memory_area);
DMA_SetTransferSize(128);
DMA_StartTransfer();Sample Program
This program simulates a DMA controller copying 10 bytes from a source array to a destination array without CPU copying each byte manually.
Embedded C
#include <stdint.h> #include <stdio.h> // Simulated DMA controller registers volatile uint32_t DMA_SRC_ADDR; volatile uint32_t DMA_DST_ADDR; volatile uint32_t DMA_SIZE; volatile uint8_t DMA_START; // Simulated memory uint8_t source[10] = {0,1,2,3,4,5,6,7,8,9}; uint8_t destination[10] = {0}; void DMA_Init() { // Normally hardware init code here } void DMA_SetSourceAddress(uint32_t addr) { DMA_SRC_ADDR = addr; } void DMA_SetDestinationAddress(uint32_t addr) { DMA_DST_ADDR = addr; } void DMA_SetTransferSize(uint32_t size) { DMA_SIZE = size; } void DMA_StartTransfer() { // Simulate DMA transfer by copying data uint8_t* src = (uint8_t*)DMA_SRC_ADDR; uint8_t* dst = (uint8_t*)DMA_DST_ADDR; for (uint32_t i = 0; i < DMA_SIZE; i++) { dst[i] = src[i]; } DMA_START = 1; // Indicate transfer done } int main() { DMA_Init(); DMA_SetSourceAddress((uint32_t)source); DMA_SetDestinationAddress((uint32_t)destination); DMA_SetTransferSize(10); DMA_StartTransfer(); printf("Destination data after DMA transfer:\n"); for (int i = 0; i < 10; i++) { printf("%d ", destination[i]); } printf("\n"); return 0; }
OutputSuccess
Important Notes
DMA transfers happen in the background, freeing the CPU to do other work.
Always ensure source and destination addresses are valid and accessible.
DMA size should not exceed the buffer sizes to avoid memory corruption.
Summary
DMA controller moves data automatically without CPU intervention.
It improves speed and efficiency in embedded systems.
Setup involves source, destination, size, and starting the transfer.