Assignment #4: Multi-Timer Synchronization, Input PWM, and Hardware Reset Mode

πŸ“š Assignment 4 CMPE2250: Multi-Timer Synchronization, Input PWM, and Hardware Reset Mode

πŸ“‹ Overview

  • In real-world embedded applications such as multi-phase motor drives, power electronics, and radar systems, multiple timer channels must operate in lockstep. However, enabling timers sequentially in software inevitably introduces phase displacement and jitter.

  • To eliminate this, STM32 timers can be coupled in hardware via the internal trigger bus (TRGO $\rightarrow$ ITR).

  • In this assignment, you will:

    • Configure two independent timers (TIM3 and TIM4) to generate PWM waveforms and analyze the phase displacement caused by software start latency.

    • Configure TIM15 in Input PWM Mode (as practiced in Assignment #3) to measure incoming signal period and duty cycle using a single input pin.

    • Use TIM15 as a Master Trigger (TRGO) in Compare Pulse Mode to lock both TIM3 and TIM4 in Reset Mode, synchronizing their counter baselines in silicon.

    • Experimentally analyze what happens to the slave PWM waveforms under dynamic trigger frequency variations and signal loss.


1️⃣ Part 1: Independent PWM Generation & Phase Displacement

Configure TIM3 and TIM4 to generate periodic PWM outputs on separate GPIO pins without hardware synchronization.

1.1 - System Clock & Timer Configuration

  • Ensure your system clock is configured to 50 MHz (PLL_50MHZ).

  • Configure TIM3:

    • Tick rate: 1 Β΅s ($PSC = 50 - 1$).

    • Period: 5 ms ($ARR = 5000$).

    • Channel: CH1 on PB4 (Alternate Function AF1), configured in PWM Mode 1.

    • Duty Cycle: 4% ($CCR1 = 200$).

  • Configure TIM4:

    • Tick rate: 1 Β΅s ($PSC = 50 - 1$).

    • Period: 5 ms ($ARR = 5000$).

    • Channel: CH1 on PB6 (Alternate Function AF9), configured in PWM Mode 1.

    • Duty Cycle: 6% ($CCR1 = 300$).

  • Enable both timers sequentially in software.

1.2 - Oscilloscope Observation & Questions

  • Connect an oscilloscope with Channel 1 on PB4 and Channel 2 on PB6.

  • Question 1.1: Are the two PWM signals perfectly in phase? Zoom into the rising edges: what is the measured time displacement between them?

  • Question 1.2: What causes this displacement when enabling timers in software?

  • Question 1.3: Is there anything you can do purely in software (e.g., pre-loading counter registers or disabling interrupts during setup) to minimize this offset? Attempt a software compensation and record the remaining displacement.


2️⃣ Part 2: Dual-Channel Input PWM Capture on TIM15

  • As covered in Assignment #3, configure TIM15 to measure the period and pulse width of an external incoming digital signal using dual-channel capture on a single pin.

2.1 - Pin & Timer Setup

  • Input Pin: PC1 mapped to TIM15_CH1 (Alternate Function AF2).

  • Prescaler: Tick rate of 1 Β΅s ($PSC = 50 - 1$).

  • Counter Range: Full 16-bit range ($ARR = 0xFFFF$ or unconstrained).

2.2 - Input PWM Configuration

  • Channel 1: Map to TI1, sensitive to rising edges (CC1S = 01b, CC1P = 0).

  • Channel 2: Map to TI1, sensitive to falling edges (CC2S = 10b, CC2P = 1).

  • Enable both capture channels (CC1E and CC2E).

  • Enable interrupts only for Channel 2 (CC2IE = 1) and configure the NVIC vector TIM15_IRQn.

2.3 - ISR & Telemetry

In TIM15_IRQHandler:

  • On CC2IF, compute the pulse width and period:

    \[\text{PW} = CCR2 - CCR1\] \[\text{Period} = CCR2 - \text{lastFallingTicks}\]
  • Update lastFallingTicks = CCR2 and clear CC2IF.

  • In your main loop, use your SysTick-based Stopwatch to print the measured period and duty width in microseconds to the debug console every 500 ms. Toggle PA5 as an activity heartbeat.


3️⃣ Part 3: Hardware Inter-Timer Synchronization (Reset Mode)

  • Now, eliminate software latency by slaving both TIM3 and TIM4 directly to TIM15.
+-----------------------------------------+
|                 TIM15                   |
PC1 (Signal In) ─>| TI1 -> CH1 Capture (Rising Edge)        |
|     -> CH2 Capture (Falling Edge)       |
| TRGO: Compare Pulse (MMS = 011b)        |
+────────────────────┬────────────────────+
β”‚
Internal Trigger Bus (ITR2)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β–Ό                             β–Ό
+──────────────────+          +──────────────────+
|      TIM3        |          |      TIM4        |
| TS:  ITR2        |          | TS:  ITR2        |
| SMS: Reset Mode  |          | SMS: Reset Mode  |
| PB4 (PWM Out 4%) |          | PB6 (PWM Out 6%) |
+──────────────────+          +──────────────────+

3.1 - Master Configuration (TIM15)

  • According to the Reference Manual, setting MMS = 011b (Compare Pulse) sends a positive pulse on TRGO when CC1IF is assertedβ€”which occurs on the rising edge of the input signal on PC1:
TIM15->CR2 &= ~TIM_CR2_MMS_Msk;
TIM15->CR2 |= (0b011 << TIM_CR2_MMS_Pos); // Compare pulse on CC1 event

3.2 - Slave Configuration (TIM3 & TIM4)

  • Configure the Slave Mode Control Register (SMCR) for both timers:

    • Trigger Source: Set TS to ITR2 (00010b), which routes TIM15_TRGO to both TIM3 and TIM4.

    • Slave Mode: Set SMS to Reset Mode (0100b). When a trigger edge arrives on ITR2, the slave timer counter (CNT) re-initializes to 0 and forces a register update.

4️⃣ Verification & Dynamic Signal Analysis

  • Feed a variable square wave from your function generator into PC1 (Start with a 5 ms period, 50% duty cycle).

  • Probe the following signals simultaneously:

    • Scope CH1: PC1 (Master Input Signal)

    • Scope CH2: PB4 (TIM3 PWM Out)

    • Scope CH3: PB6 (TIM4 PWM Out)

Deliverables & Analysis Questions

  • Question4.1 (Phase Coherence): Are TIM3 and TIM4 now locked in phase with each other and with the external signal on PC1? What is the new measured displacement?

  • Question 4.2 (Period Reduction / Truncation):

    • Change the function generator input period from 5 ms down to 2 ms, and then to 1 ms.

    • What happens to the output waveforms on PB4 and PB6? Explain why the counters behave this way when reset before reaching their own $ARR$ ($5000$).

  • Question 4.3 (Period Increase):

    • Increase the generator input period to 10 ms (slower than the 5 ms slave ARR).

    • Describe the resulting behavior of TIM3 and TIM4. How many complete PWM cycles do they produce between trigger reset pulses?

  • Question 4.4 (Signal Loss):

    • Disconnect the input wire from PC1 completely.

    • Do TIM3 and TIM4 stop outputting PWM, or do they continue running? Explain why based on how Reset Mode operates in the absence of trigger edges.