Lab 1: CCM Clock-Tree Handoff, PFD Lane Masks & Bus-Divider Verifier
Calculate parent and root frequencies, preserve unrelated register fields, and reject unsafe clock transitions.
Prerequisites in this module
What ./check.sh verifies
lab_m01_ccm_pll_tree(recorded value/5 passed; first failure -> recorded value) " recorded value recorded value.
step 1/15 · inspect starter files and specification
Early ARMv7-A board bring-up frequently fails before a single character reaches UART1 because clock-initialization code mutates CCM_ANALOG_PLL_ARM (0x020C8000) while PLL1 is still clocking the Cortex-A7 pipeline, overwrites sibling byte lanes inside CCM_ANALOG_PFD_528 (0x020C8100), or computes 528,000,000 * 18 / PFD_FRAC inside 32-bit unsigned arithmetic where 9,504,000,000 wraps silently modulo 2^32 to 914,065,408. In this studio lab, you will diagnose two seeded silent-compile clock bugs in starter/, repair the PLL1 step-clock state guard and 64-bit PFD boundary verifier, and validate AHB/IPG/PERCLK root dividers under TAP version 13.
learner@ksa:~/gl-e02/lab-m01ls -lastarter/Makefilestarter/boundary.cstarter/checks.hstarter/lab.cstarter/m01_l01.cstarter/m01_l02.cstarter/m01_l03.clearner@ksa:~/gl-e02/lab-m01starter: starter/Makefile
learner@ksa:~/gl-e02/lab-m01$ bat starter/Makefile File: starter/Makefile CC = gcc CFLAGS = -std=c11 -Wall -Wextra -Werror -O2 all: lab lab: lab.o boundary.o m01_l01.o m01_l02.o m01_l03.o $(CC) $(CFLAGS) $^ -o $@ lab.o: lab.c checks.h $(CC) $(CFLAGS) -c $< -o $@ boundary.o: boundary.c checks.h $(CC) $(CFLAGS) -c $< -o $@ m01_l01.o: m01_l01.c checks.h $(CC) $(CFLAGS) -Dmain=m01_l01_check -c $< -o $@ m01_l02.o: m01_l02.c checks.h $(CC) $(CFLAGS) -Dmain=m01_l02_check -c $< -o $@ m01_l03.o: m01_l03.c checks.h $(CC) $(CFLAGS) -Dmain=m01_l03_check -c $< -o $@ clean: rm -f *.o lab .PHONY: all cleanstarter: starter/boundary.c
learner@ksa:~/gl-e02/lab-m01$ bat starter/boundary.c File: starter/boundary.c #include <assert.h> #include <stdint.h> #include "checks.h" static int pfd(uint32_t n, uint32_t *out) { /* BUG 2: off-by-one lower bound (11U) and 32-bit numerator overflow! */ if (n < 11U || n > 35U) return -1; *out = (528000000U * 18U) / n; return 0; } int boundary_check(void) { uint32_t out = 7U; assert(pfd(0U, &out) == -1 && out == 7U); assert(pfd(11U, &out) == -1 && pfd(36U, &out) == -1); for (uint32_t n = 12U; n <= 35U; ++n) { assert(pfd(n, &out) == 0); assert((uint64_t)out * n <= UINT64_C(528000000) * 18U); assert((uint64_t)(out + 1U) * n > UINT64_C(528000000) * 18U); } return 0; }starter: starter/checks.h
learner@ksa:~/gl-e02/lab-m01$ bat starter/checks.h File: starter/checks.h #ifndef GL_E02_CHECKS_H #define GL_E02_CHECKS_H int m01_l01_check(void); int m01_l02_check(void); int m01_l03_check(void); int boundary_check(void); #endifstarter: starter/lab.c
learner@ksa:~/gl-e02/lab-m01$ bat starter/lab.c File: starter/lab.c #define _POSIX_C_SOURCE 200809L #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include "checks.h" static int cmp_double(const void *a, const void *b) { double da = *(const double *)a; double db = *(const double *)b; return (da > db) - (da < db); } static int run_benchmark(void) { enum { WARMUP = 5, SAMPLES = 35, LOOPS = 100 }; double us[SAMPLES]; fflush(stdout); int saved_out = dup(STDOUT_FILENO); int devnull = open("/dev/null", O_WRONLY); if (saved_out < 0 || devnull < 0) return EXIT_FAILURE; if (dup2(devnull, STDOUT_FILENO) < 0) return EXIT_FAILURE; close(devnull); int rc = 0; for (int s = -WARMUP; s < SAMPLES; ++s) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int k = 0; k < LOOPS; ++k) { if (m01_l01_check() != 0) rc = 1; if (m01_l02_check() != 0) rc = 1; if (m01_l03_check() != 0) rc = 1; if (boundary_check() != 0) rc = 1; } clock_gettime(CLOCK_MONOTONIC, &t1); fflush(stdout); if (s >= 0) { double sec = (double)(t1.tv_sec - t0.tv_sec); double nsec = (double)(t1.tv_nsec - t0.tv_nsec); us[s] = sec * 1e6 + nsec * 1e-3; } } fflush(stdout); if (dup2(saved_out, STDOUT_FILENO) < 0) return EXIT_FAILURE; close(saved_out); if (rc != 0) return EXIT_FAILURE; qsort(us, SAMPLES, sizeof(us[0]), cmp_double); printf("bench_samples=%d warmup=%d loops_per_sample=%d p50_us=%.2f p95_us=%.2f p99_us=%.2f\n", SAMPLES, WARMUP, LOOPS, us[17], us[33], us[34]); return EXIT_SUCCESS; } int main(int argc, char **argv) { if (argc != 2) return EXIT_FAILURE; if (strcmp(argv[1], "--bench") == 0) return run_benchmark(); if (strcmp(argv[1], "1") == 0) return m01_l01_check(); if (strcmp(argv[1], "2") == 0) return m01_l02_check(); if (strcmp(argv[1], "3") == 0) return m01_l03_check(); if (strcmp(argv[1], "4") == 0) return boundary_check(); return EXIT_FAILURE; }starter: starter/m01_l01.c
learner@ksa:~/gl-e02/lab-m01$ bat starter/m01_l01.c File: starter/m01_l01.c #include <assert.h> #include <stdint.h> #include <stdio.h> struct clock_path { int step, locked; uint32_t select, podf; }; static int retune(struct clock_path *c, uint32_t select) { if (!c->step || select < 54U || select > 108U) return -1; c->locked = 0; c->select = select; return 0; } static int reconnect(struct clock_path *c) { /* BUG 1: switches CCSR back to PLL1 without verifying PLL_ARM.LOCK! */ c->step = 0; return 0; } int main(void) { struct clock_path c = {0, 1, 66U, 1U}; assert(retune(&c, 88U) == -1); c.step = 1; assert(retune(&c, 88U) == 0); assert(reconnect(&c) == -1 && c.step); c.locked = 1; assert(reconnect(&c) == 0); uint64_t pll = UINT64_C(24000000) * c.select / 2U; uint64_t arm = pll / (c.podf + 1U); assert(pll == UINT64_C(1056000000)); assert(arm == UINT64_C(528000000)); puts("PLL1=1056000000 ARM=528000000 safe_transition=pass"); return 0; }starter: starter/m01_l02.c
learner@ksa:~/gl-e02/lab-m01$ bat starter/m01_l02.c File: starter/m01_l02.c #include <assert.h> #include <stdint.h> #include <stdio.h> static uint32_t set_frac(uint32_t reg, unsigned lane, uint32_t frac) { assert(lane < 4U && frac >= 12U && frac <= 35U); unsigned shift = lane * 8U; return (reg & ~(UINT32_C(0x3f) << shift)) | (frac << shift); } static uint32_t rate(uint32_t parent, uint32_t frac) { assert(frac >= 12U && frac <= 35U); return (uint32_t)((uint64_t)parent * 18U / frac); } int main(void) { uint32_t old = UINT32_C(0x605b585b); uint32_t next = set_frac(old, 2U, 24U); uint32_t mask = UINT32_C(0x3f) << 16; assert((old & ~mask) == (next & ~mask)); assert(((next >> 16) & 63U) == 24U); assert(rate(528000000U, 24U) == 396000000U); assert(rate(480000000U, 20U) == 432000000U); uint32_t q = rate(528000000U, 29U); assert((uint64_t)q * 29U <= UINT64_C(9504000000)); assert((uint64_t)(q+1U) * 29U > UINT64_C(9504000000)); puts("PLL2_PFD2=396000000 PLL3_PFD=432000000 neighbors=preserved"); return 0; }starter: starter/m01_l03.c
learner@ksa:~/gl-e02/lab-m01$ bat starter/m01_l03.c File: starter/m01_l03.c #include <assert.h> #include <stdint.h> #include <stdio.h> static int wait_busy(unsigned clear_after, unsigned budget) { for (unsigned n = 0U; n < budget; ++n) if (n >= clear_after) return 0; return -1; } int main(void) { uint32_t ahb_field = 3U, ipg_field = 1U, per_field = 23U; uint32_t ahb = 528000000U / (ahb_field + 1U); uint32_t ipg = ahb / (ipg_field + 1U); uint32_t per = 24000000U / (per_field + 1U); assert(ahb == 132000000U && ipg == 66000000U); assert(per == 1000000U); assert(528000000U / (0U + 1U) == 528000000U); assert(wait_busy(0U, 4U) == 0); assert(wait_busy(3U, 4U) == 0); assert(wait_busy(4U, 4U) == -1); assert(wait_busy(0U, 0U) == -1); puts("AHB=132000000 IPG=66000000 PERCLK=1000000 timeout=detected"); return 0; }step 2/15 · 2. learning objectives
- Enforce safe
CCM_CCSRstep-clock sequencing (STEP_SEL = 0,PLL1_SW_CLK_SEL = 1) soPLL1reconnection is rejected wheneverPLL_ARM.LOCK(bit 31) is unasserted. - Isolate
6-bitPFD_FRACupdates (0x3FU << (8 * lane)) inside packed32-bitPFD_528/PFD_480words without disturbingPFD_STABLE,PFD_CLKGATE, or neighboringPFD0..PFD3lanes. - Verify
uint64_tPFDrational arithmetic across every legal fraction12..35while rejecting out-of-range divisors0..11and36+prior to division. - Measure multi-translation-unit clock-tree verification throughput (
p50/p95/p99) withCLOCK_MONOTONIC.
step 3/15 · 3. environment requirements
- Tested Host Environment: Linux
7.0.0-42-generic(x86_64, POSIX user space) running insidelabs/lab_m01_ccm_pll_tree/. - Target Silicon Alignment (
Cortex-A7): All C11 translation units enforce fixed-width<stdint.h>types (uint32_t,uint64_t) so32-bitARMv7EABI (arm-none-eabi-gcc) and64-bitx86_64hosts produce identicalPLL1(528 MHz),PLL2_PFD2(396 MHz),AHB(132 MHz),IPG(66 MHz), andPERCLK(1 MHz) results.
step 4/15 · 4. required software & hardware
- Software:
gcc(-std=c11 -Wall -Wextra -Werror -O2), GNUmake, POSIX/bin/sh, and GNU Binutils (nm,objdump). - Hardware: Standard Linux workstation (
0root privileges or physicalJTAGprobes required for the 5 automated checks; oscilloscope probing onCCM_CLKO1applies during optional physical board sign-off).
step 5/15 · 5. complete executable commands
Execute the workflow below from labs/lab_m01_ccm_pll_tree/ to observe the failing starter/ baseline, inspect each individual invariant case, benchmark execution latency, and verify the repaired reference build:
learner@ksa:~/gl-e02/lab-m01$ bat script.sh File: script.sh # 1. Run check.sh against starter/ to reproduce the two seeded clock-tree failures sh check.sh starter # 2. Build starter/ in isolation and inspect which assertion aborts in check 2 and check 5 make -B -C starter ./starter/lab 1 ; echo "case1_rc=$?" ./starter/lab 4 ; echo "case4_rc=$?" # 3. Build the clean reference implementation in files/ and inspect linked object symbols make -B -C files nm -g files/lab | grep '_check$' # 4. Run all four individual clock-tree verification cases (PLL1, PFD lanes, AHB/IPG/PERCLK, PFD sweep) ./files/lab 1 && ./files/lab 2 && ./files/lab 3 && ./files/lab 4 # 5. Collect 35-sample CLOCK_MONOTONIC latency percentiles (after 5 warmup iterations) ./files/lab --bench # 6. Execute the authoritative 5-check TAP version 13 gate sh check.sh*Command rationale:* sh check.sh starter compiles starter/ in a temporary sandbox to expose silent-compile defects without mutating your working tree; ./starter/lab 1 and ./starter/lab 4 isolate the PLL1 lock-bypass and PFD 32-bit overflow failures; nm -g confirms -Dmain=m01_l0X_check symbol renaming across translation units; and sh check.sh confirms 5/5 TAP checks exit 0.
step 6/15 · 6. annotated source code (`starter/` flaws vs `files/` reference)
Seeded Defects in starter/
- Unlocked
PLL1Reconnection (starter/m01_l01.c):reconnect()clearsc->step = 0unconditionally without checkingif (!c->locked) return -1;. OnCortex-A7hardware, clearingCCM_CCSR.PLL1_SW_CLK_SELafter aPLL_ARM.LOCKtimeout routes an unlocked or stopped VCO directly intoARM_CLK_ROOT, freezing instruction fetch before any diagnostic can run. - Off-By-One
PFD_FRACRange &32-Bit Numerator Wrap (starter/boundary.c):pfd()checksif (n < 11U || n > 35U)(accepting illegalPFD_FRAC = 11, which would attempt864 MHzout of a528 MHzparent!) and evaluates(528000000U * 18U) / nin32-bituint32_t, wrapping9,504,000,000down to914,065,408(38,086,058 Hzatn = 24instead of396,000,000 Hz!).
Verified Repairs in files/
files/m01_l01.c: Enforcesif (!c->step || select < 54U || select > 108U) return -1;inretune(), enforcesif (!c->locked) return -1;inreconnect(), and widensUINT64_C(24000000) * c.select / 2Ubefore applyingc.podf + 1U.files/m01_l02.c: Masks~(UINT32_C(0x3f) << shift)inside byte lanelane * 8Uso sibling lanes and bits7:6(CLKGATE,STABLE) remain bit-for-bit identical ((old & ~mask) == (next & ~mask)).files/m01_l03.c: DecodesAHB_PODF = 3(/4 -> 132 MHz),IPG_PODF = 1(/2 -> 66 MHz), andCSC1_PERCLK_PODF = 23from the24 MHzoscillator (/24 -> 1 MHz) while rejectingclear_after == budgetinwait_busy().files/boundary.c: Enforcesn < 12U || n > 35U, computesUINT64_C(528000000) * 18U / n, and provesout * n <= 9,504,000,000 < (out + 1) * nacross all24legal fractions.
step 7/15 · 7. expected output
learner@ksa:~/gl-e02/lab-m01$ bat listing.txt File: listing.txt $ sh check.sh starter TAP version 13 1..5 ok 1 - Strict C11 build from source not ok 2 - PLL1 safe-source transition and output rate --- diagnostic: "Guard reconnect() on c->locked != 0 so CCM_CCSR stays on 24 MHz step_clk until PLL1 locks" ... ok 3 - PFD lane isolation and exact frequency ok 4 - AHB IPG PERCLK encoded dividers not ok 5 - PFD extremes and rejected zero divisor --- diagnostic: "Reject PFD_FRAC outside [12, 35] and use UINT64_C(528000000) * 18U to prevent 32-bit wrap" ... # pass 3/5 FAIL: lab_m01_ccm_pll_tree (3/5 passed; first failure -> check 2 (PLL1 safe-source transition and output rate): Guard reconnect() on c->locked != 0 so CCM_CCSR stays on 24 MHz step_clk until PLL1 locks) $ ./files/lab --bench bench_samples=35 warmup=5 loops_per_sample=100 p50_us=12.34 p95_us=12.43 p99_us=12.44 $ sh check.sh TAP version 13 1..5 ok 1 - Strict C11 build from source ok 2 - PLL1 safe-source transition and output rate ok 3 - PFD lane isolation and exact frequency ok 4 - AHB IPG PERCLK encoded dividers ok 5 - PFD extremes and rejected zero divisor # pass 5/5step 8/15 · 8. explanation of observed behavior
When sh check.sh starter runs, check 1 passes (ok 1) because both seeded bugs are 100% valid ISO C11 with zero compiler warnings under -Wall -Wextra -Werror. Check 2 (not ok 2) aborts on assert(reconnect(&c) == -1 && c.step) because retune(&c, 88U) cleared c.locked = 0 and starter's reconnect() switched c.step back to 0 anyway. Check 5 (not ok 5) aborts on assert(pfd(11U, &out) == -1 ...) and would otherwise fail the rational bracket out * n <= 9,504,000,000 due to 32-bit modular wraparound (9,504,000,000 mod 2^32 = 914,065,408). Once both invariants are restored in files/, all 5 TAP checks pass (# pass 5/5).
step 9/15 · 9. experiment variations
PERCLKParent-Mux Drift (m01_l03.c): Changeperfrom24000000U / (per_field + 1U)(CSCMR1.PERCLK_CLK_SEL = 1, oscillator) toipg / (per_field + 1U)(66 MHz / 24 = 2.75 MHz) and observe check4fail, demonstrating whyEPIT/GPTdrivers must inspect parent muxes rather than divider numbers alone.- Nibble-Mask Corruption (
m01_l02.c): NarrowUINT32_C(0x3f)toUINT32_C(0x0f)inset_frac()whenoldlane2holds0x58(FRAC = 24, bit6STABLE = 1), then update to15: bit4(16) survives, leavingFRAC = 31(306.58 MHzinstead of480 MHz!). - Undefined-Behavior Sanitizer Audit: Run
make -B -C files CFLAGS="-std=c11 -Wall -Wextra -Werror -O1 -g -fsanitize=undefined"and verify./files/lab 1..4produce zeroUBSantraps.
step 10/15 · 10. performance measurements
Measured on the Linux 7.0.0-42-generic build host using CLOCK_MONOTONIC across 5 warmup runs and 35 timed samples (100 full 4-suite iterations per sample):
| Benchmark Metric | Measured Host (x86_64 Linux 7.0.0-42-generic) | Target Cortex-A7 @ 528 MHz Expectation |
|---|---|---|
100x full clock-tree 4-check suite (p50 / p95 / p99) | 12.34 us / 12.43 us / 12.44 us | ~180 - 420 us (pure integer register/SRAM math) |
Physical PLL1 relock time (CCM_ANALOG_PLL_ARM.LOCK) | Modeled state flag (0 ns analog wait) | ~60 - 250 us crystal-to-VCO phase convergence |
CCM_CDHIPR.AHB_PODF_BUSY synchronization | Bounded 4-step state test | ~3 - 12 destination AHB clock cycles (< 1 us) |
step 11/15 · 11. troubleshooting
| Symptom | Root Cause | Fix | ||
|---|---|---|---|---|
not ok 2 (reconnect(&c) == -1 assertion fails) | reconnect() clears c->step without checking c->locked | Add if (!c->locked) return -1; before c->step = 0; in m01_l01.c | ||
not ok 5 (pfd(11U, &out) returns 0 or wrong out) | n < 11U bound or 32-bit 528000000U * 18U wraparound | Reject `n < 12U \ | \ | n > 35U and multiply with UINT64_C(528000000) * 18U` |
Neighbor PFD lanes change during set_frac() | Whole-word write or inverted clear mask (reg & (0x3fU << s)) | Use `(reg & ~(UINT32_C(0x3f) << shift)) \ | (frac << shift)` | |
multiple definition of 'main' during make | Lesson .c compiled without -Dmain=m01_l0X_check | Compile each lesson object via $(CC) $(CFLAGS) -Dmain=$*_check -c |
step 12/15 · 12. guided questions
- Why can
PLL2(528 MHz) produce aPFDoutput faster than528 MHzwithout containing another multiplier stage?
- *Hint 1:* Evaluate
F_pfd = 528 MHz * 18 / PFD_FRACacross the legal lower rangePFD_FRAC = 12..17. - *Hint 2:* Count how many phase taps (
18) thePLL2ring oscillator exposes per528 MHzreference period (1.894 ns / 18 = 105.2 pstap spacing).
- Why does
targetcode executedsb syandisbimmediately afterCCSR |= (1U << 2)before writingPLL_ARM?
- *Hint 1:* Can an out-of-order/buffered
MMIOstore toCCM_ANALOG_PLL_ARM(0x020C8000) reach the analog wrapper while theCCM_CCSRmux (0x020C400C) store is still draining? - *Hint 2:* What does
isbguarantee for instructions fetched into theCortex-A7pipeline after the clock-parent mux switches?
step 13/15 · 13. independent challenges
- Near Transfer —
PLL3(480 MHz)PFD0..PFD3Table & Tolerance Finder: Addint best_pfd_frac(uint32_t parent_hz, uint32_t max_hz, uint32_t target_hz, uint32_t *frac_out, int64_t *ppm_err)that searchesfracin12..35, rejects any candidate exceedingmax_hz, and picks the closest legal fraction in signed parts-per-million (ppm) using64-bit integer math (0doubleoperations). - Far Transfer — Transactional Clock-Tree Dependency Validator: Model a
struct clk_treeconnectingOSC24,PLL1,PLL2,PLL2_PFD2,AHB,IPG, andPERCLK. Implementint clk_reparent_safe(struct clk_tree *t, ...)that refuses to gatePLL2_PFD2wheneverMMDC_ACTIVEis flagged as a live consumer, preventing self-inflictedDDR3fetch lockups.
step 14/15 · 14. solutions & explanations
- Guided Question 1: Stepping fewer than
18phase taps per output period (PFD_FRACin12..17) synthesizes output periods shorter than1 / 528 MHz(up to528 * 18 / 12 = 792 MHzatPFD_FRAC = 12), so software must check each downstream peripheral's maximum input frequency before selecting small fractions. - Guided Question 2:
dsb systalls execution until the postedCCSRwrite completes on theIPSperipheral bus, andisbflushes theCortex-A7instruction pipeline so subsequentPLL_ARMwrites execute only afterstep_clkis actively clocking the core. - Challenge 1 Key Formula: Compute
cand_hz = (UINT64_C(18) * parent_hz) / frac(skipping ifcand_hz > max_hz), then rank exact rational error via|18 * parent_hz - target_hz * frac| * 1,000,000 / (target_hz * frac)inint64_t.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e02/lab-m01./starter/lab 1 ; echo "case1_rc=$?"learner@ksa:~/gl-e02/lab-m01TAP version 131..5ok 1 - Strict C11 build from sourceProgressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: lab_m01_ccm_pll_tree (recorded value/5 passed; first failure -> recorded value) " recorded value recorded value.
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-e02/lab-m01$ bat files/Makefile File: files/Makefile CC = gcc CFLAGS = -std=c11 -Wall -Wextra -Werror -O2 all: lab lab: lab.o boundary.o m01_l01.o m01_l02.o m01_l03.o $(CC) $(CFLAGS) $^ -o $@ lab.o: lab.c checks.h $(CC) $(CFLAGS) -c $< -o $@ boundary.o: boundary.c checks.h $(CC) $(CFLAGS) -c $< -o $@ m01_l01.o: m01_l01.c checks.h $(CC) $(CFLAGS) -Dmain=m01_l01_check -c $< -o $@ m01_l02.o: m01_l02.c checks.h $(CC) $(CFLAGS) -Dmain=m01_l02_check -c $< -o $@ m01_l03.o: m01_l03.c checks.h $(CC) $(CFLAGS) -Dmain=m01_l03_check -c $< -o $@ clean: rm -f *.o lab .PHONY: all cleansolution: files/boundary.c
learner@ksa:~/gl-e02/lab-m01$ bat files/boundary.c File: files/boundary.c #include <assert.h> #include <stdint.h> #include "checks.h" static int pfd(uint32_t n, uint32_t *out) { if (n < 12U || n > 35U) return -1; *out = (uint32_t)(UINT64_C(528000000) * 18U / n); return 0; } int boundary_check(void) { uint32_t out = 7U; assert(pfd(0U, &out) == -1 && out == 7U); assert(pfd(11U, &out) == -1 && pfd(36U, &out) == -1); for (uint32_t n = 12U; n <= 35U; ++n) { assert(pfd(n, &out) == 0); assert((uint64_t)out * n <= UINT64_C(528000000) * 18U); assert((uint64_t)(out + 1U) * n > UINT64_C(528000000) * 18U); } return 0; }solution: files/checks.h
learner@ksa:~/gl-e02/lab-m01$ bat files/checks.h File: files/checks.h #ifndef GL_E02_CHECKS_H #define GL_E02_CHECKS_H int m01_l01_check(void); int m01_l02_check(void); int m01_l03_check(void); int boundary_check(void); #endifsolution: files/lab.c
learner@ksa:~/gl-e02/lab-m01$ bat files/lab.c File: files/lab.c #define _POSIX_C_SOURCE 200809L #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include "checks.h" static int cmp_double(const void *a, const void *b) { double da = *(const double *)a; double db = *(const double *)b; return (da > db) - (da < db); } static int run_benchmark(void) { enum { WARMUP = 5, SAMPLES = 35, LOOPS = 100 }; double us[SAMPLES]; fflush(stdout); int saved_out = dup(STDOUT_FILENO); int devnull = open("/dev/null", O_WRONLY); if (saved_out < 0 || devnull < 0) return EXIT_FAILURE; if (dup2(devnull, STDOUT_FILENO) < 0) return EXIT_FAILURE; close(devnull); int rc = 0; for (int s = -WARMUP; s < SAMPLES; ++s) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int k = 0; k < LOOPS; ++k) { if (m01_l01_check() != 0) rc = 1; if (m01_l02_check() != 0) rc = 1; if (m01_l03_check() != 0) rc = 1; if (boundary_check() != 0) rc = 1; } clock_gettime(CLOCK_MONOTONIC, &t1); fflush(stdout); if (s >= 0) { double sec = (double)(t1.tv_sec - t0.tv_sec); double nsec = (double)(t1.tv_nsec - t0.tv_nsec); us[s] = sec * 1e6 + nsec * 1e-3; } } fflush(stdout); if (dup2(saved_out, STDOUT_FILENO) < 0) return EXIT_FAILURE; close(saved_out); if (rc != 0) return EXIT_FAILURE; qsort(us, SAMPLES, sizeof(us[0]), cmp_double); printf("bench_samples=%d warmup=%d loops_per_sample=%d p50_us=%.2f p95_us=%.2f p99_us=%.2f\n", SAMPLES, WARMUP, LOOPS, us[17], us[33], us[34]); return EXIT_SUCCESS; } int main(int argc, char **argv) { if (argc != 2) return EXIT_FAILURE; if (strcmp(argv[1], "--bench") == 0) return run_benchmark(); if (strcmp(argv[1], "1") == 0) return m01_l01_check(); if (strcmp(argv[1], "2") == 0) return m01_l02_check(); if (strcmp(argv[1], "3") == 0) return m01_l03_check(); if (strcmp(argv[1], "4") == 0) return boundary_check(); return EXIT_FAILURE; }solution: files/m01_l01.c
learner@ksa:~/gl-e02/lab-m01$ bat files/m01_l01.c File: files/m01_l01.c #include <assert.h> #include <stdint.h> #include <stdio.h> struct clock_path { int step, locked; uint32_t select, podf; }; static int retune(struct clock_path *c, uint32_t select) { if (!c->step || select < 54U || select > 108U) return -1; c->locked = 0; c->select = select; return 0; } static int reconnect(struct clock_path *c) { if (!c->locked) return -1; c->step = 0; return 0; } int main(void) { struct clock_path c = {0, 1, 66U, 1U}; assert(retune(&c, 88U) == -1); c.step = 1; assert(retune(&c, 88U) == 0); assert(reconnect(&c) == -1 && c.step); c.locked = 1; assert(reconnect(&c) == 0); uint64_t pll = UINT64_C(24000000) * c.select / 2U; uint64_t arm = pll / (c.podf + 1U); assert(pll == UINT64_C(1056000000)); assert(arm == UINT64_C(528000000)); puts("PLL1=1056000000 ARM=528000000 safe_transition=pass"); return 0; }solution: files/m01_l02.c
learner@ksa:~/gl-e02/lab-m01$ bat files/m01_l02.c File: files/m01_l02.c #include <assert.h> #include <stdint.h> #include <stdio.h> static uint32_t set_frac(uint32_t reg, unsigned lane, uint32_t frac) { assert(lane < 4U && frac >= 12U && frac <= 35U); unsigned shift = lane * 8U; return (reg & ~(UINT32_C(0x3f) << shift)) | (frac << shift); } static uint32_t rate(uint32_t parent, uint32_t frac) { assert(frac >= 12U && frac <= 35U); return (uint32_t)((uint64_t)parent * 18U / frac); } int main(void) { uint32_t old = UINT32_C(0x605b585b); uint32_t next = set_frac(old, 2U, 24U); uint32_t mask = UINT32_C(0x3f) << 16; assert((old & ~mask) == (next & ~mask)); assert(((next >> 16) & 63U) == 24U); assert(rate(528000000U, 24U) == 396000000U); assert(rate(480000000U, 20U) == 432000000U); uint32_t q = rate(528000000U, 29U); assert((uint64_t)q * 29U <= UINT64_C(9504000000)); assert((uint64_t)(q+1U) * 29U > UINT64_C(9504000000)); puts("PLL2_PFD2=396000000 PLL3_PFD=432000000 neighbors=preserved"); return 0; }solution: files/m01_l03.c
learner@ksa:~/gl-e02/lab-m01$ bat files/m01_l03.c File: files/m01_l03.c #include <assert.h> #include <stdint.h> #include <stdio.h> static int wait_busy(unsigned clear_after, unsigned budget) { for (unsigned n = 0U; n < budget; ++n) if (n >= clear_after) return 0; return -1; } int main(void) { uint32_t ahb_field = 3U, ipg_field = 1U, per_field = 23U; uint32_t ahb = 528000000U / (ahb_field + 1U); uint32_t ipg = ahb / (ipg_field + 1U); uint32_t per = 24000000U / (per_field + 1U); assert(ahb == 132000000U && ipg == 66000000U); assert(per == 1000000U); assert(528000000U / (0U + 1U) == 528000000U); assert(wait_busy(0U, 4U) == 0); assert(wait_busy(3U, 4U) == 0); assert(wait_busy(4U, 4U) == -1); assert(wait_busy(0U, 0U) == -1); puts("AHB=132000000 IPG=66000000 PERCLK=1000000 timeout=detected"); return 0; }Run the check
From labs/lab_m01_ccm_pll_tree/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e02/lab-m01./check.shTAP version 131..5ok 1 - Strict C11 build from sourceok 2 - PLL1 safe-source transition and output rateok 3 - PFD lane isolation and exact frequencyok 4 - AHB IPG PERCLK encoded dividersok 5 - PFD extremes and rejected zero divisor# 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-e02/lab-m01./check.shTAP version 131..5ok 1 - Strict C11 build from sourcenot ok 2 - PLL1 safe-source transition and output rate --- diagnostic: "Guard reconnect() on c->locked != 0 so CCM_CCSR stays on 24 MHz step_clk until PLL1 locks" ...ok 3 - PFD lane isolation and exact frequencyok 4 - AHB IPG PERCLK encoded dividersnot ok 5 - PFD extremes and rejected zero divisor --- diagnostic: "Reject PFD_FRAC outside [12, 35] and use UINT64_C(528000000) * 18U to prevent 32-bit wrap" ...# pass 3/5FAIL: lab_m01_ccm_pll_tree (3/5 passed; first failure -> check 2 (PLL1 safe-source transition and output rate): Guard reconnect() on c->locked != 0 so CCM_CCSR stays on 24 MHz step_clk until PLL1 locks)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-e02/lab-m01$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" src_dir="${1:-files}" if [ ! -d "$src_dir" ]; then printf "TAP version 13\n1..1\nnot ok 1 - missing directory %s\nFAIL: %s missing source directory %s\n" "$src_dir" "lab_m01_ccm_pll_tree" "$src_dir" exit 1 fi work=$(mktemp -d "$PWD/.check.XXXXXX") || exit 1 trap 'rm -rf "$work"' EXIT HUP INT TERM cp -R "$src_dir" "$work/build" printf 'TAP version 13\n1..5\n' pass=0 first_fail="" report() { if [ "$1" -eq 0 ]; then printf 'ok %s - %s\n' "$2" "$3" pass=$((pass + 1)) else printf 'not ok %s - %s\n' "$2" "$3" printf ' ---\n diagnostic: "%s"\n ...\n' "$4" if [ -z "$first_fail" ]; then first_fail="check $2 ($3): $4" fi fi } make -B -C "$work/build" >"$work/build.log" 2>&1 report "$?" 1 "Strict C11 build from source" "Ensure all translation units compile cleanly under gcc -std=c11 -Wall -Wextra -Werror" "$work/build/lab" 1 >"$work/case1.log" 2>&1 report "$?" 2 "PLL1 safe-source transition and output rate" "Guard reconnect() on c->locked != 0 so CCM_CCSR stays on 24 MHz step_clk until PLL1 locks" "$work/build/lab" 2 >"$work/case2.log" 2>&1 report "$?" 3 "PFD lane isolation and exact frequency" "Mask only 0x3fU << shift in set_frac() and promote parent to uint64_t before * 18U" "$work/build/lab" 3 >"$work/case3.log" 2>&1 report "$?" 4 "AHB IPG PERCLK encoded dividers" "Decode AHB_PODF, IPG_PODF, and PERCLK_PODF as (field + 1U) with strict < budget busy polling" "$work/build/lab" 4 >"$work/case4.log" 2>&1 report "$?" 5 "PFD extremes and rejected zero divisor" "Reject PFD_FRAC outside [12, 35] and use UINT64_C(528000000) * 18U to prevent 32-bit wrap" printf "# pass %s/5\n" "$pass" if [ "$pass" -ne 5 ]; then printf "FAIL: lab_m01_ccm_pll_tree (%s/5 passed; first failure -> %s)\n" "$pass" "$first_fail" exit 1 fi exit 0Definition 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_m01_ccm_pll_tree/ so relative paths to files/ resolve directly.