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?"
Practice
Solution
Step 1: Understand DMA role in UART transfer
DMA transfers data directly between memory and UART without CPU intervention for each byte.Step 2: Identify the benefit of reduced CPU load
This frees CPU to perform other tasks while data moves in background.Final Answer:
It reduces CPU load by handling data transfer automatically. -> Option AQuick Check:
DMA offloads CPU [OK]
- Thinking DMA increases UART speed beyond hardware limits
- Confusing DMA with encryption or clock control
- Assuming UART can work without clock signal
Solution
Step 1: Identify UART register for DMA transmit enable
UART control register 3 (CR3) contains the DMAT bit to enable DMA for transmission.Step 2: Check correct bit and register usage
Setting USART_CR3_DMAT in UART->CR3 enables DMA transmit requests.Final Answer:
UART->CR3 |= USART_CR3_DMAT; -> Option AQuick Check:
DMA transmit enable bit is in CR3 [OK]
- Using CR1 instead of CR3 for DMA transmit enable
- Confusing DMA channel enable with UART DMA request enable
- Setting baud rate register instead of control bits
DMA->CCR = DMA_CCR_MINC | DMA_CCR_TCIE; DMA->CNDTR = 100; DMA->CPAR = (uint32_t)&UART->RDR; DMA->CMAR = (uint32_t)rx_buffer; DMA->CCR |= DMA_CCR_EN;
Solution
Step 1: Understand DMA CNDTR register role
CNDTR holds the number of data items to transfer; initially set to 100.Step 2: Check when CNDTR changes
CNDTR decrements only after each data transfer, so right after enabling DMA, it remains 100.Final Answer:
100 -> Option CQuick Check:
DMA transfer count starts at set value = 100 [OK]
- Assuming CNDTR decrements immediately on enable
- Confusing CNDTR with current data register
- Thinking CNDTR resets to zero on start
DMA->CCR = DMA_CCR_MINC | DMA_CCR_DIR; DMA->CNDTR = 50; DMA->CPAR = (uint32_t)&UART->TDR; DMA->CMAR = (uint32_t)tx_buffer; DMA->CCR |= DMA_CCR_EN; UART->CR3 &= ~USART_CR3_DMAT;
Solution
Step 1: Check UART DMA transmit enable bit
UART->CR3 must have DMAT bit set to enable DMA requests for transmit.Step 2: Identify disabling of DMAT bit
Code clears DMAT bit after DMA enable, disabling UART DMA transmit requests, so transfer won't start.Final Answer:
UART DMA transmit request is disabled after enabling DMA. -> Option BQuick Check:
DMAT bit must be set for DMA transmit [OK]
- Disabling UART DMA transmit request after DMA enable
- Confusing DMA direction bit meaning
- Forgetting to enable memory increment mode
Solution
Step 1: Identify direction for UART transmit DMA
For UART transmit, DMA source is memory buffer, destination is UART TDR register.Step 2: Enable UART DMA transmit request and DMA channel
Set UART DMAT bit to enable DMA requests on transmit, then enable DMA channel to start transfer.Final Answer:
Set DMA source to buffer, destination to UART TDR, length 256; enable UART DMAT bit; enable DMA channel. -> Option DQuick Check:
Memory to UART TDR + DMAT enabled [OK]
- Confusing UART TDR and RDR registers for source/destination
- Using DMAR bit for transmit instead of DMAT
- Setting DMA direction opposite to data flow
Ready to prove it?
Pass the test and this pattern moves unknown
