作者:架構師李肯
前言
RT-Thread 編程風格
1.目錄名稱
2.文件名稱
3.頭文件定義
C 語言頭文件為了避免多次重復包含,需要定義一個符號。這個符號的定義形式請采用如下的風格:
1#ifndef__FILE_H__
2#define__FILE_H__
3/*headerfilecontent*/
4#endif
即定義的符號兩側采用 “__” 以避免重名,另外也可以根據文件名中是否包含多個詞語而采用 “_” 連接起來。
4.文件頭注釋
在每個源文件文件頭上,應該包括相應的版權信息,Change Log 記錄:
1/*
2*Copyright(c)2006-2020,RT-ThreadDevelopmentTeam
3*
4*SPDX-License-Identifier:Apache-2.0
5*
6*ChangeLogs:
7*DateAuthorNotes
8*2006-03-18Bernardthefirstversion
9*2006-04-26BernardaddsemaphoreAPIs
10*/
5.結構體定義
結構體名稱請使用小寫英文名的形式,單詞與單詞之間采用 “_” 連接,例如:
1structrt_list_node
2{
3structrt_list_node*next;
4structrt_list_node*prev;
5};
1typedefstructrt_list_nodert_list_t;
1typedefstructrt_timer*rt_timer_t;
6.宏定義
1#defineRT_TRUE1
7.函數名稱、聲明
1rt_thread_trt_thread_self(void);
1/*IPCobjectinit*/
2staticrt_err_t_ipc_object_init()
3
4/*UARTdriverops*/
5staticrt_err_t_uart_configure()
6staticrt_err_t_uart_control()
1intrt_hw_uart_init(void)
2intrt_hw_spi_init(void)
8.注釋編寫
1/*你的英文注釋*/
注釋模版請參見:rt-thread/src/ipc.c 源碼文件,英文注釋請參考使用 grammarly 以及谷歌翻譯。
1/**
2*@briefThefunctionwillinitializeastaticeventobject.
3*
4*@noteForthestaticeventobject,itsmemoryspaceisallocatedbythecompilerduringcompiling,
5*andshallplacedontheread-writedatasegmentorontheuninitializeddatasegment.
6*Bycontrast,thert_event_create()functionwillallocatememoryspaceautomatically
7*andinitializetheevent.
8*
9*@seert_event_create()
10*
11*@parameventisapointertotheeventtoinitialize.Itisassumedthatstoragefortheevent
12*willbeallocatedinyourapplication.
13*
14*@paramnameisapointertothenamethatgiventotheevent.
15*
16*@paramvalueistheinitialvaluefortheevent.
17*Ifwanttoshareresources,youshouldinitializethevalueasthenumberofavailableresources.
18*Ifwanttosignaltheoccurrenceofanevent,youshouldinitializethevalueas0.
19*
20*@paramflagistheeventflag,whichdeterminesthequeuingwayofhowmultiplethreadswait
21*whentheeventisnotavailable.
22*TheeventflagcanbeONEofthefollowingvalues:
23*
24*RT_IPC_FLAG_PRIOThependingthreadswillqueueinorderofpriority.
25*
26*RT_IPC_FLAG_FIFOThependingthreadswillqueueinthefirst-in-first-outmethod
27*(alsoknownasfirst-come-first-served(FCFS)schedulingstrategy).
28*
29*NOTE:RT_IPC_FLAG_FIFOisanon-real-timeschedulingmode.Itisstronglyrecommendedto
30*useRT_IPC_FLAG_PRIOtoensurethethreadisreal-timeUNLESSyourapplicationsconcernabout
31*thefirst-in-first-outprinciple,andyouclearlyunderstandthatallthreadsinvolvedin
32*thiseventwillbecomenon-real-timethreads.
33*
34*@returnReturntheoperationstatus.WhenthereturnvalueisRT_EOK,theinitializationissuccessful.
35*Ifthereturnvalueisanyothervalues,itrepresentstheinitializationfailed.
36*
37*@warningThisfunctioncanONLYbecalledfromthreads.
38*/
39rt_err_trt_event_init(rt_event_tevent,constchar*name,rt_uint8_tflag)
40{
41...
42}
9.縮進及分行
1if(condition)
2{
3/*others*/
4}
1switch(value)
2{
3casevalue1:
4break;
5}
10.大括號與空格
1if(condition)
2{
3/*others*/
4}
1if(x<=?y)
2{
3/*others*/
4}
5
6for(index=0;index7{
8/*others*/
9}
1if(x<=?y?)
2{
3/*other*/
4}
11.trace、log信息
12.函數
13.對象
1structrt_timer
2{
3structrt_objectparent;
4/*otherfields*/
5};
6typedefstructrt_timer*rt_timer_t;
1rt_timer_trt_timer_create(constchar*name,
2void(*timeout)(void*parameter),
3void*parameter,
4rt_tick_ttime,rt_uint8_tflag);
5rt_err_trt_timer_delete(rt_timer_ttimer);
6rt_err_trt_timer_start(rt_timer_ttimer);
7rt_err_trt_timer_stop(rt_timer_ttimer);
14.格式化代碼
使用 astyle 格式化
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
使用 formatting 格式化
將源文件編碼統一為 UTF-8
將 TAB 鍵替換為 4 空格
將每行末尾多余的空格刪除,并統一換行符為 ‘ ’
RT-Thread開發者大會
我們將聯合重量級合作伙伴,圍繞AIoT的發展、產業技術趨勢,聚焦控制、連接、行業應用開發,通過主題演講、技術分享、應用演示等環節,助力開發者探索萬物智能的世界,期待與大家一起相聚線上直播間!
本次將在大會當天在直播間宣布中獎名單
更多獎品即將來襲...
你可以添加微信17775982065為好友,注明:公司+姓名,拉進RT-Thread官方微信交流群!
愛我就給我點在看
點擊閱讀原文進入報名
-
RT-Thread
+關注
關注
32文章
1376瀏覽量
41609
原文標題:RT-Thread 編程風格
文章出處:【微信號:RTThread,微信公眾號:RTThread物聯網操作系統】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
2025 RT-Thread全球技術大會議程正式發布!

【直播預告】《實時操作系統應用技術—基于RT-Thread與ARM的編程實踐》教學脈絡及資源簡介

RT-Thread審核團招募: 深度參與開源RTOS社區治理與演進

如何將RT-Thread移植到NXP MCUXPressoIDE上

從開源中來到開源中去-RT-Thread北京社區篇

RT-Thread上CAN實踐

開源共生 商業共贏 | RT-Thread 2024開發者大會報名啟動!

【成都】9月21日RT-Thread巡回線下培訓-OpenMV機器視覺

【大連】9月7日RT-Thread巡回線下培訓-OpenMV機器視覺

瑞薩電子全球VP蒞臨RT-Thread指導交流

【QEMU系列】不用開發板運行RT-Thread指南-ARM架構

2024 RT-Thread全球巡回 線下培訓火熱來襲!

【好書推薦】RT-Thread設備驅動開發指南

RT-Thread 新里程碑達成——GitHub Star 破萬!

評論