Lab 1: SAI2 Clock Divider Solver, 7x9 WM8960 Regmap Packer & I2S 1-BCLK Frame Shifter
Derive synchronous I2S clock ratios and i.MX6ULL SAI2 FIFO DMA watermarks from sampling physics, trace the Linux 6.12 ASoC Platform/Codec/Machine split with 7x9-bit WM8960 regmap packing, and bind Device Tree audio routing to verified ALSA PCM character devices.
Prerequisites in this module
What ./check.sh verifies
- Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
- Fix compiler warnings or errors in files/core.c.
- Archives core.o into libcore.a and links independent TAP verifier.
- Preserve public declarations in files/core.h so tests.c links.
step 1/15 · inspect starter files and specification
Silent audio cards, +6 dB bit-slipped clipping, and dropped WM8960 Volume-Update strobes almost always originate from three arithmetic errors: miscomputing SAI2 TCR2[DIV] from MCLK and BCLK, dropping bit 8 when packing 7x9-bit WM8960 I2C bytes, or omitting the 1-BCLK Philips I2S MSB guard shift.
In this studio lab you will audit and repair an ISO C11 audio-bringup engine (files/core.c) that computes exact SAI2 clock/FIFO plans, packs/unpacks WM8960 7x9 I2C frames, and shifts 64-bit stereo I2S bus frames with CLOCK_MONOTONIC throughput benchmarking.
learner@ksa:~/gl-e07/lab-m01ls -lastarter/Makefilestarter/cli.cstarter/core.cstarter/core.hlearner@ksa:~/gl-e07/lab-m01starter: starter/Makefile
learner@ksa:~/gl-e07/lab-m01$ bat starter/Makefile File: starter/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 -D_POSIX_C_SOURCE=200809L LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clistarter: starter/cli.c
learner@ksa:~/gl-e07/lab-m01$ bat starter/cli.c File: starter/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static uint64_t mono_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; } static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return (x > y) - (x < y); } static int run_bench(void) { uint64_t samples[40]; volatile uint64_t sink = 0; for (int i = 0; i < 45; i++) { uint64_t t0 = mono_ns(); sai2_clk_plan_t plan; uint8_t i2c[2]; for (int k = 0; k < 500; k++) { sai2_solve_clocks(12288000u, 48000u, 32u, &plan); wm8960_pack_reg(0x17u, 0x01C4u, i2c); uint64_t w = i2s_pack_stereo32(23170 + k, -23170 - k, 24u, true); int32_t l = 0, r = 0; i2s_unpack_stereo32(w, 24u, true, &l, &r); sink ^= (uint64_t)(uint32_t)(l ^ r) ^ plan.tcr2_div ^ i2c[0]; } uint64_t dt = mono_ns() - t0; if (i >= 5) samples[i - 5] = dt; } qsort(samples, 40, sizeof(uint64_t), cmp_u64); printf("BENCH lab_m01_i2s_sai_clock_solver (500 ops/batch, 40 batches): p50=%" PRIu64 " ns p95=%" PRIu64 " ns p99=%" PRIu64 " ns (sink=%" PRIu64 ")\n", samples[20], samples[38], samples[39], sink & 0xFFu); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) { return run_bench(); } sai2_clk_plan_t p48 = {0}, p44 = {0}; if (sai2_solve_clocks(12288000u, 48000u, 32u, &p48) != 0 || sai2_solve_clocks(11289600u, 44100u, 16u, &p44) != 0) { return 1; } uint8_t b[2] = {0}; uint8_t ureg = 0; uint16_t uval = 0; wm8960_pack_reg(0x17u, 0x01C4u, b); wm8960_unpack_reg(b, &ureg, &uval); uint64_t wire = i2s_pack_stereo32(2100000, -2100000, 24u, true); int32_t l_ok = 0, r_ok = 0, l_slip = 0, r_slip = 0; i2s_unpack_stereo32(wire, 24u, true, &l_ok, &r_ok); i2s_unpack_stereo32(wire, 24u, false, &l_slip, &r_slip); printf("SAI2 48k/32b: BCLK=%u Hz TCR2.DIV=%u FIFO_ns=%u | 44.1k/16b: BCLK=%u Hz DIV=%u\n", p48.bclk_hz, p48.tcr2_div, p48.fifo_underrun_ns, p44.bclk_hz, p44.tcr2_div); printf("WM8960 R23=0x01C4 -> I2C [0x%02X, 0x%02X] -> unpack (0x%02X, 0x%03X)\n", b[0], b[1], ureg, uval); printf("I2S 24b: TX=(%d, %d) -> RX_1BCLK=(%d, %d) | RX_0BCLK_SLIP=(%d, %d)\n", 2100000, -2100000, l_ok, r_ok, l_slip, r_slip); return 0; }starter: starter/core.c
learner@ksa:~/gl-e07/lab-m01$ bat starter/core.c File: starter/core.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <errno.h> int sai2_solve_clocks(uint32_t mclk_hz, uint32_t rate_hz, uint8_t slot_bits, sai2_clk_plan_t *out) { if (!out || rate_hz == 0u || (slot_bits != 16u && slot_bits != 24u && slot_bits != 32u)) { return -EINVAL; } uint32_t osr = mclk_hz / rate_hz; if (mclk_hz % rate_hz != 0u || (osr != 256u && osr != 384u)) { return -EDOM; } uint32_t bclk_hz = 2u * rate_hz * (uint32_t)slot_bits; if (mclk_hz % bclk_hz != 0u) { return -ERANGE; } uint32_t ratio = mclk_hz / bclk_hz; if (ratio < 2u || (ratio % 2u) != 0u || ((ratio / 2u) - 1u) > 0xFFu) { return -ERANGE; } out->mclk_hz = mclk_hz; out->rate_hz = rate_hz; out->slot_bits = slot_bits; out->bclk_hz = bclk_hz; out->bclk_ratio = ratio; out->tcr2_div = (uint8_t)ratio; /* BUG: omitted /2 - 1 TCR2 encoding */ /* 32-word TX FIFO holds 16 stereo frames */ out->fifo_underrun_ns = (uint32_t)((16ULL * 1000000000ULL) / (uint64_t)rate_hz); return 0; } int wm8960_pack_reg(uint8_t reg, uint16_t val, uint8_t i2c_bytes[2]) { if (!i2c_bytes || reg > 0x37u || val > 0x01FFu) { return -EINVAL; } i2c_bytes[0] = (uint8_t)(reg << 1); /* BUG: dropped 9th payload bit B8 */ i2c_bytes[1] = (uint8_t)(val & 0xFFu); return 0; } int wm8960_unpack_reg(const uint8_t i2c_bytes[2], uint8_t *reg_out, uint16_t *val_out) { if (!i2c_bytes || !reg_out || !val_out) { return -EINVAL; } uint8_t r = (uint8_t)(i2c_bytes[0] >> 1); if (r > 0x37u) { return -ERANGE; } *reg_out = r; *val_out = (uint16_t)((((uint16_t)(i2c_bytes[0] & 0x01u)) << 8) | i2c_bytes[1]); return 0; } static uint32_t pack_slot32(int32_t sample, uint8_t valid_bits, bool delay_1bclk) { uint32_t mask = (valid_bits == 32u) ? 0xFFFFFFFFu : ((1u << valid_bits) - 1u); uint32_t u = ((uint32_t)sample) & mask; uint32_t left_aligned = u << (32u - valid_bits); (void)delay_1bclk; return left_aligned; /* BUG: emitted 0-BCLK Left-Justified frame */ } static int32_t unpack_slot32(uint32_t slot, uint8_t valid_bits, bool delay_1bclk) { uint32_t left_aligned = delay_1bclk ? (slot << 1) : slot; uint32_t raw = left_aligned >> (32u - valid_bits); if (valid_bits < 32u) { uint32_t sign_bit = 1u << (valid_bits - 1u); if (raw & sign_bit) { raw |= ~((1u << valid_bits) - 1u); } } return (int32_t)raw; } uint64_t i2s_pack_stereo32(int32_t left, int32_t right, uint8_t valid_bits, bool philips_1bclk_delay) { if (valid_bits != 16u && valid_bits != 24u) { valid_bits = 16u; } uint32_t l_slot = pack_slot32(left, valid_bits, philips_1bclk_delay); uint32_t r_slot = pack_slot32(right, valid_bits, philips_1bclk_delay); return ((uint64_t)l_slot << 32) | (uint64_t)r_slot; } void i2s_unpack_stereo32(uint64_t wire, uint8_t valid_bits, bool philips_1bclk_delay, int32_t *left_out, int32_t *right_out) { if (valid_bits != 16u && valid_bits != 24u) { valid_bits = 16u; } if (left_out) { *left_out = unpack_slot32((uint32_t)(wire >> 32), valid_bits, philips_1bclk_delay); } if (right_out) { *right_out = unpack_slot32((uint32_t)(wire & 0xFFFFFFFFu), valid_bits, philips_1bclk_delay); } }starter: starter/core.h
learner@ksa:~/gl-e07/lab-m01$ bat starter/core.h File: starter/core.h #ifndef CORE_H #define CORE_H #include <stdbool.h> #include <stddef.h> #include <stdint.h> typedef struct { uint32_t mclk_hz; uint32_t rate_hz; uint8_t slot_bits; uint32_t bclk_hz; uint32_t bclk_ratio; uint8_t tcr2_div; uint32_t fifo_underrun_ns; } sai2_clk_plan_t; int sai2_solve_clocks(uint32_t mclk_hz, uint32_t rate_hz, uint8_t slot_bits, sai2_clk_plan_t *out); int wm8960_pack_reg(uint8_t reg, uint16_t val, uint8_t i2c_bytes[2]); int wm8960_unpack_reg(const uint8_t i2c_bytes[2], uint8_t *reg_out, uint16_t *val_out); uint64_t i2s_pack_stereo32(int32_t left, int32_t right, uint8_t valid_bits, bool philips_1bclk_delay); void i2s_unpack_stereo32(uint64_t wire, uint8_t valid_bits, bool philips_1bclk_delay, int32_t *left_out, int32_t *right_out); #endifstep 2/15 · 2. learning objectives
- Implement exact
SAI2MCLK/BCLK/TCR2.DIVvalidation and32-word FIFO drain nanosecond budgeting. - Pack and unpack
7-bit register (0x00..0x37) and9-bit value (0x000..0x1FF) words into two8-bitI2Coctets. - Shift signed
16/24-bit stereo PCM samples across64-bit PhilipsI2S(1-BCLKdelay) versus Left-Justified (0-BCLK) frames.
step 3/15 · 3. environment requirements
All automated verification steps run in unprivileged user space on a POSIX Linux host using gcc -std=c11 -Wall -Wextra -Werror and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).
step 4/15 · 4. required software and hardware
- Host workstation:
gcc(-std=c11 -Wall -Wextra -Werror), GNUmake,ar(binutils), and POSIX/bin/sh. - Target environment (optional hardware step, Section 13):
i.MX6ULL(ARMv7-ACortex-A7) board withWM8960onI2C2 @ 0x1aandSAI2(0x0202C000), runningLinux kernel6.12 LTS (cat /proc/asound/cards,/sys/kernel/debug/regmap/1-001a/registers).
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-e07/lab-m01$ bat script.sh File: script.sh make -B -C files ./files/lab_cli ./files/lab_cli --bench sh check.shstep 6/15 · 6. annotated source code
files/core.c enforces the production invariants that starter/core.c breaks:
learner@ksa:~/gl-e07/lab-m01$ bat example.c File: example.c int sai2_solve_clocks(uint32_t mclk_hz, uint32_t rate_hz, uint8_t slot_bits, sai2_clk_plan_t *out) { uint32_t ratio = mclk_hz / (2u * rate_hz * (uint32_t)slot_bits); out->tcr2_div = (uint8_t)((ratio / 2u) - 1u); /* Hardware divides by (DIV + 1) * 2 */ out->fifo_underrun_ns = (uint32_t)((16ULL * 1000000000ULL) / (uint64_t)rate_hz); return 0; } int wm8960_pack_reg(uint8_t reg, uint16_t val, uint8_t i2c_bytes[2]) { i2c_bytes[0] = (uint8_t)((reg << 1) | ((val >> 8) & 0x01u)); /* [15:9] | B8 */ i2c_bytes[1] = (uint8_t)(val & 0xFFu); /* [7:0] */ return 0; }starter/core.c contains three realistic bring-up bugs: (1) writing raw ratio into TCR2[DIV] instead of (ratio / 2) - 1 (halving BCLK plus an off-by-one error), (2) dropping bit 8 of 9-bit WM8960 values in i2c_bytes[0], and (3) ignoring delay_1bclk in pack_slot32, emitting 0-BCLK Left-Justified frames.
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-e07/lab-m01$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror ok 2 - archives core.o into libcore.a and links independent TAP verifier ok 3 - computes SAI2 TCR2.DIV=(ratio/2)-1, FIFO drain ns, and rejects incoherent MCLK ok 4 - packs and unpacks 7-bit register plus 9-bit payload across two WM8960 I2C bytes ok 5 - shifts 16-bit and 24-bit stereo frames by 1 BCLK for Philips I2S with sign extension # pass 5/5CLI verification output (./files/lab_cli):
learner@ksa:~/gl-e07/lab-m01$ bat listing.txt File: listing.txt SAI2 48k/32b: BCLK=3072000 Hz TCR2.DIV=1 FIFO_ns=333333 | 44.1k/16b: BCLK=1411200 Hz DIV=3 WM8960 R23=0x01C4 -> I2C [0x2F, 0xC4] -> unpack (0x17, 0x1C4) I2S 24b: TX=(2100000, -2100000) -> RX_1BCLK=(2100000, -2100000) | RX_0BCLK_SLIP=(1050000, 7338608)Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.
step 8/15 · 8. explanation of observed behavior
All five TAP version 13 checks verify that 12.288 MHz divides cleanly to 3.072 MHz (TCR2.DIV = 1) with a 333,333 ns 16-frame FIFO horizon, that incoherent 12.288 MHz / 44.1 kHz combinations return -EDOM, that R23 = 0x01C4 encodes to [0x2F, 0xC4], and that 16/24-bit signed two's-complement samples round-trip across 1-BCLK delayed 64-bit frames without sign-bit corruption.
step 9/15 · 9. experiment variations
- Near transfer: Add
384 * fs(18.432 MHzat48 kHz,24-bit slot ->BCLK = 2.304 MHz,ratio = 8,TCR2.DIV = 3) verification cases tocli.cand confirmmclk_hz % bclk_hz == 0. - Far transfer: Extend
i2s_pack_stereo32into a4-slot TDM Mode A packer (uint32_t slots[4],1-BCLKframe sync pulse) supporting quad-microphone array capture onSAI2.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):
learner@ksa:~/gl-e07/lab-m01$ bat listing.txt File: listing.txt BENCH lab_m01_i2s_sai_clock_solver (500 ops/batch, 40 batches): p50=12791 ns p95=17141 ns p99=20363 ns (sink=244)The CLOCK_MONOTONIC percentiles above measure 500 combined clock-solve, 7x9 regmap-pack, and 64-bit I2S shift operations in host user-space RAM. Never extrapolate these nanoseconds to physical i.MX6ULL I2C2 transaction time (~200 us at 100 kHz for 3 bytes) or SDMA bus arbitration latency.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix | |
|---|---|---|---|
not ok 3 (expected DIV=1, got DIV=4) | SAI2 TCR2[DIV] hardware divides MCLK by (DIV + 1) * 2, not DIV directly | Compute out->tcr2_div = (uint8_t)((ratio / 2u) - 1u) after checking ratio >= 2 and ratio % 2 == 0. | |
not ok 4 (expected [0x2F, 0xC4], got [0x2E, 0xC4]) | Bit 8 of the 9-bit WM8960 register value (0x01C4 >> 8 == 1) was not ORed into i2c_bytes[0] LSB | Use `(uint8_t)((reg << 1) | ((val >> 8) & 0x01u))`. |
not ok 5 (expected 1-BCLK shifted slot) | pack_slot32 left MSB at bit 31 (0-BCLK Left-Justified) instead of shifting right by 1 BCLK | Return delay_1bclk ? (left_aligned >> 1) : left_aligned. |
step 12/15 · 12. guided questions
- Why does
sai2_solve_clocksreject(mclk=12288000, rate=48000, slot_bits=24)whenratiomust be an even integer?
- *Hint 1:* Compute
bclk_hz = 2 * 48000 * 24 = 2,304,000 Hz, then divide12,288,000 / 2,304,000. - *Hint 2:*
12,288,000 / 2,304,000 = 5.333...(non-integer!); packing24-bit samples inside32-bit slots yields3.072 MHz(ratio = 4), or usingMCLK = 384 * fs = 18.432 MHzyields18.432M / 2.304M = 8(TCR2.DIV = 3)!
- What happens on real
WM8960hardware ifwm8960_pack_regdrops bit8(VU, Volume Update) when writingR2(LOUT1volume)?
- *Hint 1:* Check the
WM8960datasheet behavior for stereo volume registersR2/R3andR40/R41whenVU = 0. - *Hint 2:* Without
VU = 1(bit 8), the new7-bit attenuation latches into a holding buffer but never transfers to the active analog PGA gain stage, leaving headphone volume stuck at mute!
step 13/15 · 13. independent challenges
On an attached i.MX6ULL board (or using captured /sys/kernel/debug/regmap/1-001a/registers dumps), write a small C11 tool that parses XX: XXX hex lines from debugfs and emits exact I2C2 byte sequences for replaying the codec configuration in a bare-metal U-Boot diagnostic command.
step 14/15 · 14. solutions and explanations
Restoring (ratio / 2u) - 1u in sai2_solve_clocks, ORing ((val >> 8) & 0x01u) into i2c_bytes[0] in wm8960_pack_reg, and shifting left_aligned >> 1 when delay_1bclk is true brings all 5/5 TAP checks green.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e07/lab-m01./files/lab_clilearner@ksa:~/gl-e07/lab-m01SAI2 48k/32b: BCLK=3072000 Hz TCR2.DIV=1 FIFO_ns=333333 | 44.1k/16b: BCLK=1411200 Hz DIV=3WM8960 R23=0x01C4 -> I2C [0x2F, 0xC4] -> unpack (0x17, 0x1C4)I2S 24b: TX=(2100000, -2100000) -> RX_1BCLK=(2100000, -2100000) | RX_0BCLK_SLIP=(1050000, 7338608)Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror.
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-e07/lab-m01$ bat files/Makefile File: files/Makefile CC_BIN = gcc AR = ar CFLAGS_ALL = -std=c11 -Wall -Wextra -Werror -O2 -D_POSIX_C_SOURCE=200809L LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.c core.h $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clisolution: files/cli.c
learner@ksa:~/gl-e07/lab-m01$ bat files/cli.c File: files/cli.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static uint64_t mono_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; } static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return (x > y) - (x < y); } static int run_bench(void) { uint64_t samples[40]; volatile uint64_t sink = 0; for (int i = 0; i < 45; i++) { uint64_t t0 = mono_ns(); sai2_clk_plan_t plan; uint8_t i2c[2]; for (int k = 0; k < 500; k++) { sai2_solve_clocks(12288000u, 48000u, 32u, &plan); wm8960_pack_reg(0x17u, 0x01C4u, i2c); uint64_t w = i2s_pack_stereo32(23170 + k, -23170 - k, 24u, true); int32_t l = 0, r = 0; i2s_unpack_stereo32(w, 24u, true, &l, &r); sink ^= (uint64_t)(uint32_t)(l ^ r) ^ plan.tcr2_div ^ i2c[0]; } uint64_t dt = mono_ns() - t0; if (i >= 5) samples[i - 5] = dt; } qsort(samples, 40, sizeof(uint64_t), cmp_u64); printf("BENCH lab_m01_i2s_sai_clock_solver (500 ops/batch, 40 batches): p50=%" PRIu64 " ns p95=%" PRIu64 " ns p99=%" PRIu64 " ns (sink=%" PRIu64 ")\n", samples[20], samples[38], samples[39], sink & 0xFFu); return 0; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) { return run_bench(); } sai2_clk_plan_t p48 = {0}, p44 = {0}; if (sai2_solve_clocks(12288000u, 48000u, 32u, &p48) != 0 || sai2_solve_clocks(11289600u, 44100u, 16u, &p44) != 0) { return 1; } uint8_t b[2] = {0}; uint8_t ureg = 0; uint16_t uval = 0; wm8960_pack_reg(0x17u, 0x01C4u, b); wm8960_unpack_reg(b, &ureg, &uval); uint64_t wire = i2s_pack_stereo32(2100000, -2100000, 24u, true); int32_t l_ok = 0, r_ok = 0, l_slip = 0, r_slip = 0; i2s_unpack_stereo32(wire, 24u, true, &l_ok, &r_ok); i2s_unpack_stereo32(wire, 24u, false, &l_slip, &r_slip); printf("SAI2 48k/32b: BCLK=%u Hz TCR2.DIV=%u FIFO_ns=%u | 44.1k/16b: BCLK=%u Hz DIV=%u\n", p48.bclk_hz, p48.tcr2_div, p48.fifo_underrun_ns, p44.bclk_hz, p44.tcr2_div); printf("WM8960 R23=0x01C4 -> I2C [0x%02X, 0x%02X] -> unpack (0x%02X, 0x%03X)\n", b[0], b[1], ureg, uval); printf("I2S 24b: TX=(%d, %d) -> RX_1BCLK=(%d, %d) | RX_0BCLK_SLIP=(%d, %d)\n", 2100000, -2100000, l_ok, r_ok, l_slip, r_slip); return 0; }solution: files/core.c
learner@ksa:~/gl-e07/lab-m01$ bat files/core.c File: files/core.c #define _POSIX_C_SOURCE 200809L #include "core.h" #include <errno.h> int sai2_solve_clocks(uint32_t mclk_hz, uint32_t rate_hz, uint8_t slot_bits, sai2_clk_plan_t *out) { if (!out || rate_hz == 0u || (slot_bits != 16u && slot_bits != 24u && slot_bits != 32u)) { return -EINVAL; } uint32_t osr = mclk_hz / rate_hz; if (mclk_hz % rate_hz != 0u || (osr != 256u && osr != 384u)) { return -EDOM; } uint32_t bclk_hz = 2u * rate_hz * (uint32_t)slot_bits; if (mclk_hz % bclk_hz != 0u) { return -ERANGE; } uint32_t ratio = mclk_hz / bclk_hz; if (ratio < 2u || (ratio % 2u) != 0u || ((ratio / 2u) - 1u) > 0xFFu) { return -ERANGE; } out->mclk_hz = mclk_hz; out->rate_hz = rate_hz; out->slot_bits = slot_bits; out->bclk_hz = bclk_hz; out->bclk_ratio = ratio; out->tcr2_div = (uint8_t)((ratio / 2u) - 1u); /* 32-word TX FIFO holds 16 stereo frames */ out->fifo_underrun_ns = (uint32_t)((16ULL * 1000000000ULL) / (uint64_t)rate_hz); return 0; } int wm8960_pack_reg(uint8_t reg, uint16_t val, uint8_t i2c_bytes[2]) { if (!i2c_bytes || reg > 0x37u || val > 0x01FFu) { return -EINVAL; } i2c_bytes[0] = (uint8_t)((reg << 1) | ((val >> 8) & 0x01u)); i2c_bytes[1] = (uint8_t)(val & 0xFFu); return 0; } int wm8960_unpack_reg(const uint8_t i2c_bytes[2], uint8_t *reg_out, uint16_t *val_out) { if (!i2c_bytes || !reg_out || !val_out) { return -EINVAL; } uint8_t r = (uint8_t)(i2c_bytes[0] >> 1); if (r > 0x37u) { return -ERANGE; } *reg_out = r; *val_out = (uint16_t)((((uint16_t)(i2c_bytes[0] & 0x01u)) << 8) | i2c_bytes[1]); return 0; } static uint32_t pack_slot32(int32_t sample, uint8_t valid_bits, bool delay_1bclk) { uint32_t mask = (valid_bits == 32u) ? 0xFFFFFFFFu : ((1u << valid_bits) - 1u); uint32_t u = ((uint32_t)sample) & mask; uint32_t left_aligned = u << (32u - valid_bits); return delay_1bclk ? (left_aligned >> 1) : left_aligned; } static int32_t unpack_slot32(uint32_t slot, uint8_t valid_bits, bool delay_1bclk) { uint32_t left_aligned = delay_1bclk ? (slot << 1) : slot; uint32_t raw = left_aligned >> (32u - valid_bits); if (valid_bits < 32u) { uint32_t sign_bit = 1u << (valid_bits - 1u); if (raw & sign_bit) { raw |= ~((1u << valid_bits) - 1u); } } return (int32_t)raw; } uint64_t i2s_pack_stereo32(int32_t left, int32_t right, uint8_t valid_bits, bool philips_1bclk_delay) { if (valid_bits != 16u && valid_bits != 24u) { valid_bits = 16u; } uint32_t l_slot = pack_slot32(left, valid_bits, philips_1bclk_delay); uint32_t r_slot = pack_slot32(right, valid_bits, philips_1bclk_delay); return ((uint64_t)l_slot << 32) | (uint64_t)r_slot; } void i2s_unpack_stereo32(uint64_t wire, uint8_t valid_bits, bool philips_1bclk_delay, int32_t *left_out, int32_t *right_out) { if (valid_bits != 16u && valid_bits != 24u) { valid_bits = 16u; } if (left_out) { *left_out = unpack_slot32((uint32_t)(wire >> 32), valid_bits, philips_1bclk_delay); } if (right_out) { *right_out = unpack_slot32((uint32_t)(wire & 0xFFFFFFFFu), valid_bits, philips_1bclk_delay); } }solution: files/core.h
learner@ksa:~/gl-e07/lab-m01$ bat files/core.h File: files/core.h #ifndef CORE_H #define CORE_H #include <stdbool.h> #include <stddef.h> #include <stdint.h> typedef struct { uint32_t mclk_hz; uint32_t rate_hz; uint8_t slot_bits; uint32_t bclk_hz; uint32_t bclk_ratio; uint8_t tcr2_div; uint32_t fifo_underrun_ns; } sai2_clk_plan_t; int sai2_solve_clocks(uint32_t mclk_hz, uint32_t rate_hz, uint8_t slot_bits, sai2_clk_plan_t *out); int wm8960_pack_reg(uint8_t reg, uint16_t val, uint8_t i2c_bytes[2]); int wm8960_unpack_reg(const uint8_t i2c_bytes[2], uint8_t *reg_out, uint16_t *val_out); uint64_t i2s_pack_stereo32(int32_t left, int32_t right, uint8_t valid_bits, bool philips_1bclk_delay); void i2s_unpack_stereo32(uint64_t wire, uint8_t valid_bits, bool philips_1bclk_delay, int32_t *left_out, int32_t *right_out); #endifRun the check
From labs/lab_m01_i2s_sai_clock_solver/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e07/lab-m01./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - computes SAI2 TCR2.DIV=(ratio/2)-1, FIFO drain ns, and rejects incoherent MCLKok 4 - packs and unpacks 7-bit register plus 9-bit payload across two WM8960 I2C bytesok 5 - shifts 16-bit and 24-bit stereo frames by 1 BCLK for Philips I2S with sign extension# 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-e07/lab-m01./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werrorok 2 - archives core.o into libcore.a and links independent TAP verifiernot ok 3 - computes SAI2 TCR2.DIV=(ratio/2)-1, FIFO drain ns, and rejects incoherent MCLKFAIL: compute out->tcr2_div = (ratio / 2) - 1 and reject non-256x/384x MCLK ratios in sai2_solve_clocks --- message: 'got expected DIV=1 FIFO=333333, got DIV=4 FIFO=333333' hint: 'compute out->tcr2_div = (ratio / 2) - 1 and reject non-256x/384x MCLK ratios in sai2_solve_clocks' ...not ok 4 - packs and unpacks 7-bit register plus 9-bit payload across two WM8960 I2C bytesFAIL: OR ((val >> 8) & 1) into i2c_bytes[0] bit 0 in wm8960_pack_reg and enforce reg<=0x37, val<=0x1FF --- message: 'got expected [0x2F, 0xC4], got [0x2E, 0xC4]' hint: 'OR ((val >> 8) & 1) into i2c_bytes[0] bit 0 in wm8960_pack_reg and enforce reg<=0x37, val<=0x1FF' ...not ok 5 - shifts 16-bit and 24-bit stereo frames by 1 BCLK for Philips I2S with sign extensionFAIL: shift left_aligned right by 1 bit when delay_1bclk is true in pack_slot32 --- message: 'got expected 1-BCLK shifted slot 0x2D410000, got 0x5A820000' hint: 'shift left_aligned right by 1 bit when delay_1bclk is true in pack_slot32' ...# pass 2/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-e07/lab-m01$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(dirname "$0")" || exit 1 PASS=0 WORK=$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM cp -R files "$WORK/files" printf 'TAP version 13\n1..5\n' if make -B -C "$WORK/files" >"$WORK/build.log" 2>&1; then printf 'ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' PASS=$((PASS + 1)) else printf 'not ok 1 - builds libcore.a and lab_cli cleanly with -std=c11 -Wall -Wextra -Werror\n' printf 'FAIL: fix compiler warnings or errors in files/core.c\n' fi if [ -f "$WORK/files/libcore.a" ] && gcc -std=c11 -Wall -Wextra -Werror -O2 -D_POSIX_C_SOURCE=200809L -I"$WORK/files" tests.c "$WORK/files/libcore.a" -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then printf 'ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' PASS=$((PASS + 1)) else printf 'not ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' printf 'FAIL: preserve public declarations in files/core.h so tests.c links\n' fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 1 2>&1); then printf 'ok 3 - computes SAI2 TCR2.DIV=(ratio/2)-1, FIFO drain ns, and rejects incoherent MCLK\n' PASS=$((PASS + 1)) else printf 'not ok 3 - computes SAI2 TCR2.DIV=(ratio/2)-1, FIFO drain ns, and rejects incoherent MCLK\n' printf 'FAIL: compute out->tcr2_div = (ratio / 2) - 1 and reject non-256x/384x MCLK ratios in sai2_solve_clocks\n' printf " ---\n message: 'got %s'\n hint: 'compute out->tcr2_div = (ratio / 2) - 1 and reject non-256x/384x MCLK ratios in sai2_solve_clocks'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 2 2>&1); then printf 'ok 4 - packs and unpacks 7-bit register plus 9-bit payload across two WM8960 I2C bytes\n' PASS=$((PASS + 1)) else printf 'not ok 4 - packs and unpacks 7-bit register plus 9-bit payload across two WM8960 I2C bytes\n' printf 'FAIL: OR ((val >> 8) & 1) into i2c_bytes[0] bit 0 in wm8960_pack_reg and enforce reg<=0x37, val<=0x1FF\n' printf " ---\n message: 'got %s'\n hint: 'OR ((val >> 8) & 1) into i2c_bytes[0] bit 0 in wm8960_pack_reg and enforce reg<=0x37, val<=0x1FF'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 3 2>&1); then printf 'ok 5 - shifts 16-bit and 24-bit stereo frames by 1 BCLK for Philips I2S with sign extension\n' PASS=$((PASS + 1)) else printf 'not ok 5 - shifts 16-bit and 24-bit stereo frames by 1 BCLK for Philips I2S with sign extension\n' printf 'FAIL: shift left_aligned right by 1 bit when delay_1bclk is true in pack_slot32\n' printf " ---\n message: 'got %s'\n hint: 'shift left_aligned right by 1 bit when delay_1bclk is true in pack_slot32'\n ...\n" "${OUT:-build_failed}" 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_m01_i2s_sai_clock_solver/ so relative paths to files/ resolve directly.