dsh-embedded-workbench 0.8.2 → 0.8.3

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.
@@ -1,133 +1,133 @@
1
- ---
2
- name: embedded-firmware-dev
3
- description: "Use when writing or reviewing embedded C firmware, FreeRTOS tasks, ISR handlers, NVM/flash storage, or sensor driver state machines. NOT for documentation-only RTOS references, conceptual RTOS discussions, or bare-metal projects without an RTOS or sensor subsystem."
4
- ---
5
-
6
- <HARD-GATE>
7
- This is a domain implementation skill. If you are planning, designing, or entering plan mode — load `Skill("embedded-workbench")` first to activate the workflow gates (Plan Verification Gate, Approval Gate, Closure Gate). Domain skills carry implementation guidance, not workflow enforcement.
8
- </HARD-GATE>
9
-
10
- # Embedded Firmware Development
11
-
12
- ## FreeRTOS
13
-
14
- - Give each task a clear ownership boundary. Shared resources need a deliberate synchronization strategy.
15
- - Prefer task notifications for one-to-one wakeups, queues for data transfer, event groups for combined state, mutexes for mutual exclusion, semaphores for one-way signaling.
16
- - Use a mutex (not binary semaphore) when mutual exclusion matters and priority inheritance is needed.
17
- - Make every blocking wait explicit: use a timeout unless an infinite wait is deliberate.
18
- - Keep timer callbacks short and non-blocking — use them to schedule work, not do it.
19
- - Avoid holding locks across flash, storage, or long operations.
20
- - Size task stacks from worst-case call chains. Recheck high-water marks after adding buffers or deeper call trees.
21
- - Use ISR-safe APIs for interrupt-to-task handoff. Keep ISR state capture minimal.
22
- - Avoid priority inversion: don't hold shared locks across blocking I/O or long processing.
23
- - If a task can be paused/restarted/signaled from multiple places, define resume, timeout, and recovery paths explicitly.
24
- - For cross-task pointer ownership: make lifetime and invalidation rules obvious.
25
- - Prefer the smallest critical section that protects the state transition. Don't wrap whole operations in locks when narrower ordering suffices.
26
- - For objects handed between threads: define whether the receiver owns, borrows, or copies before crossing the boundary.
27
-
28
- ## Interrupts / ISR
29
-
30
- - Keep the interrupt path short, deterministic, and bounded. Capture minimum state, clear the source, defer expensive work.
31
- - Do not block, sleep, allocate heap, or call non-ISR-safe APIs from an ISR.
32
- - Prefer top-half/bottom-half split when the handler needs more than quick state capture and wakeup.
33
- - Make shared-state ownership explicit. Use minimum synchronization for the data being shared.
34
- - If an ISR wakes a task, use the ISR-safe RTOS primitive and preserve yield-from-ISR behavior.
35
- - Define clear read/clear/re-enable ordering to avoid losing edges or creating re-trigger loops.
36
- - Avoid logging and complex branching in the hot interrupt path.
37
- - If code runs from both task and ISR context, separate wrappers so the ISR-safe path stays obvious.
38
-
39
- ## Async Lifecycle Cleanup
40
-
41
- - Any async flag (pending, in-progress, busy, data-ready) that can be set during normal operation must be explicitly cleared in every stop, init, reset, power-off, and error-recovery path. A stale flag silently blocks the next operation.
42
- - When adding a new async operation, audit all lifecycle entry points and ensure each path resets flags to known-safe.
43
- - Cleanup must happen before any new operation is attempted, not after.
44
-
45
- ```c
46
- // CORRECT: every async flag cleared in stop path. No stale state survives restart.
47
- uint8_t comm_stop_sample(void) {
48
- g_comm.data_ready = false;
49
- g_comm.state = COMM_STATE_IDLE;
50
- g_comm.activating = false;
51
- g_comm.command_fail_count = 0;
52
- g_comm.protocol_locked = false;
53
- g_comm.communication_lost = false;
54
- g_comm.warmup_start_time = 0;
55
- comm_command_complete(); // Release any pending I/O
56
- memset(&g_comm_data, 0, sizeof(g_comm_data));// Reset cached data
57
- comm_process_faults(false); // Clear fault detection state
58
- return COMM_OK;
59
- }
60
-
61
- // BAD: half the flags survive — next start inherits stale state
62
- void comm_stop_bad(void) {
63
- g_comm.state = COMM_STATE_IDLE; // Only state changed
64
- // Missing: data_ready, protocol_locked, communication_lost, fail_count...
65
- // Next start: data_ready==true blocks first sample; fail_count persists
66
- }
67
-
68
- // CALLER GUARD: clear stale flags at power-off boundaries before re-init
69
- void comm_handler(void) {
70
- if (!power_get_status() && g_comm.command_pending) {
71
- comm_command_complete(); // Clear before deinit
72
- }
73
- if (power_get_status()) {
74
- comm_state_process();
75
- }
76
- }
77
- ```
78
-
79
- ## One-Shot Event Consumption
80
-
81
- - When a low-level driver produces a transient event that multiple higher-level consumers need, use an atomic check-and-clear (consume) API rather than shared flags each consumer clears manually.
82
- - Manual clearing by multiple consumers creates races: consumer A clears before B reads, or B reads a flag already set again by the next cycle.
83
- - The consume primitive returns whether the event occurred and atomically clears the latch — every interested consumer observes the event exactly once per occurrence.
84
-
85
- ```c
86
- // Atomic check-and-clear: every consumer sees the event exactly once
87
- static volatile uint32_t event_latch;
88
-
89
- uint32_t event_consume(uint32_t mask) {
90
- uint32_t primask = __get_PRIMASK();
91
- __disable_irq();
92
- uint32_t pending = event_latch & mask;
93
- event_latch &= ~mask; // Clear consumed bits atomically
94
- if (!primask) __enable_irq();
95
- return pending; // Non-zero = event occurred this cycle
96
- }
97
-
98
- // Each consumer independently observes — no races between consumers
99
- void ui_consume(void) {
100
- if (event_consume(EVT_SENSOR_READY)) update_display();
101
- }
102
- void log_consume(void) {
103
- if (event_consume(EVT_SENSOR_READY)) write_log();
104
- }
105
- ```
106
-
107
- ## Storage / Persistence
108
-
109
- - Separate object corruption from schema change. Rebuild the whole store only when versioned layout rules require it.
110
- - Prefer recoverable write paths: write primary → read back and verify → write backup → read back and verify.
111
- - During delete, reset, or migration, preserve at least one valid recoverable copy.
112
- - Prefer targeted repair and re-sync over destructive reinitialization.
113
- - Treat startup repair, steady-state writes, emergency writes, and factory reset as separate paths with explicit guarantees.
114
-
115
- ## Boundary Analysis
116
-
117
- - When thresholds trip at edges, inspect debounce latency, sample timing, and ring-vs-bounded assumptions before tuning constants.
118
- - When behavior diverges by mode, compare all branches side by side instead of debugging only the failing branch.
119
- - When a defect appears on only one trigger path, diff which state each caller resets, preserves, or derives.
120
- - For transient inconsistencies, inspect raw state, derived state, and cached state separately — stale data in any layer masquerades as a timing problem.
121
- - When a system undergoes mode switch, direction reversal, or re-initialization, allow a bounded tolerance window for the first post-transition deviation. Treating it with steady-state thresholds produces false error accumulation.
122
-
123
- ## Deep Reference
124
-
125
- This skill's `references/` directory contains in-depth material. Load when you need more than the core rules:
126
-
127
- | Reference | Topic | Load When |
128
- |-----------|-------|-----------|
129
- | `architecture-principles.md` | 12 architecture design principles | Designing module boundaries, state ownership, or GUI architecture |
130
- | `embedded-patterns.md` | GIF timer safety, async lifecycle, state latches | Debugging timer crashes, stale flags, or state corruption |
131
- | `lvgl-pitfalls.md` | LVGL layout, alignment, alpha, and mask traps | Debugging LVGL rendering artifacts or HardFault in draw paths |
132
-
133
- See also: `Skill("state-machine-design")` for state transition rules, `Skill("debug-methodology")` for debugging process, `Skill("hardfault-triage")` for stack overflow and ISR crash triage.
1
+ ---
2
+ name: embedded-firmware-dev
3
+ description: "Use when writing or reviewing embedded C firmware, FreeRTOS tasks, ISR handlers, NVM/flash storage, or sensor driver state machines. NOT for documentation-only RTOS references, conceptual RTOS discussions, or bare-metal projects without an RTOS or sensor subsystem."
4
+ ---
5
+
6
+ <HARD-GATE>
7
+ This is a domain implementation skill. If you are planning, designing, or entering plan mode — load `Skill("embedded-workbench")` first to activate the workflow gates (Plan Verification Gate, Approval Gate, Closure Gate). Domain skills carry implementation guidance, not workflow enforcement.
8
+ </HARD-GATE>
9
+
10
+ # Embedded Firmware Development
11
+
12
+ ## FreeRTOS
13
+
14
+ - Give each task a clear ownership boundary. Shared resources need a deliberate synchronization strategy.
15
+ - Prefer task notifications for one-to-one wakeups, queues for data transfer, event groups for combined state, mutexes for mutual exclusion, semaphores for one-way signaling.
16
+ - Use a mutex (not binary semaphore) when mutual exclusion matters and priority inheritance is needed.
17
+ - Make every blocking wait explicit: use a timeout unless an infinite wait is deliberate.
18
+ - Keep timer callbacks short and non-blocking — use them to schedule work, not do it.
19
+ - Avoid holding locks across flash, storage, or long operations.
20
+ - Size task stacks from worst-case call chains. Recheck high-water marks after adding buffers or deeper call trees.
21
+ - Use ISR-safe APIs for interrupt-to-task handoff. Keep ISR state capture minimal.
22
+ - Avoid priority inversion: don't hold shared locks across blocking I/O or long processing.
23
+ - If a task can be paused/restarted/signaled from multiple places, define resume, timeout, and recovery paths explicitly.
24
+ - For cross-task pointer ownership: make lifetime and invalidation rules obvious.
25
+ - Prefer the smallest critical section that protects the state transition. Don't wrap whole operations in locks when narrower ordering suffices.
26
+ - For objects handed between threads: define whether the receiver owns, borrows, or copies before crossing the boundary.
27
+
28
+ ## Interrupts / ISR
29
+
30
+ - Keep the interrupt path short, deterministic, and bounded. Capture minimum state, clear the source, defer expensive work.
31
+ - Do not block, sleep, allocate heap, or call non-ISR-safe APIs from an ISR.
32
+ - Prefer top-half/bottom-half split when the handler needs more than quick state capture and wakeup.
33
+ - Make shared-state ownership explicit. Use minimum synchronization for the data being shared.
34
+ - If an ISR wakes a task, use the ISR-safe RTOS primitive and preserve yield-from-ISR behavior.
35
+ - Define clear read/clear/re-enable ordering to avoid losing edges or creating re-trigger loops.
36
+ - Avoid logging and complex branching in the hot interrupt path.
37
+ - If code runs from both task and ISR context, separate wrappers so the ISR-safe path stays obvious.
38
+
39
+ ## Async Lifecycle Cleanup
40
+
41
+ - Any async flag (pending, in-progress, busy, data-ready) that can be set during normal operation must be explicitly cleared in every stop, init, reset, power-off, and error-recovery path. A stale flag silently blocks the next operation.
42
+ - When adding a new async operation, audit all lifecycle entry points and ensure each path resets flags to known-safe.
43
+ - Cleanup must happen before any new operation is attempted, not after.
44
+
45
+ ```c
46
+ // CORRECT: every async flag cleared in stop path. No stale state survives restart.
47
+ uint8_t comm_stop_sample(void) {
48
+ g_comm.data_ready = false;
49
+ g_comm.state = COMM_STATE_IDLE;
50
+ g_comm.activating = false;
51
+ g_comm.command_fail_count = 0;
52
+ g_comm.protocol_locked = false;
53
+ g_comm.communication_lost = false;
54
+ g_comm.warmup_start_time = 0;
55
+ comm_command_complete(); // Release any pending I/O
56
+ memset(&g_comm_data, 0, sizeof(g_comm_data));// Reset cached data
57
+ comm_process_faults(false); // Clear fault detection state
58
+ return COMM_OK;
59
+ }
60
+
61
+ // BAD: half the flags survive — next start inherits stale state
62
+ void comm_stop_bad(void) {
63
+ g_comm.state = COMM_STATE_IDLE; // Only state changed
64
+ // Missing: data_ready, protocol_locked, communication_lost, fail_count...
65
+ // Next start: data_ready==true blocks first sample; fail_count persists
66
+ }
67
+
68
+ // CALLER GUARD: clear stale flags at power-off boundaries before re-init
69
+ void comm_handler(void) {
70
+ if (!power_get_status() && g_comm.command_pending) {
71
+ comm_command_complete(); // Clear before deinit
72
+ }
73
+ if (power_get_status()) {
74
+ comm_state_process();
75
+ }
76
+ }
77
+ ```
78
+
79
+ ## One-Shot Event Consumption
80
+
81
+ - When a low-level driver produces a transient event that multiple higher-level consumers need, use an atomic check-and-clear (consume) API rather than shared flags each consumer clears manually.
82
+ - Manual clearing by multiple consumers creates races: consumer A clears before B reads, or B reads a flag already set again by the next cycle.
83
+ - The consume primitive returns whether the event occurred and atomically clears the latch — every interested consumer observes the event exactly once per occurrence.
84
+
85
+ ```c
86
+ // Atomic check-and-clear: every consumer sees the event exactly once
87
+ static volatile uint32_t event_latch;
88
+
89
+ uint32_t event_consume(uint32_t mask) {
90
+ uint32_t primask = __get_PRIMASK();
91
+ __disable_irq();
92
+ uint32_t pending = event_latch & mask;
93
+ event_latch &= ~mask; // Clear consumed bits atomically
94
+ if (!primask) __enable_irq();
95
+ return pending; // Non-zero = event occurred this cycle
96
+ }
97
+
98
+ // Each consumer independently observes — no races between consumers
99
+ void ui_consume(void) {
100
+ if (event_consume(EVT_SENSOR_READY)) update_display();
101
+ }
102
+ void log_consume(void) {
103
+ if (event_consume(EVT_SENSOR_READY)) write_log();
104
+ }
105
+ ```
106
+
107
+ ## Storage / Persistence
108
+
109
+ - Separate object corruption from schema change. Rebuild the whole store only when versioned layout rules require it.
110
+ - Prefer recoverable write paths: write primary → read back and verify → write backup → read back and verify.
111
+ - During delete, reset, or migration, preserve at least one valid recoverable copy.
112
+ - Prefer targeted repair and re-sync over destructive reinitialization.
113
+ - Treat startup repair, steady-state writes, emergency writes, and factory reset as separate paths with explicit guarantees.
114
+
115
+ ## Boundary Analysis
116
+
117
+ - When thresholds trip at edges, inspect debounce latency, sample timing, and ring-vs-bounded assumptions before tuning constants.
118
+ - When behavior diverges by mode, compare all branches side by side instead of debugging only the failing branch.
119
+ - When a defect appears on only one trigger path, diff which state each caller resets, preserves, or derives.
120
+ - For transient inconsistencies, inspect raw state, derived state, and cached state separately — stale data in any layer masquerades as a timing problem.
121
+ - When a system undergoes mode switch, direction reversal, or re-initialization, allow a bounded tolerance window for the first post-transition deviation. Treating it with steady-state thresholds produces false error accumulation.
122
+
123
+ ## Deep Reference
124
+
125
+ This skill's `references/` directory contains in-depth material. Load when you need more than the core rules:
126
+
127
+ | Reference | Topic | Load When |
128
+ |-----------|-------|-----------|
129
+ | `architecture-principles.md` | 12 architecture design principles | Designing module boundaries, state ownership, or GUI architecture |
130
+ | `embedded-patterns.md` | GIF timer safety, async lifecycle, state latches | Debugging timer crashes, stale flags, or state corruption |
131
+ | `lvgl-pitfalls.md` | LVGL layout, alignment, alpha, and mask traps | Debugging LVGL rendering artifacts or HardFault in draw paths |
132
+
133
+ See also: `Skill("state-machine-design")` for state transition rules, `Skill("debug-methodology")` for debugging process, `Skill("hardfault-triage")` for stack overflow and ISR crash triage.