/* GPIO HAL: portable driver for Aurix TC3xx Port module */
#include
#include "Std_Types.h"
/* Port register structure (Aurix P00..P34) */
typedef struct {
volatile uint32_t OUT; /* +0x00: output register */
volatile uint32_t OMR; /* +0x04: output modification register (atomic set/clr) */
volatile uint32_t reserved08[2];
volatile uint32_t IOCR0; /* +0x10: I/O control 0–3 */
volatile uint32_t IOCR4; /* +0x14: I/O control 4–7 */
volatile uint32_t IOCR8; /* +0x18: I/O control 8–11 */
volatile uint32_t IOCR12; /* +0x1C: I/O control 12–15 */
volatile uint32_t reserved20[4];
volatile uint32_t IN; /* +0x30: input register */
} Port_Regs_t;
#define PORT0_BASE 0xF003A000u
#define PORT(n) ((Port_Regs_t *)(PORT0_BASE + (n) * 0x100u))
/* Aurix OMR: PS (set) bits [7:0]; PCL (clear) bits [23:16] */
#define PORT_OMR_SET(pin) (1u << (pin))
#define PORT_OMR_CLR(pin) (1u << ((pin) + 16u))
void Gpio_SetOutput(uint8_t port, uint8_t pin) {
PORT(port)->OMR = PORT_OMR_SET(pin); /* atomic set via OMR */
}
void Gpio_ClearOutput(uint8_t port, uint8_t pin) {
PORT(port)->OMR = PORT_OMR_CLR(pin); /* atomic clear via OMR */
}
uint8_t Gpio_ReadInput(uint8_t port, uint8_t pin) {
return (uint8_t)((PORT(port)->IN >> pin) & 0x01u);
}
void Gpio_ToggleOutput(uint8_t port, uint8_t pin) {
/* Set and clear in same write: toggle */
PORT(port)->OMR = PORT_OMR_SET(pin) | PORT_OMR_CLR(pin);
} GPIO Driver: HAL Layer Pattern
STM Timer Driver
/* Aurix STM (System Timer Module): 64-bit free-running counter */
/* 300 MHz → 1 tick = 3.33 ns; wraps after ~1.95 years */
#include
#define STM0_BASE 0xF0001000u
typedef struct {
volatile uint32_t CLC; /* +0x00: Clock Control */
volatile uint32_t reserved04;
volatile uint32_t ID; /* +0x08: Module ID */
volatile uint32_t reserved0C;
volatile uint32_t TIM0; /* +0x10: Timer register 0 (lower 32 bits) */
volatile uint32_t TIM1; /* +0x14: Timer register 1 (bits 32-63) */
volatile uint32_t TIM2; /* +0x18: Timer register 2 (bits 4-35) */
volatile uint32_t TIM3; /* +0x1C: Timer register 3 (bits 8-39) */
volatile uint32_t TIM4; /* +0x20: Timer register 4 (bits 12-43) */
volatile uint32_t TIM5; /* +0x24: Timer register 5 (bits 16-47) */
volatile uint32_t TIM6; /* +0x28: Timer register 6 (upper 32 bits) */
volatile uint32_t CAP; /* +0x2C: Capture register */
} Stm_Regs_t;
#define STM0 ((Stm_Regs_t *)STM0_BASE)
#define STM_TICKS_PER_US 300u /* 300 MHz */
#define STM_TICKS_PER_MS 300000u
uint32_t Stm_GetTick(void) { return STM0->TIM0; }
void Stm_DelayUs(uint32_t us) {
uint32_t start = Stm_GetTick();
uint32_t ticks = us * STM_TICKS_PER_US;
while ((Stm_GetTick() - start) < ticks) { /* busy wait */ }
}
boolean Stm_HasElapsed(uint32_t start_tick, uint32_t period_us) {
return (boolean)((Stm_GetTick() - start_tick) >= (period_us * STM_TICKS_PER_US));
} Exercise: Software PWM LED Dimmer
/* Software PWM using STM timer: 1 kHz PWM, 0–100% duty cycle */
#include
#include "gpio_driver.h"
#include "timer_driver.h"
#define PWM_PERIOD_US 1000u /* 1 ms = 1 kHz */
#define LED_PORT 2u
#define LED_PIN 4u
static uint8_t g_duty_pct = 50u; /* 0–100% */
static uint32_t g_pwm_start_tick;
static boolean g_phase_high;
void Pwm_Init(void) {
Gpio_SetOutputMode(LED_PORT, LED_PIN);
g_pwm_start_tick = Stm_GetTick();
g_phase_high = TRUE;
Gpio_SetOutput(LED_PORT, LED_PIN);
}
void Pwm_MainFunction(void) { /* called every 100 µs from OS task */
uint32_t elapsed_us = (Stm_GetTick() - g_pwm_start_tick) / STM_TICKS_PER_US;
if (g_phase_high && (elapsed_us >= g_duty_pct * (PWM_PERIOD_US / 100u))) {
Gpio_ClearOutput(LED_PORT, LED_PIN); /* end of ON phase */
g_phase_high = FALSE;
}
if (elapsed_us >= PWM_PERIOD_US) {
Gpio_SetOutput(LED_PORT, LED_PIN); /* start new cycle */
g_pwm_start_tick = Stm_GetTick();
g_phase_high = TRUE;
}
}
void Pwm_SetDuty(uint8_t duty_pct) {
g_duty_pct = (duty_pct > 100u) ? 100u : duty_pct;
} Summary
The GPIO and STM driver exercises cover the two most fundamental hardware driver patterns in embedded C: atomic register modification (using OMR set/clear in a single write) and free-running timer for timing measurement. The software PWM demonstrates the non-blocking periodic pattern: Pwm_MainFunction() checks elapsed time and updates the pin state, but never blocks — it's safe to call from a periodic OS task. This pattern (compute elapsed time → update state → return) is the foundation of all AUTOSAR runnable implementations.
🔬 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.