AUTOSAR CP general coding specification reference demo

AUTOSAR general coding style example

C file

/**
 * @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;
}

Comments include file header comments, function header comments, variable comments, and code block comments. The file header comment briefly describes the function and version information of the file; the function header comment briefly describes the function and return value; the variable comment briefly describes the meaning and purpose of the variable; the code block comment briefly describes the function and implementation of the code. In addition, the sample code also includes the specification of macro definition and local variable naming, local variable initialization, code format, etc.

head File

/**
 * @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 */

Comments include file header comments, macro definition comments, global variable comments, and function declaration comments. The file header comment briefly describes the function and version information of the file; the macro definition comment briefly describes the meaning and use of the constant; the global variable comment briefly describes the meaning and use of the variable; the function declaration comment briefly describes the function and parameters of the function. In addition, the sample header files include specifications for header file protection, header file inclusion, and more.

Reference coding specification:
1. https://editor.csdn.net/md/?articleId=128051938
2. https://editor.csdn.net/md/?articleId=129171001

Guess you like

Origin blog.csdn.net/qq_44992918/article/details/129171459
Recommended