@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.
- 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 +5 -4
- 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 +33 -8
- 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 +2 -1
- 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 +30 -9
- 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/COM_Driver.hpp +97 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_I2C.hpp +83 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_SPI.hpp +69 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_USART.hpp +58 -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 +81 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_ic.md +123 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_protocol.md +16 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_rtos.md +129 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/driver/driver_test.md +17 -0
- 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,138 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
/**
|
|
3
|
+
* Automatically include FreeRTOS when the project has it.
|
|
4
|
+
*/
|
|
5
|
+
#if defined(__has_include)
|
|
6
|
+
#if __has_include("FreeRTOS.h")
|
|
7
|
+
#include "FreeRTOS.h"
|
|
8
|
+
#endif
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
#ifdef INC_FREERTOS_H
|
|
13
|
+
#include <atomic>
|
|
14
|
+
extern "C" {
|
|
15
|
+
#include "cmsis_os.h"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Shared-bus mutex registry, generic over any HAL handle type
|
|
20
|
+
* (SPI_HandleTypeDef*, UART_HandleTypeDef*, I2C_HandleTypeDef*, ...).
|
|
21
|
+
*
|
|
22
|
+
* Keys by raw pointer identity: two drivers constructed against the SAME
|
|
23
|
+
* physical peripheral handle automatically converge on the same mutex,
|
|
24
|
+
* regardless of what driver class or handle type is asking. Drivers on
|
|
25
|
+
* different peripherals get independent mutexes.
|
|
26
|
+
*
|
|
27
|
+
* Thread-safety: safe to call from several tasks at once, after the
|
|
28
|
+
* scheduler started; the registry guard protects the table.
|
|
29
|
+
*/
|
|
30
|
+
class BusMutexRegistry {
|
|
31
|
+
public:
|
|
32
|
+
/**
|
|
33
|
+
* Get-or-create the mutex associated with `handle`. Accepts any
|
|
34
|
+
* pointer type - callers pass e.g. this->handle() directly and it
|
|
35
|
+
* gets reinterpreted to void* for keying purposes only, never
|
|
36
|
+
* dereferenced.
|
|
37
|
+
*/
|
|
38
|
+
template <typename HandleT>
|
|
39
|
+
static osMutexId_t acquireBusMutexFor(HandleT* handle) {
|
|
40
|
+
return acquireBusMutexForVoid(static_cast<const void*>(handle));
|
|
41
|
+
}
|
|
42
|
+
template <typename HandleT, typename OwnerT>
|
|
43
|
+
static void setActiveOwner(HandleT* handle, OwnerT* owner) {
|
|
44
|
+
setActiveOwnerVoid(static_cast<const void*>(handle),
|
|
45
|
+
static_cast<void*>(owner));
|
|
46
|
+
}
|
|
47
|
+
template <typename HandleT, typename OwnerT>
|
|
48
|
+
static OwnerT* activeOwner(HandleT* handle) {
|
|
49
|
+
return static_cast<OwnerT*>(activeOwnerVoid(static_cast<const void*>(handle)));
|
|
50
|
+
}
|
|
51
|
+
template <typename HandleT>
|
|
52
|
+
static void clearActiveOwner(HandleT* handle) {
|
|
53
|
+
clearActiveOwnerVoid(static_cast<const void*>(handle));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private:
|
|
57
|
+
static constexpr size_t MAX_BUSES = 4; // total across SPI+UART+I2C+... raise if needed
|
|
58
|
+
static inline const void* ownerKeys[MAX_BUSES] = {};
|
|
59
|
+
static inline void* ownerValues[MAX_BUSES] = {};
|
|
60
|
+
static inline size_t ownerCount = 0;
|
|
61
|
+
/**
|
|
62
|
+
* Created on first use, race-free: the firmware builds with
|
|
63
|
+
* -fno-threadsafe-statics, so a plain static initializer could run in
|
|
64
|
+
* two tasks at once. The loser deletes its mutex and uses the winner's.
|
|
65
|
+
*/
|
|
66
|
+
static osMutexId_t getRegistryGuard() {
|
|
67
|
+
static std::atomic<osMutexId_t> guard{NULL}; // constant-initialized, no init race
|
|
68
|
+
osMutexId_t current = guard.load(std::memory_order_acquire);
|
|
69
|
+
if (current != NULL) return current;
|
|
70
|
+
osMutexId_t fresh = osMutexNew(NULL);
|
|
71
|
+
if (fresh == NULL) return NULL;
|
|
72
|
+
osMutexId_t expected = NULL;
|
|
73
|
+
if (guard.compare_exchange_strong(expected, fresh, std::memory_order_acq_rel)) return fresh;
|
|
74
|
+
osMutexDelete(fresh);
|
|
75
|
+
return expected;
|
|
76
|
+
}
|
|
77
|
+
static osMutexId_t acquireBusMutexForVoid(const void* key) {
|
|
78
|
+
osMutexId_t registryGuard = getRegistryGuard();
|
|
79
|
+
configASSERT(registryGuard != NULL);
|
|
80
|
+
|
|
81
|
+
osMutexAcquire(registryGuard, osWaitForever);
|
|
82
|
+
|
|
83
|
+
static const void* keys[MAX_BUSES] = {};
|
|
84
|
+
static osMutexId_t values[MAX_BUSES] = {};
|
|
85
|
+
static size_t count = 0;
|
|
86
|
+
|
|
87
|
+
osMutexId_t result = nullptr;
|
|
88
|
+
for (size_t i = 0; i < count; ++i) {
|
|
89
|
+
if (keys[i] == key) { result = values[i]; break; }
|
|
90
|
+
}
|
|
91
|
+
if (result == nullptr) {
|
|
92
|
+
configASSERT(count < MAX_BUSES); // raise MAX_BUSES if this trips
|
|
93
|
+
result = osMutexNew(NULL);
|
|
94
|
+
configASSERT(result != NULL);
|
|
95
|
+
keys[count] = key;
|
|
96
|
+
values[count] = result;
|
|
97
|
+
count++;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
osMutexRelease(registryGuard);
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
static void clearActiveOwnerVoid(const void* key) {
|
|
104
|
+
taskENTER_CRITICAL();
|
|
105
|
+
for (size_t i = 0; i < ownerCount; ++i) {
|
|
106
|
+
if (ownerKeys[i] == key) {
|
|
107
|
+
ownerValues[i] = nullptr; // or compact the table if you want
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
taskEXIT_CRITICAL();
|
|
112
|
+
}
|
|
113
|
+
static void setActiveOwnerVoid(const void* key, void* owner) {
|
|
114
|
+
taskENTER_CRITICAL();
|
|
115
|
+
size_t slot = MAX_BUSES;
|
|
116
|
+
for (size_t i = 0; i < ownerCount; ++i) {
|
|
117
|
+
if (ownerKeys[i] == key) { slot = i; break; }
|
|
118
|
+
}
|
|
119
|
+
if (slot == MAX_BUSES) {
|
|
120
|
+
configASSERT(ownerCount < MAX_BUSES); // raise MAX_BUSES if n
|
|
121
|
+
slot = ownerCount++;
|
|
122
|
+
ownerKeys[slot] = key;
|
|
123
|
+
}
|
|
124
|
+
ownerValues[slot] = owner;
|
|
125
|
+
taskEXIT_CRITICAL();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static void* activeOwnerVoid(const void* key) {
|
|
129
|
+
void* result = nullptr;
|
|
130
|
+
UBaseType_t saved = taskENTER_CRITICAL_FROM_ISR();
|
|
131
|
+
for (size_t i = 0; i < ownerCount; ++i) {
|
|
132
|
+
if (ownerKeys[i] == key) { result = ownerValues[i]; break; }
|
|
133
|
+
}
|
|
134
|
+
taskEXIT_CRITICAL_FROM_ISR(saved);
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
#endif
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_Driver.hpp
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// ... includes ... //
|
|
4
|
+
|
|
5
|
+
class COM_Driver {
|
|
6
|
+
public:
|
|
7
|
+
// must be implemented in the subclass
|
|
8
|
+
virtual M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) = 0;
|
|
9
|
+
virtual M_Status write(uint8_t reg, const uint8_t* data, size_t len) = 0;
|
|
10
|
+
|
|
11
|
+
// read, modify, write
|
|
12
|
+
M_Status write_bit(uint8_t reg, uint8_t bit, uint8_t value);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @brief write a register and verify it; retry if it fails
|
|
16
|
+
* @returns
|
|
17
|
+
* M_OK: written and verified,
|
|
18
|
+
* M_MISMATCH: written but read back differently.
|
|
19
|
+
* ...other possible errors...
|
|
20
|
+
*/
|
|
21
|
+
M_Status write_verify(uint8_t reg, uint8_t value, size_t tries, uint8_t mask = 0xFF);
|
|
22
|
+
|
|
23
|
+
// keep retrying until (reg & mask) == expected
|
|
24
|
+
M_Status wait_for_register(uint8_t reg, uint8_t expected, size_t tries, uint8_t mask = 0xFF);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @brief set the read/write address mask for the device;
|
|
28
|
+
*
|
|
29
|
+
* sent address = (reg & ~clear) | set = (reg & ~clear) | set
|
|
30
|
+
*/
|
|
31
|
+
void set_address_mask(uint8_t read_set, uint8_t read_clear,
|
|
32
|
+
uint8_t write_set, uint8_t write_clear);
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// ---- Busy check ---- //
|
|
36
|
+
/**
|
|
37
|
+
* when are not using RTOS mutexes to protect the bus,
|
|
38
|
+
* we need a busy flag to avoid trying to send/recv data
|
|
39
|
+
* while busy for calls that dont block during the transfer (DMA, Interrupt, ...)
|
|
40
|
+
*
|
|
41
|
+
* for full duplex bus lines we can use 2 independent flags
|
|
42
|
+
*/
|
|
43
|
+
public:
|
|
44
|
+
/**
|
|
45
|
+
* @brief take the bus; false when another transfer already owns it.
|
|
46
|
+
*
|
|
47
|
+
* avoid pattenrs like:
|
|
48
|
+
* if (!isBusy()) { ... setBusy(); ... }
|
|
49
|
+
* because another thread could set it between the check and the set.
|
|
50
|
+
*/
|
|
51
|
+
bool trySetBusy() { return !this->flag_busy_.exchange(true, std::memory_order_acquire); }
|
|
52
|
+
bool isBusy() const { return flag_busy_.load(std::memory_order_acquire); }
|
|
53
|
+
void setBusy() { flag_busy_.store(true, std::memory_order_release); }
|
|
54
|
+
void clearBusy() { flag_busy_.store(false, std::memory_order_release); }
|
|
55
|
+
/**
|
|
56
|
+
* std::atomic is the more MISRA-friendly choice than __atomic_*
|
|
57
|
+
* if <atomic> is not supported then we can use:
|
|
58
|
+
*/
|
|
59
|
+
//// bool isBusy() const { return __atomic_load_n(&flag_busy_, __ATOMIC_ACQUIRE) != 0; }
|
|
60
|
+
//// void setBusy() { __atomic_store_n(&flag_busy_, 1, __ATOMIC_RELEASE); }
|
|
61
|
+
//// void clearBusy() { __atomic_store_n(&flag_busy_, 0, __ATOMIC_RELEASE); }
|
|
62
|
+
protected:
|
|
63
|
+
volatile bool flag_busy=false;
|
|
64
|
+
|
|
65
|
+
// ---- Handle ---- //
|
|
66
|
+
public:
|
|
67
|
+
/**
|
|
68
|
+
* @brief Stores the bus and DMA handles and device address (if needed)
|
|
69
|
+
*/
|
|
70
|
+
void bind_com(void* handle,
|
|
71
|
+
uint16_t dev_addr,
|
|
72
|
+
DMA_HandleTypeDef* rx_dma=nullptr,
|
|
73
|
+
DMA_HandleTypeDef* tx_dma=nullptr);
|
|
74
|
+
/**
|
|
75
|
+
* @brief isHandle and handle are non-virtual.
|
|
76
|
+
* Subclasses add a *typed version that hides them.
|
|
77
|
+
*/
|
|
78
|
+
bool isHandle(void *check)const;
|
|
79
|
+
void* handle() const;
|
|
80
|
+
protected:
|
|
81
|
+
void* _handle=nullptr;
|
|
82
|
+
DMA_HandleTypeDef* _tx_dma=nullptr;
|
|
83
|
+
DMA_HandleTypeDef* _rx_dma=nullptr;
|
|
84
|
+
|
|
85
|
+
public:
|
|
86
|
+
/**
|
|
87
|
+
* @brief level of the device's interrupt pin; false when the bus driver has no pin
|
|
88
|
+
*/
|
|
89
|
+
virtual bool is_interrupt_pin_set() const { return false; }
|
|
90
|
+
|
|
91
|
+
private:
|
|
92
|
+
uint16_t _dev_addr;
|
|
93
|
+
uint16_t _mask_read_set;
|
|
94
|
+
uint16_t _mask_read_clear;
|
|
95
|
+
uint16_t _mask_write_set;
|
|
96
|
+
uint16_t _mask_write_clear;
|
|
97
|
+
};
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_I2C.hpp
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// ... includes ... //
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @brief I2C bus driver
|
|
7
|
+
*
|
|
8
|
+
* - `read`/`write` map to `HAL_I2C_Mem_Read`/`HAL_I2C_Mem_Write` with `reg` as the memory address.
|
|
9
|
+
* - `init()` is for I2C with a `TIMINGR` register (G4, L4, H7, F7). On F1/F4 the speed is `Init.ClockSpeed` instead.
|
|
10
|
+
* - The bitrate uses worst-case spec edge times, so it is slower than the real bus; that is the safe side for a timeout.
|
|
11
|
+
* - A slave can stretch SCL, so give `I2C_TIMEOUT_MARGIN_MS` room for the slowest device on the bus.
|
|
12
|
+
*/
|
|
13
|
+
class COM_I2C : public COM_Driver {
|
|
14
|
+
public:
|
|
15
|
+
/**
|
|
16
|
+
* @param dev_addr 7-bit device address; HAL wants it shifted left by 1
|
|
17
|
+
*/
|
|
18
|
+
COM_I2C(I2C_HandleTypeDef* hi2c, uint16_t dev_addr);
|
|
19
|
+
M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) override;
|
|
20
|
+
M_Status write(uint8_t reg, const uint8_t* data, size_t len) override;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @brief call after HAL_I2C_Init and the filter config; reads the live CR1
|
|
24
|
+
*/
|
|
25
|
+
void init() {
|
|
26
|
+
I2C_TypeDef* i2c = this->handle()->Instance;
|
|
27
|
+
// kernel clock actually selected in RCC
|
|
28
|
+
uint32_t clk = 0;
|
|
29
|
+
if (i2c == I2C1) clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2C1);
|
|
30
|
+
else if (i2c == I2C2) clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2C2);
|
|
31
|
+
#ifdef I2C3
|
|
32
|
+
else if (i2c == I2C3) clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2C3);
|
|
33
|
+
#endif
|
|
34
|
+
#ifdef I2C4
|
|
35
|
+
else if (i2c == I2C4) clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2C4);
|
|
36
|
+
#endif
|
|
37
|
+
if (clk == 0) return;
|
|
38
|
+
|
|
39
|
+
// t_SCL = t_SYNC1 + t_SYNC2 + (SCLL+1 + SCLH+1) * (PRESC+1) * t_I2CCLK
|
|
40
|
+
uint32_t t = this->handle()->Init.Timing;
|
|
41
|
+
uint32_t presc = ((t & I2C_TIMINGR_PRESC) >> I2C_TIMINGR_PRESC_Pos) + 1u;
|
|
42
|
+
uint32_t scll = ((t & I2C_TIMINGR_SCLL) >> I2C_TIMINGR_SCLL_Pos) + 1u;
|
|
43
|
+
uint32_t sclh = ((t & I2C_TIMINGR_SCLH) >> I2C_TIMINGR_SCLH_Pos) + 1u;
|
|
44
|
+
uint32_t cycles = presc * (scll + sclh);
|
|
45
|
+
|
|
46
|
+
// worst-case rise + fall from the I2C spec, picked by the nominal speed
|
|
47
|
+
uint32_t nominal = clk / cycles;
|
|
48
|
+
uint32_t edge_ns = (nominal <= 150000u) ? (1000u + 300u) // standard mode
|
|
49
|
+
: (nominal <= 600000u) ? (300u + 300u) // fast mode
|
|
50
|
+
: (120u + 120u); // fast mode plus
|
|
51
|
+
uint32_t cr1 = i2c->CR1;
|
|
52
|
+
// analog filter max delay, once per edge; check the datasheet value for the part
|
|
53
|
+
if ((cr1 & I2C_CR1_ANFOFF) == 0) edge_ns += 2u * 260u;
|
|
54
|
+
uint32_t dnf = (cr1 & I2C_CR1_DNF) >> I2C_CR1_DNF_Pos;
|
|
55
|
+
|
|
56
|
+
// sync delays: digital filter + ~3 clocks resync per edge, plus the edge times
|
|
57
|
+
cycles += 2u * (dnf + 3u);
|
|
58
|
+
cycles += (uint32_t)(((uint64_t)edge_ns * clk + 999999999u) / 1000000000u);
|
|
59
|
+
this->__bitrate = clk / cycles;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @brief get maximum allowed timeout for a packet
|
|
64
|
+
* @param len transfered data length in bytes
|
|
65
|
+
*/
|
|
66
|
+
uint32_t __packet_timeout_ms(size_t len) const {
|
|
67
|
+
if (this->__bitrate == 0) return I2C_TIMEOUT_MARGIN_MS;
|
|
68
|
+
// 9 bits per byte (8 data + ACK); +3 bytes for address, register, repeated-start address
|
|
69
|
+
uint32_t bits = ((uint32_t)len + 3u) * 9u;
|
|
70
|
+
uint32_t frame_ms = (bits * 1000u + this->__bitrate - 1u) / this->__bitrate;
|
|
71
|
+
return frame_ms + I2C_TIMEOUT_MARGIN_MS;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
bool isHandle(I2C_HandleTypeDef *check) const {
|
|
75
|
+
return this->handle() == check;
|
|
76
|
+
}
|
|
77
|
+
I2C_HandleTypeDef* handle() const {
|
|
78
|
+
return (I2C_HandleTypeDef*)_handle;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private:
|
|
82
|
+
uint32_t __bitrate = 0;
|
|
83
|
+
};
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_SPI.hpp
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// ... includes ... //
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @brief SPI bus driver
|
|
7
|
+
*
|
|
8
|
+
* - `init()` is for SPI with `CR2 DS` (G4, L4, F7). On F1/F4 the frame size is `SPI_CR1_DFF` (set = 16 bits), and H7 uses `CFG1` for both the prescaler and the frame size.
|
|
9
|
+
* - `SPI_FRAME_GAP_US` is the CPU time per frame in polling/IT mode; a few µs is typical, measure it on the board if the timeout is tight.
|
|
10
|
+
*/
|
|
11
|
+
class COM_SPI : public COM_Driver {
|
|
12
|
+
public:
|
|
13
|
+
COM_SPI(SPI_HandleTypeDef* hspi, GPIO_TypeDef* cs_port, uint16_t cs_pin);
|
|
14
|
+
M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) override;
|
|
15
|
+
M_Status write(uint8_t reg, const uint8_t* data, size_t len) override;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @brief call after the clocks and HAL_SPI_Init
|
|
19
|
+
*/
|
|
20
|
+
void init() {
|
|
21
|
+
SPI_TypeDef* spi = this->handle()->Instance;
|
|
22
|
+
// SPI1 and SPI4 are on APB2, the rest on APB1; check the reference manual for the part
|
|
23
|
+
bool apb2 = (spi == SPI1);
|
|
24
|
+
#ifdef SPI4
|
|
25
|
+
apb2 = apb2 || (spi == SPI4);
|
|
26
|
+
#endif
|
|
27
|
+
uint32_t pclk = apb2 ? HAL_RCC_GetPCLK2Freq() : HAL_RCC_GetPCLK1Freq();
|
|
28
|
+
// SPI_BAUDRATEPRESCALER_x stores BR bits; divider = 2^(BR+1)
|
|
29
|
+
this->__bitrate = pclk / (2u << (this->handle()->Init.BaudRatePrescaler >> SPI_CR1_BR_Pos));
|
|
30
|
+
// SPI_DATASIZE_xBIT stores (x - 1) in CR2 DS
|
|
31
|
+
this->__frame_bits = (this->handle()->Init.DataSize >> SPI_CR2_DS_Pos) + 1u;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @brief get maximum allowed timeout for a packet
|
|
36
|
+
*
|
|
37
|
+
* calculate separate timeouts for each transfer type
|
|
38
|
+
* (Transmit, Receive) and never merge both budgets into one timeout.
|
|
39
|
+
*
|
|
40
|
+
* @param len transfered data length in bytes
|
|
41
|
+
*/
|
|
42
|
+
uint32_t __packet_timeout_ms(size_t len) const {
|
|
43
|
+
if (this->__bitrate == 0) return SPI_TIMEOUT_MARGIN_MS;
|
|
44
|
+
// ceil(len * bits * 1000 / bitrate); len <= 65535 and bits <= 16, so it fits in 32 bits
|
|
45
|
+
uint32_t frame_ms = ((uint32_t)len * this->__frame_bits * 1000u + this->__bitrate - 1u) / this->__bitrate;
|
|
46
|
+
// polling/IT mode: CPU handles each frame, so add a gap per frame; DMA has none
|
|
47
|
+
uint32_t gap_us = (this->_tx_dma || this->_rx_dma) ? 0u : SPI_FRAME_GAP_US;
|
|
48
|
+
uint32_t gap_ms = ((uint32_t)len * gap_us + 999u) / 1000u;
|
|
49
|
+
return frame_ms + gap_ms + SPI_TIMEOUT_MARGIN_MS;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
bool isHandle(SPI_HandleTypeDef *check) const {
|
|
53
|
+
return this->handle() == check;
|
|
54
|
+
}
|
|
55
|
+
SPI_HandleTypeDef* handle() const {
|
|
56
|
+
return (SPI_HandleTypeDef*)_handle;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
bool is_interrupt_pin_set() const override {
|
|
60
|
+
return this->_int_port != nullptr && HAL_GPIO_ReadPin(this->_int_port, this->_int_pin) == GPIO_PIN_SET;
|
|
61
|
+
}
|
|
62
|
+
bool is_interrupt_pin(uint16_t pin) const { return this->_int_port != nullptr && this->_int_pin == pin; }
|
|
63
|
+
|
|
64
|
+
private:
|
|
65
|
+
GPIO_TypeDef* _cs_port;
|
|
66
|
+
uint16_t _cs_pin;
|
|
67
|
+
uint32_t __bitrate = 0;
|
|
68
|
+
uint32_t __frame_bits = 8;
|
|
69
|
+
};
|
package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/code/COM_USART.hpp
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// ... includes ... //
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @brief USART bus driver
|
|
7
|
+
*
|
|
8
|
+
* - read/write `reg` is ignored for USART
|
|
9
|
+
* - `COM_USART` has no registers, so `write_bit`, `write_verify` and `wait_for_register` don't apply to it.
|
|
10
|
+
*/
|
|
11
|
+
class COM_USART : public COM_Driver {
|
|
12
|
+
public:
|
|
13
|
+
explicit COM_USART(UART_HandleTypeDef* huart);
|
|
14
|
+
|
|
15
|
+
M_Status read(uint8_t reg, uint8_t* data, size_t len = 1) override;
|
|
16
|
+
M_Status write(uint8_t reg, const uint8_t* data, size_t len) override;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @brief call after HAL_UART_Init
|
|
20
|
+
*/
|
|
21
|
+
void init() {
|
|
22
|
+
const UART_InitTypeDef& in = this->handle()->Init;
|
|
23
|
+
// word length already includes the parity bit on STM32
|
|
24
|
+
uint32_t data = (in.WordLength == UART_WORDLENGTH_9B) ? 9u : 8u;
|
|
25
|
+
#ifdef UART_WORDLENGTH_7B
|
|
26
|
+
if (in.WordLength == UART_WORDLENGTH_7B) data = 7u;
|
|
27
|
+
#endif
|
|
28
|
+
// 0.5 stop bits rounds up to 1, 1.5 to 2
|
|
29
|
+
uint32_t stop = (in.StopBits == UART_STOPBITS_2) ? 2u : 1u;
|
|
30
|
+
#ifdef UART_STOPBITS_1_5
|
|
31
|
+
if (in.StopBits == UART_STOPBITS_1_5) stop = 2u;
|
|
32
|
+
#endif
|
|
33
|
+
this->__frame_bits = 1u + data + stop; // + start bit
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @brief get maximum allowed timeout for a packet
|
|
38
|
+
* @param len length in bytes
|
|
39
|
+
*/
|
|
40
|
+
uint32_t __packet_timeout_ms(size_t len) const {
|
|
41
|
+
uint32_t baud = this->handle()->Init.BaudRate;
|
|
42
|
+
if (baud == 0) return USART_TIMEOUT_MARGIN_MS;
|
|
43
|
+
// ceil(len * bits * 1000 / baud);
|
|
44
|
+
uint32_t frame_ms = ((uint32_t)len * this->__frame_bits * 1000u + baud - 1u) / baud;
|
|
45
|
+
return frame_ms + USART_TIMEOUT_MARGIN_MS;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bool isHandle(UART_HandleTypeDef *check) const {
|
|
49
|
+
return this->handle() == check;
|
|
50
|
+
}
|
|
51
|
+
UART_HandleTypeDef* handle() const {
|
|
52
|
+
return (UART_HandleTypeDef*)_handle;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private:
|
|
56
|
+
// worst case until init() runs
|
|
57
|
+
uint32_t __frame_bits = USART_MAX_BITS_PER_FRAME;
|
|
58
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
extern "C" {
|
|
4
|
+
#include "cmsis_os.h"
|
|
5
|
+
}
|
|
6
|
+
#include <atomic>
|
|
7
|
+
#include "FreeRTOS.h"
|
|
8
|
+
#include "semphr.h"
|
|
9
|
+
#include "task.h"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Combines two FreeRTOS primitives that serve different roles:
|
|
14
|
+
*
|
|
15
|
+
* - Mutex: mutual exclusion for driver-internal state accessed only
|
|
16
|
+
* from task context. Has priority inheritance. Must be
|
|
17
|
+
* taken/given from a task — NEVER from an ISR.
|
|
18
|
+
*
|
|
19
|
+
* - Binary semaphore: signals "bus/transfer is in use." Can be
|
|
20
|
+
* released from an ISR (e.g. a DMA completion callback), which a
|
|
21
|
+
* mutex cannot. No priority inheritance.
|
|
22
|
+
*
|
|
23
|
+
* Use mutex_lock()/mutex_unlock() to protect short critical sections of
|
|
24
|
+
* your own data structures. Use semaphore_acquire()/semaphore_release()/
|
|
25
|
+
* semaphore_release_from_isr() to track "is a DMA transfer in flight,"
|
|
26
|
+
* released either by the initiating task (on failure to start) or by
|
|
27
|
+
* the completion/error ISR (on finish). Use wait_for_release() to
|
|
28
|
+
* block until the bus is next idle, without touching the semaphore.
|
|
29
|
+
*
|
|
30
|
+
* Each primitive is created on first use, race-free: if two tasks create
|
|
31
|
+
* one at the same time, one handle wins and the other is deleted.
|
|
32
|
+
*/
|
|
33
|
+
class Util_MutexManager {
|
|
34
|
+
public:
|
|
35
|
+
Util_MutexManager() {
|
|
36
|
+
this->_owns_mutex = false;
|
|
37
|
+
}
|
|
38
|
+
~Util_MutexManager() {
|
|
39
|
+
SemaphoreHandle_t mutex = this->_mutex.load(std::memory_order_acquire);
|
|
40
|
+
if (mutex != NULL && this->_owns_mutex) {
|
|
41
|
+
vSemaphoreDelete(mutex);
|
|
42
|
+
}
|
|
43
|
+
SemaphoreHandle_t binSem = this->_binSem.exchange(NULL, std::memory_order_acq_rel);
|
|
44
|
+
if (binSem != NULL) {
|
|
45
|
+
vSemaphoreDelete(binSem);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/* Copies would share the handles and delete them twice. */
|
|
49
|
+
Util_MutexManager(const Util_MutexManager&) = delete;
|
|
50
|
+
Util_MutexManager& operator=(const Util_MutexManager&) = delete;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* True once the scheduler started. Before that, every call below does
|
|
54
|
+
* nothing: creating or taking a primitive there leaves interrupts masked
|
|
55
|
+
* until the scheduler starts (the HAL tick stops, so HAL_Delay() hangs),
|
|
56
|
+
* and only one context runs anyway, so there is nothing to lock against.
|
|
57
|
+
*/
|
|
58
|
+
static bool get_scheduler_is_running() {
|
|
59
|
+
return xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Create all primitives. Safe to call more than once;
|
|
64
|
+
* each part only gets created if not already present.
|
|
65
|
+
* @param mutex_id adopt this mutex instead of creating one; it is not deleted on destruction.
|
|
66
|
+
* @retval false before the scheduler starts (nothing is created then).
|
|
67
|
+
*/
|
|
68
|
+
bool mutex_init(osMutexId_t mutex_id=NULL) {
|
|
69
|
+
if (!get_scheduler_is_running()) return false;
|
|
70
|
+
bool ok = true;
|
|
71
|
+
if(mutex_id != NULL) {
|
|
72
|
+
this->_owns_mutex = false; // adopted - do not delete on destruction
|
|
73
|
+
this->_mutex.store(reinterpret_cast<SemaphoreHandle_t>(mutex_id), std::memory_order_release);
|
|
74
|
+
}else {
|
|
75
|
+
ok = ok && (this->ensure_mutex() != NULL);
|
|
76
|
+
}
|
|
77
|
+
ok = ok && (this->ensure_bin_sem() != NULL);
|
|
78
|
+
this->_mutex_adopted = (mutex_id != NULL) && ok;
|
|
79
|
+
return ok;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
bool mutex_lock(TickType_t ticksToWait = portMAX_DELAY) {
|
|
84
|
+
if (!get_scheduler_is_running()) return true;
|
|
85
|
+
SemaphoreHandle_t mutex = this->ensure_mutex();
|
|
86
|
+
if (mutex == NULL) return false;
|
|
87
|
+
return xSemaphoreTake(mutex, ticksToWait) == pdTRUE;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
bool mutex_unlock() {
|
|
91
|
+
if (!get_scheduler_is_running()) return true;
|
|
92
|
+
SemaphoreHandle_t mutex = this->_mutex.load(std::memory_order_acquire);
|
|
93
|
+
if (mutex == NULL) return false;
|
|
94
|
+
return xSemaphoreGive(mutex) == pdTRUE;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Who currently holds the mutex (NULL if unheld/uncreated).
|
|
99
|
+
* Task context only — do not call from an ISR.
|
|
100
|
+
*/
|
|
101
|
+
TaskHandle_t mutex_holder() {
|
|
102
|
+
SemaphoreHandle_t mutex = this->_mutex.load(std::memory_order_acquire);
|
|
103
|
+
if (mutex == NULL) return NULL;
|
|
104
|
+
return xSemaphoreGetMutexHolder(mutex);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* -------------------------------------------------------------------------- */
|
|
108
|
+
/* Semaphore */
|
|
109
|
+
/* -------------------------------------------------------------------------- */
|
|
110
|
+
bool semaphore_acquire(TickType_t ticksToWait = portMAX_DELAY) {
|
|
111
|
+
if (!get_scheduler_is_running()) return true;
|
|
112
|
+
SemaphoreHandle_t binSem = this->ensure_bin_sem();
|
|
113
|
+
if (binSem == NULL) return false;
|
|
114
|
+
return xSemaphoreTake(binSem, ticksToWait) == pdTRUE;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Release from task context — e.g. the transfer failed to
|
|
119
|
+
* start, so no completion ISR will ever fire for it.
|
|
120
|
+
*/
|
|
121
|
+
bool semaphore_release() {
|
|
122
|
+
if (!get_scheduler_is_running()) return true;
|
|
123
|
+
SemaphoreHandle_t binSem = this->_binSem.load(std::memory_order_acquire);
|
|
124
|
+
if (binSem == NULL) return false;
|
|
125
|
+
return xSemaphoreGive(binSem) == pdTRUE;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Release from ISR context — e.g. HAL_UART_TxCpltCallback /
|
|
130
|
+
* HAL_UART_ErrorCallback. Yields immediately if a higher
|
|
131
|
+
* priority task was unblocked by the release.
|
|
132
|
+
*/
|
|
133
|
+
BaseType_t semaphore_release_from_isr(bool yield=false) {
|
|
134
|
+
if (!get_scheduler_is_running()) return pdFALSE;
|
|
135
|
+
SemaphoreHandle_t binSem = this->_binSem.load(std::memory_order_acquire);
|
|
136
|
+
if (binSem == NULL) return pdFALSE;
|
|
137
|
+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
138
|
+
xSemaphoreGiveFromISR(binSem, &xHigherPriorityTaskWoken);
|
|
139
|
+
if(yield){
|
|
140
|
+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
141
|
+
}
|
|
142
|
+
return xHigherPriorityTaskWoken;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* True while the bus is idle (the semaphore's token is available,
|
|
147
|
+
* or no transfer was ever started). Task context only.
|
|
148
|
+
*/
|
|
149
|
+
bool get_is_released() {
|
|
150
|
+
SemaphoreHandle_t binSem = this->_binSem.load(std::memory_order_acquire);
|
|
151
|
+
return binSem == NULL || uxSemaphoreGetCount(binSem) == 1;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Block until the bus is idle, without taking the token, so it
|
|
156
|
+
* never steals it from whoever acquires it next (e.g. write_DMA()).
|
|
157
|
+
* Any number of tasks can wait at once. Polls once per tick, so it
|
|
158
|
+
* returns up to one tick after the release.
|
|
159
|
+
*
|
|
160
|
+
* Reports "idle", not "a transfer finished since I last checked":
|
|
161
|
+
* if nothing acquires the semaphore, it returns true immediately.
|
|
162
|
+
*/
|
|
163
|
+
bool wait_for_release(TickType_t ticksToWait = portMAX_DELAY) {
|
|
164
|
+
if (!get_scheduler_is_running()) return true;
|
|
165
|
+
TickType_t start = xTaskGetTickCount();
|
|
166
|
+
while (!this->get_is_released()) {
|
|
167
|
+
if (ticksToWait != portMAX_DELAY && (xTaskGetTickCount() - start) >= ticksToWait) return false;
|
|
168
|
+
vTaskDelay(1);
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
protected:
|
|
174
|
+
/**
|
|
175
|
+
* True once mutex_init() adopted an external mutex and created every
|
|
176
|
+
* other primitive. A derived class resets it when that mutex may no
|
|
177
|
+
* longer apply (e.g. a driver rebound to another bus).
|
|
178
|
+
*/
|
|
179
|
+
bool _mutex_adopted = false;
|
|
180
|
+
|
|
181
|
+
private:
|
|
182
|
+
std::atomic<SemaphoreHandle_t> _mutex{NULL};
|
|
183
|
+
std::atomic<SemaphoreHandle_t> _binSem{NULL};
|
|
184
|
+
bool _owns_mutex;
|
|
185
|
+
|
|
186
|
+
/** Publish a new handle unless another task got there first; the loser's handle is deleted. */
|
|
187
|
+
static SemaphoreHandle_t publish_once(std::atomic<SemaphoreHandle_t>& slot, SemaphoreHandle_t fresh) {
|
|
188
|
+
SemaphoreHandle_t expected = NULL;
|
|
189
|
+
if (slot.compare_exchange_strong(expected, fresh, std::memory_order_acq_rel)) return fresh;
|
|
190
|
+
vSemaphoreDelete(fresh);
|
|
191
|
+
return expected;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
SemaphoreHandle_t ensure_mutex() {
|
|
195
|
+
SemaphoreHandle_t mutex = this->_mutex.load(std::memory_order_acquire);
|
|
196
|
+
if (mutex != NULL) return mutex;
|
|
197
|
+
SemaphoreHandle_t fresh = xSemaphoreCreateMutex();
|
|
198
|
+
if (fresh == NULL) return NULL;
|
|
199
|
+
mutex = publish_once(this->_mutex, fresh);
|
|
200
|
+
if (mutex == fresh) this->_owns_mutex = true;
|
|
201
|
+
return mutex;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
SemaphoreHandle_t ensure_bin_sem() {
|
|
205
|
+
SemaphoreHandle_t binSem = this->_binSem.load(std::memory_order_acquire);
|
|
206
|
+
if (binSem != NULL) return binSem;
|
|
207
|
+
SemaphoreHandle_t fresh = xSemaphoreCreateBinary();
|
|
208
|
+
if (fresh == NULL) return NULL;
|
|
209
|
+
xSemaphoreGive(fresh); // binary semaphores start empty; the bus starts out "free"
|
|
210
|
+
return publish_once(this->_binSem, fresh);
|
|
211
|
+
}
|
|
212
|
+
};
|