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

30 minfree previewLinux 6.12C11U-Boot 2025.x← module 3

Implement, compile, and verify lab_m03_iomuxc_gpio_mmio against ./check.sh.

Prerequisites in this module

What ./check.sh verifies

  • Strict C11 build and trusted grader link.
  • Repair the C11 build before testing behavior.
  • Updates one clock gate without changing neighboring fields.
  • Validates pad-specific mux modes and SION independently.
step 1/15

step 1/15 · inspect starter files and specification

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.

learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03ls -lastarter/Makefilestarter/core.cstarter/core.hstarter/main.clearner@ksa:~/gl-e01/lab-m03
starter: starter/Makefile
learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03$ bat starter/Makefile File: starter/Makefile    CC = gcc    AR = ar    override CFLAGS += -std=c11 -Wall -Wextra -Werror -pedantic -MMD -MP    all: libbsp.a bsp_demo    libbsp.a: core.o    	$(AR) rcs $@ $^    bsp_demo: main.o libbsp.a    	$(CC) $(CFLAGS) main.o libbsp.a -o $@    %.o: %.c core.h    	$(CC) $(CFLAGS) -c $< -o $@    -include core.d main.d    .PHONY: all clean    clean:    	rm -f core.o main.o core.d main.d libbsp.a bsp_demo
starter: starter/core.c
learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03$ bat starter/core.c File: starter/core.c    #include "core.h"    bool clock_field(uint32_t *r, unsigned index, unsigned mode)    {        if (index>=16U || mode>3U) return false;        unsigned shift=index*2U; uint32_t mask=3U<<shift;        *r=(*r & ~mask)|((uint32_t)mode<<shift); return true;    }    bool pad_mux(uint32_t *r, unsigned alt, bool sion, uint16_t allowed)    {        if (alt>8U || (allowed & (UINT16_C(1)<<alt))==0U) return false;        *r=(*r & ~UINT32_C(0x1f))|alt|(sion?0x10U:0U); return true;    }    unsigned bias_mode(uint32_t value)    {        if ((value & (1U<<12))==0U) return 0U;        return (value & (1U<<13))!=0U ? 2U : 1U;    }    uint32_t acknowledge(uint32_t pending, uint32_t mask)    {        return pending & ~(pending | mask);    }    bool icr_field(unsigned pin, unsigned *reg, unsigned *shift)    {        if (pin>=32U) return false;        *reg=pin/16U; *shift=(pin%16U)*2U; return true;    }
starter: starter/core.h
learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03$ bat starter/core.h File: starter/core.h    #ifndef GL_E01_CORE_H    #define GL_E01_CORE_H    #include <stdbool.h>    #include <stdint.h>    bool clock_field(uint32_t *r, unsigned index, unsigned mode);    bool pad_mux(uint32_t *r, unsigned alt, bool sion, uint16_t allowed);    unsigned bias_mode(uint32_t value);    uint32_t acknowledge(uint32_t pending, uint32_t mask);    bool icr_field(unsigned pin, unsigned *reg, unsigned *shift);    #endif
starter: starter/main.c
learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03$ bat starter/main.c File: starter/main.c    #include "core.h"    #include <stdio.h>    int main(void) { puts("GL-E01 BSP laboratory: run ../check.sh for graded evidence"); return 0; }

step 2/15 · 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.

step 3/15 · 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).

step 4/15 · 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).

step 5/15 · 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/lab-m03
learner@ksa:~/gl-e01/lab-m03$ 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.

step 6/15 · 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).

step 7/15 · 7. expected tap-13 output

learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03$ 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

step 8/15 · 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.

step 9/15 · 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!

step 10/15 · 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 MMIO 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

step 11/15 · 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

step 12/15 · 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?

step 13/15 · 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!).

step 14/15 · 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.

step 15/15 · run verification and inspect output

Run the verification command and confirm the output matches the expected contract.

learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03./check.shlearner@ksa:~/gl-e01/lab-m03
expected output
TAP version 131..5ok 1 - strict C11 build and trusted grader link

Progressive hints & solution

hint 1

Compare starter/Makefile against the module lessons. Focus first on: Strict C11 build and trusted grader link.

hint 2

Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.

solution

Run the check

From labs/lab_m03_iomuxc_gpio_mmio/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:

./check.sh — lab m03
learner@ksa:~/gl-e01/lab-m03./check.shTAP version 131..5ok 1 - strict C11 build and trusted grader linkok 2 - updates one clock gate without changing neighboring fieldsok 3 - validates pad-specific mux modes and SION independentlyok 4 - distinguishes disabled bias keeper and enabled pullok 5 - clears only requested W1C events and locates upper ICR fields# pass 5/5

On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:

./check.sh — lab m03 (starter)
learner@ksa:~/gl-e01/lab-m03./check.shTAP version 131..5ok 1 - strict C11 build and trusted grader linkok 2 - updates one clock gate without changing neighboring fieldsok 3 - validates pad-specific mux modes and SION independentlyok 4 - distinguishes disabled bias keeper and enabled pullnot 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

note: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....

inspect harness: check.sh
learner@ksa: ~/gl-e01/lab-m03
learner@ksa:~/gl-e01/lab-m03$ bat check.sh File: check.sh    #!/bin/sh    set -u    cd "$(dirname "$0")" || exit 1    PASS=0    WORK=$(mktemp -d "./.check.XXXXXX") || exit 1    trap 'rm -rf "$WORK"' EXIT HUP INT TERM    printf 'TAP version 13\n1..5\n'    if make -B -C files >"$WORK/build.log" 2>&1 &&       gcc -std=c11 -Wall -Wextra -Werror -pedantic -Ifiles tests.c files/libbsp.a -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then        printf 'ok 1 - strict C11 build and trusted grader link\n'        PASS=$((PASS + 1))    else        printf 'not ok 1 - strict C11 build and trusted grader link\n'        printf "  ---\n  message: 'Compilation failed'\n  hint: 'Fix the C11 diagnostics in files and preserve the exported ABI'\n  ...\n"        printf 'FAIL: repair the C11 build before testing behavior\n' >&2    fi    if [ -x "$WORK/verify" ] && "$WORK/verify" 1 >"$WORK/case2.log" 2>&1; then        printf 'ok 2 - updates one clock gate without changing neighboring fields\n'        PASS=$((PASS + 1))    else        printf 'not ok 2 - updates one clock gate without changing neighboring fields\n'        printf "  ---\n  message: 'Behavior differs from the independently specified test vectors'\n  hint: 'Repair case 1; inspect tests.c for the exact input and expected result'\n  ...\n"    fi    if [ -x "$WORK/verify" ] && "$WORK/verify" 2 >"$WORK/case3.log" 2>&1; then        printf 'ok 3 - validates pad-specific mux modes and SION independently\n'        PASS=$((PASS + 1))    else        printf 'not ok 3 - validates pad-specific mux modes and SION independently\n'        printf "  ---\n  message: 'Behavior differs from the independently specified test vectors'\n  hint: 'Repair case 2; inspect tests.c for the exact input and expected result'\n  ...\n"    fi    if [ -x "$WORK/verify" ] && "$WORK/verify" 3 >"$WORK/case4.log" 2>&1; then        printf 'ok 4 - distinguishes disabled bias keeper and enabled pull\n'        PASS=$((PASS + 1))    else        printf 'not ok 4 - distinguishes disabled bias keeper and enabled pull\n'        printf "  ---\n  message: 'Behavior differs from the independently specified test vectors'\n  hint: 'Repair case 3; inspect tests.c for the exact input and expected result'\n  ...\n"    fi    if [ -x "$WORK/verify" ] && "$WORK/verify" 4 >"$WORK/case5.log" 2>&1; then        printf 'ok 5 - clears only requested W1C events and locates upper ICR fields\n'        PASS=$((PASS + 1))    else        printf 'not ok 5 - clears only requested W1C events and locates upper ICR fields\n'        printf "  ---\n  message: 'Behavior differs from the independently specified test vectors'\n  hint: 'Repair case 4; inspect tests.c for the exact input and expected result'\n  ...\n"    fi    printf '# pass %d/5\n' "$PASS"    [ "$PASS" -eq 5 ]

Definition of done

Verification checklist

status: 0/3 checks verified

Troubleshooting

Compiler fails under -Werror on sign conversion or unused args

Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.

./check.sh reports missing files in files/

Invoke ./check.sh from inside labs/lab_m03_iomuxc_gpio_mmio/ so relative paths to files/ resolve directly.

Progress is saved on this device.

search — fzf

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

keyboard shortcuts — less

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

figure