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

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.
@@ -16,9 +16,12 @@
16
16
  // ---------------------------------------------------------------------------
17
17
  import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync } from 'node:fs';
18
18
  import { join, resolve, dirname, relative } from 'node:path';
19
+ import { ZephyrStrategy } from '../strategy.js';
19
20
  /**
20
- * Resolve the GDB binary path for the target from the build cache. Zephyr
21
- * records ZEPHYR_SDK_INSTALL_DIR in CMakeCache.txt at configure time, and the
21
+ * Resolve the GDB binary path for the target from the build cache, falling
22
+ * back to a filesystem scan of known Zephyr SDK locations when no build
23
+ * exists yet (the create-time starter artifacts path). Zephyr records
24
+ * ZEPHYR_SDK_INSTALL_DIR in CMakeCache.txt at configure time, and the
22
25
  * xtensa GDB lives at <sdk>/xtensa-espressif_esp32s3_zephyr-elf/bin/... (note
23
26
  * the Zephyr-SDK naming, distinct from the ESP-IDF xtensa-esp32s3-elf-gdb).
24
27
  *
@@ -26,27 +29,92 @@ import { join, resolve, dirname, relative } from 'node:path';
26
29
  * omits gdbPath and relies on Cortex-Debug's default resolution).
27
30
  */
28
31
  export function resolveGdbPath(buildDir, target) {
32
+ void target; // toolchain dir is esp32s3-specific today; see gdbPathFromSdkRoot
29
33
  const cachePath = join(buildDir, 'CMakeCache.txt');
30
- if (!existsSync(cachePath))
31
- return undefined;
32
- let sdk = '';
33
- try {
34
- const cache = readFileSync(cachePath, 'utf-8');
35
- const m = cache.match(/^ZEPHYR_SDK_INSTALL_DIR:PATH=(.+)$/m);
36
- if (m)
37
- sdk = m[1].trim();
34
+ if (existsSync(cachePath)) {
35
+ try {
36
+ const cache = readFileSync(cachePath, 'utf-8');
37
+ const m = cache.match(/^ZEPHYR_SDK_INSTALL_DIR:PATH=(.+)$/m);
38
+ if (m) {
39
+ const fromCache = gdbPathFromSdkRoot(m[1].trim());
40
+ if (fromCache)
41
+ return fromCache;
42
+ }
43
+ }
44
+ catch {
45
+ // unreadable cache — fall through to the SDK scan
46
+ }
38
47
  }
39
- catch {
40
- return undefined;
48
+ // No build dir yet (project just created): probe known SDK locations.
49
+ for (const sdkRoot of discoverZephyrSdkRoots()) {
50
+ const p = gdbPathFromSdkRoot(sdkRoot);
51
+ if (p)
52
+ return p;
41
53
  }
42
- if (!sdk)
43
- return undefined;
44
- // The Zephyr SDK toolchain dir is target-specific. For esp32s3 it is
45
- // xtensa-espressif_esp32s3_zephyr-elf (verified against zephyr-sdk-0.17.4).
54
+ return undefined;
55
+ }
56
+ /** The esp32s3 xtensa GDB location inside a Zephyr SDK root (verified against
57
+ * zephyr-sdk-0.17.4). Returns a forward-slash absolute path or undefined. */
58
+ export function gdbPathFromSdkRoot(sdkRoot) {
46
59
  const gdbName = 'xtensa-espressif_esp32s3_zephyr-elf-gdb.exe';
47
- const gdbPath = join(sdk, 'xtensa-espressif_esp32s3_zephyr-elf', 'bin', gdbName);
60
+ const gdbPath = join(sdkRoot, 'xtensa-espressif_esp32s3_zephyr-elf', 'bin', gdbName);
48
61
  return existsSync(gdbPath) ? gdbPath.replace(/\\/g, '/') : undefined;
49
62
  }
63
+ /** Compare two dotted version strings numerically (0.17.10 > 0.17.4). */
64
+ function compareSdkVersions(a, b) {
65
+ const segsOf = (v) => v.split('.').map((s) => parseInt(s, 10) || 0);
66
+ const aa = segsOf(a);
67
+ const bb = segsOf(b);
68
+ for (let i = 0; i < Math.max(aa.length, bb.length); i++) {
69
+ const d = (aa[i] ?? 0) - (bb[i] ?? 0);
70
+ if (d !== 0)
71
+ return d;
72
+ }
73
+ return 0;
74
+ }
75
+ /**
76
+ * Probe the well-known Zephyr SDK install locations, newest version first:
77
+ * 1. $ZEPHYR_SDK_INSTALL_DIR (the var board.cmake reads)
78
+ * 2. <MAMBA_ROOT_PREFIX | ~/micromamba>/zephyr-sdk/zephyr-sdk-<ver> — the
79
+ * @typecad/zephyr-installer layout
80
+ * 3. ~/zephyr-sdk-<ver> — the standalone download layout
81
+ *
82
+ * Only roots that actually contain the esp32s3 GDB are useful to callers;
83
+ * this returns candidate roots (gdbPathFromSdkRoot does the existence check)
84
+ * so tests can inject home/env overrides.
85
+ */
86
+ export function discoverZephyrSdkRoots(opts) {
87
+ const env = opts?.env ?? process.env;
88
+ const home = opts?.home ?? (env.USERPROFILE || env.HOME || '');
89
+ const scanned = [];
90
+ const versionedDirs = (base) => {
91
+ try {
92
+ return readdirSync(base)
93
+ .filter((d) => existsSync(join(base, d)) && d.startsWith('zephyr-sdk-'))
94
+ .map((d) => join(base, d));
95
+ }
96
+ catch {
97
+ return []; // dir absent
98
+ }
99
+ };
100
+ const mambaRoot = env.MAMBA_ROOT_PREFIX || (home ? join(home, 'micromamba') : '');
101
+ if (mambaRoot)
102
+ scanned.push(...versionedDirs(join(mambaRoot, 'zephyr-sdk')));
103
+ if (home)
104
+ scanned.push(...versionedDirs(home));
105
+ // Scanned roots newest version first; the env var stays pinned first
106
+ // (explicit user intent outranks any discovered location).
107
+ scanned.sort((a, b) => {
108
+ const va = a.match(/zephyr-sdk-([\d.]+)/)?.[1] ?? '';
109
+ const vb = b.match(/zephyr-sdk-([\d.]+)/)?.[1] ?? '';
110
+ return compareSdkVersions(vb, va);
111
+ });
112
+ const roots = env.ZEPHYR_SDK_INSTALL_DIR
113
+ ? [env.ZEPHYR_SDK_INSTALL_DIR, ...scanned]
114
+ : scanned;
115
+ // De-duplicate (an env var may repeat a scan hit) preserving order.
116
+ return roots.filter((r, i) => roots.indexOf(r) === i);
117
+ }
50
118
  /**
51
119
  * Resolve the Espressif OpenOCD binary path. The esp32s3 needs the Espressif
52
120
  * OpenOCD fork (openocd-esp32) — not the Zephyr SDK's openocd and not a
@@ -357,3 +425,47 @@ export function writeDebugConfig(o) {
357
425
  const task = buildTask(o);
358
426
  mergeJsonArrayEntry(join(vscodeDir, 'tasks.json'), 'tasks', 'label', task);
359
427
  }
428
+ /**
429
+ * The Zephyr app dir a `cuttlefish create` scaffold produces, relative to the
430
+ * project root: the scaffold fixes entry `./src/main.ts` + outDir `./out`, and
431
+ * the CLI resolves output.outDir against the ENTRY's directory (cli.ts), so
432
+ * the emitted app root — and therefore the ELF, build dir, and .cuttlefish/
433
+ * debug artifacts — always lands at `src/out`. Keep in sync with
434
+ * generateProjectConfig in @typecad/cuttlefish create/init-templates.ts.
435
+ */
436
+ const STARTER_SKETCH_REL = 'src/out';
437
+ /**
438
+ * Create-time starter debug artifacts. Called by the cuttlefish `create` flow
439
+ * (via the package's `writeProjectDebugArtifacts` export) so a fresh project
440
+ * has a working F5 before any build exists:
441
+ *
442
+ * The launch.json's preLaunchTask runs `cuttlefish build --compile --upload
443
+ * --debug`, which builds + flashes AND rewrites this same launch entry (merged
444
+ * by name) with the CMakeCache-resolved gdbPath — so the starter files upgrade
445
+ * themselves on the first debug build.
446
+ *
447
+ * No-ops (returns []) for targets without native GDB support (debugMode() !==
448
+ * 'gdb'); the gdb frame-filter script is skipped (no source map exists yet).
449
+ *
450
+ * Returns the workspace-relative paths written, for CLI reporting.
451
+ */
452
+ export function writeProjectDebugArtifacts(o) {
453
+ if (new ZephyrStrategy().debugMode(o.buildTarget) !== 'gdb')
454
+ return [];
455
+ const workspaceRoot = resolve(o.workspaceRoot);
456
+ const projectRoot = join(workspaceRoot, STARTER_SKETCH_REL);
457
+ writeDebugConfig({
458
+ projectRoot,
459
+ workspaceRoot,
460
+ sketchRel: STARTER_SKETCH_REL,
461
+ target: o.buildTarget ?? '',
462
+ // No build dir exists yet — resolveGdbPath falls back to probing known
463
+ // Zephyr SDK locations so gdbPath is still filled in when possible.
464
+ buildDir: join(projectRoot, 'build'),
465
+ });
466
+ return [
467
+ '.vscode/launch.json',
468
+ '.vscode/tasks.json',
469
+ `${STARTER_SKETCH_REL}/.cuttlefish/openocd.cfg`,
470
+ ];
471
+ }
@@ -59,6 +59,19 @@ export declare function classifyUploadResult(runner: string | undefined, status:
59
59
  * Exported (pure) so the cleansing is unit-testable without spawning west.
60
60
  */
61
61
  export declare function cleanseUploadOutput(runner: string | undefined, status: number | null, output: string): string;
62
+ /**
63
+ * Whether a failed `west build` output carries ninja's `dependency cycle`
64
+ * signature. Zephyr 4.3.99-dev snapshots have a regression
65
+ * (zephyrproject-rtos/zephyr#104757, fixed upstream by the #104784 revert,
66
+ * in v4.4+): after CMake re-runs from a .config change, the build dir's
67
+ * .ninja_deps records an `offsets.h -> offsets.c.obj -> offsets.h` cycle and
68
+ * ninja aborts with `ninja: error: dependency cycle: ...` before compiling
69
+ * anything. The cycle lives in the build dir, not the sources, so compile()
70
+ * recovers by deleting the dir and retrying once.
71
+ *
72
+ * Exported (pure) so the detection is unit-testable without spawning west.
73
+ */
74
+ export declare function isDependencyCycleFailure(output: string): boolean;
62
75
  /**
63
76
  * FrameworkToolchain for Zephyr. Spec §3.5 (mirror of the ESP-IDF toolchain).
64
77
  * The target board is carried via frameworkData.buildTarget; scaffolding
@@ -19,7 +19,7 @@
19
19
  // ---------------------------------------------------------------------------
20
20
  import { spawnSync } from 'node:child_process';
21
21
  import { basename, dirname, join } from 'node:path';
22
- import { readdirSync, readFileSync, mkdirSync, rmSync, existsSync } from 'node:fs';
22
+ import { readdirSync, readFileSync, mkdirSync, rmSync } from 'node:fs';
23
23
  import { parseCompileErrors } from '@typecad/cuttlefish/api/shared';
24
24
  import { scaffoldZephyrProject, writeIfChanged } from './scaffold.js';
25
25
  import { westSpawn, buildEnv } from './west-spawn.js';
@@ -145,6 +145,29 @@ export function cleanseUploadOutput(runner, status, output) {
145
145
  }
146
146
  return output;
147
147
  }
148
+ /**
149
+ * Whether a failed `west build` output carries ninja's `dependency cycle`
150
+ * signature. Zephyr 4.3.99-dev snapshots have a regression
151
+ * (zephyrproject-rtos/zephyr#104757, fixed upstream by the #104784 revert,
152
+ * in v4.4+): after CMake re-runs from a .config change, the build dir's
153
+ * .ninja_deps records an `offsets.h -> offsets.c.obj -> offsets.h` cycle and
154
+ * ninja aborts with `ninja: error: dependency cycle: ...` before compiling
155
+ * anything. The cycle lives in the build dir, not the sources, so compile()
156
+ * recovers by deleting the dir and retrying once.
157
+ *
158
+ * Exported (pure) so the detection is unit-testable without spawning west.
159
+ */
160
+ export function isDependencyCycleFailure(output) {
161
+ return output.includes('dependency cycle');
162
+ }
163
+ /** stdout+stderr of a spawnSync result coerced to one string. Defensive about
164
+ * the buffer form (spawnSync only returns strings when `encoding` is set,
165
+ * which every call site here does — but the coercion costs nothing). */
166
+ function combinedSpawnOutput(result) {
167
+ const so = typeof result.stdout === 'string' ? result.stdout : (result.stdout?.toString() ?? '');
168
+ const se = typeof result.stderr === 'string' ? result.stderr : (result.stderr?.toString() ?? '');
169
+ return so + se;
170
+ }
148
171
  /**
149
172
  * FrameworkToolchain for Zephyr. Spec §3.5 (mirror of the ESP-IDF toolchain).
150
173
  * The target board is carried via frameworkData.buildTarget; scaffolding
@@ -277,6 +300,7 @@ export const Toolchain = {
277
300
  mosi: typeof spiPins?.mosi === 'number' ? spiPins.mosi : undefined,
278
301
  miso: typeof spiPins?.miso === 'number' ? spiPins.miso : undefined,
279
302
  backlightPin: typeof dispCfg.backlightPin === 'number' ? dispCfg.backlightPin : undefined,
303
+ tearingEffectPin: typeof dispCfg.tearingEffectPin === 'number' ? dispCfg.tearingEffectPin : undefined,
280
304
  }
281
305
  : undefined;
282
306
  // Extract touch pin wiring from the config display.touch section so the
@@ -314,6 +338,7 @@ export const Toolchain = {
314
338
  if (usesXpt) {
315
339
  touchWiring = { controller: 'xpt2046', ...(touchWiring ?? {}) };
316
340
  }
341
+ const overlayDiagnostics = [];
317
342
  const overlay = generateOverlay(chip, {
318
343
  usesI2c: uses('i2c_'),
319
344
  usesSpi: uses('spi_'),
@@ -322,7 +347,10 @@ export const Toolchain = {
322
347
  usesTouch: uses('ft6336u') || uses('touch_') || usesXpt,
323
348
  touchController: usesXpt ? 'xpt2046' : 'ft6336u',
324
349
  psram: o.psram,
325
- }, displayProfile, wiring, touchWiring);
350
+ }, displayProfile, wiring, touchWiring, overlayDiagnostics);
351
+ for (const d of overlayDiagnostics) {
352
+ console.warn(`overlay: ${d.message}`);
353
+ }
326
354
  const overlayDir = join(projectRoot, 'boards');
327
355
  mkdirSync(overlayDir, { recursive: true });
328
356
  // Write the board-specific overlay (the one west loads). Zephyr looks for
@@ -336,16 +364,23 @@ export const Toolchain = {
336
364
  // Use a stable build dir so incremental builds reuse the Ninja graph.
337
365
  // west defaults to <projectRoot>/build.
338
366
  const buildDir = join(projectRoot, 'build');
339
- // Nuke the build dir whenever a previous build exists. Zephyr's gen_offset
340
- // flow (offsets.h is generated FROM offsets.c.obj, while gen_offset.h makes
341
- // offsets.c include offsets.h) leaves a permanent `offsets.h ->
342
- // offsets.c.obj -> offsets.h` cycle in the .ninja_deps log after the first
343
- // incremental pass ninja then fails every later build with `dependency
344
- // cycle` even when nothing changed. This is a known Zephyr-on-Windows
345
- // issue; the reliable fix is a pristine build dir per build. Also nukes
346
- // when prj.conf/CMakeLists/overlay changed, so Kconfig symbols and
347
- // generated headers never diverge from a cached graph.
348
- if (configChanged || existsSync(join(buildDir, 'zephyr', 'zephyr.bin'))) {
367
+ // Reuse the build dir across builds so ninja recompiles only the changed
368
+ // app translation units and re-links a pristine configure + the
369
+ // ~280-target Zephyr library rebuild costs minutes on Windows
370
+ // (demo-shadcn measures 69s of ninja wall time, 448s of summed compile
371
+ // work, and every build redid all of it). Nuke it only when the generated
372
+ // config changed (prj.conf / CMakeLists content), the one path that must
373
+ // not reuse a cached graph: Zephyr 4.3.99-dev snapshots carry a
374
+ // regression (zephyrproject-rtos/zephyr#104757, fixed by the #104784
375
+ // revert on 2026-03-03, in v4.4+) where re-running CMake after a .config
376
+ // change records an `offsets.h -> offsets.c.obj -> offsets.h` cycle in
377
+ // .ninja_deps, after which every ninja run fails with `dependency cycle`.
378
+ // Plain source edits never reconfigure CMake, so they cannot trigger it —
379
+ // and the retry after the spawn below self-heals any path that still does.
380
+ // Board switches need no nuke here: `west build` is --pristine=auto by
381
+ // default and recreates the dir itself when -b <board> mismatches the
382
+ // cached board.
383
+ if (configChanged) {
349
384
  try {
350
385
  rmSync(buildDir, { recursive: true, force: true });
351
386
  }
@@ -374,10 +409,26 @@ export const Toolchain = {
374
409
  buildArgs.push(...userCmakeArgs);
375
410
  }
376
411
  const inv = westSpawn(buildArgs, { cwd: projectRoot, encoding: 'utf-8', timeout: BUILD_TIMEOUT_MS });
377
- const result = spawnSync(inv.command, inv.args, inv.options);
412
+ let result = spawnSync(inv.command, inv.args, inv.options);
413
+ // Self-heal the Zephyr 4.3.99 dep-cycle regression (see the nuke comment
414
+ // above): when the cached .ninja_deps carries the cycle, ninja aborts with
415
+ // `dependency cycle` before compiling anything. The cycle lives in the
416
+ // build dir, not the sources — one pristine retry clears it and the build
417
+ // proceeds. On fixed Zephyr (>=4.4) this never fires.
418
+ let pristineRetry = false;
419
+ if (result.status !== 0 && isDependencyCycleFailure(combinedSpawnOutput(result))) {
420
+ try {
421
+ rmSync(buildDir, { recursive: true, force: true });
422
+ }
423
+ catch { /* may not exist */ }
424
+ result = spawnSync(inv.command, inv.args, inv.options);
425
+ pristineRetry = true;
426
+ }
378
427
  const stdout = typeof result.stdout === 'string' ? result.stdout : (result.stdout?.toString() ?? '');
379
428
  const stderr = typeof result.stderr === 'string' ? result.stderr : (result.stderr?.toString() ?? '');
380
- const output = stdout + stderr;
429
+ const output = stdout + stderr + (pristineRetry
430
+ ? '\n[cuttlefish] dependency cycle detected in the cached build dir — retried with a pristine build'
431
+ : '');
381
432
  // Prefix the build log with how west was resolved, for transparency.
382
433
  const header = `Using west via ${inv.install.source}` +
383
434
  (inv.install.zephyrBase ? ` (ZEPHYR_BASE=${inv.install.zephyrBase})` : '') + '\n';
@@ -24,26 +24,35 @@ export function writeIfChanged(filePath, content) {
24
24
  writeFileSync(filePath, content);
25
25
  return true;
26
26
  }
27
+ /**
28
+ * Names of the cuttlefish-emitted C/C++ sources under src/ (top level only,
29
+ * matching the old `src/*.cpp src/*.c` glob; sorted so the generated
30
+ * CMakeLists.txt is stable across readdir orderings). Empty when no sources
31
+ * exist yet (first prepare call).
32
+ */
33
+ function listEmittedSources(srcDir) {
34
+ if (!existsSync(srcDir))
35
+ return [];
36
+ const names = readdirSync(srcDir).filter((name) => name.endsWith('.cpp') || name.endsWith('.c'));
37
+ names.sort();
38
+ return names;
39
+ }
27
40
  /**
28
41
  * Concatenate all emitted source under src/ so the scaffold can detect which
29
42
  * peripherals the program actually uses. The cuttlefish lowering emits
30
43
  * well-known driver API tokens (adc_read, spi_transceive, bt_*, …), so scanning
31
- * the post-transpile source is an authoritative usage signal — and it keeps the
32
- * scaffold self-contained (no need to thread analysis through the toolchain
44
+ * the post-transpile source is an authoritative usage signal — and it keeps
45
+ * the scaffold self-contained (no need to thread analysis through the toolchain
33
46
  * contract). Returns '' when no sources exist yet (first prepare call).
34
47
  */
35
48
  function readEmittedSources(srcDir) {
36
- if (!existsSync(srcDir))
37
- return '';
38
49
  let out = '';
39
- for (const name of readdirSync(srcDir)) {
40
- if (name.endsWith('.cpp') || name.endsWith('.c')) {
41
- try {
42
- out += readFileSync(join(srcDir, name), 'utf8');
43
- }
44
- catch {
45
- // ignore unreadable files
46
- }
50
+ for (const name of listEmittedSources(srcDir)) {
51
+ try {
52
+ out += readFileSync(join(srcDir, name), 'utf8');
53
+ }
54
+ catch {
55
+ // ignore unreadable files
47
56
  }
48
57
  }
49
58
  return out;
@@ -118,8 +127,13 @@ export function scaffoldZephyrProject(projectRoot, debug = false, userKconfig, p
118
127
  };
119
128
  let changed = false;
120
129
  // ── Root CMakeLists.txt ─────────────────────────────────────────────────
121
- // The canonical Zephyr CMake application. GLOB src/*.cpp so future multi-
122
- // file emits are picked up automatically; cuttlefish owns the file contents.
130
+ // The canonical Zephyr CMake application. The emitted source list is
131
+ // explicit (no file(GLOB CONFIGURE_DEPENDS ...)): CONFIGURE_DEPENDS puts a
132
+ // cmake.verify_globs step in the ninja graph that spawns CMake to re-check
133
+ // the glob on every build, and the scaffold already rewrites this file via
134
+ // writeIfChanged whenever the emitted file set changes — which flips
135
+ // configChanged and reconfigures with the new list baked in.
136
+ const sourceFiles = listEmittedSources(srcDir);
123
137
  const cmakeLists = [
124
138
  '# Auto-generated by @typecad/framework-zephyr from cuttlefish.config.ts.',
125
139
  'cmake_minimum_required(VERSION 3.20.0)',
@@ -128,12 +142,18 @@ export function scaffoldZephyrProject(projectRoot, debug = false, userKconfig, p
128
142
  '',
129
143
  'project(zephyr_app)',
130
144
  '',
131
- '# Collect cuttlefish-emitted sources. CONFIGURE_DEPENDS makes CMake re-',
132
- '# check the glob when the source set changes (e.g. the transpiler removes',
133
- '# a stale entry), instead of linking a file list from the last configure.',
134
- 'file(GLOB app_sources CONFIGURE_DEPENDS src/*.cpp src/*.c)',
135
- '',
136
- 'target_sources(app PRIVATE ${app_sources})',
145
+ ...(sourceFiles.length > 0
146
+ ? [
147
+ '# Cuttlefish-emitted sources. This list is regenerated whenever the',
148
+ '# emitted file set changes (the scaffold rewrites CMakeLists.txt).',
149
+ 'target_sources(app PRIVATE',
150
+ ...sourceFiles.map((name) => ` src/${name}`),
151
+ ')',
152
+ ]
153
+ : [
154
+ '# No emitted sources yet — the scaffold regenerates this list on the',
155
+ '# next compile once src/ contains .cpp/.c files.',
156
+ ]),
137
157
  // When PSRAM is configured, define BOARD_HAS_PSRAM so the UI runtime's
138
158
  // PSRAM canvas allocator (ui_create_canvas_best) is compiled in.
139
159
  ...(psram ? ['', '# PSRAM enabled: activate the runtime PSRAM canvas paths.', 'target_compile_definitions(app PRIVATE BOARD_HAS_PSRAM)', ''] : ['']),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typecad/framework-zephyr",
3
- "version": "1.0.0-alpha.12",
3
+ "version": "1.0.0-alpha.13",
4
4
  "description": "TypeCAD framework package for the Zephyr RTOS — west/CMake build, devicetree-driven GPIO",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -38,11 +38,11 @@
38
38
  "test:hw:mqtt": "cd ../../tests/hardware && npm exec -- cuttlefish-test mqtt-client.test.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@typecad/cuttlefish": "1.0.0-alpha.12"
41
+ "@typecad/cuttlefish": "1.0.0-alpha.13"
42
42
  },
43
43
  "devDependencies": {
44
- "@typecad/expect": "1.0.0-alpha.12",
45
- "@typecad/board-xiao-nrf52840": "1.0.0-alpha.12",
44
+ "@typecad/expect": "1.0.0-alpha.13",
45
+ "@typecad/board-xiao-nrf52840": "1.0.0-alpha.13",
46
46
  "typescript": "^5.7.3"
47
47
  },
48
48
  "license": "MIT",
@@ -18,7 +18,7 @@ import { DEFAULT_ZEPHYR_DISPLAY_PROFILE, type ZephyrDisplayProfile } from './pro
18
18
  // and consumers can reach them from the package barrel.
19
19
  export { zephyrUiDisplayAdapter, zephyrDisplayAdapterGenerator } from './ui-adapter.js';
20
20
  export { zephyrTouchAdapter } from './touch-adapter.js';
21
- export { ZEPHYR_DISPLAY_PROFILES, DEFAULT_ZEPHYR_DISPLAY_PROFILE } from './profiles.js';
21
+ export { ZEPHYR_DISPLAY_PROFILES, DEFAULT_ZEPHYR_DISPLAY_PROFILE, BUILT_IN_PROFILES } from './profiles.js';
22
22
  export type { ZephyrDisplayProfile } from './profiles.js';
23
23
 
24
24
  export interface DisplayState {
@@ -114,3 +114,26 @@ export const ZEPHYR_DISPLAY_PROFILES: Record<string, ZephyrDisplayProfile> = {
114
114
  /** The default profile used when resolveDisplayOp is probed without a display.init. */
115
115
  export const DEFAULT_ZEPHYR_DISPLAY_PROFILE: ZephyrDisplayProfile =
116
116
  ZEPHYR_DISPLAY_PROFILES['ili9341-zephyr'];
117
+
118
+ /**
119
+ * The Zephyr profiles mapped to the shared DisplayProfile shape — the single
120
+ * mapping, so no consumer needs to know the DT-binding descriptor layout.
121
+ * The strategy's getProfileRegistry() and the preview's profile-registry
122
+ * loader both consume this (the same role `BUILT_IN_PROFILES` plays in
123
+ * framework-arduino's displays modules).
124
+ */
125
+ export const BUILT_IN_PROFILES: Record<string, import('@typecad/cuttlefish/api/shared').DisplayProfile> =
126
+ Object.fromEntries(
127
+ Object.entries(ZEPHYR_DISPLAY_PROFILES).map(([name, p]) => [
128
+ name,
129
+ {
130
+ driver: p.driver,
131
+ width: p.width,
132
+ height: p.height,
133
+ nativeWidth: p.nativeWidth,
134
+ nativeHeight: p.nativeHeight,
135
+ colorFormat: p.colorFormat,
136
+ rotation: p.rotation ?? 1,
137
+ },
138
+ ]),
139
+ );
@@ -46,6 +46,10 @@ export interface ZephyrDisplayReadbackOptions {
46
46
  scanlineSync?: boolean;
47
47
  /** MISO/SDO GPIO; readback is disabled when it is not explicitly wired. */
48
48
  miso?: number;
49
+ /** Tearing-effect GPIO (panel TE output). When set, panel updates wait for
50
+ * the TE frame pulse instead of GET_SCANLINE readback — no MISO required.
51
+ * The overlay adds te-gpios to the display DT node from this. */
52
+ tearingEffectPin?: number;
49
53
  }
50
54
 
51
55
  export function zephyrUiDisplayAdapter(
@@ -74,6 +78,9 @@ export function zephyrUiDisplayAdapter(
74
78
  // react badly to GSCAN reads. Both an explicit opt-in and an explicit MISO
75
79
  // pin are required before emitting an active synchronization path.
76
80
  const scanlineSync = readback.scanlineSync === true && readback.miso !== undefined;
81
+ // TE (hardware tearing-effect) sync: strictly opt-in via a configured GPIO.
82
+ // Preferred over GET_SCANLINE when wired — no readback traffic, no MISO.
83
+ const tePin = typeof readback.tearingEffectPin === 'number' ? readback.tearingEffectPin : undefined;
77
84
 
78
85
  const includes = [
79
86
  `// --- Zephyr UI display adapter (${profile.driver}) ---`,
@@ -140,6 +147,22 @@ export function zephyrUiDisplayAdapter(
140
147
  `// controller-specific validation are required; otherwise the display stays`,
141
148
  `// on the existing retained/composited path with no extra SPI reads.`,
142
149
  `static const bool __tc_pnl_scanline_sync = ${scanlineSync ? 'true' : 'false'};`,
150
+ `// Tearing-effect (TE) hardware sync: the panel pulses its TE line once`,
151
+ `// per frame. When te-gpios is present on the display DT node, panel`,
152
+ `// updates arm on the TE edge — tear-free writes with no MISO readback.`,
153
+ `#if DT_NODE_HAS_PROP(DT_NODELABEL(${dtLabel}), te_gpios)`,
154
+ `#define __TC_TE_SYNC 1`,
155
+ `static const struct gpio_dt_spec __tc_te =`,
156
+ ` GPIO_DT_SPEC_GET(DT_NODELABEL(${dtLabel}), te_gpios);`,
157
+ `static struct gpio_callback __tc_te_cb;`,
158
+ `static volatile uint32_t __tc_te_count = 0;`,
159
+ `static void __tc_te_isr(const struct device* port, struct gpio_callback* cb, uint32_t pins) {`,
160
+ ` (void)port; (void)cb; (void)pins;`,
161
+ ` __tc_te_count++;`,
162
+ `}`,
163
+ `#else`,
164
+ `#define __TC_TE_SYNC 0`,
165
+ `#endif`,
143
166
  `// Stashed address window from the last setAddrWindow call. The runtime`,
144
167
  `// calls setAddrWindow + writePixels as a matched pair, so we stash the rect`,
145
168
  `// here and consume it in writePixels.`,
@@ -157,6 +180,18 @@ export function zephyrUiDisplayAdapter(
157
180
  // with DT_HAS_ALIAS (the safe primitive for an alias that may be absent —
158
181
  // DT_NODE_HAS_STATUS(DT_ALIAS(...)) is version-dependent when the alias is
159
182
  // missing and can fail the build).
183
+ // TE pin: input + rising-edge interrupt (the ST7796 TE pulse), then tell
184
+ // the controller to drive the line (TEON 0x35, mode 1 = vertical sync only).
185
+ const teInit = tePin !== undefined ? `#if __TC_TE_SYNC
186
+ if (device_is_ready(__tc_te.port)) {
187
+ gpio_pin_configure_dt(&__tc_te, GPIO_INPUT);
188
+ gpio_init_callback(&__tc_te_cb, __tc_te_isr, BIT(__tc_te.pin));
189
+ (void)gpio_add_callback(__tc_te.port, &__tc_te_cb);
190
+ (void)gpio_pin_interrupt_configure_dt(&__tc_te, GPIO_INT_EDGE_RISING);
191
+ __tc_pnl_cmd1(0x35, 0x01); // TEON: TE output = vsync pulse
192
+ }
193
+ #endif` : '';
194
+
160
195
  const blInit = backlightAlias
161
196
  ? `#if DT_HAS_ALIAS(${backlightAlias})\n const struct gpio_dt_spec __bl = GPIO_DT_SPEC_GET(DT_ALIAS(${backlightAlias}), gpios);\n if (device_is_ready(__bl.port)) { gpio_pin_configure_dt(&__bl, GPIO_OUTPUT_ACTIVE); }\n#endif`
162
197
  : '';
@@ -479,6 +514,19 @@ static uint16_t __tc_pnl_read_scanline(void) {
479
514
  // have no safe post-rectangle interval, so they retain the normal single-burst
480
515
  // behavior; the caller's framebuffer still prevents intermediate software frames.
481
516
  static void __tc_pnl_wait_for_safe_rect(int16_t y, int16_t rh) {
517
+ #if __TC_TE_SYNC
518
+ // TE variant: arm on the next frame pulse, then start the burst — the write
519
+ // chases the scan beam from the top of the rect. Bounded so a stuck TE line
520
+ // can never hang the UI loop.
521
+ if (rh < 8 || y < 0) return;
522
+ uint32_t __was = __tc_te_count;
523
+ uint32_t __deadline = k_uptime_get_32() + 25U;
524
+ while (__tc_te_count == __was) {
525
+ if (static_cast<int32_t>(k_uptime_get_32() - __deadline) >= 0) break;
526
+ k_msleep(0);
527
+ }
528
+ return;
529
+ #endif
482
530
  if (!__tc_pnl_scanline_sync || rh < 8 || y < 0) return;
483
531
  int16_t __last = static_cast<int16_t>(y + rh - 1);
484
532
  if (__last >= static_cast<int16_t>(${h} - 2)) return;
@@ -578,8 +626,10 @@ const CuttlefishPanelOps __tc_display_ops = {
578
626
  CuttlefishGFX __tc_display(&__tc_display_ops, nullptr);
579
627
  ${initBlock}
580
628
  // ── display_init (called from setup) ────────────────────────────────────
629
+
581
630
  static inline void display_init() {
582
631
  printk("TC_DISPLAY: device ready\\n");
632
+ ${teInit}
583
633
  ${blInit}
584
634
  gpio_pin_configure_dt(&__tc_pnl_cs, GPIO_OUTPUT);
585
635
  gpio_pin_configure_dt(&__tc_pnl_dc, GPIO_OUTPUT);
@@ -726,5 +776,6 @@ export const zephyrDisplayAdapterGenerator: DisplayAdapterGenerator = (display)
726
776
  return zephyrUiDisplayAdapter(profile, {
727
777
  scanlineSync: display.scanlineSync,
728
778
  miso: display.spiPins?.miso,
779
+ tearingEffectPin: (display as { tearingEffectPin?: number }).tearingEffectPin,
729
780
  });
730
781
  };