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

Trace the internal flip-flops, tri-state drivers, and edge detectors of an i.MX6ULL GPIO bank: preload DR before GDIR to eliminate startup glitches, distinguish DR latches from PSR wire state, split ICR1/ICR2 half-bank triggers, and clear W1C ISR flags without losing sibling interrupts.

20 minfree previewLinux 6.12C11U-Boot 2025.x→ module 3 lab
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.

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, +0x00) and a Pad Status Register (PSR, +0x08) alongside GDIR (+0x04)—aren't DR and PSR the same 32 bits? No! Look at the internal logic schematic in Panel 1 of Figure 3.3a:

  1. DR (+0x00) vs GDIR (+0x04) vs PSR (+0x08): DR is an internal 32-bit output-request D-flip-flop; GDIR (0 = input, 1 = output) drives the Output-Enable (OE) gate of the tri-state buffer feeding the pad; and read-only PSR samples the actual electrical voltage at the package ball! Two immediate engineering consequences follow:
  • Why DR != PSR: If DR[3] = 1 and GDIR[3] = 1, yet an external short-circuit or open-drain slave clamps the board trace to 0 V (or SW_PAD_CTL.DSE == 0 left the driver Hi-Z), reading DR returns 1 (what you requested), whereas reading PSR returns 0 (what is physically on the wire)!
  • Glitch-Free Active-Low Startup (Panel 2 of Figure 3.3a): At reset, DR and GDIR both clear to 0. On an active-low LED, buzzer, or relay (0 = ON!), if firmware sets GDIR[3] = 1 before setting DR[3] = 1, the tri-state buffer turns ON while DR[3] is still 0—firing a spurious low pulse onto the hardware load! Always preload DR to its safe inactive state (1) + dsb sy before setting GDIR = 1!
  1. Interrupt Trigger Split (ICR1 / ICR2) & Why GPIO_ISR Is Write-One-to-Clear (W1C): Configuring 4 trigger modes (00 low-level, 01 high-level, 10 rising-edge, 11 falling-edge) across 32 pins takes 32 * 2 = 64 bits, so hardware splits pins 0..15 into ICR1 (+0x0C) and pins 16..31 into ICR2 (+0x10) at local shift 2 * (pin % 16) (with EDGE_SEL at +0x1C overriding for dual-edge capture), masked by IMR (+0x14). Why did silicon architects make ISR (+0x18) Write-One-to-Clear (W1C) instead of ordinary read/write? Because asynchronous pin edges can set a sibling bit in ISR on any clock cycle between a CPU LDR and STR! With W1C (ISR_next = ISR_curr & ~W_data), writing 0 bits is a hardware no-op and writing a pure mask (0x08 for pin 3) atomically clears only bit 3 in a single STR—whereas an ordinary RMW (ISR |= 0x08) replays 1s on every pending sibling bit (0x0A) and wipes them out (0x00)!
fig_m03_l03_a.svg — Inside GPIO1 @ 0x0209C000 — separating DR request, GDIR…

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

mental model: 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 .I=0 before vectoring the Cortex-A7 CPU.

definition (GPIO Bank Register Map, Half-Bank ICR & Write-One-to-Clear (W1C)): 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.

Step through all four GPIO register roles in the interactive sequence below:

ast_m03_l03.svg — Match every GPIO operation to its exact register…step 1/4
  1. Step 1. Step 1 (Output Latch): Preload inactive high DR[3] = 1 (+0x00) + DSB SY before enabling output direction.
  2. Step 2. Step 2 (Direction): Set GDIR[3] = 1 (+0x04) via masked RMW so all 31 neighboring pins keep their directions.
  3. Step 3. Step 3 (Pad Observation): Sample physical wire state from read-only PSR (+0x08), which can differ from DR under load/clamp.
  4. Step 4. Step 4 (Event Path): Partition ICR1/ICR2 via pin/16 and acknowledge ISR (+0x18) with a pure 1-bit W1C store.

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

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:

  1. ICR1 (0..15) vs ICR2 (16..31) Shift Math: Pin 15 occupies bits [31:30] of ICR1 (shift = 30). Pin 16 starts over at bits [1:0] (shift = 0!) of ICR2! Writing 2 * pin without pin % 16 shifts by 32..62 for pins 16..31—triggering ISO C11 Undefined Behavior and missing ICR2 completely.
  2. Zero-Load W1C Acknowledgment (gpio1_ack_pin3): Look at lines 23..28 of target.S! Whereas led_prepare_inactive and led_assert execute ldr -> orr/bic -> str on ordinary RW latches DR (0x0209C000) and GDIR (0x0209C004), gpio1_ack_pin3 executes mov r1, #8 followed immediately by str r1, [r0] (0x0209C018) with NO ldr! On ISR = 0x0A (pins 3 and 1 pending), storing 0x08 leaves 0x02 intact, whereas ldr + orr #8 + str writes 0x0A and destroys pin 1's interrupt!
fig_m03_l03_b.svg — ICR1 / ICR2 half-bank bit-pair split (top) and why…

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).

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]        bx lr    .global led_assert    led_assert:        ldr r0, =0x0209c000        ldr r1, [r0]        bic r1, r1, #8        str r1, [r0]        bx lr    .global gpio1_ack_pin3    gpio1_ack_pin3:        ldr r0, =0x0209c018        mov r1, #8        str r1, [r0]        bx lr

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 DR = 0x08 preload divergence from an externally clamped PSR = 0x00, tests boundary pins 15 (ICR1 shift 30), 16 (ICR2 shift 0), 31 (ICR2 shift 30), and out-of-range 32, and proves side-by-side that direct W1C store 0x08 on ISR = 0x0A retains 0x02 (W1C_rem=0x02), while buggy.isr | 0x08U wipes ISR to 0x00 (RMW_rem=0x00).

learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03$ bat -H 11 -H 12 -H 13 -H 14 -H 15 -H 16 -H 17 -H 18 -H 19 -H 21 -H 22 -H 23 -H 24 -H 25 -H 36 -H 37 -H 38 -H 39 -H 40 -H 41 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;        uint32_t mask = UINT32_C(3) << shift;        *icr = (*icr & ~mask) | ((uint32_t)trig << shift);        return true;    }        static void gpio_write_isr_w1c(struct gpio_bank_sim *b, uint32_t word_written)    {        b->isr &= ~word_written;    }      int main(void)    {        struct gpio_bank_sim g = {0U, 0U, 0x00000000U, 0U, 0U, 0U, 0U};        g.dr |= (UINT32_C(1) << 3);   /* preload inactive high before GDIR=1 */        g.gdir |= (UINT32_C(1) << 3);        assert((g.dr & 8U) != 0U && (g.psr & 8U) == 0U); /* external clamp keeps PSR=0 */            assert(gpio_cfg_icr(&g, 15U, 3U) && (g.icr1 >> 30) == 3U);        assert(gpio_cfg_icr(&g, 16U, 2U) && (g.icr2 & 3U) == 2U);        assert(gpio_cfg_icr(&g, 31U, 1U) && (g.icr2 >> 30) == 1U);        assert(!gpio_cfg_icr(&g, 32U, 2U));            g.imr = 0x0aU; /* unmask pin 1 and pin 3 */        g.isr = 0x0aU; /* both pin 1 and pin 3 latched pending */      gpio_write_isr_w1c(&g, UINT32_C(1) << 3); /* direct mask write: 0x08 */        assert(g.isr == 0x02U && (g.isr & g.imr) == 0x02U);            struct gpio_bank_sim buggy = g;        buggy.isr = 0x0aU;        gpio_write_isr_w1c(&buggy, buggy.isr | 0x08U); /* RMW writes 0x0A! */        assert(buggy.isr == 0x00U);        printf("DR=0x%02X PSR=0x%02X ICR1_15=%u ICR2_16=%u W1C_rem=0x%02X RMW_rem=0x%02X\n",               g.dr, g.psr, g.icr1 >> 30, g.icr2 & 3U, g.isr, buggy.isr);        return 0;    }
  1. Partition pins 0..15 into ICR1 and pins 16..31 into ICR2 so local shifts stay within [0, 30].
  2. Hardware W1C clears every bit position written as 1 (b->isr &= ~word_written); writing 0 leaves pending bits intact.
  3. Contrast direct mask write (0x08 -> leaves sibling 0x02) against RMW (0x0A -> destroys sibling 0x02).

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. Note the system-level boundary: clearing GPIO1_ISR handles the peripheral latch, while end-to-end interrupt delivery on bare-metal silicon also requires routing the bank IRQ through the ARM Generic Interrupt Controller (GICv2, covered in GL-E02).

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
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 +++

GPIO Register Access Semantics Comparison

Never hide DR and ISR behind one generic set_bit(addr, mask) macro—their hardware write contracts are opposites:

Register (Offset)Read MeaningWrite SemanticsSafe Single-Pin Update Pattern
GPIOx_DR (+0x00)Latched output valueOrdinary read/write latchExclusive-owner `(DR & ~mask) \val`
GPIOx_GDIR (+0x04)0 = input, 1 = outputOrdinary read/write latchPreload DR safe state + dsb sy first, then `GDIR \= mask`
GPIOx_PSR (+0x08)Sampled pad receiver stateRead-only (const in C overlay)Read (PSR >> pin) & 1U (set SION=1 if output readback required)
GPIOx_ISR (+0x18)1 = latched trigger eventWrite-One-to-Clear (W1C)Direct command store ISR = (UINT32_C(1) << pin) (0 loads!)

Later: Module 4 (m04-l01..m04-l03) shows how the i.MX6ULL internal Boot ROM actually loads your assembled image off SD/eMMC—decoding BOOT_MODE straps, IVT (0x402000D1) headers at 0x400, and DCD (0xD2) big-endian register streams!

Common Production Pitfalls

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);.

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.

Practice & Retrieval

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, shift, clear mask, and falling-edge (0b11) value (18 >= 16 -> ICR2 at 0x0209C010, shift (18 % 16) * 2 = 4, clear mask 0x30U, falling-edge 0x30U). Step 2 (EDGE_SEL Override): Because GPIO1_EDGE_SEL (+0x1C) bit 18 = 1 overrides ICR2 to trigger on any edge, confirm EDGE_SEL &= ~(UINT32_C(1) << 18) is required for single-edge ICR2 operation. Step 3 (W1C Trace): Given ISR = 0x000C0008U and IMR = 0x00040008U, state active unmasked pins (3 and 18) and ISR after GPIO1->ISR = (UINT32_C(1) << 18) (0x00080008U).

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_transition(struct gpio_bank_sim *b, uint32_t new_pads) latching rising/falling/both edges across all 32 pins, plus dispatch_next_irq() servicing the lowest set bit of b->isr & b->imr via single-bit W1C acknowledgment across simultaneous transitions on pins 0, 15, 16, and 31.

quiz · 1 of 3

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?

quiz · 2 of 3

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

quiz · 3 of 3

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

Lab 3: CCM Gate Slots, IOMUXC Pad/Bias Decoding & Non-Destructive GPIO W1C

env: host

1. Motivation

On i.MX6ULL silicon, peripheral bring-up fails across four coupled MMIO registers: accessing GPIO1 while CCM_CCGR1 CG13 (bits 27:26) is 00 stalls or faults on the bus, selecting an unimplemented MUX_MODE routes a pad to nowhere, misreading PKE/PUE in IOMUXC_SW_PAD_CTL confuses a bus-keeper (0x10B0) with an active pull-up (0x1B000), and acknowledging GPIOx_ISR with GPIO1->ISR |= (1U << 3) writes 1 back to every pending interrupt bit -- silently dropping concurrent edge events! In this laboratory, you will locate and repair a destructive W1C read-modify-write bug in starter/core.c and validate all five CCM/IOMUXC/GPIO register helpers under TAP version 13.

2. Learning Objectives

  1. Update individual 2-bit CCM_CCGRx clock-gate slots (index in 0..15, mode in 0..3) without disturbing the remaining 15 peripheral gates in the 32-bit word.
  2. Validate per-pad MUX_MODE[3:0] legality against a uint16_t allowed bitmask while orthogonally encoding SION (bit 4, 0x10U) in pad_mux().
  3. Decode IOMUXC_SW_PAD_CTL bias states (0 disabled when PKE == 0, 1 keeper when PKE == 1 && PUE == 0, 2 pull when PKE == 1 && PUE == 1) in bias_mode().
  4. Repair write-1-to-clear (W1C) interrupt acknowledgment (acknowledge) and GPIOx_ICR1/ICR2 2-bit register/shift indexing (icr_field) across pins 0..31.

3. Environment Requirements

  • Tested Workstation Host: Linux 7.0.0-42-generic (x86_64) inside labs/lab_m03_iomuxc_gpio_mmio/.
  • Target Silicon Alignment: Bit positions match i.MX6ULL CCM_CCGR0..6 (2 bits per CG), IOMUXC_SW_MUX_CTL_PAD_* (MUX_MODE[3:0], SION[4]), IOMUXC_SW_PAD_CTL_PAD_* (PKE[12], PUE[13]), and GPIO1..GPIO5 (ICR1 pins 0..15, ICR2 pins 16..31, ISR W1C).

4. Required Software & Hardware

  • Software: gcc (-std=c11 -Wall -Wextra -Werror -pedantic -MMD -MP), GNU make, GNU ar, POSIX /bin/sh, and Binutils (nm, objdump).
  • Hardware: Standard Linux workstation (0 physical target hardware required for automated TAP grading).

5. Complete Executable Commands

Run the commands below from labs/lab_m03_iomuxc_gpio_mmio/ to reproduce starter/'s W1C bug in check 5, inspect case 4 directly, compile files/libbsp.a, and pass all 5 TAP checks:

learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03$ bat script.sh File: script.sh    # 1. Stage starter/ in a temporary workspace and run check.sh (# pass 4/5, check 5 fails)    TMP=$(mktemp -d) && cp -r check.sh tests.c starter "$TMP/" && mv "$TMP/starter" "$TMP/files" && sh "$TMP/check.sh" ; rm -rf "$TMP"        # 2. Link tests.c against starter/core.c and run case 4 to trigger the W1C assertion failure    TMP=$(mktemp -d) && gcc -std=c11 -Wall -Wextra -Werror -pedantic -Istarter tests.c starter/core.c -o "$TMP/mmio" &&      { "$TMP/mmio" 1 && "$TMP/mmio" 2 && "$TMP/mmio" 3 && echo "cases 1..3 OK"; "$TMP/mmio" 4 || echo "case 4 aborted (rc=$?)"; } ; rm -rf "$TMP"        # 3. Build files/libbsp.a cleanly and list the five exported MMIO helper symbols    make -B -C files    nm -g --defined-only files/libbsp.a        # 4. Run the full 5-check TAP version 13 suite    sh check.sh

*Why each step runs:* Step 1 executes check.sh against starter/ in a disposable directory so files/ stays untouched; Step 2 confirms clock_field, pad_mux, and bias_mode pass (cases 1..3) while acknowledge aborts in case 4; Step 3 rebuilds files/libbsp.a with -MMD -MP; and Step 4 verifies # pass 5/5.

6. Annotated Source Code (starter/core.c Defect vs files/core.c Fix)

  • clock_field & pad_mux (core.c:2-12): clock_field() rejects index >= 16U || mode > 3U and updates shift = index * 2U (CG13 -> bits 27:26 = 0x0c000000U in tests.c case 1). pad_mux() rejects alt > 8U before shifting UINT16_C(1) << alt, checks (allowed & (UINT16_C(1) << alt)) != 0U, and writes (*r & ~UINT32_C(0x1f)) | alt | (sion ? 0x10U : 0U) (0x15U for ALT5 + SION).
  • bias_mode & icr_field (core.c:13-17, 22-26): bias_mode() checks PKE (bit 12) first -- returning 0U even if PUE (bit 13) is 1 (1U << 13 in tests.c:25), 1U for 0x10b0U (keeper), and 2U for 0x1b000U (pull). icr_field() maps pin / 16U to ICR1 (0) vs ICR2 (1) and (pin % 16U) * 2U to the even shift (pin 15 -> reg 0, shift 30; pin 16 -> reg 1, shift 0).
  • Seeded Flaw in starter/core.c:18-21 (acknowledge): starter computes return pending & ~(pending | mask); -- the exact state transition produced on hardware when buggy code executes GPIO1->ISR |= mask (writing pending | mask to a W1C register clears pending | mask, always leaving 0U!).
  • Verified Repair in files/core.c:18-21: Replaces the body with return pending & ~mask;, modeling a direct GPIO1->ISR = mask store that clears only mask (0x8U) and leaves concurrent pending bit 1 (0x2U) intact (acknowledge(0xaU, 8U) == 2U).

7. Expected TAP-13 Output

learner@ksa: ~/gl-e01/m03-l03
learner@ksa:~/gl-e01/m03-l03$ bat listing.txt File: listing.txt    # Unmodified starter/ baseline (exits non-zero: # pass 4/5)    TAP version 13    1..5    ok 1 - strict C11 build and trusted grader link    ok 2 - updates one clock gate without changing neighboring fields    ok 3 - validates pad-specific mux modes and SION independently    ok 4 - distinguishes disabled bias keeper and enabled pull    not ok 5 - clears only requested W1C events and locates upper ICR fields      ---      message: 'Behavior differs from the independently specified test vectors'      hint: 'Repair case 4; inspect tests.c for the exact input and expected result'      ...    # pass 4/5        # Repaired files/ implementation (sh check.sh -> exits 0: # pass 5/5)    TAP version 13    1..5    ok 1 - strict C11 build and trusted grader link    ok 2 - updates one clock gate without changing neighboring fields    ok 3 - validates pad-specific mux modes and SION independently    ok 4 - distinguishes disabled bias keeper and enabled pull    ok 5 - clears only requested W1C events and locates upper ICR fields    # pass 5/5

8. Explanation of Observed Behavior

In tests.c case 4 (line 29), two GPIO lines have latched pending interrupts simultaneously: bit 3 (0x8U) and bit 1 (0x2U), giving pending = 0xaU. When the handler acknowledges bit 3 (mask = 8U), starter/core.c evaluates 0xaU & ~(0xaU | 8U) == 0U, wiping bit 1 before its handler ever runs and aborting check 5 (not ok 5). Even worse, on line 30 (acknowledge(0xaU, 0U)), starter would also wipe 0xaU down to 0U on a no-op mask! Returning pending & ~mask in files/core.c preserves 0x2U on line 29 and 0xaU on line 30, yielding # pass 5/5.

9. Experiment Variations

  1. Missing PKE Master-Enable Check (bias_mode): Change bias_mode() in a scratch copy to inspect PUE (bit 13) without first testing PKE (bit 12): tests.c:25 (bias_mode(1U << 13)) returns 2U (pull) instead of 0U (disabled), mirroring the real datasheet trap where PUE is ignored whenever PKE == 0.
  2. Upper-Bank Modulo Omission (icr_field): Replace (pin % 16U) * 2U with pin * 2U: pin = 16U computes shift = 32U (an out-of-range shift width on GPIOx_ICR2 instead of shift = 0U at ICR2[1:0]).
  3. SION Cleared by Narrow Mux Mask: Change ~UINT32_C(0x1f) to ~UINT32_C(0x0f) in pad_mux(), call pad_mux(&r, 5U, true, 0x21U) followed by pad_mux(&r, 0U, false, 0x21U), and observe stale SION (0x10U) remain stuck high!

10. Performance Measurements

Measured on Linux 7.0.0-42-generic (x86_64) with CLOCK_MONOTONIC across 5 warmup runs and 35 timed runs:

Workload Measured (N = 35, warmup = 5)Host p50Host p95Host p99Cortex-A7 @ 528 MHz Hardware Equivalent
Full sh check.sh (make -B + gcc link + 4 cases)135.10 ms151.49 ms154.74 msHost toolchain + shell subshell orchestration
verify 4-case helper suite (verify 1..4)3554.3 us4110.7 us4220.6 us< 2 us in-cache register-helper calls
Direct W1C STR (GPIO1->ISR = mask) vs RMW1 store vs 1 load + 1 store1 store1 storeSaves 1 IPG bus read (~30 - 50 ns) and prevents race loss

11. Troubleshooting

SymptomRoot CauseFix
not ok 5 (acknowledge(0xaU, 8U) returns 0U)starter simulates `ISR \= mask via pending & ~(pending \mask)`Return pending & ~mask; (write mask directly to ISR on hardware)
not ok 4 (bias_mode(1U << 13) returns 2U)Tested PUE (bit 13) before checking master enable PKE (bit 12)Return 0U immediately whenever (value & (1U << 12)) == 0U
not ok 3 (pad_mux shifts UB on alt = 32U)Evaluated UINT16_C(1) << alt prior to checking alt > 8UGuard `if (alt > 8U \\...) return false; before forming 1 << alt`
not ok 2 (clock_field corrupts adjacent CG slot)Shifted mode by index (1-bit stride) or failed to reject mode > 3UCompute shift = index * 2U and guard index < 16U && mode <= 3U

12. Guided Questions

  1. Why is a direct W1C store (GPIO1->ISR = (1U << 3)) race-free even if hardware latches bit 1 on the exact IPG clock cycle just before the STR lands, whereas a normal RMW register requires interrupt masking?
  • *Hint 1:* Inspect every bit position k != 3 in the 32-bit value 0x00000008U written onto the peripheral data bus.
  • *Hint 2:* Under the hardware W1C contract, what effect does writing 0 to bit 1 have on the ISR[1] flip-flop?
  1. When does an i.MX6ULL peripheral pin require SION = 1 (bit 4 of IOMUXC_SW_MUX_CTL_PAD_*) in addition to selecting the right MUX_MODE?
  • *Hint 1:* Compare an output-only indicator pad against an open-drain I2C SCL/SDA pad or a GPIO output loopback check via GPIOx_PSR.
  • *Hint 2:* What happens to the pad's input path when the selected mux function drives the pad purely as an output unless SION forces the input buffer active?

13. Independent Challenges

  1. Near Transfer — Complete IOMUXC_SW_PAD_CTL Encoder/Verifier: Add bool build_pad_ctl(bool hys, unsigned pus, bool pue, bool pke, bool ode, unsigned speed, unsigned dse, bool sre, uint32_t *out) that rejects pus > 3U || speed > 3U || dse > 7U, packs bits 16, 15:14, 13, 12, 11, 7:6, 5:3, and 0, asserts (*out & ~0x0001F8F9U) == 0U, and verifies bias_mode(*out) agrees with (pke, pue).
  2. Far Transfer — Full ICR1/ICR2 Trigger-Type Configurator (gpio_set_irq_trigger): Implement bool gpio_set_irq_trigger(uint32_t *icr1, uint32_t *icr2, uint32_t *edge_sel, unsigned pin, unsigned trig) supporting LOW_LEVEL (0), HIGH_LEVEL (1), RISING_EDGE (2), FALLING_EDGE (3), and BOTH_EDGES (4) (remembering that EDGE_SEL[pin] = 1 overrides ICR1/ICR2 on i.MX6ULL!).

14. Solutions & Explanations

  • Guided Question 1: Writing 0x00000008U places 0 on bits [31:4] and [2:0]; under W1C hardware logic (next_ISR = (ISR & ~bus_write) | new_hw_edges), 0 bits are unconditional no-ops, so no read occurs and concurrent hardware edges on bit 1 cannot be overwritten.
  • Guided Question 2: SION = 1 forces the pad's input path active regardless of the output mux direction, allowing I2C controllers to monitor bus arbitration/clock-stretching on SDA/SCL and allowing software to read actual physical pad voltage back through GPIOx_PSR.
  • Challenge 2 Key Logic: Route pin < 16U to icr1 and pin >= 16U to icr2 via icr_field(pin, &reg, &shift); if trig == 4U, set *edge_sel |= (UINT32_C(1) << pin), otherwise clear *edge_sel &= ~(UINT32_C(1) << pin) and write trig & 3U into (reg == 0U ? icr1 : icr2) at shift.

Run locally in your terminal and check off each verification milestone.

Not started

Key takeaways

  • 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.
  • 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.
Progress is saved on this device.
up next · module 3 labLab: CCM Gate Slots, IOMUXC Pad/Bias Decoding & Non-Destructive GPIO W1C (30 min)
→ module 3 lab

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure