0% found this document useful (0 votes)
5 views5 pages

MA HW 07

The document outlines Lab 07 on Direct Memory Access (DMA) for a course in Microprocessor Architecture at the Institute of Technology of Cambodia. It includes conceptual questions, practical analysis, and programming exercises related to DMA, highlighting its efficiency in microcontroller operations and various applications. The document also provides specific tasks for students to complete, such as generating waveforms and handling data transfers using DMA.

Uploaded by

nar691828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

MA HW 07

The document outlines Lab 07 on Direct Memory Access (DMA) for a course in Microprocessor Architecture at the Institute of Technology of Cambodia. It includes conceptual questions, practical analysis, and programming exercises related to DMA, highlighting its efficiency in microcontroller operations and various applications. The document also provides specific tasks for students to complete, such as generating waveforms and handling data transfers using DMA.

Uploaded by

nar691828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

INSTITUTE OF TECHNOLOGY OF CAMBODIA

DEPARTMENT OF ELETRICAL AND ENERGY ENGNNERING

LAB 07: DMA

Instructor: BUN Menghorng


Course : Lab Microprocessor Architecture
Group : I3-GEE (C2)
Name of Student ID Score
YAV Visal e20220191 …………
TORN Naroth e20220476 ………….

Academic Year:2024 – 2024


Institute of Technology of Cambodia 2024 - 2025

Homework #7
Part A: Conceptual Questions
1. Explain what DMA is and how it improves microcontroller efficiency.
2. What are the main differences between:
 Polling
 Interrupt-based data transfer
 DMA?
3. What are the three main types of DMA transfers in STM32? Give one example for each.
4. Why is circular mode useful in DMA?
5. Describe what happens during a DMA transfer complete interrupt.
6. What role does a timer play when generating SPWM using DMA?
Part B: Practical Analysis
7. You configured DMA to transfer data from memory to TIM2->CCR1. Describe step-by-step how
you would generate a 50 Hz SPWM waveform using a 100-entry sine table.
8. Suppose you are using DMA1_Channel5 for USART1 RX. How would you handle a case where
the received data length varies from packet to packet?
9. In a system where ADC values are stored using DMA in a circular buffer: How would you
calculate the moving average?
How would you detect when a new sample is ready?
Part C: Programming Exercises
10. Write a function in C that uses DMA to copy a 128-byte array from src[] to dst[], and waits for
the transfer to complete.
11. Modify your UART DMA code so that it sends "OK\n" every time a full DMA RX buffer is
received.
12. Implement a simple waveform generator:
 Use a timer and DMA to output a sawtooth waveform on a PWM pin
 The waveform frequency should be adjustable using a variable
Answer
Part A: Conceptual Questions
1.DMA is a feature that allows peripherals or memory blocks to communicate directly with the main
memory without involving the CPU. DMA enhances microcontroller efficiency by offloading data
transfer tasks from the CPU. This reduces CPU load, allowing it to perform other critical operations
while the DMA controller handles data movement. This leads to faster data processing and improved
system performance.

TP: Microprocessor 2
Institute of Technology of Cambodia 2024 - 2025

2. The main differences between:


 Polling: In polling, the CPU continuously checks (or "polls") the status of a peripheral to see
if it's ready for data transfer. This is simple but inefficient because the CPU is actively
engaged in waiting, even if no data is ready for transfer.
 Interrupt-based data transfer: In interrupt-based data transfer, the CPU is interrupted when a
peripheral is ready for data transfer. The CPU temporarily stops executing its current task to
service the interrupt, transferring data or performing an action based on the interrupt.
 DMA: allows peripherals to transfer data directly to or from memory without involving the
CPU. It operates in the background and only signals the CPU when the transfer is complete.
DMA is the most efficient method because it minimizes CPU involvement and improves
throughput.
3. The three main types of DMA transfers in STM32.
 Memory-to-Memory DMA: Transfers data from one memory location to another.
 Memory-to-Peripheral DMA: Transfers data from memory to a peripheral
 Peripheral-to-Memory DMA: Transfers data from a peripheral to memory.
4. Circular mode in DMA is useful for applications that require continuous data transfer without
stopping. In this mode, once the DMA controller reaches the end of the memory buffer, it
automatically loops back to the beginning of the buffer and continues transferring data.
5. DMA transfer complete interrupt is triggered when the DMA controller finishes transferring all
the data in the defined memory block or buffer. During this interrupt, the CPU is notified that the
data transfer has been completed, allowing it to process the data or perform other actions.
6. A timer in conjunction with DMA can be used to generate SPWM (Sine Pulse Width Modulation)
by controlling the timing and frequency of the PWM signal.
Part B: Practical Analysis
7. Generate a 50 Hz SPWM waveform using a 100-entry sine table with DMA
Step-by-step Process:
 Configure the Timer (TIM2):
 Set Up DMA:
 Create the Sine Table:
 Configure the DMA Transfer:
 Start the Timer and DMA.
8. Step to Handle variable-length USART1 RX with DMA (using DMA1_Channel5)
 Set Up the DMA:
 Determine Packet Length:
TP: Microprocessor 3
Institute of Technology of Cambodia 2024 - 2025

 Handle the Data Reception:


 Post-Transfer Actions:
9. Calculate the Moving Average in a Circular Buffer and Detect New Samples.
1) int moving_avg = 0;
2) for (int i = 0; i < N; i++) {
3) moving_avg += adc_buffer[i];
4) }
5) moving_avg /= N;
Part C: Programming Exercises
10. Write a function in C that uses DMA to copy a 128-byte array from src[] to dst[], and waits for
the transfer to complete.
1) void DMA_Copy(uint8_t* src, uint8_t* dst, uint16_t size) {
2) // Configure DMA1 Channel 1 for memory to memory transfer
3) DMA1_Channel1->CCR &= ~DMA_CCR1_EN; // Disable DMA channel to configure
it
4) DMA1_Channel1->CPAR = (uint32_t)src; // Set the source address
5) DMA1_Channel1->CMAR = (uint32_t)dst; // Set the destination address
6) DMA1_Channel1->CNDTR = size; // Set the number of data to transfer
7) DMA1_Channel1->CCR |= DMA_CCR1_MINC | DMA_CCR1_DIR | DMA_CCR1_EN;
// Enable DMA with memory-to-memory transfer, increment mode
8)
9) // Wait for DMA transfer to complete
10) while (DMA1->ISR & DMA_ISR_TCIF1 == 0); // Wait for transfer complete interrupt
11) DMA1->IFCR = DMA_IFCR_CTCIF1; // Clear interrupt flag
12) }
11.Modify UART DMA Code to Send "OK\n" After RX:
1. void DMA_UART_RX_Handler(void) {
2. if (DMA1_Channel5->CNDTR == 0) { // Check if the full buffer is received
3. // Send "OK\n" via UART using DMA
4. const char* msg = "OK\n";
5. while (*msg) {
6. USART1->TDR = *msg++; // Transmit each character
7. while (!(USART1->ISR & USART_ISR_TC)); // Wait for transmission to complete
8. }
9. }
10. }
12.Waveform Generator with Timer and DMA (Sawtooth Waveform):
1. #define WAVEFORM_SIZE 256

TP: Microprocessor 4
Institute of Technology of Cambodia 2024 - 2025

2. uint16_t sawtooth_wave[WAVEFORM_SIZE];
3.
4. void Generate_Sawtooth_Wave(float frequency) {
5. // Calculate the period and initialize the sawtooth waveform
6. float increment = (float)(PWM_MAX_VALUE) / WAVEFORM_SIZE;
7. for (int i = 0; i < WAVEFORM_SIZE; i++) {
8. sawtooth_wave[i] = (uint16_t)(i * increment);
9. }
10.
11. // Configure timer for desired frequency
12. uint32_t timer_period = (SystemCoreClock / frequency) - 1;
13. TIM2->ARR = timer_period; // Set timer period
14.
15. // Set up DMA to transfer data to TIM2 CCR1 (PWM output)
16. DMA1_Channel1->CPAR = (uint32_t)&TIM2->CCR1;
17. DMA1_Channel1->CMAR = (uint32_t)sawtooth_wave;
18. DMA1_Channel1->CNDTR = WAVEFORM_SIZE;
19. DMA1_Channel1->CCR |= DMA_CCR1_EN; // Enable DMA
20.
21. // Start Timer
22. TIM2->CR1 |= TIM_CR1_CEN; // Enable timer
23. }

TP: Microprocessor 5

You might also like