freertos学习笔记一之基础介绍
22.02.10
1,freertos编码规则
FreeRTOS 核心源码文件的编写遵循 MISRA 代码规则
不支持 C 语言的新标准 C99 和 C11 的一些特性和语法,但是源码中有用到头文件 stdint.h(这个文件是 C99 标准才 引入的)
MISRA (The Motor Industry Software Reliability Association 汽车工业软件可靠性联会) 是位于英国的一个跨国汽车工业协会
这个组织最出名的成果是所谓的 MISRA C
Coding Standard,这一标准中包括了 127 条 C 语言编码标准,通常认为,如果能够完全遵守这些标
准,那你的 C 代码是易读、可靠、可移植和易于维护的。现在很多嵌入式开发者都以 MISRA C 来衡量
自己的编码风格,比如 uC/OS-II 就宣传 99%遵守 MISRA 标准
2,FreeRTOS 的命名规则
变量
- uint32_t 定义的变量都加上前缀 ul。u 代表 unsigned 无符号,l 代表 long 长整型。
- uint16_t 定义的变量都加上前缀 us。u 代表 unsigned 无符号,s 代表 short 短整型。
- uint8_t 定义的变量都加上前缀 uc。u 代表 unsigned 无符号,c 代表 char 字符型。
- stdint.h 文件中未定义的变量类型,在定义变量时需要加上前缀 x,比如 BaseType_t 和 TickType_t 定义的变量。
- stdint.h 文件中未定义的无符号变量类型,在定义变量时要加上前缀 u,比如 UBaseType_t 定义 的变量要加上前缀 ux。
- size_t 定义的变量也要加上前缀 ux。
- 枚举变量会加上前缀 e。
- 指针变量会加上前缀 p,比如 uint16_t 定义的指针变量会加上前缀 pus。
- 根据 MISRA 代码规则,char 定义的变量只能用于 ASCII 字符,前缀使用 c。
- 根据 MISRA 代码规则,char *定义的指针变量只能用于 ASCII 字符串,前缀使用 pc。
函数
- 加上了 static 声明的函数,定义时要加上前缀 prv,这个是单词 private 的缩写。
- 带有返回值的函数,根据返回值的数据类型,加上相应的前缀,如果没有返回值,即 void 类型 ,函数的前缀加上字母 v。
- 根据文件名,文件中相应的函数定义时也将文件名加到函数命名中,比如 tasks.c 文件中函数 vTaskDelete,函数中的 task 就是文件名中的 task。
宏定义
- 根据宏定义所在的文件,文件中的宏定义声明时也将文件名加到宏定义中,比如宏定义 configUSE_PREEMPTION 是定义在文件 FreeRTOSConfig.h 里面。宏定义中的 config 就是文 件名中的 config。另外注意,前缀要小写。
- 除了前缀,其余部分全部大写,同时用下划线分开。
- char 型变量无符号数和有符号数的切换方法
3,FreeRTOS 中数据类型
FreeRTOS 使用的数据类型主要分为 stdint.h 文件中定义的和自己定义的两种。
四种类型:
- TickType_t
如果用户使能了宏定义 configUSE_16_BIT_TICKS,那么 TickType_t 定义的就是 16 位无符号数,如 果没有使能,那么 TickType_t 定义的就是 32 位无符号数。对于 32 位架构的处理器,一定要禁止此 宏定义,即设置此宏定义数值为 0 即可。
- BaseType_t
对于 32 位架构,BaseType_t 定义的是 32 位有符号数,对 于 16 位架构,BaseType_t 定义的是 16 位有符号数。如果是char,则应该设置有符合型。因为有函数返回值是用负数表错误类型。
- UBaseType_t
是 BaseType_t 类型的无符号版本。
- StackType_t
栈变量数据类型定义,这个数量类型由系统架构决定,对于 16 位系统架构,StackType_t 定义的是 16 位变量,对于 32 位系统架构,StackType_t 定义的是 32 位变量。
4,配置信息config
本条信息源自https://www.freertos.org/a00110.html
`#ifndef FREERTOS_CONFIG_H #define FREERTOS_CONFIG_H /* Here is a good place to include header files that are required across your application. */ #include "something.h" #define configUSE_PREEMPTION 1 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 #define configUSE_TICKLESS_IDLE 0 #define configCPU_CLOCK_HZ 60000000 #define configSYSTICK_CLOCK_HZ 1000000 #define configTICK_RATE_HZ 250 #define configMAX_PRIORITIES 5 #define configMINIMAL_STACK_SIZE 128 #define configMAX_TASK_NAME_LEN 16 #define configUSE_16_BIT_TICKS 0 #define configIDLE_SHOULD_YIELD 1 #define configUSE_TASK_NOTIFICATIONS 1 #define configTASK_NOTIFICATION_ARRAY_ENTRIES 3 #define configUSE_MUTEXES 0 #define configUSE_RECURSIVE_MUTEXES 0 #define configUSE_COUNTING_SEMAPHORES 0 #define configUSE_ALTERNATIVE_API 0 /* Deprecated! */ #define configQUEUE_REGISTRY_SIZE 10 #define configUSE_QUEUE_SETS 0 #define configUSE_TIME_SLICING 0 #define configUSE_NEWLIB_REENTRANT 0 #define configENABLE_BACKWARD_COMPATIBILITY 0 #define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 #define configSTACK_DEPTH_TYPE uint16_t #define configMESSAGE_BUFFER_LENGTH_TYPE size_t /* Memory allocation related definitions. */ #define configSUPPORT_STATIC_ALLOCATION 1 #define configSUPPORT_DYNAMIC_ALLOCATION 1 #define configTOTAL_HEAP_SIZE 10240 #define configAPPLICATION_ALLOCATED_HEAP 1 #define configSTACK_ALLOCATION_FROM_SEPARATE_HEAP 1 /* Hook function related definitions. */ #define configUSE_IDLE_HOOK 0 #define configUSE_TICK_HOOK 0 #define configCHECK_FOR_STACK_OVERFLOW 0 #define configUSE_MALLOC_FAILED_HOOK 0 #define configUSE_DAEMON_TASK_STARTUP_HOOK 0 /* Run time and task stats gathering related definitions. */ #define configGENERATE_RUN_TIME_STATS 0 #define configUSE_TRACE_FACILITY 0 #define configUSE_STATS_FORMATTING_FUNCTIONS 0 /* Co-routine related definitions. */ #define configUSE_CO_ROUTINES 0 #define configMAX_CO_ROUTINE_PRIORITIES 1 /* Software timer related definitions. */ #define configUSE_TIMERS 1 #define configTIMER_TASK_PRIORITY 3 #define configTIMER_QUEUE_LENGTH 10 #define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE /* Interrupt nesting behaviour configuration. */ #define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] #define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] #define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] /* Define to trap errors during development. */ #define configASSERT( ( x ) ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ ) /* FreeRTOS MPU specific definitions. */ #define configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS 0 #define configTOTAL_MPU_REGIONS 8 /* Default value. */ #define configTEX_S_C_B_FLASH 0x07UL /* Default value. */ #define configTEX_S_C_B_SRAM 0x07UL /* Default value. */ #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY 1 #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1 /* ARMv8-M secure side port related definitions. */ #define secureconfigMAX_SECURE_CONTEXTS 5 /* Optional functions - most linkers will remove unused functions anyway. */ #define INCLUDE_vTaskPrioritySet 1 #define INCLUDE_uxTaskPriorityGet 1 #define INCLUDE_vTaskDelete 1 #define INCLUDE_vTaskSuspend 1 #define INCLUDE_xResumeFromISR 1 #define INCLUDE_vTaskDelayUntil 1 #define INCLUDE_vTaskDelay 1 #define INCLUDE_xTaskGetSchedulerState 1 #define INCLUDE_xTaskGetCurrentTaskHandle 1 #define INCLUDE_uxTaskGetStackHighWaterMark 0 #define INCLUDE_xTaskGetIdleTaskHandle 0 #define INCLUDE_eTaskGetState 0 #define INCLUDE_xEventGroupSetBitFromISR 1 #define INCLUDE_xTimerPendFunctionCall 0 #define INCLUDE_xTaskAbortDelay 0 #define INCLUDE_xTaskGetHandle 0 #define INCLUDE_xTaskResumeFromISR 1 /* A header file that defines trace macro can be included here. */ #endif /* FREERTOS_CONFIG_H */`
5,freertos调试方法
任务栈的使用情况以及各个任务的 CPU 使用率:
vTaskList((char *)&pcWriteBuffer);
vTaskGetRunTimeStats((char *)&pcWriteBuffer);
`static void vTaskTaskUserIF(void *pvParameters) { uint8_t pcWriteBuffer[500]; while(1) { if(KEY1_StateRead()==KEY_DOWN) { printf("=================================================\r\n"); printf("任务名 任务状态 优先级 剩余栈 任务序号\r\n"); vTaskList((char *)&pcWriteBuffer); printf("%s\r\n", pcWriteBuffer); printf("\r\n任务名 运行计数 使用率\r\n"); vTaskGetRunTimeStats((char *)&pcWriteBuffer); printf("%s\r\n", pcWriteBuffer); } vTaskDelay(20); } } ================================================= 任务名 任务状态 优先级 剩余栈 任务序号 vTaskUserIF R 1 320 1 IDLE R 0 114 5 vTaskLED1 B 2 490 2 vTaskLED3 B 4 490 4 vTaskLED2 B 3 490 3 任务名 运行计数 使用率 vTaskUserIF 23752 2% IDLE 911753 97% vTaskLED1 106 <1% vTaskLED3 11 <1% vTaskLED2 110 <1%`
任务的剩余栈的单位是 word,即 4 字节(Byte)。1Byte=8bit
6,freertos任务管理
是让初学者从裸机的,单任务编程过渡到带 OS 的,多任务编程上来,搞清楚了这一点, 那么 FreeRTOS 学习就算入门了。