π 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 (
TIM3andTIM4) to generate PWM waveforms and analyze the phase displacement caused by software start latency. -
Configure
TIM15in Input PWM Mode (as practiced in Assignment #3) to measure incoming signal period and duty cycle using a single input pin. -
Use
TIM15as a Master Trigger (TRGO) in Compare Pulse Mode to lock bothTIM3andTIM4in 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
PB4and Channel 2 onPB6. -
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
TIM15to 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:
PC1mapped toTIM15_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 (
CC1EandCC2E). -
Enable interrupts only for Channel 2 (
CC2IE = 1) and configure the NVIC vectorTIM15_IRQn.
2.3 - ISR & Telemetry
In TIM15_IRQHandler:
-
On
\[\text{PW} = CCR2 - CCR1\] \[\text{Period} = CCR2 - \text{lastFallingTicks}\]CC2IF, compute the pulse width and period: -
Update
lastFallingTicks = CCR2and clearCC2IF. -
In your main loop, use your SysTick-based
Stopwatchto print the measured period and duty width in microseconds to the debug console every 500 ms. TogglePA5as an activity heartbeat.
3οΈβ£ Part 3: Hardware Inter-Timer Synchronization (Reset Mode)
- Now, eliminate software latency by slaving both
TIM3andTIM4directly toTIM15.
+-----------------------------------------+ | 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 onTRGOwhenCC1IFis assertedβwhich occurs on the rising edge of the input signal onPC1:
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(TIM3PWM Out) -
Scope CH3:
PB6(TIM4PWM Out)
-
Deliverables & Analysis Questions
-
Question4.1 (Phase Coherence): Are
TIM3andTIM4now locked in phase with each other and with the external signal onPC1? 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
TIM3andTIM4stop outputting PWM, or do they continue running? Explain why based on how Reset Mode operates in the absence of trigger edges.
-