@turingcode/cli 0.3.14 → 0.3.16

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 (46) hide show
  1. package/dist/cli/create-cli.js +46 -31
  2. package/dist/core/mcp.d.ts +0 -10
  3. package/dist/core/mcp.js +0 -18
  4. package/dist/features/project/analysis_static.js +5 -4
  5. package/dist/features/project/command.d.ts +3 -1
  6. package/dist/features/project/command.js +15 -11
  7. package/dist/features/project/project_regenerate.js +4 -3
  8. package/dist/features/project/project_sync_from_server.js +4 -3
  9. package/dist/features/project/project_sync_to_server.js +33 -8
  10. package/dist/features/project/stm32_cubetools_download_drivers.js +5 -4
  11. package/dist/features/status/get_status.js +6 -3
  12. package/dist/features/syntax-check/command.d.ts +2 -0
  13. package/dist/features/syntax-check/command.js +8 -0
  14. package/dist/features/syntax-check/syntax_check.d.ts +3 -0
  15. package/dist/features/syntax-check/syntax_check.js +27 -0
  16. package/dist/features/tool/command.d.ts +2 -0
  17. package/dist/features/tool/command.js +58 -7
  18. package/dist/lib.d.ts +1 -1
  19. package/dist/lib.js +1 -1
  20. package/dist/stm32/analysis.d.ts +40 -4
  21. package/dist/stm32/analysis.js +61 -5
  22. package/dist/stm32/toolCommand.d.ts +1 -0
  23. package/dist/stm32/toolCommand.js +7 -2
  24. package/dist/stm32/utils/makeTempProject.d.ts +16 -0
  25. package/dist/stm32/utils/makeTempProject.js +79 -0
  26. package/dist/stm32/utils/project_utils.d.ts +36 -1
  27. package/dist/stm32/utils/project_utils.js +48 -2
  28. package/mods/claude/turingcode-skills/skills/AGENTS.md +0 -5
  29. package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +2 -1
  30. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +50 -42
  31. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/M_StatusEnum.md +30 -9
  32. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/BusMutexRegistry.hpp +138 -0
  33. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_Driver.hpp +97 -0
  34. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_I2C.hpp +83 -0
  35. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_SPI.hpp +69 -0
  36. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_USART.hpp +58 -0
  37. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/Util_MutexManager.hpp +212 -0
  38. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver.md +81 -0
  39. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_ic.md +123 -0
  40. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_protocol.md +16 -0
  41. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_rtos.md +129 -0
  42. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_test.md +17 -0
  43. package/package.json +1 -1
  44. package/dist/features/project/analysis_syntax_check.d.ts +0 -3
  45. package/dist/features/project/analysis_syntax_check.js +0 -21
  46. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver.md +0 -97
@@ -0,0 +1,81 @@
1
+ # Driver creating and editing manual
2
+
3
+ - Use this when you want to create or edit driver for ICs or bus communication protocols.
4
+
5
+ ## Workflow
6
+
7
+ ### 1 Startup
8
+ Determine if the current working directory is an existing stm32 project or we are to create standalone driver files.
9
+ * for existing projects that already have a driver with similar name we must re use them and not create separate one, so do a proper search in the current existing project.
10
+
11
+ ### 2 Determination
12
+ a project definition or a requirement or a task would be given to you
13
+
14
+ - If there is no input given, halt and ask for an input task.
15
+ - If input is given you must detect what drivers does the task require (we are to make those drivers)
16
+ - For sensors that support multiple protocols and user did not specify the exact protocol we must not guess and should stop and ask the user first.
17
+
18
+ - on-chip peripherals (ADC, DAC, TIM/PWM, FMAC, CORDIC, RTC, CRC, IWDG) are not drivers. The app sets them up and uses them through HAL. Write a driver only when an IC driver needs one of them.
19
+
20
+ Examples:
21
+
22
+ | Task | Drivers | Ask the user |
23
+ |------|---------|--------------|
24
+ | `read data from X sensor via spi` | SPI, X_Sensor | - |
25
+ | `read data from X sensor via spi and send via USART to USART1` | SPI, USART, X_Sensor | - |
26
+ | `connect to X sensor and read their data and send to my pc` | X_Sensor | X_Sensor's bus; link to the PC |
27
+ | `drive motor with DRV8871 via PWM` | PWM, DRV8871 | - |
28
+ | `filter ADC samples with FMAC` | none | - |
29
+
30
+ After correctly determining what drivers are needed we can go onto the next steps.
31
+
32
+ ### 3 Source of truth - IMPORTANT
33
+
34
+ - Turingcode tools have a knowledge base tool that you should use to find these information (see: `turingcode tool list`)
35
+ - when using the knowledge base tool first list file names then search on related files
36
+ - ALWAYS save result of the knowledge base calls into a file the grep it.
37
+
38
+ - ALWAYS take every register, reset value, timing, encoding and other facts about parts that you write in code take them from datasheet, errata or application notes.
39
+ - For values that you find from references cite them: `// [CITE: ABC]` in the code.
40
+ - For values that you guessed or did not find from reference files mark them with a `// [GUESS]` in the code and list them in the end.
41
+ - Timeouts and poll counts are not guesses: compute them from the cited timing (transfer length × slowest clock, worst-case reset or conversion time) plus a stated margin, and write the calculation in the comment, e.g. `// [CALC: 7 B at 1 MHz = 56 us; 1 ms tick -> 2 ms]`. Use `[GUESS]` only when there is no timing to compute from.
42
+ - Register writes are not retried: a read-back mismatch is an error (tries = 1) unless the project gives a reason to retry.
43
+ - timing and margin checks use the datasheet's worst-case (min/max) values, not typical when a setting depends on a margin, cite it.
44
+
45
+ - NEVER skip this step to go faster, the time it takes to debug a faulty register value because you guessed it is much longer than first reading it from the proper source.
46
+
47
+ #### 3.1 Facts to check
48
+
49
+ Check at least these facts, plus any others that apply to the part and project,
50
+ - save a table of the checked facts into `App/facts.md`
51
+ - if possible you can delegate the task of fact checking to a subagent then read the facts yourself:
52
+
53
+ Facts to check:
54
+ * chip ID
55
+ * bus mode and maximum clock, checked against the definition
56
+ * start-up boot and reset timing
57
+ * turn-on and settling time after the output is enabled (samples that are not valid yet)
58
+ * interrupt latched/pulsed behaviour
59
+ * interface bits to set when the chip is used over one bus only, for example turning I2C off on an SPI-only design.
60
+ * "Must set to 1" bits such as DEVICE_CONF
61
+ * errata of both the part and of the MCU (An item applies only if the driver uses that mode or feature)
62
+ * to find every item: search `<MCU> errata` with `names_only` to get the file, then search that file once per peripheral the driver uses, with `names_only` and `top_k` 10, and decide each item by its title
63
+
64
+
65
+ ### 4 Writing Drivers
66
+
67
+ Only Communication protocol drivers call HAL; IC drivers and the base classes don't (except HAL_Delay or osDelay when working with RTOS), so they easily port to another MCU unchanged.
68
+
69
+ - Read and follow `references/driver/driver_protocol.md` when making Communication protocol drivers (eg: USART, SPI, I2C, ...)
70
+ - Read and follow `references/driver/driver_ic.md` when making IC, Sensor or Component drivers
71
+ - If project is using RTOS (eg: FreeRTOS) also read `references/driver/driver_rtos.md` when creating drivers
72
+
73
+ ### 5 Testing drivers
74
+
75
+ - After drivers are written we must also test them, a syntax-only compile does not count as tested, read `references/driver/driver_test.md`
76
+ - Before finishing, list every line of the definition that touches a driver (bus mode, pin edge, rates, ranges, what stays powered off). Check that the code and a test cover each one.
77
+
78
+ ### 6 Final Deliverable
79
+
80
+ - if your task was only to make drivers: dont get confused by the project definition given to you, it might say things like "I want a stm32 project that does XYZ" there is no need to create a stm32 project, the final deliverable of the task are the driver files only.
81
+ - if your task was to create a project: then you can make both the drivers and project files.
@@ -0,0 +1,123 @@
1
+ # IC and Sensor driver
2
+
3
+ - Use this when creating or editing a driver for an external IC, sensor or component.
4
+ - Name of classes and variables here is a suggestion, use the existing naming convention of the project or instructions if they are given.
5
+
6
+ ## Generic IC Drivers Rules
7
+
8
+ - IC drivers live in `Drivers/<IC>/`, IC_Driver are generic and dont depend on the bus protocol. they talk to the chip only through a `COM_Driver*`.
9
+ - any driver method call (read, write, verify_id, ...) before a successful init() should return an error.
10
+
11
+ ## Register header
12
+
13
+ - Check thee part's register table and make a `<ic>_regs.h`:
14
+ ```h
15
+ #define ABC123_WHO_AM_I 0x0F
16
+ #define ABC123_ID 0x6B
17
+ #define ABC123_CTRL1 0x10
18
+ #define ABC123_ODR_POS 4
19
+ #define ABC123_ODR_MASK (0x0F << ABC123_ODR_POS)
20
+ #define ABC123_SPI_READ 0x80
21
+ ```
22
+
23
+ ## IC_Driver
24
+
25
+ - Base class for all ICs; subclasses implement the virtual methods.
26
+ - A feature the IC lacks (eg: writing into a read only temperature sensor) returns `M_NOT_SUPPORTED`.
27
+ - read appends one sample at buf[*index]
28
+
29
+ ```cpp
30
+ class IC_Driver {
31
+ public:
32
+ explicit IC_Driver(COM_Driver* com) : _com(com) {}
33
+ virtual M_Status init() = 0;
34
+ virtual M_Status reset() = 0;
35
+ virtual M_Status read(uint8_t* buf, size_t capacity, size_t* index) = 0;
36
+
37
+ bool is_interrupt_pin_set() const { return this->com()->is_interrupt_pin_set(); }
38
+ protected:
39
+ COM_Driver* com() const{ return this->_com; }
40
+ private:
41
+ COM_Driver* _com;
42
+ };
43
+ ```
44
+
45
+ ## Sensor driver
46
+
47
+ - A sensor driver never creates its COM driver, The owner wires the sensor and com driver together.
48
+ - Sensor Driver is generic; the project's choices (range, ODR, filters, FIFO watermark, interrupt routing, oversampling, ...) belong to the app, not the driver.
49
+ - Values that depend on the config (sensitivity per LSB, sample period, settling time) are computed from `_cfg`, never fixed constants.
50
+
51
+ - ICs of one family can share a templated base for compile-time sample sizes (eg: `template <size_t AXES, size_t BYTES_PER_AXIS> class IC_Sensor : public IC_Driver`).
52
+
53
+ ```cpp
54
+ class Sensor_ABC123 : public IC_Driver {
55
+ public:
56
+ using IC_Driver::IC_Driver;
57
+ M_Status reset() override;
58
+ M_Status init() override;
59
+ /**
60
+ *
61
+ * Read one sample in one burst
62
+ * (if available: auto-increment, BDU block data update to prevent tearing while reading).
63
+ *
64
+ * Return only fresh data: check data-ready if polling (status bit)
65
+ * for DRDY interrupt based reads, data should be fresh
66
+ * so no need for checking again.
67
+ *
68
+ * change the caller's buffer and `*index` on successful reads only.
69
+ */
70
+ M_Status read(uint8_t* buf, size_t capacity, size_t* index) override;
71
+
72
+ M_Status verify_id();
73
+
74
+ bool is_interrupt_pin_set() const;
75
+
76
+ /* ---- Config ---- */
77
+ enum class CFG_ODR : uint8_t { OFF = 0x0, HZ_104 = 0x4, HZ_416 = 0x6 };
78
+ enum class CFG_RANGE : uint8_t { G2 = 0x0, G4 = 0x2, G8 = 0x3 };
79
+ struct Config {
80
+ CFG_ODR odr = CFG_ODR::HZ_104;
81
+ CFG_RANGE range = CFG_RANGE::G2;
82
+ };
83
+ /**
84
+ * store the config, then init(); also used to reconfigure at runtime
85
+ */
86
+ M_Status init(const Config& cfg) { this->_cfg = cfg; return this->init(); }
87
+ private:
88
+ Config _cfg;
89
+ };
90
+ ```
91
+
92
+ ```cpp
93
+ COM_SPI spi_imu(&hspi1, IMU_CS_GPIO_Port, IMU_CS_Pin);
94
+ Sensor_ABC123 imu(&spi_imu);
95
+ imu.init({ .odr = Sensor_ABC123::CFG_ODR::HZ_416 }); // or imu.init() for the defaults
96
+ ```
97
+
98
+ ### init() order
99
+
100
+ 1. Set bus address masks before the first access
101
+ 2. check the ID register first. If it fails return.
102
+ 3. reset and wait for the datasheet's completion bit with a bounded poll or a fixed delay if the datasheet gives a timed procedure with no status bit.
103
+ - samples lost during the init sequence should not count as discared or lost, because there is no safe way for us to retrive them.
104
+ 4. set range, filters and options, then enable the output rate, unless the datasheet gives another order.
105
+ - never hand settling samples as valid data: wait out the settling time before starting the FIFO or interrupt.
106
+
107
+ - use `write_verify` to check the bits that are written to be set.
108
+ - stop at the first failed write: never enable the output rate after a failed configuration write.
109
+
110
+ ```cpp
111
+ M_Status Sensor_ABC123::init()
112
+ ```
113
+
114
+
115
+ ### Interrupt pin
116
+
117
+ - Pick the interrupt mode from how the MCU takes the pin:
118
+ - Edge-triggered external interrupt: use pulsed, so every sample gives a new edge even after a missed read.
119
+ - Level-checked or polled pin: use latched, so a high pin means unread data.
120
+ - If the chip only latches and the MCU uses edges, the app must also check the pin level.
121
+
122
+ - missed edge recovery (watchdogs, re-arming) should be handled in the app task not in the drivers;
123
+ - when working on projects that go beyond making drivers only, see `references/tasks-and-interrupts.md` also.
@@ -0,0 +1,16 @@
1
+ # Communication protocol driver
2
+
3
+ - Use this when creating or editing a bus driver (SPI, I2C, ...) that IC drivers talk through.
4
+ - Name of classes and variables here is a suggestion, you must use the existing naming convention of the project or instuctions if they are given.
5
+
6
+ ## COM_Driver
7
+ - `COM_Driver` is the base class; subclasses (`COM_SPI`, `COM_I2C`, `COM_USART`) implement read and write with HAL or LL.
8
+ - Read `references/code/COM_Driver.hpp`
9
+
10
+ ## Protocol driver
11
+
12
+ - Owns the HAL handle and chip select; the IC driver never sees them.
13
+ - Read the example for the bus you are writing:
14
+ - SPI: `references/code/COM_SPI.hpp`
15
+ - I2C: `references/code/COM_I2C.hpp`
16
+ - USART: `references/code/COM_USART.hpp`
@@ -0,0 +1,129 @@
1
+ # RTOS support for drivers
2
+
3
+ - Use this when the project runs an RTOS (eg: FreeRTOS) so its drivers can be called from more than one task.
4
+ - when using freeRTOS, use osDelay instead of HAL_Delay
5
+
6
+ ## Util_MutexManager
7
+
8
+ - `COM_Driver` inherits `Util_MutexManager` under `#ifdef INC_FREERTOS_H`.
9
+
10
+ - A base class that gives each COM driver a FreeRTOS mutex and a binary semaphore, so tasks sharing a bus take turns and a DMA transfer keeps the bus busy until its ISR finishes.
11
+ - **Mutex**: guards a blocking transfer. use from task context only, never from an ISR.
12
+ - **Binary semaphore**: marks a DMA/IT transfer in flight. The starting task releases it if the start fails; otherwise the complete/error ISR releases it.
13
+ - Every call does nothing before the scheduler starts, so drivers can init in `main()`.
14
+
15
+ | Method | Does |
16
+ |--------|------|
17
+ | `get_scheduler_is_running()` | true once the scheduler started (static) |
18
+ | `mutex_init(mutex_id = NULL)` | creates both primitives; adopts `mutex_id` (eg: a shared bus mutex) instead of creating one |
19
+ | `mutex_lock(ticks)` / `mutex_unlock()` | takes / gives the mutex |
20
+ | `mutex_holder()` | task holding the mutex, or NULL |
21
+ | `semaphore_acquire(ticks)` | marks a transfer in flight |
22
+ | `semaphore_release()` | ends it from a task, when the transfer failed to start |
23
+ | `semaphore_release_from_isr(yield)` | ends it from the complete/error callback |
24
+ | `get_is_released()` | true while no transfer is in flight |
25
+ | `wait_for_release(ticks)` | blocks until idle without taking the token |
26
+
27
+ - If the project does not have a MutexManager, copy the `references/code/Util_MutexManager.hpp` into the project.
28
+
29
+
30
+ ## BusMutexRegistry
31
+
32
+ - Mutex instances for COM_Drivers are created via `BusMutexRegistry` so two drivers constructed against the SAME
33
+ physical peripheral handle automatically converge on the same mutex
34
+
35
+ ```cpp
36
+ void COM_Driver::init_freeRTOS() {
37
+ this->mutex_init(BusMutexRegistry::acquireBusMutexFor(this->handle()));
38
+ }
39
+ bool COM_Driver::mutex_lock(TickType_t ticksToWait) {
40
+ if (!this->_mutex_adopted &&Util_MutexManager::get_scheduler_is_running())
41
+ this->init_freeRTOS();
42
+ return Util_MutexManager::mutex_lock(ticksToWait);
43
+ }
44
+ ```
45
+
46
+ - If the project does not have a BusMutexRegistry, copy the `references/code/BusMutexRegistry.hpp` into the project.
47
+
48
+
49
+ ## COM_Driver
50
+
51
+ ```cpp
52
+ #ifdef INC_FREERTOS_H
53
+ #include "Util_MutexManager.hpp"
54
+ #endif
55
+
56
+ class COM_Driver
57
+ #ifdef INC_FREERTOS_H
58
+ : public Util_MutexManager
59
+ #endif
60
+ {
61
+ public:
62
+ #ifdef INC_FREERTOS_H
63
+ // adopts the shared bus mutex on first use after the scheduler starts, then locks
64
+ bool mutex_lock(TickType_t ticks = portMAX_DELAY);
65
+ #else
66
+ /**
67
+ * Bare-metal builds get no-op locks with the same names, and drivers call them unconditionally.
68
+ */
69
+ bool mutex_lock(uint32_t = 0) { return true; }
70
+ bool mutex_unlock() { return true; }
71
+ bool semaphore_acquire(uint32_t = 0) { return true; }
72
+ bool semaphore_release() { return true; }
73
+ int semaphore_release_from_isr(bool = false) { return 0; }
74
+ bool get_is_released() { return true; }
75
+ bool wait_for_release(uint32_t = 0) { return true; }
76
+ #endif
77
+ /* ... rest of COM_Driver ... */
78
+
79
+ /**
80
+ * ...
81
+ * with FreeRTOS it also adopts the bus mutex
82
+ */
83
+ void bind_com(...)
84
+ };
85
+ ```
86
+
87
+
88
+ ## Blocking transfer
89
+
90
+ - Lock around the whole transfer and chip select, and unlock on every return path.
91
+ - On a bus that also runs DMA, wait for the transfer in flight after taking the mutex.
92
+
93
+ ```cpp
94
+ M_Status COM_SPI::read(uint8_t reg, uint8_t* data, size_t len) {
95
+ if (!this->mutex_lock(SPI_LOCK_TICKS)) return M_StatusEnum::M_TIMEOUT;
96
+ this->wait_for_release(SPI_LOCK_TICKS);
97
+ this->cs_select();
98
+ M_Status status = HAL_SPI_TransmitReceive(/* ... */);
99
+ this->cs_free();
100
+ this->mutex_unlock();
101
+ return status;
102
+ }
103
+ ```
104
+
105
+ ## DMA or IT transfer
106
+
107
+ - Take the mutex, then the semaphore, then start the transfer, then unlock the mutex. The semaphore keeps the bus busy until the ISR releases it.
108
+ - If the start fails, release the semaphore yourself, since no ISR will fire.
109
+
110
+ ```cpp
111
+ M_Status COM_USART::write_DMA(const uint8_t* data, size_t len) {
112
+ if (!this->mutex_lock(UART_LOCK_TICKS)) return M_StatusEnum::M_TIMEOUT;
113
+ if (!this->semaphore_acquire(UART_LOCK_TICKS)) {
114
+ this->mutex_unlock();
115
+ return M_StatusEnum::M_BUSY;
116
+ }
117
+ M_Status status = HAL_UART_Transmit_DMA(this->handle(), data, len);
118
+ if (status != M_StatusEnum::M_OK) this->semaphore_release();
119
+ this->mutex_unlock();
120
+ return status;
121
+ }
122
+
123
+ /**
124
+ * called from HAL_UART_TxCpltCallback and HAL_UART_ErrorCallback
125
+ */
126
+ void COM_USART::on_tx_done() {
127
+ this->semaphore_release_from_isr(true);
128
+ }
129
+ ```
@@ -0,0 +1,17 @@
1
+ ## Testing drivers
2
+
3
+ Build a fake COM_Driver that records every read and write and can be made to fail at call N. Run the test with a local g++/clang; if there is none, sync the folder and run it with the Turingcode code_run tool. If neither works, report "host test written, not run". Never report it as tested.
4
+
5
+ Cases it must cover:
6
+ * the exact order and values of every init write;
7
+ * wrong identity causes zero writes;
8
+ * a wrong identity and a failed identity read return different errors;
9
+ * a bus failure at each init step;
10
+ * a reset that never finishes;
11
+ * each read-back mismatch;
12
+ * no new data returns M_BUSY and leaves the output untouched;
13
+ * decoding with values whose high and low bytes differ, plus negative and extreme values.
14
+ * Work out the expected bytes from the datasheet, not from the driver's own constants.
15
+
16
+ ### Prove the tests can fail
17
+ - In a throwaway copy, break one thing per area (an init value or its order, an error path, the decoding) and check that the matching test fails. If it still passes, strengthen the test.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turingcode/cli",
3
- "version": "0.3.14",
3
+ "version": "0.3.16",
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": {
@@ -1,3 +0,0 @@
1
- /** Syntax-checks files of the server copy; local project-relative paths are mapped to projects/<name>/. */
2
- declare const _default: import("../../stm32/tool.js").ToolDef;
3
- export default _default;
@@ -1,21 +0,0 @@
1
- import { syntaxCheckProject } from '../../stm32/analysis.js';
2
- import { callServer } from '../../stm32/context.js';
3
- import { _defineTool } from '../../stm32/tool.js';
4
- import { findStm32Project } from '../../stm32/utils/project_utils.js';
5
- /** Syntax-checks files of the server copy; local project-relative paths are mapped to projects/<name>/. */
6
- export default _defineTool({
7
- name: 'analysis_syntax_check',
8
- description: 'Check .c/.h/.cpp/.hpp files of your stm32 project for syntax errors on the TuringCode server \
9
- (run `turingcode project sync` first to include local changes). Returns `file:line:col: severity: message` lines.',
10
- properties: {
11
- files: {
12
- type: 'string',
13
- description: 'Comma-separated project-relative files, globs or folders, e.g. Core/Src/main.c (default: Core,App)',
14
- },
15
- },
16
- run: async (args) => {
17
- const project = await findStm32Project();
18
- const files = (args.files || 'Core,App').split(',').map(f => f.trim()).filter(Boolean);
19
- return syntaxCheckProject(callServer, project, files);
20
- },
21
- });
@@ -1,97 +0,0 @@
1
- # Writing and Editing an IC driver
2
-
3
- - Use this when you want to create or edit driver for ICs or Protocols.
4
- - If the project already has a COM driver or an IC base with similar names, inherit from it. Don't write a second one.
5
-
6
- ## Layers
7
-
8
- - `COM_Driver` is the base for protocol drivers (`COM_SPI`, `COM_I2C`). Only these call HAL.
9
- - `IC_Driver` is the base for IC drivers in `Drivers/<IC>/`. It talks to the chip only through a `COM_Driver*`.
10
-
11
- ## COM driver
12
-
13
- - Used as a base class for communication protocols (I2C, SPI, ...)
14
- - Subclasses implement transfers (read and write) using HAL or LL methods.
15
-
16
- ```cpp
17
- class COM_Driver {
18
- public:
19
- virtual M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) = 0;
20
- virtual M_Status write(uint8_t reg, const uint8_t* data, size_t len) = 0;
21
-
22
- M_Status write_bit(uint8_t reg, uint8_t bit, uint8_t value); // read-modify-write
23
- M_Status write_verify(uint8_t reg, uint8_t value, size_t tries); // write, read back, compare
24
- M_Status wait_for_register(uint8_t reg, uint8_t expected, size_t tries);
25
- void set_address_mask(uint8_t write_or, uint8_t read_or); // e.g. SPI read bit 0x80
26
- };
27
- ```
28
-
29
- ### Protocol driver
30
-
31
- - Owns the HAL handle and chip select; the IC driver never sees them.
32
- - Holds the bus mutex for the whole transfer, chip select included, so ICs sharing the bus don't interleave.
33
-
34
- ```cpp
35
- class COM_SPI : public COM_Driver {
36
- public:
37
- COM_SPI(SPI_HandleTypeDef* hspi, GPIO_TypeDef* cs_port, uint16_t cs_pin);
38
- M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) override; // CS low, TX reg|read mask, RX data, CS high
39
- M_Status write(uint8_t reg, const uint8_t* data, size_t len) override;
40
- private:
41
- SPI_HandleTypeDef* _hspi;
42
- GPIO_TypeDef* _cs_port;
43
- uint16_t _cs_pin;
44
- };
45
- ```
46
-
47
- ## IC_Driver Class
48
-
49
- - Base class used for all ICs
50
- - Subclasses implement the virtual methods.
51
- - A feature the IC lacks (eg: write into a temperature sensor) returns `M_StatusEnum::M_NOT_SUPPORTED`, never `M_StatusEnum::M_OK`.
52
- - ICs of one family can share a templated base between the two for compile-time sample sizes, (eg: `template <size_t AXES, size_t BYTES_PER_AXIS> class IC_Sensor : public IC_Driver`).
53
-
54
- ```cpp
55
- class IC_Driver {
56
- public:
57
- explicit IC_Driver(COM_Driver* com) : _com(com) {}
58
- virtual M_Status init() = 0;
59
- virtual M_Status reset() = 0;
60
- virtual M_Status read(uint8_t* buf, size_t* index) = 0; // appends at buf[*index]
61
- protected:
62
- COM_Driver* com() { return this->_com; }
63
- private:
64
- COM_Driver* _com;
65
- };
66
- ```
67
-
68
- ### IC Sensor Driver
69
-
70
- ```cpp
71
- class Sensor_ABC123 : public IC_Driver {
72
- public:
73
- using IC_Driver::IC_Driver;
74
- M_Status init() override;
75
- M_Status reset() override;
76
- M_Status read(uint8_t* buf, size_t* index) override;
77
- M_Status verify_id() { return this->com()->wait_for_register(ABC123_WHO_AM_I, ABC123_ID, 5); }
78
- };
79
- ```
80
-
81
- - It never calls HAL and never creates its COM driver. The owner wires the two together:
82
- ```cpp
83
- COM_SPI spi_imu(&hspi1, IMU_CS_GPIO_Port, IMU_CS_Pin);
84
- Sensor_ABC123 imu(&spi_imu);
85
- ```
86
-
87
- ## Register header
88
-
89
- `abc123_regs.h`, plain C, in datasheet order:
90
- ```c
91
- #define ABC123_WHO_AM_I 0x0F
92
- #define ABC123_ID 0x6B
93
- #define ABC123_CTRL1 0x10
94
- #define ABC123_ODR_POS 4
95
- #define ABC123_ODR_MASK (0x0F << ABC123_ODR_POS)
96
- #define ABC123_SPI_READ 0x80
97
- ```