📖 Scenario: You are working on a simple embedded system that reads data from a sensor peripheral and stores it into memory for later processing.
🎯 Goal: Build a small program that simulates reading data from a peripheral register and transfers it into a memory buffer using a loop.
📋 What You'll Learn
Create an array called memory_buffer with 5 elements initialized to zero
Create a variable called peripheral_data as an array with 5 predefined sensor values
Use a for loop with variable i to copy data from peripheral_data to memory_buffer
Print the contents of memory_buffer after the transfer
💡 Why This Matters
🌍 Real World
Embedded systems often read sensor or peripheral data and store it in memory for processing or communication.
💼 Career
Understanding peripheral-to-memory transfers is essential for embedded software developers working with microcontrollers and hardware interfaces.
Progress0 / 4 steps
1
DATA SETUP: Create the peripheral data array
Create an integer array called peripheral_data with these exact values: 10, 20, 30, 40, 50.
Embedded C
Hint
Use C array syntax to create peripheral_data with the given values.
2
CONFIGURATION: Create the memory buffer array
Create an integer array called memory_buffer with 5 elements, all initialized to zero.
Embedded C
Hint
Initialize memory_buffer with zeros using curly braces.
3
CORE LOGIC: Transfer data from peripheral to memory
Use a for loop with variable i from 0 to 4 to copy each element from peripheral_data to memory_buffer.
Embedded C
Hint
Use a for loop to copy each element by index.
4
OUTPUT: Print the memory buffer contents
Use a for loop with variable i from 0 to 4 to print each element of memory_buffer separated by spaces, followed by a newline.
Embedded C
Hint
Use printf inside a loop to print each element followed by a space, then print a newline.
Practice
(1/5)
1. What is the main purpose of peripheral-to-memory transfer in embedded systems?
easy
A. To execute arithmetic operations on peripheral data
B. To send data from memory back to the peripheral device
C. To move data directly from a peripheral device to memory without CPU intervention
D. To reset the peripheral device after data transfer
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 C
Quick Check:
Peripheral-to-memory transfer = direct data move without CPU [OK]
Hint: Peripheral-to-memory means data moves without CPU help [OK]
Common Mistakes:
Confusing peripheral-to-memory with memory-to-peripheral transfer
Thinking CPU processes data during transfer
Assuming transfer resets the device
2. Which of the following is the correct way to enable DMA for peripheral-to-memory transfer in embedded C?
easy
A. DMA_Channel->CCR = DMA_CCR_EN << 1;
B. DMA_Channel->CCR |= DMA_CCR_EN;
C. DMA_Channel->CCR &= ~DMA_CCR_EN;
D. DMA_Channel->CCR = 0;
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 B
Quick Check:
Enable DMA by setting bit with |= [OK]
Hint: Use |= to set enable bit without clearing others [OK]
Common Mistakes:
Using = instead of |= clears other bits
Clearing enable bit instead of setting it
Shifting bits incorrectly
3. Given the following code snippet, what will be the value of memory_buffer[0] after DMA transfer completes?
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
medium
A. 0x55
B. 0x00
C. 0xFF
D. Undefined (random value)
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 A
Quick Check:
DMA copies 0x55 to memory_buffer[0] [OK]
Hint: DMA copies source byte to destination buffer [OK]
Common Mistakes:
Assuming memory_buffer stays unchanged
Confusing peripheral_data value
Thinking DMA sets memory to 0xFF
4. Identify the error in this DMA peripheral-to-memory transfer setup code snippet:
5. You want to transfer 8 bytes from a UART peripheral data register to a memory buffer using DMA. Which sequence correctly sets up the DMA for peripheral-to-memory transfer?
hard
A. Set DMA peripheral address to UART data register; set memory address to buffer; set transfer length to 8; enable DMA channel
B. Set DMA memory address to UART data register; set peripheral address to buffer; set transfer length to 8; enable DMA channel
C. Set DMA peripheral address to buffer; set memory address to UART data register; set transfer length to 8; enable DMA channel
D. Set DMA transfer length to 8; enable DMA channel; set peripheral and memory addresses afterwards
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 A
Quick Check:
Peripheral = source, Memory = destination, length set before enable [OK]