@turingcode/cli 0.3.4 → 0.3.6
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.
- package/dist/cli/create-cli.js +6 -4
- package/dist/cli/index.js +1 -1
- package/dist/core/exact/fraction.js +2 -0
- package/dist/core/files.js +2 -2
- package/dist/core/json-shape.js +1 -0
- package/dist/core/query.js +2 -2
- package/dist/core/tables/csv.d.ts +13 -2
- package/dist/core/tables/csv.js +54 -25
- package/dist/core/tables/markdown.js +8 -2
- package/dist/features/contract/diagnostics.d.ts +3 -0
- package/dist/features/contract/diagnostics.js +7 -4
- package/dist/features/contract/load.js +4 -4
- package/dist/features/contract/rules.d.ts +12 -0
- package/dist/features/contract/rules.js +31 -4
- package/dist/features/debug/clear_breakpoint.d.ts +1 -0
- package/dist/features/debug/clear_breakpoint.js +1 -1
- package/dist/features/debug/command.js +2 -2
- package/dist/features/debug/continue.d.ts +1 -0
- package/dist/features/debug/continue.js +2 -2
- package/dist/features/debug/debug_start.d.ts +1 -0
- package/dist/features/debug/debug_start.js +2 -2
- package/dist/features/debug/debug_stop.d.ts +1 -0
- package/dist/features/debug/debug_stop.js +1 -1
- package/dist/features/debug/evaluate.d.ts +1 -0
- package/dist/features/debug/evaluate.js +5 -5
- package/dist/features/debug/get_stack.d.ts +1 -0
- package/dist/features/debug/get_stack.js +1 -1
- package/dist/features/debug/get_variables.d.ts +1 -0
- package/dist/features/debug/get_variables.js +2 -2
- package/dist/features/debug/halt.d.ts +1 -0
- package/dist/features/debug/halt.js +1 -1
- package/dist/features/debug/set_breakpoint.d.ts +1 -0
- package/dist/features/debug/set_breakpoint.js +1 -1
- package/dist/features/debug/step.d.ts +1 -0
- package/dist/features/debug/step.js +2 -2
- package/dist/features/install-mods/install.js +4 -3
- package/dist/features/install-skills/install.d.ts +1 -0
- package/dist/features/install-skills/install.js +1 -1
- package/dist/features/probe/command.d.ts +1 -1
- package/dist/features/probe/command.js +103 -35
- package/dist/features/probe/kill_all_debuggers.d.ts +1 -0
- package/dist/features/probe/kill_all_debuggers.js +5 -5
- package/dist/features/probe/list_probes.d.ts +1 -0
- package/dist/features/probe/list_probes.js +1 -1
- package/dist/features/probe/program_project.d.ts +1 -0
- package/dist/features/probe/program_project.js +3 -3
- package/dist/features/probe/read_fault_status.d.ts +1 -0
- package/dist/features/probe/read_fault_status.js +1 -1
- package/dist/features/probe/read_memory.d.ts +1 -0
- package/dist/features/probe/read_memory.js +2 -2
- package/dist/features/probe/read_peripheral.d.ts +1 -0
- package/dist/features/probe/read_peripheral.js +7 -5
- package/dist/features/probe/reset_probe_usb.d.ts +1 -0
- package/dist/features/probe/reset_probe_usb.js +1 -1
- package/dist/features/probe/reset_target.d.ts +1 -0
- package/dist/features/probe/reset_target.js +1 -1
- package/dist/features/probe/target_info.d.ts +1 -0
- package/dist/features/probe/target_info.js +1 -1
- package/dist/features/project/analysis_static.d.ts +1 -0
- package/dist/features/project/analysis_static.js +1 -1
- package/dist/features/project/analysis_syntax_check.d.ts +1 -0
- package/dist/features/project/analysis_syntax_check.js +1 -1
- package/dist/features/project/command.js +3 -8
- package/dist/features/project/project_download_template.d.ts +1 -0
- package/dist/features/project/project_download_template.js +4 -4
- package/dist/features/project/project_sync_to_server.js +2 -2
- package/dist/features/project/stm32_cubetools_download_drivers.d.ts +1 -0
- package/dist/features/project/stm32_cubetools_download_drivers.js +2 -2
- package/dist/features/status/get_status.d.ts +1 -0
- package/dist/features/status/get_status.js +1 -1
- package/dist/features/table/check.js +5 -5
- package/dist/features/table/read.js +9 -9
- package/dist/features/tool/command.d.ts +2 -2
- package/dist/features/tool/command.js +95 -66
- package/dist/lib.d.ts +4 -0
- package/dist/lib.js +4 -2
- package/dist/stm32/actions/DebugAction.d.ts +4 -2
- package/dist/stm32/actions/DebugAction.js +6 -5
- package/dist/stm32/actions/DebugActionInfo.d.ts +4 -0
- package/dist/stm32/actions/DebugActionInfo.js +4 -2
- package/dist/stm32/actions/DebugActionListProbes.d.ts +1 -0
- package/dist/stm32/actions/DebugActionListProbes.js +1 -1
- package/dist/stm32/actions/DebugActionProgram.d.ts +8 -2
- package/dist/stm32/actions/DebugActionProgram.js +9 -7
- package/dist/stm32/actions/DebugActionRead.d.ts +8 -7
- package/dist/stm32/actions/DebugActionRead.js +5 -3
- package/dist/stm32/actions/DebugActionReset.d.ts +2 -1
- package/dist/stm32/actions/DebugActionReset.js +1 -1
- package/dist/stm32/actions/DebugActionServer.d.ts +4 -0
- package/dist/stm32/actions/DebugActionServer.js +4 -2
- package/dist/stm32/analysis.d.ts +1 -0
- package/dist/stm32/build.d.ts +4 -4
- package/dist/stm32/build.js +1 -1
- package/dist/stm32/context.js +2 -2
- package/dist/stm32/daemon.d.ts +8 -6
- package/dist/stm32/daemon.js +25 -18
- package/dist/stm32/regen.js +14 -8
- package/dist/stm32/session.d.ts +9 -6
- package/dist/stm32/session.js +11 -10
- package/dist/stm32/sync.d.ts +1 -0
- package/dist/stm32/sync.js +5 -3
- package/dist/stm32/tool.d.ts +5 -3
- package/dist/stm32/toolCommand.d.ts +4 -4
- package/dist/stm32/toolCommand.js +1 -3
- package/dist/stm32/utils/discover_tools.d.ts +2 -1
- package/dist/stm32/utils/discover_tools.js +1 -1
- package/dist/stm32/utils/downloadzip_utils.js +4 -4
- package/dist/stm32/utils/faultUtils.d.ts +2 -1
- package/dist/stm32/utils/faultUtils.js +2 -2
- package/dist/stm32/utils/findElf.d.ts +1 -1
- package/dist/stm32/utils/findElf.js +1 -1
- package/dist/stm32/utils/listSyncFiles.js +12 -10
- package/dist/stm32/utils/mcu_utils.d.ts +5 -6
- package/dist/stm32/utils/mcu_utils.js +10 -9
- package/dist/stm32/utils/project_utils.d.ts +1 -1
- package/dist/stm32/utils/project_utils.js +5 -3
- package/dist/stm32/utils/usb_utils.js +2 -2
- package/dist/stm32/utils/user_code.d.ts +4 -1
- package/dist/stm32/utils/user_code.js +5 -3
- package/dist/stm32/utils/utils_mutex.js +34 -14
- package/dist/stm32/validate.d.ts +1 -0
- package/dist/stm32/validate.js +1 -1
- package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +19 -23
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +71 -23
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/M_StatusEnum.md +33 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/app-startup.md +49 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver.md +103 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/host-protocol.md +48 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +24 -23
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/storage.md +140 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/tasks-and-interrupts.md +66 -0
- package/package.json +4 -1
- package/dist/features/project/project_build.d.ts +0 -2
- package/dist/features/project/project_build.js +0 -22
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +0 -22
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +0 -19
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +0 -9
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +0 -42
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +0 -35
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +0 -34
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/host-protocol.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Host protocol
|
|
2
|
+
|
|
3
|
+
- Use this when parsing frames from a host (UART, USB, SPI slave, CAN), dispatching commands, or building reply and telemetry packets.
|
|
4
|
+
- Every received byte is untrusted: one bad length can make the host write anywhere in memory.
|
|
5
|
+
|
|
6
|
+
## Frame sizes
|
|
7
|
+
|
|
8
|
+
- Every size and offset of the frame is a `constexpr` derived from the others. No length is written as a literal in the parser.
|
|
9
|
+
- Use a template for compile-time size variants, per the house style.
|
|
10
|
+
|
|
11
|
+
## Receiving
|
|
12
|
+
|
|
13
|
+
- Find the start of a frame, and drop the bytes before it. If the frame isn't complete yet, wait for more bytes.
|
|
14
|
+
- Remove the frame from the buffer whether or not it parsed, so a bad frame can't block the queue.
|
|
15
|
+
- Bound every search and copy by the bytes actually received, not by the maximum frame size.
|
|
16
|
+
|
|
17
|
+
## Parse checks, in order
|
|
18
|
+
|
|
19
|
+
1. The received length covers the header before any header byte is read.
|
|
20
|
+
2. The length field matches the bytes received and is at most the maximum.
|
|
21
|
+
3. Unsigned subtraction is guarded:
|
|
22
|
+
```cpp
|
|
23
|
+
if (len < OVERHEAD) return M_StatusEnum::M_OUT_OF_RANGE;
|
|
24
|
+
size_t data_len = len - OVERHEAD;
|
|
25
|
+
```
|
|
26
|
+
4. The checksum or CRC matches before anything acts on the frame.
|
|
27
|
+
|
|
28
|
+
## Dispatch
|
|
29
|
+
|
|
30
|
+
- A write to an indexed range checks the whole range, without overflow:
|
|
31
|
+
```cpp
|
|
32
|
+
if (start >= COUNT || len > COUNT - start) return M_StatusEnum::M_OUT_OF_RANGE;
|
|
33
|
+
```
|
|
34
|
+
- Writes to read-only data are rejected.
|
|
35
|
+
- Reply with the result, so the host sees a rejection.
|
|
36
|
+
- Keep these checks compiled in; never put them behind a debug-only macro.
|
|
37
|
+
|
|
38
|
+
## Outgoing packets
|
|
39
|
+
|
|
40
|
+
- Build into a static buffer, and check the room left before each append.
|
|
41
|
+
- Clip values to their field width (`CLIP_U16(dt_us)`), and state the unit.
|
|
42
|
+
|
|
43
|
+
## Checklist
|
|
44
|
+
- [ ] Frame sizes are `constexpr` and derive from one source
|
|
45
|
+
- [ ] Searches and copies are bounded by the received length
|
|
46
|
+
- [ ] Unsigned subtraction is guarded; the checksum is checked before acting
|
|
47
|
+
- [ ] Range writes check `start + len` without overflow
|
|
48
|
+
- [ ] Bad frames are consumed and answered with an error
|
|
@@ -1,35 +1,36 @@
|
|
|
1
|
-
# Registers
|
|
1
|
+
# Registers and units
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
- Use this when turning datasheet registers into code, or writing bit fields, units, conversions, constants or macros.
|
|
4
4
|
|
|
5
|
-
|
|
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.
|
|
5
|
+
## Datasheet registers
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
- Copy bit positions, widths and reset values from the datasheet, and cite it: `/** DS §8.3, table 21 */`.
|
|
8
|
+
- Read two's-complement registers into signed types. Sign-extend non-native widths explicitly:
|
|
9
|
+
```cpp
|
|
10
|
+
int32_t v = ((int32_t)b[0] << 16) | (b[1] << 8) | b[2];
|
|
11
|
+
if (v & 0x800000) v -= 0x1000000; // 24-bit two's complement
|
|
12
|
+
```
|
|
13
|
+
- Read multi-byte values out of byte buffers with `memcpy` or byte shifts, never through a cast pointer (`*(float*)&buf[i]`): the offset may be unaligned.
|
|
14
|
+
- Clear a field before setting it:
|
|
12
15
|
```cpp
|
|
13
|
-
reg = (reg & ~ODR_MASK) | (odr << ODR_POS);
|
|
16
|
+
reg = (reg & ~ODR_MASK) | ((odr << ODR_POS) & ODR_MASK);
|
|
14
17
|
```
|
|
15
|
-
Don't just OR a new value into a register that already holds the old one.
|
|
16
18
|
|
|
17
|
-
## Units
|
|
19
|
+
## Units
|
|
18
20
|
|
|
19
|
-
- Put the unit in
|
|
20
|
-
-
|
|
21
|
-
-
|
|
21
|
+
- Put the unit in every name, or in a comment beside it: `dt_us`, `ref_uT`, `scale_mg_per_lsb`.
|
|
22
|
+
- Convert between units with a named conversion (`us_to_ns()`), never a cast.
|
|
23
|
+
- Write constants in the sensor's output unit.
|
|
24
|
+
- Name noise as `_std` or `_var`; the two differ by a square.
|
|
22
25
|
|
|
23
|
-
##
|
|
26
|
+
## Constants and macros
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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.
|
|
28
|
+
- Prefer `constexpr` and `inline` over macros unless you need token pasting.
|
|
29
|
+
- Fully parenthesize macro bodies and arguments: `#define CLIP_U16(x) ((x) > 0xFFFF ? 0xFFFF : (x))`.
|
|
30
|
+
- Derive sizes and offsets from one source.
|
|
30
31
|
|
|
31
32
|
## Checklist
|
|
32
|
-
- [ ] Bit positions and signedness match the datasheet
|
|
33
|
+
- [ ] Bit positions and signedness match the datasheet, with a citation
|
|
33
34
|
- [ ] Field writes clear the old value first
|
|
34
|
-
- [ ] Every
|
|
35
|
-
- [ ]
|
|
35
|
+
- [ ] Every value states its unit; conversions are named
|
|
36
|
+
- [ ] Macros are parenthesized; sizes come from one source
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Storage
|
|
2
|
+
|
|
3
|
+
- Use this when saving settings, calibration or other data to flash or EEPROM.
|
|
4
|
+
|
|
5
|
+
## Register array
|
|
6
|
+
|
|
7
|
+
- All settings live in one byte array, addressed by offset. RAM is the working copy; flash is the saved copy, saved and loaded as a whole.
|
|
8
|
+
- Each offset derives from the previous one and from `sizeof`, so nothing overlaps or is counted twice:
|
|
9
|
+
|
|
10
|
+
```cpp
|
|
11
|
+
#define REG_CTRL 0
|
|
12
|
+
#define REG_BOOT_COUNT 1 // uint8
|
|
13
|
+
#define REG_CALIB_ACC 2 // 3 × float
|
|
14
|
+
#define REG_CALIB_MAG (REG_CALIB_ACC + 3 * sizeof(float)) // 3 × float
|
|
15
|
+
#define REG_COUNT (REG_CALIB_MAG + 3 * sizeof(float))
|
|
16
|
+
```
|
|
17
|
+
- Save only settings. Live status and measurements stay out of the saved array.
|
|
18
|
+
- Read and write multi-byte values with `memcpy`, never through a cast pointer. Offsets are not aligned, and an unaligned `float` access faults:
|
|
19
|
+
```cpp
|
|
20
|
+
float x;
|
|
21
|
+
memcpy(&x, &this->_regs[REG_CALIB_ACC], sizeof(x)); // not *(float*)&this->_regs[...]
|
|
22
|
+
```
|
|
23
|
+
- Defaults are one table in code. `load_defaults()` copies it into the array:
|
|
24
|
+
```cpp
|
|
25
|
+
static constexpr uint8_t REG_DEFAULTS[REG_COUNT] = {
|
|
26
|
+
0x01, // REG_CTRL
|
|
27
|
+
0, // REG_BOOT_COUNT
|
|
28
|
+
// the rest is zero-filled: calibration floats = 0.0f
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- Setters reject offsets at or past the count, for every byte of a multi-byte write:
|
|
33
|
+
|
|
34
|
+
```cpp
|
|
35
|
+
M_Status Register_Store::set_bytes(size_t reg, const uint8_t* src, size_t len) {
|
|
36
|
+
if (reg >= REG_COUNT || len > REG_COUNT - reg)
|
|
37
|
+
return M_StatusEnum::M_OUT_OF_RANGE;
|
|
38
|
+
memcpy(&this->_regs[reg], src, len);
|
|
39
|
+
return M_StatusEnum::M_OK;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Settings page
|
|
44
|
+
|
|
45
|
+
- Use the last flash page. Take its address and size from the reference manual's flash chapter.
|
|
46
|
+
- Shrink the `FLASH` length in the linker script by that page, so code can never be placed on it:
|
|
47
|
+
```ld
|
|
48
|
+
/* last 2 KB page reserved for settings */
|
|
49
|
+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K - 2K
|
|
50
|
+
```
|
|
51
|
+
- Take page size, write unit and erase style from the HAL flash header and API, not from memory: `FLASH_PAGE_SIZE`, the `FLASH_TYPEPROGRAM_*` types (half-word on F1/F3, double-word on G4/L4, flash-word on H7), and whether `FLASH_EraseInitTypeDef` takes a page number (`Page`, plus `Banks` on dual-bank parts) or an address (`PageAddress`). Every programmed block starts on, and is padded to, that write unit. The HAL has no write-unit constant; derive it from the program type, e.g. `#define FLASH_WRITE_UNIT sizeof(uint64_t)` for `FLASH_TYPEPROGRAM_DOUBLEWORD`.
|
|
52
|
+
- Check every HAL flash return, and `PageError` after `HAL_FLASHEx_Erase` (`0xFFFFFFFF` means success).
|
|
53
|
+
|
|
54
|
+
## Header
|
|
55
|
+
|
|
56
|
+
- The first 16 bytes of the settings flash page are a header; the register array follows. One header covers the whole array, never one per setting.
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
settings page: [ magic | version | crc | pad ][ registers ... ]
|
|
60
|
+
0 4 6 10 16
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
- Each field answers one question on load:
|
|
64
|
+
- `magic`: does this page hold our settings at all? It's any fixed constant you pick. Blank flash (`0xFFFFFFFF`) or leftover data from other firmware won't match it.
|
|
65
|
+
- `version`: was it saved with the current register layout? An old layout would load into the wrong offsets.
|
|
66
|
+
- `crc`: are the registers intact? A save cut off by a reset, or a flipped bit, won't match.
|
|
67
|
+
|
|
68
|
+
```cpp
|
|
69
|
+
#define FLASH_MAGIC 0x52454753u // "REGS"
|
|
70
|
+
#define FLASH_VERSION 3 // bump when an offset moves, a register is added/removed, or a new default must replace saved values
|
|
71
|
+
#define HDR_MAGIC 0 // uint32
|
|
72
|
+
#define HDR_VERSION 4 // uint16
|
|
73
|
+
#define HDR_CRC 6 // uint32, CRC of the registers
|
|
74
|
+
#define HDR_LEN 16 // padded to a multiple of the flash write unit
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
- On load, a blank flash (0xFF), or a magic, version or CRC mismatch, loads the defaults from code and reports it. The caller logs that `M_ERROR` and keeps running on the defaults; it's expected on first boot, not a fatal error:
|
|
78
|
+
|
|
79
|
+
```cpp
|
|
80
|
+
M_Status Register_Store::load() {
|
|
81
|
+
uint8_t hdr[HDR_LEN];
|
|
82
|
+
M_Status status = this->flash_read(0, hdr, HDR_LEN);
|
|
83
|
+
status |= this->flash_read(HDR_LEN, this->_regs, REG_COUNT);
|
|
84
|
+
if (status != M_StatusEnum::M_OK
|
|
85
|
+
|| read_u32(&hdr[HDR_MAGIC]) != FLASH_MAGIC // blank flash reads 0xFFFFFFFF
|
|
86
|
+
|| read_u16(&hdr[HDR_VERSION]) != FLASH_VERSION
|
|
87
|
+
|| read_u32(&hdr[HDR_CRC]) != crc32(this->_regs, REG_COUNT)) {
|
|
88
|
+
this->load_defaults();
|
|
89
|
+
return M_StatusEnum::M_ERROR; // caller reports it
|
|
90
|
+
}
|
|
91
|
+
return M_StatusEnum::M_OK;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Writing
|
|
96
|
+
|
|
97
|
+
- Update the CRC, then erase, program, read back and compare.
|
|
98
|
+
- Keep the flash locked except around erase and program, on the failure path too.
|
|
99
|
+
- Pad with `0xFF`, the erased value, so padding programs nothing.
|
|
100
|
+
- HAL erase and program calls time out internally; check their return. Code that polls the flash busy bit itself needs its own timeout.
|
|
101
|
+
|
|
102
|
+
```cpp
|
|
103
|
+
M_Status Register_Store::save() {
|
|
104
|
+
uint8_t hdr[HDR_LEN];
|
|
105
|
+
memset(hdr, 0xFF, HDR_LEN);
|
|
106
|
+
write_u32(&hdr[HDR_MAGIC], FLASH_MAGIC);
|
|
107
|
+
write_u16(&hdr[HDR_VERSION], FLASH_VERSION);
|
|
108
|
+
write_u32(&hdr[HDR_CRC], crc32(this->_regs, REG_COUNT));
|
|
109
|
+
|
|
110
|
+
M_Status status = this->flash_unlock();
|
|
111
|
+
if (status == M_StatusEnum::M_OK) { // never erase or program a locked flash
|
|
112
|
+
status |= this->flash_erase_page(SETTINGS_PAGE); // checks PageError too
|
|
113
|
+
if (status == M_StatusEnum::M_OK) status |= this->flash_program(0, hdr, HDR_LEN);
|
|
114
|
+
if (status == M_StatusEnum::M_OK) status |= this->flash_program(HDR_LEN, this->_regs, REG_COUNT); // pads the tail with 0xFF
|
|
115
|
+
}
|
|
116
|
+
status |= this->flash_lock(); // runs even after a failure
|
|
117
|
+
if (status != M_StatusEnum::M_OK) return status;
|
|
118
|
+
return this->verify(hdr);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
M_Status Register_Store::verify(const uint8_t* hdr) { // read back header and registers
|
|
122
|
+
uint8_t buf[HDR_LEN + REG_COUNT];
|
|
123
|
+
M_Status status = this->flash_read(0, buf, sizeof(buf));
|
|
124
|
+
if (status == M_StatusEnum::M_OK
|
|
125
|
+
&& (memcmp(buf, hdr, HDR_LEN) != 0 || memcmp(&buf[HDR_LEN], this->_regs, REG_COUNT) != 0))
|
|
126
|
+
status = M_StatusEnum::M_ERROR;
|
|
127
|
+
return status;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Checklist
|
|
132
|
+
- [ ] Settings page is the last page and is removed from the linker's `FLASH` region
|
|
133
|
+
- [ ] Page size, write unit and erase fields come from the HAL; every HAL return and `PageError` is checked
|
|
134
|
+
- [ ] Blocks are aligned and `0xFF`-padded to the write unit
|
|
135
|
+
- [ ] Multi-byte values are copied with `memcpy`; erase and program run only after a successful unlock
|
|
136
|
+
- [ ] One header (magic, version, CRC) covers the whole array
|
|
137
|
+
- [ ] Version is bumped on any layout change, or when a new default must replace saved values
|
|
138
|
+
- [ ] Blank flash or a bad header falls back to defaults
|
|
139
|
+
- [ ] Writes are read back and compared; flash stays locked after
|
|
140
|
+
- [ ] Offsets derive from one source; setters are bounded
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Tasks and interrupts
|
|
2
|
+
|
|
3
|
+
- Use this when writing ISRs, HAL callbacks, RTOS tasks, DMA transfers, or data shared between them.
|
|
4
|
+
|
|
5
|
+
## ISR → task
|
|
6
|
+
|
|
7
|
+
- ISRs and HAL callbacks only signal a task. Bus transfers, parsing and math run in the task.
|
|
8
|
+
- One notify bit per source, so one task can serve several.
|
|
9
|
+
- Null-check every global the callback uses, because it can fire before the object exists.
|
|
10
|
+
|
|
11
|
+
```cpp
|
|
12
|
+
void HAL_GPIO_EXTI_Callback(uint16_t pin) {
|
|
13
|
+
if (reader_task == nullptr) return;
|
|
14
|
+
uint32_t bits = 0;
|
|
15
|
+
if (pin == IMU_DRDY_Pin) bits |= BIT_IMU_READY;
|
|
16
|
+
if (pin == MAG_DRDY_Pin) bits |= BIT_MAG_READY;
|
|
17
|
+
if (bits == 0) return;
|
|
18
|
+
BaseType_t woken = pdFALSE;
|
|
19
|
+
xTaskNotifyFromISR(reader_task, bits, eSetBits, &woken);
|
|
20
|
+
portYIELD_FROM_ISR(woken);
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Task loop
|
|
25
|
+
|
|
26
|
+
- Waits on hardware events have a timeout. On timeout, poll the source (e.g. the DRDY pin level), so a missed edge doesn't stall it.
|
|
27
|
+
|
|
28
|
+
```cpp
|
|
29
|
+
void task_read_sensors(void*) {
|
|
30
|
+
for (;;) {
|
|
31
|
+
uint32_t bits = 0;
|
|
32
|
+
if (xTaskNotifyWait(0, UINT32_MAX, &bits, pdMS_TO_TICKS(50)) != pdTRUE
|
|
33
|
+
&& HAL_GPIO_ReadPin(IMU_DRDY_GPIO_Port, IMU_DRDY_Pin)) bits |= BIT_IMU_READY;
|
|
34
|
+
if (bits & BIT_IMU_READY) imu->read_into_memory();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Shared data
|
|
40
|
+
|
|
41
|
+
- Every buffer used by two contexts has one comment: who writes it, who reads it, what protects it.
|
|
42
|
+
- Task ↔ task: a mutex taken with a timeout. On timeout, skip this cycle instead of blocking.
|
|
43
|
+
- ISR ↔ task: a single-producer/single-consumer ring, or a critical section around the task's compaction and clearing.
|
|
44
|
+
- The writer fills a temporary buffer and copies or swaps it in under the lock. Never write into a buffer that is being parsed.
|
|
45
|
+
- A read-and-clear flag is consumed once per cycle, and the result is kept in a local.
|
|
46
|
+
|
|
47
|
+
## DMA
|
|
48
|
+
|
|
49
|
+
- DMA buffers are static or class members, never locals.
|
|
50
|
+
- Check the length against the DMA buffer size before copying into it.
|
|
51
|
+
- Wait for the previous transfer (or take the TX semaphore) before reusing the buffer.
|
|
52
|
+
- If starting the transfer fails, release what you took:
|
|
53
|
+
```cpp
|
|
54
|
+
if (HAL_UART_Transmit_DMA(this->_huart, this->_tx_buf, len) != HAL_OK) {
|
|
55
|
+
this->_tx_sem.release();
|
|
56
|
+
return M_StatusEnum::M_ERROR;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
- An error callback releases only the resources of the transfer that failed.
|
|
60
|
+
|
|
61
|
+
## Checklist
|
|
62
|
+
- [ ] ISRs only notify; callbacks null-check globals
|
|
63
|
+
- [ ] Every wait on hardware has a timeout and a fallback
|
|
64
|
+
- [ ] Every shared buffer documents its writer, reader and lock
|
|
65
|
+
- [ ] DMA buffers aren't on the stack; every acquire is released on failure
|
|
66
|
+
- [ ] Each read-and-clear flag is consumed once per cycle
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turingcode/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
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
6
|
"exports": {
|
|
@@ -23,7 +23,10 @@
|
|
|
23
23
|
"build": "node scripts/build.cjs",
|
|
24
24
|
"typecheck": "tsc --noEmit && tsc --project tsconfig.test.json --noEmit",
|
|
25
25
|
"test": "node scripts/test.cjs",
|
|
26
|
+
"install-skills": "node scripts/build.cjs && node dist/cli/index.js install-skills",
|
|
27
|
+
"install-mods": "node scripts/build.cjs && node dist/cli/index.js install-mods",
|
|
26
28
|
"default_publish": "npm publish --access public --tag latest",
|
|
29
|
+
"after_publish": "npm i -g @azrico/azcli",
|
|
27
30
|
"prepublishOnly": "npm test && npm run build"
|
|
28
31
|
},
|
|
29
32
|
"publishConfig": {
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { runBuild } from '../../stm32/context.js';
|
|
2
|
-
import { _defineTool } from '../../stm32/tool.js';
|
|
3
|
-
import { findStm32Project } from '../../stm32/utils/project_utils.js';
|
|
4
|
-
// Builds the server copy and downloads the resulting ELF into the local build/<preset>/ folder.
|
|
5
|
-
export default _defineTool({
|
|
6
|
-
name: 'project_build',
|
|
7
|
-
description: 'Build the project on the TuringCode server \
|
|
8
|
-
(run `turingcode project sync` first to include local changes), then download the .elf ' +
|
|
9
|
-
'into the local build/<preset>/ folder for `turingcode probe program`. \
|
|
10
|
-
Prints the result, warnings in the project code, sizes and the clock audit summary (`--verbose` for the full reply).',
|
|
11
|
-
properties: {
|
|
12
|
-
preset: {
|
|
13
|
-
type: 'string',
|
|
14
|
-
enum: ['Debug', 'Release', 'RelWithDebInfo', 'MinSizeRel'],
|
|
15
|
-
},
|
|
16
|
-
verbose: {
|
|
17
|
-
type: 'boolean',
|
|
18
|
-
description: 'Print the full build reply (compiler, Drivers/ warnings, map file, stack usage, build log, clock tables); a failed build always prints it',
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
run: async (args) => runBuild(await findStm32Project(), args.preset ?? 'Debug', !!args.verbose),
|
|
22
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
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
|
|
@@ -1,19 +0,0 @@
|
|
|
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. |
|
|
@@ -1,9 +0,0 @@
|
|
|
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).
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
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
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
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
|