// Lightweight cycle-counter profiling using Aurix STM
// No external tools required; works on production hardware
#include "IfxStm_reg.h"
typedef struct {
uint32 start;
uint32 end;
uint32 min_ticks;
uint32 max_ticks;
uint32 count;
uint64 total_ticks;
} ProfSlot_t;
extern ProfSlot_t g_profSlots[32];
#define STM_TICKS_PER_US 300u /* 300 MHz Aurix TC397 */
#define PROF_START(slot) (g_profSlots[slot].start = (uint32)MODULE_STM0.TIM0.U)
#define PROF_END(slot) do { uint32 _elapsed = (uint32)MODULE_STM0.TIM0.U - g_profSlots[slot].start; if (_elapsed < g_profSlots[slot].min_ticks) g_profSlots[slot].min_ticks = _elapsed; if (_elapsed > g_profSlots[slot].max_ticks) g_profSlots[slot].max_ticks = _elapsed; g_profSlots[slot].total_ticks += _elapsed; g_profSlots[slot].count++; } while(0)
/* Usage in application code:
PROF_START(0);
Can_MainFunction();
PROF_END(0);
TRACE32 watch:
Var.View g_profSlots[0..7]
→ shows min/max/avg execution time per slot in TRACE32 */Profiling with Aurix STM Cycle Counter
ETM-Based Execution Profiling
// ETM instruction profiling: automatic function time accounting
// Shows where CPU time is spent without modifying source code
// Enable ETM and run for profiling period
ETM.ON
ETM.DataTrace NONE
Trace.METHOD Analyzer
Trace.Size 128MB
Go
WAIT 5s // capture 5 seconds of normal operation
Break
// Generate function execution time histogram
Trace.Chart.Func // flame chart: shows call tree with time proportions
// Flat profile: time per function, sorted by % CPU
Trace.Statistics.Func /MAX // most time-consuming functions first
// Drill down: per-function call count and average time
Trace.Statistics.Func "Can_MainFunction" // details for one function
// Coverage: which lines were never executed?
Trace.Chart.sYmbol // source with executed (green) / not-executed (grey) lines
// Export profile data for offline analysis
Trace.SAVE.Func profile_20241015.csv // CSV: func_name, count, total_ns, min_ns, max_ns
// Identify excessive interrupt preemption
// Trace.Chart shows ISR entries as vertical bars interrupting task execution
// High ISR rate visible as narrow slivers in task timelineFinding the Hotspot: Step-by-Step
Problem: OsTask_10ms regularly exceeds 10 ms budget
Step 1: Enable STM profiling macros in each runnable
PROF_START(0) at entry; PROF_END(0) at exit for each of 8 runnables
→ Read g_profSlots in TRACE32 after 100 cycles
→ Result: Com_MainFunction: avg 2.1ms, max 8.3ms (budget: 0.5ms)
Step 2: ETM profiling of Com_MainFunction sub-functions
Go Com_MainFunction; Trace.Statistics.Func
→ Com_RxIndication called 847 times per cycle (expected: ≤20)
→ each call: 9.8µs
Step 3: Source inspection
Why is Com_RxIndication called 847× per 10ms cycle?
→ TRACE32 Trace.Find /Entry "Com_RxIndication"
→ Called from DMA transfer complete callback firing for each 8-byte CAN FIFO entry
→ FIFO not being drained: DMA misconfigured — triggers interrupt per FIFO entry
instead of per-FIFO-full
Fix: reconfigure DMA burst size from 8B to 64B; interrupt fires 8× less oftenSummary
STM cycle-counter macros provide lightweight, zero-overhead profiling that works on production hardware — the 32-bit STM counter wraps every 14 seconds at 300 MHz, sufficient for profiling any automotive task period. ETM-based profiling adds the full call tree and line-level coverage without any code modification, at the cost of requiring a trace-capable probe. The combination — STM macros for continuous integration, ETM for deep investigation — covers both CI regression and field issue diagnosis scenarios.
🔬 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.