TIM2 PWM Reference Chapter
STM32F103 TIM2 PWM path, pin truth, and compact register subset
Contents
- Official reference basis
- PWM generation path
- Pin and clock routing
- Register map and simulated subset
- Register descriptions
- Student register guide and code examples
1 Official reference basis
| Source | Relevant item | Use in this chapter |
|---|---|---|
| STM32F103x8/xB datasheet DS5319 Rev 20 | Table 5, alternate functions | Pin truth: PA0 = TIM2_CH1_ETR, PA2 = TIM2_CH3, PA15 can be TIM2_CH1_ETR after remap. |
| STM32F10xxx reference manual RM0008 Rev 21 | RCC section 7.3.8 | RCC->APB1ENR.TIM2EN gates TIM2. |
| RM0008 Rev 21 | Timer section 15, PWM mode and registers | PSC, ARR, CNT, CCMR1.OC1M, CCMR1.OC1PE, CCR1, CCER.CC1E, and CR1.CEN are the real PWM control path. |
| RM0008 Rev 21 | GPIO section 9, alternate function output | Real PWM output requires the GPIO pin configured as alternate-function output, commonly push-pull for this route. |
2 PWM generation path
Figure 1. TIM2 PWM generation path and fixed PA2 trainer route
Source reconstruction from RM0008 Rev 21 Figure 126, Figure 127, Figure 130, timer PWM register text in Section 15, and DS5319 Rev 20 Table 5. Reconstructed drawing; no vendor PDF image was pasted.
In official PWM mode 1, the output reference is active while CNT < CCRx and inactive for the rest of the period, subject to polarity and output enable settings. In PWM mode 2, the active/inactive relation is inverted. With an up-counting timer, the counter clock is TIMxCLK / (PSC + 1), the PWM period is approximately (ARR + 1) / counter_clock, and the active-high duty ratio is approximately CCR / (ARR + 1).
The trainer does not implement a cycle-accurate PSC/ARR/CNT waveform engine. Instead, it stores the visible duty command in TIM2_CCR1 as 0..100 percent and renders an average PA2 / PWM_OUT value in the Pin, Graph, Schematic, Mission, and Log views. Read TIM2_CCR1 here as the trainer duty command, not as proof that PA2 is official TIM2_CH1. On real STM32F103 hardware, PA2 PWM belongs to TIM2_CH3 and would be controlled through the channel-3 compare path, including TIM2_CCR3.
3 Pin and clock routing
| Layer | Official STM32F103 fact | Current trainer contract |
|---|---|---|
| Clock | TIM2 is on APB1. Enable RCC->APB1ENR.TIM2EN. GPIOA and AFIO are on APB2. | The simulator gates the PWM effect on RCC_APB1ENR.TIM2EN and shows clock-gated states when it is not set. |
| GPIO AF | Real output compare pins must be configured through GPIO alternate-function output mode. | GPIOA_CRL PA2 configuration is displayed, but the route is a simplified trainer path. |
| Channel 1 pin | TIM2_CH1 is on PA0 by default and PA15 after remap. | The trainer still uses TIM2_CCR1 and TIM2_CCER.CC1E names for the compact duty/output gate. |
| PA2 pin | PA2 is TIM2_CH3 on the real STM32F103; official PA2 PWM would use TIM2_CCR3. | The board labels this as PA2 / PWM_OUT trainer route. The trainer's TIM2_CCR1 duty field is an alias for this route, not an official PA2 channel-1 mapping. |
4 Register map and simulated subset
| Register | Current use | Main role | Read it together with |
|---|---|---|---|
| RCC_APB1ENR | Use now | Turns on the TIM2 register block on APB1. | Figure 1 upper path and TIM2_CCER. |
| RCC_APB2ENR | Use now | Turns on AFIO and GPIOA for the pad-side route. | Section 3 pin path and GPIOA_CRL. |
| GPIOA_CRL | Use now | Places PA2 in alternate-function push-pull style for the trainer PWM route. | Section 3 route table and Figure 1 trainer route box. |
| TIM2_CCR1 | Use now | Stores the compact 0..100 duty command used by the trainer as a PA2 route alias. | Figure 1 compare path and Graph View PA2 / PWM_OUT. |
| TIM2_CCER | Use now | Bit 0, CC1E, gates the trainer PWM output alias. | Figure 1 output stage and Pin View PWM_OUT. |
| TIM2_CCMR1 | Reference-visible | Official PWM mode selection and preload control. | Figure 1 PWM path boxes OC1M and OC1PE. |
| TIM2_PSC / TIM2_ARR / TIM2_CNT | Reference-visible | Define timer rate, PWM period, and compare timing on real STM32F103 hardware. | Figure 1 waveform reference and compare relation CNT < CCR1. |
| TIM2_CR1 | Full STM32F103 example | Starts the counter and enables preload behavior in the full timer flow. | Figure 1 CEN + ARPE box. |
4.1 Compact trainer register map
| Offset | Register | Reset value | Access | Current use | Description |
|---|---|---|---|---|---|
| 0x00 | TIM2_CCER | 0x00000000 | R/W | Use now | Compact output enable gate. Bit 0 mirrors the role of CC1E for the trainer alias path. |
| 0x04 | TIM2_CCR1 | 0x00000000 | R/W | Use now | Trainer duty command in percent, clamped to 0..100. This is a compact PA2 route alias, not official PA2 TIM2_CCR3 hardware compare. |
5 Register descriptions
5.1 Capture / compare enable register (TIM2_CCER)
Trainer address offset: 0x00. Reset value: 0x00000000.
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Reserved | CC1E | ||||||||||||||||||||||||||||||
| - | R/W | ||||||||||||||||||||||||||||||
Bit 0 CC1E: Enables the trainer PWM output gate when set to 1 and the TIM2 clock is enabled. In this chapter it is the compact trainer gate for PA2 / PWM_OUT, not an official PA2 channel-1 gate.
5.2 Capture / compare register 1 (TIM2_CCR1)
Trainer address offset: 0x04. Reset value: 0x00000000.
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Reserved | DUTY[6:0] | ||||||||||||||||||||||||||||||
| - | R/W | ||||||||||||||||||||||||||||||
Bits 6:0 DUTY: Trainer duty command. 0 renders 0% and 100 renders 100% average output. Official PA2 channel-3 PWM would use TIM2_CCR3; the trainer keeps this value in TIM2_CCR1 as a compact alias.
6 Student register guide and code examples
6.1 Current trainer code example
- Enable RCC->APB2ENR.AFIOEN and RCC->APB2ENR.IOPAEN before relying on the GPIO/alternate-function path.
- Enable RCC->APB1ENR.TIM2EN before relying on TIM2 register effects.
- Use alternate-function push-pull as the official GPIO reference mode. The trainer observes the simplified PA2 / PWM_OUT route.
- Write the trainer duty command to TIM2_CCR1, then set TIM2_CCER.CC1E; this is the trainer alias for PA2 / PWM_OUT, not the official PA2 channel map.
RCC->APB2ENR |= (1u << 0); /* AFIOEN */
RCC->APB2ENR |= (1u << 2); /* IOPAEN */
RCC->APB1ENR |= (1u << 0); /* TIM2EN */
/* PA2 trainer route: use AF push-pull style setup as the hardware reference. */
GPIOA->CRL &= ~(0xFu << 8);
GPIOA->CRL |= (0xBu << 8);
TIM2->CCR1 = 65u; /* trainer duty percent */
TIM2->CCER |= (1u << 0); /* trainer output gate */
Read this flow as RCC gate -> GPIO route -> duty register -> output gate -> PA2 / PWM_OUT. The visible proof appears in Register View TIM2_CCR1/TIM2_CCER, Pin View PWM_OUT, and Graph View PA2 / PWM_OUT.
6.2 Full STM32F103 example when every timer register is available
- Use PSC and ARR to define timer frequency and period.
- Use CCMR1.OC1M to select PWM mode and CCMR1.OC1PE for preload.
- Use CCER.CC1E to open the channel output and CR1.CEN to run the counter.
- Keep the official pin truth separate: TIM2_CH1 is on PA0 or remapped PA15, while PA2 is TIM2_CH3.
RCC->APB2ENR |= (1u << 0); /* AFIOEN */
RCC->APB2ENR |= (1u << 2); /* IOPAEN */
RCC->APB1ENR |= (1u << 0); /* TIM2EN */
/* Official TIM2_CH1 example on PA0 */
GPIOA->CRL &= ~(0xFu << 0);
GPIOA->CRL |= (0xBu << 0); /* PA0 AF push-pull */
TIM2->PSC = 71u; /* 72 MHz / (71 + 1) = 1 MHz counter clock */
TIM2->ARR = 999u; /* 1 kHz PWM period */
TIM2->CCR1 = 250u; /* 25% duty */
TIM2->CCMR1 = (6u << 4) | (1u << 3); /* PWM mode 1, preload */
TIM2->CCER |= (1u << 0); /* CC1E */
TIM2->CR1 |= (1u << 7) | (1u << 0); /* ARPE | CEN */
This full example explains the official timer path behind Figure 1, but the current pasteable trainer code remains the shorter example above.
6.3 Simulator alignment notes
| View | Expected wording or behavior |
|---|---|
| Pin View | PA2 / PWM_OUT is described as a trainer route. It must not be labeled official TIM2_CH1. |
| Register View | RCC_APB1ENR.TIM2EN, TIM2_CCER, and TIM2_CCR1 show the compact contract. |
| Graph View | The PWM trace is average output voltage from the trainer duty command, not a period-accurate hardware PWM capture. |
| Schematic/Mission/Log | Clock-gated, GPIO configured, output enabled, duty changed, and motor-driver input states must agree with this chapter. |