[Guide] Some friends feel that the FreeRTOS kernel code looks very uncomfortable and unaccustomed to its coding style. This article will sort out the code specifications to improve the efficiency of reading the code. The code is based on FreeRTOS V10.4.3.
FreeRTOS code structure
The kernel code files are just these, very concise:
croutine.c/croutine.h: Coroutine, it is more efficient on 8-bit/16-bit platform, it is recommended to use task on 32-bit platform
event_groups.c / event_groups.h: As the name suggests, this is the implementation of event groups
heap_x.c: kernel heap implementation, FreeRTOS provides 5 heap managers from heap_1.c to heap_5.c, each with its own advantages and disadvantages, and needs to be selected according to the application.
list.c/list.h: Linked list implementation, mainly to provide data structure algorithm support services for the scheduler. For example, the task list.
port.c/portmacro.h: Hardware-related levels of portable abstraction, including SysTick interrupt, context switching, and interrupt management. The specific implementation largely depends on the platform (the microcontroller system hardware kernel and compiler tool set). Usually implemented in assembly language.
queue.c/queue.h/semphr.h: semaphore, mutex implementation
tasks.c/task.h: task manager implementation
timers.c/timers.h: software timer implementation
FreeRTOS.h: Select the compilation configuration file, used to summarize the compilation selection control of all source files
FreeRTOSConfig.h: FreeRTOS kernel configuration, Tick clock and irq interrupt configuration
Next, I will sort out the relevant code specifications, which are specifically reflected in the encoding of the above-mentioned files.
variable
Variables have strict prefixes to identify variable type attributes:
c-char character variable
s-short short variable
l-long variable
x-portBASE_TYPE is defined in portmacro.h, a data type that is easy to transplant
u-unsigned unsigned integer
p-pointer
For example:
//x表示portBASE_TYPE, u 表示无符号型
PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
//比如在list.h 中
struct xLIST_ITEM
{
configLIST_VOLATILE TickType_t xItemValue;
//指针以p打头
struct xLIST_ITEM * configLIST_VOLATILE pxNext;
struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;
void * pvOwner;
struct xLIST * configLIST_VOLATILE pxContainer;
};
For the basic data types of the C language, a portable transfer definition is made:
#define portCHAR char
#define portFLOAT float
#define portDOUBLE double
#define portLONG long
#define portSHORT short
#define portSTACK_TYPE uint32_t
#define portBASE_TYPE long
function
Prefix:
v: void no return type
x: return portBASE_TYPE
prv: private function, used in the module
//ux 表示无符号portBASE_TYPE 返回值
//List表示该函数所属文件
//Remove函数名
UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove ) PRIVILEGED_FUNCTION;
//又比如prv 表示模块内函数
static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
Macro
Define the file to which the macro belongs, that is, in which file it is defined:
port: such as portMAX_DELAY in portable.h
task: For example, task_ENTER_CRITICAL in task.h
pd: For example, pdTRUE defined in projdefs.h
config: For example, configUSE_PREEMPTION defined in FreeRTOSConfig.h
err: For example, errQUEUE_FULL defined in projdefs.h
As for whether such a strict code specification is worthy of praise, people have different opinions. I personally prefer the Linux code style. For overly complex code specifications, I feel that sometimes it is uncomfortable in actual development.
1. The GD32 Arm MCU Internet of Things developer online course is coming, soon join the group and wait for the class to start!
2. Expert opinion: Embedded and IoT industry trends in 2021
3. Why is everyone optimistic about RISC-V?
4. Three program architectures to be used in embedded development~
5. Ten major technology trends of Ali Dharma Academy in 2021~
6. FPGA is difficult to understand? Actually compare with GPU to understand
Disclaimer: This article is reproduced online, and the copyright belongs to the original author. If you are involved in copyright issues, please contact us, we will confirm the copyright based on the copyright certification materials you provide and pay the author's remuneration or delete the content.