📚 Assignment 6 CMPE2250: High-Throughput UART with DMA, RTO, and Oscilloscope Profiling
📋 Objectives
In standard interrupt- or polling-driven UART operations, the CPU is forced to service every individual byte transmitted or received. In this assignment, you will offload UART Tx and Rx entirely to the Direct Memory Access (DMA) controller and the DMAMUX request router on the STM32G0B1.
By completing this assignment, you will:
- Configure dual-channel DMA transfers: Peripheral-to-Memory (Rx) and Memory-to-Peripheral (Tx).
- Interface the USART Receiver Timeout (RTO) unit with DMA reception to process variable-length frames without CPU byte polling.
- Detect and recover from a buffer overrun condition whenever an incoming payload exceeds the designated buffer boundary.
-
Measure and compare CPU execution overhead between blocking polled transmission vs. autonomous DMA transmission using an oscilloscope.
🔌 Hardware & Pinout Specifications
| Signal / Peripheral | Pin | Mode / Configuration | Assignment Role |
|---|---|---|---|
| USART2 TX | PA2 |
Alternate Function 1 (AF1) |
Serial Data Out (115200 8-N-1) |
| USART2 RX | PA3 |
Alternate Function 1 (AF1) |
Serial Data In (115200 8-N-1) |
| Periodic TX Profiling | PA6 |
GPIO Push-Pull Output | High on TX start $\rightarrow$ Low inside DMA TC ISR |
| Reply TX Profiling | PA5 |
GPIO Push-Pull Output | Single-shot pulse measuring CPU execution time during reply |
| User Push Button | PC13 |
Digital Input | Nucleo Board User Button |
1️⃣ Memory Buffers & Sizing
- Define
BUFFER_SIZEas exactly 32 bytes. -
Allocate static memory buffers:
#define BUFFER_SIZE 32 uint8_t rxBuffer[BUFFER_SIZE]; uint8_t txBuffer[BUFFER_SIZE + 16]; // Sized to accommodate prefix + payload + newline
2️⃣Part 1: Periodic Transmission & Oscilloscope Analysis
- Configure SysTick to trigger an interrupt every 1 ms.
- Every 250 ms, transmit the fixed string
"Hello CMPE2250\r\n"(16 bytes) viaUSART2using DMA (DMA1_Channel2). - GPIO Profiling on
PA6:- Drive
PA6HIGH immediately before triggering the DMA transmission. - Enable the DMA Transfer Complete Interrupt (
TCIE) onDMA1_Channel2. - Clear the interrupt flag in
DMA1->IFCRand drivePA6LOW insideDMA1_Channel2_3_IRQHandler().
- Drive
- Connect the oscilloscope:
- Channel 1 (Yellow): Pin
PA6(Profiling Pin) - Channel 2 (Blue): Pin
PA2(USART2_TX)
- Channel 1 (Yellow): Pin
- In your report, address the following theoretical questions:
- What does the pulse period of
PA6represent? - What does the pulse width (HIGH time) of
PA6represent? - At 115,200 baud (1 start bit, 8 data bits, 1 stop bit = 10 bits per frame): \(\text{Bit Time} = \frac{1}{115200\,\text{s}^{-1}} \approx 8.68\,\mu\text{s}\) \(\text{Frame Time (16 bytes)} = 16 \times 10 \times 8.68\,\mu\text{s} \approx 1.389\,\text{ms}\)
-
Compare your measured pulse width on
PA6against the theoretical calculation and account for any small discrepancies.
- What does the pulse period of
3️⃣ Part 2: DMA Reception with RTO (Normal Packet Echo)
- Initialize
DMA1_Channel1for Peripheral-to-Memory (USART2->RDR$\rightarrow$rxBuffer) withCNDTR = BUFFER_SIZE. - Configure the USART2 Receiver Timeout (
RTOR):- Set timeout duration to 1 ms ($115\text{ bit times at }115200\text{ baud}\approx 1\text{ ms}$).
- Enable
RTOENinUSART2->CR2andRTOIEinUSART2->CR1.
- When the host finishes sending a packet, an RTO interrupt fires:
- Calculate the number of received bytes: \(\text{rxBytes} = \text{BUFFER\_SIZE} - \text{DMA1\_Channel1->CNDTR}\)
- Echo the packet back to the host prefixed with
"CMPE2250: <received_string>\r\n". - Clear and re-arm
DMA1_Channel1for the next transaction.
4️⃣ Part 3: CPU Overhead Profiling on Reply (PA5 Single-Shot)
Profile the difference in CPU execution time when returning the received frame:
- Blocking Mode (
#undef ReplyDMA):- Drive
PA5HIGH. - Transmit the prefix and payload using blocking polled loops (
UART_TxStr/UART_TxByte). - Drive
PA5LOW.
- Drive
- DMA Non-Blocking Mode (
#define ReplyDMA):- Drive
PA5HIGH. - Assemble the reply in
txBufferand start transmission usingUART_TxBufferDma(). - Drive
PA5LOW immediately after starting the channel.
- Drive
- Configure your oscilloscope for a Single-Shot Trigger on
PA5:- Measure the pulse width of
PA5under both modes. -
Calculate the percentage reduction in CPU blocking time achieved by offloading the task to DMA.
- Measure the pulse width of
5️⃣ Part 4: Buffer Overrun Detection & Recovery
If an incoming stream exceeds 32 bytes without a timeout:
DMA1_Channel1->CNDTRwill count down to 0, and the DMA engine will automatically stop writing to memory (preventing memory corruption).- Detect this condition:
- Enable the Transfer Complete interrupt on
DMA1_Channel1(DMA_TCIE), which fires when exactly 32 bytes have arrived.
- Enable the Transfer Complete interrupt on
- When an overrun occurs:
- Flag the overrun condition.
- Transmit an error message over the serial port:
"[ERROR: RX Buffer Overrun (>32 Bytes)]". -
Clear the buffer and re-arm
DMA1_Channel1withCNDTR = BUFFER_SIZE.