Peripheral-to-memory transfer in Embedded C - Time & Space Complexity
We want to understand how the time to transfer data from a peripheral to memory changes as the amount of data grows.
How does the program's running time increase when we transfer more data?
Analyze the time complexity of the following code snippet.
void peripheral_to_memory_transfer(uint8_t* peripheral, uint8_t* memory, int size) {
for (int i = 0; i < size; i++) {
memory[i] = peripheral[i];
}
}
This code copies data byte-by-byte from a peripheral buffer to a memory buffer for a given size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying one byte from peripheral to memory inside the loop.
- How many times: The loop runs exactly
sizetimes, once per byte.
As the number of bytes to transfer grows, the total operations grow in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: Doubling the data size doubles the number of copy operations.
Time Complexity: O(n)
This means the time to transfer grows directly in proportion to the amount of data.
[X] Wrong: "The transfer time stays the same no matter how much data we copy."
[OK] Correct: Each byte must be copied one by one, so more data means more time.
Understanding how data transfer time grows helps you explain performance in embedded systems clearly and confidently.
"What if we used DMA (Direct Memory Access) to transfer data instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand peripheral-to-memory transfer
This transfer moves data directly from a peripheral device to memory, bypassing the CPU to improve efficiency.Step 2: Identify the main purpose
The main goal is to reduce CPU load by letting hardware handle data movement.Final Answer:
To move data directly from a peripheral device to memory without CPU intervention -> Option CQuick Check:
Peripheral-to-memory transfer = direct data move without CPU [OK]
- Confusing peripheral-to-memory with memory-to-peripheral transfer
- Thinking CPU processes data during transfer
- Assuming transfer resets the device
Solution
Step 1: Understand DMA enable bit setting
To enable DMA channel, the enable bit (DMA_CCR_EN) must be set using bitwise OR to preserve other bits.Step 2: Analyze options
DMA_Channel->CCR |= DMA_CCR_EN; sets the enable bit correctly with |=. DMA_Channel->CCR = 0; clears all bits, disabling DMA. DMA_Channel->CCR &= ~DMA_CCR_EN; clears the enable bit. DMA_Channel->CCR = DMA_CCR_EN << 1; shifts enable bit incorrectly.Final Answer:
DMA_Channel->CCR |= DMA_CCR_EN; -> Option BQuick Check:
Enable DMA by setting bit with |= [OK]
- Using = instead of |= clears other bits
- Clearing enable bit instead of setting it
- Shifting bits incorrectly
uint8_t peripheral_data = 0x55;
uint8_t memory_buffer[1] = {0x00};
// DMA configured to transfer 1 byte from &peripheral_data to memory_buffer
// DMA transfer completes here
Solution
Step 1: Understand DMA transfer setup
DMA is set to transfer 1 byte from peripheral_data (0x55) to memory_buffer.Step 2: Determine memory_buffer[0] after transfer
After transfer, memory_buffer[0] will hold the value 0x55 copied from peripheral_data.Final Answer:
0x55 -> Option AQuick Check:
DMA copies 0x55 to memory_buffer[0] [OK]
- Assuming memory_buffer stays unchanged
- Confusing peripheral_data value
- Thinking DMA sets memory to 0xFF
DMA_Channel->CPAR = (uint32_t)&memory_buffer; DMA_Channel->CMAR = (uint32_t)&peripheral_data; DMA_Channel->CNDTR = 4; DMA_Channel->CCR &= ~DMA_CCR_EN; DMA_Channel->CCR |= DMA_CCR_EN;
Solution
Step 1: Recall DMA register roles
CPAR holds peripheral (source) address, CMAR holds memory (destination) address for peripheral-to-memory transfer.Step 2: Check the code assignments
The code sets CPAR = &memory_buffer (should be peripheral), CMAR = &peripheral_data (should be memory). Addresses are swapped.Final Answer:
The peripheral address and memory address are swapped -> Option DQuick Check:
CPAR=peripheral, CMAR=memory [OK]
- Swapping peripheral and memory addresses
- Enabling DMA before setting parameters
- Not disabling DMA before re-enabling
Solution
Step 1: Identify source and destination
Source is UART data register (peripheral), destination is memory buffer.Step 2: Set DMA registers correctly
Peripheral address register (CPAR) must point to UART data register, memory address register (CMAR) to buffer, transfer length to 8 bytes, then enable DMA channel.Final Answer:
Set DMA peripheral address to UART data register; set memory address to buffer; set transfer length to 8; enable DMA channel -> Option AQuick Check:
Peripheral = source, Memory = destination, length set before enable [OK]
- Swapping peripheral and memory addresses
- Enabling DMA before setting addresses and length
- Setting transfer length after enabling DMA
Ready to prove it?
Pass the test and this pattern moves unknown
