@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,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
|
|
@@ -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
|
+
};
|
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.
|