@turingcode/cli 0.2.13 → 0.3.4

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 (177) hide show
  1. package/README.md +2 -0
  2. package/dist/cli/create-cli.d.ts +10 -0
  3. package/dist/cli/create-cli.js +12 -1
  4. package/dist/cli/index.d.ts +9 -0
  5. package/dist/cli/index.js +5 -1
  6. package/dist/core/bound-error.d.ts +10 -0
  7. package/dist/core/date.d.ts +12 -0
  8. package/dist/core/exact/expression.d.ts +49 -0
  9. package/dist/core/exact/fraction.d.ts +85 -0
  10. package/dist/core/files.d.ts +44 -0
  11. package/dist/core/files.js +46 -1
  12. package/dist/core/json-shape.d.ts +45 -0
  13. package/dist/core/mcp.d.ts +92 -0
  14. package/dist/core/mcp.js +56 -15
  15. package/dist/core/query.d.ts +69 -0
  16. package/dist/core/report.d.ts +11 -0
  17. package/dist/core/tables/csv.d.ts +18 -0
  18. package/dist/core/tables/markdown.d.ts +22 -0
  19. package/dist/core/tables/types.d.ts +25 -0
  20. package/dist/features/contract/check.d.ts +30 -0
  21. package/dist/features/contract/command.d.ts +12 -0
  22. package/dist/features/contract/command.js +64 -64
  23. package/dist/features/contract/describe.d.ts +22 -0
  24. package/dist/features/contract/diagnostics.d.ts +108 -0
  25. package/dist/features/contract/load.d.ts +58 -0
  26. package/dist/features/contract/model.d.ts +67 -0
  27. package/dist/features/contract/rules.d.ts +137 -0
  28. package/dist/features/debug/clear_breakpoint.d.ts +2 -0
  29. package/dist/features/debug/clear_breakpoint.js +24 -0
  30. package/dist/features/debug/command.d.ts +5 -0
  31. package/dist/features/debug/command.js +66 -0
  32. package/dist/features/debug/continue.d.ts +2 -0
  33. package/dist/features/debug/continue.js +22 -0
  34. package/dist/features/debug/debug_start.d.ts +2 -0
  35. package/dist/features/debug/debug_start.js +22 -0
  36. package/dist/features/debug/debug_stop.d.ts +2 -0
  37. package/dist/features/debug/debug_stop.js +11 -0
  38. package/dist/features/debug/evaluate.d.ts +2 -0
  39. package/dist/features/debug/evaluate.js +48 -0
  40. package/dist/features/debug/get_stack.d.ts +2 -0
  41. package/dist/features/debug/get_stack.js +14 -0
  42. package/dist/features/debug/get_variables.d.ts +2 -0
  43. package/dist/features/debug/get_variables.js +26 -0
  44. package/dist/features/debug/halt.d.ts +2 -0
  45. package/dist/features/debug/halt.js +16 -0
  46. package/dist/features/debug/set_breakpoint.d.ts +2 -0
  47. package/dist/features/debug/set_breakpoint.js +30 -0
  48. package/dist/features/debug/step.d.ts +2 -0
  49. package/dist/features/debug/step.js +23 -0
  50. package/dist/features/install-mods/command.d.ts +6 -0
  51. package/dist/features/install-mods/command.js +18 -0
  52. package/dist/features/install-mods/install.d.ts +23 -0
  53. package/dist/features/install-mods/install.js +48 -0
  54. package/dist/features/install-skills/command.d.ts +7 -0
  55. package/dist/features/install-skills/command.js +22 -0
  56. package/dist/features/install-skills/install.d.ts +13 -0
  57. package/dist/features/install-skills/install.js +29 -0
  58. package/dist/features/probe/command.d.ts +5 -0
  59. package/dist/features/probe/command.js +44 -0
  60. package/dist/features/probe/kill_all_debuggers.d.ts +2 -0
  61. package/dist/features/probe/kill_all_debuggers.js +50 -0
  62. package/dist/features/probe/list_probes.d.ts +2 -0
  63. package/dist/features/probe/list_probes.js +8 -0
  64. package/dist/features/probe/program_project.d.ts +2 -0
  65. package/dist/features/probe/program_project.js +29 -0
  66. package/dist/features/probe/read_fault_status.d.ts +2 -0
  67. package/dist/features/probe/read_fault_status.js +19 -0
  68. package/dist/features/probe/read_memory.d.ts +2 -0
  69. package/dist/features/probe/read_memory.js +36 -0
  70. package/dist/features/probe/read_peripheral.d.ts +2 -0
  71. package/dist/features/probe/read_peripheral.js +58 -0
  72. package/dist/features/probe/reset_probe_usb.d.ts +2 -0
  73. package/dist/features/probe/reset_probe_usb.js +17 -0
  74. package/dist/features/probe/reset_target.d.ts +2 -0
  75. package/dist/features/probe/reset_target.js +16 -0
  76. package/dist/features/probe/target_info.d.ts +2 -0
  77. package/dist/features/probe/target_info.js +34 -0
  78. package/dist/features/project/analysis_static.d.ts +2 -0
  79. package/dist/features/project/analysis_static.js +19 -0
  80. package/dist/features/project/analysis_syntax_check.d.ts +2 -0
  81. package/dist/features/project/analysis_syntax_check.js +21 -0
  82. package/dist/features/project/command.d.ts +5 -0
  83. package/dist/features/project/command.js +64 -0
  84. package/dist/features/project/project_build.d.ts +2 -0
  85. package/dist/features/project/project_build.js +22 -0
  86. package/dist/features/project/project_download_template.d.ts +2 -0
  87. package/dist/features/project/project_download_template.js +34 -0
  88. package/dist/features/project/project_regenerate.d.ts +2 -0
  89. package/dist/features/project/project_regenerate.js +20 -0
  90. package/dist/features/project/project_sync_from_server.d.ts +2 -0
  91. package/dist/features/project/project_sync_from_server.js +13 -0
  92. package/dist/features/project/project_sync_to_server.d.ts +2 -0
  93. package/dist/features/project/project_sync_to_server.js +51 -0
  94. package/dist/features/project/stm32_cubetools_download_drivers.d.ts +2 -0
  95. package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
  96. package/dist/features/status/command.d.ts +2 -0
  97. package/dist/features/status/command.js +6 -0
  98. package/dist/features/status/get_status.d.ts +2 -0
  99. package/dist/features/status/get_status.js +44 -0
  100. package/dist/features/table/check.d.ts +38 -0
  101. package/dist/features/table/command.d.ts +3 -0
  102. package/dist/features/table/read.d.ts +28 -0
  103. package/dist/features/tool/command.d.ts +35 -0
  104. package/dist/lib.d.ts +11 -0
  105. package/dist/lib.js +9 -0
  106. package/dist/stm32/actions/DebugAction.d.ts +63 -0
  107. package/dist/stm32/actions/DebugAction.js +105 -0
  108. package/dist/stm32/actions/DebugActionInfo.d.ts +7 -0
  109. package/dist/stm32/actions/DebugActionInfo.js +9 -0
  110. package/dist/stm32/actions/DebugActionListProbes.d.ts +9 -0
  111. package/dist/stm32/actions/DebugActionListProbes.js +10 -0
  112. package/dist/stm32/actions/DebugActionProgram.d.ts +28 -0
  113. package/dist/stm32/actions/DebugActionProgram.js +79 -0
  114. package/dist/stm32/actions/DebugActionRead.d.ts +38 -0
  115. package/dist/stm32/actions/DebugActionRead.js +59 -0
  116. package/dist/stm32/actions/DebugActionReset.d.ts +11 -0
  117. package/dist/stm32/actions/DebugActionReset.js +9 -0
  118. package/dist/stm32/actions/DebugActionServer.d.ts +7 -0
  119. package/dist/stm32/actions/DebugActionServer.js +9 -0
  120. package/dist/stm32/analysis.d.ts +19 -0
  121. package/dist/stm32/analysis.js +21 -0
  122. package/dist/stm32/build.d.ts +34 -0
  123. package/dist/stm32/build.js +79 -0
  124. package/dist/stm32/context.d.ts +23 -0
  125. package/dist/stm32/context.js +42 -0
  126. package/dist/stm32/daemon.d.ts +63 -0
  127. package/dist/stm32/daemon.js +202 -0
  128. package/dist/stm32/regen.d.ts +68 -0
  129. package/dist/stm32/regen.js +225 -0
  130. package/dist/stm32/session.d.ts +134 -0
  131. package/dist/stm32/session.js +374 -0
  132. package/dist/stm32/sync.d.ts +24 -0
  133. package/dist/stm32/sync.js +72 -0
  134. package/dist/stm32/tool.d.ts +43 -0
  135. package/dist/stm32/tool.js +20 -0
  136. package/dist/stm32/toolCommand.d.ts +50 -0
  137. package/dist/stm32/toolCommand.js +102 -0
  138. package/dist/stm32/utils/discover_tools.d.ts +24 -0
  139. package/dist/stm32/utils/discover_tools.js +121 -0
  140. package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
  141. package/dist/stm32/utils/downloadzip_utils.js +180 -0
  142. package/dist/stm32/utils/faultUtils.d.ts +14 -0
  143. package/dist/stm32/utils/faultUtils.js +69 -0
  144. package/dist/stm32/utils/findElf.d.ts +9 -0
  145. package/dist/stm32/utils/findElf.js +19 -0
  146. package/dist/stm32/utils/hash_utils.d.ts +19 -0
  147. package/dist/stm32/utils/hash_utils.js +28 -0
  148. package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
  149. package/dist/stm32/utils/listSyncFiles.js +98 -0
  150. package/dist/stm32/utils/mcu_utils.d.ts +59 -0
  151. package/dist/stm32/utils/mcu_utils.js +99 -0
  152. package/dist/stm32/utils/project_utils.d.ts +54 -0
  153. package/dist/stm32/utils/project_utils.js +102 -0
  154. package/dist/stm32/utils/svdUtils.d.ts +31 -0
  155. package/dist/stm32/utils/svdUtils.js +30 -0
  156. package/dist/stm32/utils/truncate.d.ts +7 -0
  157. package/dist/stm32/utils/truncate.js +9 -0
  158. package/dist/stm32/utils/usb_utils.d.ts +5 -0
  159. package/dist/stm32/utils/usb_utils.js +23 -0
  160. package/dist/stm32/utils/user_code.d.ts +14 -0
  161. package/dist/stm32/utils/user_code.js +43 -0
  162. package/dist/stm32/utils/utils_mutex.d.ts +15 -0
  163. package/dist/stm32/utils/utils_mutex.js +93 -0
  164. package/dist/stm32/validate.d.ts +16 -0
  165. package/dist/stm32/validate.js +46 -0
  166. package/mods/claude/.claude-plugin/marketplace.json +8 -0
  167. package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
  168. package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +50 -0
  169. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +22 -0
  170. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +19 -0
  171. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +9 -0
  172. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +43 -0
  173. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +42 -0
  174. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +35 -0
  175. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +34 -0
  176. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
  177. package/package.json +11 -3
@@ -0,0 +1,93 @@
1
+ // Probe lock for the debug actions and the debug session: one probe operation at a time, in this process and,
2
+ // once setProbeLockFile was called, across processes through a lock file that holds the owner's pid.
3
+ import { mkdirSync, readFileSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
4
+ import { dirname } from 'node:path';
5
+ let owner; // name of the run in this process holding the probe
6
+ let lockFile; // set by the CLI to <project>/.turingcode/probe.lock
7
+ /**
8
+ * Makes claimProbe also take a lock file, so other processes see the probe as busy.
9
+ * @param file lock file path, e.g. <project>/.turingcode/probe.lock; undefined turns the lock file off
10
+ */
11
+ export function setProbeLockFile(file) {
12
+ lockFile = file;
13
+ }
14
+ /**
15
+ * Tells whether a process is running.
16
+ * @param pid process id
17
+ * @returns true when it exists (also when it belongs to another user)
18
+ */
19
+ function isAlive(pid) {
20
+ try {
21
+ process.kill(pid, 0);
22
+ return true;
23
+ }
24
+ catch (e) {
25
+ return e?.code === 'EPERM';
26
+ }
27
+ }
28
+ /**
29
+ * Takes the lock file for this process.
30
+ * @param name name of the run, written into the file for the busy error of other processes
31
+ * @throws when a running process other than this one holds it
32
+ */
33
+ function claimLockFile(name) {
34
+ const file = lockFile;
35
+ mkdirSync(dirname(file), { recursive: true });
36
+ for (;;) {
37
+ try {
38
+ writeFileSync(file, JSON.stringify({ pid: process.pid, name }), { flag: 'wx' });
39
+ return;
40
+ }
41
+ catch (e) {
42
+ if (e?.code !== 'EEXIST')
43
+ throw e;
44
+ }
45
+ let held = {};
46
+ try {
47
+ held = JSON.parse(readFileSync(file, 'utf8'));
48
+ }
49
+ catch {
50
+ // unreadable or half written: treated as stale
51
+ }
52
+ // shortcut: a reused pid of a dead owner looks alive, so its lock waits for that process; add a start time if that bites
53
+ if (held.pid && held.pid !== process.pid && isAlive(held.pid))
54
+ throw new Error(`The probe is busy with another turingcode run (pid ${held.pid}): ${held.name ?? 'unknown'}`);
55
+ unlinkSync(file); // stale: its process is gone
56
+ }
57
+ }
58
+ /**
59
+ * Takes the probe for a debug action or the debug session; fails if another run holds it.
60
+ * @param name name of the run, shown in the error when a later claim fails
61
+ */
62
+ export function claimProbe(name) {
63
+ if (owner !== undefined)
64
+ throw new Error(`The probe is busy with another debug tool run: ${owner}`);
65
+ if (lockFile)
66
+ claimLockFile(name);
67
+ owner = name;
68
+ }
69
+ /**
70
+ * Releases the probe taken with claimProbe, and the lock file if this process holds it; removes the lock's
71
+ * folder when that leaves it empty.
72
+ */
73
+ export function releaseProbe() {
74
+ owner = undefined;
75
+ if (!lockFile)
76
+ return;
77
+ try {
78
+ if (JSON.parse(readFileSync(lockFile, 'utf8')).pid === process.pid)
79
+ unlinkSync(lockFile);
80
+ }
81
+ catch {
82
+ // no lock file, or not ours
83
+ }
84
+ try {
85
+ // an empty .turingcode/ left outside a project would mark that folder as one.
86
+ // Keep this non-recursive: rmdir removes only an empty folder in one atomic step, so it can never delete
87
+ // project files or another process's lock; a readdir check first would race with that process.
88
+ rmdirSync(dirname(lockFile));
89
+ }
90
+ catch {
91
+ // not empty (other project files, or another process's lock) or already gone
92
+ }
93
+ }
@@ -0,0 +1,16 @@
1
+ import { ToolDef } from './tool.js';
2
+ export declare const PROBE_BUSY = "probe owned by the debug session: run `turingcode debug stop` first";
3
+ /**
4
+ * Checks tool arguments against the tool's schema.
5
+ * @param def tool definition
6
+ * @param args arguments to check
7
+ * @returns the error message, or undefined when the arguments are valid
8
+ */
9
+ export declare function validateArgs(def: ToolDef, args: unknown): string | undefined;
10
+ /**
11
+ * Tells why a valid call must be refused in the current state.
12
+ * @param def tool definition
13
+ * @param debugActive true while a debug session owns the probe
14
+ * @returns the reason, or undefined when the call may run
15
+ */
16
+ export declare function safetyCheck(def: ToolDef, debugActive: boolean): string | undefined;
@@ -0,0 +1,46 @@
1
+ export const PROBE_BUSY = 'probe owned by the debug session: run `turingcode debug stop` first';
2
+ /**
3
+ * Checks tool arguments against the tool's schema.
4
+ * @param def tool definition
5
+ * @param args arguments to check
6
+ * @returns the error message, or undefined when the arguments are valid
7
+ */
8
+ export function validateArgs(def, args) {
9
+ if (typeof args !== 'object' || args === null || Array.isArray(args))
10
+ return 'Arguments must be an object';
11
+ const { properties, required = [] } = def.inputSchema;
12
+ for (const key of required)
13
+ if (!Object.hasOwn(args, key))
14
+ return `Missing argument: ${key}`;
15
+ for (const [key, v] of Object.entries(args)) {
16
+ // own keys only: properties.__proto__ or .constructor would otherwise be found on Object.prototype
17
+ const p = Object.hasOwn(properties, key) ? properties[key] : undefined;
18
+ if (!p)
19
+ return `Unknown argument: ${key}`;
20
+ const okType = p.type === 'integer' ? Number.isInteger(v) : p.type === 'number' ? typeof v === 'number' && Number.isFinite(v) : typeof v === p.type;
21
+ if (!okType)
22
+ return `${key} must be ${p.type}`;
23
+ if (p.enum && !p.enum.includes(v))
24
+ return `${key} must be one of ${p.enum.join(', ')}`;
25
+ if (p.pattern && !new RegExp(p.pattern).test(v))
26
+ return `${key} has an invalid format`;
27
+ if (p.maxLength !== undefined && v.length > p.maxLength)
28
+ return `${key} is longer than ${p.maxLength}`;
29
+ if (p.minimum !== undefined && v < p.minimum)
30
+ return `${key} must be >= ${p.minimum}`;
31
+ if (p.maximum !== undefined && v > p.maximum)
32
+ return `${key} must be <= ${p.maximum}`;
33
+ }
34
+ return undefined;
35
+ }
36
+ /**
37
+ * Tells why a valid call must be refused in the current state.
38
+ * @param def tool definition
39
+ * @param debugActive true while a debug session owns the probe
40
+ * @returns the reason, or undefined when the call may run
41
+ */
42
+ export function safetyCheck(def, debugActive) {
43
+ if (debugActive && def.needsProbe)
44
+ return PROBE_BUSY;
45
+ return undefined;
46
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "turingcode-mods",
3
+ "owner": { "name": "turingcode" },
4
+ "description": "Claude Code mods shipped with the turingcode CLI",
5
+ "plugins": [
6
+ { "name": "turingcode-skills", "source": "./turingcode-skills" }
7
+ ]
8
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "turingcode-skills",
3
+ "version": "0.1.5",
4
+ "description": "Skills for working with the turingcode CLI and STM32 projects."
5
+ }
@@ -0,0 +1,50 @@
1
+ ---
2
+ name: turingcode-cli
3
+ description: Use when running the turingcode CLI on an STM32 project - creating a project, syncing, building, syntax checks or static analysis, regenerating from the `.ioc`, changing pins, clocks or peripherals through server tools, flashing, reading the board's registers, or debugging with breakpoints. Not for how to write the C/C++ code itself.
4
+ ---
5
+
6
+ # turingcode for STM32
7
+
8
+ `turingcode` syncs a local STM32 (CubeMX/CMake) project with the Turing Code server, builds it
9
+ there, and drives the board through the local debug probe. Run any command with `--help` for its
10
+ flags. Start with `turingcode status`: it shows the project, token, last build, debug session and
11
+ the installed probe tools. Server commands need `TURINGCODE_TOKEN`: set it in the environment, or
12
+ put `TURINGCODE_TOKEN=<token>` in a git-ignored `.env` in the folder you run `turingcode` from (a set
13
+ variable wins). Never print the token or commit the `.env`.
14
+
15
+ ## Load the reference that matches the task
16
+
17
+ Read the file before you run commands in that area.
18
+
19
+ | Read | When the task is |
20
+ |---|---|
21
+ | `references/new-project.md` | creating a project: identifying the chip, picking the CubeMX name, downloading the template |
22
+ | `references/build-and-check.md` | syncing, building, pulling, regenerating from the `.ioc`, restoring drivers, syntax checks, static analysis |
23
+ | `references/board-and-debug.md` | flashing, resetting, reading registers or memory, decoding faults, recovering a probe, breakpoints and stepping |
24
+
25
+ ## Edit
26
+
27
+ In generated files (main.c, stm32*_it.c, ...) write code only between `/* USER CODE BEGIN <name> */` and
28
+ `/* USER CODE END <name> */`; CubeMX deletes everything else on `project regen`. Never change, move or delete
29
+ the markers, and keep code the section already holds.
30
+ `project sync` stops on unmatched markers and warns about code outside the sections.
31
+
32
+ ## Server tools
33
+
34
+ Pins, clocks, .ioc edits, the knowledge base and register lookup are server tools:
35
+
36
+ - `turingcode tool list` to find one, `turingcode tool describe <name>` for its arguments,
37
+ - `turingcode tool call <name> --args '<json>' > result.md` to run it.
38
+
39
+ After a tool changes the server copy of the project, run `project pull` (or `project regen`).
40
+
41
+ ## Typical loop
42
+
43
+ 1. Edit the code, then `turingcode project sync -b` and fix the reported errors;
44
+ `turingcode project sync -b -a` before finishing a change.
45
+ 2. `turingcode probe program` to flash.
46
+ 3. Confirm it runs without looking at the board: read the register the program changes a few times
47
+ with `probe read` and check it changes as expected. E.g. a blinking LED on PB6 of an STM32F3:
48
+ GPIOB ODR is `0x48000414`, and `probe read --address 0x48000414 --length 4` should alternate
49
+ between `00000040` and `00000000` (look up the base address and ODR offset for your chip).
50
+ 4. On a crash, `probe faults`, or `debug start` with breakpoints to find the cause.
@@ -0,0 +1,22 @@
1
+ # Flash, inspect and debug the board
2
+
3
+ | Command | Use it to |
4
+ | --- | --- |
5
+ | `turingcode probe list` | See the connected probes (ST-LINK, J-Link, CMSIS-DAP). |
6
+ | `turingcode probe info` | Identify the connected chip and its matching CubeMX names; works outside a project. |
7
+ | `turingcode probe program` | Flash the newest .elf in `build/`, verify and reset. |
8
+ | `turingcode probe reset` | Hardware-reset the target. |
9
+ | `turingcode probe peripheral --name GPIOA` | Read and decode a peripheral's registers without halting. The register layout is downloaded once per peripheral (needs the token), then works offline. |
10
+ | `turingcode probe read --address 0x20000000 --length 64` | Read raw memory (max 4096 bytes). |
11
+ | `turingcode probe faults` | Decode the Cortex-M fault registers after a HardFault. |
12
+ | `turingcode probe reset-usb`, `turingcode probe kill-debuggers` | Recover a stuck probe or one held by another debugger. |
13
+
14
+ ## Debug
15
+
16
+ `turingcode debug start` opens a probe-rs session that lasts across commands until `debug stop`;
17
+ probe commands that need the probe are refused while it runs.
18
+
19
+ - `debug break --file Core/Src/main.c --line 120 [--condition 'count > 3']`, `debug clear ...`
20
+ - `debug continue`, `debug halt`, `debug step [--kind over|into|out]`
21
+ - `debug stack`, `debug vars`, `debug eval` on the halted target
22
+ - `debug stop` to release the probe
@@ -0,0 +1,19 @@
1
+ # Sync, build and check
2
+
3
+ | Command | Use it to |
4
+ | --- | --- |
5
+ | `turingcode project sync [-b [preset]] [-a [profile]]` | Sync local changes to the server; `-b`/`--build` then builds (default Debug), `-a`/`--analyse` then runs static analysis (default quick). Run before every build. |
6
+ | `turingcode project build [--preset Debug]` | Build on the server and download the .elf into `build/<preset>/`. Prints a short result (own-code warnings, sizes, clock audit summary); a failed build prints everything, `--verbose` always does. |
7
+ | `turingcode project pull` | Bring server-side changes (e.g. after a server tool edited the .ioc) back to the local files. |
8
+ | `turingcode project regen` | Regenerate the code from the .ioc with CubeMX after pin, clock or peripheral changes, then pull. |
9
+ | `turingcode project drivers` | Restore missing `Drivers/` files (CMSIS, HAL). |
10
+ | `turingcode project template` | Download a new barebone project for an MCU. Only when the user asks for a new project. |
11
+
12
+ ## Check
13
+
14
+ Both check the synced server copy, so run `project sync` first. They print `file:line:col: severity: message` lines.
15
+
16
+ | Command | Use it to |
17
+ | --- | --- |
18
+ | `turingcode project check [--files Core/Src/main.c,App]` | Syntax-check files or folders (default: `Core`, `App`). Fast check after an edit. |
19
+ | `turingcode project analyze [--profile quick\|full\|misra] [--paths Core/Src]` | Static analysis of the project's own code (never `Drivers/`): `quick` cppcheck, `full` + clang-tidy, `misra` MISRA C:2012. |
@@ -0,0 +1,9 @@
1
+ # New project
2
+
3
+ 1. `turingcode probe info` identifies the connected chip (device ID, name, flash size, core) and lists the
4
+ CubeMX names that match it, e.g. `STM32F303CCTx, STM32F303RCTx, ...`. Don't call probe-rs or
5
+ STM32_Programmer_CLI yourself.
6
+ 2. Pick one name. The package (pin count) can't be read over SWD: ask the user, or pick one that has the
7
+ pins they need. Names differing only in package build the same code for pins they share.
8
+ 3. `turingcode project template --mcu <name>` downloads the project into `./<name>/`; `cd` into it,
9
+ every project command runs from the project folder (copy the `.env` there too if you use one).
@@ -0,0 +1,43 @@
1
+ ---
2
+ name: turingcode-firmware-code
3
+ description: Coding rules for embedded C/C++ firmware (STM32, HAL, FreeRTOS/CMSIS-RTOS2). Use whenever writing, editing or reviewing .c, .cpp, .h or .hpp files for a microcontroller - sensor drivers, ISRs and HAL callbacks, DMA, UART/SPI protocols, RTOS tasks, flash storage, filters, register maps.
4
+ ---
5
+
6
+ # Embedded C/C++ rules
7
+
8
+ When a rule and speed conflict, the rule wins.
9
+
10
+ ## Load the reference that matches the task
11
+
12
+ Read the file before you write code in that area, not after.
13
+
14
+ | Read | When the change touches |
15
+ |---|---|
16
+ | `references/input-and-bounds.md` | parsing UART/SPI/host frames, lengths, indices, register writes from a host, array or page bounds |
17
+ | `references/concurrency.md` | ISRs, HAL callbacks, DMA, semaphores/mutexes, buffers shared between tasks or with an ISR, read-and-clear flags |
18
+ | `references/init-and-timing.md` | boot order, flash-backed config, driver (re)init, timer callbacks, µs time, `extern "C"` interfaces |
19
+ | `references/registers-and-units.md` | sensor drivers, datasheet registers, bit fields, units, signedness, macros |
20
+
21
+ ## House style
22
+
23
+ Match this; don't "modernize" it.
24
+
25
+ - **Classes**: PascalCase joined with underscores, prefixed by layer or role: `Sensor_Driver`, `Util_Mutex`, `Filter_MovingAvg`.
26
+ - **Members**: private fields start with `_`; internal helpers start with `__`. Always access members with `this->`.
27
+ - **Methods**: snake_case for new code. Existing names in another case stay as they are; don't rename them on the side.
28
+ - **Errors**: return a status enum, not `bool` or raw ints. Accumulate with `status |= ...` so the worst result wins.
29
+ - **Comments**: `/* ---- Title ---- */` section banners. `/** */` blocks explain why, physics and units, and cite the datasheet. `////` marks code switched off for a short time.
30
+ - **Compile-time choices**: templates and `if constexpr` for sizes and variants. Project selection goes through typedefs in a config header. Runtime feature switches are register or config bits.
31
+ - **Layering**: reusable code goes in a shared library; project code only adds what's chip- or product-specific. C code (`main.c`) reaches C++ only through a thin `extern "C"` interface.
32
+ - Never edit vendor code (HAL, CMSIS, generated code outside `USER CODE` blocks).
33
+
34
+ ## Rules for all code
35
+
36
+ 1. **Never drop a status.** Check or accumulate every status, `HAL_StatusTypeDef` and RTOS return (`xTaskCreate`, semaphore acquire, queue send). Don't return a hard-coded OK after fallible steps. If ignoring one is truly fine, say why in one comment.
37
+ 2. **Every wait has a timeout and an error path.** No bare `while(1)` on hardware (ID checks, waiting on a bit, ready flags), and no retry count where 0 means "forever". Return an error and make the failure visible; a missing peripheral must not hang boot silently.
38
+ 3. **Memory budget.** No `new`/`delete` in periodic or per-transaction paths; preallocate buffers and matrices as members or statics. Keep KB-sized objects (large filter windows, matrices, arrays) off task stacks, and size each stack from what it holds.
39
+ 4. **Speed tweaks get tested.** Before committing a performance change to a read or parse path, test it on hardware and check that the success branch still runs on success.
40
+
41
+ ## Before you finish
42
+
43
+ Re-read the diff against the rules above and against the checklist at the end of each reference file you loaded.
@@ -0,0 +1,42 @@
1
+ # Concurrency: ISRs, DMA, tasks
2
+
3
+ ## Release on every path
4
+
5
+ If a function takes a semaphore or mutex and then starts a transfer, release it when the start fails too. Otherwise the next wait blocks forever:
6
+ ```cpp
7
+ if (HAL_UART_Transmit_DMA(...) != HAL_OK) {
8
+ this->_tx_sem.release();
9
+ return Status::ERROR;
10
+ }
11
+ ```
12
+ Error callbacks only release what belongs to that error. An RX overrun must not release the TX semaphore while TX DMA is still running.
13
+
14
+ ## DMA buffers outlive the transfer
15
+
16
+ - A buffer handed to DMA is static, a class member, or from a pool, never a local array. The stack frame is gone while DMA is still reading it.
17
+ - Check the length against the DMA buffer size before copying into it.
18
+ - Outgoing bytes (command, address) go in the TX buffer, not the RX one.
19
+
20
+ ## Shared data has one owner and one protection
21
+
22
+ For every buffer touched by an ISR and a task, or by two tasks, write a comment saying who writes it, who reads it and what protects it. Then pick one of these:
23
+
24
+ - **ISR-filled queue:** the task wraps compaction and clearing in a critical section (or disables that IRQ), or the queue is a lock-free single-producer/single-consumer ring.
25
+ - **Sample written by one task while another parses it:** read into a temp buffer, then copy or swap under the lock. Never write straight into the buffer being parsed, or you get torn samples.
26
+ - **Driver used by a running task:** don't re-init or calibrate it while that task is still using it (see `init-and-timing.md`).
27
+
28
+ ## Read-and-clear flags are consumed once
29
+
30
+ A "consume" call returns a flag and clears it. Call it once per flag per cycle and keep the result:
31
+ ```cpp
32
+ const bool acc_changed = this->acc->consume_changed();
33
+ const bool gyro_changed = this->gyro->consume_changed(); // its own flag
34
+ ```
35
+ A second call on the same flag always returns false.
36
+
37
+ ## Checklist
38
+ - [ ] Every acquire has a release on the failure path
39
+ - [ ] Error callbacks only release their own resources
40
+ - [ ] No DMA buffer on the stack; lengths checked against the DMA buffer
41
+ - [ ] Every shared buffer has a documented owner and lock
42
+ - [ ] Each read-and-clear flag is consumed exactly once per cycle
@@ -0,0 +1,35 @@
1
+ # Initialization and timing
2
+
3
+ ## Order is explicit
4
+
5
+ - Nothing reads a store before it's initialized: init the flash driver before loading config from it. If the load fails, use defaults, not whatever `new` left in memory (it isn't zeroed).
6
+ - Erased flash reads 0xFF, and that isn't a valid config: every bit on, every scale at its maximum. Detect it with a magic number, version or CRC, and fall back to safe defaults.
7
+ - Leave the flash locked after init. Unlock only around an erase or program.
8
+
9
+ ## Callbacks may fire before your objects exist
10
+
11
+ HAL and ISR callbacks (EXTI, UART error, timer) can run before the task that creates the global object, for example when the interrupt is enabled in generated init code. Null-check every global pointer they use:
12
+ ```cpp
13
+ if (driver == nullptr) return;
14
+ ```
15
+
16
+ ## Re-init
17
+
18
+ Before re-initializing or calibrating a driver:
19
+ 1. Stop or pause the tasks that use it.
20
+ 2. Clear its ready flag first.
21
+ 3. Set the flag again only when init succeeds.
22
+
23
+ ## Timers
24
+
25
+ - Pass the correct handle to every timer helper. Helpers that compare handles return early on a mismatch, and the event is lost.
26
+ - Declare every C-callable function in its header. Otherwise C compiles an implicit declaration, and the function gets garbage arguments without any error.
27
+ - A 32-bit µs counter wraps after about 71.6 min. Extend it with overflow counting, and test the wrap.
28
+ - Names match types: `delay_us(time_us_t us)`. Don't cast ns into a µs type without converting.
29
+
30
+ ## Checklist
31
+ - [ ] Stores are initialized before they're read; failed loads fall back to defaults
32
+ - [ ] Blank flash is detected
33
+ - [ ] Callbacks null-check globals
34
+ - [ ] Re-init stops users and clears the ready flag first
35
+ - [ ] Timer callbacks pass the right handle; C interfaces are declared in headers
@@ -0,0 +1,34 @@
1
+ # Input and bounds
2
+
3
+ Bytes from UART, SPI, flash or a host are untrusted. One bad length byte can let the host write anywhere in memory.
4
+
5
+ ## Check before you use
6
+
7
+ Before using a received value as a length, index or offset:
8
+
9
+ - Check it against the **real** buffer size, not a "max expected" constant. A `memcpy` or search over the max length reads past a shorter buffer.
10
+ - Guard unsigned subtraction:
11
+ ```cpp
12
+ if (recv_len < HEADER_LEN) return Status::OUT_OF_RANGE;
13
+ size_t data_len = recv_len - HEADER_LEN;
14
+ ```
15
+ - Bound every write into a table (registers, config) against its count, for every element (`start + i < count`), not only the first.
16
+ - Keep these checks compiled in. Don't put them behind a debug-only macro.
17
+
18
+ ## Bounds are `>=`, not `>`
19
+
20
+ For a count N, valid indices are `0..N-1`:
21
+ ```cpp
22
+ if (page >= PAGE_COUNT) return Status::OUT_OF_RANGE;
23
+ ```
24
+ Don't rely on hardware masking (a value silently truncated by a register field) to make a wrong value work. Compute the value the hardware actually expects.
25
+
26
+ ## Frames that never complete
27
+
28
+ If a protocol buffer fills up with a start marker but no end marker, drop it and resync. A buffer that stays full stops RX for good.
29
+
30
+ ## Checklist
31
+ - [ ] Every external length, index and offset is checked against the real size
32
+ - [ ] No unsigned subtraction without a guard
33
+ - [ ] Index checks use `>=`
34
+ - [ ] A full buffer without a valid frame is dropped
@@ -0,0 +1,35 @@
1
+ # Registers, units and macros
2
+
3
+ ## Read the datasheet, then copy it exactly
4
+
5
+ - Check every bit position against the datasheet. A reset bit isn't bit 0 just because it's listed first.
6
+ - Read two's-complement registers (trim values, temperature) into signed types (`int8_t`), as the datasheet says.
7
+ - Keep the datasheet formula and section in a `/** */` comment next to the code that uses it.
8
+
9
+ ## Bit edits clear before they set
10
+
11
+ To change a field, clear it and then set it:
12
+ ```cpp
13
+ reg = (reg & ~ODR_MASK) | (odr << ODR_POS);
14
+ ```
15
+ Don't just OR a new value into a register that already holds the old one.
16
+
17
+ ## Units are part of the name
18
+
19
+ - Put the unit in the name or in the comment next to every constant: `ref_uT`, `dt_us`, `heading_rad`.
20
+ - A fallback or reference constant uses the sensor's real output unit (a reference in gauss is wrong for a sensor that outputs µT).
21
+ - Noise and scale conversions keep their units consistent: rad vs deg, magnitude vs magnitude squared.
22
+
23
+ ## Macros are fully parenthesized
24
+
25
+ ```cpp
26
+ #define BUF_SIZE (3 * sizeof(float))
27
+ #define CLIP_U16(x) ((x) > 0xFFFF ? 0xFFFF : (x))
28
+ ```
29
+ Prefer `constexpr` constants and `inline` functions over macros when no token pasting is needed. Derive layout constants (group lengths, offsets) from one source, so nothing is counted twice.
30
+
31
+ ## Checklist
32
+ - [ ] Bit positions and signedness match the datasheet
33
+ - [ ] Field writes clear the old value first
34
+ - [ ] Every constant states its unit, and the unit matches the sensor
35
+ - [ ] Macro bodies and arguments are parenthesized
package/package.json CHANGED
@@ -1,13 +1,20 @@
1
1
  {
2
2
  "name": "@turingcode/cli",
3
- "version": "0.2.13",
3
+ "version": "0.3.4",
4
4
  "description": "Turing Code toolkit for AI agents: check Markdown/CSV tables against table contracts, e.g. `turingcode table check`.",
5
5
  "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/lib.d.ts",
9
+ "default": "./dist/lib.js"
10
+ }
11
+ },
6
12
  "bin": {
7
13
  "turingcode": "dist/cli/index.js"
8
14
  },
9
15
  "files": [
10
- "dist"
16
+ "dist",
17
+ "mods"
11
18
  ],
12
19
  "engines": {
13
20
  "node": ">=18"
@@ -23,6 +30,7 @@
23
30
  "access": "public"
24
31
  },
25
32
  "dependencies": {
33
+ "diff": "9.0.0",
26
34
  "sift": "17.1.4",
27
35
  "yargs": "17.7.3"
28
36
  },
@@ -31,4 +39,4 @@
31
39
  "@types/yargs": "17.0.35",
32
40
  "typescript": "7.0.2"
33
41
  }
34
- }
42
+ }