@mytechtoday/augment-extensions 1.3.1 → 1.5.0
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/README.md +105 -6
- package/augment-extensions/coding-standards/c/CHANGELOG.md +55 -0
- package/augment-extensions/coding-standards/c/LICENSE +22 -0
- package/augment-extensions/coding-standards/c/README.md +167 -0
- package/augment-extensions/coding-standards/c/config/defaults.json +26 -0
- package/augment-extensions/coding-standards/c/config/examples/embedded.yaml +25 -0
- package/augment-extensions/coding-standards/c/config/examples/systems.json +31 -0
- package/augment-extensions/coding-standards/c/config/schema.json +244 -0
- package/augment-extensions/coding-standards/c/docs/API.md +613 -0
- package/augment-extensions/coding-standards/c/docs/CONFIGURATION.md +259 -0
- package/augment-extensions/coding-standards/c/docs/USER_GUIDE.md +567 -0
- package/augment-extensions/coding-standards/c/examples/drivers/Makefile +33 -0
- package/augment-extensions/coding-standards/c/examples/drivers/README.md +192 -0
- package/augment-extensions/coding-standards/c/examples/drivers/dma-example.c +224 -0
- package/augment-extensions/coding-standards/c/examples/drivers/example.dts +64 -0
- package/augment-extensions/coding-standards/c/examples/drivers/platform-driver.c +174 -0
- package/augment-extensions/coding-standards/c/examples/embedded/README.md +167 -0
- package/augment-extensions/coding-standards/c/examples/embedded/gpio-control.c +172 -0
- package/augment-extensions/coding-standards/c/examples/embedded/timer-isr.c +198 -0
- package/augment-extensions/coding-standards/c/examples/embedded/uart-communication.c +212 -0
- package/augment-extensions/coding-standards/c/examples/kernel/Makefile +82 -0
- package/augment-extensions/coding-standards/c/examples/kernel/README.md +168 -0
- package/augment-extensions/coding-standards/c/examples/kernel/char-device.c +198 -0
- package/augment-extensions/coding-standards/c/examples/kernel/proc-file.c +131 -0
- package/augment-extensions/coding-standards/c/examples/kernel/simple-module.c +111 -0
- package/augment-extensions/coding-standards/c/examples/legacy/Makefile +62 -0
- package/augment-extensions/coding-standards/c/examples/legacy/README.md +255 -0
- package/augment-extensions/coding-standards/c/examples/legacy/c89-to-c11-migration.c +268 -0
- package/augment-extensions/coding-standards/c/examples/legacy/compatibility-layer.c +239 -0
- package/augment-extensions/coding-standards/c/examples/networking/Makefile +35 -0
- package/augment-extensions/coding-standards/c/examples/networking/README.md +207 -0
- package/augment-extensions/coding-standards/c/examples/networking/protocol-parser.c +270 -0
- package/augment-extensions/coding-standards/c/examples/networking/tcp-server.c +197 -0
- package/augment-extensions/coding-standards/c/examples/networking/udp-multicast.c +220 -0
- package/augment-extensions/coding-standards/c/examples/realtime/Makefile +53 -0
- package/augment-extensions/coding-standards/c/examples/realtime/README.md +199 -0
- package/augment-extensions/coding-standards/c/examples/realtime/deadline-monitoring.c +260 -0
- package/augment-extensions/coding-standards/c/examples/realtime/priority-scheduling.c +258 -0
- package/augment-extensions/coding-standards/c/examples/systems/Makefile +34 -0
- package/augment-extensions/coding-standards/c/examples/systems/README.md +123 -0
- package/augment-extensions/coding-standards/c/examples/systems/ipc-pipes.c +181 -0
- package/augment-extensions/coding-standards/c/examples/systems/process-management.c +153 -0
- package/augment-extensions/coding-standards/c/examples/systems/signal-handling.c +162 -0
- package/augment-extensions/coding-standards/c/module.json +149 -0
- package/augment-extensions/coding-standards/c/rules/categories/drivers.md +635 -0
- package/augment-extensions/coding-standards/c/rules/categories/embedded.md +510 -0
- package/augment-extensions/coding-standards/c/rules/categories/kernel.md +653 -0
- package/augment-extensions/coding-standards/c/rules/categories/legacy.md +526 -0
- package/augment-extensions/coding-standards/c/rules/categories/networking.md +735 -0
- package/augment-extensions/coding-standards/c/rules/categories/realtime.md +631 -0
- package/augment-extensions/coding-standards/c/rules/categories/systems.md +586 -0
- package/augment-extensions/coding-standards/c/rules/universal/const-correctness.md +275 -0
- package/augment-extensions/coding-standards/c/rules/universal/documentation.md +251 -0
- package/augment-extensions/coding-standards/c/rules/universal/error-handling.md +250 -0
- package/augment-extensions/coding-standards/c/rules/universal/header-guards.md +254 -0
- package/augment-extensions/coding-standards/c/rules/universal/memory-safety.md +233 -0
- package/augment-extensions/coding-standards/c/rules/universal/naming.md +146 -0
- package/augment-extensions/coding-standards/c/src/conflict-detector.ts +461 -0
- package/augment-extensions/coding-standards/c/src/prompt-generator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-evaluator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-override.ts +427 -0
- package/augment-extensions/coding-standards/c/src/template-engine.ts +217 -0
- package/augment-extensions/coding-standards/c/templates/prompts/drivers.txt +191 -0
- package/augment-extensions/coding-standards/c/templates/prompts/embedded.txt +164 -0
- package/augment-extensions/coding-standards/c/templates/prompts/kernel.txt +175 -0
- package/augment-extensions/coding-standards/c/templates/prompts/legacy.txt +280 -0
- package/augment-extensions/coding-standards/c/templates/prompts/networking.txt +259 -0
- package/augment-extensions/coding-standards/c/templates/prompts/realtime.txt +219 -0
- package/augment-extensions/coding-standards/c/templates/prompts/systems.txt +147 -0
- package/augment-extensions/coding-standards/c/tests/integration/category-specific.test.ts +356 -0
- package/augment-extensions/coding-standards/c/tests/integration/end-to-end-workflow.test.ts +377 -0
- package/augment-extensions/coding-standards/c/tests/performance/benchmarks.test.ts +407 -0
- package/augment-extensions/coding-standards/c/tests/unit/config-manager.test.ts +345 -0
- package/augment-extensions/coding-standards/c/tests/unit/conflict-detector.test.ts +294 -0
- package/augment-extensions/coding-standards/c/tests/unit/prompt-generator.test.ts +174 -0
- package/augment-extensions/coding-standards/c/tests/unit/registry.test.ts +313 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-evaluator.test.ts +318 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-override.test.ts +326 -0
- package/augment-extensions/coding-standards/c/tests/unit/template-engine.test.ts +314 -0
- package/augment-extensions/coding-standards/go/CHARACTER-COUNT-REPORT.md +135 -0
- package/augment-extensions/coding-standards/go/PHASE1-COMPLETION.md +146 -0
- package/augment-extensions/coding-standards/go/PHASE4-COMPLETION.md +184 -0
- package/augment-extensions/coding-standards/go/README.md +200 -0
- package/augment-extensions/coding-standards/go/VALIDATION-CHECKLIST.md +154 -0
- package/augment-extensions/coding-standards/go/config/examples/example-cli.json +15 -0
- package/augment-extensions/coding-standards/go/config/examples/example-microservices.json +21 -0
- package/augment-extensions/coding-standards/go/config/examples/example-multi-category.yaml +24 -0
- package/augment-extensions/coding-standards/go/config/examples/example-web.json +15 -0
- package/augment-extensions/coding-standards/go/config/schema.json +110 -0
- package/augment-extensions/coding-standards/go/docs/CATEGORIES.md +221 -0
- package/augment-extensions/coding-standards/go/docs/CONFIGURATION.md +198 -0
- package/augment-extensions/coding-standards/go/docs/TROUBLESHOOTING.md +285 -0
- package/augment-extensions/coding-standards/go/examples/cli/cobra-app.go +287 -0
- package/augment-extensions/coding-standards/go/examples/cloud-native-app.go +217 -0
- package/augment-extensions/coding-standards/go/examples/devops-tool.go +250 -0
- package/augment-extensions/coding-standards/go/examples/distributed-system.go +247 -0
- package/augment-extensions/coding-standards/go/examples/microservices/grpc-service.go +253 -0
- package/augment-extensions/coding-standards/go/examples/rest-api.go +270 -0
- package/augment-extensions/coding-standards/go/examples/web/http-server.go +224 -0
- package/augment-extensions/coding-standards/go/module.json +139 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/api-versioning.md +149 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rate-limiting.md +209 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rest-api-design.md +183 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/cloud-config.md +193 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/health-checks.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/kubernetes.md +180 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/automation.md +179 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/ci-cd-integration.md +147 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/infrastructure-as-code.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/caching.md +150 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/consensus.md +187 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/event-sourcing.md +246 -0
- package/augment-extensions/coding-standards/go/rules/cli/command-parsing.md +264 -0
- package/augment-extensions/coding-standards/go/rules/cli/configuration.md +268 -0
- package/augment-extensions/coding-standards/go/rules/cli/cross-platform.md +324 -0
- package/augment-extensions/coding-standards/go/rules/microservices/distributed-tracing.md +253 -0
- package/augment-extensions/coding-standards/go/rules/microservices/grpc.md +257 -0
- package/augment-extensions/coding-standards/go/rules/microservices/metrics.md +278 -0
- package/augment-extensions/coding-standards/go/rules/microservices/service-discovery.md +249 -0
- package/augment-extensions/coding-standards/go/rules/universal/code-organization.md +221 -0
- package/augment-extensions/coding-standards/go/rules/universal/documentation.md +269 -0
- package/augment-extensions/coding-standards/go/rules/universal/performance.md +323 -0
- package/augment-extensions/coding-standards/go/rules/universal/testing.md +162 -0
- package/augment-extensions/coding-standards/go/rules/web/graceful-shutdown.md +249 -0
- package/augment-extensions/coding-standards/go/rules/web/http-handlers.md +164 -0
- package/augment-extensions/coding-standards/go/rules/web/middleware.md +234 -0
- package/augment-extensions/coding-standards/go/rules/web/routing.md +251 -0
- package/augment-extensions/coding-standards/go/templates/prompts/api.md +160 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cli.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cloud-native.md +121 -0
- package/augment-extensions/coding-standards/go/templates/prompts/devops.md +146 -0
- package/augment-extensions/coding-standards/go/templates/prompts/distributed.md +133 -0
- package/augment-extensions/coding-standards/go/templates/prompts/microservices.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/web.md +181 -0
- package/augment-extensions/coding-standards/go/tests/integration/module-integration.test.ts +164 -0
- package/augment-extensions/coding-standards/go/tests/unit/category-selection.test.ts +147 -0
- package/augment-extensions/coding-standards/go/tests/unit/module-structure.test.ts +154 -0
- package/augment-extensions/coding-standards/go/tests/validate-character-count.ps1 +13 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.ps1 +148 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.sh +135 -0
- package/cli/dist/analysis/ast-parser.d.ts +47 -0
- package/cli/dist/analysis/ast-parser.d.ts.map +1 -0
- package/cli/dist/analysis/ast-parser.js +161 -0
- package/cli/dist/analysis/ast-parser.js.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts +27 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.js +189 -0
- package/cli/dist/analysis/complexity-analyzer.js.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts +23 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.js +237 -0
- package/cli/dist/analysis/dependency-analyzer.js.map +1 -0
- package/cli/dist/analysis/index.d.ts +9 -0
- package/cli/dist/analysis/index.d.ts.map +1 -0
- package/cli/dist/analysis/index.js +25 -0
- package/cli/dist/analysis/index.js.map +1 -0
- package/cli/dist/analysis/security-scanner.d.ts +11 -0
- package/cli/dist/analysis/security-scanner.d.ts.map +1 -0
- package/cli/dist/analysis/security-scanner.js +294 -0
- package/cli/dist/analysis/security-scanner.js.map +1 -0
- package/cli/dist/analysis/types.d.ts +151 -0
- package/cli/dist/analysis/types.d.ts.map +1 -0
- package/cli/dist/analysis/types.js +6 -0
- package/cli/dist/analysis/types.js.map +1 -0
- package/cli/dist/cli.js +24 -0
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/code-analysis.d.ts +11 -0
- package/cli/dist/commands/code-analysis.d.ts.map +1 -0
- package/cli/dist/commands/code-analysis.js +412 -0
- package/cli/dist/commands/code-analysis.js.map +1 -0
- package/modules.md +99 -3
- package/package.json +21 -2
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
# Rule: Embedded Systems Programming
|
|
2
|
+
|
|
3
|
+
## Metadata
|
|
4
|
+
- **ID**: category-embedded
|
|
5
|
+
- **Category**: embedded
|
|
6
|
+
- **Severity**: ERROR
|
|
7
|
+
- **Standard**: MISRA C:2012, CERT C
|
|
8
|
+
- **Version**: 1.0.0
|
|
9
|
+
|
|
10
|
+
## Description
|
|
11
|
+
Embedded systems programming rules covering volatile usage, interrupt service routines (ISRs), memory constraints, hardware register access, and deterministic behavior.
|
|
12
|
+
|
|
13
|
+
## Rationale
|
|
14
|
+
Embedded systems operate under strict constraints: limited memory, real-time requirements, direct hardware interaction, and often no operating system. These rules ensure reliable, predictable, and safe embedded code.
|
|
15
|
+
|
|
16
|
+
## Applies To
|
|
17
|
+
- C Standards: c99, c11, c17
|
|
18
|
+
- Categories: embedded
|
|
19
|
+
- Platforms: Microcontrollers, bare-metal systems, RTOS environments
|
|
20
|
+
|
|
21
|
+
## Rule Details
|
|
22
|
+
|
|
23
|
+
### 1. Volatile Keyword Usage
|
|
24
|
+
- Use `volatile` for hardware registers
|
|
25
|
+
- Use `volatile` for variables shared with ISRs
|
|
26
|
+
- Use `volatile` for memory-mapped I/O
|
|
27
|
+
- Never optimize away volatile accesses
|
|
28
|
+
|
|
29
|
+
### 2. Interrupt Service Routine (ISR) Constraints
|
|
30
|
+
- Keep ISRs short and fast
|
|
31
|
+
- Avoid blocking operations in ISRs
|
|
32
|
+
- Use only async-signal-safe operations
|
|
33
|
+
- Minimize stack usage in ISRs
|
|
34
|
+
- Use atomic operations for shared data
|
|
35
|
+
|
|
36
|
+
### 3. No Dynamic Memory Allocation
|
|
37
|
+
- Avoid malloc/free in embedded systems
|
|
38
|
+
- Use static allocation or stack allocation
|
|
39
|
+
- Pre-allocate all buffers at compile time
|
|
40
|
+
- Use memory pools if dynamic allocation needed
|
|
41
|
+
|
|
42
|
+
### 4. Fixed-Size Buffers
|
|
43
|
+
- Use fixed-size arrays, not variable-length
|
|
44
|
+
- Validate buffer sizes at compile time
|
|
45
|
+
- Prevent buffer overflows with bounds checking
|
|
46
|
+
- Use circular buffers for streaming data
|
|
47
|
+
|
|
48
|
+
### 5. Hardware Register Access
|
|
49
|
+
- Use volatile pointers for memory-mapped registers
|
|
50
|
+
- Use bit manipulation macros for register fields
|
|
51
|
+
- Document register addresses and bit fields
|
|
52
|
+
- Use read-modify-write for partial updates
|
|
53
|
+
|
|
54
|
+
## Examples
|
|
55
|
+
|
|
56
|
+
### ✅ Example 1: Proper Volatile Usage for Hardware Registers
|
|
57
|
+
|
|
58
|
+
```c
|
|
59
|
+
#include <stdint.h>
|
|
60
|
+
|
|
61
|
+
// Memory-mapped register addresses
|
|
62
|
+
#define GPIO_BASE_ADDR 0x40020000
|
|
63
|
+
#define GPIO_ODR_OFFSET 0x14
|
|
64
|
+
|
|
65
|
+
// Volatile pointer to hardware register
|
|
66
|
+
#define GPIO_ODR (*(volatile uint32_t *)(GPIO_BASE_ADDR + GPIO_ODR_OFFSET))
|
|
67
|
+
|
|
68
|
+
void set_gpio_pin(uint8_t pin) {
|
|
69
|
+
GPIO_ODR |= (1U << pin); // Set bit
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void clear_gpio_pin(uint8_t pin) {
|
|
73
|
+
GPIO_ODR &= ~(1U << pin); // Clear bit
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
uint8_t read_gpio_pin(uint8_t pin) {
|
|
77
|
+
return (GPIO_ODR & (1U << pin)) ? 1 : 0;
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### ❌ Example 1: Missing Volatile
|
|
82
|
+
|
|
83
|
+
```c
|
|
84
|
+
// WRONG: Missing volatile - compiler may optimize away reads
|
|
85
|
+
#define GPIO_ODR (*(uint32_t *)(GPIO_BASE_ADDR + GPIO_ODR_OFFSET))
|
|
86
|
+
|
|
87
|
+
// This may be optimized to a single read!
|
|
88
|
+
while (GPIO_ODR & (1 << 5)) {
|
|
89
|
+
// Wait for bit to clear
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### ✅ Example 2: Interrupt Service Routine Best Practices
|
|
94
|
+
|
|
95
|
+
```c
|
|
96
|
+
#include <stdint.h>
|
|
97
|
+
#include <stdbool.h>
|
|
98
|
+
|
|
99
|
+
// Shared between ISR and main code - must be volatile
|
|
100
|
+
volatile bool data_ready = false;
|
|
101
|
+
volatile uint8_t received_byte = 0;
|
|
102
|
+
|
|
103
|
+
// ISR: Keep it short and simple
|
|
104
|
+
void UART_RX_IRQHandler(void) {
|
|
105
|
+
// Read data register (clears interrupt flag)
|
|
106
|
+
received_byte = UART_DR;
|
|
107
|
+
data_ready = true;
|
|
108
|
+
|
|
109
|
+
// That's it! No complex processing in ISR
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Main code processes the data
|
|
113
|
+
void main_loop(void) {
|
|
114
|
+
while (1) {
|
|
115
|
+
if (data_ready) {
|
|
116
|
+
// Disable interrupts while accessing shared data
|
|
117
|
+
__disable_irq();
|
|
118
|
+
uint8_t byte = received_byte;
|
|
119
|
+
data_ready = false;
|
|
120
|
+
__enable_irq();
|
|
121
|
+
|
|
122
|
+
// Process the byte (outside critical section)
|
|
123
|
+
process_data(byte);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### ❌ Example 2: Bad ISR Practices
|
|
130
|
+
|
|
131
|
+
```c
|
|
132
|
+
// WRONG: Too much processing in ISR
|
|
133
|
+
void BAD_UART_RX_IRQHandler(void) {
|
|
134
|
+
uint8_t byte = UART_DR;
|
|
135
|
+
|
|
136
|
+
// WRONG: Complex processing in ISR
|
|
137
|
+
parse_protocol(byte);
|
|
138
|
+
update_state_machine(byte);
|
|
139
|
+
send_response(byte);
|
|
140
|
+
|
|
141
|
+
// WRONG: Blocking operation
|
|
142
|
+
delay_ms(10);
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### ✅ Example 3: Static Memory Allocation
|
|
147
|
+
|
|
148
|
+
```c
|
|
149
|
+
#include <stdint.h>
|
|
150
|
+
|
|
151
|
+
#define MAX_BUFFER_SIZE 256
|
|
152
|
+
#define MAX_MESSAGES 10
|
|
153
|
+
|
|
154
|
+
// Static allocation - no malloc/free
|
|
155
|
+
typedef struct {
|
|
156
|
+
uint8_t data[MAX_BUFFER_SIZE];
|
|
157
|
+
uint16_t length;
|
|
158
|
+
} Message;
|
|
159
|
+
|
|
160
|
+
// Pre-allocated message pool
|
|
161
|
+
static Message message_pool[MAX_MESSAGES];
|
|
162
|
+
static uint8_t pool_index = 0;
|
|
163
|
+
|
|
164
|
+
Message* allocate_message(void) {
|
|
165
|
+
if (pool_index >= MAX_MESSAGES) {
|
|
166
|
+
return NULL; // Pool exhausted
|
|
167
|
+
}
|
|
168
|
+
return &message_pool[pool_index++];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
void reset_message_pool(void) {
|
|
172
|
+
pool_index = 0;
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### ❌ Example 3: Dynamic Allocation (Avoid in Embedded)
|
|
177
|
+
|
|
178
|
+
```c
|
|
179
|
+
// WRONG: Using malloc in embedded system
|
|
180
|
+
Message* bad_allocate_message(void) {
|
|
181
|
+
Message *msg = malloc(sizeof(Message)); // Fragmentation risk!
|
|
182
|
+
if (msg == NULL) {
|
|
183
|
+
// Out of memory - hard to recover
|
|
184
|
+
return NULL;
|
|
185
|
+
}
|
|
186
|
+
return msg;
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### ✅ Example 4: Circular Buffer for Streaming Data
|
|
191
|
+
|
|
192
|
+
```c
|
|
193
|
+
#include <stdint.h>
|
|
194
|
+
#include <stdbool.h>
|
|
195
|
+
|
|
196
|
+
#define BUFFER_SIZE 128
|
|
197
|
+
|
|
198
|
+
typedef struct {
|
|
199
|
+
uint8_t buffer[BUFFER_SIZE];
|
|
200
|
+
volatile uint16_t head;
|
|
201
|
+
volatile uint16_t tail;
|
|
202
|
+
} CircularBuffer;
|
|
203
|
+
|
|
204
|
+
static CircularBuffer rx_buffer = {0};
|
|
205
|
+
|
|
206
|
+
bool buffer_put(CircularBuffer *cb, uint8_t data) {
|
|
207
|
+
uint16_t next_head = (cb->head + 1) % BUFFER_SIZE;
|
|
208
|
+
|
|
209
|
+
if (next_head == cb->tail) {
|
|
210
|
+
return false; // Buffer full
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
cb->buffer[cb->head] = data;
|
|
214
|
+
cb->head = next_head;
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
bool buffer_get(CircularBuffer *cb, uint8_t *data) {
|
|
219
|
+
if (cb->head == cb->tail) {
|
|
220
|
+
return false; // Buffer empty
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
*data = cb->buffer[cb->tail];
|
|
224
|
+
cb->tail = (cb->tail + 1) % BUFFER_SIZE;
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Can be called from ISR
|
|
229
|
+
void UART_RX_ISR(void) {
|
|
230
|
+
uint8_t byte = UART_DR;
|
|
231
|
+
buffer_put(&rx_buffer, byte);
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### ✅ Example 5: Bit Manipulation Macros for Registers
|
|
236
|
+
|
|
237
|
+
```c
|
|
238
|
+
#include <stdint.h>
|
|
239
|
+
|
|
240
|
+
// Bit manipulation macros
|
|
241
|
+
#define BIT(n) (1U << (n))
|
|
242
|
+
#define SET_BIT(reg, bit) ((reg) |= BIT(bit))
|
|
243
|
+
#define CLEAR_BIT(reg, bit) ((reg) &= ~BIT(bit))
|
|
244
|
+
#define TOGGLE_BIT(reg, bit) ((reg) ^= BIT(bit))
|
|
245
|
+
#define READ_BIT(reg, bit) (((reg) >> (bit)) & 1U)
|
|
246
|
+
|
|
247
|
+
// Register field macros
|
|
248
|
+
#define FIELD_MASK(width, offset) (((1U << (width)) - 1) << (offset))
|
|
249
|
+
#define FIELD_SET(reg, value, width, offset) \
|
|
250
|
+
((reg) = ((reg) & ~FIELD_MASK(width, offset)) | \
|
|
251
|
+
(((value) << (offset)) & FIELD_MASK(width, offset)))
|
|
252
|
+
#define FIELD_GET(reg, width, offset) \
|
|
253
|
+
(((reg) & FIELD_MASK(width, offset)) >> (offset))
|
|
254
|
+
|
|
255
|
+
// Example: Configure UART
|
|
256
|
+
#define UART_CR1 (*(volatile uint32_t *)0x40011000)
|
|
257
|
+
#define UART_CR1_UE_BIT 0 // UART Enable
|
|
258
|
+
#define UART_CR1_TE_BIT 3 // Transmitter Enable
|
|
259
|
+
#define UART_CR1_RE_BIT 2 // Receiver Enable
|
|
260
|
+
|
|
261
|
+
void uart_init(void) {
|
|
262
|
+
SET_BIT(UART_CR1, UART_CR1_UE_BIT); // Enable UART
|
|
263
|
+
SET_BIT(UART_CR1, UART_CR1_TE_BIT); // Enable TX
|
|
264
|
+
SET_BIT(UART_CR1, UART_CR1_RE_BIT); // Enable RX
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### ✅ Example 6: Atomic Operations for Shared Data
|
|
269
|
+
|
|
270
|
+
```c
|
|
271
|
+
#include <stdint.h>
|
|
272
|
+
#include <stdbool.h>
|
|
273
|
+
|
|
274
|
+
volatile uint32_t shared_counter = 0;
|
|
275
|
+
|
|
276
|
+
// Atomic increment (disable interrupts)
|
|
277
|
+
void increment_counter(void) {
|
|
278
|
+
__disable_irq();
|
|
279
|
+
shared_counter++;
|
|
280
|
+
__enable_irq();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Better: Use atomic operations if available
|
|
284
|
+
#ifdef __ARM_ARCH_7M__
|
|
285
|
+
void atomic_increment_counter(void) {
|
|
286
|
+
uint32_t old_val, new_val;
|
|
287
|
+
do {
|
|
288
|
+
old_val = __LDREXW(&shared_counter);
|
|
289
|
+
new_val = old_val + 1;
|
|
290
|
+
} while (__STREXW(new_val, &shared_counter));
|
|
291
|
+
}
|
|
292
|
+
#endif
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### ✅ Example 7: Fixed-Size String Buffers
|
|
296
|
+
|
|
297
|
+
```c
|
|
298
|
+
#include <stdint.h>
|
|
299
|
+
#include <string.h>
|
|
300
|
+
|
|
301
|
+
#define MAX_NAME_LENGTH 32
|
|
302
|
+
|
|
303
|
+
typedef struct {
|
|
304
|
+
char name[MAX_NAME_LENGTH];
|
|
305
|
+
uint8_t id;
|
|
306
|
+
} Device;
|
|
307
|
+
|
|
308
|
+
// Safe string copy with bounds checking
|
|
309
|
+
void set_device_name(Device *dev, const char *name) {
|
|
310
|
+
if (dev == NULL || name == NULL) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Copy at most MAX_NAME_LENGTH-1 characters
|
|
315
|
+
strncpy(dev->name, name, MAX_NAME_LENGTH - 1);
|
|
316
|
+
dev->name[MAX_NAME_LENGTH - 1] = '\0'; // Ensure null termination
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### ✅ Example 8: Hardware Timer Configuration
|
|
321
|
+
|
|
322
|
+
```c
|
|
323
|
+
#include <stdint.h>
|
|
324
|
+
|
|
325
|
+
#define TIM2_BASE 0x40000000
|
|
326
|
+
#define TIM2_CR1 (*(volatile uint32_t *)(TIM2_BASE + 0x00))
|
|
327
|
+
#define TIM2_PSC (*(volatile uint32_t *)(TIM2_BASE + 0x28))
|
|
328
|
+
#define TIM2_ARR (*(volatile uint32_t *)(TIM2_BASE + 0x2C))
|
|
329
|
+
|
|
330
|
+
// Configure timer for 1ms tick (assuming 72MHz clock)
|
|
331
|
+
void timer_init_1ms(void) {
|
|
332
|
+
// Prescaler: 72MHz / 72 = 1MHz
|
|
333
|
+
TIM2_PSC = 72 - 1;
|
|
334
|
+
|
|
335
|
+
// Auto-reload: 1MHz / 1000 = 1ms
|
|
336
|
+
TIM2_ARR = 1000 - 1;
|
|
337
|
+
|
|
338
|
+
// Enable timer
|
|
339
|
+
TIM2_CR1 |= (1U << 0); // CEN bit
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### ✅ Example 9: Watchdog Timer Usage
|
|
344
|
+
|
|
345
|
+
```c
|
|
346
|
+
#include <stdint.h>
|
|
347
|
+
|
|
348
|
+
#define IWDG_KR (*(volatile uint32_t *)0x40003000)
|
|
349
|
+
#define IWDG_PR (*(volatile uint32_t *)0x40003004)
|
|
350
|
+
#define IWDG_RLR (*(volatile uint32_t *)0x40003008)
|
|
351
|
+
|
|
352
|
+
#define IWDG_KEY_ENABLE 0xCCCC
|
|
353
|
+
#define IWDG_KEY_RELOAD 0xAAAA
|
|
354
|
+
#define IWDG_KEY_ACCESS 0x5555
|
|
355
|
+
|
|
356
|
+
void watchdog_init(void) {
|
|
357
|
+
// Enable write access to IWDG registers
|
|
358
|
+
IWDG_KR = IWDG_KEY_ACCESS;
|
|
359
|
+
|
|
360
|
+
// Set prescaler (divide by 32)
|
|
361
|
+
IWDG_PR = 3;
|
|
362
|
+
|
|
363
|
+
// Set reload value (1 second timeout)
|
|
364
|
+
IWDG_RLR = 1000;
|
|
365
|
+
|
|
366
|
+
// Start watchdog
|
|
367
|
+
IWDG_KR = IWDG_KEY_ENABLE;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
void watchdog_refresh(void) {
|
|
371
|
+
IWDG_KR = IWDG_KEY_RELOAD;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Call this regularly in main loop
|
|
375
|
+
void main_loop(void) {
|
|
376
|
+
while (1) {
|
|
377
|
+
// Do work...
|
|
378
|
+
|
|
379
|
+
watchdog_refresh(); // Prevent reset
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### ✅ Example 10: Power Management
|
|
385
|
+
|
|
386
|
+
```c
|
|
387
|
+
#include <stdint.h>
|
|
388
|
+
|
|
389
|
+
#define SCB_SCR (*(volatile uint32_t *)0xE000ED10)
|
|
390
|
+
|
|
391
|
+
typedef enum {
|
|
392
|
+
SLEEP_MODE_SLEEP,
|
|
393
|
+
SLEEP_MODE_DEEP_SLEEP,
|
|
394
|
+
SLEEP_MODE_STANDBY
|
|
395
|
+
} SleepMode;
|
|
396
|
+
|
|
397
|
+
void enter_sleep_mode(SleepMode mode) {
|
|
398
|
+
switch (mode) {
|
|
399
|
+
case SLEEP_MODE_SLEEP:
|
|
400
|
+
// Clear SLEEPDEEP bit
|
|
401
|
+
SCB_SCR &= ~(1U << 2);
|
|
402
|
+
break;
|
|
403
|
+
|
|
404
|
+
case SLEEP_MODE_DEEP_SLEEP:
|
|
405
|
+
// Set SLEEPDEEP bit
|
|
406
|
+
SCB_SCR |= (1U << 2);
|
|
407
|
+
break;
|
|
408
|
+
|
|
409
|
+
case SLEEP_MODE_STANDBY:
|
|
410
|
+
// Configure standby mode
|
|
411
|
+
// (platform-specific)
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Wait for interrupt
|
|
416
|
+
__WFI();
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### ✅ Example 11: DMA Configuration
|
|
421
|
+
|
|
422
|
+
```c
|
|
423
|
+
#include <stdint.h>
|
|
424
|
+
|
|
425
|
+
#define DMA1_BASE 0x40020000
|
|
426
|
+
#define DMA1_CH1_CCR (*(volatile uint32_t *)(DMA1_BASE + 0x08))
|
|
427
|
+
#define DMA1_CH1_CNDTR (*(volatile uint32_t *)(DMA1_BASE + 0x0C))
|
|
428
|
+
#define DMA1_CH1_CPAR (*(volatile uint32_t *)(DMA1_BASE + 0x10))
|
|
429
|
+
#define DMA1_CH1_CMAR (*(volatile uint32_t *)(DMA1_BASE + 0x14))
|
|
430
|
+
|
|
431
|
+
static uint8_t dma_buffer[256];
|
|
432
|
+
|
|
433
|
+
void dma_init_uart_rx(void) {
|
|
434
|
+
// Disable DMA channel
|
|
435
|
+
DMA1_CH1_CCR &= ~(1U << 0);
|
|
436
|
+
|
|
437
|
+
// Set peripheral address (UART data register)
|
|
438
|
+
DMA1_CH1_CPAR = (uint32_t)&UART_DR;
|
|
439
|
+
|
|
440
|
+
// Set memory address
|
|
441
|
+
DMA1_CH1_CMAR = (uint32_t)dma_buffer;
|
|
442
|
+
|
|
443
|
+
// Set number of data items
|
|
444
|
+
DMA1_CH1_CNDTR = sizeof(dma_buffer);
|
|
445
|
+
|
|
446
|
+
// Configure: Memory increment, circular mode, peripheral-to-memory
|
|
447
|
+
DMA1_CH1_CCR = (1U << 7) | // MINC: Memory increment
|
|
448
|
+
(1U << 5) | // CIRC: Circular mode
|
|
449
|
+
(0U << 4); // DIR: Peripheral-to-memory
|
|
450
|
+
|
|
451
|
+
// Enable DMA channel
|
|
452
|
+
DMA1_CH1_CCR |= (1U << 0);
|
|
453
|
+
}
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
### ✅ Example 12: Compile-Time Assertions for Buffer Sizes
|
|
457
|
+
|
|
458
|
+
```c
|
|
459
|
+
#include <stdint.h>
|
|
460
|
+
|
|
461
|
+
#define BUFFER_SIZE 256
|
|
462
|
+
|
|
463
|
+
// Compile-time assertion
|
|
464
|
+
#define STATIC_ASSERT(condition, message) \
|
|
465
|
+
typedef char static_assertion_##message[(condition) ? 1 : -1]
|
|
466
|
+
|
|
467
|
+
// Ensure buffer size is power of 2 (for efficient modulo)
|
|
468
|
+
STATIC_ASSERT((BUFFER_SIZE & (BUFFER_SIZE - 1)) == 0,
|
|
469
|
+
buffer_size_must_be_power_of_2);
|
|
470
|
+
|
|
471
|
+
// Ensure buffer fits in available RAM
|
|
472
|
+
#define AVAILABLE_RAM 8192
|
|
473
|
+
STATIC_ASSERT(BUFFER_SIZE <= AVAILABLE_RAM,
|
|
474
|
+
buffer_exceeds_available_ram);
|
|
475
|
+
|
|
476
|
+
uint8_t buffer[BUFFER_SIZE];
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
## References
|
|
480
|
+
|
|
481
|
+
- MISRA C:2012 Guidelines for the use of the C language in critical systems
|
|
482
|
+
- CERT C Coding Standard
|
|
483
|
+
- Embedded C Coding Standard (Michael Barr)
|
|
484
|
+
- ARM Cortex-M Programming Guide
|
|
485
|
+
- Microcontroller vendor reference manuals
|
|
486
|
+
|
|
487
|
+
## Related Rules
|
|
488
|
+
|
|
489
|
+
- universal-memory-safety
|
|
490
|
+
- universal-const-correctness
|
|
491
|
+
- category-realtime
|
|
492
|
+
|
|
493
|
+
## Configuration
|
|
494
|
+
|
|
495
|
+
Enable in `.augment/c-standards.json`:
|
|
496
|
+
|
|
497
|
+
```json
|
|
498
|
+
{
|
|
499
|
+
"categories": ["embedded"],
|
|
500
|
+
"category_overrides": {
|
|
501
|
+
"embedded": {
|
|
502
|
+
"require_volatile_for_hardware": true,
|
|
503
|
+
"prohibit_dynamic_allocation": true,
|
|
504
|
+
"require_fixed_buffers": true,
|
|
505
|
+
"max_isr_duration_us": 100
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
```
|
|
510
|
+
|