Lab 1: Lab M01: SNVS 47-Bit Rollover RTC & Atomic AP3216C I2C Client Framing
Register Linux 6.12 rtc_device drivers via devm_rtc_allocate_device(), bind single-argument i2c_driver probes from Device Tree, and execute atomic repeated-start i2c_transfer() reads for AP3216C.
Prerequisites in this module
What ./check.sh verifies
- Builds librtc_i2c.a and rtc_i2c_cli with -std=c11 -Wall -Wextra -Werror.
- Archives rtc_i2c_verifier.o into librtc_i2c.a and links rtc_i2c_cli.
snvs_read_47bit_secondsrejects torn carry and converges after 1 retry.i2c_build_ap3216c_read_msgssets I2C_M_RD (0x1) on second repeated-start segment.
step 1/15 · inspect starter files and specification
Two silent defects plague embedded board bring-up on I2C and battery-backed RTC peripherals because both compile cleanly without a single compiler warning. On i.MX6ULL, the Secure Non-Volatile Storage (SNVS) real-time counter splits a 47-bit 32.768 kHz tick accumulator across two 32-bit MMIO registers (LPSRTCMR bits [14:0] and LPSRTCLR bits [31:0]). Reading those two registers once without checking whether LPSRTCLR wrapped from 0xFFFFFFFF to 0x00000000 between the two loads tears the timestamp backward or forward by 2^32 / 32768 = 131,072 seconds (36.4 hours).
On the I2C1 bus (0x021a0000), an AP3216C ambient-light, infrared, and proximity sensor (7-bit address 0x1e) requires a two-message i2c_transfer() sequence with a repeated START (Sr) and I2C_M_RD (0x0001) set on the second struct i2c_msg, followed by bitfield masking across 0x0A..0x0F. Omitting I2C_M_RD turns the read phase into a rogue multi-byte write, while reading IR and PS registers as raw 16-bit integers folds status/overflow bits directly into optical telemetry. In this lab you diagnose and repair all three defects inside a deterministic C11 contract harness.
learner@ksa:~/gl-e06/lab-m01ls -lastarter/Makefilestarter/rtc_i2c_cli.cstarter/rtc_i2c_verifier.cstarter/rtc_i2c_verifier.hlearner@ksa:~/gl-e06/lab-m01starter: starter/Makefile
learner@ksa:~/gl-e06/lab-m01$ bat starter/Makefile File: starter/Makefile CC := gcc AR := ar CFLAGS := -std=c11 -Wall -Wextra -Werror -O2 .PHONY: all clean all: librtc_i2c.a rtc_i2c_cli rtc_i2c_verifier.o: rtc_i2c_verifier.c rtc_i2c_verifier.h $(CC) $(CFLAGS) -c rtc_i2c_verifier.c -o rtc_i2c_verifier.o librtc_i2c.a: rtc_i2c_verifier.o $(AR) rcs librtc_i2c.a rtc_i2c_verifier.o rtc_i2c_cli: rtc_i2c_cli.c librtc_i2c.a rtc_i2c_verifier.h $(CC) $(CFLAGS) rtc_i2c_cli.c librtc_i2c.a -o rtc_i2c_cli clean: rm -f *.o librtc_i2c.a rtc_i2c_clistarter: starter/rtc_i2c_cli.c
learner@ksa:~/gl-e06/lab-m01$ bat starter/rtc_i2c_cli.c File: starter/rtc_i2c_cli.c #define _POSIX_C_SOURCE 200809L #include "rtc_i2c_verifier.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> 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) { const uint32_t cmr_seq[3] = {0x033DU, 0x033EU, 0x033EU}; const uint32_t clr_seq[3] = {0xFFFFFFFFU, 0x00000000U, 0x00000000U}; uint8_t reg = 0x0AU; uint8_t raw[6] = {0x02U, 0x45U, 0x20U, 0x03U, 0x4AU, 0x15U}; uint64_t samples[40]; volatile uint64_t sink = 0U; for (int iter = -5; iter < 40; ++iter) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int r = 0; r < 256; ++r) { uint64_t sec = 0U; size_t retries = 0U; struct ksa_i2c_msg msgs[2]; struct ap3216c_sample smp; (void)snvs_read_47bit_seconds(cmr_seq, clr_seq, 3U, &sec, &retries); i2c_build_ap3216c_read_msgs(0x1EU, ®, raw, 6U, msgs); ap3216c_decode_raw(raw, &smp); sink += sec + retries + msgs[1].flags + smp.ps; } clock_gettime(CLOCK_MONOTONIC, &t1); if (iter >= 0) { samples[iter] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ULL + (uint64_t)(t1.tv_nsec - t0.tv_nsec); } } (void)sink; qsort(samples, 40U, sizeof(samples[0]), cmp_u64); printf("benchmark_samples=40 warmup=5 p50_ns=%llu p95_ns=%llu p99_ns=%llu\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39]); return EXIT_SUCCESS; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) return run_bench(); /* Simulate carry: sample 0 torn (cmr=0x033D, clr=0xFFFFFFFF), samples 1&2 stable */ const uint32_t cmr_seq[3] = {0x033DU, 0x033EU, 0x033EU}; const uint32_t clr_seq[3] = {0xFFFFFFFFU, 0x00000000U, 0x00000000U}; uint64_t sec = 0U; size_t retries = 0U; if (snvs_read_47bit_seconds(cmr_seq, clr_seq, 3U, &sec, &retries) != 0) return EXIT_FAILURE; uint8_t reg = 0x0AU; uint8_t raw[6] = {0x02U, 0x45U, 0x20U, 0x03U, 0x4AU, 0x15U}; struct ksa_i2c_msg msgs[2]; i2c_build_ap3216c_read_msgs(0x1EU, ®, raw, 6U, msgs); struct ap3216c_sample smp; ap3216c_decode_raw(raw, &smp); printf("snvs_sec=%llu retries=%zu msg0_flags=0x%x msg1_flags=0x%x ir=%u als=%u ps=%u obj=%u\n", (unsigned long long)sec, retries, msgs[0].flags, msgs[1].flags, smp.ir, smp.als, smp.ps, smp.obj_detected); return EXIT_SUCCESS; }starter: starter/rtc_i2c_verifier.c
learner@ksa:~/gl-e06/lab-m01$ bat starter/rtc_i2c_verifier.c File: starter/rtc_i2c_verifier.c #include "rtc_i2c_verifier.h" #include <string.h> int snvs_read_47bit_seconds(const uint32_t *cmr_seq, const uint32_t *clr_seq, size_t n, uint64_t *sec_out, size_t *retries_out) { /* BUG: single read trusts potentially torn cmr_seq[0]/clr_seq[0] across rollover */ if (!cmr_seq || !clr_seq || !sec_out || !retries_out || n < 1U) return -1; uint64_t c = (((uint64_t)(cmr_seq[0] & 0x7FFFU)) << 32) | clr_seq[0]; *retries_out = 0U; *sec_out = c >> 15; return 0; } void i2c_build_ap3216c_read_msgs(uint8_t addr7, uint8_t *reg_ptr, uint8_t *rx_buf, uint16_t rx_len, struct ksa_i2c_msg msgs[2]) { /* BUG: forgets KSA_I2C_M_RD on msgs[1] */ memset(msgs, 0, 2U * sizeof(msgs[0])); msgs[0].addr = addr7; msgs[0].len = 1U; msgs[0].buf = reg_ptr; msgs[1].addr = addr7; msgs[1].len = rx_len; msgs[1].buf = rx_buf; } void ap3216c_decode_raw(const uint8_t raw[6], struct ap3216c_sample *out) { /* BUG: treats IR and PS as plain 16-bit words without bitfield masking */ memset(out, 0, sizeof(*out)); out->ir = (uint16_t)(((uint16_t)raw[1] << 8) | raw[0]); out->als = (uint16_t)(((uint16_t)raw[3] << 8) | raw[2]); out->ps = (uint16_t)(((uint16_t)raw[5] << 8) | raw[4]); }starter: starter/rtc_i2c_verifier.h
learner@ksa:~/gl-e06/lab-m01$ bat starter/rtc_i2c_verifier.h File: starter/rtc_i2c_verifier.h #ifndef KSA_RTC_I2C_VERIFIER_H #define KSA_RTC_I2C_VERIFIER_H #include <stddef.h> #include <stdint.h> #define KSA_I2C_M_RD 0x0001U struct ksa_i2c_msg { uint16_t addr; uint16_t flags; uint16_t len; uint8_t *buf; }; struct ap3216c_sample { uint16_t ir; uint16_t als; uint16_t ps; uint8_t ir_overflow; uint8_t obj_detected; }; int snvs_read_47bit_seconds(const uint32_t *cmr_seq, const uint32_t *clr_seq, size_t n, uint64_t *sec_out, size_t *retries_out); void i2c_build_ap3216c_read_msgs(uint8_t addr7, uint8_t *reg_ptr, uint8_t *rx_buf, uint16_t rx_len, struct ksa_i2c_msg msgs[2]); void ap3216c_decode_raw(const uint8_t raw[6], struct ap3216c_sample *out); #endifstep 2/15 · 2. learning objectives
- Reject torn
47-bitSNVSLPSRTCMR/LPSRTCLRregister pairs by looping until two back-to-back64-bit snapshots match before shifting right by15(32,768 Hzto1 Hz). - Construct a two-segment
struct ksa_i2c_msgdescriptor array forAP3216Cregister0x0Awithmsgs[0].flags = 0(write pointer) andmsgs[1].flags = KSA_I2C_M_RD(repeated-start burst read). - Extract
10-bitIR,16-bitALS,10-bitPS,ir_overflow(bit 7of0x0A), andobj_detected(bit 6of0x0E) from a6-byteAP3216Cburst without bitfield bleed. - Measure
40-sampleCLOCK_MONOTONICexecution percentiles (p50,p95,p99) on the host verifier and separate user-space bit-manipulation timing from100 kHz/400 kHzI2Cwire time.
step 3/15 · 3. environment requirements
All automated checks execute in user space on a POSIX Linux workstation using a C11 toolchain and clock_gettime(CLOCK_MONOTONIC), requiring neither root privileges nor an attached board. Recorded transcripts in captures.json were captured on an x86_64 host running kernel 7.0.0-42-generic and validate register/protocol state machines only. Hardware timing, IOMUXC open-drain (0x4001b8b0) pad electrical behavior, and /dev/rtc0 + /dev/i2c-0 driver binding belong exclusively to ARMv7-A target testing under Linux kernel 6.12 LTS.
step 4/15 · 4. required software and hardware
- Host workstation:
gcc(-std=c11 -Wall -Wextra -Werror), GNUmake,ar(binutils), and POSIX/bin/sh. - Target board (optional, Section 13):
i.MX6ULLCortex-A7board bootingLinux kernel6.12 LTS withrtc-snvs,i2c-imx,i2c-tools(i2cdetect,i2ctransfer),util-linuxhwclock, and an on-boardAP3216Csensor at0x1eonI2C1.
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-e06/lab-m01$ bat script.sh File: script.sh make -B -C files ./files/rtc_i2c_cli ./files/rtc_i2c_cli --bench sh check.shmake -B -C filescompilesrtc_i2c_verifier.cunder-std=c11 -Wall -Wextra -Werror -O2, packslibrtc_i2c.awithar rcs, and linksrtc_i2c_cli../files/rtc_i2c_clifeeds a simulatedLPSRTCLRcarry boundary (0x033D:0xFFFFFFFF->0x033E:0x00000000) and a6-byteAP3216Cpayload (02 45 20 03 4a 15) through the library../files/rtc_i2c_cli --benchruns5warmup iterations followed by40timed256-call batches underCLOCK_MONOTONIC.sh check.shbuilds a clean temporary copy offiles/and emits5TAP version 13assertions. To watch the defective baseline fail, copy the lab directory to/tmp/m01_starter_test, overwritefiles/withstarter/, and runsh check.shthere.
step 6/15 · 6. annotated source code
files/rtc_i2c_verifier.c enforces three hardware contracts that starter/rtc_i2c_verifier.c violates:
learner@ksa:~/gl-e06/lab-m01$ bat example.c File: example.c for (size_t i = 0; i + 1U < n; ++i) { uint64_t c1 = (((uint64_t)(cmr_seq[i] & 0x7FFFU)) << 32) | clr_seq[i]; uint64_t c2 = (((uint64_t)(cmr_seq[i + 1U] & 0x7FFFU)) << 32) | clr_seq[i + 1U]; if (c1 == c2) { *sec_out = c2 >> 15; return 0; } (*retries_out)++; }In starter/rtc_i2c_verifier.c, snvs_read_47bit_seconds() accepts n >= 1 and shifts cmr_seq[0]:clr_seq[0] immediately without a second confirming read; i2c_build_ap3216c_read_msgs() leaves msgs[1].flags zeroed instead of setting KSA_I2C_M_RD (0x0001); and ap3216c_decode_raw() shifts raw[1] << 8 | raw[0] and raw[5] << 8 | raw[4] as 16-bit words instead of packing (raw[1] << 2) | (raw[0] & 0x03) for 10-bit IR and ((raw[5] & 0x3F) << 4) | (raw[4] & 0x0F) for 10-bit PS.
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-e06/lab-m01$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds librtc_i2c.a and rtc_i2c_cli with -std=c11 -Wall -Wextra -Werror ok 2 - archives rtc_i2c_verifier.o into librtc_i2c.a and links rtc_i2c_cli ok 3 - snvs_read_47bit_seconds rejects torn carry and converges after 1 retry ok 4 - i2c_build_ap3216c_read_msgs sets I2C_M_RD (0x1) on second repeated-start segment ok 5 - ap3216c_decode_raw extracts 10-bit IR/PS, 16-bit ALS, and object-detect bit # pass 5/5Defective starter/ run (exit code 1): not ok 3, not ok 4, and not ok 5 emit FAIL: diagnostics (got "snvs_sec=108789759 retries=0 msg0_flags=0x0 msg1_flags=0x0 ir=17666 als=800 ps=5450 obj=0").
step 8/15 · 8. explanation of observed behavior
Sample 0 (0x033D:0xFFFFFFFF) represents a torn snapshot where LPSRTCMR was latched one tick before LPSRTCLR rolled to 0x00000000 (0x033E:0x00000000). The starter trusts sample 0 and computes 0x033DFFFFFFFF >> 15 = 108,789,759 (1 second early; had cmr latched 0x033E with stale 0xFFFFFFFF, it would jump +131,071 seconds ahead!), whereas files/ retries once (retries=1) and converges on 0x033E00000000 >> 15 = 108,789,760. On AP3216C bytes {0x02, 0x45, 0x20, 0x03, 0x4A, 0x15}, masking gives ir = (0x45 << 2) | 0x02 = 278, als = 0x0320 = 800, ps = (0x15 << 4) | 0x0A = 346, and obj = (0x4A >> 6) & 1 = 1.
step 9/15 · 9. experiment variations
- Near transfer: Change
cmr_seq[0]in a scratch test from0x033Dto0x033Ewhile keepingclr_seq[0] = 0xFFFFFFFF(the opposite tear direction whereCLRis read first at0xFFFFFFFFandCMRis read after carry at0x033E). Compare the131,071-second jump instarter/againstfiles/. - Far transfer: Adapt
snvs_read_47bit_seconds()to model theARMv7-A64-bit Generic Timer (CNTPCTread viaCNTPCT_HI,CNTPCT_LO,CNTPCT_HIthree-register loop) and state why3register reads suffice there whilertc-snvs.ccompares full64-bit pairs in a bounded loop.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/rtc_i2c_cli --bench on x86_64 7.0.0-42-generic, CLOCK_MONOTONIC, warmup=5, N=40 batches of 256 calls):
learner@ksa:~/gl-e06/lab-m01$ bat listing.txt File: listing.txt benchmark_samples=40 warmup=5 p50_ns=1423 p95_ns=1442 p99_ns=1443Dividing 1,423 ns by 256 iterations shows ~5.6 ns of pure CPU register/bitfield work on x86_64 (with N=40, p99_ns is samples[39], the single slowest batch). Never quote host RAM numbers as I2C driver latency: on i.MX6ULL at 100 kHz I2C (10 us/bit), a 2-message 6-byte AP3216C read shifts 9 address/write bits + 9 reg-pointer bits + 9 repeated-start address/read bits + 6 * 9 payload bits = 81 bit periods (> 810 us wire time), measured on target via trace-cmd record -e i2c:i2c_write -e i2c:i2c_read -e i2c:i2c_result.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix |
|---|---|---|
snvs_read_47bit_seconds returns -1 when n == 2 and c1 == c2 | Loop bound written i + 2 < n instead of i + 1U < n | Iterate for (size_t i = 0; i + 1U < n; ++i) |
msg1_flags=0x0 fails TAP test 4 | msgs[1].flags left 0 after memset | Assign msgs[1].flags = KSA_I2C_M_RD (0x0001U) |
ps exceeds 1023 when obj_detected == 1 | Forgot to mask bit 6 (IR_OF/OBJ) out of raw[4]/raw[5] | Mask (raw[5] & 0x3FU) << 4 and (raw[4] & 0x0FU) |
On target: i2ctransfer returns -EREMOTEIO (NACK) | Missing SION + ODE (0x4001b8b0) on I2C1 pads or sensor in power-down | Set 0x4001b8b0 in Device Tree and write 0x03 to reg 0x00 after 0x04 reset + >= 10 ms sleep |
step 12/15 · 12. guided questions
- Why does
snvs_rtcmaskLPSRTCMRwith0x7FFFUbefore shifting left by32?
- *Hint 1:* Count
15 + 32bits. - *Hint 2:* Bits
[31:15]ofLPSRTCMRare unrelated/reserved bits outside the47-bit counter.
- Why would replacing
i2c_transfer(adap, msgs, 2)withi2c_master_send()followed byi2c_master_recv()fail intermittently on a sharedI2C1bus?
- *Hint 1:* Which lock does
i2c_transfer()hold acrossmsgs[0..1]? - *Hint 2:* Between two separate calls, another
I2Cthread can acquireadapter->bus_lockor aSTOPcondition can reset the device's register pointer.
step 13/15 · 13. independent challenges
Extend rtc_i2c_verifier.c with ap3216c_validate_sample() returning -EAGAIN (-11) whenever ir_overflow == 1 (bit 7 of 0x0A or bit 6 of 0x0F), and test raw inputs with saturated IR light.
For target hardware acceptance on the i.MX6ULL board running Linux kernel 6.12 LTS:
- Read
/sys/class/rtc/rtc0/nameto confirmsnvs_rtcbound cleanly, then samplehwclock -racross100consecutive reads with zero backward timestamp jumps. - Probe
I2C1withi2cdetect -y 0(0x1evisible orUUif bound), resetAP3216C(i2cset -y 0 0x1e 0x00 0x04), sleep20 ms, enableALS+PS+IR(0x03), and capture a repeated-start burst viai2ctransfer -y 0 w1@0x1e 0x0a r6.
step 14/15 · 14. solutions and explanations
Repairing starter/rtc_i2c_verifier.c requires: (1) rejecting n < 2U and comparing c1 vs c2 across adjacent (cmr_seq[i], clr_seq[i]) pairs before *sec_out = c2 >> 15; (2) setting msgs[1].flags = KSA_I2C_M_RD; and (3) masking raw[0] & 0x03U, raw[4] & 0x0FU, and raw[5] & 0x3FU while extracting (raw[4] >> 6) & 1U into obj_detected. On Question 1, 15 upper bits + 32 lower bits form the 47-bit SNVS counter; unmasked upper bits corrupt time64_t. On Question 2, a single i2c_transfer() call holds adapter->bus_lock across the repeated START without releasing the bus.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-e06/lab-m01./files/rtc_i2c_clilearner@ksa:~/gl-e06/lab-m01snvs_sec=108789760 retries=1 msg0_flags=0x0 msg1_flags=0x1 ir=278 als=800 ps=346 obj=1Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds librtc_i2c.a and rtc_i2c_cli 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-e06/lab-m01$ bat files/Makefile File: files/Makefile CC := gcc AR := ar CFLAGS := -std=c11 -Wall -Wextra -Werror -O2 .PHONY: all clean all: librtc_i2c.a rtc_i2c_cli rtc_i2c_verifier.o: rtc_i2c_verifier.c rtc_i2c_verifier.h $(CC) $(CFLAGS) -c rtc_i2c_verifier.c -o rtc_i2c_verifier.o librtc_i2c.a: rtc_i2c_verifier.o $(AR) rcs librtc_i2c.a rtc_i2c_verifier.o rtc_i2c_cli: rtc_i2c_cli.c librtc_i2c.a rtc_i2c_verifier.h $(CC) $(CFLAGS) rtc_i2c_cli.c librtc_i2c.a -o rtc_i2c_cli clean: rm -f *.o librtc_i2c.a rtc_i2c_clisolution: files/rtc_i2c_cli.c
learner@ksa:~/gl-e06/lab-m01$ bat files/rtc_i2c_cli.c File: files/rtc_i2c_cli.c #define _POSIX_C_SOURCE 200809L #include "rtc_i2c_verifier.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> 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) { const uint32_t cmr_seq[3] = {0x033DU, 0x033EU, 0x033EU}; const uint32_t clr_seq[3] = {0xFFFFFFFFU, 0x00000000U, 0x00000000U}; uint8_t reg = 0x0AU; uint8_t raw[6] = {0x02U, 0x45U, 0x20U, 0x03U, 0x4AU, 0x15U}; uint64_t samples[40]; volatile uint64_t sink = 0U; for (int iter = -5; iter < 40; ++iter) { struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); for (int r = 0; r < 256; ++r) { uint64_t sec = 0U; size_t retries = 0U; struct ksa_i2c_msg msgs[2]; struct ap3216c_sample smp; (void)snvs_read_47bit_seconds(cmr_seq, clr_seq, 3U, &sec, &retries); i2c_build_ap3216c_read_msgs(0x1EU, ®, raw, 6U, msgs); ap3216c_decode_raw(raw, &smp); sink += sec + retries + msgs[1].flags + smp.ps; } clock_gettime(CLOCK_MONOTONIC, &t1); if (iter >= 0) { samples[iter] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ULL + (uint64_t)(t1.tv_nsec - t0.tv_nsec); } } (void)sink; qsort(samples, 40U, sizeof(samples[0]), cmp_u64); printf("benchmark_samples=40 warmup=5 p50_ns=%llu p95_ns=%llu p99_ns=%llu\n", (unsigned long long)samples[20], (unsigned long long)samples[38], (unsigned long long)samples[39]); return EXIT_SUCCESS; } int main(int argc, char **argv) { if (argc > 1 && strcmp(argv[1], "--bench") == 0) return run_bench(); /* Simulate carry: sample 0 torn (cmr=0x033D, clr=0xFFFFFFFF), samples 1&2 stable */ const uint32_t cmr_seq[3] = {0x033DU, 0x033EU, 0x033EU}; const uint32_t clr_seq[3] = {0xFFFFFFFFU, 0x00000000U, 0x00000000U}; uint64_t sec = 0U; size_t retries = 0U; if (snvs_read_47bit_seconds(cmr_seq, clr_seq, 3U, &sec, &retries) != 0) return EXIT_FAILURE; uint8_t reg = 0x0AU; uint8_t raw[6] = {0x02U, 0x45U, 0x20U, 0x03U, 0x4AU, 0x15U}; struct ksa_i2c_msg msgs[2]; i2c_build_ap3216c_read_msgs(0x1EU, ®, raw, 6U, msgs); struct ap3216c_sample smp; ap3216c_decode_raw(raw, &smp); printf("snvs_sec=%llu retries=%zu msg0_flags=0x%x msg1_flags=0x%x ir=%u als=%u ps=%u obj=%u\n", (unsigned long long)sec, retries, msgs[0].flags, msgs[1].flags, smp.ir, smp.als, smp.ps, smp.obj_detected); return EXIT_SUCCESS; }solution: files/rtc_i2c_verifier.c
learner@ksa:~/gl-e06/lab-m01$ bat files/rtc_i2c_verifier.c File: files/rtc_i2c_verifier.c #include "rtc_i2c_verifier.h" #include <string.h> int snvs_read_47bit_seconds(const uint32_t *cmr_seq, const uint32_t *clr_seq, size_t n, uint64_t *sec_out, size_t *retries_out) { if (!cmr_seq || !clr_seq || !sec_out || !retries_out || n < 2U) return -1; *retries_out = 0U; for (size_t i = 0; i + 1U < n; ++i) { uint64_t c1 = (((uint64_t)(cmr_seq[i] & 0x7FFFU)) << 32) | clr_seq[i]; uint64_t c2 = (((uint64_t)(cmr_seq[i + 1U] & 0x7FFFU)) << 32) | clr_seq[i + 1U]; if (c1 == c2) { *sec_out = c2 >> 15; return 0; } (*retries_out)++; } return -1; } void i2c_build_ap3216c_read_msgs(uint8_t addr7, uint8_t *reg_ptr, uint8_t *rx_buf, uint16_t rx_len, struct ksa_i2c_msg msgs[2]) { memset(msgs, 0, 2U * sizeof(msgs[0])); msgs[0].addr = addr7; msgs[0].flags = 0U; msgs[0].len = 1U; msgs[0].buf = reg_ptr; msgs[1].addr = addr7; msgs[1].flags = KSA_I2C_M_RD; msgs[1].len = rx_len; msgs[1].buf = rx_buf; } void ap3216c_decode_raw(const uint8_t raw[6], struct ap3216c_sample *out) { memset(out, 0, sizeof(*out)); out->ir_overflow = (uint8_t)((raw[0] >> 7) & 0x01U); out->ir = (uint16_t)(((uint16_t)raw[1] << 2) | (raw[0] & 0x03U)); out->als = (uint16_t)(((uint16_t)raw[3] << 8) | raw[2]); out->obj_detected = (uint8_t)((raw[4] >> 6) & 0x01U); out->ps = (uint16_t)(((uint16_t)(raw[5] & 0x3FU) << 4) | (raw[4] & 0x0FU)); }solution: files/rtc_i2c_verifier.h
learner@ksa:~/gl-e06/lab-m01$ bat files/rtc_i2c_verifier.h File: files/rtc_i2c_verifier.h #ifndef KSA_RTC_I2C_VERIFIER_H #define KSA_RTC_I2C_VERIFIER_H #include <stddef.h> #include <stdint.h> #define KSA_I2C_M_RD 0x0001U struct ksa_i2c_msg { uint16_t addr; uint16_t flags; uint16_t len; uint8_t *buf; }; struct ap3216c_sample { uint16_t ir; uint16_t als; uint16_t ps; uint8_t ir_overflow; uint8_t obj_detected; }; int snvs_read_47bit_seconds(const uint32_t *cmr_seq, const uint32_t *clr_seq, size_t n, uint64_t *sec_out, size_t *retries_out); void i2c_build_ap3216c_read_msgs(uint8_t addr7, uint8_t *reg_ptr, uint8_t *rx_buf, uint16_t rx_len, struct ksa_i2c_msg msgs[2]); void ap3216c_decode_raw(const uint8_t raw[6], struct ap3216c_sample *out); #endifRun the check
From labs/lab_m01_rtc_i2c_client/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-e06/lab-m01./check.shTAP version 131..5ok 1 - builds librtc_i2c.a and rtc_i2c_cli with -std=c11 -Wall -Wextra -Werrorok 2 - archives rtc_i2c_verifier.o into librtc_i2c.a and links rtc_i2c_cliok 3 - snvs_read_47bit_seconds rejects torn carry and converges after 1 retryok 4 - i2c_build_ap3216c_read_msgs sets I2C_M_RD (0x1) on second repeated-start segmentok 5 - ap3216c_decode_raw extracts 10-bit IR/PS, 16-bit ALS, and object-detect bit# 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-e06/lab-m01./check.shTAP version 131..5ok 1 - builds librtc_i2c.a and rtc_i2c_cli with -std=c11 -Wall -Wextra -Werrorok 2 - archives rtc_i2c_verifier.o into librtc_i2c.a and links rtc_i2c_clinot ok 3 - snvs_read_47bit_seconds rejects torn carry and converges after 1 retryFAIL: retry consecutive LPSRTCMR/LPSRTCLR reads until equal before shifting >> 15 --- message: 'got "snvs_sec=108789759 retries=0 msg0_flags=0x0 msg1_flags=0x0 ir=17666 als=800 ps=5450 obj=0"' hint: 'retry consecutive LPSRTCMR/LPSRTCLR reads until equal before shifting >> 15' ...not ok 4 - i2c_build_ap3216c_read_msgs sets I2C_M_RD (0x1) on second repeated-start segmentFAIL: set msgs[0].flags = 0 and msgs[1].flags = KSA_I2C_M_RD --- message: 'got "snvs_sec=108789759 retries=0 msg0_flags=0x0 msg1_flags=0x0 ir=17666 als=800 ps=5450 obj=0"' hint: 'set msgs[0].flags = 0 and msgs[1].flags = KSA_I2C_M_RD' ...not ok 5 - ap3216c_decode_raw extracts 10-bit IR/PS, 16-bit ALS, and object-detect bitFAIL: mask IR[1:0], PS[3:0]/[5:0], and bit 6 of raw[4] for obj_detected --- message: 'got "snvs_sec=108789759 retries=0 msg0_flags=0x0 msg1_flags=0x0 ir=17666 als=800 ps=5450 obj=0"' hint: 'mask IR[1:0], PS[3:0]/[5:0], and bit 6 of raw[4] for obj_detected' ...# 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-e06/lab-m01$ bat check.sh File: check.sh #!/bin/sh set -u PASS=0 TOTAL=5 clean_msg() { printf '%s' "$1" | tr '\r\n' ' ' | tr "'" '"' | sed 's/ */ /g; s/^ //; s/ $//'; } emit_diag() { printf 'FAIL: %s\n' "$(clean_msg "$2")"; printf " ---\n message: '%s'\n hint: '%s'\n ...\n" "$(clean_msg "$1")" "$(clean_msg "$2")"; } WORKDIR="$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab_e06_m01.XXXXXX")" trap 'rm -rf "$WORKDIR"' EXIT printf 'TAP version 13\n1..%d\n' "$TOTAL" BUILD_OK=0 cp -R files "$WORKDIR/build" if make -C "$WORKDIR/build" >"$WORKDIR/mk.out" 2>"$WORKDIR/mk.err"; then echo "ok 1 - builds librtc_i2c.a and rtc_i2c_cli with -std=c11 -Wall -Wextra -Werror" PASS=$((PASS + 1)) BUILD_OK=1 else ERR="$(cat "$WORKDIR/mk.err" 2>/dev/null)" echo "not ok 1 - builds librtc_i2c.a and rtc_i2c_cli with -std=c11 -Wall -Wextra -Werror" emit_diag "make failed: ${ERR:-error}" "fix compiler errors in files/" fi if [ "$BUILD_OK" -eq 1 ] && ar t "$WORKDIR/build/librtc_i2c.a" 2>/dev/null | grep -q "rtc_i2c_verifier.o" && [ -x "$WORKDIR/build/rtc_i2c_cli" ]; then echo "ok 2 - archives rtc_i2c_verifier.o into librtc_i2c.a and links rtc_i2c_cli" PASS=$((PASS + 1)) else echo "not ok 2 - archives rtc_i2c_verifier.o into librtc_i2c.a and links rtc_i2c_cli" emit_diag "missing librtc_i2c.a or rtc_i2c_cli" "check Makefile archive and link rules" fi OUT="" if [ -x "$WORKDIR/build/rtc_i2c_cli" ]; then OUT="$("$WORKDIR/build/rtc_i2c_cli" 2>&1 || true)" fi case "$OUT" in *"snvs_sec=108789760 retries=1"*) echo "ok 3 - snvs_read_47bit_seconds rejects torn carry and converges after 1 retry" PASS=$((PASS + 1)) ;; *) echo "not ok 3 - snvs_read_47bit_seconds rejects torn carry and converges after 1 retry" emit_diag "got '$OUT'" "retry consecutive LPSRTCMR/LPSRTCLR reads until equal before shifting >> 15" ;; esac case "$OUT" in *"msg0_flags=0x0 msg1_flags=0x1"*) echo "ok 4 - i2c_build_ap3216c_read_msgs sets I2C_M_RD (0x1) on second repeated-start segment" PASS=$((PASS + 1)) ;; *) echo "not ok 4 - i2c_build_ap3216c_read_msgs sets I2C_M_RD (0x1) on second repeated-start segment" emit_diag "got '$OUT'" "set msgs[0].flags = 0 and msgs[1].flags = KSA_I2C_M_RD" ;; esac if [ "$OUT" = "snvs_sec=108789760 retries=1 msg0_flags=0x0 msg1_flags=0x1 ir=278 als=800 ps=346 obj=1" ]; then echo "ok 5 - ap3216c_decode_raw extracts 10-bit IR/PS, 16-bit ALS, and object-detect bit" PASS=$((PASS + 1)) else echo "not ok 5 - ap3216c_decode_raw extracts 10-bit IR/PS, 16-bit ALS, and object-detect bit" emit_diag "got '$OUT'" "mask IR[1:0], PS[3:0]/[5:0], and bit 6 of raw[4] for obj_detected" fi printf '# pass %d/%d\n' "$PASS" "$TOTAL" [ "$PASS" -eq "$TOTAL" ]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_rtc_i2c_client/ so relative paths to files/ resolve directly.