@typecad/framework-zephyr 1.0.0-alpha.13 → 1.0.0-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/README.md +22 -35
  2. package/dist/as-built.d.ts +33 -0
  3. package/dist/as-built.js +188 -0
  4. package/dist/async/timer-polyfill.js +1 -1
  5. package/dist/boardgen.d.ts +128 -0
  6. package/dist/boardgen.js +1366 -0
  7. package/dist/chips/board-overrides.d.ts +7 -0
  8. package/dist/chips/board-overrides.js +11 -0
  9. package/dist/chips/controllers.d.ts +28 -8
  10. package/dist/chips/controllers.js +49 -12
  11. package/dist/chips/esp32s3.js +49 -0
  12. package/dist/chips/index.d.ts +6 -12
  13. package/dist/chips/index.js +18 -34
  14. package/dist/chips/resolve.d.ts +16 -2
  15. package/dist/chips/resolve.js +298 -15
  16. package/dist/chips/soc/.d.ts +2 -0
  17. package/dist/chips/soc/.js +129 -0
  18. package/dist/chips/soc/esp32.d.ts +2 -0
  19. package/dist/chips/soc/esp32.js +120 -0
  20. package/dist/chips/soc/esp32c3.d.ts +2 -0
  21. package/dist/chips/soc/esp32c3.js +90 -0
  22. package/dist/chips/soc/esp32c6.d.ts +2 -0
  23. package/dist/chips/soc/esp32c6.js +109 -0
  24. package/dist/chips/soc/esp32s3.d.ts +2 -0
  25. package/dist/chips/soc/esp32s3.js +189 -0
  26. package/dist/chips/soc/index.d.ts +2 -0
  27. package/dist/chips/soc/index.js +23 -0
  28. package/dist/chips/soc/nrf52840.d.ts +2 -0
  29. package/dist/chips/soc/nrf52840.js +130 -0
  30. package/dist/chips/soc/rp2040.d.ts +2 -0
  31. package/dist/chips/soc/rp2040.js +141 -0
  32. package/dist/chips/soc/rp2350a.d.ts +2 -0
  33. package/dist/chips/soc/rp2350a.js +145 -0
  34. package/dist/chips/soc/samd21g18a.d.ts +2 -0
  35. package/dist/chips/soc/samd21g18a.js +143 -0
  36. package/dist/chips/soc/stm32f411xe.d.ts +2 -0
  37. package/dist/chips/soc/stm32f411xe.js +251 -0
  38. package/dist/chips/types.d.ts +382 -12
  39. package/dist/chips/xiao-ble.js +33 -0
  40. package/dist/display/profiles.js +1 -1
  41. package/dist/doctor.js +38 -4
  42. package/dist/dt-config/custom-board.d.ts +23 -0
  43. package/dist/dt-config/custom-board.js +227 -0
  44. package/dist/dt-config/kconfig.d.ts +83 -1
  45. package/dist/dt-config/kconfig.js +116 -27
  46. package/dist/dt-config/overlay.js +623 -18
  47. package/dist/framework.manifest.d.ts +20 -30
  48. package/dist/framework.manifest.js +124 -178
  49. package/dist/index.d.ts +4 -1
  50. package/dist/index.js +8 -2
  51. package/dist/lowering/adc.d.ts +26 -5
  52. package/dist/lowering/adc.js +137 -31
  53. package/dist/lowering/ble.js +31 -28
  54. package/dist/lowering/dac.js +9 -9
  55. package/dist/lowering/fs.js +18 -10
  56. package/dist/lowering/gpio.d.ts +2 -0
  57. package/dist/lowering/gpio.js +90 -10
  58. package/dist/lowering/http.js +39 -14
  59. package/dist/lowering/hwtimer.d.ts +4 -0
  60. package/dist/lowering/hwtimer.js +35 -10
  61. package/dist/lowering/i2c.js +46 -54
  62. package/dist/lowering/index.d.ts +5 -5
  63. package/dist/lowering/index.js +15 -16
  64. package/dist/lowering/interrupts.d.ts +22 -5
  65. package/dist/lowering/interrupts.js +104 -22
  66. package/dist/lowering/mqtt.js +12 -6
  67. package/dist/lowering/power.js +0 -4
  68. package/dist/lowering/preferences.js +49 -77
  69. package/dist/lowering/pulse.js +5 -36
  70. package/dist/lowering/pwm.d.ts +32 -3
  71. package/dist/lowering/pwm.js +105 -17
  72. package/dist/lowering/random.js +3 -3
  73. package/dist/lowering/sensor.d.ts +37 -0
  74. package/dist/lowering/sensor.js +119 -0
  75. package/dist/lowering/spi.d.ts +12 -0
  76. package/dist/lowering/spi.js +65 -40
  77. package/dist/lowering/thread.d.ts +14 -0
  78. package/dist/lowering/thread.js +55 -0
  79. package/dist/lowering/timing.js +20 -46
  80. package/dist/lowering/tone.js +18 -5
  81. package/dist/lowering/uart.d.ts +13 -0
  82. package/dist/lowering/uart.js +72 -51
  83. package/dist/lowering/usb.d.ts +34 -0
  84. package/dist/lowering/usb.js +206 -0
  85. package/dist/lowering/wdt.d.ts +1 -1
  86. package/dist/lowering/wdt.js +17 -7
  87. package/dist/lowering/wifi.d.ts +3 -2
  88. package/dist/lowering/wifi.js +120 -111
  89. package/dist/sdk/board-catalog-sync.d.ts +16 -0
  90. package/dist/sdk/board-catalog-sync.js +41 -0
  91. package/dist/sdk/board-data.generated.d.ts +2 -0
  92. package/dist/sdk/board-data.generated.js +4 -0
  93. package/dist/sdk/catalog-walker.d.ts +90 -0
  94. package/dist/sdk/catalog-walker.js +682 -0
  95. package/dist/sdk/dts-reader.d.ts +83 -0
  96. package/dist/sdk/dts-reader.js +596 -0
  97. package/dist/strategy.d.ts +136 -17
  98. package/dist/strategy.js +881 -212
  99. package/dist/toolchain/bossac-touch.d.ts +18 -0
  100. package/dist/toolchain/bossac-touch.js +88 -0
  101. package/dist/toolchain/compat.d.ts +2 -2
  102. package/dist/toolchain/compat.js +8 -3
  103. package/dist/toolchain/debug-config.d.ts +21 -26
  104. package/dist/toolchain/debug-config.js +212 -72
  105. package/dist/toolchain/env-check.d.ts +4 -4
  106. package/dist/toolchain/env-check.js +5 -5
  107. package/dist/toolchain/index.d.ts +66 -1
  108. package/dist/toolchain/index.js +713 -58
  109. package/dist/toolchain/scaffold.d.ts +9 -0
  110. package/dist/toolchain/scaffold.js +84 -7
  111. package/dist/toolchain/west-discover.d.ts +8 -1
  112. package/dist/toolchain/west-discover.js +19 -6
  113. package/dist/toolchain/west-spawn.js +17 -5
  114. package/installer/CHANGELOG.md +473 -0
  115. package/installer/README.md +328 -0
  116. package/installer/environment.yml +46 -0
  117. package/installer/etc/conda/activate.d/zephyr.bat +7 -0
  118. package/installer/etc/conda/activate.d/zephyr.ps1 +12 -0
  119. package/installer/etc/conda/activate.d/zephyr.sh +20 -0
  120. package/installer/etc/conda/deactivate.d/zephyr.bat +6 -0
  121. package/installer/etc/conda/deactivate.d/zephyr.ps1 +6 -0
  122. package/installer/etc/conda/deactivate.d/zephyr.sh +4 -0
  123. package/installer/install.mjs +580 -0
  124. package/installer/install.ps1 +619 -0
  125. package/installer/install.sh +310 -0
  126. package/installer/lib/detect-platform.sh +62 -0
  127. package/installer/lib/fetch-sdk.sh +304 -0
  128. package/installer/lib/init-workspace.sh +153 -0
  129. package/installer/lib/write-activation.sh +76 -0
  130. package/installer/patches/tfm-c-include-separators.patch +19 -0
  131. package/installer/patches/zephyr-tfm-nordic-zephyr-include.patch +24 -0
  132. package/installer/templates/project/.typecad/activate-zephyr.ps1 +31 -0
  133. package/installer/templates/project/.typecad/activate-zephyr.sh +31 -0
  134. package/installer/templates/project/.vscode/settings.json +30 -0
  135. package/installer/templates/project/README.md +58 -0
  136. package/installer/versions.env +110 -0
  137. package/package.json +16 -16
  138. package/src/as-built.ts +206 -0
  139. package/src/boardgen.ts +1428 -0
  140. package/src/chips/controllers.ts +61 -12
  141. package/src/chips/index.ts +35 -54
  142. package/src/chips/resolve.ts +316 -14
  143. package/src/chips/types.ts +388 -13
  144. package/src/display/profiles.ts +1 -1
  145. package/src/doctor.ts +110 -77
  146. package/src/dt-config/custom-board.ts +325 -0
  147. package/src/dt-config/kconfig.ts +177 -19
  148. package/src/dt-config/overlay.ts +1058 -450
  149. package/src/framework.manifest.ts +125 -178
  150. package/src/index.ts +57 -41
  151. package/src/lowering/adc.ts +222 -102
  152. package/src/lowering/ble.ts +32 -28
  153. package/src/lowering/dac.ts +82 -82
  154. package/src/lowering/fs.ts +18 -10
  155. package/src/lowering/gpio.ts +242 -152
  156. package/src/lowering/http.ts +512 -488
  157. package/src/lowering/hwtimer.ts +139 -101
  158. package/src/lowering/i2c.ts +116 -126
  159. package/src/lowering/index.ts +91 -93
  160. package/src/lowering/interrupts.ts +113 -21
  161. package/src/lowering/mqtt.ts +12 -6
  162. package/src/lowering/preferences.ts +327 -354
  163. package/src/lowering/pwm.ts +167 -74
  164. package/src/lowering/random.ts +3 -3
  165. package/src/lowering/sensor.ts +155 -0
  166. package/src/lowering/spi.ts +164 -124
  167. package/src/lowering/thread.ts +63 -0
  168. package/src/lowering/timing.ts +46 -72
  169. package/src/lowering/uart.ts +73 -51
  170. package/src/lowering/usb.ts +221 -0
  171. package/src/lowering/wdt.ts +95 -84
  172. package/src/lowering/wifi.ts +120 -111
  173. package/src/sdk/board-catalog-sync.ts +62 -0
  174. package/src/strategy.ts +888 -229
  175. package/src/toolchain/bossac-touch.ts +112 -0
  176. package/src/toolchain/compat.ts +8 -3
  177. package/src/toolchain/debug-config.ts +666 -522
  178. package/src/toolchain/env-check.ts +9 -9
  179. package/src/toolchain/index.ts +765 -44
  180. package/src/toolchain/scaffold.ts +81 -7
  181. package/src/toolchain/west-discover.ts +334 -316
  182. package/src/toolchain/west-spawn.ts +17 -5
  183. package/src/async/timer-polyfill.ts +0 -107
  184. package/src/chips/esp32.ts +0 -74
  185. package/src/chips/esp32s3.ts +0 -57
  186. package/src/chips/xiao-ble.ts +0 -70
  187. package/src/lowering/power.ts +0 -70
  188. package/src/lowering/pulse.ts +0 -93
  189. package/src/lowering/tone.ts +0 -60
  190. package/src/lowering/worker-backing.ts +0 -94
  191. package/src/lowering/worker.ts +0 -17
@@ -0,0 +1,227 @@
1
+ // ---------------------------------------------------------------------------
2
+ // Custom board generation — an out-of-tree Zephyr board for an MCU-only target
3
+ //
4
+ // When a project targets an MCU package with no board package (`mcu:` set,
5
+ // `board:` absent) there may be no Zephyr board for the user's hardware. The
6
+ // chip descriptor's `customBoard` block (silicon facts from the MCU package:
7
+ // SoC name, devicetree includes, default console mux, default clock plan) is
8
+ // all a minimal HWMv2 board needs, so we generate one into the project:
9
+ //
10
+ // boards/typecad/<name>/board.yml — board + SoC declaration
11
+ // boards/typecad/<name>/<name>.dts — includes, chosen, console, clocks
12
+ // boards/typecad/<name>/Kconfig.<name> — BOARD_<NAME> symbol selecting the SoC
13
+ // boards/typecad/<name>/<name>_defconfig— baseline (GPIO + console) every
14
+ // in-tree board carries
15
+ //
16
+ // The app's CMakeLists.txt adds the project root to BOARD_ROOT (see
17
+ // toolchain/scaffold.ts), so Zephyr discovers the board without touching the
18
+ // checkout. Everything else (buses, ADC, PWM, USB) stays usage-driven: the
19
+ // overlay generator keeps layering `boards/<name>.overlay` on top exactly as
20
+ // it does for upstream boards.
21
+ //
22
+ // Verified against Zephyr 4.3/4.4 (HWMv2): a minimal board is exactly these
23
+ // four files — Kconfig.<name> + CONFIG_GPIO=y are load-bearing (a board
24
+ // without them fails with "ARCH not defined" / gpio link errors).
25
+ // ---------------------------------------------------------------------------
26
+ import { mkdirSync } from 'node:fs';
27
+ import { join } from 'node:path';
28
+ import { writeIfChanged } from '../toolchain/scaffold.js';
29
+ /** The vendor segment of the generated board path + board.yml. */
30
+ const CUSTOM_BOARD_VENDOR = 'typecad';
31
+ /**
32
+ * Sanitize a raw name into a legal Zephyr board name: lowercase, `[a-z0-9_]`
33
+ * (dashes/dots/spaces collapse to underscores, leading/trailing underscores
34
+ * trimmed). Board names become C identifiers in Kconfig symbols, so anything
35
+ * stricter would break `config BOARD_<NAME>`.
36
+ */
37
+ export function sanitizeBoardName(raw) {
38
+ const cleaned = raw
39
+ .toLowerCase()
40
+ .replace(/[^a-z0-9_]+/g, '_')
41
+ .replace(/^_+|_+$/g, '');
42
+ return cleaned.length > 0 ? cleaned : 'custom_board';
43
+ }
44
+ /** Kconfig symbol fragment: `cb_f411` → `CB_F411`. */
45
+ function kconfigFragment(name) {
46
+ return name.toUpperCase();
47
+ }
48
+ /**
49
+ * Emit the custom board for a chip whose descriptor carries `customBoard`
50
+ * silicon data. No-op (returns null) when the chip has no generator inputs —
51
+ * board-resolved chips don't need one.
52
+ */
53
+ export function generateCustomBoard(projectRoot, chip, rawName) {
54
+ const cb = chip.customBoard;
55
+ if (!cb)
56
+ return null;
57
+ const name = sanitizeBoardName(rawName);
58
+ const soc = cb.socs[0];
59
+ if (!soc)
60
+ return null;
61
+ const dir = join(projectRoot, 'boards', CUSTOM_BOARD_VENDOR, name);
62
+ mkdirSync(dir, { recursive: true });
63
+ let changed = false;
64
+ // ── board.yml ────────────────────────────────────────────────────────────
65
+ changed =
66
+ writeIfChanged(join(dir, 'board.yml'), [
67
+ '# Auto-generated by @typecad/framework-zephyr from the MCU package',
68
+ '# silicon definition. Regenerated on every compile — edit the MCU',
69
+ '# package (or add a board package) instead of this file.',
70
+ 'board:',
71
+ ` name: ${name}`,
72
+ ` full_name: Cuttlefish custom board (${chip.soc})`,
73
+ ` vendor: ${CUSTOM_BOARD_VENDOR}`,
74
+ ' socs:',
75
+ ` - name: ${soc}`,
76
+ '',
77
+ ].join('\n')) || changed;
78
+ // ── Kconfig.<name> — the board symbol selecting the SoC ─────────────────
79
+ // Load-bearing: without the BOARD_<NAME> symbol selecting SOC_<SOC>, Kconfig
80
+ // completes with no SoC and the build dies with "ARCH not defined".
81
+ changed =
82
+ writeIfChanged(join(dir, `Kconfig.${name}`), [
83
+ '# Auto-generated by @typecad/framework-zephyr. Do not edit.',
84
+ '',
85
+ `config BOARD_${kconfigFragment(name)}`,
86
+ `\tselect SOC_${kconfigFragment(soc)}`,
87
+ '',
88
+ ].join('\n')) || changed;
89
+ // ── <name>.dts — includes, chosen, console, clock plan ───────────────────
90
+ const lines = [
91
+ '// Auto-generated by @typecad/framework-zephyr from the MCU package',
92
+ '// silicon definition. Regenerated on every compile — edit the MCU',
93
+ '// package (or add a board package) instead of this file.',
94
+ '/dts-v1/;',
95
+ ...cb.dtsIncludes.map((inc) => `#include <${inc}>`),
96
+ '',
97
+ '/ {',
98
+ ` model = "Cuttlefish custom board (${chip.soc})";`,
99
+ ` compatible = "${CUSTOM_BOARD_VENDOR},${name.replace(/_/g, '-')}";`,
100
+ '',
101
+ ' chosen {',
102
+ ` zephyr,console = &${cb.console.nodeLabel};`,
103
+ ' zephyr,sram = &sram0;',
104
+ ' zephyr,flash = &flash0;',
105
+ ' };',
106
+ '};',
107
+ '',
108
+ ];
109
+ // The chip descriptor's DT-aliased GPIOs (sw0/led0 from the reference
110
+ // board data): the generated firmware addresses them via DT_ALIAS, so the
111
+ // custom board must define the nodes. Pins map through gpioControllers
112
+ // (port-block numbering: pin = minPin + port bit).
113
+ const controllerFor = (pin) => {
114
+ const c = chip.gpioControllers?.find((c) => pin >= c.minPin && pin <= c.maxPin);
115
+ if (c)
116
+ return { label: c.nodelabel, bit: pin - c.minPin };
117
+ return undefined;
118
+ };
119
+ const keys = chip.gpio.dtSpecs.filter((s) => s.dtSpec.startsWith('sw') || s.dtSpec.startsWith('button'));
120
+ const leds = chip.gpio.dtSpecs.filter((s) => s.dtSpec.startsWith('led'));
121
+ if (keys.length > 0 || leds.length > 0) {
122
+ lines.push('/ {');
123
+ if (leds.length > 0) {
124
+ lines.push(' leds {', ' compatible = "gpio-leds";');
125
+ for (const l of leds) {
126
+ const c = controllerFor(l.pin);
127
+ if (!c)
128
+ continue;
129
+ lines.push(` ${l.dtSpec}_node: ${l.dtSpec.replace(/_/g, '-')} {`, ` gpios = <&${c.label} ${c.bit} GPIO_ACTIVE_LOW>;`, ' };');
130
+ }
131
+ lines.push(' };');
132
+ }
133
+ if (keys.length > 0) {
134
+ lines.push(' gpio_keys {', ' compatible = "gpio-keys";');
135
+ for (const k of keys) {
136
+ const c = controllerFor(k.pin);
137
+ if (!c)
138
+ continue;
139
+ lines.push(` ${k.dtSpec}_node: ${k.dtSpec.replace(/_/g, '-')} {`, ` gpios = <&${c.label} ${c.bit} (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;`, ' };');
140
+ }
141
+ lines.push(' };');
142
+ lines.push(' aliases {');
143
+ for (const k of keys)
144
+ lines.push(` ${k.dtSpec} = &${k.dtSpec}_node;`);
145
+ for (const l of leds)
146
+ lines.push(` ${l.dtSpec} = &${l.dtSpec}_node;`);
147
+ lines.push(' };');
148
+ }
149
+ lines.push('};', '');
150
+ }
151
+ lines.push(
152
+ // Console UART — the one controller the board enables unconditionally;
153
+ // every other bus is enabled by the usage-driven overlay.
154
+ `&${cb.console.nodeLabel} {`, ` pinctrl-0 = <&${cb.console.tx} &${cb.console.rx}>;`, ' pinctrl-names = "default";', ' status = "okay";', ` current-speed = <${cb.console.speed}>;`, '};', '');
155
+ // USB: alias the silicon node under the Zephyr convention the descriptor's
156
+ // usb.controller names (e.g. zephyr_udc0: &usbotg_fs), pre-enabled with its
157
+ // pinctrl — the st,stm32-otgfs binding requires pinctrl-0 on an enabled
158
+ // node, and the overlay's CDC composition only flips status/enables the
159
+ // stack (Kconfig-gated), never pins. Mirrors the blackpill board shape.
160
+ if (cb.usbNode && chip.usb) {
161
+ lines.push(`${chip.usb.controller}: &${cb.usbNode} {`, ...(cb.usbPinctrl && cb.usbPinctrl.length > 0
162
+ ? [
163
+ ` pinctrl-0 = <${cb.usbPinctrl.map((p) => `&${p}`).join(' ')}>;`,
164
+ ' pinctrl-names = "default";',
165
+ ]
166
+ : []), ' status = "okay";', '};', '');
167
+ }
168
+ // PWM: the overlay enables `&pwm<N>` and synthesizes pwm-leds consumers
169
+ // against it — but the pwm<N> LABEL only exists when a board DTS declares
170
+ // it (the SoC dtsi ships the timers' pwm child unlabeled), and the
171
+ // st,stm32-pwm binding requires pinctrl-0 on an enabled node. Emit the
172
+ // blackpill-shape block per controller whose specs all carry pinctrl
173
+ // tokens; controllers without full token coverage stay to board packages.
174
+ if (chip.pwm) {
175
+ const byController = new Map();
176
+ for (const s of chip.pwm.specs) {
177
+ if (!s.controller)
178
+ continue;
179
+ const timersMatch = s.controller.match(/^pwm(\d+)$/);
180
+ if (!timersMatch)
181
+ continue;
182
+ const entry = byController.get(s.controller) ?? { n: timersMatch[1], tokens: [], complete: true };
183
+ if (s.pinctrl) {
184
+ entry.tokens.push(s.pinctrl);
185
+ }
186
+ else {
187
+ entry.complete = false;
188
+ }
189
+ byController.set(s.controller, entry);
190
+ }
191
+ for (const [controller, entry] of byController) {
192
+ if (!entry.complete || entry.tokens.length === 0)
193
+ continue;
194
+ lines.push(`&timers${entry.n} {`, ' status = "okay";', '', ` ${controller}: pwm {`, ` pinctrl-0 = <${entry.tokens.map((t) => `&${t}`).join(' ')}>;`, ' pinctrl-names = "default";', ' status = "okay";', ' };', '};', '');
195
+ }
196
+ }
197
+ // ADC: some SoC drivers require board-DTS properties on an enabled adc node
198
+ // (STM32 F4: st,adc-clock-source + st,adc-prescaler, or the binding rejects
199
+ // the node when the usage-driven overlay enables it). Pre-enable with the
200
+ // silicon-required props + a baseline pinctrl; the overlay rewrites
201
+ // pinctrl-0 to the channels the program actually reads.
202
+ if (cb.adcNode) {
203
+ const a = cb.adcNode;
204
+ lines.push(`&${a.nodeLabel} {`, ` pinctrl-0 = <&${a.pinctrl}>;`, ' pinctrl-names = "default";', ` st,adc-clock-source = "${a.clockSource}";`, ` st,adc-prescaler = <${a.prescaler}>;`, ' status = "okay";', '};', '');
205
+ }
206
+ // Clock plan — STM32 F4 shape today (the only family carrying a clocks
207
+ // block so far): HSE crystal, PLL, RCC prescalers. SoCs whose dtsi clocks
208
+ // work out of the box ship no clocks block and emit nothing here.
209
+ const clk = cb.clocks;
210
+ lines.push(`&clk_hse {`, ` clock-frequency = <DT_FREQ_M(${clk.hseMHz})>;`, ' status = "okay";', '};', '', '&pll {', ` div-m = <${clk.pll.divM}>;`, ` mul-n = <${clk.pll.mulN}>;`, ` div-p = <${clk.pll.divP}>;`, ` div-q = <${clk.pll.divQ}>;`, ' clocks = <&clk_hse>;', ' status = "okay";', '};', '', '&rcc {', ' clocks = <&pll>;', ` clock-frequency = <DT_FREQ_M(${clk.sysMHz})>;`, ` ahb-prescaler = <${clk.ahbPrescaler}>;`, ` apb1-prescaler = <${clk.apb1Prescaler}>;`, ` apb2-prescaler = <${clk.apb2Prescaler}>;`, '};', '');
211
+ changed = writeIfChanged(join(dir, `${name}.dts`), lines.join('\n')) || changed;
212
+ // ── <name>_defconfig — the baseline every in-tree board carries ──────────
213
+ // Load-bearing: without CONFIG_GPIO the STM32 pinctrl driver's gpio_ports
214
+ // table references devices that are never built (undefined __device_dts_ord
215
+ // at link). Console/serial match what the DTS enables.
216
+ changed =
217
+ writeIfChanged(join(dir, `${name}_defconfig`), [
218
+ '# Auto-generated by @typecad/framework-zephyr. Do not edit.',
219
+ '',
220
+ 'CONFIG_GPIO=y',
221
+ 'CONFIG_SERIAL=y',
222
+ 'CONFIG_CONSOLE=y',
223
+ 'CONFIG_UART_CONSOLE=y',
224
+ '',
225
+ ].join('\n')) || changed;
226
+ return { dir, name, changed };
227
+ }
@@ -1,3 +1,18 @@
1
+ /** A sensor part's catalog facts, as the exceptions helper consumes them. */
2
+ interface SensorKconfigSource {
3
+ kconfig: readonly string[];
4
+ }
5
+ /**
6
+ * Apply the per-part Kconfig exceptions the generated catalog records, for
7
+ * exactly the parts the program constructs. Lines are CONFIG_<SYM>=<value>
8
+ * strings parsed into the map; a symbol the user set explicitly in
9
+ * zephyr.kconfig is left alone (scaffold.ts already skips user overrides for
10
+ * auto symbols, and Map.set idempotence covers the rest). The lookup is a
11
+ * parameter so tests can drive it with a synthetic catalog.
12
+ */
13
+ export declare function applySensorKconfigExceptions(m: Map<string, string>, parts: readonly {
14
+ part: string;
15
+ }[] | undefined, lookup?: Readonly<Record<string, SensorKconfigSource>>): void;
1
16
  export interface KconfigUsage {
2
17
  usesAdc?: boolean;
3
18
  usesPwm?: boolean;
@@ -7,24 +22,90 @@ export interface KconfigUsage {
7
22
  usesI2c?: boolean;
8
23
  usesSpi?: boolean;
9
24
  usesUart?: boolean;
25
+ /** USB CDC-ACM serial used (usb.* ops). Selects the "next" USB device
26
+ * stack + CDC class; the class instances themselves are composed in the
27
+ * DT overlay and auto-default on once the node exists (assigning the
28
+ * class symbol keeps prj.conf explicit and survives DT-only probes). */
29
+ usesUsb?: boolean;
10
30
  usesWdt?: boolean;
11
31
  usesBle?: boolean;
12
32
  usesDisplay?: boolean;
13
- usesPower?: boolean;
14
33
  usesWifi?: boolean;
15
34
  usesHttp?: boolean;
16
35
  usesMqtt?: boolean;
17
36
  usesPreferences?: boolean;
18
37
  usesRandom?: boolean;
38
+ /** A printf-family format specifier with a float conversion (%f, %.2f, %e,
39
+ * …) appears in the emitted source — Zephyr's cbprintf only links float
40
+ * conversions with FP_SUPPORT (which itself needs the COMPLETE impl). */
41
+ usesFloatFormat?: boolean;
42
+ /** DT-bound sensor parts used (sensor.* ops — the generic catalog). The
43
+ * umbrella under which every driver sensor Kconfig lives (`if SENSOR`);
44
+ * the per-driver symbols default on from their DT node presence. */
45
+ usesSensor?: boolean;
46
+ /** Distinct constructed sensors — only the overlay generator consumes this
47
+ * (one DT child node per entry, on the given I2C controller index); prj.conf
48
+ * ignores it. Same adcReadPins/pwmUsedPins pattern. */
49
+ sensorParts?: readonly {
50
+ part: string;
51
+ busIndex: number;
52
+ port: number;
53
+ busKind: 'i2c' | 'spi';
54
+ spiHz?: number;
55
+ spiMode?: number;
56
+ alertPin?: number;
57
+ }[];
58
+ /** Distinct thin SPI targets (hal/spi-target.ts) — one DT child node per
59
+ * entry (no compatible — a raw spi_dt_spec peer), appended after sensor
60
+ * CS entries in the controller's merged cs-gpios. */
61
+ spiTargets?: readonly {
62
+ busIndex: number;
63
+ cs: number;
64
+ hz?: number;
65
+ mode?: number;
66
+ }[];
19
67
  /** Touch controller referenced (UI touch adapter emits DT_NODELABEL(ft6336u)
20
68
  * or DT_NODELABEL(xpt2046)). Selects the bus driver the node needs. */
21
69
  usesTouch?: boolean;
22
70
  /** Which touch controller the program uses — FT6336U rides I2C, XPT2046
23
71
  * rides the display's SPI bus. Only meaningful with usesTouch. */
24
72
  touchController?: 'ft6336u' | 'xpt2046';
73
+ /** The emitted shim carries the STM32F4 DBGMCU keep-SWD-alive init
74
+ * (__tc_stm32_dbgmcu token). Selects Zephyr's own "debugger attach in
75
+ * stop/sleep" init (sets DBG_STOP via the LL headers); the shim's raw
76
+ * register poke additionally covers DBG_SLEEP, which the Zephyr F4 path
77
+ * does not set. */
78
+ usesStm32DebugSleep?: boolean;
25
79
  /** PSRAM type ('opi' | 'quad') when the target board has PSRAM. Emits the
26
80
  * CONFIG_SPIRAM symbols so the ESP heap serves PSRAM for canvas allocations. */
27
81
  psram?: 'opi' | 'quad';
82
+ /** HAL pin numbers the program reads with adc.* — scanned from the emitted
83
+ * `__tc_adc<N>_setup()` calls at compile time. Only the overlay generator
84
+ * consumes this (to rewrite the ADC node's pinctrl-0 to the used channels
85
+ * on SoCs that need pad muxing, e.g. STM32); prj.conf ignores it. */
86
+ adcReadPins?: readonly number[];
87
+ /** HAL pin numbers the program drives with dac.* — scanned from the
88
+ * emitted lazy-setup guards (__tc_dact<pin>_done) at compile time. Only
89
+ * the overlay generator consumes this (the DAC node's pinctrl-0 lists
90
+ * the used channels); prj.conf ignores it. */
91
+ dacWritePins?: readonly number[];
92
+ /** HAL pin numbers the program drives with pwm.* — scanned from the
93
+ * emitted `__tc_pwm_*` spec references at compile time. Only the overlay
94
+ * generator consumes this (synthesized pwm-leds consumers + aliases are
95
+ * emitted per used pin, so the DT carries no dead channels); prj.conf
96
+ * ignores it. */
97
+ pwmUsedPins?: readonly number[];
98
+ /** Controller indexes per bus the program actually drives — scanned from
99
+ * the emitted `__tc_<bus><N>_dev` state blocks at compile time. The
100
+ * overlay enables only those controllers (an enabled-but-unused
101
+ * controller claims its default pins — e.g. i2c0's GP4/GP5 on the Pico —
102
+ * which a program using the OTHER controller may want as GPIO). Absent
103
+ * (prepare-time overlays, driver-API-only users like the display
104
+ * adapter) → every declared controller is enabled, preserving the old
105
+ * behavior. prj.conf ignores these. */
106
+ i2cUsedInstances?: readonly number[];
107
+ spiUsedInstances?: readonly number[];
108
+ uartUsedInstances?: readonly number[];
28
109
  }
29
110
  /**
30
111
  * Resolve the Kconfig symbol→value map for a prj.conf. Returns a Map preserving
@@ -32,3 +113,4 @@ export interface KconfigUsage {
32
113
  * are always present; driver symbols are usage-gated.
33
114
  */
34
115
  export declare function resolveKconfigFragments(usage: KconfigUsage, debug: boolean): Map<string, string>;
116
+ export {};
@@ -1,12 +1,24 @@
1
- // ---------------------------------------------------------------------------
2
- // Kconfig fragment resolver — extracts the prj.conf symbol logic
3
- //
4
- // Previously inline in scaffold.ts as token-scanning of the emitted source.
5
- // Formalized here as a typed, unit-testable function keyed off a usage-analysis
6
- // object (the same ctx.analysis.usesX flags the strategy uses). scaffold.ts
7
- // calls this instead of inlining the scan, keeping prj.conf generation honest
8
- // and testable.
9
- // ---------------------------------------------------------------------------
1
+ import { SENSOR_PART_INFO } from '@typecad/hal';
2
+ /**
3
+ * Apply the per-part Kconfig exceptions the generated catalog records, for
4
+ * exactly the parts the program constructs. Lines are CONFIG_<SYM>=<value>
5
+ * strings parsed into the map; a symbol the user set explicitly in
6
+ * zephyr.kconfig is left alone (scaffold.ts already skips user overrides for
7
+ * auto symbols, and Map.set idempotence covers the rest). The lookup is a
8
+ * parameter so tests can drive it with a synthetic catalog.
9
+ */
10
+ export function applySensorKconfigExceptions(m, parts, lookup = SENSOR_PART_INFO) {
11
+ if (!parts)
12
+ return;
13
+ for (const sp of parts) {
14
+ for (const line of lookup[sp.part]?.kconfig ?? []) {
15
+ const eq = line.indexOf('=');
16
+ if (eq <= 0)
17
+ continue;
18
+ m.set(line.slice(0, eq), line.slice(eq + 1));
19
+ }
20
+ }
21
+ }
10
22
  /**
11
23
  * Resolve the Kconfig symbol→value map for a prj.conf. Returns a Map preserving
12
24
  * insertion order (callers join with '\n'). Core GPIO + C++ + workqueue symbols
@@ -21,6 +33,22 @@ export function resolveKconfigFragments(usage, debug) {
21
33
  m.set('CONFIG_CONSOLE', 'y');
22
34
  if (usage.usesAdc)
23
35
  m.set('CONFIG_ADC', 'y');
36
+ // Sensors: only the umbrella — each in-tree driver is `default y` on its
37
+ // DT_HAS_<COMPAT>_ENABLED, so the overlay's child node enables the driver.
38
+ if (usage.usesSensor)
39
+ m.set('CONFIG_SENSOR', 'y');
40
+ // The rare driver that is NOT DT-default-on: the catalog records its
41
+ // Kconfig lines (from the driver's own Kconfig) and they are applied for
42
+ // exactly the parts the program constructs. Empty for every in-tree part
43
+ // today — this path is insurance for the next exception.
44
+ applySensorKconfigExceptions(m, usage.sensorParts);
45
+ // Float formatting: cbprintf builds float conversions only under
46
+ // FP_SUPPORT, which depends on the COMPLETE implementation — without these,
47
+ // %f silently prints garbage instead of the value.
48
+ if (usage.usesFloatFormat) {
49
+ m.set('CONFIG_CBPRINTF_COMPLETE', 'y');
50
+ m.set('CONFIG_CBPRINTF_FP_SUPPORT', 'y');
51
+ }
24
52
  if (usage.usesPwm)
25
53
  m.set('CONFIG_PWM', 'y');
26
54
  if (usage.usesDac)
@@ -29,8 +57,37 @@ export function resolveKconfigFragments(usage, debug) {
29
57
  m.set('CONFIG_I2C', 'y');
30
58
  if (usage.usesSpi)
31
59
  m.set('CONFIG_SPI', 'y');
60
+ // USB CDC-ACM serial: the "next" USB device stack + the CDC-ACM class.
61
+ // Symbol names verified against Zephyr 4.3 (subsys/usb/device_next/Kconfig):
62
+ // - USB_DEVICE_STACK_NEXT is the new stack (selects UDC_DRIVER);
63
+ // USB_DEVICE_STACK is the LEGACY stack — deprecated in 4.3 (selects
64
+ // DEPRECATED) and its assignment is what trips the build.
65
+ // - USBD_CDC_ACM_CLASS is the class (note the USBD_ prefix — a bare
66
+ // CDC_ACM_CLASS is an undefined symbol). It depends on SERIAL +
67
+ // DT_HAS_ZEPHYR_CDC_ACM_UART_ENABLED (the overlay's cdc-acm-uart node)
68
+ // and selects UART_INTERRUPT_DRIVEN/RING_BUFFER itself; assigning it
69
+ // keeps prj.conf explicit.
70
+ // - UART_LINE_CTRL gates the driver's line_ctrl_get — usb.connected()
71
+ // polls DTR through it.
72
+ if (usage.usesUsb) {
73
+ m.set('CONFIG_USB_DEVICE_STACK_NEXT', 'y');
74
+ m.set('CONFIG_USBD_CDC_ACM_CLASS', 'y');
75
+ m.set('CONFIG_UART_LINE_CTRL', 'y');
76
+ m.set('CONFIG_SERIAL', 'y');
77
+ // The device presents a serial-number string descriptor sourced from
78
+ // hwinfo (the SoC's unique ID). Without it Windows keys the CDC devnode
79
+ // on the physical USB port: replugs reuse stale nodes and repeated flash
80
+ // cycles wedge them into permanent "access denied" opens. A serial makes
81
+ // the instance path identity-based — stable across ports, immune to the
82
+ // port-keyed ghost pool.
83
+ m.set('CONFIG_HWINFO', 'y');
84
+ }
32
85
  if (usage.usesWdt)
33
86
  m.set('CONFIG_WATCHDOG', 'y');
87
+ // STM32: keep the debugger attachable in sleep/stop (see the shim's
88
+ // DBGMCU init — this covers the Zephyr-side DBG_STOP bit via LL headers).
89
+ if (usage.usesStm32DebugSleep)
90
+ m.set('CONFIG_STM32_ENABLE_DEBUG_SLEEP_STOP', 'y');
34
91
  // Hardware timers via the counter driver.
35
92
  if (usage.usesHwtimer)
36
93
  m.set('CONFIG_COUNTER', 'y');
@@ -87,24 +144,19 @@ export function resolveKconfigFragments(usage, debug) {
87
144
  m.set('CONFIG_SPIRAM_MODE_QUAD', 'y');
88
145
  }
89
146
  }
90
- // deep_sleep_pin wake needs PM + PM_DEVICE.
91
- if (usage.usesPower) {
92
- m.set('CONFIG_PM', 'y');
93
- m.set('CONFIG_PM_DEVICE', 'y');
94
- }
95
147
  if (usage.usesWifi) {
96
148
  // Master networking switch — every CONFIG_NET_* symbol depends on NETWORKING
97
149
  // (without it, Kconfig silently forces them all to n).
98
150
  m.set('CONFIG_NETWORKING', 'y');
99
151
  m.set('CONFIG_WIFI', 'y');
100
- m.set('CONFIG_WIFI_ESP32', 'y'); // ESP32-specific driver (sole WiFi target)
152
+ m.set('CONFIG_WIFI_ESP32', 'y'); // family-wide ESP32 driver (esp32/s3/c3/c6)
101
153
  m.set('CONFIG_NET_L2_ETHERNET', 'y');
102
154
  m.set('CONFIG_NET_IPV4', 'y');
103
155
  m.set('CONFIG_NET_UDP', 'y'); // transitive dep of NET_DHCPV4
104
156
  m.set('CONFIG_NET_DHCPV4', 'y');
105
157
  // NOT CONFIG_NET_CONFIG_SETTINGS: that runs net_config_init() at boot which
106
158
  // BLOCKS up to NET_CONFIG_INIT_TIMEOUT (default 30s) waiting for the iface
107
- // to come up — but our shim brings the iface up itself in setup() (connect),
159
+ // to come up — but our shim brings the iface up itself in main() (connect),
108
160
  // so net_config waits the full 30s, then the dual management of the same
109
161
  // iface crashes the driver. Our shim owns connectivity (net_mgmt connect/
110
162
  // disconnect + conn_mgr monitor for L4), exactly like the standalone Zephyr
@@ -189,13 +241,26 @@ export function resolveKconfigFragments(usage, debug) {
189
241
  // server's RSA cert (rsa:2048) and is widely offered; add more ciphersuites
190
242
  // here to broaden server compatibility.
191
243
  m.set('CONFIG_MBEDTLS_CIPHERSUITE_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256', 'y');
244
+ // Ciphersuites only *depend on* X509_CRT_PARSE_C (they don't select it) —
245
+ // without it sockets_tls.c compiles out mbedtls_x509_crt_parse and every
246
+ // pinned-CA handshake fails with EPERM at connect (the insecure path
247
+ // skips verification, so it works either way).
248
+ m.set('CONFIG_MBEDTLS_X509_CRT_PARSE_C', 'y');
249
+ // KNOWN LIMITATION on this Zephyr tree: pinned-CA (verified TLS) fails at
250
+ // connect — the tf-psa-crypto mbedTLS needs a wider symbol matrix
251
+ // (RSA public-key parse + PEM/DER glue) than the single-ciphersuite
252
+ // select pulls in, and forcing the extra symbols regresses the insecure
253
+ // path. insecure() HTTPS is fully verified; caCert() chain verification
254
+ // stays open until the upstream matrix is mapped.
192
255
  // Handshake/protocol buffers allocate from the mbedTLS heap; MBEDTLS_HEAP_SIZE
193
256
  // must hold ~2x MBEDTLS_SSL_MAX_CONTENT_LEN plus working state. 65000 fits on
194
257
  // the ESP32; mbedTLS requires the full libc and PEM (not DER) cert format.
195
258
  m.set('CONFIG_MBEDTLS_ENABLE_HEAP', 'y');
196
- m.set('CONFIG_MBEDTLS_HEAP_SIZE', '65000');
259
+ // 2x SSL_MAX_CONTENT_LEN record buffers + CA-chain parse state +
260
+ // handshake working memory. 65000 fit insecure-mode handshakes but the
261
+ // pinned-CA path (verification state) overflowed → EPERM at connect.
262
+ m.set('CONFIG_MBEDTLS_HEAP_SIZE', '100000');
197
263
  m.set('CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN', '16384');
198
- m.set('CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT', 'y');
199
264
  m.set('CONFIG_PSA_CRYPTO', 'y');
200
265
  m.set('CONFIG_REQUIRES_FULL_LIBC', 'y');
201
266
  // The http client + TLS handshake are stack-hungry; mirror the wifi bumps
@@ -219,6 +284,12 @@ export function resolveKconfigFragments(usage, debug) {
219
284
  m.set('CONFIG_NET_SOCKETS', 'y');
220
285
  m.set('CONFIG_MQTT_LIB', 'y');
221
286
  m.set('CONFIG_MQTT_LIB_TLS', 'y');
287
+ // The shim getaddrinfo-resolves the broker host — without the DNS
288
+ // resolver linked, getaddrinfo fails numeric AND hostname lookups with
289
+ // EAI_FAIL even while plain HTTP (which selects DNS via its own block)
290
+ // works.
291
+ m.set('CONFIG_DNS_RESOLVER', 'y');
292
+ m.set('CONFIG_DNS_SERVER_IP_ADDRESSES', 'y');
222
293
  // NET_MAX_CONTEXTS / NET_MAX_CONN: same exhaustion risk as HTTP — sequential
223
294
  // connections linger after close. Give the broker session + headroom.
224
295
  m.set('CONFIG_NET_MAX_CONTEXTS', '16');
@@ -230,10 +301,23 @@ export function resolveKconfigFragments(usage, debug) {
230
301
  m.set('CONFIG_MBEDTLS', 'y');
231
302
  m.set('CONFIG_MBEDTLS_BUILTIN', 'y');
232
303
  m.set('CONFIG_MBEDTLS_CIPHERSUITE_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256', 'y');
304
+ // Ciphersuites only *depend on* X509_CRT_PARSE_C (they don't select it) —
305
+ // without it sockets_tls.c compiles out mbedtls_x509_crt_parse and every
306
+ // pinned-CA handshake fails with EPERM at connect (the insecure path
307
+ // skips verification, so it works either way).
308
+ m.set('CONFIG_MBEDTLS_X509_CRT_PARSE_C', 'y');
309
+ // KNOWN LIMITATION on this Zephyr tree: pinned-CA (verified TLS) fails at
310
+ // connect — the tf-psa-crypto mbedTLS needs a wider symbol matrix
311
+ // (RSA public-key parse + PEM/DER glue) than the single-ciphersuite
312
+ // select pulls in, and forcing the extra symbols regresses the insecure
313
+ // path. insecure() HTTPS is fully verified; caCert() chain verification
314
+ // stays open until the upstream matrix is mapped.
233
315
  m.set('CONFIG_MBEDTLS_ENABLE_HEAP', 'y');
234
- m.set('CONFIG_MBEDTLS_HEAP_SIZE', '65000');
316
+ // 2x SSL_MAX_CONTENT_LEN record buffers + CA-chain parse state +
317
+ // handshake working memory. 65000 fit insecure-mode handshakes but the
318
+ // pinned-CA path (verification state) overflowed → EPERM at connect.
319
+ m.set('CONFIG_MBEDTLS_HEAP_SIZE', '100000');
235
320
  m.set('CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN', '16384');
236
- m.set('CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT', 'y');
237
321
  m.set('CONFIG_PSA_CRYPTO', 'y');
238
322
  m.set('CONFIG_REQUIRES_FULL_LIBC', 'y');
239
323
  m.set('CONFIG_NET_TX_STACK_SIZE', '2048');
@@ -271,19 +355,24 @@ export function resolveKconfigFragments(usage, debug) {
271
355
  m.set('CONFIG_FLASH_MAP', 'y');
272
356
  m.set('CONFIG_FILE_SYSTEM', 'y');
273
357
  m.set('CONFIG_FILE_SYSTEM_LITTLEFS', 'y');
358
+ // fs_mkfs (the lazy first-use format in the shim) is gated behind this —
359
+ // without it the mount path links fine but the format call is undefined.
360
+ m.set('CONFIG_FILE_SYSTEM_MKFS', 'y');
274
361
  }
275
362
  // usesUart: the board enables the console UART by default; the overlay (not
276
363
  // Kconfig) is where a UART node would be enabled, so no symbol here.
277
364
  // Random: <zephyr/random/random.h> sys_rand_get is backed by the random
278
- // generator subsystem. CONFIG_RANDOM_GENERATOR is the umbrella that selects a
279
- // working backend for the board (nRF52840 hardware RNG via the entropy
280
- // driver; QEMU/host → the test generator). CONFIG_ENTROPY_GENERATOR is its
281
- // hard dependency on hardware targets. Both default on for most boards, but
282
- // setting them explicitly keeps the symbol set honest and survives a board
283
- // whose defconfig leaves them off.
365
+ // generator subsystem's RNG_GENERATOR_CHOICE. There is no umbrella symbol
366
+ // CONFIG_RANDOM_GENERATOR is a phantom in 4.x (assigning an undefined symbol
367
+ // aborts the build). The choice defaults to the entropy-device generator
368
+ // when the board has a driver (ENTROPY_HAS_DRIVER: nRF52840, ESP32, …);
369
+ // RNG-less SoCs (STM32F411 has no hardware RNG) need
370
+ // CONFIG_TEST_RANDOM_GENERATOR to unlock the timer-clock fallback, which
371
+ // the choice then picks by default. Setting both keys gets the best
372
+ // available source per board.
284
373
  if (usage.usesRandom) {
285
374
  m.set('CONFIG_ENTROPY_GENERATOR', 'y');
286
- m.set('CONFIG_RANDOM_GENERATOR', 'y');
375
+ m.set('CONFIG_TEST_RANDOM_GENERATOR', 'y');
287
376
  }
288
377
  // System workqueue — bumped for worker-offload AND timer callbacks. The
289
378
  // workqueue itself is unconditionally built (no CONFIG_SYSTEM_WORKQUEUE symbol