代码规范(MISRA C)
← 返回 MOC | ← 主页
官方文档
博客解读MISRA C
博客文章:代码规范总结
1.每个文件都要有的:
/******************************************************************************
* Copyright (C) 2026 XXXXXXXXXXX, Inc.(Gmbh) or its affiliates.
*
* All Rights Reserved.
*
* @file XXXXXXXXXXXXXX.h
*
* @par dependencies
* - XXXXXXXXXXXXXXXXXX.h
* - stdio.h
* - stdint.h
*
* @author YA
*
* @brief Provide the HAL APIs of AHT21 and corresponding opetions.
*
* Processing flow:
*
* call directly.
*
* @version V1.0
*
* @note 1 tab == 4 spaces!
*
*****************************************************************************/
2.头文件:
#ifndef xxxxxxxxxxxxxxxxx //Avoid repeated including same files later
#define xxxxxxxxxxxxxxxxx
//********************************Includes*********************************//
#include"xxxxxxxxxxxxxxxxx.h"// 包含自己的寄存器.h文件一定是在最开始
#include <stdint.h>
#include <stdio.h>
//********************************Includes*********************************//
#endif
3.函数注释:
/**
* @brief Instantiates the bsp_led_handler_t target.
*
* Steps:
* 1. Adds Core interfaces into bsp_led_driver instance target.
* 2. Adds OS interfaces into bsp_led_driver instance target.
* 3. Adds timebase interfaces into bsp_led_driver instance target.
*
* @param[in] self : Pointer to the target of handler.
* @param[in] os_delay : Pointer to the os_delay_interface.
* @param[in] os_queue : Pointer to the os_queue_interface.
* @param[in] os_critical : Pointer to the os_critical_interface.
* @param[in] os_thread : Pointer to the os_thread_interface.
* @param[in] time_base : Pointer to the time_base_interface.
*
* @return led_handler_status_t : Status of the function.
*
*/
led_handler_status_t led_handler_inst(
bsp_led_handler_t * const self,
#ifdef OS_SUPPORTING
os_delay_t * const os_delay,
handler_os_queue_t * const os_queue,
handler_os_critical_t * const os_critical,
handler_os_thread_t * const os_thread,
#endif //OS_SUPPORTING
handler_time_base_ms_t * const time_base );
4.变量命名规则:
- 匈牙利命名-----下划线隔开
- 变量名统一小写,枚举项和宏定义统一大写
- 全局变量
g_开头
- 指针变量
p_开头
- 函数指针变量
pf_开头
5.函数部分:
- 所有函数都要返回状态(用枚举项返回),
- 一开始要有一段是判断参数的(比如是否空指针),不正常直接return
3.杂项:
- 缩进: 1Tab==4Spaces
- 常数放到 == 的左边
- 代码宽度不超过80列,若函数传参太长,可换行参数,其他同理