Assignment #6: High-Throughput UART with DMA, RTO & Oscilloscope Profiling

📚 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:

  1. Configure dual-channel DMA transfers: Peripheral-to-Memory (Rx) and Memory-to-Peripheral (Tx).
  2. Interface the USART Receiver Timeout (RTO) unit with DMA reception to process variable-length frames without CPU byte polling.
  3. Detect and recover from a buffer overrun condition whenever an incoming payload exceeds the designated buffer boundary.
  4. 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_SIZE as 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

  1. Configure SysTick to trigger an interrupt every 1 ms.
  2. Every 250 ms, transmit the fixed string "Hello CMPE2250\r\n" (16 bytes) via USART2 using DMA (DMA1_Channel2).
  3. GPIO Profiling on PA6:
    • Drive PA6 HIGH immediately before triggering the DMA transmission.
    • Enable the DMA Transfer Complete Interrupt (TCIE) on DMA1_Channel2.
    • Clear the interrupt flag in DMA1->IFCR and drive PA6 LOW inside DMA1_Channel2_3_IRQHandler().
  4. Connect the oscilloscope:
    • Channel 1 (Yellow): Pin PA6 (Profiling Pin)
    • Channel 2 (Blue): Pin PA2 (USART2_TX)
  5. In your report, address the following theoretical questions:
    • What does the pulse period of PA6 represent?
    • What does the pulse width (HIGH time) of PA6 represent?
    • 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 PA6 against the theoretical calculation and account for any small discrepancies.

3️⃣ Part 2: DMA Reception with RTO (Normal Packet Echo)

  1. Initialize DMA1_Channel1 for Peripheral-to-Memory (USART2->RDR $\rightarrow$ rxBuffer) with CNDTR = BUFFER_SIZE.
  2. 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 RTOEN in USART2->CR2 and RTOIE in USART2->CR1.
  3. 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}\)
  4. Echo the packet back to the host prefixed with "CMPE2250: <received_string>\r\n".
  5. Clear and re-arm DMA1_Channel1 for 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:

  1. Blocking Mode (#undef ReplyDMA):
    • Drive PA5 HIGH.
    • Transmit the prefix and payload using blocking polled loops (UART_TxStr / UART_TxByte).
    • Drive PA5 LOW.
  2. DMA Non-Blocking Mode (#define ReplyDMA):
    • Drive PA5 HIGH.
    • Assemble the reply in txBuffer and start transmission using UART_TxBufferDma().
    • Drive PA5 LOW immediately after starting the channel.
  3. Configure your oscilloscope for a Single-Shot Trigger on PA5:
    • Measure the pulse width of PA5 under both modes.
    • Calculate the percentage reduction in CPU blocking time achieved by offloading the task to DMA.

5️⃣ Part 4: Buffer Overrun Detection & Recovery

If an incoming stream exceeds 32 bytes without a timeout:

  1. DMA1_Channel1->CNDTR will count down to 0, and the DMA engine will automatically stop writing to memory (preventing memory corruption).
  2. Detect this condition:
    • Enable the Transfer Complete interrupt on DMA1_Channel1 (DMA_TCIE), which fires when exactly 32 bytes have arrived.
  3. 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_Channel1 with CNDTR = BUFFER_SIZE.