DMA with UART for bulk transfer in Embedded C - Time & Space Complexity
When using DMA with UART for bulk data transfer, it's important to understand how the time to complete the transfer changes as the data size grows.
We want to know how the program's work increases when sending more data using DMA and UART.
Analyze the time complexity of the following code snippet.
// Start DMA transfer of data buffer over UART
void start_dma_uart_transfer(uint8_t *buffer, size_t length) {
// Configure DMA source and destination
DMA_SetSource(buffer);
DMA_SetDestination(UART_DR);
DMA_SetLength(length);
DMA_Enable();
UART_EnableDMA();
// Transfer happens in background
}
This code starts a DMA transfer that sends a block of data over UART without CPU intervention during the transfer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: DMA hardware moves each byte from memory to UART data register.
- How many times: Once per byte in the data buffer (length times).
The DMA transfers each byte one by one in the background, so the total work grows as the number of bytes increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 byte transfers |
| 100 | 100 byte transfers |
| 1000 | 1000 byte transfers |
Pattern observation: The number of operations grows directly with the size of the data.
Time Complexity: O(n)
This means the time to transfer data grows linearly with the amount of data being sent.
[X] Wrong: "DMA transfers all data instantly regardless of size."
[OK] Correct: DMA still moves each byte one at a time; larger data means more transfers and more time.
Understanding how DMA offloads work from the CPU but still depends on data size helps you explain efficient data handling in embedded systems.
"What if we used interrupt-driven UART instead of DMA? How would the time complexity change?"