AUTOSAR CP通用编码规范参考demo

AUTOSAR通用编码风格示例

C文件

/**
 * @file    example.c
 * @brief   This is an example source file for AUTOSAR coding style.
 *          It contains functions for controlling the speed of a vehicle.
 * @author  John Doe
 * @version 1.0
 * @date    2023-02-22
 */

/* Include header files */
#include "example.h"

/* Define macros */
#define VEHICLE_SPEED_LIMIT   (120) /* The maximum speed of the vehicle */

/* Declare global variables */
uint8_t gu8VehicleSpeed; /* The current speed of the vehicle */

/**
 * @brief   This function initializes the vehicle speed control system.
 * @return  None
 */
void init_vehicle_speed(void)
{
    
    
    /* Initialize the global variable */
    gu8VehicleSpeed = 0;
}

/**
 * @brief   This function reads the current speed of the vehicle.
 * @return  The current speed of the vehicle in km/h.
 */
uint8_t read_vehicle_speed(void)
{
    
    
    /* Read the speed from the sensor */
    uint8_t u8Speed = read_speed_sensor();

    /* Check if the speed is above the limit */
    if (u8Speed > VEHICLE_SPEED_LIMIT)
    {
    
    
        /* Set the speed to the limit */
        u8Speed = VEHICLE_SPEED_LIMIT;

        /* Log a warning message */
        log_warning("Vehicle speed is above the limit");
    }

    /* Save the speed to the global variable */
    gu8VehicleSpeed = u8Speed;

    /* Return the speed */
    return u8Speed;
}

/**
 * @brief   This function sets the speed of the vehicle.
 * @param   u8Speed The desired speed of the vehicle in km/h.
 * @return  None
 */
void set_vehicle_speed(uint8_t u8Speed)
{
    
    
    /* Check if the speed is above the limit */
    if (u8Speed > VEHICLE_SPEED_LIMIT)
    {
    
    
        /* Set the speed to the limit */
        u8Speed = VEHICLE_SPEED_LIMIT;

        /* Log a warning message */
        log_warning("Vehicle speed is above the limit");
    }

    /* Set the speed */
    set_speed_actuator(u8Speed);

    /* Save the speed to the global variable */
    gu8VehicleSpeed = u8Speed;
}

/**
 * @brief   This function gets the speed of the vehicle.
 * @param   pu8Speed Pointer to a variable to store the speed.
 * @return  None
 */
void get_vehicle_speed(uint8_t *pu8Speed)
{
    
    
    /* Get the speed from the global variable */
    *pu8Speed = gu8VehicleSpeed;
}

注释包括文件头注释、函数头注释、变量注释和代码块注释。文件头注释简要地描述文件的作用和版本信息;函数头注释简要地描述函数的作用和返回值;变量注释简要地描述变量的含义和用途;代码块注释简要地描述代码的作用和实现方式。此外,示例代码还包含了宏定义和局部变量的命名、局部变量的初始化、代码格式等方面的规范。

头文件

/**
 * @file    example.h
 * @brief   This is an example header file for AUTOSAR coding style.
 *          It contains function prototypes for controlling the speed of a vehicle.
 *          It also declares constants and global variables for the module.
 * @author  John Doe
 * @version 1.0
 * @date    2023-02-22
 */

#ifndef EXAMPLE_H
#define EXAMPLE_H

/* Include header files */
#include "std_types.h"

/* Declare constants */
#define VEHICLE_SPEED_LIMIT   (120) /* The maximum speed of the vehicle */

/* Declare global variables */
extern uint8_t gu8VehicleSpeed; /* The current speed of the vehicle */

/* Declare function prototypes */
void init_vehicle_speed(void);
uint8_t read_vehicle_speed(void);
void set_vehicle_speed(uint8_t u8Speed);
void get_vehicle_speed(uint8_t *pu8Speed);

#endif /* EXAMPLE_H */

注释包括文件头注释、宏定义注释、全局变量注释和函数声明注释。文件头注释简要地描述文件的作用和版本信息;宏定义注释简要地描述常量的含义和用途;全局变量注释简要地描述变量的含义和用途;函数声明注释简要地描述函数的作用和参数。此外,示例头文件还包含了头文件保护、头文件包含等方面的规范。

参考编码规范:
1、https://editor.csdn.net/md/?articleId=128051938
2、https://editor.csdn.net/md/?articleId=129171001

猜你喜欢

转载自blog.csdn.net/qq_44992918/article/details/129171459