General-purpose timerTIM2 PWM Reference Chapter

TIM2 PWM Reference Chapter

STM32F103 TIM2 PWM path, pin truth, and compact register subset

Document ID
TIM2-PWM-REF001
Revision
Rev 1.3
Trainer base
0x40000020
Primary refs
DS5319 / RM0008
This chapter focuses on the TIM2 PWM path used by the project. It explains why RCC_APB1ENR.TIM2EN, TIM2_CCER, TIM2_CCR1, GPIO alternate-function setup, and PWM_OUT all have to agree. Official STM32F103 TIM2 PWM uses the timer clock, time base, output compare mode, compare register, channel enable, and GPIO alternate-function output path. The current trainer exposes a compact observable subset: TIM2_CCER bit 0 enables output, TIM2_CCR1 stores a 0..100 duty command, and the rendered board output is the fixed PA2 / PWM_OUT trainer route. Per DS5319 Rev 20, Table 5, real PA2 is TIM2_CH3, not TIM2_CH1; official PA2 PWM would use the channel-3 path such as TIM2_CCR3, while this trainer intentionally keeps a compact TIM2_CCR1 alias for duty.

Contents

  1. Official reference basis
  2. PWM generation path
  3. Pin and clock routing
  4. Register map and simulated subset
  5. Register descriptions
  6. Student register guide and code examples

1 Official reference basis

SourceRelevant itemUse in this chapter
STM32F103x8/xB datasheet DS5319 Rev 20Table 5, alternate functionsPin truth: PA0 = TIM2_CH1_ETR, PA2 = TIM2_CH3, PA15 can be TIM2_CH1_ETR after remap.
STM32F10xxx reference manual RM0008 Rev 21RCC section 7.3.8RCC->APB1ENR.TIM2EN gates TIM2.
RM0008 Rev 21Timer section 15, PWM mode and registersPSC, ARR, CNT, CCMR1.OC1M, CCMR1.OC1PE, CCR1, CCER.CC1E, and CR1.CEN are the real PWM control path.
RM0008 Rev 21GPIO section 9, alternate function outputReal PWM output requires the GPIO pin configured as alternate-function output, commonly push-pull for this route.

2 PWM generation path

TIM2 PWM generation path and trainer PA2 output route

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

LayerOfficial STM32F103 factCurrent trainer contract
ClockTIM2 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 AFReal 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 pinTIM2_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 pinPA2 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

RegisterCurrent useMain roleRead it together with
RCC_APB1ENRUse nowTurns on the TIM2 register block on APB1.Figure 1 upper path and TIM2_CCER.
RCC_APB2ENRUse nowTurns on AFIO and GPIOA for the pad-side route.Section 3 pin path and GPIOA_CRL.
GPIOA_CRLUse nowPlaces PA2 in alternate-function push-pull style for the trainer PWM route.Section 3 route table and Figure 1 trainer route box.
TIM2_CCR1Use nowStores 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_CCERUse nowBit 0, CC1E, gates the trainer PWM output alias.Figure 1 output stage and Pin View PWM_OUT.
TIM2_CCMR1Reference-visibleOfficial PWM mode selection and preload control.Figure 1 PWM path boxes OC1M and OC1PE.
TIM2_PSC / TIM2_ARR / TIM2_CNTReference-visibleDefine timer rate, PWM period, and compare timing on real STM32F103 hardware.Figure 1 waveform reference and compare relation CNT < CCR1.
TIM2_CR1Full STM32F103 exampleStarts the counter and enables preload behavior in the full timer flow.Figure 1 CEN + ARPE box.

4.1 Compact trainer register map

OffsetRegisterReset valueAccessCurrent useDescription
0x00TIM2_CCER0x00000000R/WUse nowCompact output enable gate. Bit 0 mirrors the role of CC1E for the trainer alias path.
0x04TIM2_CCR10x00000000R/WUse nowTrainer 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.

313029282726252423222120191817161514131211109876543210
ReservedCC1E
-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.

313029282726252423222120191817161514131211109876543210
ReservedDUTY[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

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

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

ViewExpected wording or behavior
Pin ViewPA2 / PWM_OUT is described as a trainer route. It must not be labeled official TIM2_CH1.
Register ViewRCC_APB1ENR.TIM2EN, TIM2_CCER, and TIM2_CCR1 show the compact contract.
Graph ViewThe PWM trace is average output voltage from the trainer duty command, not a period-accurate hardware PWM capture.
Schematic/Mission/LogClock-gated, GPIO configured, output enabled, duty changed, and motor-driver input states must agree with this chapter.
TIM2 compact PWM subsetTIM2-PWM-REF001 Rev 1.3