關注+星標公眾號,不錯過精彩內容
作者:架構師李肯
來源 | RTThread
一個優秀的項目,必定有優秀的“基因”,而編碼規范就是其中之一。
之前給大家分享過很多關于編碼規范的內容,今年分享一下RT-Thread的編碼規范。
C 語言頭文件為了避免多次重復包含,需要定義一個符號。這個符號的定義形式請采用如下的風格:
1????#ifndef?__FILE_H__
2????#define?__FILE_H__
3????/*?header?file?content?*/
4????#endif
即定義的符號兩側采用 “__” 以避免重名,另外也可以根據文件名中是否包含多個詞語而采用 “_” 連接起來。
在每個源文件文件頭上,應該包括相應的版權信息,Change Log 記錄:
1/*
2?*?Copyright?(c)?2006-2020,?RT-Thread?Development?Team
3?*
4?*?SPDX-License-Identifier:?Apache-2.0
5?*
6?*?Change?Logs:
7?*?Date???????????Author???????Notes
8?*?2006-03-18?????Bernard??????the?first?version
9?*?2006-04-26?????Bernard??????add?semaphore?APIs
10?*/結構體名稱請使用小寫英文名的形式,單詞與單詞之間采用 “_” 連接,例如:
1????struct?rt_list_node
2????{
3????????struct?rt_list_node?*next;
4????????struct?rt_list_node?*prev;
5????};1????typedef?struct?rt_list_node?rt_list_t;1????typedef?struct?rt_timer*?rt_timer_t;
1????#define?RT_TRUE?????????????????????????11rt_thread_t?rt_thread_self(void);
1/*?IPC?object?init?*/
2static?rt_err_t?_ipc_object_init()
3
4/*?UART?driver?ops?*/
5static?rt_err_t?_uart_configure()
6static?rt_err_t?_uart_control()????????????????????1int?rt_hw_uart_init(void)
2int?rt_hw_spi_init(void)1/*?你的英文注釋?*/注釋模版請參見:rt-thread/src/ipc.c 源碼文件,英文注釋請參考使用 grammarly 以及谷歌翻譯。
1/**
2?*?@brief????The?function?will?initialize?a?static?event?object.
3?*
4?*?@note?????For?the?static?event?object,?its?memory?space?is?allocated?by?the?compiler?during?compiling,
5?*???????????and?shall?placed?on?the?read-write?data?segment?or?on?the?uninitialized?data?segment.
6?*???????????By?contrast,?the?rt_event_create()?function?will?allocate?memory?space?automatically
7?*???????????and?initialize?the?event.
8?*
9?*?@see??????rt_event_create()
10?*
11?*?@param????event?is?a?pointer?to?the?event?to?initialize.?It?is?assumed?that?storage?for?the?event
12?*???????????will?be?allocated?in?your?application.
13?*
14?*?@param????name?is?a?pointer?to?the?name?that?given?to?the?event.
15?*
16?*?@param????value?is?the?initial?value?for?the?event.
17?*???????????If?want?to?share?resources,?you?should?initialize?the?value?as?the?number?of?available?resources.
18?*???????????If?want?to?signal?the?occurrence?of?an?event,?you?should?initialize?the?value?as?0.
19?*
20?*?@param????flag?is?the?event?flag,?which?determines?the?queuing?way?of?how?multiple?threads?wait
21?*???????????when?the?event?is?not?available.
22?*???????????The?event?flag?can?be?ONE?of?the?following?values:
23?*
24?*???????????????RT_IPC_FLAG_PRIO??????????The?pending?threads?will?queue?in?order?of?priority.
25?*
26?*???????????????RT_IPC_FLAG_FIFO??????????The?pending?threads?will?queue?in?the?first-in-first-out?method
27?*?????????????????????????????????????????(also?known?as?first-come-first-served?(FCFS)?scheduling?strategy).
28?*
29?*???????????????NOTE:?RT_IPC_FLAG_FIFO?is?a?non-real-time?scheduling?mode.?It?is?strongly?recommended?to
30?*???????????????use?RT_IPC_FLAG_PRIO?to?ensure?the?thread?is?real-time?UNLESS?your?applications?concern?about
31?*???????????????the?first-in-first-out?principle,?and?you?clearly?understand?that?all?threads?involved?in
32?*???????????????this?event?will?become?non-real-time?threads.
33?*
34?*?@return???Return?the?operation?status.?When?the?return?value?is?RT_EOK,?the?initialization?is?successful.
35?*???????????If?the?return?value?is?any?other?values,?it?represents?the?initialization?failed.
36?*
37?*?@warning??This?function?can?ONLY?be?called?from?threads.
38?*/
39rt_err_t?rt_event_init(rt_event_t?event,?const?char?*name,?rt_uint8_t?flag)
40{
41???...?
42}
1????if?(condition)
2????{
3????????/*?others?*/
4????}1????switch?(value)
2????{
3????case?value1:
4????????break;
5????}1????if?(condition)
2????{
3????????/*?others?*/
4????}1????if?(x?<=?y)
2????{
3????????/*?others?*/
4????}
5
6????for?(index?=?0;?index?7????{
8????????/*?others?*/
9????}1????if?(?x?<=?y?)
2????{
3????????/*?other?*/
4????}
1????struct?rt_timer
2????{
3????????struct?rt_object?parent;
4????????/*?other?fields?*/
5????};
6????typedef?struct?rt_timer*?rt_timer_t;1rt_timer_t?rt_timer_create(const?char*?name,
2???????????????????????????void?(*timeout)(void*?parameter),?
3???????????????????????????void*?parameter,
4???????????????????????????rt_tick_t?time,?rt_uint8_t?flag);
5rt_err_t?rt_timer_delete(rt_timer_t?timer);
6rt_err_t?rt_timer_start(rt_timer_t?timer);
7rt_err_t?rt_timer_stop(rt_timer_t?timer); 1??????--style=allman
2??????--indent=spaces=4
3??????--indent-preproc-block
4??????--pad-oper
5??????--pad-header
6??????--unpad-paren
7??????--suffix=none
8??????--align-pointer=name
9??????--lineend=linux
10??????--convert-tabs
11??????--verbose將源文件編碼統一為 UTF-8
將 TAB 鍵替換為 4 空格
將每行末尾多余的空格刪除,并統一換行符為 ‘\n’
------------?END?------------

●專欄《嵌入式工具》
●專欄《嵌入式開發》
●專欄《Keil教程》
●嵌入式專欄精選教程
關注公眾號回復“加群”按規則加入技術交流群,回復“1024”查看更多內容。


點擊“閱讀原文”查看更多分享。