Skip to content

Commit 93b17bf

Browse files
authored
Merge pull request #4990 from yangjie11/drv_name_doc
【更新】编码规范
2 parents 6bd2182 + 1bb7ded commit 93b17bf

File tree

1 file changed

+117
-14
lines changed

1 file changed

+117
-14
lines changed

documentation/coding_style_cn.md

Lines changed: 117 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010

1111
文件名称如果无特殊的需求(如果是引用其他地方,可以保留相应的名称),请使用全小写的形式。另外为了避免文件名重名的问题,一些地方请尽量不要使用通用化、使用频率高的名称。
1212

13+
设备驱动源码文件:`drv_class.c` 的命名方式,如:
14+
15+
- drv_spi.c
16+
- drv_gpio.c
17+
1318
## 3.头文件定义
1419

15-
C语言头文件为了避免多次重复包含,需要定义一个符号。这个符号的定义形式请采用如下的风格:
20+
C 语言头文件为了避免多次重复包含,需要定义一个符号。这个符号的定义形式请采用如下的风格:
1621

1722
```c
1823
#ifndef __FILE_H__
@@ -81,14 +86,96 @@ C语言头文件为了避免多次重复包含,需要定义一个符号。这
8186
函数名称请使用小写英文的形式,单词之间使用 "_" 连接。提供给上层应用使用的 API接口,必须在相应的头文件中声明;如果函数入口参数是空,必须使用 void 作为入口参数,例如:
8287

8388
```c
84-
rt_thread_t rt_thread_self(void);
89+
rt_thread_t rt_thread_self(void);
90+
```
91+
92+
内部静态函数命名:以下划线开头,使用 `_class_method` 格式,不携带`_rt_`开头,如内核或驱动文件中的函数命名:
93+
94+
```c
95+
/* IPC object init */
96+
static rt_err_t _ipc_object_init()
97+
98+
/* UART driver ops */
99+
static rt_err_t _uart_configure()
100+
static rt_err_t _uart_control()
101+
```
102+
103+
调用注册设备接口的函数命名:使用 `rt_hw_class_init()` 格式,举例:
104+
105+
```c
106+
int rt_hw_uart_init(void)
107+
int rt_hw_spi_init(void)
85108
```
86109
87110
## 8.注释编写
88111
89-
请使用英文做为注释,使用中文注释将意味着在编写代码时需要来回不停的切换中英文输入法从而打断编写代码的思路。并且使用英文注释也能够比较好的与中国以外的技术者进行交流。
112+
请使用**英文**做为注释,使用中文注释将意味着在编写代码时需要来回不停的切换中英文输入法从而打断编写代码的思路。并且使用英文注释也能够比较好的与中国以外的技术者进行交流。
113+
114+
**语句注释**:
115+
116+
源代码的注释不应该过多,更多的说明应该是代码做了什么,仅当个别关键点才需要一些相应提示性的注释以解释一段复杂的算法它是如何工作的。对语句的注释只能写在它的**上方或右方**,其他位置都是非法的。
117+
118+
```c
119+
/* 你的英文注释 */
120+
```
121+
122+
**函数注释**
123+
124+
注释以 `/**` 开头,以 ` */` 结尾,中间写入函数注释,组成元素如下,每个元素描述之间空一行,且首列对齐:
125+
126+
- @brief + 简述函数作用。在描述中,着重说明该函数的作用,每句话首字母大写,句尾加英文句号。
127+
- @note + 函数说明。在上述简述中未能体现到的函数功能或作用的一些点,可以做解释说明,每句话首字母大写,句尾加英文句号。
128+
- @see + 相关 API 罗列。若有与当前函数相关度较高的 API,可以进行列举。
129+
- @param + 以参数为主语 + be 动词 + 描述,说明参数的意义或来源。
130+
- @return + 枚举返回值 + 返回值的意思,若返回值为数据,则直接介绍数据的功能。
131+
- @warning + 函数使用注意要点。在函数使用时,描述需要注意的事项,如使用环境、使用方式等。每句话首字母大写,句尾加英文句号。
132+
133+
注释模版请参见:rt-thread/src/ipc.c 源码文件,英文注释请参考使用 grammarly 以及谷歌翻译。
90134

91-
源代码的注释不应该过多,更多的说明应该是代码做了什么,仅当个别关键点才需要一些相应提示性的注释以解释一段复杂的算法它是如何工作的。对语句的注释只能写在它的上方或右方,其他位置都是非法的。
135+
```C
136+
/**
137+
* @brief The function will initialize a static event object.
138+
*
139+
* @note For the static event object, its memory space is allocated by the compiler during compiling,
140+
* and shall placed on the read-write data segment or on the uninitialized data segment.
141+
* By contrast, the rt_event_create() function will allocate memory space automatically
142+
* and initialize the event.
143+
*
144+
* @see rt_event_create()
145+
*
146+
* @param event is a pointer to the event to initialize. It is assumed that storage for the event
147+
* will be allocated in your application.
148+
*
149+
* @param name is a pointer to the name that given to the event.
150+
*
151+
* @param value is the initial value for the event.
152+
* If want to share resources, you should initialize the value as the number of available resources.
153+
* If want to signal the occurrence of an event, you should initialize the value as 0.
154+
*
155+
* @param flag is the event flag, which determines the queuing way of how multiple threads wait
156+
* when the event is not available.
157+
* The event flag can be ONE of the following values:
158+
*
159+
* RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
160+
*
161+
* RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
162+
* (also known as first-come-first-served (FCFS) scheduling strategy).
163+
*
164+
* NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
165+
* use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
166+
* the first-in-first-out principle, and you clearly understand that all threads involved in
167+
* this event will become non-real-time threads.
168+
*
169+
* @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
170+
* If the return value is any other values, it represents the initialization failed.
171+
*
172+
* @warning This function can ONLY be called from threads.
173+
*/
174+
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
175+
{
176+
...
177+
}
178+
```
92179
93180
## 9.缩进及分行
94181
@@ -101,8 +188,7 @@ C语言头文件为了避免多次重复包含,需要定义一个符号。这
101188
}
102189
```
103190

104-
唯一的例外是 switch 语句,switch-case 语句采用 case 语句与 switch 对齐的方式,
105-
例如:
191+
唯一的例外是 switch 语句,switch-case 语句采用 case 语句与 switch 对齐的方式,例如:
106192

107193
```c
108194
switch (value)
@@ -178,21 +264,28 @@ RT-Thread 内核采用了 C 语言对象化技术,命名表现形式是:对
178264
结构体定义 rt_timer 代表了 timer 对象的类定义;
179265
180266
```c
181-
rt_timer_t rt_timer_create(const char* name,
182-
void (*timeout)(void* parameter), void* parameter,
183-
rt_tick_t time, rt_uint8_t flag);
184-
rt_err_t rt_timer_delete(rt_timer_t timer);
185-
rt_err_t rt_timer_start(rt_timer_t timer);
186-
rt_err_t rt_timer_stop(rt_timer_t timer);
267+
rt_timer_t rt_timer_create(const char* name,
268+
void (*timeout)(void* parameter),
269+
void* parameter,
270+
rt_tick_t time, rt_uint8_t flag);
271+
rt_err_t rt_timer_delete(rt_timer_t timer);
272+
rt_err_t rt_timer_start(rt_timer_t timer);
273+
rt_err_t rt_timer_stop(rt_timer_t timer);
187274
```
188275

189276
rt_timer + 动词短语的形式表示能够应用于 timer 对象的方法。
190277

191278
在创建一个新的对象时,应该思考好,对象的内存操作处理:是否允许一个静态对象存在,或仅仅支持从堆中动态分配的对象。
192279

193-
## 14. 用 astyle 自动格式化代码
280+
## 14.格式化代码
281+
282+
格式化代码是指通过脚本自动整理你的代码,并使其符合 RT-Thread 的编码规范。本文提供以下两种自动格式化代码方法,可以自行选择或配合使用。
283+
284+
### 使用 astyle 格式化
194285

195-
参数:--style=allman
286+
用 astyle 自动格式化代码,参数如下:
287+
288+
--style=allman
196289
--indent=spaces=4
197290
--indent-preproc-block
198291
--pad-oper
@@ -203,3 +296,13 @@ rt_timer + 动词短语的形式表示能够应用于 timer 对象的方法。
203296
--lineend=linux
204297
--convert-tabs
205298
--verbose
299+
300+
能满足函数空格、缩进、函数语句等的规范。
301+
302+
### 使用 formatting 格式化
303+
304+
使用 [formatting](https://github.com/mysterywolf/formatting) 扫描文件来格式化代码:formatting 可以满足编码规则的基本要求,如:
305+
306+
- 将源文件编码统一为 UTF-8
307+
- 将 TAB 键替换为 4 空格
308+
- 将每行末尾多余的空格删除,并统一换行符为 '\n'

0 commit comments

Comments
 (0)