@turingcode/cli 0.3.13 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/create-cli.js +46 -31
- package/dist/core/mcp.d.ts +0 -10
- package/dist/core/mcp.js +0 -18
- package/dist/features/project/analysis_static.js +7 -6
- package/dist/features/project/command.d.ts +3 -1
- package/dist/features/project/command.js +15 -11
- package/dist/features/project/project_regenerate.js +4 -3
- package/dist/features/project/project_sync_from_server.js +4 -3
- package/dist/features/project/project_sync_to_server.js +36 -11
- package/dist/features/project/stm32_cubetools_download_drivers.js +5 -4
- package/dist/features/status/get_status.js +6 -3
- package/dist/features/syntax-check/command.d.ts +2 -0
- package/dist/features/syntax-check/command.js +8 -0
- package/dist/features/syntax-check/syntax_check.d.ts +3 -0
- package/dist/features/syntax-check/syntax_check.js +27 -0
- package/dist/features/tool/command.d.ts +2 -0
- package/dist/features/tool/command.js +58 -7
- package/dist/lib.d.ts +1 -1
- package/dist/lib.js +1 -1
- package/dist/stm32/analysis.d.ts +40 -4
- package/dist/stm32/analysis.js +61 -5
- package/dist/stm32/toolCommand.d.ts +1 -0
- package/dist/stm32/toolCommand.js +7 -2
- package/dist/stm32/utils/listSyncFiles.js +1 -1
- package/dist/stm32/utils/makeTempProject.d.ts +16 -0
- package/dist/stm32/utils/makeTempProject.js +79 -0
- package/dist/stm32/utils/project_utils.d.ts +36 -1
- package/dist/stm32/utils/project_utils.js +48 -2
- package/mods/claude/turingcode-skills/skills/AGENTS.md +0 -5
- package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +2 -1
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +51 -43
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/M_StatusEnum.md +24 -6
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/BusMutexRegistry.hpp +138 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/Util_MutexManager.hpp +212 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver.md +72 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_ic.md +125 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_protocol.md +116 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_rtos.md +130 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_test.md +14 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/tasks-and-interrupts.md +4 -1
- package/package.json +1 -1
- package/dist/features/project/analysis_syntax_check.d.ts +0 -3
- package/dist/features/project/analysis_syntax_check.js +0 -21
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver.md +0 -97
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
|
|
5
|
+
|
|
6
|
+
## Util_MutexManager
|
|
7
|
+
|
|
8
|
+
- `COM_Driver` inherits `Util_MutexManager` under `#ifdef INC_FREERTOS_H`.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
- 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.
|
|
12
|
+
- **Mutex**: guards a blocking transfer. use from task context only, never from an ISR.
|
|
13
|
+
- **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.
|
|
14
|
+
- Every call does nothing before the scheduler starts, so drivers can init in `main()`.
|
|
15
|
+
|
|
16
|
+
| Method | Does |
|
|
17
|
+
|--------|------|
|
|
18
|
+
| `get_scheduler_is_running()` | true once the scheduler started (static) |
|
|
19
|
+
| `mutex_init(mutex_id = NULL)` | creates both primitives; adopts `mutex_id` (eg: a shared bus mutex) instead of creating one |
|
|
20
|
+
| `mutex_lock(ticks)` / `mutex_unlock()` | takes / gives the mutex |
|
|
21
|
+
| `mutex_holder()` | task holding the mutex, or NULL |
|
|
22
|
+
| `semaphore_acquire(ticks)` | marks a transfer in flight |
|
|
23
|
+
| `semaphore_release()` | ends it from a task, when the transfer failed to start |
|
|
24
|
+
| `semaphore_release_from_isr(yield)` | ends it from the complete/error callback |
|
|
25
|
+
| `get_is_released()` | true while no transfer is in flight |
|
|
26
|
+
| `wait_for_release(ticks)` | blocks until idle without taking the token |
|
|
27
|
+
|
|
28
|
+
- If the project does not have a MutexManager, copy the `references/code/Util_MutexManager.hpp` into the project.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## BusMutexRegistry
|
|
32
|
+
|
|
33
|
+
- Mutex instances for COM_Drivers are created via `BusMutexRegistry` so two drivers constructed against the SAME
|
|
34
|
+
physical peripheral handle automatically converge on the same mutex
|
|
35
|
+
|
|
36
|
+
```cpp
|
|
37
|
+
void COM_Driver::init_freeRTOS() {
|
|
38
|
+
this->mutex_init(BusMutexRegistry::acquireBusMutexFor(this->handle()));
|
|
39
|
+
}
|
|
40
|
+
bool COM_Driver::mutex_lock(TickType_t ticksToWait) {
|
|
41
|
+
if (!this->_mutex_adopted &&Util_MutexManager::get_scheduler_is_running())
|
|
42
|
+
this->init_freeRTOS();
|
|
43
|
+
return Util_MutexManager::mutex_lock(ticksToWait);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- If the project does not have a BusMutexRegistry, copy the `references/code/BusMutexRegistry.hpp` into the project.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## COM_Driver
|
|
51
|
+
|
|
52
|
+
```cpp
|
|
53
|
+
#ifdef INC_FREERTOS_H
|
|
54
|
+
#include "Util_MutexManager.hpp"
|
|
55
|
+
#endif
|
|
56
|
+
|
|
57
|
+
class COM_Driver
|
|
58
|
+
#ifdef INC_FREERTOS_H
|
|
59
|
+
: public Util_MutexManager
|
|
60
|
+
#endif
|
|
61
|
+
{
|
|
62
|
+
public:
|
|
63
|
+
#ifdef INC_FREERTOS_H
|
|
64
|
+
// adopts the shared bus mutex on first use after the scheduler starts, then locks
|
|
65
|
+
bool mutex_lock(TickType_t ticks = portMAX_DELAY);
|
|
66
|
+
#else
|
|
67
|
+
/**
|
|
68
|
+
* Bare-metal builds get no-op locks with the same names, and drivers call them unconditionally.
|
|
69
|
+
*/
|
|
70
|
+
bool mutex_lock(uint32_t = 0) { return true; }
|
|
71
|
+
bool mutex_unlock() { return true; }
|
|
72
|
+
bool semaphore_acquire(uint32_t = 0) { return true; }
|
|
73
|
+
bool semaphore_release() { return true; }
|
|
74
|
+
int semaphore_release_from_isr(bool = false) { return 0; }
|
|
75
|
+
bool get_is_released() { return true; }
|
|
76
|
+
bool wait_for_release(uint32_t = 0) { return true; }
|
|
77
|
+
#endif
|
|
78
|
+
/* ... rest of COM_Driver ... */
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* ...
|
|
82
|
+
* with FreeRTOS it also adopts the bus mutex
|
|
83
|
+
*/
|
|
84
|
+
void bind_com(...)
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## Blocking transfer
|
|
90
|
+
|
|
91
|
+
- Lock around the whole transfer and chip select, and unlock on every return path.
|
|
92
|
+
- On a bus that also runs DMA, wait for the transfer in flight after taking the mutex.
|
|
93
|
+
|
|
94
|
+
```cpp
|
|
95
|
+
M_Status COM_SPI::read(uint8_t reg, uint8_t* data, size_t len) {
|
|
96
|
+
if (!this->mutex_lock(SPI_LOCK_TICKS)) return M_StatusEnum::M_TIMEOUT;
|
|
97
|
+
this->wait_for_release(SPI_LOCK_TICKS);
|
|
98
|
+
this->cs_select();
|
|
99
|
+
M_Status status = HAL_SPI_TransmitReceive(/* ... */);
|
|
100
|
+
this->cs_free();
|
|
101
|
+
this->mutex_unlock();
|
|
102
|
+
return status;
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## DMA or IT transfer
|
|
107
|
+
|
|
108
|
+
- 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.
|
|
109
|
+
- If the start fails, release the semaphore yourself, since no ISR will fire.
|
|
110
|
+
|
|
111
|
+
```cpp
|
|
112
|
+
M_Status COM_USART::write_DMA(const uint8_t* data, size_t len) {
|
|
113
|
+
if (!this->mutex_lock(UART_LOCK_TICKS)) return M_StatusEnum::M_TIMEOUT;
|
|
114
|
+
if (!this->semaphore_acquire(UART_LOCK_TICKS)) {
|
|
115
|
+
this->mutex_unlock();
|
|
116
|
+
return M_StatusEnum::M_BUSY;
|
|
117
|
+
}
|
|
118
|
+
M_Status status = HAL_UART_Transmit_DMA(this->handle(), data, len);
|
|
119
|
+
if (status != M_StatusEnum::M_OK) this->semaphore_release();
|
|
120
|
+
this->mutex_unlock();
|
|
121
|
+
return status;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* called from HAL_UART_TxCpltCallback and HAL_UART_ErrorCallback
|
|
126
|
+
*/
|
|
127
|
+
void COM_USART::on_tx_done() {
|
|
128
|
+
this->semaphore_release_from_isr(true);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
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.
|
|
@@ -32,8 +32,11 @@ BaseType_t woken = pdFALSE;
|
|
|
32
32
|
xTaskNotifyFromISR(reader_task, EVT_IMU_READY, eSetBits, &woken);
|
|
33
33
|
portYIELD_FROM_ISR(woken);
|
|
34
34
|
```
|
|
35
|
+
- When an interrupt marks new data (UART RX event, data-ready pin, DMA complete), notify the task from it; don't make the task poll with `osDelay`. Poll only when no interrupt source exists, and say so in a comment.
|
|
36
|
+
- One notification can stand for several events: on each wake, handle everything pending (loop until no complete item is left).
|
|
37
|
+
- Set the task handle before enabling the IRQ, and reset it to `nullptr` if task creation fails.
|
|
35
38
|
- Give ISRs that call FreeRTOS an NVIC priority ≥ `configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY` (numerically).
|
|
36
|
-
- Wait with a timeout; on timeout poll the source, so a missed edge can't stall the task:
|
|
39
|
+
- Wait with a timeout as a safety net; on timeout poll the source, so a missed edge (or data a parse step left behind) can't stall the task:
|
|
37
40
|
```cpp
|
|
38
41
|
if (xTaskNotifyWait(0, UINT32_MAX, &bits, pdMS_TO_TICKS(50)) != pdTRUE
|
|
39
42
|
&& HAL_GPIO_ReadPin(IMU_DRDY_GPIO_Port, IMU_DRDY_Pin)) bits |= EVT_IMU_READY;
|
package/package.json
CHANGED
|
@@ -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
|
-
```
|