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.
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:
DR(+0x00) vsGDIR(+0x04) vsPSR(+0x08):DRis an internal32-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-onlyPSRsamples the actual electrical voltage at the package ball! Two immediate engineering consequences follow:
- Why
DR!=PSR: IfDR[3] = 1andGDIR[3] = 1, yet an external short-circuit or open-drain slave clamps the board trace to0 V(orSW_PAD_CTL.DSE == 0left the driverHi-Z), readingDRreturns1(what you requested), whereas readingPSRreturns0(what is physically on the wire)! - Glitch-Free Active-Low Startup (Panel 2 of Figure 3.3a): At reset,
DRandGDIRboth clear to0. On an active-low LED, buzzer, or relay (0= ON!), if firmware setsGDIR[3] = 1before settingDR[3] = 1, the tri-state buffer turns ON whileDR[3]is still0—firing a spurious low pulse onto the hardware load! Always preloadDRto its safe inactive state (1) +dsb sybefore settingGDIR = 1!
- Interrupt Trigger Split (
ICR1/ICR2) & WhyGPIO_ISRIs Write-One-to-Clear (W1C): Configuring4trigger modes (00low-level,01high-level,10rising-edge,11falling-edge) across32pins takes32 * 2 = 64bits, so hardware splits pins0..15intoICR1(+0x0C) and pins16..31intoICR2(+0x10) at local shift2 * (pin % 16)(withEDGE_SELat+0x1Coverriding for dual-edge capture), masked byIMR(+0x14). Why did silicon architects makeISR(+0x18) Write-One-to-Clear (W1C) instead of ordinary read/write? Because asynchronous pin edges can set a sibling bit inISRon any clock cycle between a CPULDRandSTR! WithW1C(ISR_next = ISR_curr & ~W_data), writing0bits is a hardware no-op and writing a pure mask (0x08for pin3) atomically clears only bit3in a singleSTR—whereas an ordinary RMW (ISR |= 0x08) replays1s on every pending sibling bit (0x0A) and wipes them out (0x00)!
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:
- Step 1. Step 1 (Output Latch): Preload inactive high DR[3] = 1 (+0x00) + DSB SY before enabling output direction.
- Step 2. Step 2 (Direction): Set GDIR[3] = 1 (+0x04) via masked RMW so all 31 neighboring pins keep their directions.
- Step 3. Step 3 (Pad Observation): Sample physical wire state from read-only PSR (+0x08), which can differ from DR under load/clamp.
- 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:
ICR1(0..15) vsICR2(16..31) Shift Math: Pin15occupies bits[31:30]ofICR1(shift = 30). Pin16starts over at bits[1:0](shift = 0!) ofICR2! Writing2 * pinwithoutpin % 16shifts by32..62for pins16..31—triggeringISO C11Undefined Behavior and missingICR2completely.- Zero-Load
W1CAcknowledgment (gpio1_ack_pin3): Look at lines23..28oftarget.S! Whereasled_prepare_inactiveandled_assertexecuteldr -> orr/bic -> stron ordinary RW latchesDR(0x0209C000) andGDIR(0x0209C004),gpio1_ack_pin3executesmov r1, #8followed immediately bystr r1, [r0](0x0209C018) with NOldr! OnISR = 0x0A(pins3and1pending), storing0x08leaves0x02intact, whereasldr + orr #8 + strwrites0x0Aand destroys pin1's interrupt!
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$ 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 lrSimulating 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$ 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; }- Partition pins 0..15 into ICR1 and pins 16..31 into ICR2 so local shifts stay within [0, 30].
- Hardware W1C clears every bit position written as 1 (b->isr &= ~word_written); writing 0 leaves pending bits intact.
- 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-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-l03learner@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 Meaning | Write Semantics | Safe Single-Pin Update Pattern | |
|---|---|---|---|---|
GPIOx_DR (+0x00) | Latched output value | Ordinary read/write latch | Exclusive-owner `(DR & ~mask) \ | val` |
GPIOx_GDIR (+0x04) | 0 = input, 1 = output | Ordinary read/write latch | Preload DR safe state + dsb sy first, then `GDIR \ | = mask` |
GPIOx_PSR (+0x08) | Sampled pad receiver state | Read-only (const in C overlay) | Read (PSR >> pin) & 1U (set SION=1 if output readback required) | |
GPIOx_ISR (+0x18) | 1 = latched trigger event | Write-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.
→ open standalone lab workbench
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
- Update individual
2-bitCCM_CCGRxclock-gate slots (indexin0..15,modein0..3) without disturbing the remaining15peripheral gates in the32-bit word. - Validate per-pad
MUX_MODE[3:0]legality against auint16_t allowedbitmask while orthogonally encodingSION(bit 4,0x10U) inpad_mux(). - Decode
IOMUXC_SW_PAD_CTLbias states (0disabled whenPKE == 0,1keeper whenPKE == 1 && PUE == 0,2pull whenPKE == 1 && PUE == 1) inbias_mode(). - Repair write-1-to-clear (
W1C) interrupt acknowledgment (acknowledge) andGPIOx_ICR1/ICR22-bit register/shift indexing (icr_field) across pins0..31.
3. Environment Requirements
- Tested Workstation Host:
Linux 7.0.0-42-generic(x86_64) insidelabs/lab_m03_iomuxc_gpio_mmio/. - Target Silicon Alignment: Bit positions match
i.MX6ULLCCM_CCGR0..6(2bits perCG),IOMUXC_SW_MUX_CTL_PAD_*(MUX_MODE[3:0],SION[4]),IOMUXC_SW_PAD_CTL_PAD_*(PKE[12],PUE[13]), andGPIO1..GPIO5(ICR1pins0..15,ICR2pins16..31,ISRW1C).
4. Required Software & Hardware
- Software:
gcc(-std=c11 -Wall -Wextra -Werror -pedantic -MMD -MP), GNUmake, GNUar, POSIX/bin/sh, and Binutils (nm,objdump). - Hardware: Standard Linux workstation (
0physical target hardware required for automatedTAPgrading).
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$ 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()rejectsindex >= 16U || mode > 3Uand updatesshift = index * 2U(CG13 -> bits 27:26 = 0x0c000000Uintests.ccase1).pad_mux()rejectsalt > 8Ubefore shiftingUINT16_C(1) << alt, checks(allowed & (UINT16_C(1) << alt)) != 0U, and writes(*r & ~UINT32_C(0x1f)) | alt | (sion ? 0x10U : 0U)(0x15UforALT5 + SION).bias_mode&icr_field(core.c:13-17, 22-26):bias_mode()checksPKE(bit 12) first -- returning0Ueven ifPUE(bit 13) is1(1U << 13intests.c:25),1Ufor0x10b0U(keeper), and2Ufor0x1b000U(pull).icr_field()mapspin / 16UtoICR1(0) vsICR2(1) and(pin % 16U) * 2Uto the even shift (pin 15 -> reg 0, shift 30;pin 16 -> reg 1, shift 0).- Seeded Flaw in
starter/core.c:18-21(acknowledge):startercomputesreturn pending & ~(pending | mask);-- the exact state transition produced on hardware when buggy code executesGPIO1->ISR |= mask(writingpending | maskto aW1Cregister clearspending | mask, always leaving0U!). - Verified Repair in
files/core.c:18-21: Replaces the body withreturn pending & ~mask;, modeling a directGPIO1->ISR = maskstore that clears onlymask(0x8U) and leaves concurrent pending bit1(0x2U) intact (acknowledge(0xaU, 8U) == 2U).
7. Expected TAP-13 Output
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/58. 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
- Missing
PKEMaster-Enable Check (bias_mode): Changebias_mode()in a scratch copy to inspectPUE(bit 13) without first testingPKE(bit 12):tests.c:25(bias_mode(1U << 13)) returns2U(pull) instead of0U(disabled), mirroring the real datasheet trap wherePUEis ignored wheneverPKE == 0. - Upper-Bank Modulo Omission (
icr_field): Replace(pin % 16U) * 2Uwithpin * 2U:pin = 16Ucomputesshift = 32U(an out-of-range shift width onGPIOx_ICR2instead ofshift = 0UatICR2[1:0]). SIONCleared by Narrow Mux Mask: Change~UINT32_C(0x1f)to~UINT32_C(0x0f)inpad_mux(), callpad_mux(&r, 5U, true, 0x21U)followed bypad_mux(&r, 0U, false, 0x21U), and observe staleSION(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 p50 | Host p95 | Host p99 | Cortex-A7 @ 528 MHz Hardware Equivalent |
|---|---|---|---|---|
Full sh check.sh (make -B + gcc link + 4 cases) | 135.10 ms | 151.49 ms | 154.74 ms | Host toolchain + shell subshell orchestration |
verify 4-case helper suite (verify 1..4) | 3554.3 us | 4110.7 us | 4220.6 us | < 2 us in-cache register-helper calls |
Direct W1C STR (GPIO1->ISR = mask) vs RMW | 1 store vs 1 load + 1 store | 1 store | 1 store | Saves 1 IPG bus read (~30 - 50 ns) and prevents race loss |
11. Troubleshooting
| Symptom | Root Cause | Fix | ||
|---|---|---|---|---|
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 > 8U | Guard `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 > 3U | Compute shift = index * 2U and guard index < 16U && mode <= 3U |
12. Guided Questions
- Why is a direct
W1Cstore (GPIO1->ISR = (1U << 3)) race-free even if hardware latches bit1on the exactIPGclock cycle just before theSTRlands, whereas a normal RMW register requires interrupt masking?
- *Hint 1:* Inspect every bit position
k != 3in the32-bit value0x00000008Uwritten onto the peripheral data bus. - *Hint 2:* Under the hardware
W1Ccontract, what effect does writing0to bit1have on theISR[1]flip-flop?
- When does an
i.MX6ULLperipheral pin requireSION = 1(bit 4ofIOMUXC_SW_MUX_CTL_PAD_*) in addition to selecting the rightMUX_MODE?
- *Hint 1:* Compare an output-only indicator pad against an open-drain
I2CSCL/SDApad or aGPIOoutput loopback check viaGPIOx_PSR. - *Hint 2:* What happens to the pad's input path when the selected mux function drives the pad purely as an output unless
SIONforces the input buffer active?
13. Independent Challenges
- Near Transfer — Complete
IOMUXC_SW_PAD_CTLEncoder/Verifier: Addbool build_pad_ctl(bool hys, unsigned pus, bool pue, bool pke, bool ode, unsigned speed, unsigned dse, bool sre, uint32_t *out)that rejectspus > 3U || speed > 3U || dse > 7U, packs bits16,15:14,13,12,11,7:6,5:3, and0, asserts(*out & ~0x0001F8F9U) == 0U, and verifiesbias_mode(*out)agrees with(pke, pue). - Far Transfer — Full
ICR1/ICR2Trigger-Type Configurator (gpio_set_irq_trigger): Implementbool gpio_set_irq_trigger(uint32_t *icr1, uint32_t *icr2, uint32_t *edge_sel, unsigned pin, unsigned trig)supportingLOW_LEVEL (0),HIGH_LEVEL (1),RISING_EDGE (2),FALLING_EDGE (3), andBOTH_EDGES (4)(remembering thatEDGE_SEL[pin] = 1overridesICR1/ICR2oni.MX6ULL!).
14. Solutions & Explanations
- Guided Question 1: Writing
0x00000008Uplaces0on bits[31:4]and[2:0]; underW1Chardware logic (next_ISR = (ISR & ~bus_write) | new_hw_edges),0bits are unconditional no-ops, so no read occurs and concurrent hardware edges on bit1cannot be overwritten. - Guided Question 2:
SION = 1forces the pad's input path active regardless of the output mux direction, allowingI2Ccontrollers to monitor bus arbitration/clock-stretching onSDA/SCLand allowing software to read actual physical pad voltage back throughGPIOx_PSR. - Challenge 2 Key Logic: Route
pin < 16Utoicr1andpin >= 16Utoicr2viaicr_field(pin, ®, &shift); iftrig == 4U, set*edge_sel |= (UINT32_C(1) << pin), otherwise clear*edge_sel &= ~(UINT32_C(1) << pin)and writetrig & 3Uinto(reg == 0U ? icr1 : icr2)atshift.
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-onlyGPIOx_PSR(+0x08) samples physical pad state; preloadDRinactive before settingGDIRon active-low loads.GPIOx_ICR1(+0x0C) configures2-bit triggers for pins0..15andGPIOx_ICR2(+0x10) configures pins16..31at local shift2 * (pin % 16).GPIOx_IMR(+0x14) masks interrupt propagation whileGPIOx_ISR(+0x18) latches trigger events.GPIOx_ISRis 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.