| Component | Detail |
|---|---|
| Target | Cortex-M4 (STM32F407 or QEMU emulation) |
| Toolchain | arm-none-eabi-gcc; GNU ld linker script |
| Goal | PBL runs from 0x08000000; validates app at 0x08010000; jumps to it |
| App | LED blink at 0x08010000 (compiled separately with APP_BASE offset) |
Lab Scope: Bare-Metal PBL Skeleton
Exercise 1: PBL Main Loop
/* PBL: minimal C main — runs from flash 0x08000000 */
/* Application: must be at 0x08010000 (separate compile) */
#include
/* Boot flags in RAM (zero-initialised at startup) */
/* In production: stored in dedicated flash sector */
static volatile uint32_t g_reprog_flag;
#define REPROG_MAGIC 0xA5A5A5A5u
/* Application header at app base address */
typedef struct {
uint32_t magic; /* 0xDEADC0DE */
uint32_t length; /* bytes of application code */
uint32_t crc32; /* CRC-32 of [APP_BASE + 256 ... APP_BASE + length] */
uint8_t pad[244]; /* align to 256 bytes */
} AppHdr_t;
#define APP_BASE 0x08010000u
#define APP_MAGIC 0xDEADC0DEu
static uint32_t crc32_simple(const uint8_t *data, uint32_t len)
{
uint32_t crc = 0xFFFFFFFFu;
for (uint32_t i = 0; i < len; i++) {
crc ^= data[i];
for (int j = 0; j < 8; j++) {
crc = (crc & 1u) ? ((crc >> 1) ^ 0xEDB88320u) : (crc >> 1);
}
}
return ~crc;
}
static int app_is_valid(void)
{
const AppHdr_t *hdr = (const AppHdr_t *)APP_BASE;
if (hdr->magic != APP_MAGIC || hdr->length == 0 || hdr->length > 512*1024u)
return 0;
uint32_t crc = crc32_simple((const uint8_t *)(APP_BASE + 256u), hdr->length);
return (crc == hdr->crc32);
}
int main(void)
{
/* Reprogramming requested? */
if (g_reprog_flag == REPROG_MAGIC) {
g_reprog_flag = 0u;
/* In full implementation: init UART/CAN, wait for UDS */
while (1) { /* stub: hang in reprog mode */ }
}
/* Validate and jump to application */
if (app_is_valid()) {
__disable_irq();
/* Cortex-M VTOR relocation */
*(volatile uint32_t *)0xE000ED08u = APP_BASE;
const uint32_t *vec = (const uint32_t *)APP_BASE;
__asm__ volatile (
"msr msp, %0
" /* set main stack pointer */
"bx %1
" /* branch to app reset handler */
:: "r"(vec[0]), "r"(vec[1])
);
}
/* No valid app: hang (production: enter reprog mode) */
while (1) {}
} Exercise 3: Linker Script for Dual Image
/* Linker script for PBL at 0x08000000; App at 0x08010000 */
/* Build PBL with -T pbl.ld; build App with -T app.ld (same format with APP_BASE) */
MEMORY {
FLASH_PBL (rx) : ORIGIN = 0x08000000, LENGTH = 64K /* PBL */
FLASH_APP (rx) : ORIGIN = 0x08010000, LENGTH = 448K /* Application */
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 128K /* SRAM */
RAMCODE (rwx) : ORIGIN = 0x20010000, LENGTH = 32K /* Flash driver RAM */
}
SECTIONS {
/* PBL vector table at absolute start of PBL flash */
.isr_vector : {
KEEP(*(.isr_vector))
} > FLASH_PBL
.text : {
*(.text*)
*(.rodata*)
} > FLASH_PBL
/* Flash driver: copied to RAM at startup */
.ramcode : {
__ramcode_start__ = .;
*(.ramcode*)
__ramcode_end__ = .;
} > RAMCODE AT > FLASH_PBL /* LMA in flash; VMA in RAM */
.data : {
*(.data*)
} > RAM AT > FLASH_PBL
.bss : {
*(.bss*)
*(COMMON)
} > RAM
}Summary
The bare-metal PBL skeleton demonstrates the three essential functions: reprogramming flag detection, application CRC validation, and the ARM Cortex-M jump sequence (MSP initialisation + BX to reset handler). The linker script separation — PBL at 0x08000000, application at 0x08010000 — is what enables the PBL to validate and jump without knowing the application's internal structure. The .ramcode section (VMA in RAM, LMA in flash) is the standard linker pattern for flash driver functions that must run from RAM — the startup code copies the section from flash to RAM before calling main().
🔬 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.