@turingcode/cli 0.3.14 → 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/contract/command.js +64 -64
- package/dist/features/probe/program_project.js +3 -3
- 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/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 +48 -47
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +50 -42
- 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/storage.md +57 -57
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/tasks-and-interrupts.md +70 -70
- package/package.json +2 -2
- 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
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
- 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.
|
|
35
|
+
- Turingcode tools have a knowledge base tool that you should use to find these information (see: `turingcode tool list`) when using the knowledge base tool first list file names then search on related files.
|
|
36
|
+
- For values that you find from references cite them: `// [CITE: ABC]` in the code.
|
|
37
|
+
- 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.
|
|
38
|
+
- timing and margin checks use the datasheet's worst-case (min/max) values, not typical when a setting depends on a margin, cite it.
|
|
39
|
+
|
|
40
|
+
- 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.
|
|
41
|
+
|
|
42
|
+
#### 3.1 Facts to check
|
|
43
|
+
|
|
44
|
+
Check at least these facts, plus any others that apply to the part, then saved a table of the checked facts into `App/facts.md`, if possible you can delegate the task of fact checking to a subagent then read the facts yourself:
|
|
45
|
+
|
|
46
|
+
- chip ID
|
|
47
|
+
- bus mode and maximum clock, checked against the definition
|
|
48
|
+
- start-up boot and reset timing
|
|
49
|
+
- turn-on and settling time after the output is enabled (samples that are not valid yet)
|
|
50
|
+
- interrupt latched/pulsed behaviour
|
|
51
|
+
- interface bits to set when the chip is used over one bus only, for example turning I2C off on an SPI-only design.
|
|
52
|
+
- "Must set to 1" bits such as DEVICE_CONF
|
|
53
|
+
- errata of both the part and of the MCU (An item applies only if the driver uses that mode or feature)
|
|
54
|
+
- 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
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
### 4 Writing Drivers
|
|
58
|
+
|
|
59
|
+
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.
|
|
60
|
+
|
|
61
|
+
- Read and follow `references/driver/driver_protocol.md` when making Communication protocol drivers (eg: USART, SPI, I2C, ...)
|
|
62
|
+
- Read and follow `references/driver/driver_ic.md` when making IC, Sensor or Component drivers
|
|
63
|
+
- If project is using RTOS (eg: FreeRTOS) also read `references/driver/driver_rtos.md` when creating drivers
|
|
64
|
+
|
|
65
|
+
### 5 Testing drivers
|
|
66
|
+
|
|
67
|
+
- After drivers are written we must also test them, a syntax-only compile does not count as tested, read `references/driver/driver_test.md`
|
|
68
|
+
|
|
69
|
+
### 6 Final Deliverable
|
|
70
|
+
|
|
71
|
+
- 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.
|
|
72
|
+
- if your task was to create a project: then you can make both the drivers and project files.
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_ic.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
- IC drivers live in `Drivers/<IC>/` and talk to the chip only through a `COM_Driver*` (`driver_protocol.md`).
|
|
7
|
+
|
|
8
|
+
## Register header
|
|
9
|
+
|
|
10
|
+
- before the driver Write the register map first. Check the knowledge base server for the part's register table before writing it.
|
|
11
|
+
- `<ic>_regs.h`:
|
|
12
|
+
```h
|
|
13
|
+
#define ABC123_WHO_AM_I 0x0F
|
|
14
|
+
#define ABC123_ID 0x6B
|
|
15
|
+
#define ABC123_CTRL1 0x10
|
|
16
|
+
#define ABC123_ODR_POS 4
|
|
17
|
+
#define ABC123_ODR_MASK (0x0F << ABC123_ODR_POS)
|
|
18
|
+
#define ABC123_SPI_READ 0x80
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## IC_Driver
|
|
22
|
+
|
|
23
|
+
- Base class for all ICs; subclasses implement the virtual methods.
|
|
24
|
+
- A feature the IC lacks (eg: writing into a read only temperature sensor) returns `M_NOT_SUPPORTED`.
|
|
25
|
+
- read appends one sample at buf[*index]
|
|
26
|
+
|
|
27
|
+
```cpp
|
|
28
|
+
class IC_Driver {
|
|
29
|
+
public:
|
|
30
|
+
explicit IC_Driver(COM_Driver* com) : _com(com) {}
|
|
31
|
+
virtual M_Status init() = 0;
|
|
32
|
+
virtual M_Status reset() = 0;
|
|
33
|
+
virtual M_Status read(uint8_t* buf, size_t* index) = 0;
|
|
34
|
+
|
|
35
|
+
bool is_interrupt_pin_set() const { return this->com()->is_interrupt_pin_set(); }
|
|
36
|
+
protected:
|
|
37
|
+
COM_Driver* com() const{ return this->_com; }
|
|
38
|
+
private:
|
|
39
|
+
COM_Driver* _com;
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Sensor driver
|
|
44
|
+
|
|
45
|
+
- A sensor driver never creates its COM driver, The owner wires the sensor and com driver together:
|
|
46
|
+
- The driver is generic; the project's choices (range, ODR, filters, FIFO watermark, interrupt routing, oversampling, ...) belong to the app, not the driver.
|
|
47
|
+
- Values that depend on the config (sensitivity per LSB, sample period, settling time) are computed from `_cfg`, never fixed constants.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
```cpp
|
|
51
|
+
class Sensor_ABC123 : public IC_Driver {
|
|
52
|
+
public:
|
|
53
|
+
using IC_Driver::IC_Driver;
|
|
54
|
+
M_Status init() override;
|
|
55
|
+
M_Status reset() override;
|
|
56
|
+
M_Status read(uint8_t* buf, size_t* index) override;
|
|
57
|
+
M_Status verify_id();
|
|
58
|
+
|
|
59
|
+
bool is_interrupt_pin_set() const;
|
|
60
|
+
|
|
61
|
+
/* ---- Config ---- */
|
|
62
|
+
enum class CFG_ODR : uint8_t { OFF = 0x0, HZ_104 = 0x4, HZ_416 = 0x6 };
|
|
63
|
+
enum class CFG_RANGE : uint8_t { G2 = 0x0, G4 = 0x2, G8 = 0x3 };
|
|
64
|
+
struct Config {
|
|
65
|
+
CFG_ODR odr = CFG_ODR::HZ_104;
|
|
66
|
+
CFG_RANGE range = CFG_RANGE::G2;
|
|
67
|
+
};
|
|
68
|
+
M_Status set_config(const Config& cfg);
|
|
69
|
+
private:
|
|
70
|
+
Config _cfg;
|
|
71
|
+
};
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```cpp
|
|
75
|
+
COM_SPI spi_imu(&hspi1, IMU_CS_GPIO_Port, IMU_CS_Pin);
|
|
76
|
+
Sensor_ABC123 imu(&spi_imu);
|
|
77
|
+
imu.set_config(...);//with required config in the app
|
|
78
|
+
imu.init();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- 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`).
|
|
82
|
+
|
|
83
|
+
### init() order
|
|
84
|
+
|
|
85
|
+
1. Set bus address masks before the first access
|
|
86
|
+
2. check the ID register first. If it fails return.
|
|
87
|
+
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.
|
|
88
|
+
- samples lost during the init sequence should not count as discared or lost, because there is no safe way for us to retrive them.
|
|
89
|
+
4. set range, filters and options, then enable the output rate, unless the datasheet gives another order.
|
|
90
|
+
- never hand settling samples as valid data: wait out the settling time before starting the FIFO or interrupt.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
- use `write_verify` to check the bits that are written to be set.
|
|
94
|
+
|
|
95
|
+
```cpp
|
|
96
|
+
M_Status Sensor_ABC123::init() {
|
|
97
|
+
// set bus address masks before the first access
|
|
98
|
+
this->com()->set_address_mask(ABC123_SPI_READ, 0, 0, 0);
|
|
99
|
+
|
|
100
|
+
M_Status status = this->verify_id();
|
|
101
|
+
if (status != M_StatusEnum::M_OK) return status;
|
|
102
|
+
status |= this->reset();
|
|
103
|
+
if (status != M_StatusEnum::M_OK) return status;
|
|
104
|
+
|
|
105
|
+
// range, filters and other configs are set before setting the ODR
|
|
106
|
+
status |= this->com()->write_verify(ABC123_CTRL2, (uint8_t)this->_cfg.range << ABC123_FS_POS, 3);
|
|
107
|
+
|
|
108
|
+
// set ODR last as it usually powers up and starts the board
|
|
109
|
+
status |= this->com()->write_verify(ABC123_CTRL1, (uint8_t)this->_cfg.odr << ABC123_ODR_POS, 3, ABC123_CTRL1_RW_MASK);
|
|
110
|
+
return status;
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### read()
|
|
115
|
+
|
|
116
|
+
- Read one sample in one burst (auto-increment, block data update if available), so all axes come from the same sample.
|
|
117
|
+
- Return only fresh data: check data-ready if polling (status bit) and return `M_BUSY` when there is none, for DRDY interrupt based reads data should be fresh so no need for checking again.
|
|
118
|
+
- change the caller's buffer and `*index` on successful reads only.
|
|
119
|
+
|
|
120
|
+
### Interrupt pin
|
|
121
|
+
|
|
122
|
+
- Set the chip's interrupt output to latched or level where the chip supports it, so a high pin means unread data.
|
|
123
|
+
- missed edge recovery (watchdogs, re-arming) should be handled in the app task not in the drivers, when working on projects that go beyond making drivers only, see `references/tasks-and-interrupts.md` also.
|
|
124
|
+
|
|
125
|
+
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
|
|
9
|
+
|
|
10
|
+
```cpp
|
|
11
|
+
class COM_Driver {
|
|
12
|
+
public:
|
|
13
|
+
// must be implemented in the subclass
|
|
14
|
+
virtual M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) = 0;
|
|
15
|
+
virtual M_Status write(uint8_t reg, const uint8_t* data, size_t len) = 0;
|
|
16
|
+
|
|
17
|
+
// read, modify, write
|
|
18
|
+
M_Status write_bit(uint8_t reg, uint8_t bit, uint8_t value);
|
|
19
|
+
// write, read, compare under mask
|
|
20
|
+
M_Status write_verify(uint8_t reg, uint8_t value, size_t tries, uint8_t mask = 0xFF);
|
|
21
|
+
|
|
22
|
+
// keep retrying until (reg & mask) == expected
|
|
23
|
+
M_Status wait_for_register(uint8_t reg, uint8_t expected, size_t tries, uint8_t mask = 0xFF);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @brief address on the wire = (reg & ~clear) | set
|
|
27
|
+
*/
|
|
28
|
+
void set_address_mask(uint8_t read_set, uint8_t read_clear,
|
|
29
|
+
uint8_t write_set, uint8_t write_clear);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @brief Stores the bus and DMA handles and device address (if needed)
|
|
33
|
+
*/
|
|
34
|
+
void bind_com(void* handle,
|
|
35
|
+
uint16_t dev_addr,
|
|
36
|
+
DMA_HandleTypeDef* rx_dma=nullptr,
|
|
37
|
+
DMA_HandleTypeDef* tx_dma=nullptr);
|
|
38
|
+
|
|
39
|
+
public:
|
|
40
|
+
bool isBusy() const;
|
|
41
|
+
void clearBusy();
|
|
42
|
+
/**
|
|
43
|
+
* @brief isHandle and handle are non-virtual.
|
|
44
|
+
* Subclasses add a *typed version that hides them.
|
|
45
|
+
*/
|
|
46
|
+
bool isHandle(void *check)const;
|
|
47
|
+
void* handle() const;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @brief level of the device's interrupt pin; false when the bus driver has no pin
|
|
51
|
+
*/
|
|
52
|
+
virtual bool is_interrupt_pin_set() const { return false; }
|
|
53
|
+
|
|
54
|
+
private:
|
|
55
|
+
uint16_t _dev_addr;
|
|
56
|
+
uint16_t _mask_read_set;
|
|
57
|
+
uint16_t _mask_read_clear;
|
|
58
|
+
uint16_t _mask_write_set;
|
|
59
|
+
uint16_t _mask_write_clear;
|
|
60
|
+
|
|
61
|
+
protected:
|
|
62
|
+
void* _handle=nullptr;
|
|
63
|
+
volatile bool flag_busy=false;
|
|
64
|
+
DMA_HandleTypeDef* _tx_dma=nullptr;
|
|
65
|
+
DMA_HandleTypeDef* _rx_dma=nullptr;
|
|
66
|
+
};
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Protocol driver
|
|
70
|
+
|
|
71
|
+
- Owns the HAL handle and chip select; the IC driver never sees them.
|
|
72
|
+
|
|
73
|
+
### Example 1:
|
|
74
|
+
```cpp
|
|
75
|
+
class COM_SPI : public COM_Driver {
|
|
76
|
+
public:
|
|
77
|
+
COM_SPI(SPI_HandleTypeDef* hspi, GPIO_TypeDef* cs_port, uint16_t cs_pin);
|
|
78
|
+
M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) override;
|
|
79
|
+
M_Status write(uint8_t reg, const uint8_t* data, size_t len) override;
|
|
80
|
+
|
|
81
|
+
bool isHandle(SPI_HandleTypeDef *check) const {
|
|
82
|
+
return this->handle() == check;
|
|
83
|
+
}
|
|
84
|
+
SPI_HandleTypeDef* handle() const {
|
|
85
|
+
return (SPI_HandleTypeDef*)_handle;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
bool is_interrupt_pin_set() const override {
|
|
89
|
+
return this->_int_port != nullptr && HAL_GPIO_ReadPin(this->_int_port, this->_int_pin) == GPIO_PIN_SET;
|
|
90
|
+
}
|
|
91
|
+
bool is_interrupt_pin(uint16_t pin) const { return this->_int_port != nullptr && this->_int_pin == pin; }
|
|
92
|
+
|
|
93
|
+
private:
|
|
94
|
+
GPIO_TypeDef* _cs_port;
|
|
95
|
+
uint16_t _cs_pin;
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Example 2:
|
|
100
|
+
```cpp
|
|
101
|
+
class COM_USART : public COM_Driver {
|
|
102
|
+
public:
|
|
103
|
+
explicit COM_USART(UART_HandleTypeDef* huart);
|
|
104
|
+
|
|
105
|
+
M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) override;
|
|
106
|
+
M_Status write(uint8_t reg, const uint8_t* data, size_t len) override;
|
|
107
|
+
bool isHandle(UART_HandleTypeDef *check) const {
|
|
108
|
+
return this->handle() == check;
|
|
109
|
+
}
|
|
110
|
+
UART_HandleTypeDef* handle() const {
|
|
111
|
+
return (UART_HandleTypeDef*)_handle;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
```
|
|
115
|
+
- read/write `reg` is ignored for USART
|
|
116
|
+
- `COM_USART` has no registers, so `write_bit`, `write_verify` and `wait_for_register` don't apply to it.
|
|
@@ -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.
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
# Storage
|
|
2
|
-
|
|
3
|
-
- Use this when saving settings, calibration or other data to flash or EEPROM.
|
|
4
|
-
|
|
5
|
-
## Register array
|
|
6
|
-
|
|
7
|
-
- Keep all settings in one byte array addressed by offset. RAM is the working copy; save and load it as a whole.
|
|
8
|
-
- Derive each offset from the previous one with `sizeof`:
|
|
9
|
-
```cpp
|
|
10
|
-
#define REG_CTRL 0 // uint8
|
|
11
|
-
#define REG_CALIB_ACC 1 // 3 × float
|
|
12
|
-
#define REG_COUNT (REG_CALIB_ACC + 3 * sizeof(float))
|
|
13
|
-
```
|
|
14
|
-
- Save only settings, never live status or measurements.
|
|
15
|
-
- Copy multi-byte values with `memcpy`, never through a cast pointer; offsets are unaligned and an unaligned `float` access faults.
|
|
16
|
-
- Keep defaults in one `static constexpr uint8_t REG_DEFAULTS[REG_COUNT]` table; `load_defaults()` copies it in.
|
|
17
|
-
- Bound every setter over the whole write:
|
|
18
|
-
```cpp
|
|
19
|
-
if (reg >= REG_COUNT || len > REG_COUNT - reg) return M_StatusEnum::M_OUT_OF_RANGE;
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Settings page
|
|
23
|
-
|
|
24
|
-
- Use the last flash page, and shrink `FLASH` in the linker script by it so code can't land there:
|
|
25
|
-
```ld
|
|
26
|
-
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K - 2K /* last 2 KB page: settings */
|
|
27
|
-
```
|
|
28
|
-
- Take the page size, program type and erase fields from the HAL, not from memory: `FLASH_PAGE_SIZE`, `FLASH_TYPEPROGRAM_*`, and `Page` or `PageAddress` in `FLASH_EraseInitTypeDef`.
|
|
29
|
-
- Derive the write unit from the program type (`sizeof(uint64_t)` for `DOUBLEWORD`). Align every block to it and pad with `0xFF`.
|
|
30
|
-
- Check every HAL flash return, and `PageError == 0xFFFFFFFF` after erase.
|
|
31
|
-
|
|
32
|
-
## Header
|
|
33
|
-
|
|
34
|
-
- Start the page with one 16-byte header for the whole array, then the registers:
|
|
35
|
-
```
|
|
36
|
-
[ magic u32 | version u16 | crc u32 | pad ][ registers ... ]
|
|
37
|
-
0 4 6 10 16
|
|
38
|
-
```
|
|
39
|
-
- `magic`: a fixed constant; blank or foreign flash won't match.
|
|
40
|
-
- `version`: bump it when an offset moves, a register is added or removed, or a new default must replace saved values.
|
|
41
|
-
- `crc`: over the registers; catches a cut-off save or a flipped bit.
|
|
42
|
-
- On load, if the read fails or any field mismatches, load the defaults and return `M_ERROR`. The caller logs it and keeps running; it's expected on first boot.
|
|
43
|
-
|
|
44
|
-
## Writing
|
|
45
|
-
|
|
46
|
-
- Write in this order: build the header with the new CRC, unlock, erase, program the header, program the registers, lock, then read back and compare header and registers.
|
|
47
|
-
- Erase or program only after a successful unlock, and stop at the first failure. Always lock, even after a failure:
|
|
48
|
-
```cpp
|
|
49
|
-
M_Status status = this->flash_unlock();
|
|
50
|
-
if (status == M_StatusEnum::M_OK) status |= this->flash_erase_page(SETTINGS_PAGE);
|
|
51
|
-
if (status == M_StatusEnum::M_OK) status |= this->flash_program(0, hdr, HDR_LEN);
|
|
52
|
-
if (status == M_StatusEnum::M_OK) status |= this->flash_program(HDR_LEN, this->_regs, REG_COUNT);
|
|
53
|
-
status |= this->flash_lock();
|
|
54
|
-
if (status == M_StatusEnum::M_OK) status = this->verify(hdr);
|
|
55
|
-
```
|
|
56
|
-
- Give your own busy-bit polling a timeout; the HAL calls already have one.
|
|
57
|
-
- Save from a task or the main loop, never from an ISR.
|
|
1
|
+
# Storage
|
|
2
|
+
|
|
3
|
+
- Use this when saving settings, calibration or other data to flash or EEPROM.
|
|
4
|
+
|
|
5
|
+
## Register array
|
|
6
|
+
|
|
7
|
+
- Keep all settings in one byte array addressed by offset. RAM is the working copy; save and load it as a whole.
|
|
8
|
+
- Derive each offset from the previous one with `sizeof`:
|
|
9
|
+
```cpp
|
|
10
|
+
#define REG_CTRL 0 // uint8
|
|
11
|
+
#define REG_CALIB_ACC 1 // 3 × float
|
|
12
|
+
#define REG_COUNT (REG_CALIB_ACC + 3 * sizeof(float))
|
|
13
|
+
```
|
|
14
|
+
- Save only settings, never live status or measurements.
|
|
15
|
+
- Copy multi-byte values with `memcpy`, never through a cast pointer; offsets are unaligned and an unaligned `float` access faults.
|
|
16
|
+
- Keep defaults in one `static constexpr uint8_t REG_DEFAULTS[REG_COUNT]` table; `load_defaults()` copies it in.
|
|
17
|
+
- Bound every setter over the whole write:
|
|
18
|
+
```cpp
|
|
19
|
+
if (reg >= REG_COUNT || len > REG_COUNT - reg) return M_StatusEnum::M_OUT_OF_RANGE;
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Settings page
|
|
23
|
+
|
|
24
|
+
- Use the last flash page, and shrink `FLASH` in the linker script by it so code can't land there:
|
|
25
|
+
```ld
|
|
26
|
+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K - 2K /* last 2 KB page: settings */
|
|
27
|
+
```
|
|
28
|
+
- Take the page size, program type and erase fields from the HAL, not from memory: `FLASH_PAGE_SIZE`, `FLASH_TYPEPROGRAM_*`, and `Page` or `PageAddress` in `FLASH_EraseInitTypeDef`.
|
|
29
|
+
- Derive the write unit from the program type (`sizeof(uint64_t)` for `DOUBLEWORD`). Align every block to it and pad with `0xFF`.
|
|
30
|
+
- Check every HAL flash return, and `PageError == 0xFFFFFFFF` after erase.
|
|
31
|
+
|
|
32
|
+
## Header
|
|
33
|
+
|
|
34
|
+
- Start the page with one 16-byte header for the whole array, then the registers:
|
|
35
|
+
```
|
|
36
|
+
[ magic u32 | version u16 | crc u32 | pad ][ registers ... ]
|
|
37
|
+
0 4 6 10 16
|
|
38
|
+
```
|
|
39
|
+
- `magic`: a fixed constant; blank or foreign flash won't match.
|
|
40
|
+
- `version`: bump it when an offset moves, a register is added or removed, or a new default must replace saved values.
|
|
41
|
+
- `crc`: over the registers; catches a cut-off save or a flipped bit.
|
|
42
|
+
- On load, if the read fails or any field mismatches, load the defaults and return `M_ERROR`. The caller logs it and keeps running; it's expected on first boot.
|
|
43
|
+
|
|
44
|
+
## Writing
|
|
45
|
+
|
|
46
|
+
- Write in this order: build the header with the new CRC, unlock, erase, program the header, program the registers, lock, then read back and compare header and registers.
|
|
47
|
+
- Erase or program only after a successful unlock, and stop at the first failure. Always lock, even after a failure:
|
|
48
|
+
```cpp
|
|
49
|
+
M_Status status = this->flash_unlock();
|
|
50
|
+
if (status == M_StatusEnum::M_OK) status |= this->flash_erase_page(SETTINGS_PAGE);
|
|
51
|
+
if (status == M_StatusEnum::M_OK) status |= this->flash_program(0, hdr, HDR_LEN);
|
|
52
|
+
if (status == M_StatusEnum::M_OK) status |= this->flash_program(HDR_LEN, this->_regs, REG_COUNT);
|
|
53
|
+
status |= this->flash_lock();
|
|
54
|
+
if (status == M_StatusEnum::M_OK) status = this->verify(hdr);
|
|
55
|
+
```
|
|
56
|
+
- Give your own busy-bit polling a timeout; the HAL calls already have one.
|
|
57
|
+
- Save from a task or the main loop, never from an ISR.
|