@/"...
1> 文件结构
1.1> 头文件结构
/**
******************************************************************************
* @file : demo.h
* @author : X
* @date : 04/11, 2021
* @brief : Just Do It
*
******************************************************************************
* @attention
*
*
******************************************************************************
*/
#ifndef __DEMO_H__
#define __DEMO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* 0> Includes ---------------------------------------------------------------*/
/* 1> Exported Types ---------------------------------------------------------*/
/* 2> Exported Constants -----------------------------------------------------*/
/* 3> Exported macro ---------------------------------------------------------*/
/* 4> Exported functions -----------------------------------------------------*/
/* 5> Private types ----------------------------------------------------------*/
/* 6> Private Constants ------------------------------------------------------*/
/* 7> Private macros ---------------------------------------------------------*/
/* 8> Private functions ------------------------------------------------------*/
#ifdef __cplusplus
}
#endif
#endif
/* End of file ****************************************************************/
1.2> C文件结构
/**---------------------Copyright (C) SUB Corporation-------------------------**
* @File mani.c
* @Author X@/''
* @Version V1.0
* @Date 12/13, 2020
*
**---------------------------------------------------------------------------**
* @Description
*
*
*
**---------------------------------------------------------------------------**
* @Attention
*
**---------------------------------------------------------------------------*/
/* @Includes -----------------------------------------------------------------*/
模块1> "头文件包含"
/* @Private typedef ----------------------------------------------------------*/
模块2> "数据类型声明"
/* @Private constants --------------------------------------------------------*/
模块3> "常量定义"
/* @Private macro ------------------------------------------------------------*/
模块4> "宏函数"
/* @Private variables --------------------------------------------------------*/
模块5> "变量声明"
/* @Private function prototypes ----------------------------------------------*/
模块6> "私有函数声明"
/* @Private functions --------------------------------------------------------*/
模块7> "宏函实现"
/**
* @brief
* @param
* @retval
*
*/
int main ()
{
while (1) {
;
}
}
/* main.c */
2> 代码示例
2.1> 函数示例
int hex_to_bin(char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
ch = tolower(ch);
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
return -1;
}
-----------------------------------------
2.2> if 语句
if (FLOAT(theta) > 90) {
theta -= FIXED(180);
signx = -1;
} else if (FLOAT(theta) < -90) {
theta += FIXED(180);
signx = -1;
}
2.3> for语句
for (i = 0; i < nSelectors; i++) {
}
2.4> do-while语句
int strcasecmp(const char *s1, const char *s2)
{
int c1, c2;
do {
c1 = tolower(*s1++);
c2 = tolower(*s2++);
} while (c1 == c2 && c1 != 0);
return c1 - c2;
}
2.5> Switch语句
int strtobool(const char *s, bool *res)
{
switch (s[0]) {
case 'y':
case 'Y':
case '1':
*res = true;
break;
case 'n':
case 'N':
case '0':
*res = false;
break;
default:
return -EINVAL;
}
return 0;
}
2.6> Typedef
/* 结构体中不使用 */
struct rt_mutex_waiter {
struct plist_node list_entry;
struct plist_node pi_list_entry;
struct task_struct *task;
struct rt_mutex *lock;
};
struct rt_mutex_waiter *w;
总结:
<1> 函数
名称: 全小写,采用下划线风格;
功能: 一次只做一件事,短小精悍
局部变量: 不超过5~10个;
<2> 变量
局部变量:全小写,简短;
全局变量:g_xx
静态变量:s_xx
3> 注释
3.1> 函数注释
Linux风格:
/**
* textsearch_find_continuous - search a pattern in continuous/linear data
* @conf: search configuration
* @state: search state
* @data: data to search in
* @len: length of data
*
* A simplified version of textsearch_find() for continuous/linear data.
* Call textsearch_next() to retrieve subsequent matches.
*
* Returns the position of first occurrence of the pattern or
* %UINT_MAX if no occurrence was found.
*/
unsigned int textsearch_find_continuous(struct ts_config *conf,
struct ts_state *state,
const void *data, unsigned int len)
{
struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
conf->get_next_block = get_linear_data;
st->data = data;
st->len = len;
return textsearch_find(conf, state);
}
ST公司风格:
/**
* @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
* @param pData Pointer to data buffer
* @param Size Amount of data to be sent
* @retval HAL status
*/
HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
{
uint32_t xfermode;
if (hi2c->State == HAL_I2C_STATE_READY)
{
if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET)
{
return HAL_BUSY;
}
/* Process Locked */
__HAL_LOCK(hi2c);
hi2c->State = HAL_I2C_STATE_BUSY_RX;
hi2c->Mode = HAL_I2C_MODE_MASTER;
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
/* Prepare transfer parameters */
hi2c->pBuffPtr = pData;
hi2c->XferCount = Size;
hi2c->XferOptions = I2C_NO_OPTION_FRAME;
hi2c->XferISR = I2C_Master_ISR_IT;
if (hi2c->XferCount > MAX_NBYTE_SIZE)
{
hi2c->XferSize = MAX_NBYTE_SIZE;
xfermode = I2C_RELOAD_MODE;
}
else
{
hi2c->XferSize = hi2c->XferCount;
xfermode = I2C_AUTOEND_MODE;
}
/* Send Slave Address */
/* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */
I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ);
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
/* Note : The I2C interrupts must be enabled after unlocking current process
to avoid the risk of I2C interrupt handle execution before current
process unlock */
/* Enable ERR, TC, STOP, NACK, RXI interrupt */
/* possible to enable all of these */
/* I2C_IT_ERRI | I2C_IT_TCI| I2C_IT_STOPI| I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */
I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
3.2> 函数内部注释
int stmp_reset_block(void __iomem *reset_addr)
{
int ret;
int timeout = 0x400;
/* clear and poll SFTRST */
ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
if (unlikely(ret))
goto error;
/* clear CLKGATE */
writel(STMP_MODULE_CLKGATE, reset_addr + STMP_OFFSET_REG_CLR);
/* set SFTRST to reset the block */
writel(STMP_MODULE_SFTRST, reset_addr + STMP_OFFSET_REG_SET);
udelay(1);
/* poll CLKGATE becoming set */
while ((!(readl(reset_addr) & STMP_MODULE_CLKGATE)) && --timeout)
/* nothing */;
if (unlikely(!timeout))
goto error;
/* clear and poll SFTRST */
ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
if (unlikely(ret))
goto error;
/* clear and poll CLKGATE */
ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_CLKGATE);
if (unlikely(ret))
goto error;
return 0;
error:
pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
return -ETIMEDOUT;
}
总结:
不过度注重
4> 常用缩写
arg argumnet
-----------------------
buf buffer
-----------------------
clk clock
cmd command
cmp compare
cfg configuration
-----------------------
dev device
disp display
-----------------------
err error
-----------------------
hex hexadecimal
-----------------------
inc increment
init initialize
-----------------------
max maximum
msg message
min minimum
-----------------------
param parameter
prev previous
reg register
ret return
-----------------------
sem semaphore
stat statistic
sync synchronize
tmp temp
-----------------------
5> 参考
<1> ST标准库;
<2> Linux 内核源码;