E36 — RP square wave as an ADC stimulus (ADC1, dual ADC)
| ID | E36 |
| Date | 2026-12-04 |
| Status | done: sample rate from the RP square wave = SysTick rate within ±0.15 % in every mode (1.7 to 11.4 MS/s); full scale 0–4095; edge seen ~25 ns wide in every mode; dual mode ADC2 − ADC1 ±0.4 LSB at 1.5 cycles, ±9 LSB at 3.5 cycles |
| Decisions | D112 (DECISIONS.md) |
| Board | #1 (rev A), CH32 ×32; RP sq vsync (GPIO32 PWM → 33 Ω → CH32 PA5 = ADC_IN5), RP lines released with hv z |
| Firmware + results | fw/tests/2026-12-04-1-links, raw: adcsq.txt |
| Scripts | links_adcsq.py; CH32 adcrun, adcdual, adcdump (E11, E12) |
Summary: A square wave from the RP (its own crystal) on PA5 checks the CH32 ADC in two ways at once: the number of samples per period gives the sample rate against a clock that is not the CH32’s, and the samples caught on the edges show how the sample-and-hold sees a fast step. The rate checks agree with SysTick within 0.15 % everywhere. Every mode catches a transition sample at the same rate per unit of time, about 25 ns of “edge” — so even 1.5 ADC cycles (18.75 ns at 80 MHz) are enough for this 33 Ω source.
Results (16384 samples per mode, 97.02 kHz square wave, 5 kHz for the slow mode)
| mode | SysTick rate | from the square wave | low / high code | edges | mid-edge samples |
|---|---|---|---|---|---|
| ADC1 HCLK/8, 239.5 cycles | 47 617 S/s | 47 600 (−0.04 %) | 3 / 4095 | 860 | 1 |
| ADC1 HCLK/4, 1.5 cycles | 1 714 062 | 1 713 454 (−0.04 %) | 3 / 4095 | 1854 | 79 (4.3 %) |
| ADC1 80 MHz (USBHS/6), 1.5 cycles | 5 711 696 | 5 713 778 (+0.04 %) | 0 / 4095 | 556 | 79 (14.2 %) |
| ADC1 80 MHz, 3.5 cycles | 4 997 285 | 4 999 677 (+0.05 %) | 3 / 4094 | 636 | 80 (12.6 %) |
| ADC1 80 MHz, 5.5 cycles | 4 441 613 | 4 443 726 (+0.05 %) | 3 / 4094 | 715 | 79 (11.0 %) |
| dual 80 MHz, 1.5 cycles | 11 416 148 | 11 427 555 (+0.10 %) | 0 / 4087 | 278 | 79 (28.4 %) |
| dual 80 MHz, 3.5 cycles | 9 985 190 | 9 999 353 (+0.14 %) | 7 / 4089 | 318 | 80 (25.2 %) |
- Rates: both clocks agree within 0.15 %. The square-wave figure has a ±1-sample-per-period resolution over ~150 periods, which explains most of the spread; the CH32 HSE is −27 ppm (E4).
- Mid-edge samples: a sample lands between 10 % and 90 % of the step only if it was taken while the input was moving. 97 kHz is not commensurate with any of the rates, so the edge phase walks across the sample period. The count per capture is then N × 2f × t_edge, independent of the sample rate: 79–80 in every fast mode gives t_edge ≈ 25 ns (RP output edge through 33 Ω into the pin and S/H capacitance). A sampling time too short to settle would add mid-level samples at the low sampling times; none are seen.
- Dual mode offset: on the flat parts ADC2 − ADC1 = −0.2/+0.4 LSB at 1.5 cycles, but −8.9/+8.4 LSB (high/low level) at 3.5 cycles: a gain mismatch between the two converters at that setting. Use 1.5 cycles in dual mode, or calibrate per converter.
Lesson
When a figure stays the same across very different settings, check whether it should: here a constant count of mid-edge samples is the expected result (time-domain width), not a bug.
Chip facts and numbers: fw/LEARNINGS.md. All experiments: index.