Bird
Raised Fist0
Embedded Cprogramming~10 mins

Peripheral-to-memory transfer in Embedded C - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the peripheral-to-memory DMA transfer.

Embedded C
DMA_Start_Transfer([1]);
Drag options to blanks, or click blank then click option'
ADMA_CHANNEL_1
BTIMER2
CADC1
DUART1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the peripheral name instead of the DMA channel.
Using a timer channel instead of DMA channel.
2fill in blank
medium

Complete the code to configure the DMA data size for peripheral-to-memory transfer.

Embedded C
DMA_Config.DataSize = [1];
Drag options to blanks, or click blank then click option'
ADMA_DATA_SIZE_64BIT
BDMA_DATA_SIZE_8BIT
CDMA_DATA_SIZE_32BIT
DDMA_DATA_SIZE_16BIT
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 8-bit size when peripheral sends 16-bit data.
Selecting unsupported 64-bit size.
3fill in blank
hard

Fix the error in the DMA transfer complete interrupt handler to clear the interrupt flag.

Embedded C
void DMA_IRQHandler(void) {
    if (DMA_GetFlagStatus([1])) {
        DMA_ClearFlag([1]);
        // Process data
    }
}
Drag options to blanks, or click blank then click option'
ADMA_FLAG_TE1
BDMA_FLAG_HT1
CDMA_FLAG_TC1
DDMA_FLAG_GL1
Attempts:
3 left
💡 Hint
Common Mistakes
Clearing half-transfer flag instead of transfer complete.
Not clearing any flag causing interrupt to repeat.
4fill in blank
hard

Fill both blanks to set the peripheral and memory addresses for DMA transfer.

Embedded C
DMA_Config.PeripheralBaseAddr = [1];
DMA_Config.MemoryBaseAddr = [2];
Drag options to blanks, or click blank then click option'
A(uint32_t)&UART1->DR
B(uint32_t)&ADC1->DR
C(uint32_t)buffer
D(uint32_t)data_array
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping peripheral and memory addresses.
Using ADC1 address when UART1 is the peripheral.
5fill in blank
hard

Fill all three blanks to configure DMA transfer direction, enable circular mode, and set priority.

Embedded C
DMA_Config.Direction = [1];
DMA_Config.Mode = [2];
DMA_Config.Priority = [3];
Drag options to blanks, or click blank then click option'
ADMA_DIR_PeripheralSRC
BDMA_DIR_PeripheralDST
CDMA_MODE_Circular
DDMA_PRIORITY_High
Attempts:
3 left
💡 Hint
Common Mistakes
Setting direction as peripheral destination.
Using normal mode instead of circular for continuous transfer.
Setting low priority causing delays.

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

  1. Step 1: Understand peripheral-to-memory transfer

    This transfer moves data directly from a peripheral device to memory, bypassing the CPU to improve efficiency.
  2. Step 2: Identify the main purpose

    The main goal is to reduce CPU load by letting hardware handle data movement.
  3. Final Answer:

    To move data directly from a peripheral device to memory without CPU intervention -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    DMA_Channel->CCR |= DMA_CCR_EN; -> Option B
  4. 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

  1. Step 1: Understand DMA transfer setup

    DMA is set to transfer 1 byte from peripheral_data (0x55) to memory_buffer.
  2. Step 2: Determine memory_buffer[0] after transfer

    After transfer, memory_buffer[0] will hold the value 0x55 copied from peripheral_data.
  3. Final Answer:

    0x55 -> Option A
  4. 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:
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;
medium
A. The DMA channel is enabled before setting transfer length
B. The DMA channel is enabled without disabling first
C. The transfer length (CNDTR) is set after enabling DMA
D. The peripheral address and memory address are swapped

Solution

  1. Step 1: Recall DMA register roles

    CPAR holds peripheral (source) address, CMAR holds memory (destination) address for peripheral-to-memory transfer.
  2. Step 2: Check the code assignments

    The code sets CPAR = &memory_buffer (should be peripheral), CMAR = &peripheral_data (should be memory). Addresses are swapped.
  3. Final Answer:

    The peripheral address and memory address are swapped -> Option D
  4. Quick Check:

    CPAR=peripheral, CMAR=memory [OK]
Hint: Peripheral address = CPAR, Memory address = CMAR [OK]
Common Mistakes:
  • Swapping peripheral and memory addresses
  • Enabling DMA before setting parameters
  • Not disabling DMA before re-enabling
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

  1. Step 1: Identify source and destination

    Source is UART data register (peripheral), destination is memory buffer.
  2. 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.
  3. 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
  4. Quick Check:

    Peripheral = source, Memory = destination, length set before enable [OK]
Hint: Peripheral address = source, memory address = destination [OK]
Common Mistakes:
  • Swapping peripheral and memory addresses
  • Enabling DMA before setting addresses and length
  • Setting transfer length after enabling DMA
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard