---
id: embedded-coding-style
name: "embedded-coding-style"
url: https://skills.yangsir.net/skill/embedded-coding-style
author: Z1R343L-D77
domain: automation
tags: ["embedded", "c-style", "code-quality", "automation", "misra"]
install_count: 120
rating: 4.00 (26 reviews)
github: https://github.com/Z1R343L-D77/embedded-coding-style-codex-skills
---

# embedded-coding-style

> 此Skill专为嵌入式C项目设计，提供类MISRA的编码风格治理。它强调严格的GB2312编码安全，支持可选范围执行，并能对代码风格进行可验证的规则检查，确保项目代码质量和一致性。

**Stats**: 120 installs · 4.0/5 (26 reviews)

## Before / After 对比

### 嵌入式C代码风格治理效率

**Before**:

在没有自动化工具辅助时，嵌入式C项目团队需要投入大量时间进行人工代码风格审查，逐一检查编码规范、括号位置、注释格式等，耗时且容易遗漏，导致代码库风格不一致，增加维护成本。

**After**:

通过SkillForge的嵌入式编码风格Skill，团队可以自动化执行MISRA-like风格检查与修正，确保所有代码严格遵循GB2312编码和统一规范，显著减少人工审查时间，提升代码质量和开发效率。

| Metric | Before | After | Change |
|---|---|---|---|
| 代码风格审查与修正时间 | 60分钟 | 10分钟 | -83% |

## Readme

---
name: embedded-coding-style
description: "Initialize embedded project context, then apply and verify MISRA-like C coding style rules with selectable scope, ANSI encoding policy, brace policy, header/doc comment policy, and verification checklist."
---

# Embedded Coding Style Skill

Initialize project context first, then apply a strict, repeatable embedded C style policy to selected scope.

---

## Initialization + Scope Selection

### Step 0: Initialize Project Context (Mandatory)

Before any edit, initialize the project and gather context:

```powershell
Get-Location
Get-ChildItem -Path <project_root>
```

If backup is provided, verify backup path exists before edits.

### Step 1: Select Scope (Mandatory)

Choose one scope per run:

1. Driver layer only: `<project_root>/<driver_dir>`
2. BSP layer only: `<project_root>/<bsp_dir>`
3. App layer only: `<project_root>/<app_dir>`
4. Combined subset: any two scopes
5. Full target: all selected core scopes

Default behavior: if user does not specify scope, use full target scope.

Do not edit outside selected scope.

---

## Hard Rules

1. Encoding: `GB2312` (code page 936), mandatory.
2. Line ending: `CRLF`.
3. File end: one blank line (`CRLF+CRLF`).
4. Every `.c/.h` begins with:

```c
/**
 * @file ...
 * @author  ...
 * @brief ...
 * @version ...
 * @date ...
 *
 * @copyright Copyright (c) 2026
 */
```

5. Function doc comment uses `/** ... */` and includes `@brief @param @retval`.
6. Inline/block explanation comments use `/* ... */`.
7. Comment language: Chinese; avoid AI-template phrasing.
8. Standard headers use `<>` (e.g. `<stdint.h>`, `<stdio.h>`, `<string.h>`, `<math.h>`, `<stddef.h>`).
9. Project headers use `"..."`.
10. Header files keep include guard and must contain C++ compatibility block:

```c
#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif
```

11. Left brace must be on a new line.
12. Brace indentation must align with control statement indentation.
13. Always use braces for single-line bodies:
- `if`
- `while`
- `else if`
- `else`
- (also keep `for/switch/do` as full blocks)

14. `switch` requires `default`.
15. Each `case` must end with explicit `break/return` (or controlled jump).
16. Prefer explicit comparisons; avoid implicit truthiness when unclear.
17. Integer literals should use explicit suffixes where applicable (`U`, `UL`).
18. Macro parameters/expressions require parentheses.
19. Multi-statement macros require `do { ... } while (0)`.
20. Minimize global visibility: file-private symbols should be `static`.
21. Pointer params should be null-checked before dereference for externally reachable paths.
22. Avoid magic numbers; extract to macro/constant (prefer existing project style).
23. Do not alter timing/ISR/state-machine behavior for formatting-only tasks.
24. Text read/write encoding is mandatory `GB2312` (code page 936), no auto-detect, no fallback encoding.
25. Read path must use strict `GB2312` decoding (decoder exception fallback enabled); if decode fails, stop immediately.
26. Write path must use strict `GB2312` encoding (encoder exception fallback enabled); if encode fails, stop immediately and rollback.
27. Remove obvious AI-style comments (for example: `/* global variable with static scope */`); rewrite as natural engineering comments that describe current module intent/context, not generic coding slogans.
28. Struct-pointer function parameters must use unified name `handle` (for example: `static void gpio_control_work(GPIO_Control_t *handle)`), avoid mixed aliases such as `p_ctrl`/`p_xxx`.

---

## Execution Procedure

### Step 2: Pre-check Files in Selected Scope

Enumerate selected files only:

```powershell
Get-ChildItem -Path <selected_scope_paths> -Recurse -File
```

### Step 3: Safe Edit Strategy

- Edit in small passes (headers first, then braces, then comments).
- Avoid broad regex that can touch function comments repeatedly.
- For file header replacement, replace only the first top-of-file block comment.
- During comment cleanup, rewrite AI-template comments into natural project comments; preserve technical meaning and avoid repetitive rule-recitation wording.
- During signature cleanup, rename struct-pointer parameters to `handle` consistently and update in-function references in the same scope.
- Run strict encoding preflight before first write:
  - Force `GB2312` decode for source text (code page 936).
  - Decoder fallback must be exception-based; any decode error means immediate stop.
  - Do not switch to UTF-8/UTF-16/other encodings as fallback.
- Required operation: explicit `GB2312` read + explicit `GB2312` write for every touched `.c/.h` file.

### Step 4: Brace Normalization

- Convert control statements to newline brace style.
- Add missing braces to all single-line `if/while/else if/else` bodies.
- Ensure opening brace indent equals control-statement indent.

### Step 5: Header/C++ Guard Normalization

- Keep include guard.
- Ensure exactly one open C++ block and one close C++ block.
- Place close C++ block immediately before final `#endif` of include guard.

### Step 6: Include Normalization

- Convert standard C headers from `"x.h"` to `<x.h>`.
- Keep project headers in `"x.h"`.

### Step 7: Final Newline + Encoding Pass

- Normalize newline to CRLF.
- Trim trailing blank noise.
- Force final `CRLF+CRLF`.
- Write using `GB2312` (code page 936) with strict encoder fallback (exception on unmappable chars).

---

## Verification Checklist

Run and confirm all are clean (within selected scope only):

1. `if/while/else if/else` without brace:
```powershell
rg -n "^\s*(if|while|else\s+if|else)\b" <selected_scope_paths>
```

2. C++ guards exist in headers:
```powershell
rg -n "#ifdef __cplusplus|extern \"C\"|#endif" <selected_scope_paths>
```

3. File header presence:
```powershell
rg -n "^/\*\*|@file|@author|@brief|@version|@date" <selected_scope_paths>
```

4. Tail bytes check (`13,10,13,10` expected):
```powershell
Get-ChildItem <selected_scope_paths> -Recurse -File | ForEach-Object {
  $b=[IO.File]::ReadAllBytes($_.FullName)
  $tail=if($b.Length -ge 4){$b[($b.Length-4)..($b.Length-1)]}else{$b}
  "{0}:{1}" -f $_.FullName,($tail -join ',')
}
```

5. Spot-check random files for Chinese comment readability.
6. Encoding safety check:
```powershell
rg -n "\?\?\?|\uFFFD" <selected_scope_paths>
```
Result must be empty for newly changed content.

---

## Recovery / Rollback

- If text becomes mojibake or strict `GB2312` decode/encode throws:
  - Stop immediately.
  - Restore from known-good backup.
  - Re-apply rules with explicit strict `GB2312` read/write configuration.
- Never perform non-GB2312 encode/decode cycles.
- Do not continue batch edits after detecting corruption or codec exception.
- If strict GB2312 preflight fails, only allow byte-level operations (copy/backup/line-ending check), no semantic rewrite.

---

## Success Criteria

- All files in selected scope comply with Hard Rules.
- No mojibake/no `?` replacement damage in Chinese comments.
- No unintended behavior changes.
- Diff is style-focused and reviewable.

---
*Source: https://skills.yangsir.net/skill/embedded-coding-style*
*Markdown mirror: https://skills.yangsir.net/api/skill/embedded-coding-style/markdown*