0
0
Embedded Cprogramming~20 mins

DMA controller concept in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DMA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
DMA Transfer Completion Flag Check

What is the output of this embedded C code snippet simulating a DMA transfer completion flag check?

Embedded C
#include <stdio.h>

#define DMA_FLAG_TC (1 << 1)  // Transfer complete flag

unsigned int DMA_StatusRegister = 0;

void simulate_dma_transfer() {
    DMA_StatusRegister |= DMA_FLAG_TC;  // Set transfer complete flag
}

int main() {
    simulate_dma_transfer();
    if (DMA_StatusRegister & DMA_FLAG_TC) {
        printf("DMA Transfer Completed\n");
    } else {
        printf("DMA Transfer Not Completed\n");
    }
    return 0;
}
ADMA Transfer Not Completed
BCompilation error due to undefined DMA_FLAG_TC
CDMA Transfer Completed
DRuntime error due to invalid memory access
Attempts:
2 left
💡 Hint

Check how the DMA status register flag is set and tested using bitwise AND.

🧠 Conceptual
intermediate
1:30remaining
DMA Controller Purpose

What is the primary purpose of a DMA controller in embedded systems?

ATo transfer data between peripherals and memory without CPU intervention
BTo manage interrupts from multiple devices
CTo increase the CPU clock speed automatically
DTo allow the CPU to directly access external memory faster
Attempts:
2 left
💡 Hint

Think about how DMA helps reduce CPU workload during data transfer.

🔧 Debug
advanced
2:30remaining
DMA Transfer Size Configuration Bug

Identify the error in this DMA transfer size configuration code snippet.

Embedded C
typedef struct {
    unsigned int CCR;
    unsigned int CNDTR;
    unsigned int CPAR;
    unsigned int CMAR;
} DMA_Channel_TypeDef;

DMA_Channel_TypeDef DMA1_Channel1;

void configure_dma_transfer(unsigned int size) {
    DMA1_Channel1.CNDTR = size;
    DMA1_Channel1.CCR |= (1 << 0); // Enable DMA channel
}

int main() {
    configure_dma_transfer(0);
    if (DMA1_Channel1.CNDTR == 0) {
        // Start transfer
        // ...
        return 1;
    }
    return 0;
}
ADMA transfer size is zero, so no data will be transferred
BDMA channel is not enabled due to missing bit set in CCR
CDMA1_Channel1 is not initialized, causing undefined behavior
DDMA transfer size should be set after enabling the channel
Attempts:
2 left
💡 Hint

Check the value assigned to the transfer size and its effect on the transfer.

📝 Syntax
advanced
1:30remaining
DMA Interrupt Handler Syntax

Which option shows the correct syntax for a DMA interrupt handler function in embedded C?

Avoid DMA1_Channel1_IRQHandler(void) -> void { /* handler code */ }
Bint DMA1_Channel1_IRQHandler() { /* handler code */ }
Cvoid DMA1_Channel1_IRQHandler() { /* handler code */ }
Dvoid DMA1_Channel1_IRQHandler(void) { /* handler code */ }
Attempts:
2 left
💡 Hint

Consider the standard C syntax for interrupt handlers in embedded systems.

🚀 Application
expert
3:00remaining
DMA Circular Mode Behavior

Given a DMA configured in circular mode to transfer 4 bytes repeatedly from a peripheral to memory, what will be the value of the transfer count register (CNDTR) after 10 transfers?

Embedded C
typedef struct {
    unsigned int CCR;
    unsigned int CNDTR;
    unsigned int CPAR;
    unsigned int CMAR;
} DMA_Channel_TypeDef;

DMA_Channel_TypeDef DMA1_Channel1 = {0};

void configure_dma_circular(unsigned int size) {
    DMA1_Channel1.CNDTR = size;
    DMA1_Channel1.CCR |= (1 << 5); // Circular mode enable
    DMA1_Channel1.CCR |= (1 << 0); // Enable DMA channel
}

void simulate_transfer() {
    DMA1_Channel1.CNDTR--;
    if (DMA1_Channel1.CNDTR == 0) {
        DMA1_Channel1.CNDTR = 4; // Reload in circular mode
    }
}

int main() {
    configure_dma_circular(4);
    for (int i = 0; i < 10; i++) {
        simulate_transfer();
    }
    return DMA1_Channel1.CNDTR;
}
A4
B2
C0
D6
Attempts:
2 left
💡 Hint

Count how the CNDTR value changes each transfer and resets after reaching zero.