|
10 | 10 |
|
11 | 11 | 文件名称如果无特殊的需求(如果是引用其他地方,可以保留相应的名称),请使用全小写的形式。另外为了避免文件名重名的问题,一些地方请尽量不要使用通用化、使用频率高的名称。 |
12 | 12 |
|
| 13 | +设备驱动源码文件:`drv_class.c` 的命名方式,如: |
| 14 | + |
| 15 | +- drv_spi.c |
| 16 | +- drv_gpio.c |
| 17 | + |
13 | 18 | ## 3.头文件定义 |
14 | 19 |
|
15 | | -C语言头文件为了避免多次重复包含,需要定义一个符号。这个符号的定义形式请采用如下的风格: |
| 20 | +C 语言头文件为了避免多次重复包含,需要定义一个符号。这个符号的定义形式请采用如下的风格: |
16 | 21 |
|
17 | 22 | ```c |
18 | 23 | #ifndef __FILE_H__ |
@@ -81,14 +86,67 @@ C语言头文件为了避免多次重复包含,需要定义一个符号。这 |
81 | 86 | 函数名称请使用小写英文的形式,单词之间使用 "_" 连接。提供给上层应用使用的 API接口,必须在相应的头文件中声明;如果函数入口参数是空,必须使用 void 作为入口参数,例如: |
82 | 87 |
|
83 | 88 | ```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) |
85 | 108 | ``` |
86 | 109 |
|
87 | 110 | ## 8.注释编写 |
88 | 111 |
|
89 | | -请使用英文做为注释,使用中文注释将意味着在编写代码时需要来回不停的切换中英文输入法从而打断编写代码的思路。并且使用英文注释也能够比较好的与中国以外的技术者进行交流。 |
| 112 | +请使用**英文**做为注释,使用中文注释将意味着在编写代码时需要来回不停的切换中英文输入法从而打断编写代码的思路。并且使用英文注释也能够比较好的与中国以外的技术者进行交流。 |
90 | 113 |
|
91 | | -源代码的注释不应该过多,更多的说明应该是代码做了什么,仅当个别关键点才需要一些相应提示性的注释以解释一段复杂的算法它是如何工作的。对语句的注释只能写在它的上方或右方,其他位置都是非法的。 |
| 114 | +**语句注释**: |
| 115 | +
|
| 116 | +源代码的注释不应该过多,更多的说明应该是代码做了什么,仅当个别关键点才需要一些相应提示性的注释以解释一段复杂的算法它是如何工作的。对语句的注释只能写在它的**上方或右方**,其他位置都是非法的。 |
| 117 | +
|
| 118 | +```c |
| 119 | +/* 你的英文注释 */ |
| 120 | +``` |
| 121 | + |
| 122 | +**函数注释**: |
| 123 | + |
| 124 | +- 以 `/**` 开头,以 ` */` 结尾,中间写入函数注释 |
| 125 | +- 第一部分:一个段落介绍函数的作用 |
| 126 | +- 第二部分:参数采用 @param + 参数 + 介绍参数 的方式 |
| 127 | +- 第三部分:返回采用 @return + 返回值 + 介绍返回值 的方式 |
| 128 | +- 以上每个部分之间空一行 |
| 129 | + |
| 130 | +```C |
| 131 | +/** |
| 132 | + * This function will initialize a semaphore and put it under control of |
| 133 | + * resource management. |
| 134 | + * |
| 135 | + * @param sem the semaphore object |
| 136 | + * @param name the name of semaphore |
| 137 | + * @param value the initial value of semaphore |
| 138 | + * @param flag the flag of semaphore |
| 139 | + * |
| 140 | + * @return the operation status, RT_EOK on successful |
| 141 | + */ |
| 142 | +rt_err_t rt_sem_init(rt_sem_t sem, |
| 143 | + const char *name, |
| 144 | + rt_uint32_t value, |
| 145 | + rt_uint8_t flag) |
| 146 | +{ |
| 147 | + .... |
| 148 | +} |
| 149 | +``` |
92 | 150 |
|
93 | 151 | ## 9.缩进及分行 |
94 | 152 |
|
@@ -178,19 +236,20 @@ RT-Thread 内核采用了 C 语言对象化技术,命名表现形式是:对 |
178 | 236 | 结构体定义 rt_timer 代表了 timer 对象的类定义; |
179 | 237 |
|
180 | 238 | ```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); |
| 239 | +rt_timer_t rt_timer_create(const char* name, |
| 240 | + void (*timeout)(void* parameter), |
| 241 | + void* parameter, |
| 242 | + rt_tick_t time, rt_uint8_t flag); |
| 243 | +rt_err_t rt_timer_delete(rt_timer_t timer); |
| 244 | +rt_err_t rt_timer_start(rt_timer_t timer); |
| 245 | +rt_err_t rt_timer_stop(rt_timer_t timer); |
187 | 246 | ``` |
188 | 247 |
|
189 | 248 | rt_timer + 动词短语的形式表示能够应用于 timer 对象的方法。 |
190 | 249 |
|
191 | 250 | 在创建一个新的对象时,应该思考好,对象的内存操作处理:是否允许一个静态对象存在,或仅仅支持从堆中动态分配的对象。 |
192 | 251 |
|
193 | | -## 14. 用 astyle 自动格式化代码 |
| 252 | +## 14.用 astyle 自动格式化代码 |
194 | 253 |
|
195 | 254 | 参数:--style=allman |
196 | 255 | --indent=spaces=4 |
|
0 commit comments