定位到uCOS-II/Source/目录,这部分是系统的核心代码。先看ucos_ii.h文件:
1. 宏定义类型以及头文件包含
#define OS_VERSION 29207u //定义版本号
#include <app_cfg.h>
#include <os_cfg.h>
#include <os_cpu.h>
#ifdef OS_GLOBALS
#define OS_EXT
#else
#define OS_EXT extern
#endif
#ifndef OS_FALSE
#define OS_FALSE 0u
#endif
#ifndef OS_TRUE
#define OS_TRUE 1u
#endif
#define OS_ASCII_NUL (INT8U)0
#define OS_PRIO_SELF 0xFFu /* Indicate SELF priority */
OS_PRIO_SELF被宏定义为0xFF,从注释上看OS_PRIO_SELF代表的是任务自身的优先级。为什么是0xff?这宏是适用于当你不知道任务的优先级但是又要操作任务的优先级的时候,uCOS-II的内部函数会将其转换为真正优先级的代码。例如你想用OSTaskDel()删除当前任务但是你又不知道当前任务的优先级,可以写为:
OSTaskDel(OS_PRIO_SELF);
#if OS_TASK_STAT_EN > 0
#define OS_N_SYS_TASKS 2u /* Number of system tasks */
#else
#define OS_N_SYS_TASKS 1u
#endif
OS_TASK_STAT_EN是一个配置宏,OS_TASK_STAT_EN > 0表示使用系统中的统计任务。统计任务是属于系统任务,默认开启且无法被删除的系统任务还有一个空闲任务。当使能了系统统计任务,那么OS_N_SYS_TASKS=2,反之OS_N_SYS_TASKS=1。
#define OS_TASK_STAT_PRIO (OS_LOWEST_PRIO - 1)
#define OS_TASK_IDLE_PRIO (OS_LOWEST_PRIO)
OS_TASK_STAT_PRIO、OS_TASK_IDLE_PRIO分别是统计任务、空闲任务的优先级,OS_LOWEST_PRIO为系统任务的最低优先级。假设系统为最多支持32个任务,那么任务的最低优先级为31(优先级数值越大,优先级越低)。倒数两个低优先级分别赋给统计任务和空闲任务。OS_TASK_STAT_PRIO = 30,OS_TASK_IDLE_PRIO = 29。统计任务用于统计系统各种状态参数,如内存/CPU使用率等,空闲任务在系统中无任务可执行时才轮到它执行。
#if OS_LOWEST_PRIO <= 63
#define OS_EVENT_TBL_SIZE ((OS_LOWEST_PRIO) / 8 + 1) /* Size of event table */
#define OS_RDY_TBL_SIZE ((OS_LOWEST_PRIO) / 8 + 1) /* Size of ready table */
#else
#define OS_EVENT_TBL_SIZE ((OS_LOWEST_PRIO) / 16 + 1) /* Size of event table */
#define OS_RDY_TBL_SIZE ((OS_LOWEST_PRIO) / 16 + 1) /* Size of ready table */
#endif
OS_RDY_TBL_SIZE是任务就绪表的下标取值范围,OS_LOWEST_PRIO=31, 这里计算得出OS_RDY_TBL_SIZE等于4,即下标取值为0~3。OS_EVENT_TBL_SIZE同理。
#define OS_TASK_IDLE_ID 65535u /* ID numbers for Idle, Stat and Timer tasks */
#define OS_TASK_STAT_ID 65534u
#define OS_TASK_TMR_ID 65533u
Idle、Stat、Timer任务的ID,为什么是65535?因为uCOS-II最多支持2^16个任务,其ID最大为65535。在uCOS-II中id值并没什么用处。
#define OS_EVENT_EN (((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0) || (OS_SEM_EN > 0) || (OS_MUTEX_EN > 0))
#define OS_TCB_RESERVED ((OS_TCB *)1)
OS_EVENT_EN为1表示开启系统的事件功能机制。
#define OS_STAT_RDY 0x00u /* Ready to run (就绪态) */
#define OS_STAT_SEM 0x01u /* Pending on semaphore (因等待信号量而被挂起) */
#define OS_STAT_MBOX 0x02u /* Pending on mailbox (因等效MBox而被挂起) */
#define OS_STAT_Q 0x04u /* Pending on queue (因等待消息队列而被挂起) */
#define OS_STAT_SUSPEND 0x08u /* Task is suspended (挂起态) */
#define OS_STAT_MUTEX 0x10u /* Pending on mutual exclusion semaphore (因等待Mutex而被挂起) */
#define OS_STAT_FLAG 0x20u /* Pending on event flag group (因等待flag而被挂起) */
#define OS_STAT_MULTI 0x80u /* Pending on multiple events (因等待MULTI而被挂起,MULTI指代所有事件类型的综合) */
#define OS_STAT_PEND_ANY (OS_STAT_SEM | OS_STAT_MBOX | OS_STAT_Q | OS_STAT_MUTEX | OS_STAT_FLAG)
如上宏表示任务当前的运行状态,是TCB中OSTCBStat成员的取值选项。
#define OS_STAT_PEND_OK 0u /* 已经挂起结束或没有挂起 */
#define OS_STAT_PEND_TO 1u /* 在有超时机制的挂起状态中 */
#define OS_STAT_PEND_ABORT 2u /* 挂起出错 */
如上宏表示任务当前的挂起状态,也是TCB中OSTCBStatPend成员的取值选项。
#define OS_EVENT_TYPE_UNUSED 0u
#define OS_EVENT_TYPE_MBOX 1u
#define OS_EVENT_TYPE_Q 2u
#define OS_EVENT_TYPE_SEM 3u
#define OS_EVENT_TYPE_MUTEX 4u
#define OS_EVENT_TYPE_FLAG 5u
#define OS_TMR_TYPE 100u
如上宏表示系统中的事件类型。
#define OS_FLAG_WAIT_CLR_ALL 0u /* Wait for ALL the bits specified to be CLR (i.e. 0) */
#define OS_FLAG_WAIT_CLR_AND 0u
#define OS_FLAG_WAIT_CLR_ANY 1u /* Wait for ANY of the bits specified to be CLR (i.e. 0) */
#define OS_FLAG_WAIT_CLR_OR 1u
#define OS_FLAG_WAIT_SET_ALL 2u /* Wait for ALL the bits specified to be SET (i.e. 1) */
#define OS_FLAG_WAIT_SET_AND 2u
#define OS_FLAG_WAIT_SET_ANY 3u /* Wait for ANY of the bits specified to be SET (i.e. 1) */
#define OS_FLAG_WAIT_SET_OR 3u
#define OS_FLAG_CONSUME 0x80u /* Consume the flags if condition(s) satisfied */
#define OS_FLAG_CLR 0u
#define OS_FLAG_SET 1u
跟flag事件相关的宏定义。
#if OS_TICK_STEP_EN > 0
#define OS_TICK_STEP_DIS 0u /* Stepping is disab