Challenge - 5 Problems
Memory-to-peripheral Transfer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this DMA transfer status code?
Consider this embedded C code snippet that checks the status of a DMA transfer. What will be printed?
Embedded C
#include <stdio.h> #include <stdint.h> #define DMA_TRANSFER_COMPLETE 1 uint8_t DMA_Status = DMA_TRANSFER_COMPLETE; int main() { if (DMA_Status & DMA_TRANSFER_COMPLETE) { printf("Transfer Complete\n"); } else { printf("Transfer Incomplete\n"); } return 0; }
Attempts:
2 left
💡 Hint
Look at how the DMA_Status variable is checked using bitwise AND.
✗ Incorrect
The DMA_Status variable has the DMA_TRANSFER_COMPLETE bit set. The if condition checks this bit using bitwise AND, so it prints 'Transfer Complete'.
❓ Predict Output
intermediate2:00remaining
What is the value of buffer after this memory-to-peripheral transfer simulation?
This code simulates copying data from memory to a peripheral buffer. What is the final content of buffer?
Embedded C
#include <stdio.h> #include <string.h> int main() { char memory[] = "DATA"; char buffer[5] = {0}; memcpy(buffer, memory, 4); buffer[4] = '\0'; printf("%s", buffer); return 0; }
Attempts:
2 left
💡 Hint
Check how memcpy copies bytes and how the null terminator is added.
✗ Incorrect
memcpy copies 4 bytes from memory to buffer, which are 'D','A','T','A'. Then buffer[4] is set to '\0' to terminate the string. So printing buffer outputs 'DATA'.
🔧 Debug
advanced3:00remaining
Why does this DMA transfer code cause a runtime error?
Examine this code snippet for a DMA transfer setup. Why does it cause a runtime error?
Embedded C
void start_dma_transfer(uint32_t *src, uint32_t *dst, size_t size) {
for (size_t i = 0; i < size; i++) {
dst[i] = src[i];
}
}
int main() {
uint32_t source[3] = {1, 2, 3};
uint32_t destination[3] = {0};
start_dma_transfer(source, destination, 3);
return 0;
}Attempts:
2 left
💡 Hint
Check the loop condition and array sizes carefully.
✗ Incorrect
The loop runs from i=0 to i<=size, which means it accesses dst[3] and src[3], but arrays have indices 0 to 2. This causes out-of-bounds access and undefined behavior.
📝 Syntax
advanced2:00remaining
Which option fixes the syntax error in this DMA transfer initialization?
This code snippet has a syntax error. Which option fixes it?
Embedded C
typedef struct {
uint32_t *source;
uint32_t *destination;
size_t length;
} DMA_Transfer;
void init_dma(DMA_Transfer *transfer) {
transfer->source = (uint32_t *)0x20000000;
transfer->destination = (uint32_t *)0x40000000;
transfer->length = 256;
}Attempts:
2 left
💡 Hint
Look carefully at the end of each statement line.
✗ Incorrect
The line 'transfer->source = (uint32_t *)0x20000000' is missing a semicolon at the end, causing a syntax error. Adding the semicolon fixes it.
🚀 Application
expert3:00remaining
How many bytes are transferred in this memory-to-peripheral DMA setup?
Given this DMA transfer configuration, how many bytes will be transferred?
Embedded C
typedef struct {
uint8_t *src;
volatile uint8_t *peripheral;
size_t count;
} DMA_Config;
int main() {
DMA_Config config = {
.src = (uint8_t *)0x20001000,
.peripheral = (volatile uint8_t *)0x40002000,
.count = 128
};
// DMA transfer code here
return 0;
}Attempts:
2 left
💡 Hint
Look at the count field and data type size.
✗ Incorrect
The count is 128 and the data type is uint8_t which is 1 byte, so total bytes transferred is 128 * 1 = 128 bytes.