embedded-cstyle
Skill9 starsChanged 6 months agoMITView on GitHub
Use when generating, modifying, reviewing, or refactoring C code for embedded firmware projects (STM32, GD32, ESP32, FreeRTOS, bare-metal, driver development, robotics). Enforces engineering coding standards — naming, types, functions, variables, macros, headers, memory, concurrency, error handling, and documentation. Catches AI-common mistakes: global variables, magic numbers, missing static/const, volatile abuse, ignored return values, unchecked null pointers, and HAL calls leaking into business logic.
In this repository6 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- embedded-architectureSkill
- embedded-documentationSkill
- embedded-driver-designSkill
- embedded-isr-designSkill
- embedded-rtos-designSkill
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 6 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 45
Only interface/ and bsp/ layers may access vendor HAL APIs.
- Very specific: line 119
Always use <stdint.h> fixed-width types.
- Distinctive: line 282
Every header must use either:
- Very specific: line 301
Prefer forward declarations over #include in headers:
- Distinctive: line 329
Use static allocation instead:
- Distinctive: line 361
Use a single project-wide error enum rather than magic return values:
- Distinctive: line 385
All public interfaces must carry a Doxygen-style doc block:
- Distinctive: line 400
After generating or reviewing code, verify:
- Distinctive: line 419
When violations are found:
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Embedded C Style
Purpose
This skill defines mandatory coding standards for embedded firmware projects.
Goals:
- readability
- maintainability
- portability
- safety
- deterministic behavior
The style is inspired by MISRA-C, Linux Kernel Style, AUTOSAR C Guidelines, and STM32 firmware best practices — adapted for practical use in small-to-medium embedded projects rather than rigid compliance.
This skill applies whenever generating, modifying, reviewing, or refactoring C code.
Priority order when making trade-offs: Safety > Determinism > Maintainability > Readability.
Interface Boundary Rule
Application and business modules shall never call HAL APIs directly.
Forbidden in app/ and module/:
HAL_GPIO_WritePin()
HAL_UART_Transmit()
HAL_SPI_Transmit()
HAL_I2C_Master_Transmit()Only interface/ and bsp/ layers may access vendor HAL APIs.
Naming Rules
Type Naming
Format: typename_t
Required:
motor_t
imu_t
pid_t
uart_config_tForbidden:
Motor
IMU
PIDStruct
UART_CONFIGFunction Naming
Format: module_action() — lowercase, underscore-separated.
Required:
motor_init()
motor_set_speed()
imu_read()
pid_update()Forbidden:
InitMotor()
ReadIMU()
PID()Internal (file-scope) functions must be declared static:
static void motor_update(void);Macro Naming
Format: MODULE_DESCRIPTION — uppercase, underscore-separated.
Required:
MOTOR_MAX_SPEED_RPM
UART_RX_BUFFER_SIZE
PID_DEFAULT_KPForbidden:
MaxSpeed
bufferSizeType Rules
Always use <stdint.h> fixed-width types.
Forbidden:
int
short
long
unsignedRequired:
uint8_t
uint16_t
uint32_t
int8_t
int16_t
int32_tBooleans must use <stdbool.h>:
#include <stdbool.h>
bool ready;Forbidden as boolean substitutes:
u8 flag;
uint8_t status;Variable Rules
Global Variables
Plain global variables are forbidden:
int speed; // forbiddenFile-private statics are allowed:
static int32_t speed; // allowedCross-module access must go through interface functions:
motor_get_speed() // correctNot:
extern int speed; // forbiddenconst Preference
Immutable data must be declared const:
static const uint8_t crc_table[256];
static const float pid_gains[3] = {1.0f, 0.1f, 0.05f};volatile Discipline
volatile is only allowed for:
- ISR-shared variables
- DMA status variables
- Memory-mapped peripheral registers
volatile bool uart_rx_done; // correctForbidden on ordinary variables:
volatile int count; // forbiddenFunction Rules
Parameter Validation
Pointer parameters must be checked before dereference:
if (ptr == NULL) {
return ERR_INVALID_PARAM;
}Forbidden:
ptr->data = 1; // no null checkReturn Value Checking
Callers must check return values:
ret = uart_write(...);
if (ret != ERR_OK) {
return ret;
}Forbidden:
uart_write(...); // return value ignoredMacro Rules
No Magic Numbers
Forbidden:
if (speed > 123)Required:
#define MOTOR_MAX_SPEED_RPM (123U)
if (speed > MOTOR_MAX_SPEED_RPM)Parenthesize Macro Bodies
Forbidden:
#define MAX_SPEED 100 + 20Required:
#define MAX_SPEED (120U)Header Rules
Include Guard
Every header must use either:
#ifndef MOTOR_H
#define MOTOR_H
// ...
#endifor:
#pragma onceMinimize Header Dependencies
Prefer forward declarations over #include in headers:
struct motor; // forward declarationRather than:
#include "a.h"
#include "b.h"
#include "c.h"
#include "d.h"Memory Rules
No Dynamic Allocation
Runtime heap allocation is forbidden:
malloc()
calloc()
realloc()
free()Use static allocation instead:
static uint8_t rx_buffer[256];This prevents heap fragmentation and unpredictable allocation latency — critical for real-time embedded systems.
Concurrency Rules
Critical Sections
Shared resources accessed from multiple threads or ISRs must be protected:
osMutexAcquire(mutex_id, timeout);
// ... access shared resource ...
osMutexRelease(mutex_id);or:
taskENTER_CRITICAL();
// ... access shared resource ...
taskEXIT_CRITICAL();Error Handling Rules
Unified Error Codes
Use a single project-wide error enum rather than magic return values:
typedef enum {
ERR_OK = 0,
ERR_TIMEOUT,
ERR_BUSY,
ERR_INVALID_PARAM,
ERR_NOT_INITIALIZED,
ERR_OVERFLOW,
ERR_UNDERFLOW,
} error_t;Forbidden:
return -1;
return -2;
return -3;Documentation Rules
All public interfaces must carry a Doxygen-style doc block:
/**
* @brief Set motor speed
*
* @param rpm Target speed in revolutions per minute
*
* @return error_t ERR_OK on success
*/
error_t motor_set_speed(int32_t rpm);Validation Checklist
After generating or reviewing code, verify:
<stdint.h>types used — no bareint,short,long,unsigned<stdbool.h>used for booleans- Internal functions declared
static - No unchecked pointer dereference
- No ignored return values
- No runtime
malloc/free - No magic numbers — all constants are named macros
- Macro bodies parenthesized
- Global variables minimized; cross-module access via functions
constused wherever data is immutablevolatileused only for ISR/DMA/register variables- Naming follows
module_action,typename_t,MACRO_NAMEconventions - Public interfaces have Doxygen doc blocks
- No HAL calls in
app/ormodule/
Auto Fix Strategy
When violations are found:
- Report the violation with file and line
- Explain the embedded impact (safety, determinism, or portability risk)
- Provide corrected code inline
- Refactor automatically if the fix is mechanical
Priority order for fixes:
Safety > Determinism > Maintainability > ReadabilityDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

