learner@ksa:~/gl-e01$

>_ Kernel Systems Academy

ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP

Embedded C, BSP, firmware, and Linux bring-up engineers

Outcome: Construct an inspected ARMv7-A bare-metal image, establish its C runtime, and test a modular GPIO BSP without confusing host-model evidence with board proof.

6 modules18 lessons6 labsLinux 6.12 · C11
  1. m01/ Cross-compilation workbench, ARM EABI, and schematics
  2. m02/ Cortex-A7 modes, CP15, and ARMv7 assembly
  3. m03/ IOMUXC muxing, pad electrical policy, and GPIO MMIO
  4. m04/ Boot ROM selection, IVT geometry, and DCD command streams
  5. m05/ C runtime startup, linker placement, and peripheral headers
  6. m06/ Modular BSP, transistor-switched buzzer, and polled key capstone
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP1/523

m01/

>_ Kernel Systems Academy

Cross-compilation workbench, ARM EABI, and schematics

  1. 1.1 Host–Target Workbench and the i.MX6ULL SoC
  2. 1.2 ARM Hard-Float EABI, ELF Sections, and Object Inspection
  3. 1.3 Schematics, Active-Low Circuits, and Register Maps

lab → lab_m01_eabi_workbench

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP2/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host–Target Workbench and the i.MX6ULL SoC

free preview20 min

What you'll learn

  • Trace host, transport, RAM, and CPU boundaries in a bring-up experiment.
  • Diagnose serial, TFTP, and NFS failures without conflating their layers.
  • Record reproducible build evidence while identifying untested target behavior.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP3/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

  • When you compile and run ./hello on a desktop workstation, one computer performs every role: your `x86_64…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP4/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

  • What physically sits inside that i.MX6ULL package alongside the Cortex-A7 CPU?
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP5/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP6/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

  • Because every hardware block along that chain can fail independently, the Cortex-A7 core can fetch and ex…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP7/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

fig_m01_l01_a.svg

Figure 1.1a: Host-to-target workbench architecture — separating x86_64 LP64 compilation, three independent transport channels, and the internal silicon blocks of the i.MX6ULL SoC.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP8/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Mental model

note: Bringing up a bare-metal board from a workstation is like machining turbine blades in a CNC shop (build host), shipping the crate (TFTP transport), staging it on the plant floor (target RAM), and spinning the rotor (Cortex-A7 execution): a signed shipping receipt proves the bytes arrived intact, never that the rotor balanced or generated power.

Where it stops working: Unlike physical freight, host LP64 and target ILP32 compilers disagree on pointer and long widths before shipping, and writing to the wrong target RAM address can silently overwrite the live bootloader receiving the transfer.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP9/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Cross-Compilation Host, Freestanding Target & Bring-Up Planes

definition: A cross compiler executes on the build host (x86_64 LP64 workstation) while emitting machine instructions and binary data layouts for a distinct execution target (32-bit ARMv7-A ILP32 Cortex-A7). In a freestanding target image (-ffreestanding -nostdlib), no OS loader maps virtual addresses, zeroes .bss, or implements POSIX system calls. During bring-up, asynchronous UART serial (115200 8N1 character observation), TFTP (UDP block transfer into target RAM under U-Boot), and NFS (remote VFS filesystem mount under a running Linux kernel) operate on separate hardware and protocol layers with independent failure modes.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP10/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

  • Step through the four workbench boundaries below to see exactly what each observation proves—and what remai…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP11/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Host vs Target Machines and the i.MX6ULL Silicon Graph

Step 1 (Build Host): C11 host verifiers prove math and struct layout; the cross toolchain emits EM_ARM ELF32 and raw binary artifacts.

Identify which hardware or software boundary each workbench observation actually tests.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP12/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Isolating UART, TFTP, and NFS Transport Planes

  • Why do bring-up engineers wire both a 3.3 V USB-to-UART adapter and an isolated lab Ethernet cable to…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP13/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Isolating UART, TFTP, and NFS Transport Planes

  • UART Serial Console (115200 8N1, 3.3 V TTL): A two-wire (TX, RX + GND) asynchronous character stream. Always verify the schematic's logic voltage (3.3 V vs 1.8 V—never RS-232 +/-12 V!) and ground continuity before connecting. Silence on UART1 means the console port, baud rate, IOMUXC pad mux, UART clock gate, or early boot path failed; it says nothing** about Ethernet health.
  • TFTP Image Download (UDP port 69 in U-Boot 2025.x): Once a network-capable bootloader (U-Boot) is running in initialized DDR3 RAM with a working ENET driver, tftpboot ${loadaddr} bsp.bin copies raw file bytes from the host TFTP export directory into physical target RAM** at ${loadaddr} (0x87800000). TFTP cannot run on a blank bare-metal CPU without a network stack, and a passing crc32 proves only byte-identical delivery—not a valid ARM entry point or stack!
  • NFS Root Filesystem (RPC / TCP port 2049): Mounts an exported host folder into a running Linux kernel** Virtual Filesystem (nfsroot=). NFS does not transfer bare-metal .bin images into OCRAM or DDR3; restrict exports strictly to your isolated lab subnet (192.0.2.0/24) rather than disabling host firewalls globally.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP14/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Isolating UART, TFTP, and NFS Transport Planes

  • Study the U-Boot 2025.x transfer sequence below from the target's perspective.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP15/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

workbench.uboot

learner@ksa: ~/gl-e01/m01-l01
learner@ksa:~/gl-e01/m01-l01$ bat workbench.uboot File: workbench.uboot    # Example isolated lab assignments; adapt to your lab network.    setenv ipaddr 192.0.2.20    setenv serverip 192.0.2.10    setenv loadaddr 0x87800000    printenv ipaddr serverip loadaddr    # Run only after checking target RAM ownership and server policy.    tftpboot ${loadaddr} bsp.bin    crc32 ${loadaddr} ${filesize}    # Downloading is not permission to jump to an uninspected image.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP16/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Locking LP64 vs ILP32 Struct Widths and Window Bounds in C11

  • Throughout GL-E01, every lesson pairs real ARMv7-A target code with a native **strict ISO C11 host ve…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP17/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Locking LP64 vs ILP32 Struct Widths and Window Bounds in C11

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP18/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Locking LP64 vs ILP32 Struct Widths and Window Bounds in C11

fig_m01_l01_b.svg

Figure 1.1b: Locking 16-byte cross-ABI descriptor layout with uint32_t (top) and preventing 32-bit modular wraparound across the i.MX6ULL physical address map (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP19/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

abi_workbench_audit.c

learner@ksa: ~/gl-e01/m01-l01
learner@ksa:~/gl-e01/m01-l01$ bat abi_workbench_audit.c File: abi_workbench_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stdalign.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>        struct armv7_load_desc {        uint32_t load_addr;        uint32_t entry_addr;        uint32_t byte_len;        uint32_t crc32_val;    };    _Static_assert(sizeof(struct armv7_load_desc) == 16U, "descriptor size");
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP20/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Executing the ABI and Placement Verifier

  • Compile and run abi_workbench_audit.c under -std=c11 -Wall -Wextra -Werror -pedantic.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP21/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Executing the ABI and Placement Verifier

learner@ksa: ~/gl-e01/m01-l01
learner@ksa:~/gl-e01/m01-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic abi_workbench_audit.c -o abi_workbench_auditlearner@ksa:~/gl-e01/m01-l01./abi_workbench_auditabi_desc=16B align=4B host_ptr=8B arm32_ptr=4B le=1 window=oklearner@ksa:~/gl-e01/m01-l01
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP22/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Executing the ABI and Placement Verifier

strace — learner@ksa: ~/gl-e01/m01-l01
learner@ksa:~/gl-e01/m01-l01strace -e trace=write,exit_group \./abi_workbench_auditwrite(1, "abi_desc=16B align=4B host_ptr=8"..., 62) = 62exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP23/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Evidence Boundaries, Wraparound Proofs, and Channel Trade-Offs

  • Look at the strace trace above (write(1, ..., 62) followed by exit_group(0) on an x86_64 host): tha…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP24/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Evidence Boundaries, Wraparound Proofs, and Channel Trade-Offs

  • Later: Lesson m01-l02 opens the compiled binary itself to inspect how ARM AAPCS32 passes integer and…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP25/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: A binary image header written by an x86_64 C host utility passes desktop unit tests, yet Cortex-A7 boot code reads 0x00000000 for entry_addr and jumps into upper zero padding. Root Cause: The host struct used unsigned long or size_t (8 bytes on LP64) instead of uint32_t (4 bytes on ILP32), doubling field offsets in the serialized byte stream. Fix: Declare every cross-target wire or image field with <stdint.h> fixed-width types (uint32_t, uint16_t) and enforce total byte size via _Static_assert.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP26/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: tftpboot ${loadaddr} bsp.bin prints Bytes transferred = 262144 cleanly, yet U-Boot freezes or resets before you even type go ${loadaddr}. Root Cause: loadaddr overlapped U-Boot's own relocated code, malloc heap, stack, or active page tables near the top of DDR3 because the address was picked without inspecting bdinfo. Fix: Run bdinfo on the U-Boot console first, select a non-overlapping payload window such as 0x87800000, and verify filesize fits before transferring.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP27/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Guided Practice: Auditing a Mixed-Width Header & OCRAM Span

  • Step 1 (Stride Calculation): On x86_64 (LP64), compute offsetof(struct bad_hdr, entry) and `sizeo…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP28/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Independent Practice: Multi-Region Target Memory Map Auditor (memmap_audit.c)

  • Write a standalone ISO C11 program memmap_audit.c encoding the i.MX6ULL bring-up map: Boot ROM worksp…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP29/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Quick check

In U-Boot, tftpboot 0x87800000 bsp.bin and crc32 succeed, but the serial console stays completely silent after jumping to 0x87800000. Which engineering conclusion is justified?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP30/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Quick check

Why does ram_window_fits(base, size, start, length) check start >= base first and then test length <= size - (start - base) instead of start + length <= base + size?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP31/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Quick check

Retrieval: what does a 0 exit code from the native x86_64 abi_workbench_audit binary prove?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP32/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Summary

  • Cross-compilation separates an LP64 build host (8-byte pointers/longs) from a 32-bit ILP32 freestanding Cortex-A7 target (4-byte pointers/longs, zero OS loader); lock shared binary structs to uint32_t.
  • Inside the i.MX6ULL SoC, Boot ROM and 128 KiB OCRAM (0x00900000) work immediately at reset, whereas external DDR3 (0x80000000+), CCM clocks, and IOMUXC pads require explicit initialization.
  • UART serial (115200 8N1 console), U-Boot TFTP (UDP RAM download), and Linux kernel NFS (VFS mount) test distinct layers with independent failure modes.
  • Guard 32-bit RAM placement windows by validating start >= base before comparing length <= size - (start - base), and distinguish host C11 proofs from target silicon execution.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP33/523

GL-E01 · 1.1 (m01-l01)

>_ Kernel Systems Academy

Key takeaways

  • A cross compiler runs on the build host and emits instructions for a different execution target.
  • Serial console, TFTP file transfer, and NFS filesystem access are distinct paths with separate failure modes.
  • The tested arithmetic satisfies the explicit RAM-window fixture.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP34/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

ARM Hard-Float EABI, ELF Sections, and Object Inspection

free preview20 min

What you'll learn

  • Identify ARM machine and floating-point ABI evidence in an ELF object.
  • Trace argument, return-value, and preserved-register obligations across an AAPCS call.
  • Classify initialized data and zero-initialized storage without confusing file and RAM size.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP35/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Why Binary Objects Need a Hardware Calling Convention

  • Once a C compiler translates float gain(float a, float b); into 32-bit ARMv7-A machine instructions,…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP36/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Why Binary Objects Need a Hardware Calling Convention

  • Trace both halves of the Cortex-A7 register contract in Figure 1.2a before reading a line of assembly:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP37/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Why Binary Objects Need a Hardware Calling Convention

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP38/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Why Binary Objects Need a Hardware Calling Convention

fig_m01_l02_a.svg

Figure 1.2a: AAPCS32 register ownership — integer caller/callee split (left) vs VFPv4 hard-float register passing and its bare-metal CPACR/FPEXC reset precondition (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP39/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Mental model

note: Two independently machined hydraulic manifolds can share an identical bolt pattern (matching C prototypes) yet route pressure into different ports (r0 vs s0), starving the actuator with zero external compiler warning.

Where it stops working: Unlike mechanical ports, ELF .ARM.attributes let linkers and readelf audit register conventions statically, yet even a perfectly matched hard-float binary will fault at runtime until privileged startup enables the VFP coprocessor in CP15.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP40/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

AAPCS32, Hard-Float EABI & ELF32 Section/Segment Roles

definition: AAPCS32 places the first four 32-bit integer/pointer arguments in r0..r3, requires callees to preserve r4..r11 and 8-byte-aligned SP, and under the hard-float variant (.eabi_attribute 28, 1, Tag_ABI_VFP_args: VFP registers) passes eligible floating-point values in s0..s15 / d0..d7. An Executable and Linkable Format (ELF32) object records EM_ARM (40) and .ARM.attributes metadata alongside .text (machine code), .rodata (constants), .data (initialized globals stored in the file), and SHT_NOBITS .bss (zero-initialized globals occupying zero file bytes whose p_memsz - p_filesz RAM extent startup code must explicitly zero).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP41/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Why Binary Objects Need a Hardware Calling Convention

  • Walk through the four-stage contract from C call site to runtime RAM layout below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP42/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Why Binary Objects Need a Hardware Calling Convention

Step 1 (C Call Site): Word arguments enter in r0..r3; hard-float variants pass eligible floats in VFPv4 registers s0..s15.

Never trust a .o or .bin filename: inspect ELF32 machine class, VFP ABI attributes, and NOBITS .bss expansion before execution.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP43/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Inspecting AAPCS32 Leaf Assembly, VFPv4 Attributes, and ELF Headers

  • Why does an ELF file have both Section Headers (.text, .rodata, .data, .bss, `.ARM.attribut…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP44/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Inspecting AAPCS32 Leaf Assembly, VFPv4 Attributes, and ELF Headers

  • Examine the two Cortex-A7 leaf functions in target.S below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP45/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m01-l02
learner@ksa:~/gl-e01/m01-l02$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .fpu neon-vfpv4    .eabi_attribute 28, 1    .arm    .text    .global add_words    .type add_words, %function    add_words:        add r0, r0, r1        bx lr    .size add_words, .-add_words    .global add_floats    .type add_floats, %function
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP46/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Auditing 32-Bit ELF Headers and NOBITS Segment Expansion in C11

  • Compare the exact byte offsets in Figure 1.2b against elf32_phdr_audit.c below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP47/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Auditing 32-Bit ELF Headers and NOBITS Segment Expansion in C11

fig_m01_l02_b.svg

Figure 1.2b: Exact Elf32_Ehdr / Elf32_Phdr byte offsets (top) and why runtime p_memsz (216 B) exceeds file-backed p_filesz (152 B) by the 64-byte SHT_NOBITS .bss tail (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP48/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

elf32_phdr_audit.c

learner@ksa: ~/gl-e01/m01-l02
learner@ksa:~/gl-e01/m01-l02$ bat elf32_phdr_audit.c File: elf32_phdr_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>        static uint16_t rd_le16(const unsigned char *p)    {        return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));    }    static uint32_t rd_le32(const unsigned char *p)    {        return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |               ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP49/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Cross-Assembling for Cortex-A7 and Verifying ELF Metadata

  • Run the native elf32_phdr_audit verifier first, then cross-assemble target.S with `clang --target=armv7…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP50/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Cross-Assembling for Cortex-A7 and Verifying ELF Metadata

learner@ksa: ~/gl-e01/m01-l02
learner@ksa:~/gl-e01/m01-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic elf32_phdr_audit.c -o elf32_phdr_auditlearner@ksa:~/gl-e01/m01-l02./elf32_phdr_auditELF32 EM_ARM=40 ehdr=52B phdr=32B filesz=152 memsz=216 bss=64learner@ksa:~/gl-e01/m01-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m01-l02readelf -h -A target.oELF Header:  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00   Class:                             ELF32  Data:                              2's complement, little endian  Version:                           1 (current)  OS/ABI:                            UNIX - System V  ABI Version:                       0  Type:                              REL (Relocatable file)  Machine:                           ARMlearner@ksa:~/gl-e01/m01-l02
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP51/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Cross-Assembling for Cortex-A7 and Verifying ELF Metadata

strace — learner@ksa: ~/gl-e01/m01-l02
learner@ksa:~/gl-e01/m01-l02strace -e trace=write,exit_group \./elf32_phdr_auditwrite(1, "ELF32 EM_ARM=40 ehdr=52B phdr=32"..., 62) = 62exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP52/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

File Payload vs Runtime RAM Footprint and Float-ABI Trade-Offs

  • Always archive the linked .elf and linker .map file beside any objcopy -O binary output!
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP53/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

File Payload vs Runtime RAM Footprint and Float-ABI Trade-Offs

  • Later: Lesson m01-l03 moves from binary contracts to hardware schematics, deriving active-low CMOS sink c…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP54/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: Bare-metal C code compiled with arm-linux-gnueabihf-gcc -O2 crashes with an Undefined Instruction exception inside main() even though the C source contains zero float or double variables! Root Cause: With -mfloat-abi=hard and -mfpu=neon-vfpv4 enabled by default on gnueabihf toolchains, the optimizer auto-vectorizes struct copies and memset loops using 64/128-bit vldr/vstr VFP/NEON instructions before _start enables CPACR (CP10/CP11) and FPEXC.EN. Fix: Compile early bare-metal boot/BSP code with -mfloat-abi=soft -mno-unaligned-access (or explicitly enable CP10/CP11 + FPEXC.EN in assembly before calling C).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP55/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: A custom bootloader reserves p_filesz bytes (152 B) in OCRAM for an ELF32 PT_LOAD segment, and running the loaded firmware immediately corrupts the stack or descriptor placed at p_vaddr + 152. Root Cause: The loader allocated runtime memory from p_filesz instead of p_memsz (216 B), forgetting the 64-byte SHT_NOBITS .bss region that occupies zero file bytes. Fix: Always reserve p_memsz bytes in target RAM, copy p_filesz bytes from p_offset, and zero [p_vaddr + p_filesz, p_vaddr + p_memsz).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP56/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Guided Practice: Decoding a Bare-Metal OCRAM Segment Header

  • Step 1 (Little-Endian Decode): Given Elf32_Phdr slices ph[8..11] = {0x00, 0x70, 0x90, 0x00}, `ph[16…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP57/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Independent Practice: Multi-Segment ELF32 Overlap & Entry Verifier (elf32_image_check.c)

  • Extend elf32_phdr_audit.c into elf32_image_check.c accepting e_entry (Elf32_Ehdr + 24) and an array…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP58/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Quick check

Caller control.o places a 32-bit float argument in r0, while callee pid.o reads its first float parameter from s0. What caused this silent numerical failure?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP59/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Quick check

Why does adding static uint8_t dma_desc[64]; (uninitialized) increase Elf32_Phdr.p_memsz by 64 bytes while leaving p_filesz unchanged?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP60/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Quick check

Retrieval: before transferring an object or executable to an ARMv7-A board, what proves it targets 32-bit ARM with the expected floating-point ABI?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP61/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Summary

  • AAPCS32 passes the first four 32-bit word arguments in caller-saved r0..r3, requires callees to preserve r4..r11 and 8-byte-aligned SP, and returns 32-bit scalars in r0.
  • The VFPv4 hard-float EABI (-mfloat-abi=hard, .eabi_attribute 28, 1) passes floats in s0..s15 / d0..d7, but faults on bare-metal Cortex-A7 reset until CPACR (CP10/CP11) and FPEXC.EN are enabled.
  • ELF32 stores code and constants in .text/.rodata and initialized variables in .data (p_filesz bytes), while SHT_NOBITS .bss adds p_memsz - p_filesz zero-initialized RAM bytes with zero file bloat.
  • Always retain .elf artifacts alongside stripped .bin images and verify ELFCLASS32, EM_ARM (40), and .ARM.attributes with readelf -h -A.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP62/523

GL-E01 · 1.2 (m01-l02)

>_ Kernel Systems Academy

Key takeaways

  • AAPCS assigns ordinary word-sized arguments to r0–r3 and requires preserved-register obligations across calls.
  • The hard-float procedure-call variant can pass eligible floating-point arguments in VFP registers.
  • Inspect ELF class, machine metadata, and relevant ABI attributes.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP63/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Schematics, Active-Low Circuits, and Register Maps

free preview20 minlab lab_m01_eabi_workbench

What you'll learn

  • Trace a carrier net through a core connector to its SoC pad.
  • Derive active-low output behavior from an LED current path.
  • Calculate register addresses and masks without altering sibling pins.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP64/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

CMOS Push-Pull Pads, Active-Low Sinking, and Kirchhoff Current Loops

  • Beginners often assume "writing 1 to a GPIO pin turns an LED on, and writing 0 turns it off." On indu…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP65/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

CMOS Push-Pull Pads, Active-Low Sinking, and Kirchhoff Current Loops

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP66/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

CMOS Push-Pull Pads, Active-Low Sinking, and Kirchhoff Current Loops

fig_m01_l03_a.svg

Figure 1.3a: Physical CMOS pad stage — why active-low sink circuits conduct I = (V_DD - V_F - V_OL) / R when DR = 0 (left) vs active-high sourcing when DR = 1 (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP67/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

CMOS Push-Pull Pads, Active-Low Sinking, and Kirchhoff Current Loops

  • Why do board designers prefer active-low sinking for status LEDs?
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP68/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Mental model

note: A schematic net label (LED0) is like a highway route number crossing county lines: the name can change at a border bridge (SOM-to-carrier connector), and the route name tells you connectivity—never which direction current flows or the pad's current limit.

Where it stops working: Unlike road traffic, electrical current requires a closed voltage loop (V_DD > V_F + V_OL), and shifting a signed 32-bit C integer into bit 31 invokes undefined behavior before a single electron moves.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP69/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Schematic Net Continuity, Active-Low Sink & Peripheral Offset Geometry

definition: A schematic net label joins electrically connected nodes on a drawing; tracing a signal from a carrier board load into a System-on-Module (SOM) requires following mating connector pin numbers (LED0 -> J1.14 -> P1.14 -> ball GPIO1_IO03), never guessing from text names. In an active-low sink circuit, logical assertion (on = true) drives the output bit to 0 (level_high = (on != active_low)). Each 32-bit MMIO register sits at physical address Base + Offset (GPIO1_DR = 0x0209C000 + 0x00, GPIO1_GDIR = 0x0209C000 + 0x04), where single-pin writes must preserve all 31 sibling bits.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP70/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

CMOS Push-Pull Pads, Active-Low Sinking, and Kirchhoff Current Loops

  • Trace the complete four-stage path from external LED load to 32-bit register bitmask below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP71/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

CMOS Push-Pull Pads, Active-Low Sinking, and Kirchhoff Current Loops

Step 1 (Load Circuit): 3.3 V -> resistor -> LED -> GPIO cathode conducts when the pin sinks low (active-low).

Never guess polarity or GPIO numbers from net names: trace Load -> Connector Pin -> SoC Ball -> Masked Base+Offset.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP72/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Tracing Carrier Nets Across Connectors to Physical GPIO Registers

  • Most production i.MX6ULL hardware splits into two schematics: a compact Core SOM (housing the SoC,…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP73/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Tracing Carrier Nets Across Connectors to Physical GPIO Registers

fig_m01_l03_b.svg

Figure 1.3b: Four-column schematic trace across board-to-board connectors (top) and 32-bit GPIO1 masked update geometry preserving all 31 sibling pins (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP74/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Tracing Carrier Nets Across Connectors to Physical GPIO Registers

  • Once GPIO1_IO03 maps to Bank GPIO1 (0x0209C000), local bit 3, examine how schematic_gpio.c tr…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP75/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Tracing Carrier Nets Across Connectors to Physical GPIO Registers

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP76/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

schematic_gpio.c

learner@ksa: ~/gl-e01/m01-l03
learner@ksa:~/gl-e01/m01-l03$ bat schematic_gpio.c File: schematic_gpio.c    #include <stdint.h>    #include <stdbool.h>    static volatile uint32_t *const gpio1_dr =        (volatile uint32_t *)(uintptr_t)0x0209c000U;    bool led_write(unsigned pin, bool on, bool active_low)    {        if (pin >= 32U) return false;        const uint32_t mask = UINT32_C(1) << pin;        const bool high = on != active_low;        uint32_t value = *gpio1_dr; /* exclusive bank ownership required */        value = high ? value | mask : value & ~mask;        *gpio1_dr = value;        return true;    }
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP77/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Verifying KVL Sink Headroom, Polarity XOR, and Shift Safety in C11

  • Never dereference (volatile uint32_t *)0x0209c000U in a host process (where 0x0209C000 is an unmapped v…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP78/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

sink_drive_audit.c

learner@ksa: ~/gl-e01/m01-l03
learner@ksa:~/gl-e01/m01-l03$ bat sink_drive_audit.c File: sink_drive_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>        static bool sink_current_ua(uint32_t vdd_mv, uint32_t vf_mv, uint32_t vol_mv,                                uint32_t r_ohm, uint32_t max_ua, uint32_t *out_ua)    {        if (r_ohm == 0U || vdd_mv <= vf_mv + vol_mv) return false;        uint32_t headroom_mv = vdd_mv - vf_mv - vol_mv;        uint32_t ua = (headroom_mv * 1000U) / r_ohm;        if (ua == 0U || ua > max_ua) return false;        *out_ua = ua;
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP79/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Running the Circuit Current and Mask Verifier

  • Execute sink_drive_audit on the workstation and cross-compile schematic_gpio.c into freestanding `ARMv7…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP80/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Running the Circuit Current and Mask Verifier

learner@ksa: ~/gl-e01/m01-l03
learner@ksa:~/gl-e01/m01-l03gcc -std=c11 -Wall -Wextra -Werror -pedantic sink_drive_audit.c -o sink_drive_auditlearner@ksa:~/gl-e01/m01-l03./sink_drive_auditsink_ua=1100 gdir=0x0209C004 dr_active_low_on=0xA5A5A5A5learner@ksa:~/gl-e01/m01-l03clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -mfloat-abi=soft -ffreestanding -std=c11 -Wall -Wextra -Werror -x c -c schematic_gpio.c -o target.olearner@ksa:~/gl-e01/m01-l03
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP81/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Running the Circuit Current and Mask Verifier

strace — learner@ksa: ~/gl-e01/m01-l03
learner@ksa:~/gl-e01/m01-l03strace -e trace=write,exit_group \./sink_drive_auditwrite(1, "sink_ua=1100 gdir=0x0209C004 dr_"..., 57) = 57exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP82/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

End-to-End Signal Provenance and Drive Topology Trade-Offs

  • Compare the four drive and register patterns in the matrix below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP83/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

End-to-End Signal Provenance and Drive Topology Trade-Offs

  • Later: Module 2 (m02-l01..m02-l03) dives inside the Cortex-A7 core itself—processor modes, banked regis…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP84/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Searching the core SOM schematic for carrier net name LED0 yields zero matches, leading an engineer to guess GPIO1_IO00 and toggle an unrelated board net. Root Cause: Net names are local to a schematic sheet or hierarchy unless linked by ports; across a SOM-to-carrier header (J1 pin 14 -> P1 pin 14), the same electrical conductor is renamed GPIO1_IO03. Fix: Always trace signals by physical connector pin numbers from load terminal to SoC ball before writing software.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP85/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Calling set_pin_level(&dr, 31U, true, false) triggers a signed-overflow sanitizer abort when the mask is written 1 << pin instead of UINT32_C(1) << pin. Root Cause: Integer literal 1 has type signed int, and left-shifting 1 into sign bit 31 overflows INT32_MAX (Undefined Behavior in ISO C11). Fix: Reject pin >= 32U first and form every 32-bit register mask from unsigned UINT32_C(1) << pin.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP86/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Guided Practice: Sizing a 1.8 V Rail Indicator & Pin 31 Masking

  • Step 1 (1.8 V Headroom): A board revision moves an active-low indicator onto a 1800 mV (1.8 V) bank…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP87/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Independent Practice: Worst-Case Tolerance & Netlist Auditor (board_led_spec.c)

  • Write a standalone ISO C11 verifier board_led_spec.c evaluating struct led_circuit across supply tole…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP88/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Quick check

An indicator branch runs from 3.3 V through a 1 kOhm resistor to an LED anode, with the LED cathode wired to GPIO1_IO03. Which register state illuminates the LED?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP89/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Quick check

A helper computes uint32_t mask = 1U << pin; if (pin >= 32U) return false;. Why is this C11 code defective?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP90/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Quick check

Retrieval: does computing 0x0209C000U + 0x04U == 0x0209C004U in a host C11 program map GPIO1_GDIR into the workstation process?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP91/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Lab: lab_m01_eabi_workbench

  • Complete the module studio lab lab_m01_eabi_workbench: repair the ELF32 EM_ARM header inspector, over…

Verify: ./check.sh exits 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP92/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Summary

  • Schematic net labels denote electrical conductors on a sheet; always trace across mating SOM-to-carrier connector pin numbers to identify the exact SoC ball and GPIO bank/bit.
  • Active-low sink circuits (V_DD -> R -> LED -> GPIO) conduct I = (V_DD - V_F - V_OL) / R when DR drives 0 (level_high = (on != active_low)).
  • Peripheral registers sit at Base + Offset (GPIO1_DR = 0x0209C000, GPIO1_GDIR = 0x0209C004); never dereference physical SoC addresses inside host user space.
  • Guard pin < 32U before evaluating UINT32_C(1) << pin and use masked read-modify-write under exclusive ownership so all 31 sibling pins remain intact.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP93/523

GL-E01 · 1.3 (m01-l03)

>_ Kernel Systems Academy

Key takeaways

  • A schematic net label names electrical connectivity rather than a software GPIO number.
  • An active-low LED connected from a supply through resistance to a GPIO conducts when the GPIO sinks current.
  • No; it identifies a target address, not a host mapping.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP94/523

m02/

>_ Kernel Systems Academy

Cortex-A7 modes, CP15, and ARMv7 assembly

  1. 2.1 Processor Modes, Register Banks, and CP15 Control
  2. 2.2 ARM Load/Store Addressing, Shifts, and Conditions
  3. 2.3 Branches, Full-Descending Stacks, and Calling C

lab → lab_m02_armv7_isa_sim

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP95/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Processor Modes, Register Banks, and CP15 Control

free preview20 min

What you'll learn

  • Classify all nine Cortex-A7 processor modes and their privilege boundaries.
  • Trace which register bank an exception handler actually modifies.
  • Decode CPSR controls and identify an MRC or MCR access to SCTLR.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP96/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Why Silicon Separates Processor Modes and Banked Register Storage

  • Ask a fundamental CPU design question: **what happens on the exact clock edge an external Ethernet or timer…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP97/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Why Silicon Separates Processor Modes and Banked Register Storage

  • ARMv7-A (AArch32 on Cortex-A7) solves both problems simultaneously using **nine architectural process…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP98/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Why Silicon Separates Processor Modes and Banked Register Storage

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP99/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Why Silicon Separates Processor Modes and Banked Register Storage

fig_m02_l01_a.svg

Figure 2.1a: Physical Cortex-A7 register banking across all nine modes — private SP/LR/SPSR protect exception entry, while shared r0..r7 (and r8..r12 outside FIQ) still require software stack saves.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP100/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Mental model

note: Switching processor modes on Cortex-A7 is like turning a master key on a control console: the top-bench tools (r0..r7) remain shared in plain sight, while the bottom emergency drawers (SP, LR, SPSR) physically mux in a private compartment for IRQ or SVC without copying a byte.

Where it stops working: Unlike mechanical drawers, unprivileged User mode cannot turn the mode key itself, User and System modes share the exact same drawer with zero SPSR, and changing CP15 system switches requires pipeline synchronization barriers to take effect cleanly.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP101/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

CPSR, Saved Program Status (SPSR) & CP15 System Control (SCTLR)

definition: In AArch32, CPSR holds arithmetic flags N, Z, C, V ([31:28]), async abort mask A ([8]), IRQ mask I ([7]), FIQ mask F ([6]), Thumb state T ([5]), and mode M[4:0] ([4:0]). Entering an exception copies CPSR into that mode's SPSR_<mode> (USR and SYS have no SPSR). Architectural caches, MMU, alignment faults, and vector bases live in Coprocessor 15 (CP15) registers—such as SCTLR (c1, c0, 0), accessed at >= PL1 via MRC (read) and MCR (write) bracketed by dsb sy and isb.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP102/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Why Silicon Separates Processor Modes and Banked Register Storage

  • Step through the four-stage resolution of mode, register bank, SPSR, and CP15 state below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP103/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Why Silicon Separates Processor Modes and Banked Register Storage

Step 1 (Current State): Decode CPSR M[4:0] and preserve NZCV [31:28] plus I/F/T [7:5] via ~0x1FU masking.

Always resolve active CPSR mode, physical register bank, and I/F masks before touching SP or CP15.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP104/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Decoding CPSR Control Lanes and Synchronizing CP15 SCTLR Writes

  • Inspect Figure 2.1b to see every bit slice inside CPSR and CP15 SCTLR (p15, 0, Rt, c1, c0, 0).
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP105/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Decoding CPSR Control Lanes and Synchronizing CP15 SCTLR Writes

fig_m02_l01_b.svg

Figure 2.1b: Exact 32-bit bitfields for CPSR (top) and CP15 SCTLR (bottom) with barrier-ordered control updates.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP106/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Decoding CPSR Control Lanes and Synchronizing CP15 SCTLR Writes

  • Why does SCTLR use special coprocessor instructions (mrc / mcr) plus dsb sy and isb instead of a…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP107/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m02-l01
learner@ksa:~/gl-e01/m02-l01$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .text    .global read_sctlr    read_sctlr:        mrc p15, 0, r0, c1, c0, 0        bx lr    .global enable_alignment_check    enable_alignment_check:        mrc p15, 0, r0, c1, c0, 0        orr r0, r0, #2        dsb sy        mcr p15, 0, r0, c1, c0, 0
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP108/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Modeling CPSR Mode Validity and Physical Bank Aliasing in C11

  • In cpsr_bank_decoder.c, we turn the architectural tables into an executable verifier.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP109/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

cpsr_bank_decoder.c

learner@ksa: ~/gl-e01/m02-l01
learner@ksa:~/gl-e01/m02-l01$ bat cpsr_bank_decoder.c File: cpsr_bank_decoder.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        enum {        MODE_USR = 0x10U, MODE_FIQ = 0x11U, MODE_IRQ = 0x12U,        MODE_SVC = 0x13U, MODE_MON = 0x16U, MODE_ABT = 0x17U,        MODE_HYP = 0x1aU, MODE_UND = 0x1bU, MODE_SYS = 0x1fU    };        static bool decode_cpsr(uint32_t cpsr, bool *priv, bool *has_spsr,                            bool *irq_m, bool *fiq_m, bool *thumb)    {
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP110/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Running the Mode Decoder and Cross-Assembling CP15 Accessors

  • Compile and run cpsr_bank_decoder.c, then cross-assemble target.S for Cortex-A7.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP111/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Running the Mode Decoder and Cross-Assembling CP15 Accessors

learner@ksa: ~/gl-e01/m02-l01
learner@ksa:~/gl-e01/m02-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic cpsr_bank_decoder.c -o cpsr_bank_decoderlearner@ksa:~/gl-e01/m02-l01./cpsr_bank_decoderCPSR=0xA00000D3 mode=0x13 I=1 F=1 T=0 SCTLR.A=1learner@ksa:~/gl-e01/m02-l01clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m02-l01
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP112/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Running the Mode Decoder and Cross-Assembling CP15 Accessors

strace — learner@ksa: ~/gl-e01/m02-l01
learner@ksa:~/gl-e01/m02-l01strace -e trace=write,exit_group \./cpsr_bank_decoderwrite(1, "CPSR=0xA00000D3 mode=0x13 I=1 F="..., 48) = 48exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP113/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Hardware Banking vs AAPCS Software Preservation

  • Keep hardware mode banking (CPSR.M[4:0] selecting private SP/LR) strictly separate from **softwar…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP114/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Hardware Banking vs AAPCS Software Preservation

  • Later: Lesson m02-l02 examines how SCTLR.V and CP15 VBAR compute exception vector addresses alongsi…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP115/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Early _start assembly executes msr cpsr_c, #0x13 to enter Supervisor mode, and a pending timer or GPIO interrupt immediately crashes the Cortex-A7 before SP_irq or VBAR is initialized. Root Cause: Writing literal 0x13 (0001_0011_2) into CPSR[7:0] cleared I (bit 7) and F (bit 6) to 0, unmasking IRQ and FIQ prematurely. Fix: Keep mask bits 0xC0 set during startup (0xD3 = I | F | MODE_SVC, or cpsid if, #0x13) until all exception stacks and vectors are ready.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP116/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Boot code clears SCTLR.C (bit 2, data cache enable) via mcr p15, 0, r0, c1, c0, 0 without cache maintenance, and global structs modified moments earlier silently revert to old DDR3 values. Root Cause: Clearing SCTLR.C stops cache lookups without writing dirty lines sitting inside L1/L2 data caches out to DDR3, and omitting dsb sy / isb leaves memory and pipeline state unsynchronized. Fix: Clean and invalidate affected data-cache levels first, issue dsb sy, write SCTLR, and execute isb.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP117/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Guided Practice: Decoding CPSR 0x80000192 & Multi-Mode SP Setup

  • Step 1 (Bitfield Decode): Decode CPSR = 0x80000192U into flags, masks, ISA state, mode, privilege, an…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP118/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Independent Practice: IRQ Entry & SPSR Return Simulator (mode_transition_sim.c)

  • Write a standalone ISO C11 verifier mode_transition_sim.c modeling a 31-word physical Cortex-A7 reg…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP119/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Quick check

An IRQ (0x12) assembly handler overwrites r0 without pushing it first. Why does this corrupt the interrupted User (0x10) program upon return?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP120/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Quick check

Which masked update switches CPSR mode bits M[4:0] held in r0 to Supervisor (0x13) without disturbing NZCV flags or I/F/T control bits?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP121/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Quick check

Retrieval: during an ordinary BL subroutine call in Supervisor mode, what protects a caller's value in r4?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP122/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Summary

  • Cortex-A7 provides nine modes (User at PL0; FIQ, IRQ, Supervisor, Abort, Undefined, System, and Secure Monitor at PL1; Hyp at PL2) encoded in CPSR.M[4:0].
  • r0..r7 are physically shared across every mode (FIQ privately banks r8..r14, other exception modes bank SP and LR, and System shares User's r0..r14).
  • CPSR holds live NZCV flags, A/I/F masks, T state, and M[4:0]; exception modes save interrupted CPSR into SPSR_<mode>, whereas User and System have no SPSR.
  • Privileged CP15 registers like SCTLR (c1, c0, 0) are read via MRC p15 and updated via masked RMW + dsb sy + MCR p15 + isb.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP123/523

GL-E01 · 2.1 (m02-l01)

>_ Kernel Systems Academy

Key takeaways

  • The Cortex-A7 architectural modes are User, FIQ, IRQ, Supervisor, Monitor, Abort, Hyp, Undefined, and System.
  • User and System share general-purpose register storage; exception-mode banking does not preserve every general-purpose register.
  • The callee saves and restores r4 under the call convention.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP124/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

ARM Load/Store Addressing, Shifts, and Conditions

free preview20 min

What you'll learn

  • Compute pre-indexed and post-indexed effective addresses and writeback values.
  • Derive field replacement with BIC and ORR without corrupting adjacent bits.
  • Distinguish unsigned carry conditions from signed overflow conditions.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP125/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Load/Store Effective Addresses, Barrel Shifts, and ALU Condition Physics

  • Why does ARMv7-A forbid CISC-style instructions like orl $8, 0x0209C000 that modify memory in a single…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP126/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Load/Store Effective Addresses, Barrel Shifts, and ALU Condition Physics

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP127/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Load/Store Effective Addresses, Barrel Shifts, and ALU Condition Physics

fig_m02_l02_a.svg

Figure 2.2a: Separating accessed Effective Address from base writeback (top) and inline barrel-shifted BIC/ORR field replacement (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP128/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Mental model

note: Pre-indexed ([r1, #4]!) versus post-indexed ([r1], #4) addressing is like stepping forward on a tiled floor either before or after taking a core sample: your boots stop on 0x1004 either way, yet you sampled two different locations (0x1004 vs 0x1000).

Where it stops working: Unlike floor tiles, ARMv7-A memory accesses also check alignment and MMIO side effects, and comparing 32-bit pointers above 0x80000000 with signed condition codes treats valid DDR3 RAM as negative numbers.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP129/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Effective Address,BIC/ORR Field Masking & Unsigned vs Signed CMP Flags

definition: An ARMv7-A load/store computes an Effective Address (EA) separately from base writeback: pre-indexed [Rn, #off]! accesses Rn + off and updates Rn, while post-indexed [Rn], #off accesses Rn then updates Rn <- Rn + off. BIC clears mask bits (Rn & ~mask) ahead of ORR insertion. CMP Rn, Rm computes Rn + ~Rm + 1 and sets CPSR flags without storing a difference: unsigned conditions test carry C == 1 (BHS = no borrow, Rn >= Rm) and C == 0 (BLO, Rn < Rm), whereas signed two's-complement conditions test N == V (BGE) and N != V (BLT).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP130/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Load/Store Effective Addresses, Barrel Shifts, and ALU Condition Physics

  • Trace the four independent computations—base/offset, effective address, shifted mask, and flag interpretati…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP131/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Load/Store Effective Addresses, Barrel Shifts, and ALU Condition Physics

Step 1 (Base & Offset): Separate base pointer Rn from byte offset; VBAR exception bases also require 32-byte alignment.

Track accessed address, final base register, field width mask, and unsigned vs signed flags as four separate facts.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP132/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Exception Vector Base Routing and Unsigned vs Signed Flag Divergence

  • Why does signed versus unsigned branching bite i.MX6ULL firmware engineers on Day 1?
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP133/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Exception Vector Base Routing and Unsigned vs Signed Flag Divergence

fig_m02_l02_b.svg

Figure 2.2b: Exception vector base routing via SCTLR.V and 32-byte-aligned VBAR (left) and why DDR3 addresses (>= 0x80000000) require unsigned BHS/BLO carry conditions rather than signed BGE/BLT (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP134/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Exception Vector Base Routing and Unsigned vs Signed Flag Divergence

  • Now inspect the left panel of Figure 2.2b (Exception Vector Base Routing) and replace_nibble in `ta…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP135/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m02-l02
learner@ksa:~/gl-e01/m02-l02$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .text    .global replace_nibble    .type replace_nibble, %function    replace_nibble:        ldr r2, [r0], #4        bic r2, r2, #0x00000f00        and r1, r1, #15        orr r2, r2, r1, lsl #8        str r2, [r0, #-4]        bx lr    .size replace_nibble, .-replace_nibble
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP136/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Auditing Vector Alignment, Pre/Post Indexing, and Nibble Masking in C11

  • sctlr_vbar_addr_audit.c verifies all four mechanisms in strict ISO C11: vector_entry_addr() rejects r…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP137/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

sctlr_vbar_addr_audit.c

learner@ksa: ~/gl-e01/m02-l02
learner@ksa:~/gl-e01/m02-l02$ bat sctlr_vbar_addr_audit.c File: sctlr_vbar_addr_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        enum { VEC_RESET = 0U, VEC_UNDEF = 1U, VEC_SVC = 2U, VEC_PABT = 3U,           VEC_DABT = 4U, VEC_IRQ = 6U, VEC_FIQ = 7U };        static bool vector_entry_addr(uint32_t sctlr, uint32_t vbar,                                  unsigned slot, uint32_t *addr_out)    {        if (slot > 7U || slot == 5U) return false;        bool high_vecs = ((sctlr >> 13) & 1U) != 0U;        if (!high_vecs && (vbar & 0x1fU) != 0U) return false;
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP138/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Executing the Addressing and Vector Verifier

  • Build and run sctlr_vbar_addr_audit.c on the workstation and cross-assemble target.S for Cortex-A7.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP139/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Executing the Addressing and Vector Verifier

learner@ksa: ~/gl-e01/m02-l02
learner@ksa:~/gl-e01/m02-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic sctlr_vbar_addr_audit.c -o sctlr_vbar_addr_auditlearner@ksa:~/gl-e01/m02-l02./sctlr_vbar_addr_auditIRQ_vec=0x87800018 hi_vec=0xFFFF0018 post=0x1000 pre=0x1004 nib=0xA5A5F2FFlearner@ksa:~/gl-e01/m02-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m02-l02
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP140/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Executing the Addressing and Vector Verifier

strace — learner@ksa: ~/gl-e01/m02-l02
learner@ksa:~/gl-e01/m02-l02strace -e trace=write,exit_group \./sctlr_vbar_addr_auditwrite(1, "IRQ_vec=0x87800018 hi_vec=0xFFFF"..., 75) = 75exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP141/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Comparing ARMv7-A Addressing Modes and Condition Suffixes

  • Review the side-by-side summary table below before writing startup copy/zero loops in Module 5.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP142/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Comparing ARMv7-A Addressing Modes and Condition Suffixes

  • Later: Lesson m02-l03 combines load/store writeback with control flow—building 8-byte-aligned full-desc…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP143/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: A .bss zeroing loop in DDR3 at 0x87800000 compares cmp r0, r1 and branches with signed blt (or bge), either aborting after 0 iterations or overrunning RAM when comparing against lower regions. Root Cause: Any 32-bit address >= 0x80000000 has sign bit 31 == 1 (negative in two's-complement!), inverting signed BLT/BGE/BGT/BLE predicates. Fix: Always compare memory addresses, sizes, and cycle counters with unsigned condition codes (BLO for <, BHS for >=, BHI for >, BLS for <=).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP144/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: Bare-metal startup writes 0x87800000 to CP15 VBAR (mcr p15, 0, r0, c12, c0, 0), yet the first IRQ jumps to 0xFFFF0018 instead of 0x87800018. Root Cause: Either SCTLR.V (bit 13) remained 1 from the Boot ROM environment (forcing high vectors at 0xFFFF0000), or .vectors lacked .balign 32 (VBAR[4:0] == 0). Fix: Align .vectors to 32 bytes, clear SCTLR bit 13 (bic r0, r0, #(1 << 13)), write VBAR, and execute isb.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP145/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Guided Practice: Scaled Barrel-Shift Indexing & CMP Flag Trace

  • Step 1 (Scaled Register Offset): Given r0 = 0x020C4068 (CCM_CCGR0) and r1 = 3, compute EA and f…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP146/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Independent Practice: Exact AArch32 ALU Subtraction Flag Simulator (cmp_flags_audit.c)

  • Write a standalone ISO C11 verifier cmp_flags_audit.c implementing `void arm_cmp32(uint32_t a, uint32_t…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP147/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Quick check

Starting from r1 = 0x1000, both ldr r0, [r1, #4]! and ldr r0, [r1], #4 leave r1 == 0x1004. What distinguishes their memory behavior?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP148/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Quick check

In replace_nibble, why execute and r1, r1, #15 before orr r2, r2, r1, lsl #8 even after bic r2, r2, #0x0f00 cleared bits [11:8]?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP149/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Quick check

Retrieval: after cmp r0, r1 with r0 = 0xFFFFFFFF and r1 = 0x00000001, why does bhi (unsigned >) branch while blt (signed <) also branches on the exact same CPSR flags?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP150/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Summary

  • ARMv7-A separates ALU/barrel-shifter register operations from LDR/STR memory transfers, and separates accessed Effective Address (EA) from base writeback.
  • Pre-indexed [Rn, #off]! accesses Rn + off and writes Rn + off back; post-indexed [Rn], #off accesses original Rn first and writes Rn + off back afterward.
  • Replace multi-bit subfields by clearing the field mask with BIC, clamping the new value's bit-width with AND, and inserting via shifted ORR.
  • After CMP, unsigned comparisons test carry (BHS C==1, BLO C==0—mandatory for DDR3 addresses >= 0x80000000!), whereas signed comparisons test N == V (BGE) and N != V (BLT).
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP151/523

GL-E01 · 2.2 (m02-l02)

>_ Kernel Systems Academy

Key takeaways

  • ARM load/store instructions compute an effective address separately from arithmetic on the transferred value.
  • Pre-indexed writeback updates the base using the offset before the access; post-indexing accesses the original base then updates it.
  • The conditions interpret carry and signed flags differently.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP152/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Branches, Full-Descending Stacks, and Calling C

free preview20 minlab lab_m02_armv7_isa_sim

What you'll learn

  • Trace B, BL, and BX without confusing subroutine and exception returns.
  • Construct an eight-byte-aligned full-descending frame around a nested call.
  • Verify saved-register order and restore the caller-visible state.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP153/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Link-Register Lifetime, Full-Descending Stacks, and 8-Byte Alignment

  • On x86_64, executing call helper automatically decrements %rsp and writes an 8-byte return address…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP154/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Link-Register Lifetime, Full-Descending Stacks, and 8-Byte Alignment

  • How does ARMv7-A organize that stack in RAM, and why can pushing {lr} alone (4 bytes) crash a C program?
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP155/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Link-Register Lifetime, Full-Descending Stacks, and 8-Byte Alignment

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP156/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Link-Register Lifetime, Full-Descending Stacks, and 8-Byte Alignment

fig_m02_l03_a.svg

Figure 2.3a: Full-descending stack frame layout and 16-bit register bitmask ordering (left) vs why public C calls require 8-byte SP alignment (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP157/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Mental model

note: Holding a return address in LR is like keeping a finger on page 104 while reading a footnote (leaf call): before jumping from that footnote to a second index table (nested BL), you must tuck an 8-byte bookmark pair ({r4, lr}) onto the stack.

Where it stops working: Unlike paper bookmarks, STMFD orders registers strictly by hardware register number rather than assembly text order, public C calls mandate 8-byte SP alignment, and hardware exception returns (subs pc, lr, #4) must never be used to return from BL subroutines.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP158/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

B / BL / BX Control Transfer, Full-Descending Stack & 8-Byte AAPCS32 Frame

definition: In ARMv7-A, B jumps without touching LR; BL writes the subroutine return address into LR before branching; and BX Rm branches to Rm & ~1U while setting CPSR.T = Rm[0] (1 = Thumb, 0 = ARM interworking). STMFD sp!, {reglist} (STMDB) allocates 4 * k bytes below SP and stores registers in ascending register-number order (r0..r15) from lowest to highest address; LDMFD sp!, {..., pc} (LDMIA) pops the frame and returns. Every public AAPCS32 call site must satisfy SP mod 8 == 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP159/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Link-Register Lifetime, Full-Descending Stacks, and 8-Byte Alignment

  • Step through the four phases of a non-leaf assembly-to-C call frame below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP160/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Link-Register Lifetime, Full-Descending Stacks, and 8-Byte Alignment

Step 1 (Caller Entry): Outer BL writes return address into live LR with incoming SP aligned to 8 bytes.

Every non-leaf assembly routine calling C must save outer LR and keep SP 8-byte aligned across all exit paths.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP161/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Tracing Nested Calls and Separating Subroutine vs Exception Returns

  • Follow the exact register and stack values across sum_then_double in Figure 2.3b and target.S below…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP162/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Tracing Nested Calls and Separating Subroutine vs Exception Returns

  • Look at Panel 2 of Figure 2.3b: never confuse bx lr with subs pc, lr, #4! Hardware IRQ except…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP163/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Tracing Nested Calls and Separating Subroutine vs Exception Returns

fig_m02_l03_b.svg

Figure 2.3b: Return-address lifetime across nested BL calls (top) and why BX LR / LDMFD subroutine returns differ fundamentally from subs pc, lr, #4 exception returns (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP164/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m02-l03
learner@ksa:~/gl-e01/m02-l03$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .text    .global sum_then_double    .type sum_then_double, %function    sum_then_double:        stmfd sp!, {r4, lr}        bl add_words        lsl r0, r0, #1        ldmfd sp!, {r4, pc}    .size sum_then_double, .-sum_then_double    .type add_words, %function    add_words:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP165/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Proving AAPCS32 Stack Alignment and Callee Preservation in C11

  • aapcs32_frame_verifier.c simulates the 16-word ARMv7-A register file and a 16-word full-descending…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP166/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

aapcs32_frame_verifier.c

learner@ksa: ~/gl-e01/m02-l03
learner@ksa:~/gl-e01/m02-l03$ bat aapcs32_frame_verifier.c File: aapcs32_frame_verifier.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        struct aapcs_ctx {        uint32_t r[16]; /* r13 = SP word index, r14 = LR, r15 = PC */        uint32_t stack[16];    };        static bool push_pair_fd(struct aapcs_ctx *c, unsigned reg_lo, unsigned reg_hi)    {        if (reg_lo >= reg_hi || c->r[13] < 2U || ((c->r[13] * 4U) & 7U) != 0U)            return false;
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP167/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Running the Frame Verifier and Checking Target Encodings

  • Compile and run aapcs32_frame_verifier.c, then cross-assemble target.S.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP168/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Running the Frame Verifier and Checking Target Encodings

learner@ksa: ~/gl-e01/m02-l03
learner@ksa:~/gl-e01/m02-l03gcc -std=c11 -Wall -Wextra -Werror -pedantic aapcs32_frame_verifier.c -o aapcs32_frame_verifierlearner@ksa:~/gl-e01/m02-l03./aapcs32_frame_verifierAAPCS32 frame=8B sp_word=8 r0=32 r4=0x40000004 ret_pc=0x87800104learner@ksa:~/gl-e01/m02-l03clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m02-l03
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP169/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Running the Frame Verifier and Checking Target Encodings

strace — learner@ksa: ~/gl-e01/m02-l03
learner@ksa:~/gl-e01/m02-l03strace -e trace=write,exit_group \./aapcs32_frame_verifierwrite(1, "AAPCS32 frame=8B sp_word=8 r0=32"..., 65) = 65exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP170/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Control-Transfer Matrix and Balanced Exit Paths

  • Audit every early-return branch in hand-written assembly: if an error path jumps to bx lr after `stmfd sp…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP171/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Control-Transfer Matrix and Balanced Exit Paths

  • Later: Module 3 (m03-l01..m03-l03) applies BIC, ORR, DSB SY, and AAPCS32-callable assembly routin…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP172/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: An assembly startup stub pushes {lr} (4 bytes) from an 8-byte-aligned stack and calls a C diagnostic function using uint64_t or LDRD, triggering an Alignment Data Abort or printing garbled 64-bit numbers. Root Cause: Subtracting 4 bytes left SP mod 8 == 4 at a public AAPCS32 C interface where the compiler assumes SP mod 8 == 0. Fix: Push an even number of registers (stmfd sp!, {r4, lr}) so SP moves in multiples of 8 bytes.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP173/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: A leaf assembly function called via bl delay_ticks ends with subs pc, lr, #4 instead of bx lr, hanging in an infinite loop re-executing bl delay_ticks (or trapping). Root Cause: subs pc, lr, #4 is the hardware IRQ exception return (restoring SPSR and subtracting the +4 pipeline bias), whereas bl stores the exact return address in lr. Fix: Return from BL subroutines exclusively via bx lr (leaf) or ldmfd sp!, {..., pc} (non-leaf).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP174/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Guided Practice: Five-Argument Stack Passing & Register-List Ordering

  • Step 1 (5th Argument Frame): To call `uint32_t pack5(uint32_t a, uint32_t b, uint32_t c, uint32_t d, ui…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP175/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Independent Practice: General 16-Bit Bitmask STMFD/LDMFD Verifier (stmfd_abi_audit.c)

  • Extend aapcs32_frame_verifier.c into stmfd_abi_audit.c implementing exec_stmfd() and exec_ldmfd() f…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP176/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Quick check

Non-leaf function outer_fn is entered via BL and immediately executes bl inner_fn without saving LR. What happens when outer_fn later executes bx lr?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP177/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Quick check

With SP = 0x87820000 (8-byte aligned), an assembly wrapper executes stmfd sp!, {lr} (4 bytes) and calls a C function. How must this prologue be fixed?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP178/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Quick check

Retrieval: when stmfd sp!, {lr, r4} executes at SP = 0x1000, which word lands at the lower address 0x0FF8?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP179/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Lab: lab_m02_armv7_isa_sim

  • Complete the module studio lab lab_m02_armv7_isa_sim: repair CPSR mode-field preservation, width-bounde…

Verify: ./check.sh exits 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP180/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Summary

  • B jumps without touching LR, BL records the subroutine return address in LR (clobbering any unsaved LR), and BX Rm branches while selecting ARM/Thumb state from Rm[0].
  • STMFD sp!, {...} (STMDB) grows a full-descending stack downward and stores registers strictly in ascending register-number order (r0..r15), placing the lowest register at the lowest address.
  • AAPCS32 requires 8-byte stack alignment (SP mod 8 == 0) at every public call site and requires callees to preserve r4..r11 and SP.
  • Never use subs pc, lr, #4 (an IRQ exception return) to return from an ordinary BL subroutine; use bx lr (leaf) or ldmfd sp!, {..., pc} (non-leaf).
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP181/523

GL-E01 · 2.3 (m02-l03)

>_ Kernel Systems Academy

Key takeaways

  • B branches without creating a subroutine return address, while BL records a return address in LR.
  • BX branches to a register target and uses the architectural instruction-state convention encoded in that target.
  • At the lower address of the newly allocated two-word frame.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP182/523

m03/

>_ Kernel Systems Academy

IOMUXC muxing, pad electrical policy, and GPIO MMIO

  1. 3.1 CCM Clock Gates, Pad Mux Modes, and SION
  2. 3.2 Pad Electrical Fields: Pulls, Drive, Slew, and Open-Drain
  3. 3.3 GPIO DR, GDIR, PSR, Interrupt Status, and Assembly LED Control

lab → lab_m03_iomuxc_gpio_mmio

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP183/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

CCM Clock Gates, Pad Mux Modes, and SION

free preview20 min

What you'll learn

  • Locate CCGR0–CCGR6 fields without enabling every peripheral.
  • Derive a legal pad mux value from the selected pad's function table.
  • Explain what SION forces and what it does not connect.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP184/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Why SoCs Gate Peripheral Clocks and Multiplex Package Balls

  • Why can't you just write *gpio1_dr = 0 immediately after reset and watch GPIO1_IO03 pull low?
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP185/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Why SoCs Gate Peripheral Clocks and Multiplex Package Balls

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP186/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Why SoCs Gate Peripheral Clocks and Multiplex Package Balls

fig_m03_l01_a.svg

Figure 3.1a: End-to-end silicon signal graph — CCM clock gate (CG13), peripheral block, IOMUXC MUX_MODE + SION loopback, and SELECT_INPUT daisy routing.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP187/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Mental model

note: Driving a package ball requires both powering the pump (CCM_CCGR clock gate) and turning the manifold selector (IOMUXC MUX_MODE): running an unrouted peripheral or routing an unclocked peripheral leaves the external pin dead.

Where it stops working: Unlike plumbing valves, reading or writing a clock-gated peripheral register can stall or fault on the internal IPG bus immediately, and forcing SION=1 only activates that ball's input buffer rather than wiring arbitrary peripherals together.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP188/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

CCM Clock Gates (CCGR0..6), IOMUXC MUX_MODE, SION & SELECT_INPUT

definition: CCM_CCGR0..CCGR6 (0x020C4068..0x020C4080) divide each 32-bit word into 16 two-bit CGk fields (bits [2k+1 : 2k]; GPIO1 is CCGR1 CG13 at [27:26]). Per-ball IOMUXC_SW_MUX_CTL_PAD_* (0x020E0068 for GPIO1_IO03) selects one of that pad's implemented functions in MUX_MODE[3:0] (ALT5 = 5 for GPIO1_IO03) and provides SION (bit 4, 0x10), which forces that ball's input receiver path active even when an output function drives the pin. When multiple balls can feed one peripheral input (UART_RX, I2C_SDA), a separate *_SELECT_INPUT (DAISY) register selects which ball reaches the receiver.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP189/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Why SoCs Gate Peripheral Clocks and Multiplex Package Balls

  • Step through the four hardware dependencies in order before writing register code:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP190/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Why SoCs Gate Peripheral Clocks and Multiplex Package Balls

Step 1 (CCM Policy): Update only CCGR1[27:26] = 0b11 (mask 0x0C000000) and issue DSB SY before touching GPIO1.

Enable the peripheral clock first, then select a pad-supported ALT mode, SION policy, and SELECT_INPUT daisy source.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP191/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

What SION and Daisy-Chain Input Muxes Physically Connect

  • Demystify two IOMUXC features that trip up experienced engineers—both drawn in Figure 3.1a and bit-ma…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP192/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

What SION and Daisy-Chain Input Muxes Physically Connect

fig_m03_l01_b.svg

Figure 3.1b: Exact 32-bit bitfields for CCM_CCGR1 (top) and IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO03 + SELECT_INPUT (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP193/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

What SION and Daisy-Chain Input Muxes Physically Connect

  • Trace gpio1_clock_and_mux in target.S below: first it updates CCM_CCGR1 (0x020C406C) bits [27:26]…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP194/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m03-l01
learner@ksa:~/gl-e01/m03-l01$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .text    .global gpio1_clock_and_mux    gpio1_clock_and_mux:        ldr r0, =0x020c406c        ldr r1, [r0]        bic r1, r1, #(3 << 26)        orr r1, r1, #(3 << 26)        str r1, [r0]        dsb sy        ldr r0, =0x020e0068        ldr r1, [r0]
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP195/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Validating CCGR Slots, Per-Ball ALT Tables, and Daisy Bounds in C11

  • In iomuxc_mux_daisy_audit.c, notice how we validate hardware capability, not just bit width!
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP196/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

iomuxc_mux_daisy_audit.c

learner@ksa: ~/gl-e01/m03-l01
learner@ksa:~/gl-e01/m03-l01$ bat iomuxc_mux_daisy_audit.c File: iomuxc_mux_daisy_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        static bool ccgr_set_gate(uint32_t *ccgr, unsigned index, unsigned mode)    {        if (index >= 16U || mode > 3U || mode == 2U) return false;        uint32_t shift = index * 2U, mask = UINT32_C(3) << shift;        *ccgr = (*ccgr & ~mask) | ((uint32_t)mode << shift);        return true;    }        static bool iomuxc_set_mux(uint32_t *mux_ctl, unsigned alt, bool sion,
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP197/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Running the Clock-Gate and Pinmux Verifier

  • Compile and run iomuxc_mux_daisy_audit.c on the workstation and cross-assemble target.S into target.o.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP198/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Running the Clock-Gate and Pinmux Verifier

learner@ksa: ~/gl-e01/m03-l01
learner@ksa:~/gl-e01/m03-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic iomuxc_mux_daisy_audit.c -o iomuxc_mux_daisy_auditlearner@ksa:~/gl-e01/m03-l01./iomuxc_mux_daisy_auditCCGR1_CG13=0x0C000000 MUX_ALT5_SION=0x15 MUX_ALT5=0x05 DAISY=1learner@ksa:~/gl-e01/m03-l01clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m03-l01
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP199/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Running the Clock-Gate and Pinmux Verifier

strace — learner@ksa: ~/gl-e01/m03-l01
learner@ksa:~/gl-e01/m03-l01strace -e trace=write,exit_group \./iomuxc_mux_daisy_auditwrite(1, "CCGR1_CG13=0x0C000000 MUX_ALT5_S"..., 63) = 63exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP200/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Clock-Gate and Pin-Routing Policy Matrix

  • Compare narrow field updates against blanket register writes below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP201/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Clock-Gate and Pin-Routing Policy Matrix

  • Later: Lesson m03-l02 opens the electrical pad cell right behind SW_MUX_CTL—decoding Schmitt hysteresis…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP202/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: UART1 or I2C1 transmits clean waveforms on an oscilloscope, yet its receive FIFO stays completely empty even though SW_MUX_CTL_PAD_* selected the documented ALT mode and RX pulses arrive at the SoC ball. Root Cause: Multiple package balls can supply that RX line, and the peripheral's IOMUXC_*_SELECT_INPUT (DAISY) register still points at a different default pad. Fix: Always check whether an input signal has a SELECT_INPUT register and program its DAISY index alongside SW_MUX_CTL_PAD_*.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP203/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Firmware writes GPIO1_DR and GPIO1_GDIR before enabling CCM_CCGR1[27:26], triggering a synchronous external Data Abort or leaving GPIO1 latches stuck at reset values. Root Cause: Without IPG clock edges (CG13 == 0b00), the peripheral's bus slave logic cannot complete or latch register transactions. Fix: Enable the peripheral's CCM_CCGRx CG field and execute dsb sy before reading or writing any register inside that peripheral.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP204/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Guided Practice: Deriving GPIO5 Clock Gates & SION Masks

  • Step 1 (CCGR1 CG15): Given CCM_CCGR0 at 0x020C4068, verify CCM_CCGR1 is 0x020C406C and compute…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP205/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Independent Practice: Board Pinmux & Daisy-Chain Conflict Linter (pinmux_table_audit.c)

  • Write a standalone ISO C11 verifier pinmux_table_audit.c auditing an array of struct pin_cfg board en…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP206/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Quick check

CCM_CCGR1[27:26] is 0b11 and GPIO1_DR / GPIO1_GDIR toggle bit 3 in debugger readbacks, yet ball GPIO1_IO03 still outputs I2C1 activity instead of GPIO. What is missing?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP207/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Quick check

Why must iomuxc_set_mux() reject ALT8 (8) on GPIO1_IO03 even though MUX_MODE[3:0] is 4 bits wide (0..15)?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP208/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Quick check

Retrieval: what does setting SION (bit 4 = 1) in IOMUXC_SW_MUX_CTL_PAD_* physically accomplish?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP209/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Summary

  • CCM_CCGR0..CCGR6 (0x020C4068..0x020C4080) gate peripheral clocks using 16 two-bit CG fields per word (00 off, 01 RUN, 11 RUN+WAIT); enable the clock + dsb sy before accessing peripheral MMIO.
  • Peripheral clocking (CCM) and ball signal routing (IOMUXC_SW_MUX_CTL_PAD_*) are independent hardware requirements.
  • MUX_MODE[3:0] must select an ALT function actually implemented on that exact SoC ball (ALT5 for GPIO1_IO03).
  • SION (bit 4) forces the pad's input receiver active for pin loopback/monitoring, while *_SELECT_INPUT (DAISY) chooses which ball drives a multi-pad peripheral RX input.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP210/523

GL-E01 · 3.1 (m03-l01)

>_ Kernel Systems Academy

Key takeaways

  • The i.MX6ULL CCM exposes CCGR0 through CCGR6 for peripheral clock-gating policy.
  • A peripheral's clock-gate field and its pad mux route are independent configuration requirements.
  • It forces the selected pad input path on, not arbitrary wiring.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP211/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Pad Electrical Fields: Pulls, Drive, Slew, and Open-Drain

free preview20 min

What you'll learn

  • Decode HYS, PUS, PUE, PKE, ODE, SPEED, DSE, and SRE fields.
  • Diagnose why selecting PUS alone does not necessarily enable a pull resistor.
  • Choose a conservative pad policy and state which electrical claims still require measurement.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP212/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Inside an i.MX6ULL CMOS Pad Cell: Transistors, Bias, and Hysteresis

  • Open almost any vendor BSP file and you will spot magic constants like *pad_ctl = 0x10B0; with a misleadi…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP213/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Inside an i.MX6ULL CMOS Pad Cell: Transistors, Bias, and Hysteresis

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP214/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Inside an i.MX6ULL CMOS Pad Cell: Transistors, Bias, and Hysteresis

fig_m03_l02_a.svg

Figure 3.2a: Inside an i.MX6ULL CMOS pad cell — output DSE slices and ODE open-drain release (left), PKE -> PUE -> PUS keeper vs pull hierarchy (center), and HYS Schmitt receiver (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP215/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Mental model

note: If SW_MUX_CTL routes which peripheral track reaches the station, SW_PAD_CTL configures the electrical overhead wire (PKE/PUE/PUS), regenerative holding brake (Keeper), locomotive traction motors (DSE), and throttle ramp (SRE).

Where it stops working: Unlike rail switches, setting pull-strength bits (PUS) has zero physical effect when the mode switch (PUE) selects bus-keeper mode or the master breaker (PKE) is open, and actual signal rise times depend on external trace capacitance and pull-up resistors.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP216/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

IOMUXC SW_PAD_CTL Electrical Policy Bitfields

definition: In standard i.MX6ULL IOMUXC_SW_PAD_CTL_PAD_* registers (0x020E02F4 for GPIO1_IO03, active field mask 0x0001F8F9U), HYS[16] enables Schmitt input hysteresis; PKE[12] powers the bias cell; PUE[13] selects resistive pull (1) over bus keeper (0); PUS[15:14] selects 100K down (0), 47K up (1), 100K up (2), or 22K up (3) only when PKE=1 and PUE=1; ODE[11] enables open-drain output; and SPEED[7:6], DSE[5:3] (0=Hi-Z, 1..7 = R0/1..R0/7), and SRE[0] govern output impedance and edge slew.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP217/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Inside an i.MX6ULL CMOS Pad Cell: Transistors, Bias, and Hysteresis

  • Step through the four-stage electrical design checklist below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP218/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Inside an i.MX6ULL CMOS Pad Cell: Transistors, Bias, and Hysteresis

Step 1 (Input Path): Enable HYS[16] = 1 for Schmitt noise immunity on slow digital edges (still debounce switches in software!).

Decode PKE -> PUE -> PUS and DSE != 0 explicitly instead of copying opaque hex constants.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP219/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Decoding 32-Bit SW_PAD_CTL Words and Preserving Reserved Gaps

  • Inspect Figure 3.2b to compare the exact 32-bit bitfield layout (0x0001F8F9U active mask, leaving `…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP220/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Decoding 32-Bit SW_PAD_CTL Words and Preserving Reserved Gaps

fig_m03_l02_b.svg

Figure 3.2b: Exact 32-bit SW_PAD_CTL bitfields under mask 0x0001F8F9 (top) and three verified electrical profiles (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP221/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

pad_policy.c

learner@ksa: ~/gl-e01/m03-l02
learner@ksa:~/gl-e01/m03-l02$ bat pad_policy.c File: pad_policy.c    #include <stdint.h>    static volatile uint32_t *const pad_gpio1_io03 =        (volatile uint32_t *)(uintptr_t)0x020e02f4U;    void configure_input_pullup(void)    {        const uint32_t mask = 0x0001f8f9U;        const uint32_t policy = (1U << 16) | (2U << 14) |                                (1U << 13) | (1U << 12);        uint32_t old = *pad_gpio1_io03;        *pad_gpio1_io03 = (old & ~mask) | (policy & mask);    }
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP222/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Encoding and Auditing Hierarchical Pad Policies in C11

  • iomuxc_pad_ctl_builder.c encodes all eight fields with range checks (pus <= 3, speed <= 3, dse <= 7…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP223/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

iomuxc_pad_ctl_builder.c

learner@ksa: ~/gl-e01/m03-l02
learner@ksa:~/gl-e01/m03-l02$ bat iomuxc_pad_ctl_builder.c File: iomuxc_pad_ctl_builder.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        enum bias_kind { BIAS_DISABLED = 0, BIAS_KEEPER = 1, BIAS_PULL = 2 };        struct pad_cfg {        bool hys, pue, pke, ode, sre;        unsigned pus, speed, dse;    };        static bool build_pad_ctl(const struct pad_cfg *c, uint32_t *word_out)    {
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP224/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Running the Pad-Control Builder and Cross-Compiling Target Policy

  • Execute iomuxc_pad_ctl_builder on the workstation and cross-compile pad_policy.c for freestanding `Cort…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP225/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Running the Pad-Control Builder and Cross-Compiling Target Policy

learner@ksa: ~/gl-e01/m03-l02
learner@ksa:~/gl-e01/m03-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic iomuxc_pad_ctl_builder.c -o iomuxc_pad_ctl_builderlearner@ksa:~/gl-e01/m03-l02./iomuxc_pad_ctl_builder0x10B0=keeper(DSE=6,SPD=2) pullup=0x1B000 od_i2c=0x1B8B0learner@ksa:~/gl-e01/m03-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -mfloat-abi=soft -ffreestanding -std=c11 -Wall -Wextra -Werror -x c -c pad_policy.c -o target.olearner@ksa:~/gl-e01/m03-l02
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP226/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Running the Pad-Control Builder and Cross-Compiling Target Policy

strace — learner@ksa: ~/gl-e01/m03-l02
learner@ksa:~/gl-e01/m03-l02strace -e trace=write,exit_group \./iomuxc_pad_ctl_builderwrite(1, "0x10B0=keeper(DSE=6,SPD=2) pullu"..., 57) = 57exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP227/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Pad Electrical Role Comparison Matrix

  • Match every board trace to the electrical profile its external circuit requires:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP228/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Pad Electrical Role Comparison Matrix

  • Later: Lesson m03-l03 completes the GPIO chain inside 0x0209C000—preload-safe DR/GDIR sequencing,…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP229/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: A push-button pin configured with (2U << 14) | (1U << 12) (PUS = 100K_UP, PKE = 1, forgetting PUE = 1U << 13) reads 0 permanently after the very first button press to ground. Root Cause: With PUE == 0, the bias cell operates as a bus keeper that latches whichever logic level last appeared on the wire (0 after ground contact) instead of pulling back to 3.3 V. Fix: Set both PKE (bit 12) and PUE (bit 13) whenever PUS (bits 15:14) must act as a pull-up or pull-down resistor.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP230/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: CCM_CCGR1, MUX_MODE = ALT5, GPIO1_GDIR = (1U << 3), and GPIO1_DR are all configured, yet the physical output pin stays completely high-impedance (0 mA drive). Root Cause: SW_PAD_CTL was left at 0x0001B000 (an input policy with DSE[5:3] == 0), disconnecting all seven output transistor fingers (Hi-Z). Fix: Program a non-zero DSE drive strength (such as DSE = 6, 6U << 3) on every push-pull or open-drain output pad.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP231/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Guided Practice: Decoding 0x000178B1 & Encoding Custom Output Pads

  • Step 1 (Decode): Decode 0x000178B1U field by field (HYS=1, PUS=1 47K pull-up, PUE=1, PKE=1,…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP232/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Independent Practice: Pad-Role Consistency Linter (pad_role_lint.c)

  • Write a standalone ISO C11 verifier pad_role_lint.c implementing `int audit_pad_policy(uint32_t mux_ctl…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP233/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Quick check

In SW_PAD_CTL, PUS[15:14] = 2 (100K_UP), PKE[12] = 1, and PUE[13] = 0. Which electrical bias circuit is actually connected to the ball?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP234/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Quick check

When ODE (bit 11) is 1 (open-drain enabled), what happens electrically at the ball when software writes logical 1 to the output latch?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP235/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Quick check

Retrieval: does writing 0x0001B000 (HYS=1, 100K pull-up) to SW_PAD_CTL_PAD_GPIO1_IO03 (0x020E02F4) route ball GPIO1_IO03 to the GPIO1 controller?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP236/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Summary

  • Standard i.MX6ULL SW_PAD_CTL words pack HYS[16], PUS[15:14], PUE[13], PKE[12], ODE[11], SPEED[7:6], DSE[5:3], and SRE[0] inside mask 0x0001F8F9U.
  • PKE (bit 12) powers the bias block, and PUE (bit 13) chooses resistive pull (1, using PUS) versus bus keeper (0, ignoring PUS).
  • ODE (bit 11 = 1) releases high-side PMOS drive on logical 1; DSE[5:3] sets output impedance (R0/1..R0/7, with 0 = Hi-Z disabled!).
  • 0x000010B0 enables bus keeper (PKE=1, PUE=0) with DSE=6 and SPEED=2—not an active 100 kOhm pull-down.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP237/523

GL-E01 · 3.2 (m03-l02)

>_ Kernel Systems Academy

Key takeaways

  • For the general pad-control example, HYS is bit 16, PUS is bits 15:14, PUE is bit 13, and PKE is bit 12.
  • PKE enables the pull/keeper function, while PUE selects pull operation rather than keeper operation.
  • No; the mux route remains a separate configuration choice.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP238/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

GPIO DR, GDIR, PSR, Interrupt Status, and Assembly LED Control

free preview20 minlab lab_m03_iomuxc_gpio_mmio

What you'll learn

  • Distinguish DR output state, GDIR direction, and PSR pad sampling.
  • Derive ICR1 and ICR2 fields from a bank-local GPIO index.
  • Acknowledge one W1C interrupt bit without clearing sibling events.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP239/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Inside a 32-Pin GPIO Bank: DR Latches, Tri-State GDIR, PSR, and W1C Physics

  • Why does an i.MX6ULL GPIO controller (GPIO1 at 0x0209C000) expose both a Data Register (DR,…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP240/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Inside a 32-Pin GPIO Bank: DR Latches, Tri-State GDIR, PSR, and W1C Physics

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP241/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Inside a 32-Pin GPIO Bank: DR Latches, Tri-State GDIR, PSR, and W1C Physics

fig_m03_l03_a.svg

Figure 3.3a: Inside GPIO1 @ 0x0209C000 — separating DR request, GDIR tri-state enable, PSR wire sampling, and the ICR -> ISR (W1C) -> IMR interrupt pipeline.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP242/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Mental model

note: Operating a GPIO bank is like running a manifold with a setpoint dial (DR), an actuator clutch (GDIR), a live line-pressure gauge (PSR), and push-to-reset fault lamps (W1C ISR): set the dial safe before engaging the clutch, and push only the single fault lamp you just serviced.

Where it stops working: Unlike mechanical gauges, reading PSR while driving an output may require SION=1 on some mux paths, and GPIO interrupts still require GICv2 distributor routing and CPSR.I=0 before vectoring the Cortex-A7 CPU.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP243/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

GPIO Bank Register Map, Half-Bank ICR & Write-One-to-Clear (W1C)

definition: Each 32-pin i.MX6ULL GPIO bank (GPIO1 0x0209C000 .. GPIO5 0x020AC000) exposes DR (+0x00), GDIR (+0x04), read-only PSR (+0x08), ICR1 (+0x0C, pins 0..15), ICR2 (+0x10, pins 16..31), IMR (+0x14), ISR (+0x18), and EDGE_SEL (+0x1C). Pin p's 2-bit trigger field sits in ICR1 (p < 16) or ICR2 (p >= 16) at shift 2 * (p % 16). GPIOx_ISR uses W1C acknowledgment: software writes (UINT32_C(1) << p) directly (0 loads!) so sibling pending bits survive.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP244/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Inside a 32-Pin GPIO Bank: DR Latches, Tri-State GDIR, PSR, and W1C Physics

  • Step through all four GPIO register roles in the interactive sequence below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP245/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Inside a 32-Pin GPIO Bank: DR Latches, Tri-State GDIR, PSR, and W1C Physics

Step 1 (Output Latch): Preload inactive high DR[3] = 1 (+0x00) + DSB SY before enabling output direction.

Match every GPIO operation to its exact register contract: preload DR -> enable GDIR -> sample PSR -> direct-mask W1C ISR.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP246/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Half-Bank ICR Shifts, W1C Assembly Stores, and Active-Low LED Routines

  • Compare the two register traps in Figure 3.3b against our Cortex-A7 assembly listing target.S below…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP247/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Half-Bank ICR Shifts, W1C Assembly Stores, and Active-Low LED Routines

fig_m03_l03_b.svg

Figure 3.3b: ICR1 / ICR2 half-bank bit-pair split (top) and why direct W1C mask stores preserve concurrent sibling interrupts that RMW wipes out (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP248/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .text    .global led_prepare_inactive    led_prepare_inactive:        ldr r0, =0x0209c000        ldr r1, [r0]        orr r1, r1, #8        str r1, [r0]        dsb sy        ldr r1, [r0, #4]        orr r1, r1, #8        str r1, [r0, #4]
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP249/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Simulating GPIO Bank Registers and W1C Race Loss in C11

  • gpio_w1c_bank_sim.c models all seven core GPIO registers in a deterministic C11 harness: it verifies…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP250/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

gpio_w1c_bank_sim.c

learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03$ bat gpio_w1c_bank_sim.c File: gpio_w1c_bank_sim.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        struct gpio_bank_sim {        uint32_t dr, gdir, psr, icr1, icr2, imr, isr;    };        static bool gpio_cfg_icr(struct gpio_bank_sim *b, unsigned pin, unsigned trig)    {        if (pin >= 32U || trig > 3U) return false;        uint32_t *icr = (pin < 16U) ? &b->icr1 : &b->icr2;        unsigned shift = (pin % 16U) * 2U;
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP251/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Executing the GPIO Bank Simulator and Assembly Check

  • Build and run gpio_w1c_bank_sim on the host and cross-assemble target.S for Cortex-A7.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP252/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Executing the GPIO Bank Simulator and Assembly Check

learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03gcc -std=c11 -Wall -Wextra -Werror -pedantic gpio_w1c_bank_sim.c -o gpio_w1c_bank_simlearner@ksa:~/gl-e01/m03-l03./gpio_w1c_bank_simDR=0x08 PSR=0x00 ICR1_15=3 ICR2_16=2 W1C_rem=0x02 RMW_rem=0x00learner@ksa:~/gl-e01/m03-l03clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m03-l03
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP253/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Executing the GPIO Bank Simulator and Assembly Check

strace — learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03strace -e trace=write,exit_group \./gpio_w1c_bank_simwrite(1, "DR=0x08 PSR=0x00 ICR1_15=3 ICR2_"..., 63) = 63exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP254/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

GPIO Register Access Semantics Comparison

  • Never hide DR and ISR behind one generic set_bit(addr, mask) macro—their hardware write contracts are…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP255/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

GPIO Register Access Semantics Comparison

  • Later: Module 4 (m04-l01..m04-l03) shows how the i.MX6ULL internal Boot ROM actually loads your assem…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP256/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: When two safety switches on GPIO1_IO18 and GPIO1_IO19 trip microseconds apart, firmware services IO18 and completely misses IO19. Root Cause: The ISR acknowledged IO18 with GPIO1->ISR |= (1U << 18);, reading 0x000C0000 (bits 18 and 19 both 1) and writing 0x000C0000 back to the W1C port—clearing both pending events! Fix: Acknowledge W1C status flags exclusively with a direct mask assignment: GPIO1->ISR = (UINT32_C(1) << 18);.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP257/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Configuring falling-edge detection on GPIO1_IO18 (pin = 18) via GPIO1->ICR1 |= (3U << (2 * pin)) triggers C11 Undefined Behavior or corrupts ICR1 while leaving IO18 on low-level sensitivity. Root Cause: 2 * 18 = 36 overflows 32-bit ICR1 (pins 0..15) instead of indexing ICR2 (+0x10, pins 16..31) at shift (18 % 16) * 2 = 4. Fix: Select ICR1 vs ICR2 with pin < 16U and compute local shift as (pin % 16U) * 2U.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP258/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Guided Practice: Configuring GPIO1_IO18 (UART1_CTS_B Button) Interrupts

  • Step 1 (ICR2 Address & Mask): For GPIO1 (0x0209C000) pin 18, compute the ICR physical address,…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP259/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Independent Practice: Full 8-Register GPIO Edge & Dispatch Simulator (gpio_edge_sim.c)

  • Extend gpio_w1c_bank_sim.c into gpio_edge_sim.c adding edge_sel (+0x1C) and `gpio_apply_pad_transit…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP260/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Quick check

GPIO1_ISR (+0x18, W1C) currently reads 0x0A (bits 3 and 1 pending), and your handler has serviced only pin 3. What value must software write to GPIO1_ISR?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP261/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Quick check

Where does GPIO1 pin 16's two-bit interrupt trigger configuration live?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP262/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Quick check

Retrieval: why can GPIO1_DR bit 3 read 1 while GPIO1_PSR bit 3 simultaneously reads 0?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP263/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Lab: lab_m03_iomuxc_gpio_mmio

  • Complete the module studio lab lab_m03_iomuxc_gpio_mmio: repair isolated CCM_CCGR and IOMUXC field re…

Verify: ./check.sh exits 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP264/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Summary

  • GPIOx_DR (+0x00) holds the output latch, GPIOx_GDIR (+0x04) enables output direction (1 = out, 0 = in), and read-only GPIOx_PSR (+0x08) samples physical pad state; preload DR inactive before setting GDIR on active-low loads.
  • GPIOx_ICR1 (+0x0C) configures 2-bit triggers for pins 0..15 and GPIOx_ICR2 (+0x10) configures pins 16..31 at local shift 2 * (pin % 16).
  • GPIOx_IMR (+0x14) masks interrupt propagation while GPIOx_ISR (+0x18) latches trigger events.
  • GPIOx_ISR is Write-One-to-Clear (W1C): write (UINT32_C(1) << pin) directly with zero RMW so sibling pending interrupts are never wiped out.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP265/523

GL-E01 · 3.3 (m03-l03)

>_ Kernel Systems Academy

Key takeaways

  • GPIO DR is the data register, GDIR selects direction, and PSR reports pad state.
  • ICR1 configures pins 0–15 and ICR2 configures pins 16–31 using two-bit trigger fields.
  • The latch request and sampled electrical pad are different views.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP266/523

m04/

>_ Kernel Systems Academy

Boot ROM selection, IVT geometry, and DCD command streams

  1. 4.1 Boot Straps, eFUSE Selection, and Internal OCRAM
  2. 4.2 IVT, Boot Data, and SD/eMMC Offset Geometry
  3. 4.3 DCD Commands and a Checked C11 .imx Packer

lab → lab_m04_ivt_dcd_image

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP267/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Boot Straps, eFUSE Selection, and Internal OCRAM

free preview20 min

What you'll learn

  • Decode all BOOT_MODE values without treating a reserved value as a recovery mode.
  • Distinguish strap-selected media policy from fuse-selected policy.
  • Place pre-DDR work in an explicitly reserved OCRAM region rather than uninitialized external RAM.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP268/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

What Happens Electrically on Reset Before Any User Instruction Exists

  • When power stabilizes on an i.MX6ULL board and the external power-on-reset pin (POR_B) rises from 0 V…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP269/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

What Happens Electrically on Reset Before Any User Instruction Exists

  • As Figure 4.1a illustrates, the i.MX6ULL System Reset Controller (SRC, 0x020D8000) wires 26…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP270/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

What Happens Electrically on Reset Before Any User Instruction Exists

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP271/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

What Happens Electrically on Reset Before Any User Instruction Exists

fig_m04_l01_a.svg

Figure 4.1a: POR_B rising-edge strap capture circuit (top) and the four BMOD[1:0] Boot ROM selection paths governed by BT_FUSE_SEL (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP272/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Mental model

note: Reset-time boot selection works like an automated rail-yard switch tower that photographs a train's routing placard at the exact instant it crosses the entry sensor (POR_B): repainting the placard down the track (IOMUXC reuse or live DIP flips) never reroutes the train already on the spur.

Where it stops working: Unlike a mechanical rail placard, Boot ROM routing is also governed by permanent silicon eFUSE bits and active ROM workspace reservations inside OCRAM that remain live until handoff completes.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP273/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

System Reset Controller Boot Mode Registers (SRC_SBMR1/2) & Internal OCRAM

definition: At POR_B deassertion, SRC_SBMR2 (0x020D801C) latches BMOD[1:0] in bits [25:24] (00 Boot From Fuses, 01 Serial Downloader SDP, 10 Internal Boot, 11 Reserved) and reflects eFUSE override state in BT_FUSE_SEL (bit 4), while SRC_SBMR1 (0x020D8004) latches BOOT_CFG1..4 strap bytes (LCD_DATA00..23). Note that Internal Boot (10) means ROM-directed boot flow (fetching from external SD, eMMC, NAND, or QSPI), not that the image is stored inside the SoC! Before external DDR3 is trained, internal On-Chip RAM (OCRAM) provides 128 KiB of static SRAM at [0x00900000, 0x00920000)—with low bytes reserved by the active Boot ROM itself.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP274/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

What Happens Electrically on Reset Before Any User Instruction Exists

  • Step through the four-stage reset-to-handoff pipeline below to connect pad sampling, policy selection, and…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP275/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

What Happens Electrically on Reset Before Any User Instruction Exists

Step 1: At POR_B rising edge, SRC latches BOOT_MODE[1:0] and LCD_DATA00..23 into read-only SBMR2 and SBMR1.

Separate reset-latched boot policy and ROM-reserved low OCRAM from post-boot GPIO/LCD pin behavior.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP276/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Decoding SRC_SBMR1/2 Bitfields & Partitioning 128 KiB Internal OCRAM

  • Inspect Figure 4.1b to map the exact 32-bit register slices and physical SRAM boundaries used during…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP277/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Decoding SRC_SBMR1/2 Bitfields & Partitioning 128 KiB Internal OCRAM

  • Look equally closely at panel 3 of Figure 4.1b.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP278/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Decoding SRC_SBMR1/2 Bitfields & Partitioning 128 KiB Internal OCRAM

fig_m04_l01_b.svg

Figure 4.1b: Exact 32-bit bitfields of SRC_SBMR2 and SRC_SBMR1 alongside the 28 KiB ROM-reserved vs 100 KiB usable OCRAM split.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP279/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Decoding SRC_SBMR1/2 Bitfields & Partitioning 128 KiB Internal OCRAM

  • In boot_policy.c below, our freestanding ARMv7-A helper inspects SRC_SBMR2 strictly through a `volati…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP280/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

boot_policy.c

learner@ksa: ~/gl-e01/m04-l01
learner@ksa:~/gl-e01/m04-l01$ bat boot_policy.c File: boot_policy.c    #include <stdint.h>    static volatile const uint32_t *const src_sbmr2 =        (volatile const uint32_t *)(uintptr_t)0x020d801cU;    unsigned sampled_boot_mode(void)    {        return (*src_sbmr2 >> 24) & 3U;    }    unsigned fuse_policy_selected(void)    {        return (*src_sbmr2 >> 4) & 1U;    }    /* Read-only inspection: no fuse programming and no boot-media writes. */
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP281/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Proving SBMR1/SBMR2 Policy Decoding & Overflow-Safe OCRAM Bounds in C11

  • Before touching target hardware, sbmr_boot_decoder.c proves the complete Boot ROM decision tree and `OCRA…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP282/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

sbmr_boot_decoder.c

learner@ksa: ~/gl-e01/m04-l01
learner@ksa:~/gl-e01/m04-l01$ bat sbmr_boot_decoder.c File: sbmr_boot_decoder.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        enum boot_path { BP_FUSE, BP_USB_SERIAL, BP_INT_STRAPS, BP_INT_FUSE, BP_RESERVED };    enum boot_dev  { DEV_NONE, DEV_NOR_EIM, DEV_QSPI, DEV_SD_ESD, DEV_MMC_EMMC, DEV_NAND };        static enum boot_path decode_sbmr2(uint32_t sbmr2)    {        unsigned bmod = (sbmr2 >> 24) & 3U;        bool bt_fuse_sel = ((sbmr2 >> 4) & 1U) != 0U;        switch (bmod) {        case 0U: return BP_FUSE;
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP283/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Running the Host Policy Verifier & Freestanding Cortex-A7 Object Check

  • Compile and execute sbmr_boot_decoder.c on the workstation, then cross-compile boot_policy.c with `clan…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP284/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Running the Host Policy Verifier & Freestanding Cortex-A7 Object Check

learner@ksa: ~/gl-e01/m04-l01
learner@ksa:~/gl-e01/m04-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic sbmr_boot_decoder.c -o sbmr_boot_decoderlearner@ksa:~/gl-e01/m04-l01./sbmr_boot_decoderSBMR2=0x02000000->INT_STRAPS SBMR1=0x40->SD_ESD OCRAM_free=100KiBlearner@ksa:~/gl-e01/m04-l01clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -mfloat-abi=soft -ffreestanding -std=c11 -Wall -Wextra -Werror -x c -c boot_policy.c -o target.olearner@ksa:~/gl-e01/m04-l01
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP285/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Running the Host Policy Verifier & Freestanding Cortex-A7 Object Check

strace — learner@ksa: ~/gl-e01/m04-l01
learner@ksa:~/gl-e01/m04-l01strace -e trace=write,exit_group \./sbmr_boot_decoderwrite(1, "SBMR2=0x02000000->INT_STRAPS SBM"..., 66) = 66exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP286/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Separating Bench Symptoms Across Reset Edges, USB SDP & Image Headers

  • Keeping the reset-edge capture model in mind resolves three bench mysteries that routinely stall bring-up t…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP287/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: A custom carrier attaches an external 1 kOhm pull-up or active sensor output to LCD_DATA05 or LCD_DATA06, and cold power-on stops booting from SD even though BOOT_MODE[1:0] switches still select 10 (Internal Boot). Root Cause: LCD_DATA00..23 double as BOOT_CFG1/2/4 inputs sampled on the rising edge of POR_B; the 1 kOhm external load overpowered the weak 47 kOhm board strap resistor during reset, latching eMMC or NAND into SRC_SBMR1. Fix: Isolate or tri-state external drivers on LCD_DATA pins until POR_B completes, and read back SRC_SBMR1 (0x020D8004) over SDP or JTAG to confirm latched strap bits.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP288/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Linking an early pre-DDR diagnostic image directly at 0x00900000 (the base of physical OCRAM) causes USB SDP download or SD boot handoff to crash before reaching _start. Root Cause: Physical validity ([0x00900000, 0x00920000)) is not allocation ownership: the Boot ROM itself uses [0x00900000, 0x00907000) (28 KiB) for its live execution stack, MMU/cache descriptors, and SDP state during transfer. Fix: Place pre-DDR SPL images and stacks strictly inside the unreserved 100 KiB window [0x00907000, 0x00920000).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP289/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Guided Practice: Decoding SBMR1/SBMR2 & Sizing an OCRAM Stack

  • Solve each bring-up check step by step.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP290/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Independent Practice: USDHC Port & Bus-Width Strap Auditor

  • Extend sbmr_boot_decoder.c into boot_cfg_audit.c decoding BOOT_CFG2 (SBMR1[15:8]) alongside `BOOT_C…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP291/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Quick check

SRC_SBMR2 reports BMOD[1:0] == 2 (10_2, Internal Boot) and BT_FUSE_SEL == 1 (bit 4). How does the i.MX6ULL Boot ROM choose the boot medium?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP292/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Quick check

An engineer flips the BOOT_MODE DIP switch from SD (10) to Serial Downloader (01) while the board remains powered, yet a software warm routine still sees the old boot mode in SRC_SBMR2. What is the root cause?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP293/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Quick check

Retrieval: why does ocram_user_fits(0x00900000U, 0x1000U) reject address 0x00900000 even though physical OCRAM spans [0x00900000, 0x00920000)?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP294/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Summary

  • BOOT_MODE[1:0] (SRC_SBMR2[25:24]) selects 00 Fuse Boot, 01 USB/UART Serial Downloader (SDP recovery), 10 ROM-directed Internal Boot, or 11 Reserved (invalid).
  • Under Internal Boot (10), BT_FUSE_SEL (SBMR2[4]) chooses between reset-sampled BOOT_CFG pin straps (SRC_SBMR1) and permanent eFUSE configuration.
  • Strap pads (BOOT_MODE and LCD_DATA00..23) are latched on the rising edge of POR_B; post-reset peripheral pin reuse cannot alter SBMR1/SBMR2, whereas external loads during reset can corrupt media selection.
  • Internal 128 KiB OCRAM ([0x00900000, 0x00920000)) operates immediately before external DDR3 initialization, with [0x00907000, 0x00920000) (100 KiB) available above the Boot ROM's own workspace.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP295/523

GL-E01 · 4.1 (m04-l01)

>_ Kernel Systems Academy

Key takeaways

  • BOOT_MODE values 00, 01, 10, and 11 select fuse boot, serial downloader, internal boot, and a reserved encoding respectively.
  • Internal boot media policy can depend on sampled BOOT_CFG inputs or fuse-selected configuration.
  • No; ROM or handoff state may reserve part of that window.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP296/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

IVT, Boot Data, and SD/eMMC Offset Geometry

free preview20 min

What you'll learn

  • Decode the D1 00 20 40 IVT bytes without confusing mixed byte-order fields.
  • Translate SD/eMMC media offset 0x400 into compact-file and runtime coordinates.
  • Validate IVT self, entry, and Boot Data pointers against a stated image layout.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP297/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why Raw .bin Images Cannot Cold-Boot & The Three Coordinate Systems

  • When arm-none-eabi-objcopy -O binary bsp.elf bsp.bin strips an ELF executable down to raw ARMv7-A mac…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP298/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why Raw .bin Images Cannot Cold-Boot & The Three Coordinate Systems

  • Instead, on SD/eMMC media, the i.MX6ULL Boot ROM seeks 1 KiB (1024 B = 0x0400, **two 512-byte sec…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP299/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why Raw .bin Images Cannot Cold-Boot & The Three Coordinate Systems

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP300/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why Raw .bin Images Cannot Cold-Boot & The Three Coordinate Systems

fig_m04_l02_a.svg

Figure 4.2a: Exact alignment between SD/eMMC media offsets (M), compact .imx file offsets (F = M - 0x400), and runtime RAM addresses (R = start + M).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP301/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Mental model

note: Packing a bootable .imx container is like loading a pre-assembled equipment skid (F) onto bay 2 (M = 0x0400, LBA 2) of a flatbed railcar bound for a numbered factory floor (R = 0x877FF000 + M): bay position on the railcar, byte offset inside the skid, and floor coordinates at the destination are linked by exact arithmetic, never interchangeable zeros.

Where it stops working: Unlike physical cargo labels, the i.MX6ULL Boot ROM mixes big-endian 16-bit header lengths with little-endian 32-bit ARM runtime pointers inside the very same 32-byte table.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP302/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Image Vector Table (IVT) & Boot Data Structure

definition: The i.MX6ULL Image Vector Table (IVT) is a 32-byte structure at SD/eMMC media offset 0x0400 (LBA 2). Its 4-byte header D1 00 20 40 encodes tag 0xD1, big-endian 16-bit byte length 0x0020 (32), and version 0x40—which appears as 0x402000D1 when read as one 32-bit little-endian ARM word! Seven little-endian 32-bit pointers follow: entry (+0x04), reserved1 = 0 (+0x08), dcd (+0x0C), boot_data (+0x10), self (+0x14), csf = 0 (+0x18), and reserved2 = 0 (+0x1C). Immediately following (at self + 0x20), the 12-byte Boot Data record specifies little-endian start, length, and plugin (0 for normal boot images).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP303/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why Raw .bin Images Cannot Cold-Boot & The Three Coordinate Systems

  • Walk through the four-step coordinate conversion and pointer audit below before writing serializer code:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP304/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why Raw .bin Images Cannot Cold-Boot & The Three Coordinate Systems

Step 1: Anchor SD/eMMC media coordinates at M_ivt = 0x0400 (LBA 2, sparing MBR) and M_payload = 0x1000 (LBA 8).

Name the coordinate origin (media M, compact file F, or runtime RAM R) on every variable before adding or comparing offsets.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP305/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Mixed-Endian Header Bytes vs Little-Endian Runtime Pointers

  • Study Figure 4.2b carefully to avoid a classic endianness trap.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP306/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Mixed-Endian Header Bytes vs Little-Endian Runtime Pointers

fig_m04_l02_b.svg

Figure 4.2b: Byte-accurate anatomy of the 32-byte IVT and 12-byte Boot Data — big-endian 16-bit header length paired with little-endian 32-bit runtime pointers.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP307/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Mixed-Endian Header Bytes vs Little-Endian Runtime Pointers

  • In target.S below, .section .ivt, "a" emits image_ivt (32 B) and image_boot_data (12 B) using l…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP308/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m04-l02
learner@ksa:~/gl-e01/m04-l02$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .section .ivt, "a"    .global image_ivt    image_ivt:        .byte 0xd1, 0x00, 0x20, 0x40        .word _start        .word 0        .word image_dcd        .word image_boot_data        .word image_ivt        .word 0        .word 0
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP309/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Serializing & Auditing 44-Byte IVT + Boot Data Coordinates in ISO C11

  • Never overlay a packed C struct directly onto disk bytes when fields mix endianness.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP310/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

ivt_serializer_audit.c

learner@ksa: ~/gl-e01/m04-l02
learner@ksa:~/gl-e01/m04-l02$ bat ivt_serializer_audit.c File: ivt_serializer_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>    #include <string.h>        static void put_le32(unsigned char *p, uint32_t v)    {        for (unsigned i = 0U; i < 4U; ++i) p[i] = (unsigned char)(v >> (8U * i));    }    static uint32_t get_le32(const unsigned char *p)    {        return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP311/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Verifying Mixed-Endian Serialization & ARMv7-A Relocation Assembly

  • Build and run ivt_serializer_audit on the host, then assemble target.S with `clang --target=armv7a-none…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP312/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Verifying Mixed-Endian Serialization & ARMv7-A Relocation Assembly

learner@ksa: ~/gl-e01/m04-l02
learner@ksa:~/gl-e01/m04-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic ivt_serializer_audit.c -o ivt_serializer_auditlearner@ksa:~/gl-e01/m04-l02./ivt_serializer_auditIVT_word=0x402000D1 self=0x877FF400 bdata=0x877FF420 entry=0x87800000 LBA=2learner@ksa:~/gl-e01/m04-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m04-l02
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP313/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Verifying Mixed-Endian Serialization & ARMv7-A Relocation Assembly

strace — learner@ksa: ~/gl-e01/m04-l02
learner@ksa:~/gl-e01/m04-l02strace -e trace=write,exit_group \./ivt_serializer_auditwrite(1, "IVT_word=0x402000D1 self=0x877FF"..., 76) = 76exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP314/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Why ENTRY(_start) Never Populates IVT.entry Automatically

  • Connect what you just proved back to ELF linking from Module m01.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP315/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: Flashing firmware.imx onto a removable SD card with dd if=firmware.imx of=/dev/sdX bs=512 (forgetting seek=2) wipes the card's FAT/ext4 partition table while the board fails silently into USB Serial Downloader. Root Cause: Without seek=2 (1024 B), byte 0 of the compact .imx (0xD1) overwrites LBA 0 (MBR at 0x0000) instead of landing at 0x0400 (LBA 2), leaving payload padding at 0x0400 where the ROM checks for 0xD1. Fix: Always flash compact SD/eMMC .imx images with bs=512 seek=2 conv=fsync onto a verified removable block device.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP316/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: A small 4 KiB blinky boots fine from SD, but after growing the firmware to 48 KiB, execution crashes randomly inside functions linked near the end of .text. Root Cause: BootData.length (+0x24) was left hardcoded to 0x4000 (16 KiB) instead of updating to 0x1000 + payload_bytes, so the Boot ROM stopped reading SD sectors partway through .text and left upper code words holding uninitialized DDR3 garbage. Fix: Compute BootData.length programmatically from the final binary byte count (0x1000 + payload_size) and assert entry + payload_size <= start + length.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP317/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Guided Practice: Relocating an IVT Container Into Pre-DDR OCRAM

  • Re-derive all runtime pointers for an SD-booted SPL executing inside OCRAM without DCD.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP318/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Independent Practice: Multi-Media IVT Validator (SD/eMMC vs QSPI)

  • Extend ivt_serializer_audit.c into ivt_media_check.c parameterized by media_ivt (0x0400 for `SD/eMM…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP319/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Quick check

A compact .imx image begins directly with its IVT and will be written to SD/eMMC media offset 0x0400 (LBA 2). Where must a payload linked for media offset 0x1000 sit inside that compact .imx file?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP320/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Quick check

What do the four IVT header bytes D1 00 20 40 encode, and how do those exact same four bytes appear as one 32-bit little-endian ARM word?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP321/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Quick check

Retrieval: what value must IVT.self (+0x14) hold in our worked 0x87800000 SD layout (BootData.start = 0x877FF000)?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP322/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Summary

  • IVT header bytes D1 00 20 40 encode tag 0xD1, big-endian 16-bit length 32 (0x0020), and version 0x40, which read as 0x402000D1 in a 32-bit little-endian ARM word.
  • On SD/eMMC media, placing the IVT at media offset 0x0400 (1 KiB, LBA 2 across 512-byte sectors) preserves LBA 0..1 partitioning structures.
  • Translating any media offset M >= 0x0400 into a compact .imx file yields F = M - 0x0400 (0x1000 -> 0x0C00), while runtime addresses follow R = BootData.start + M = IVT.self + F.
  • IVT pointers (entry, dcd, boot_data, self) and Boot Data (start, length, plugin) serialize as little-endian 32-bit words with reserved fields strictly zero.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP323/523

GL-E01 · 4.2 (m04-l02)

>_ Kernel Systems Academy

Key takeaways

  • The IVT header bytes D1 00 20 40 encode tag D1, a big-endian length of 32, and version 40.
  • Those four bytes appear as 0x402000D1 when interpreted as one little-endian 32-bit word.
  • The IVT runtime address, not its compact-file offset.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP324/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

DCD Commands and a Checked C11 .imx Packer

free preview20 minlab lab_m04_ivt_dcd_image

What you'll learn

  • Encode big-endian DCD lengths, addresses, values, and masks.
  • Reject truncated or zero-length Write, Check, and NOP commands.
  • Construct a compact .imx artifact with independently checked IVT and payload offsets.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP325/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Solving the Cold-Boot Chicken-and-Egg Problem with DCD

  • Think through what happens on cold power-up when IVT.entry points to 0x87800000 in external DDR3 RAM.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP326/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Solving the Cold-Boot Chicken-and-Egg Problem with DCD

  • Examine Figure 4.3a to see how DCD bytecode is framed—and why naive .imx packers break on their ver…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP327/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Solving the Cold-Boot Chicken-and-Egg Problem with DCD

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP328/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Solving the Cold-Boot Chicken-and-Egg Problem with DCD

fig_m04_l03_a.svg

Figure 4.3a: Inside the 36-byte big-endian DCD stream (top), why put_le32() triggers silent ROM rejection (bottom left), and parser termination invariants (bottom right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP329/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Mental model

note: A DCD table is a pre-flight punch-card deck fed into the Boot ROM before the main engine (_start) starts: every card carries its own byte length and big-endian register coordinates, so one zero-length card or swapped endian helper derails every operation behind it.

Where it stops working: Unlike syntax-checking a punch card offline, proving that a DCD stream parses cleanly never proves that its register values match your PCB's DDR3 trace delays or voltage rails.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP330/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Device Configuration Data (DCD) & Structural vs Electrical Validation

definition: Device Configuration Data (DCD) is a bounded (<= 1768 B) big-endian Boot ROM command table (tag 0xD2, version 0x40) placed between Boot Data (0x002C in compact .imx) and the application payload (0x0C00), containing validated Write Data (0xCC), Check Data (0xCF), and NOP (0xC0) subcommands. Structural validity proves byte framing, big-endian encoding, and non-overlapping container offsets—it never proves that DDR3 timing or pad constants match an unmeasured physical board.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP331/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Solving the Cold-Boot Chicken-and-Egg Problem with DCD

  • Trace the four-stage encoding, validation, and .imx packing pipeline below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP332/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Solving the Cold-Boot Chicken-and-Egg Problem with DCD

Step 1: Emit DCD header [0xD2, BE16(total), 0x40] where 4 <= total <= 1768 includes the 4-byte header.

Keep LE32 IVT pointers and BE16/BE32 DCD commands strictly separated, and never equate valid framing with calibrated DDR3 timing.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP333/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Linker Coordinates, Container Packing & Pre-Boot Trade-Offs

  • Inspect Figure 4.3b alongside image_layout.lds below to see how linker addresses and .imx file offs…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP334/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Linker Coordinates, Container Packing & Pre-Boot Trade-Offs

fig_m04_l03_b.svg

Figure 4.3b: Exact byte map of verified.imx (3088 B) and the engineering boundary between structural container proof and board DDR3 calibration.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP335/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

image_layout.lds

learner@ksa: ~/gl-e01/m04-l03
learner@ksa:~/gl-e01/m04-l03$ bat image_layout.lds File: image_layout.lds    ENTRY(_start)    SECTIONS    {      image_load_base = 0x877ff000;      . = image_load_base + 0x400;      .ivt : { KEEP(*(.ivt)) }      .dcd : { KEEP(*(.dcd)) }      . = 0x87800000;      .text : { KEEP(*(.text.start)) *(.text*) }      .rodata ALIGN(4) : { *(.rodata*) }      .data ALIGN(4) : { *(.data*) }      image_load_end = .;      .bss (NOLOAD) : { *(.bss*) *(COMMON) }    }
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP336/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Implementing a Bounded DCD Validator & .imx Packer in ISO C11

  • Study valid_dcd() and main() in dcd_encoder_packer.c below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP337/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

dcd_encoder_packer.c

learner@ksa: ~/gl-e01/m04-l03
learner@ksa:~/gl-e01/m04-l03$ bat dcd_encoder_packer.c File: dcd_encoder_packer.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>    #include <string.h>        static void put_be16(unsigned char *p, uint16_t v)    {        p[0] = (unsigned char)(v >> 8); p[1] = (unsigned char)(v & 0xffU);    }    static uint16_t get_be16(const unsigned char *p)    {        return (uint16_t)(((uint16_t)p[0] << 8) | p[1]);
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP338/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Generating verified.imx & Inspecting the Syscall Write Trace

  • Compile and run dcd_encoder_packer below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP339/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Generating verified.imx & Inspecting the Syscall Write Trace

learner@ksa: ~/gl-e01/m04-l03
learner@ksa:~/gl-e01/m04-l03gcc -std=c11 -Wall -Wextra -Werror -pedantic dcd_encoder_packer.c -o dcd_encoder_packerlearner@ksa:~/gl-e01/m04-l03./dcd_encoder_packerverified.imx=3088B DCD=36B write_addr=02_0C_40_6C cmds=3learner@ksa:~/gl-e01/m04-l03
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP340/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Generating verified.imx & Inspecting the Syscall Write Trace

strace — learner@ksa: ~/gl-e01/m04-l03
learner@ksa:~/gl-e01/m04-l03strace -e trace=write,exit_group \./dcd_encoder_packerwrite(3, "\321\0 @\0\0\200\207\0\0\0\0,\364\177\207 \364\177\207\0\364\177\207\0\0\0\0\0\0\0\0"..., 3088) = 3088write(1, "verified.imx=3088B DCD=36B write"..., 57) = 57exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP341/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Coalescing Multi-Pair DCD Writes & Bridging to C Runtime Startup

  • Notice how byte budgeting works when a real MMDC DDR3 init script needs ~65 register writes: emitting…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP342/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Reusing put_le32() from IVT serialization to write 0x020C406C into a DCD Write Data (0xCC) command causes the i.MX6ULL Boot ROM to reject the image and enter USB Serial Downloader with zero UART output. Root Cause: Little-endian bytes 6C 40 0C 02 are parsed by the big-endian DCD interpreter as physical address 0x6C400C02, which falls outside the Boot ROM's allowed DCD address windows (MMDC, IOMUXC, CCM). Fix: Always serialize IVT/Boot Data pointers with put_le32() and every DCD length, address, value, mask, and count with put_be16()/put_be32().

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP343/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Expanding a DCD register list pushes total_dcd_len past 1768 B (or past 0x0C00 - 0x002C), causing silent ROM rejection or corrupting the first instructions of _start. Root Cause: Emitting uncoalesced 12-byte 0xCC commands without asserting total <= 1768U and 0x2C + total <= payload_offset overflows the Boot ROM's fixed DCD buffer limit. Fix: Coalesce adjacent 0xCC pairs (4 + 8 * k bytes) and fail the host packer build whenever total > 1768U or 0x2C + total > payload_offset.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP344/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Guided Practice: Encoding a Coalesced 2-Pair Set-Mask DCD Command

  • Compute the exact hex framing by hand.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP345/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Independent Practice: Whitelist-Aware Coalescing DCD Compiler

  • Extend dcd_encoder_packer.c into dcd_range_compiler.c that validates every target addr against allowe…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP346/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Quick check

While scanning a DCD buffer, valid_dcd() encounters a subcommand whose big-endian length field equals 0x0000. What must the parser do?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP347/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Quick check

In what exact byte order must register address 0x020C406C (CCM_CCGR1) appear inside a DCD Write Data (0xCC) command?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP348/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Quick check

Retrieval: what engineering claim does generating and validating verified.imx (3088 B) on the host legitimately prove?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP349/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Lab: lab_m04_ivt_dcd_image

  • Repair endian serialization, IVT coordinate relationships, DCD framing validation, and bounded compact-imag…

Verify: ./check.sh exits 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP350/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Summary

  • A DCD table begins with [0xD2, len_be16, 0x40], where total_len (4 <= total_len <= 1768 B) includes the 4-byte header itself.
  • Write Data (0xCC), Check Data (0xCF), and NOP (0xC0) serialize all 16-bit lengths and 32-bit addresses, values, masks, and counts in big-endian order—distinct from little-endian IVT pointers.
  • Bounded parsers must verify len >= 4 and len <= total - at before advancing, preventing zero-length infinite loops and buffer overruns.
  • Structural .imx validation proves container framing and 0x00 / 0x20 / 0x2C / 0x0C00 offsets, never board-specific DDR3 calibration.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP351/523

GL-E01 · 4.3 (m04-l03)

>_ Kernel Systems Academy

Key takeaways

  • A DCD header uses tag D2 and a big-endian total length including its header.
  • Write Data command CC contains command framing followed by big-endian address and value data.
  • Its tested structure and offsets, not a calibrated cold boot.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP352/523

m05/

>_ Kernel Systems Academy

C runtime startup, linker placement, and peripheral headers

  1. 5.1 start.S, SVC Entry, Data Copy, and BSS Initialization
  2. 5.2 GNU ld Scripts, VMA/LMA, and Linker Boundary Symbols
  3. 5.3 Struct-Mapped MMIO and Auditable SDK Header Adaptation

lab → lab_m05_lds_crt_sdk

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP353/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

start.S, SVC Entry, Data Copy, and BSS Initialization

free preview20 min

What you'll learn

  • Establish an aligned SVC stack before calling any C function.
  • Copy the linked .data initialization span when its load and execution addresses differ.
  • Zero exactly the linker-defined .bss range without damaging adjacent state.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP354/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

What ISO C11 Quietly Assumes Before main() Executes Its First Line

  • On a desktop Linux workstation, writing `int g_retry = 3; static int g_err_cnt; int main(void) { ...
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP355/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

What ISO C11 Quietly Assumes Before main() Executes Its First Line

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP356/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

What ISO C11 Quietly Assumes Before main() Executes Its First Line

fig_m05_l01_a.svg

Figure 5.1a: CPSR Supervisor/interrupt-mask configuration (top) and physical RAM transformation performed by start.S before bl main (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP357/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Mental model

note: Entering C main() on bare silicon without start.S is like walking surgeons into an operating theater before facilities turn on power (OCRAM/DDR3), clear stray trays (.bss), unpack calibrated instruments (.data LMA -> VMA), or lock the operating table (8-byte-aligned SP_svc): every assumption C makes fails on contact.

Where it stops working: Unlike preparing a room once, start.S executes with zero stack until ldr sp completes, relies on prior OCRAM/DDR3 readiness, and must honor exact half-open linker address intervals.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP358/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Freestanding C Runtime Startup (crt0 / start.S)

definition: In a freestanding ARMv7-A binary, start.S (crt0) is the stackless assembly entry routine at _start that establishes every hardware precondition assumed by ISO C11 and AAPCS: (1) masking asynchronous IRQ/FIQ (CPSR.I = 1, F = 1) and selecting Supervisor mode (SVC, 0x13) from a compatible privileged state; (2) loading an 8-byte-aligned SP_svc = __stack_top in live RAM; (3) copying initialized writable .data from __data_load (LMA) to [__data_start, __data_end) (VMA) when LMA != VMA; (4) zeroing [__bss_start, __bss_end); and (5) calling bl main followed by a deterministic wfi halt loop.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP359/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

What ISO C11 Quietly Assumes Before main() Executes Its First Line

  • Step through the four-stage runtime bring-up sequence below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP360/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

What ISO C11 Quietly Assumes Before main() Executes Its First Line

Step 1: Enter _start in 32-bit ARM privileged state with OCRAM or DCD-trained DDR3 already backing RAM.

Never execute a C call before SP_svc, .data relocation, and .bss zeroing all complete.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP361/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Half-Open Loop Invariants, Unsigned BHS & Stackless Discipline

  • Inspect Figure 5.1b alongside target.S below to see why every instruction in _start is chosen delib…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP362/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Half-Open Loop Invariants, Unsigned BHS & Stackless Discipline

  • Third, trace the .data and .bss word loops in Figure 5.1b: why cmp r0, r1 followed by bhs (…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP363/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Half-Open Loop Invariants, Unsigned BHS & Stackless Discipline

fig_m05_l01_b.svg

Figure 5.1b: Pre-checked unsigned half-open [start, end) word loops (left) contrasted against three classic bare-metal startup bugs (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP364/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

target.S

learner@ksa: ~/gl-e01/m05-l01
learner@ksa:~/gl-e01/m05-l01$ bat target.S File: target.S    .syntax unified    .cpu cortex-a7    .arm    .section .text.start, "ax"    .global _start    _start:        mrs r0, cpsr        bic r0, r0, #0x1f        orr r0, r0, #0x13        orr r0, r0, #0xc0        msr cpsr_c, r0        ldr sp, =__stack_top        ldr r0, =__data_start        ldr r1, =__data_end
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP365/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Sentinel-Poisoned Relocation & BSS Boundary Proof in ISO C11

  • In lds_vma_lma_solver.c below, we poison a 64-byte simulated RAM arena with 0xA5, place 8 initializ…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP366/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

lds_vma_lma_solver.c

learner@ksa: ~/gl-e01/m05-l01
learner@ksa:~/gl-e01/m05-l01$ bat lds_vma_lma_solver.c File: lds_vma_lma_solver.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>    #include <string.h>        struct crt_syms {        uint32_t text_base, etext, data_load, data_start, edata;        uint32_t bss_start, bss_end, stack_top;    };        static struct crt_syms solve_syms(uint32_t base, uint32_t text_ro,                                      uint32_t data_vma, uint32_t data_sz,
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP367/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Running the Relocation Solver & Assembling start.S for Cortex-A7

  • Compile and run lds_vma_lma_solver.c on the workstation, then assemble target.S into ARMv7-A object c…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP368/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Running the Relocation Solver & Assembling start.S for Cortex-A7

learner@ksa: ~/gl-e01/m05-l01
learner@ksa:~/gl-e01/m05-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic lds_vma_lma_solver.c -o lds_vma_lma_solverlearner@ksa:~/gl-e01/m05-l01./lds_vma_lma_solveretext=0x87800090 data_vma=0x87810000..0x87810008 bss_end=0x87810018 sp=0x87820000learner@ksa:~/gl-e01/m05-l01clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m05-l01
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP369/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Running the Relocation Solver & Assembling start.S for Cortex-A7

strace — learner@ksa: ~/gl-e01/m05-l01
learner@ksa:~/gl-e01/m05-l01strace -e trace=write,exit_group \./lds_vma_lma_solverwrite(1, "etext=0x87800090 data_vma=0x8781"..., 82) = 82exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP370/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Flat RAM Builds vs Split XIP Builds & Linker Contract Coupling

  • Notice two subtle couplings between start.S and the rest of the boot chain.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP371/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Zero-initialized globals (static int count;) start at 0 as expected, yet initialized globals (int baud = 115200;) hold random garbage inside main(). Root Cause: The linker script placed .data at VMA 0x87810000 using AT(_etext) (LMA 0x87800090), and start.S zeroed .bss while omitting the __data_load -> __data_start relocation loop (or BootData.length stopped before _etext + SIZEOF(.data)). Fix: Always copy [__data_load, __data_load + (__data_end - __data_start)) to __data_start when __data_load != __data_start and verify BootData.length covers the packed .data LMA tail.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP372/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: When a diagnostic main() returns 0 at the end of a self-test, the board unexpectedly fires random GPIO writes or triggers an Undefined Instruction exception. Root Cause: start.S branched to main with b main (leaving LR pointing back into Boot ROM) or omitted a halt loop after bl main, allowing execution to fall straight into literal pools (=0x87820000) assembled immediately after _start! Fix: Always invoke bl main and follow it immediately with 5: wfi; b 5b.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP373/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Guided Practice: Solving QSPI-to-OCRAM Relocation Symbols

  • Trace a split XIP build where .text + .rodata (0x1240 B) execute in QSPI flash at 0x60001000 wh…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP374/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Independent Practice: Overlap-Safe Memmove & Arbitrary-Byte Tail Verifier

  • Extend lds_vma_lma_solver.c into crt_reloc_verifier.c supporting unrounded byte counts (data_sz = 7 B…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP375/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Quick check

On a cold bare-metal boot, static uint32_t rx_errors; (declared at file scope with no explicit initializer) reads 0xA5A5A5A5 on the first line of main(). Which start.S obligation failed?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP376/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Quick check

Why does start.S load three distinct symbols (__data_load, __data_start, and __data_end) to initialize .data?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP377/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Quick check

Retrieval: why is inserting bl early_uart_banner as the very first instruction of _start (before ldr sp, =__stack_top) a severe bare-metal defect?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP378/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Summary

  • Bare-metal _start must mask IRQ/FIQ (0xC0), select SVC mode (0x13), and load a valid, reserved SP_svc = __stack_top before calling any C function.
  • AAPCS requires 8-byte stack alignment ((SP & 7) == 0) at every public call interface (bl main).
  • When .data's Load Memory Address (__data_load) differs from its execution Virtual Memory Address (__data_start), start.S copies [__data_load, __data_load + size) before C reads writable globals.
  • Pre-checked unsigned cmp + bhs loops zero [__bss_start, __bss_end) cleanly without touching [__bss_end] or misfiring on 0-byte sections.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP379/523

GL-E01 · 5.1 (m05-l01)

>_ Kernel Systems Academy

Key takeaways

  • Bare-metal startup must establish a valid stack before calling C code that can use it.
  • A public AAPCS call boundary requires eight-byte stack alignment.
  • The helper may use an invalid stack before its preconditions hold.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP380/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

GNU ld Scripts, VMA/LMA, and Linker Boundary Symbols

free preview20 min

What you'll learn

  • Place the entry section and data spans explicitly with ENTRY and SECTIONS.
  • Compute VMA/LMA relocation and the serialized image extent.
  • Assert aligned non-overlapping BSS and stack boundaries.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP381/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

How GNU ld Turns Relocatable .o Files Into Dual-Address Firmware Maps

  • In Lesson m05-l01, start.S consumed six magic names—__stack_top, __data_load, __data_start, `__da…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP382/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

How GNU ld Turns Relocatable .o Files Into Dual-Address Firmware Maps

  • Trace Figure 5.2a from left to right to understand the two distinct addresses every output section poss…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP383/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

How GNU ld Turns Relocatable .o Files Into Dual-Address Firmware Maps

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP384/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

How GNU ld Turns Relocatable .o Files Into Dual-Address Firmware Maps

fig_m05_l02_a.svg

Figure 5.2a: Packed 152-byte LMA load image (left) versus sparse runtime VMA execution map with 8-byte-aligned banked IRQ and SVC stacks (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP385/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Mental model

note: A linker script (bsp.lds) is the civil blueprint that assigns both compact shipping-container slots (LMA for objcopy / SD transfer) and anchored factory floor pads (VMA for C execution): startup cranes (start.S) read the blueprint's survey stakes (linker boundary symbols) directly rather than opening crates to look for coordinates inside.

Where it stops working: Unlike paper survey stakes, ELF linker symbols live solely in the symbol table (allocating zero bytes in RAM), and link-time ASSERTs verify address arithmetic without proving physical DDR3 is powered or trained.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP386/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

GNU ld Location Counter (.), VMA vs AT(LMA) & Symbol-Address Semantics

definition: Inside a GNU ld SECTIONS block, ENTRY(_start) records the ELF entry symbol, . (the location counter) tracks the current output VMA, and ALIGN(n) rounds upward to ( . + n - 1 ) & ~(n - 1) (leaving already-aligned addresses unchanged). ADDR(sec) yields runtime VMA while LOADADDR(sec) yields AT(...) load placement. A script assignment __bss_start = .; creates an ELF symbol-table entry whose address equals . (allocating zero storage bytes in RAM!), defining the exact [__bss_start, __bss_end) interval zeroed by start.S.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP387/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

How GNU ld Turns Relocatable .o Files Into Dual-Address Firmware Maps

  • Walk through the four-stage contract connecting input .o sections, bsp.lds policy, boundary symbols, an…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP388/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

How GNU ld Turns Relocatable .o Files Into Dual-Address Firmware Maps

Step 1: Set ENTRY(_start) and place KEEP(*(.text.start)) first in .text so _start stays pinned at 0x87800000.

Linker boundary symbols are an executable contract: define them inside aligned output sections and reference their addresses, never their dereferenced values.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP389/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Linker Symbol Address-vs-Value Semantics & Exact ALIGN Placement

  • Study Figure 5.2b carefully—it illustrates one of the most notorious C firmware bugs in industry.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP390/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Linker Symbol Address-vs-Value Semantics & Exact ALIGN Placement

  • Check two placement rules inside bsp.lds below as well.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP391/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Linker Symbol Address-vs-Value Semantics & Exact ALIGN Placement

fig_m05_l02_b.svg

Figure 5.2b: Why linker symbols must be declared as char arrays in C (top) and how ALIGN(4) brackets [__bss_start, __bss_end) for word loops (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP392/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

bsp.lds

learner@ksa: ~/gl-e01/m05-l02
learner@ksa:~/gl-e01/m05-l02$ bat bsp.lds File: bsp.lds    ENTRY(_start)    SECTIONS    {      . = 0x87800000;      .text : { KEEP(*(.text.start)) *(.text*) }      .rodata ALIGN(4) : { *(.rodata*) }      __data_lma = ALIGN(., 4);      .data 0x87810000 : AT(__data_lma)      {        __data_start = .;        *(.data*)        . = ALIGN(4);        __data_end = .;      }
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP393/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Verifying Overflow-Safe ALIGN() & Dual-Mode Stack Partitioning in C11

  • When firmware enables GICv2 interrupts (GL-E02), _start partitions both SP_irq (4 KiB) and `S…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP394/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

reset_sp_bss_verifier.c

learner@ksa: ~/gl-e01/m05-l02
learner@ksa:~/gl-e01/m05-l02$ bat reset_sp_bss_verifier.c File: reset_sp_bss_verifier.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        static bool align_up(uint32_t value, uint32_t alignment, uint32_t *out)    {        if (alignment == 0U || (alignment & (alignment - 1U)) != 0U) return false;        uint32_t add = alignment - 1U;        if (value > UINT32_MAX - add) return false;        *out = (value + add) & ~add;        return true;    }
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP395/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Executing the Linker-Map & Banked-Stack Verifier

  • Compile and run reset_sp_bss_verifier.c below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP396/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Executing the Linker-Map & Banked-Stack Verifier

learner@ksa: ~/gl-e01/m05-l02
learner@ksa:~/gl-e01/m05-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic reset_sp_bss_verifier.c -o reset_sp_bss_verifierlearner@ksa:~/gl-e01/m05-l02./reset_sp_bss_verifierLMA=0x87800090 packed=152B bss_end=0x87810048 irq_sp=0x87811048 svc_sp=0x87813048learner@ksa:~/gl-e01/m05-l02
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP397/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Executing the Linker-Map & Banked-Stack Verifier

strace — learner@ksa: ~/gl-e01/m05-l02
learner@ksa:~/gl-e01/m05-l02strace -e trace=write,exit_group \./reset_sp_bss_verifierwrite(1, "LMA=0x87800090 packed=152B bss_e"..., 82) = 82exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP398/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Auditing Orphan Sections with readelf -l & nm -n

  • Even with bsp.lds in place, compilers occasionally emit auxiliary sections—such as .ARM.exidx (unwind t…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP399/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: A C startup helper declares extern uint32_t __bss_start, __bss_end; and calls memset((void *)__bss_start, 0, __bss_end - __bss_start), immediately crashing or zeroing address 0x00000000. Root Cause: Evaluating __bss_start as a uint32_t variable loads the word stored at __bss_start instead of taking the linker symbol's address. Fix: Declare linker boundary symbols as extern char __bss_start[], __bss_end[]; (using (size_t)(__bss_end - __bss_start)), where array decay yields the symbol table address directly without dereferencing RAM.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP400/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: Adding -ffunction-sections -fdata-sections -Wl,--gc-sections shrinks bsp.bin, but the board stops booting because bsp.bin is 0 bytes (or starts inside an unrelated C helper at 0x87800000). Root Cause: Without ENTRY(_start) and KEEP(*(.text.start)) at the top of .text, linker garbage collection prunes _start (and .ivt) because no C function references them. Fix: Root reachability with ENTRY(_start) and wrap .ivt, .dcd, .vectors, and .text.start in KEEP(...).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP401/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Guided Practice: Binary Gap Bloat & Banked SP Initialization

  • Check three linker mechanics by hand.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP402/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Independent Practice: Multi-Section Overlap & Orphan Auditor (lds_map_auditor.c)

  • Extend reset_sp_bss_verifier.c into lds_map_auditor.c checking an array of section records (name, `vm…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP403/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Quick check

In bsp.lds, .data is linked with VMA = 0x87810000 (ADDR(.data)) and LMA = 0x87800090 (LOADADDR(.data)). Which address do compiled C functions access when reading or writing a global variable in .data?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP404/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Quick check

Where must __bss_start = .; and __bss_end = .; be placed when start.S clears .bss using 4-byte str r2, [r0], #4 stores?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP405/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Quick check

Retrieval: does writing ENTRY(_start) inside bsp.lds automatically populate the IVT.entry pointer inside a raw .imx boot image?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP406/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Summary

  • ENTRY(_start) plus KEEP(*(.text.start)) at the start of .text pins the reset entry point at 0x87800000 and protects it from --gc-sections.
  • The location counter . tracks current VMA, while ALIGN(n) rounds upward to power-of-two boundaries without advancing already-aligned addresses.
  • .data VMA : AT(LMA) separates runtime ADDR(.data) from packed LOADADDR(.data), preventing multi-kilobyte zero-padding bloat in objcopy -O binary outputs while (NOLOAD) omits .bss and .stack bytes.
  • Linker boundary symbols (__bss_start, __bss_end, __stack_top) encode addresses in .symtab (never allocated C pointer variables) and pair with ASSERT(...) to catch alignment or RAM overflows at link time.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP407/523

GL-E01 · 5.2 (m05-l02)

>_ Kernel Systems Academy

Key takeaways

  • ENTRY selects the ELF entry symbol, while SECTIONS controls placement of linked output sections.
  • The location counter denotes the current linked address; ALIGN adjusts placement to the required boundary.
  • No; the boot structure must carry the matching entry value.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP408/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Struct-Mapped MMIO and Auditable SDK Header Adaptation

free preview20 minlab lab_m05_lds_crt_sdk

What you'll learn

  • Verify GPIO struct offsets with offsetof and static assertions.
  • Explain why volatile is neither an atomic update nor a CPU memory barrier.
  • Adapt register and pin identifiers through a narrow board wrapper without copying opaque policy.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP409/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

How C Struct Overlays Map Onto Silicon Decode Comparators

  • In early assembly experiments (m03-l03), poking 0x0209C000 (GPIO1_DR) and 0x0209C004 (GPIO1_GDIR)…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP410/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

How C Struct Overlays Map Onto Silicon Decode Comparators

  • Look at the right panel of Figure 5.3a to see why two well-intentioned "defensive" C habits actually **…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP411/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

How C Struct Overlays Map Onto Silicon Decode Comparators

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP412/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

How C Struct Overlays Map Onto Silicon Decode Comparators

fig_m05_l03_a.svg

Figure 5.3a: Verified 32-byte struct gpio_regs layout (left) and why packed attributes and C bit-fields fail on ARMv7-A peripheral buses (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP413/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Mental model

note: A C register struct overlay is a machined drilling jig clamped over an engine block (0x0209C000): if every bushing lines up to the byte (_Static_assert + offsetof), every 32-bit ldr/str hits the intended register; if one member shifts by 4 bytes, you bore through a neighboring register with zero syntax errors.

Where it stops working: Even a millimeter-accurate struct jig constrains only C byte offsets and compiler lvalue caching—it never makes read-modify-write updates atomic, inserts ARMv7-A memory barriers, or replaces W1C hardware rules.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP414/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Struct-Mapped MMIO Overlay, volatile Semantics & SDK Header Layering

definition: A struct-mapped MMIO overlay binds naturally aligned volatile uint32_t fields (and uint32_t _pad[k] reserved gaps) to documented hardware byte offsets verified at compile time via _Static_assert(offsetof(T, member) == offset, ...). Qualifying members volatile prevents compiler dead-store removal and register caching, without making load -> modify -> store sequences atomic or inserting CPU memory barriers (dsb). Clean SDK header adaptation keeps immutable silicon offsets in an SoC header while isolating carrier-specific pin assignments and active-low polarity inside a narrow board wrapper.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP415/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

How C Struct Overlays Map Onto Silicon Decode Comparators

  • Step through the four-stage workflow for engineering auditable MMIO headers below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP416/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

How C Struct Overlays Map Onto Silicon Decode Comparators

Step 1: Record peripheral base addresses, 32-bit register offsets, reserved gaps, and RW/RO/W1C contracts.

Prove every struct offset with _Static_assert(offsetof(...)), use explicit 32-bit masks, and separate silicon facts from board polarity.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP417/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Volatile Lost-Update Proof & Three-Tier SDK Adaptation

  • Examine Figure 5.3b to separate two distinct boundaries that vendor SDKs routinely blur.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP418/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Volatile Lost-Update Proof & Three-Tier SDK Adaptation

  • In panel 2 of Figure 5.3b, observe how to adapt a vendor header cleanly instead of dumping a 40,000…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP419/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Volatile Lost-Update Proof & Three-Tier SDK Adaptation

fig_m05_l03_b.svg

Figure 5.3b: Why volatile RMW still loses concurrent updates without serialization (top) and the three-tier SDK adaptation boundary (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP420/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

gpio_regs.h

learner@ksa: ~/gl-e01/m05-l03
learner@ksa:~/gl-e01/m05-l03$ bat gpio_regs.h File: gpio_regs.h    #ifndef KSA_GPIO_REGS_H    #define KSA_GPIO_REGS_H    #include <stddef.h>    #include <stdint.h>    struct gpio_regs {        volatile uint32_t DR;        volatile uint32_t GDIR;        volatile const uint32_t PSR;        volatile uint32_t ICR1, ICR2, IMR, ISR, EDGE_SEL;    };    _Static_assert(offsetof(struct gpio_regs,GDIR)==0x04U,"GDIR offset");    _Static_assert(offsetof(struct gpio_regs,PSR)==0x08U,"PSR offset");    _Static_assert(offsetof(struct gpio_regs,ISR)==0x18U,"ISR offset");    _Static_assert(sizeof(struct gpio_regs)==0x20U,"GPIO span");
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP421/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Proving Offset Geometry, Interleaved RMW Loss & -MMD -MP Guards in C11

  • In mmio_map_dep_auditor.c below, we verify three interlocking firmware invariants on the host: (1) `_Stat…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP422/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

mmio_map_dep_auditor.c

learner@ksa: ~/gl-e01/m05-l03
learner@ksa:~/gl-e01/m05-l03$ bat mmio_map_dep_auditor.c File: mmio_map_dep_auditor.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>    #include <string.h>        struct gpio_overlay {        uint32_t dr, gdir, psr, icr1, icr2, imr, isr, edge_sel;    };    _Static_assert(offsetof(struct gpio_overlay, gdir) == 0x04U, "GDIR");    _Static_assert(offsetof(struct gpio_overlay, psr) == 0x08U, "PSR");    _Static_assert(offsetof(struct gpio_overlay, isr) == 0x18U, "ISR");    _Static_assert(sizeof(struct gpio_overlay) == 0x20U, "GPIO span");
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP423/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Cross-Checking Struct Layout on Both Host GCC & Target ARMv7-A Clang

  • Run the host verifier and cross-compile gpio_regs.h directly with `clang --target=armv7a-none-eabi ...
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP424/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Cross-Checking Struct Layout on Both Host GCC & Target ARMv7-A Clang

learner@ksa: ~/gl-e01/m05-l03
learner@ksa:~/gl-e01/m05-l03gcc -std=c11 -Wall -Wextra -Werror -pedantic -MMD -MP mmio_map_dep_auditor.c -o mmio_map_dep_auditorlearner@ksa:~/gl-e01/m05-l03./mmio_map_dep_auditorgpio=32B ISR=+24 lost=2 ser=0x80000003 bin=152B imx=3224B mp=oklearner@ksa:~/gl-e01/m05-l03clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -mfloat-abi=soft -ffreestanding -std=c11 -Wall -Wextra -Werror -Wno-unused-function -x c -c gpio_regs.h -o target.olearner@ksa:~/gl-e01/m05-l03
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP425/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Cross-Checking Struct Layout on Both Host GCC & Target ARMv7-A Clang

strace — learner@ksa: ~/gl-e01/m05-l03
learner@ksa:~/gl-e01/m05-l03strace -e trace=write,exit_group \./mmio_map_dep_auditorwrite(1, "gpio=32B ISR=+24 lost=2 ser=0x80"..., 64) = 64exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP426/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

From Bare-Metal Physical Casts to Linux 6.12 ioremap() & Modular BSPs

  • Keep two architectural horizons clear as your codebase grows.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP427/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Annotating struct gpio_regs with __attribute__((packed)) causes freestanding Cortex-A7 builds (-mno-unaligned-access) to trigger Data Aborts or corrupt peripheral state whenever writing gpio->DR. Root Cause: packed sets alignof(struct gpio_regs) == 1, forcing the compiler to assume misaligned pointers and emit four 8-bit strb instructions instead of a single 32-bit str across the AIPS bus. Fix: Remove packed from aligned 32-bit MMIO structs, pad reserved gaps explicitly with volatile uint32_t _pad[N], and verify offsets with _Static_assert(offsetof(...)).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP428/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Modeling GPIO1_ISR (+0x18) as a 32-bit C bit-field union and executing gpio->ISR_bits.pin18 = 1U; silently drops simultaneous interrupts on sibling pins in GPIO1. Root Cause: Bit-field assignment compiles to ldr + orr + str (RMW), replaying 1s onto every already-set bit in the W1C status register and clearing unhandled interrupts! Fix: Keep volatile uint32_t ISR; as an undivided 32-bit word and write (UINT32_C(1) << pin) directly via gpio_ack_mask().

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP429/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Guided Practice: Sizing Reserved Gap Arrays for CCM (0x020C4000)

  • Design a sparse struct ccm_slice overlay without packed covering CCSR (+0x0C), CACRR (+0x10), a…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP430/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Independent Practice: Multi-Peripheral Header Suite (soc_headers_audit.c)

  • Author a self-contained soc_headers_audit.c defining assertion-locked overlays for GPIO (0x20 B), `CC…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP431/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Quick check

During SDK header adaptation, a missing reserved word shifts ISR inside struct gpio_regs to byte offset 0x14 instead of 0x18. What catches that defect deterministically at compile time?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP432/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Quick check

Why does updating gpio->DR |= (1U << 3); on a volatile uint32_t DR member still require exclusive bank ownership or interrupt masking?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP433/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Quick check

Retrieval: how must a struct-based inline helper acknowledge pending interrupt mask inside the Write-One-to-Clear (W1C) ISR member?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP434/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Lab: lab_m05_lds_crt_sdk

  • Repair overflow-safe alignment, initialized-data copy, exact BSS clearing, and GPIO header geometry without…

Verify: ./check.sh exits 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP435/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Summary

  • Every member of a struct-mapped MMIO overlay must sit at its exact documented peripheral byte offset, proven at compile time with offsetof and _Static_assert (never __attribute__((packed))).
  • volatile prevents compiler load/store removal and merging, yet provides neither atomic read-modify-write execution nor ARMv7-A CPU memory barriers.
  • Explicit 32-bit unsigned masks avoid implementation-defined C bit-field ordering, narrow byte loads (ldrb/strb), and accidental RMW on W1C status registers (gpio->ISR = mask).
  • Clean SDK adaptation keeps silicon base addresses and register structs separate from carrier-board pin assignments and active-low polarity wrappers.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP436/523

GL-E01 · 5.3 (m05-l03)

>_ Kernel Systems Academy

Key takeaways

  • A struct-mapped register header must place every member at the peripheral's exact documented byte offset.
  • Volatile-qualified accesses constrain compiler treatment but do not make a read-modify-write atomic.
  • Assign the acknowledgment mask directly to the ISR member.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP437/523

m06/

>_ Kernel Systems Academy

Modular BSP, transistor-switched buzzer, and polled key capstone

  1. 6.1 Modular BSP Layout and Dependency-Correct Make Rules
  2. 6.2 Transistor Buzzer Switching and Safe GPIO Polarity
  3. 6.3 Capstone: A Nonblocking Polled Key and Modular BSP

lab → lab_m06_modular_bsp_io

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP438/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Modular BSP Layout and Dependency-Correct Make Rules

free preview20 min

What you'll learn

  • Partition clock, indicator, delay, and application responsibilities into narrow modules.
  • Write pattern rules that retain source paths and generate header dependencies.
  • Diagnose a stale object caused by a missing header dependency.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP439/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Why Moving Files Into Folders Is Not Modularity Without a Header Firewall

  • Once a bare-metal codebase grows beyond start.S and one main.c into clock gating, status LEDs, buzzer…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP440/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Why Moving Files Into Folders Is Not Modularity Without a Header Firewall

  • Study Figure 6.1a to see how a real production Board Support Package (BSP) establishes both **arc…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP441/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Why Moving Files Into Folders Is Not Modularity Without a Header Firewall

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP442/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Why Moving Files Into Folders Is Not Modularity Without a Header Firewall

fig_m06_l01_a.svg

Figure 6.1a: Three-layer bare-metal BSP header firewall — main.c never sees soc_regs.h, bounding both coupling and incremental rebuilds.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP443/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Mental model

note: A modular BSP and its Makefile work like a multi-sheet electrical schematic paired with an automated netlist checker: neat sheet boundaries isolate raw peripheral stages (soc_regs.h) from control logic (main.c), while the netlist (-MMD -MP .d files) ensures changing one header constant flags every affected .o target automatically.

Where it stops working: Unlike visual folder trees in an editor, GNU Make knows nothing about directories unless explicit pattern rules mirror paths under obj/ and -include splices generated .d prerequisite edges into the DAG.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP444/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Modular BSP Ownership & GCC -MMD -MP Dependency Generation

definition: A modular Board Support Package (BSP) partitions clock, indicator, timing, and application code behind narrow public headers while GNU Make schedules rebuilds strictly from its declared Directed Acyclic Graph (DAG) of target prerequisites—never from folder layout. Passing -MMD tells GCC/Clang to emit a .d file listing every non-system #include consumed by that .o, -MP appends empty phony header.h: rules so renamed or deleted headers do not break make, and -include $(OBJ:.o=.d) splices those edges into subsequent incremental builds.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP445/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Why Moving Files Into Folders Is Not Modularity Without a Header Firewall

  • Step through the four-stage ownership and dependency loop below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP446/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Why Moving Files Into Folders Is Not Modularity Without a Header Firewall

Step 1: Keep project/main.c purely logical (led_set, delay_ms) with zero raw MMIO literals.

Pair narrow subsystem headers with path-preserving obj/%.o rules and -include $(OBJ:.o=.d) so neither object collisions nor stale headers can survive.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP447/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Path-Preserving Object Rules, Phony Header Guards & ABI Consistency

  • Inspect Figure 6.1b alongside Makefile (target_m06_l01) below to see three subtle Makefile bugs p…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP448/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Path-Preserving Object Rules, Phony Header Guards & ABI Consistency

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP449/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Path-Preserving Object Rules, Phony Header Guards & ABI Consistency

fig_m06_l01_b.svg

Figure 6.1b: Path-mirroring pattern rules prevent basename collisions (left), while -MMD -MP plus -include closes the incremental header rebuild DAG (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP450/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Makefile

learner@ksa: ~/gl-e01/m06-l01
learner@ksa:~/gl-e01/m06-l01$ bat Makefile File: Makefile    CROSS_COMPILE ?= arm-linux-gnueabihf-    CC := $(CROSS_COMPILE)gcc    OBJCOPY := $(CROSS_COMPILE)objcopy    CFLAGS := -std=c11 -Wall -Wextra -Werror -O2 -ffreestanding \              -fno-builtin -mcpu=cortex-a7 -marm -mfloat-abi=soft -MMD -MP    CPPFLAGS := -Iinclude -Ibsp/clk -Ibsp/led -Ibsp/delay    SRC := project/main.c bsp/clk/clk.c bsp/led/led.c bsp/delay/delay.c    OBJ := $(patsubst %.c,obj/%.o,$(SRC)) obj/project/start.o    all: bsp.bin    obj/%.o: %.c    	@mkdir -p $(dir $@)    	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@    obj/%.o: %.S    	@mkdir -p $(dir $@)
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP451/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Auditing CCM Sparse Struct Offsets & DAG Invalidation Closure in C11

  • In bsp_header_dep_audit.c below, we prove two properties side by side: first, _Static_assert verifies `…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP452/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

bsp_header_dep_audit.c

learner@ksa: ~/gl-e01/m06-l01
learner@ksa:~/gl-e01/m06-l01$ bat bsp_header_dep_audit.c File: bsp_header_dep_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>    #include <string.h>        struct bsp_ccm_regs {        volatile uint32_t _pad0[26]; /* 0x00..0x64 */        volatile uint32_t CCGR[7];   /* 0x68..0x80 */    };    _Static_assert(offsetof(struct bsp_ccm_regs, CCGR[0]) == 0x68U, "CCGR0");    _Static_assert(offsetof(struct bsp_ccm_regs, CCGR[1]) == 0x6cU, "CCGR1");    _Static_assert(offsetof(struct bsp_ccm_regs, CCGR[6]) == 0x80U, "CCGR6");
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP453/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Compiling with -MMD -MP & Verifying Incremental Invalidation Masks

  • Compile bsp_header_dep_audit.c with -MMD -MP and execute the audit binary below.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP454/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Compiling with -MMD -MP & Verifying Incremental Invalidation Masks

learner@ksa: ~/gl-e01/m06-l01
learner@ksa:~/gl-e01/m06-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic -MMD -MP bsp_header_dep_audit.c -o bsp_header_dep_auditlearner@ksa:~/gl-e01/m06-l01./bsp_header_dep_auditCCM_span=132B CCGR1=+0x6C soc_chg=0x13 brd_chg=0x12 lds_chg=0x10learner@ksa:~/gl-e01/m06-l01
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP455/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Compiling with -MMD -MP & Verifying Incremental Invalidation Masks

strace — learner@ksa: ~/gl-e01/m06-l01
learner@ksa:~/gl-e01/m06-l01strace -e trace=write,exit_group \./bsp_header_dep_auditwrite(1, "CCM_span=132B CCGR1=+0x6C soc_ch"..., 65) = 65exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP456/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Tracking Compiler Flag Changes Alongside Header Dependencies

  • One subtle detail separates good Makefiles from airtight ones: .d files track .c and .h timestamps, *…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP457/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Editing LED_PIN inside bsp/led/led_board.h and running make prints Nothing to be done for 'all', leaving the old pin number baked into bsp.bin. Root Cause: Either -MMD -MP was omitted from CFLAGS, or -include looked for *.d in the project root while objects (and .d files) were placed under obj/bsp/led/led.d. Fix: Match the -include glob directly to OBJ via -include $(OBJ:.o=.d) and verify with touch bsp/led/led_board.h && make -n.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP458/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Warning

warning: Symptom: Changing -mfloat-abi=hard to -mfloat-abi=soft in Makefile and running incremental make fails during ld with uses VFP register arguments, bsp.elf does not. Root Cause: Generated .d files record #include header files, not Makefile flag variables, leaving stale hard-float .o files on disk. Fix: Add Makefile (or a compiler-flags stamp file) as a prerequisite on obj/%.o: %.c Makefile or run a clean rebuild whenever ABI flags change.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP459/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Guided Practice: Tracing a Deleted Header & Linker-Script Edit

  • Walk through two incremental build scenarios.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP460/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Independent Practice: Build-Graph Layer & Cycle Linter (build_graph_lint.c)

  • Extend bsp_header_dep_audit.c into build_graph_lint.c that checks an array of source paths for flattene…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP461/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Quick check

An engineer updates a register offset inside include/soc_regs.h and runs make, but obj/bsp/clk/clk.o does not recompile. What is the first build-graph defect to check?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP462/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Quick check

Why does OBJ := $(patsubst %.c,obj/%.o,$(SRC)) mirror full relative paths (obj/bsp/clk/clk.o) instead of flattening every object into obj/$(notdir).o?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP463/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Quick check

Retrieval: why must bsp.lds appear in the prerequisite list of bsp.elf: $(OBJ) bsp.lds in addition to -Wl,-T,bsp.lds in the recipe?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP464/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Summary

  • A modular BSP separates pure silicon register structs (include/soc_regs.h), board-aware subsystem drivers (bsp/clk/, bsp/led/, bsp/delay/), and logical application code (project/main.c).
  • GNU Make rebuilds targets strictly from declared prerequisite timestamps and path-preserving obj/%.o rules rather than directory conventions.
  • GCC/Clang -MMD emits non-system header dependencies per .o, while -MP adds empty phony header targets so deleted or renamed headers never stall incremental builds.
  • -include $(OBJ:.o=.d) reconnects .d files into Make's DAG, and bsp.elf: $(OBJ) bsp.lds ensures linker-script edits trigger deterministic relinking.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP465/523

GL-E01 · 6.1 (m06-l01)

>_ Kernel Systems Academy

Key takeaways

  • A BSP can separate clock, LED, delay, and application responsibilities into distinct source modules.
  • GNU Make decides rebuilds from declared prerequisites and artifact state rather than directory appearance.
  • A new link of the ELF that depends on that placement policy.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP466/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Transistor Buzzer Switching and Safe GPIO Polarity

free preview20 min

What you'll learn

  • Derive PNP and NPN buzzer control polarity from their current paths.
  • Reject invalid bank-local pin requests before shifting or writing.
  • Preserve sibling DR bits while stating the exclusive-ownership requirement.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP467/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Why Buzzers Use Transistor Stages & How BJT Topologies Set Polarity

  • Why not wire a 30 mA magnetic or piezoelectric buzzer directly between an i.MX6ULL GPIO pin and groun…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP468/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Why Buzzers Use Transistor Stages & How BJT Topologies Set Polarity

  • Trace all three BJT switching circuits in Figure 6.2a from emitter to collector to derive software pola…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP469/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Why Buzzers Use Transistor Stages & How BJT Topologies Set Polarity

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP470/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Why Buzzers Use Transistor Stages & How BJT Topologies Set Polarity

fig_m06_l02_a.svg

Figure 6.2a: BJT switching physics — 3.3 V PNP high-side active-low (A), 5.0 V NPN low-side active-high (B), and why a 5.0 V PNP cannot turn off from a 3.3 V GPIO (C).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP471/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Mental model

note: A BJT switching stage is a hydraulic pilot valve: a 1.67 mA pilot current through R_B opens a 30 mA supply pipe through the collector-emitter path without forcing the i.MX6ULL CMOS pad to carry the heavy actuator load itself.

Where it stops working: Unlike an optically isolated relay, a BJT shares a common electrical reference with the SoC—mismatched 5 V PNP emitter rails can back-feed current straight into a 3.3 V GPIO pad!

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP472/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Active vs Passive Sounders, BJT Polarity & Shared-Bank Ownership

definition: An active buzzer integrates an internal oscillator and sounds from steady DC, whereas a passive sounder requires an external AC/PWM waveform. A 3.3 V PNP high-side switch conducts when its base is pulled low (active-low), while an NPN low-side switch conducts on positive base current (active-high)—translated behind buzzer_set(bool enabled) via drive_high = (asserted != active_low). Because GPIOx_DR packs 32 pins into one word, read-modify-write updates must preserve all 31 sibling bits under exclusive bank ownership or serialization.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP473/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Why Buzzers Use Transistor Stages & How BJT Topologies Set Polarity

  • Step through the four-stage actuator control path from logical request to collector current below:
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP474/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Why Buzzers Use Transistor Stages & How BJT Topologies Set Polarity

Step 1: Application calls buzzer_set(true/false) with zero knowledge of PNP or NPN inversion.

Expose logical buzzer_set(enabled) to application code, preload inactive DR before GDIR, and validate pin < 32 before shifting.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP475/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

CCM_CCGR1 2-Bit Clock Gates, IOMUXC_SNVS & Chirp-Free Startup

  • Inspect Figure 6.2b to wire up the complete i.MX6ULL hardware path for an active buzzer on `SNVS_TAMP…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP476/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

CCM_CCGR1 2-Bit Clock Gates, IOMUXC_SNVS & Chirp-Free Startup

fig_m06_l02_b.svg

Figure 6.2b: CCM_CCGR1 2-bit clock gates (top), IOMUXC_SNVS pad routing plus chirp-free preload (bottom left), and Boolean polarity translation (bottom right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP477/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

CCM_CCGR1 2-Bit Clock Gates, IOMUXC_SNVS & Chirp-Free Startup

  • In bsp_buzzer.c below, verify how gpio_pin_write() checks dr == NULL || pin >= 32U before evaluat…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP478/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

bsp_buzzer.c

learner@ksa: ~/gl-e01/m06-l02
learner@ksa:~/gl-e01/m06-l02$ bat bsp_buzzer.c File: bsp_buzzer.c    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    bool gpio_pin_write(volatile uint32_t *dr, unsigned pin,                        bool asserted, bool active_low)    {        if (dr == NULL || pin >= 32U) return false;        uint32_t mask = UINT32_C(1) << pin;        uint32_t value = *dr; /* caller serializes the shared bank */        *dr = (asserted != active_low) ? value | mask : value & ~mask;        return true;    }    bool buzzer_set(bool enabled)    {
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP479/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Verifying Selective CCGR1 Gating & Buzzer Polarity Safety in C11

  • In ccm_ccgr_buzzer_audit.c below, ccgr_update() rejects invalid CCGR indices (>= 7), invalid slots…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP480/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

ccm_ccgr_buzzer_audit.c

learner@ksa: ~/gl-e01/m06-l02
learner@ksa:~/gl-e01/m06-l02$ bat ccm_ccgr_buzzer_audit.c File: ccm_ccgr_buzzer_audit.c    #include <assert.h>    #include <stdbool.h>    #include <stddef.h>    #include <stdint.h>    #include <stdio.h>        enum { CG_OFF = 0U, CG_RUN = 1U, CG_RSVD = 2U, CG_RUN_WAIT = 3U };        static bool ccgr_update(uint32_t ccgr[7], unsigned reg_idx,                            unsigned cg_slot, unsigned policy)    {        if (reg_idx >= 7U || cg_slot >= 16U || policy > 3U || policy == CG_RSVD)            return false;        unsigned shift = cg_slot * 2U;
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP481/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Running the CCGR1 & Buzzer Polarity Audit + ARMv7-A Cross-Check

  • Compile and run ccm_ccgr_buzzer_audit on the workstation, then cross-compile bsp_buzzer.c for freestand…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP482/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Running the CCGR1 & Buzzer Polarity Audit + ARMv7-A Cross-Check

learner@ksa: ~/gl-e01/m06-l02
learner@ksa:~/gl-e01/m06-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic ccm_ccgr_buzzer_audit.c -o ccm_ccgr_buzzer_auditlearner@ksa:~/gl-e01/m06-l02./ccm_ccgr_buzzer_auditCCGR1=0xCFFFFFF0 pnp_on_bit1=0 pnp_off_bit1=1 npn_on_bit1=1learner@ksa:~/gl-e01/m06-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -mfloat-abi=soft -ffreestanding -std=c11 -Wall -Wextra -Werror -x c -c bsp_buzzer.c -o target.olearner@ksa:~/gl-e01/m06-l02
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP483/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Running the CCGR1 & Buzzer Polarity Audit + ARMv7-A Cross-Check

strace — learner@ksa: ~/gl-e01/m06-l02
learner@ksa:~/gl-e01/m06-l02strace -e trace=write,exit_group \./ccm_ccgr_buzzer_auditwrite(1, "CCGR1=0xCFFFFFF0 pnp_on_bit1=0 p"..., 60) = 60exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP484/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Base-Current Sizing & Future Linux gpiod Descriptor Mapping

  • Notice how clean hardware-software co-design makes future Linux kernel migration effortless.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP485/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: Powering a PNP high-side buzzer from 5.0 V while connecting its base resistor directly to a 3.3 V i.MX6ULL GPIO causes the buzzer to sound continuously even when software sets DR |= (1U << 1) (3.3 V). Root Cause: At GPIO = 3.3 V, V_EB = 5.0 V - 3.3 V = 1.7 V > 0.7 V, keeping the PNP emitter-base junction forward-biased in saturation and injecting current into the 3.3 V pad clamp! Fix: Switch 5.0 V loads from 3.3 V GPIOs using an NPN (or N-channel MOSFET) low-side stage, or keep the PNP emitter on the matching 3.3 V rail.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP486/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Warning

warning: Symptom: CCGR1[31:30] (GPIO5) and GPIO5_GDIR/GPIO5_DR (0x020AC000) are configured properly, yet SNVS_TAMPER1 never toggles on an oscilloscope because mux writes targeted 0x020E0000 + offset. Root Cause: SNVS_TAMPER0..9 pads reside in the separate IOMUXC_SNVS peripheral block (0x02290000, SNVS_TAMPER1 mux at 0x0229000C), not main IOMUXC (0x020E0000). Fix: Always check whether a GPIO5 pad belongs to IOMUXC_SNVS (0x02290000) before programming ALT5.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP487/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Guided Practice: Sizing an NPN Base Resistor & Glitchless Init

  • Compute the analog drive and register sequence for a 5.0 V, 40 mA active buzzer switched on the low sid…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP488/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Independent Practice: Multi-Actuator Chirp-Free BSP Verifier (actuator_bsp_audit.c)

  • Extend ccm_ccgr_buzzer_audit.c into actuator_bsp_audit.c managing LED0 (GPIO1_IO03, active-low, `CC…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP489/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Quick check

Why is it electrically incorrect to describe a standard PNP or NPN transistor buzzer stage as providing galvanic isolation for the i.MX6ULL?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP490/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Quick check

On our 3.3 V PNP high-side active buzzer (SNVS_TAMPER1 / GPIO5_IO01, active_low = true), which GPIO5_DR bit 1 state silences (disables) the buzzer, and when must initialization establish it?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP491/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Quick check

Retrieval: under what concurrency condition is gpio_pin_write()'s *dr = ... read-modify-write guaranteed free from lost sibling-pin updates?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP492/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Summary

  • Active buzzers contain an internal oscillator driven by steady DC, whereas passive sounders require an alternating PWM / timer waveform.
  • A 3.3 V PNP high-side switch conducts when pulled low (V_EB ~= 0.7 V, active-low), while an NPN low-side switch conducts when driven high (V_BE ~= 0.75 V, active-high), unified behind drive_high = (asserted != active_low).
  • BJT switches buffer load current (I_B << I_C) via base resistor R_B without providing galvanic isolation; never tie a PNP emitter to 5 V when driven directly from a 3.3 V GPIO.
  • Enable GPIO5's CCM_CCGR1[31:30] (CG15) clock gate and IOMUXC_SNVS (0x0229000C) mux, preload inactive DR before GDIR, validate pin < 32U, and serialize shared DR read-modify-writes.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP493/523

GL-E01 · 6.2 (m06-l02)

>_ Kernel Systems Academy

Key takeaways

  • An active buzzer contains an oscillator and can sound from an appropriate steady supply; a passive sounder needs an appropriate waveform.
  • A PNP high-side switch commonly asserts when its base is driven sufficiently below its emitter, while an NPN low-side switch commonly asserts with positive base drive.
  • When the bank operation has exclusive ownership or serialization.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP494/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Capstone: A Nonblocking Polled Key and Modular BSP

free preview20 minlab lab_m06_modular_bsp_io

What you'll learn

  • Normalize the sampled active-low PSR input into a logical pressed state.
  • Accept a candidate only after a continuous sampled stable interval.
  • Integrate one press event with LED and buzzer policy without blocking polling.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP495/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mechanical Contact Bounce Physics & Why Blocking Delays Fail

  • Press a spring-loaded metal dome inside a tactile PCB push-button and zoom in on an oscilloscope at `500 us…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP496/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mechanical Contact Bounce Physics & Why Blocking Delays Fail

  • Why not "fix" bounce with `if (pressed) { delay_ms(10); ...
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP497/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mechanical Contact Bounce Physics & Why Blocking Delays Fail

  • 1.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP498/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mechanical Contact Bounce Physics & Why Blocking Delays Fail

fig_m06_l03_a.svg

Figure 6.3a: Active-low tactile switch input path into GPIO1_PSR[18] (top) and the exact capstone waveform turning press/release chatter into single-shot transitions (bottom).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP499/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mental model

note: A nonblocking debounce FSM is an air-traffic clearance desk that logs a status transition only after radar sweeps confirm the new state holds continuously for 10 ticks—restarting since_ms on any bounce blip while keeping every other BSP subsystem runnable in < 30 CPU cycles per poll.

Where it stops working: Unlike continuous analog radar, discrete polling observes pad voltage only at sample instants and relies on a monotonic unsigned tick source whose poll interval stays well below the 2^32 wraparound horizon.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP500/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Nonblocking Debounce FSM & Modulo-2^32 Elapsed-Time Subtraction

definition: A nonblocking contact-debounce FSM stores (candidate, stable, since_ms): any sample pressed != candidate updates candidate = pressed and since_ms = now_ms (KEY_NONE), while an unchanged candidate != stable commits stable = candidate and emits exactly one transition event (KEY_PRESS or KEY_RELEASE) once (uint32_t)(now_ms - since_ms) >= threshold_ms. Computing elapsed duration via unsigned modulo-2^32 subtraction (uint32_t)(now - since) remains exact across UINT32_MAX -> 0 wraparound (~49.71 days at 1 kHz), whereas deadline addition now >= since + 10U wraps and misfires early!

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP501/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mechanical Contact Bounce Physics & Why Blocking Delays Fail

  • Step through the four-stage capstone super-loop connecting GPIO1_PSR, debounce qualification, and `bsp/le…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP502/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Mechanical Contact Bounce Physics & Why Blocking Delays Fail

Step 1: Read GPIO1_PSR (+0x08) and normalize active-low bit 18 into logical pressed.

Sample PSR (never DR), separate candidate from stable state, and always subtract (uint32_t)(now - since) >= threshold.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP503/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Modulo-2^32 Tick Wraparound Proof & O(1) Poll Architecture

  • Study Figure 6.3b (Panel 2) carefully—it explains a classic 49.7-day uptime field bug that crashes…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP504/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Modulo-2^32 Tick Wraparound Proof & O(1) Poll Architecture

fig_m06_l03_b.svg

Figure 6.3b: Nonblocking O(1) debounce branches (left) and why unsigned subtraction (uint32_t)(now - since) survives 49.7-day UINT32_MAX wraparound (right).

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP505/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Modulo-2^32 Tick Wraparound Proof & O(1) Poll Architecture

  • In bsp_key.c below, key_poll() reads GPIO1_PSR (0x0209C008, base 0x0209C000 + 0x08) through a `vo…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP506/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

bsp_key.c

learner@ksa: ~/gl-e01/m06-l03
learner@ksa:~/gl-e01/m06-l03$ bat bsp_key.c File: bsp_key.c    #include <stdbool.h>    #include <stdint.h>    enum key_event { KEY_NONE, KEY_PRESS, KEY_RELEASE };    struct key_filter { bool candidate, stable; uint32_t since; };    enum key_event key_poll(struct key_filter *f, uint32_t now_ms)    {        volatile const uint32_t *psr =            (volatile const uint32_t *)(uintptr_t)0x0209c008U;        bool pressed = (*psr & (UINT32_C(1) << 18)) == 0U;        if (pressed != f->candidate) {            f->candidate = pressed;            f->since = now_ms;            return KEY_NONE;        }
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP507/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Proving End-to-End Capstone Integration & Wraparound Safety in C11

  • debounce_fsm_capstone.c wires together every concept from GL-E01 into one deterministic host testbench:…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP508/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

debounce_fsm_capstone.c

learner@ksa: ~/gl-e01/m06-l03
learner@ksa:~/gl-e01/m06-l03$ bat debounce_fsm_capstone.c File: debounce_fsm_capstone.c    #include <assert.h>    #include <stdbool.h>    #include <stdint.h>    #include <stdio.h>        enum key_ev { EV_NONE = 0, EV_PRESS = 1, EV_RELEASE = 2 };    struct deb_fsm { bool candidate, stable; uint32_t since_ms; };        static bool psr_key_pressed(uint32_t gpio1_psr)    {        return (gpio1_psr & (UINT32_C(1) << 18)) == 0U;    }        static enum key_ev debounce_step(struct deb_fsm *f, bool pressed, uint32_t now)
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP509/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Running the Capstone Verifier & Freestanding Cortex-A7 Object Check

  • Compile and execute debounce_fsm_capstone.c on the workstation, then cross-compile bsp_key.c for freest…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP510/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Running the Capstone Verifier & Freestanding Cortex-A7 Object Check

learner@ksa: ~/gl-e01/m06-l03
learner@ksa:~/gl-e01/m06-l03gcc -std=c11 -Wall -Wextra -Werror -pedantic debounce_fsm_capstone.c -o debounce_fsm_capstonelearner@ksa:~/gl-e01/m06-l03./debounce_fsm_capstonepress@14 release@36 press@47 wrap@4 dr1_off=0xA5A5A5AD dr5_off=0x5A5A5A57learner@ksa:~/gl-e01/m06-l03clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -mfloat-abi=soft -ffreestanding -std=c11 -Wall -Wextra -Werror -x c -c bsp_key.c -o target.olearner@ksa:~/gl-e01/m06-l03
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP511/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Running the Capstone Verifier & Freestanding Cortex-A7 Object Check

strace — learner@ksa: ~/gl-e01/m06-l03
learner@ksa:~/gl-e01/m06-l03strace -e trace=write,exit_group \./debounce_fsm_capstonewrite(1, "press@14 release@36 press@47 wra"..., 74) = 74exit_group(0)                           = ?+++ exited with 0 +++
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP512/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Where You Stand Now & Bridging Forward to GL-E02

  • Step back and appreciate the full bare-metal stack you now own from first principles: ARMv7-A AAPCS / `…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP513/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: After ~49.7 days of continuous 1 ms uptime (or whenever unit tests seed now_ms near 0xFFFFFFFA), tactile button bounces trigger immediate false KEY_PRESS events on the very first microsecond of contact chatter! Root Cause: Elapsed time was written if (now_ms >= f->since + 10U), allowing f->since + 10U to wrap modulo 2^32 to a small integer (4U) that 0xFFFFFFFB immediately exceeds! Fix: Always compute elapsed ticks via unsigned subtraction (uint32_t)(now_ms - f->since) >= 10U.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP514/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Warning

warning: Symptom: Configuring GPIO1_IO18 as an input (GDIR[18] = 0) and polling *gpio1_dr & (1U << 18) never detects a button press even when a multimeter measures 0 V right on the SoC ball. Root Cause: GPIOx_DR (+0x00) reads back the internal output data latch, whereas external pin voltage on an input pad is sampled through GPIOx_PSR (+0x08) (and requires IOMUXC pull-up biasing so the open switch cannot float). Fix: Read inputs strictly from GPIOx_PSR (+0x08) after configuring the ALT5 mux, pull-up network, and GDIR bit 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP515/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Guided Practice: Hand-Tracing Release Chatter & Modulo Subtraction

  • Trace debounce_step() starting from accepted pressed state `{candidate = true, stable = true, since_ms =…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP516/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Independent Practice: Short-Tap vs 500 ms Long-Hold BSP Controller

  • Extend debounce_fsm_capstone.c into key_short_long_bsp.c adding EV_LONG_HOLD (emitted once per press…
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP517/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Quick check

In debounce_step(), a bouncing switch finally stays low starting at now = 4 (f->candidate = true, f->since_ms = 4) with threshold 10U. What does debounce_step() return when polled with pressed = true at now = 13?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP518/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Quick check

After EV_PRESS fires at now = 14, the user keeps holding the tactile switch down at now = 19, 20, 25.... Why does key_poll() emit zero additional KEY_PRESS events while held?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP519/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Quick check

Retrieval: which GPIO1 register (base 0x0209C000) must bsp_key.c read to observe the external tactile switch on GPIO1_IO18?

Select an option or press R to reveal.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP520/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Lab: lab_m06_modular_bsp_io

  • Repair a candidate/stable debouncer, active-low output policy, and integrated press-driven toggling while r…

Verify: ./check.sh exits 0.

GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP521/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Summary

  • Mechanical switch contacts bounce repeatedly across 1..5 ms on both press and release; IOMUXC hysteresis (HYS = 1) cleans slow edges while a software debounce FSM filters millisecond chatter.
  • Tracking (candidate, stable, since_ms) in an O(1) nonblocking poll function qualifies transitions without starving sibling BSP work inside delay_ms() spin loops.
  • Unsigned subtraction (uint32_t)(now_ms - since_ms) >= threshold computes exact elapsed ticks across UINT32_MAX -> 0 wraparound (~49.7 days at 1 kHz), avoiding the since + threshold overflow bug.
  • Sampling GPIO1_PSR (+0x08) and dispatching single-edge EV_PRESS transitions into polarity-aware GPIO1_DR/GPIO5_DR helpers completes an auditable, modular bare-metal BSP.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP522/523

GL-E01 · 6.3 (m06-l03)

>_ Kernel Systems Academy

Key takeaways

  • Mechanical contacts can create multiple electrical transitions during one physical press or release.
  • A debouncer can track candidate state, candidate-start time, and accepted stable state separately.
  • PSR, with the selected active-low bit normalized into pressed.
GL-E01 · ARMv7-A Cortex-A7 Architecture, Boot ROM & Bare-Metal BSP523/523

slides — overview

cover

m01/ Cross-compilation workbench, ARM EABI, and schematics

m02/ Cortex-A7 modes, CP15, and ARMv7 assembly

m03/ IOMUXC muxing, pad electrical policy, and GPIO MMIO

m04/ Boot ROM selection, IVT geometry, and DCD command streams

m05/ C runtime startup, linker placement, and peripheral headers

m06/ Modular BSP, transistor-switched buzzer, and polled key capstone

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure