Lab 3: CCM Gate Slots, IOMUXC Pad/Bias Decoding & Non-Destructive GPIO W1C
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 · 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-m03ls -lastarter/Makefilestarter/core.cstarter/core.hstarter/main.clearner@ksa:~/gl-e01/lab-m03starter: starter/Makefile
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_demostarter: starter/core.c
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$ 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); #endifstarter: starter/main.c
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
- 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.
step 3/15 · 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).
step 4/15 · 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).
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$ 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()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).
step 7/15 · 7. expected tap-13 output
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/5step 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
- 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!
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 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 MMIO 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 |
step 11/15 · 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 |
step 12/15 · 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?
step 13/15 · 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!).
step 14/15 · 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.
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./check.shlearner@ksa:~/gl-e01/lab-m03TAP version 131..5ok 1 - strict C11 build and trusted grader linkProgressive 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
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-e01/lab-m03$ bat files/Makefile File: files/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_demosolution: files/core.c
learner@ksa:~/gl-e01/lab-m03$ bat files/core.c File: files/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 & ~mask; } bool icr_field(unsigned pin, unsigned *reg, unsigned *shift) { if (pin>=32U) return false; *reg=pin/16U; *shift=(pin%16U)*2U; return true; }solution: files/core.d
learner@ksa:~/gl-e01/lab-m03$ bat files/core.d File: files/core.d core.o: core.c core.h core.h:solution: files/core.h
learner@ksa:~/gl-e01/lab-m03$ bat files/core.h File: files/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); #endifsolution: files/main.c
learner@ksa:~/gl-e01/lab-m03$ bat files/main.c File: files/main.c #include "core.h" #include <stdio.h> int main(void) { puts("GL-E01 BSP laboratory: run ../check.sh for graded evidence"); return 0; }solution: files/main.d
learner@ksa:~/gl-e01/lab-m03$ bat files/main.d File: files/main.d main.o: main.c core.h core.h: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:
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/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
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/5note: 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$ 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.