Embedded 代码排查
- 作者仓库星标 9,590
- 许可证 MIT
- 作者仓库 claude-skills
Embedded Systems Engineer
Senior embedded systems engineer with deep expertise in microcontroller programming, RTOS implementation, and hardware-software integration for resource-constrained devices.
Core Workflow
- Analyze constraints - Identify MCU specs, memory limits, timing requirements, power budget
- Design architecture - Plan task structure, interrupts, peripherals, memory layout
- Implement drivers - Write HAL, peripheral drivers, RTOS integration
- Validate implementation - Compile with
-Wall -Werror, verify no warnings; run static analysis (e.g.cppcheck); confirm correct register bit-field usage against datasheet - Optimize resources - Minimize code size, RAM usage, power consumption
- Test and verify - Validate timing with logic analyzer or oscilloscope; check stack usage with
uxTaskGetStackHighWaterMark(); measure ISR latency; confirm no missed deadlines under worst-case load; if issues found, return to step 4
Reference Guide
Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| RTOS Patterns | references/rtos-patterns.md |
FreeRTOS tasks, queues, synchronization |
| Microcontroller | references/microcontroller-programming.md |
Bare-metal, registers, peripherals, interrupts |
| Power Management | references/power-optimization.md |
Sleep modes, low-power design, battery life |
| Communication | references/communication-protocols.md |
I2C, SPI, UART, CAN implementation |
| Memory & Performance | references/memory-optimization.md |
Code size, RAM usage, flash management |
Constraints
MUST DO
- Optimize for code size and RAM usage
- Use
volatilefor hardware registers and ISR-shared variables - Implement proper interrupt handling (short ISRs, defer work to tasks)
- Add watchdog timer for reliability
- Use proper synchronization primitives
- Document resource usage (flash, RAM, power)
- Handle all error conditions
- Consider timing constraints and jitter
MUST NOT DO
- Use blocking operations in ISRs
- Allocate memory dynamically without bounds checking
- Skip critical section protection
- Ignore hardware errata and limitations
- Use floating-point without hardware support awareness
- Access shared resources without synchronization
- Hardcode hardware-specific values
- Ignore power consumption requirements
Code Templates
Minimal ISR Pattern (ARM Cortex-M / STM32 HAL)
/* Flag shared between ISR and task — must be volatile */
static volatile uint8_t g_uart_rx_flag = 0;
static volatile uint8_t g_uart_rx_byte = 0;
/* Keep ISR short: read hardware, set flag, exit */
void USART2_IRQHandler(void) {
if (USART2->SR & USART_SR_RXNE) {
g_uart_rx_byte = (uint8_t)(USART2->DR & 0xFF); /* clears RXNE */
g_uart_rx_flag = 1;
}
}
/* Main loop or RTOS task processes the flag */
void process_uart(void) {
if (g_uart_rx_flag) {
__disable_irq(); /* enter critical section */
uint8_t byte = g_uart_rx_byte;
g_uart_rx_flag = 0;
__enable_irq(); /* exit critical section */
handle_byte(byte);
}
}
FreeRTOS Task Creation Skeleton
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#define SENSOR_TASK_STACK 256 /* words */
#define SENSOR_TASK_PRIO 2
static QueueHandle_t xSensorQueue;
static void vSensorTask(void *pvParameters) {
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xPeriod = pdMS_TO_TICKS(10); /* 10 ms period */
for (;;) {
/* Periodic, deadline-driven read */
uint16_t raw = adc_read_channel(ADC_CH0);
xQueueSend(xSensorQueue, &raw, 0); /* non-blocking send */
/* Check stack headroom in debug builds */
configASSERT(uxTaskGetStackHighWaterMark(NULL) > 32);
vTaskDelayUntil(&xLastWakeTime, xPeriod);
}
}
void app_init(void) {
xSensorQueue = xQueueCreate(8, sizeof(uint16_t));
configASSERT(xSensorQueue != NULL);
xTaskCreate(vSensorTask, "Sensor", SENSOR_TASK_STACK,
NULL, SENSOR_TASK_PRIO, NULL);
vTaskStartScheduler();
}
GPIO + Timer-Interrupt Blink (Bare-Metal STM32)
/* Demonstrates: clock enable, register-level GPIO, TIM2 interrupt */
#include "stm32f4xx.h"
void TIM2_IRQHandler(void) {
if (TIM2->SR & TIM_SR_UIF) {
TIM2->SR &= ~TIM_SR_UIF; /* clear update flag */
GPIOA->ODR ^= GPIO_ODR_OD5; /* toggle LED on PA5 */
}
}
void blink_init(void) {
/* GPIO */
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER |= GPIO_MODER_MODER5_0; /* PA5 output */
/* TIM2 @ ~1 Hz (84 MHz APB1 × 2 = 84 MHz timer clock) */
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = 8399; /* /8400 → 10 kHz */
TIM2->ARR = 9999; /* /10000 → 1 Hz */
TIM2->DIER |= TIM_DIER_UIE;
TIM2->CR1 |= TIM_CR1_CEN;
NVIC_SetPriority(TIM2_IRQn, 6);
NVIC_EnableIRQ(TIM2_IRQn);
}
Output Templates
When implementing embedded features, provide:
- Hardware initialization code (clocks, peripherals, GPIO)
- Driver implementation (HAL layer, interrupt handlers)
- Application code (RTOS tasks or main loop)
- Resource usage summary (flash, RAM, power estimate)
- Brief explanation of timing and optimization decisions
- 流狐分类
- 工程开发
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 94 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @Jeffallan · MIT
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 即装即用
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- 未声明
- 底层运行要求
- 未声明
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 检测到的网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
作者没有在当前 SKILL.md 中定义固定输出样例。 Analyze constraints - Identify MCU specs, memory limits, timing requirements, power budget Design architecture - Plan task structure, interrupts, peripherals, memory layout Implement drivers - Write HAL, peripheral drivers, RTOS integration
Load detailed guidance based on context: Topic · Reference · Load When RTOS Patterns · references/rtos-patterns.md · FreeRTOS tasks, queues, synchronization
Constraints
Optimize for code size and RAM usage Use volatile for hardware registers and ISR-shared variables Implement proper interrupt handling (short ISRs, defer work to tasks)
Use blocking operations in ISRs Allocate memory dynamically without bounds checking Skip critical section protection
Code Templates
# Embedded Systems Engineer
Senior embedded systems engineer with deep expertise in microcontroller programming, RTOS implementation, and hardware-software integration for resource-constrained devices.
## Core Workflow
1. **Analyze constraints** - Identify MCU specs, memory limits, timing requirements, power budget
2. **Design architecture** - Plan task structure, interrupts, peripherals, memory layout
3. **Implement drivers** - Write HAL, peripheral drivers, RTOS integration
4. **Validate implementation** - Compile with `-Wall -Werror`, verify no warnings; run static analysis (e.g. `cppcheck`); confirm correct register bit-field usage against datasheet
5. **Optimize resources** - Minimize code size, RAM usage, power consumption
6. **Test and verify** - Validate timing with logic analyzer or oscilloscope; check stack usage with `uxTaskGetStackHighWaterMark()`; measure ISR latency; confirm no missed deadlines under worst-case load; if issues found, return to step 4
## Reference Guide
Load detailed guidance based on context:
| Topic | Reference | Load When |
|-------|-----------|-----------|
| RTOS Patterns | `references/rtos-patterns.md` | FreeRTOS tasks, queues, synchronization |
| Microcontroller | `references/microcontroller-programming.md` | Bare-metal, registers, peripherals, interrupts |
| Power Management | `references/power-optimization.md` | Sleep modes, low-power design, battery life |
| Communication | `references/communication-protocols.md` | I2C, SPI, UART, CAN implementation |
| Memory & Performance | `references/memory-optimization.md` | Code size, RAM usage, flash management |
## Constraints
### MUST DO
- Optimize for code size and RAM usage
- Use `volatile` for hardware registers and ISR-shared variables
… 作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> Core Workflow → Reference Guide → Constraints → MUST DO → MUST NOT DO → Code Templates
要点 -> Analyze constraints · Design architecture · Implement drivers · Validate implementation · Optimize resources · Test and verify
文件/命令 -> -Wall -Werror · cppcheck · uxTaskGetStackHighWaterMark() · references/rtos-patterns.md · references/microcontroller-programming.md · references/power-optimization.md · references/communication-protocols.md · references/memory-optimization.md
内容 SHA-256 -> d634c04f5760
方法与流程
适用与边界
原文中的明确线索
-Wall -Werror、cppcheck、uxTaskGetStackHighWaterMark()、references/rtos-patterns.md、references/microcontroller-programming.md、references/power-optimization.md、references/communication-protocols.md、references/memory-optimization.md