/* ARM SysTick: 24-bit down-counter, RTOS tick source */
/* Aurix STM: 64-bit free-running (covered in GPIO lab) */
/* Aurix GTM (Generic Timer Module): full-featured for PWM/capture */
/* STM32-style TIM: General Purpose Timer — demonstrates ARM timer patterns */
typedef struct {
volatile uint16_t CR1; /* Control Register 1 */
volatile uint16_t CR2; /* Control Register 2 */
volatile uint16_t SMCR; /* Slave Mode Control */
volatile uint16_t DIER; /* DMA/Interrupt Enable */
volatile uint16_t SR; /* Status Register */
volatile uint16_t EGR; /* Event Generation */
volatile uint16_t CCMR1; /* Capture/Compare Mode 1 */
volatile uint16_t CCMR2; /* Capture/Compare Mode 2 */
volatile uint16_t CCER; /* Capture/Compare Enable */
volatile uint16_t CNT; /* Counter */
volatile uint16_t PSC; /* Prescaler */
volatile uint16_t ARR; /* Auto-Reload Register (period) */
volatile uint16_t reserved;
volatile uint16_t CCR1; /* Capture/Compare Register 1 */
volatile uint16_t CCR2; /* Capture/Compare Register 2 */
} Tim_Regs_t;
#define TIM2 ((Tim_Regs_t *)0x40000000u)
/* Timer period: f_timer = f_APB / (PSC+1); period = (ARR+1) / f_timer
Example: 72 MHz APB, 1 kHz tick:
PSC = 71 (divide by 72 → 1 MHz timer clock)
ARR = 999 (count 0→999, reload → 1 ms period) */Timer Modes in Automotive MCUs
PWM Output Generation
#include
/* Timer-based PWM: 1 kHz, 25% duty cycle on TIM2 CH1 */
/* TIM2 runs at 1 MHz (PSC=71 for 72 MHz APB1) */
/* ARR=999 → 1 kHz PWM frequency */
/* CCR1=250 → 25% duty (compare match at count=250) */
void Tim2_PwmInit(uint16_t duty_1000) /* duty_1000: 0–1000 = 0–100% */
{
/* 1. Enable TIM2 clock in RCC */
RCC_APB1ENR |= (1u << 0u); /* TIM2 clock enable */
/* 2. Configure timer: PSC, ARR */
TIM2->PSC = 71u; /* 72 MHz / 72 = 1 MHz timer clock */
TIM2->ARR = 999u; /* period: 1000 ticks = 1 ms = 1 kHz */
/* 3. Configure CH1 as PWM mode 1 (high while CNT < CCR1) */
TIM2->CCMR1 = (0x06u << 4u); /* OC1M = 0b110: PWM mode 1 */
TIM2->CCMR1 |= (1u << 3u); /* OC1PE: preload enable */
/* 4. Set duty cycle */
TIM2->CCR1 = (uint16_t)((duty_1000 * (TIM2->ARR + 1u)) / 1000u);
/* 5. Enable CH1 output and counter */
TIM2->CCER |= (1u << 0u); /* CC1E: capture/compare 1 enable */
TIM2->CR1 |= (1u << 0u); /* CEN: counter enable */
}
void Tim2_SetDuty(uint16_t duty_1000) {
TIM2->CCR1 = (uint16_t)((duty_1000 * (TIM2->ARR + 1u)) / 1000u);
/* CCR1 write is buffered via preload until next update event (ARR reload) */
} Input Capture: Measuring Signal Period
#include
/* Input capture: measure period of an external signal (e.g., wheel speed sensor) */
/* Each rising edge triggers capture → store CNT value in CCR */
static volatile uint16_t g_last_capture = 0u;
static volatile uint16_t g_period_ticks = 0u;
/* TIM3 CH1 input capture ISR */
void TIM3_IRQHandler(void)
{
if (TIM3->SR & (1u << 1u)) { /* CC1IF: capture event on CH1 */
uint16_t current = TIM3->CCR1; /* reading CCR clears CC1IF */
g_period_ticks = current - g_last_capture; /* period in timer ticks */
g_last_capture = current;
/* 16-bit subtraction handles wrap-around correctly */
}
}
/* Convert timer ticks to RPM */
/* Timer: 1 MHz clock; 1 pulse per tooth; 60 teeth on wheel */
#define TIMER_FREQ_HZ 1000000u
#define TEETH_PER_REV 60u
float get_wheel_rpm(void)
{
uint16_t ticks;
Critical_t s = Critical_Enter();
ticks = g_period_ticks;
Critical_Exit(s);
if (ticks == 0u) return 0.0f;
float freq_hz = (float)TIMER_FREQ_HZ / (float)ticks;
return freq_hz * 60.0f / (float)TEETH_PER_REV;
} Summary
Timer/counter programming covers three core use cases: PWM generation (motor control, LED dimming — uses Output Compare mode), input capture (speed measurement, pulse width measurement — uses Input Capture mode), and period timing (RTOS tick, task deadline monitoring — uses Auto-Reload + update interrupt). The critical detail for input capture on wheel speed sensors: 16-bit timer wrap-around is handled correctly by subtraction (current - last works even if the counter wrapped) because unsigned 16-bit subtraction produces the correct modular result. At very low speeds (long periods), a timeout must be added: if no capture event occurs within N ticks, report speed = 0.
🔬 Deep Dive — Core Concepts Expanded
This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.
Key principles to reinforce:
- Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
- Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
- Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.
🏭 How This Topic Appears in Production Projects
- Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
- Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
- Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.
⚠️ Common Mistakes and How to Avoid Them
- Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
- Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
- Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
- Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.
📊 Industry Note
Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.