特别说明:
1. 本教程是安富莱电子原创。
2. 安富莱STM32F407开发板资料已经全部开源,开源地址:地址链接
3. 当前共配套300多个实例,4套用户手册。
第11章 μCOS-III内核函数分析
本期教程开始分析μCOS-III的内核函数,源码的分析采用先对源码进行注释,然后讲解函数实现的功能和相关的原理分析,最后是举一个例子(如果这个函数是供外部函数调用的)。内核函数很重要,是学习任务管理,任务间通信机制的基础。希望初学的同学认真学习,这部分应该算是μCOS-III的核心代码。 11.1 系统配置文件 11.2 源码文件 11.3 μCOS-III初始化 11.4 μCOS-III启动 11.5 获取系统版本 11.6 空闲任务 11.7 临界段 11.8 安全关键IEC61508 11.9 任务切换 11.10 调度锁 11.11 Round-Robin调度 11.12 总结
11.1 系统配置文件 下面先简单说明下μCOS-III中几个配置文件的作用,方便分析源码的时候查看,配置文件主要有以下几个:
11.1.1 lib_cfg.h配置文件 lib_cfg.h文件内容如下: - /*
- *********************************************************************************************************
- * MODULE
- *********************************************************************************************************
- */
-
- #ifndef LIB_CFG_MODULE_PRESENT
- #define LIB_CFG_MODULE_PRESENT
-
- /*
- *********************************************************************************************************
- * uC/LIB CONFIGURATION
- *********************************************************************************************************
- */
-
- #define LIB_MEM_CFG_ARG_CHK_EXT_EN DEF_ENABLED
- /* DEF_DISABLED Argument check DISABLED */
- /* DEF_ENABLED Argument check ENABLED */
-
- #define LIB_MEM_CFG_OPTIMIZE_ASM_EN DEF_ENABLED
- /* DEF_DISABLED Assembly-optimized function(s) DISABLED */
- /* DEF_ENABLED Assembly-optimized function(s) ENABLED */
-
- #define LIB_MEM_CFG_ALLOC_EN DEF_ENABLED
- /* DEF_DISABLED Memory allocation DISABLED */
- /* DEF_ENABLED Memory allocation ENABLED */
-
- #define LIB_MEM_CFG_HEAP_SIZE 23u * 1024u /* Configure Heap Memory Size */
-
- /*
- *********************************************************************************************************
- * MODULE END
- *********************************************************************************************************
- */
-
- #endif<span lang="EN-US" style="text-indent: 21.2pt; line-height: 12pt; font-size: 9pt; font-family: 新宋体; background-color: rgb(255, 255, 255);"> </span><span lang="EN-US" style="text-indent: 21.2pt; line-height: 12pt; font-family: 微软雅黑, sans-serif; background-color: rgb(255, 255, 255);"> </span>
复制代码 lib_cfg.h是用于给uC/LIB做配置的头文件。如果程序中使用uC/LIB的话,需要调用函数Mem_Init()进行初始化。 11.1.2 os_cfg.h配置文件 os_cfg.h文件中的内容如下: - #ifndef OS_CFG_H
- #define OS_CFG_H
-
-
- /* ---------------------------- MISCELLANEOUS -------------------------- */
- #define OS_CFG_APP_HOOKS_EN 1u /* Enable (1) or Disable (0) application specific hooks */
- #define OS_CFG_ARG_CHK_EN 1u /* Enable (1) or Disable (0) argument checking */
- #define OS_CFG_CALLED_FROM_ISR_CHK_EN 1u /* Enable (1) or Disable (0) check for called from ISR */
- #define OS_CFG_DBG_EN 1u /* Enable (1) debug code/variables */
- #define OS_CFG_ISR_POST_DEFERRED_EN 0u /* Enable (1) or Disable (0) Deferred ISR posts */
- #define OS_CFG_OBJ_TYPE_CHK_EN 1u /* Enable (1) or Disable (0) object type checking */
- #define OS_CFG_TS_EN 1u /* Enable (1) or Disable (0) time stamping */
-
- #define OS_CFG_PEND_MULTI_EN 1u /* Enable (1) or Disable (0) code generation for multi-pend feature */
-
- #define OS_CFG_PRIO_MAX 64u /* Defines the maximum number of task priorities (see OS_PRIO data type) */
-
- #define OS_CFG_SCHED_LOCK_TIME_MEAS_EN 0u /* Include code to measure scheduler lock time */
- #define OS_CFG_SCHED_ROUND_ROBIN_EN 0u /* Include code for Round-Robin scheduling */
- #define OS_CFG_STK_SIZE_MIN 64u /* Minimum allowable task stack size */
-
-
- /* ----------------------------- EVENT FLAGS --------------------------- */
- #define OS_CFG_FLAG_EN 1u /* Enable (1) or Disable (0) code generation for EVENT FLAGS */
- #define OS_CFG_FLAG_DEL_EN 1u /* Include code for OSFlagDel() */
- #define OS_CFG_FLAG_MODE_CLR_EN 1u /* Include code for Wait on Clear EVENT FLAGS */
- #define OS_CFG_FLAG_PEND_ABORT_EN 1u /* Include code for OSFlagPendAbort() */
-
-
- /* -------------------------- MEMORY MANAGEMENT ------------------------ */
- #define OS_CFG_MEM_EN 1u /* Enable (1) or Disable (0) code generation for MEMORY MANAGER */
-
-
- /* --------------------- MUTUAL EXCLUSION SEMAPHORES ------------------- */
- #define OS_CFG_MUTEX_EN 1u /* Enable (1) or Disable (0) code generation for MUTEX */
- #define OS_CFG_MUTEX_DEL_EN 1u /* Include code for OSMutexDel() */
- #define OS_CFG_MUTEX_PEND_ABORT_EN 1u /* Include code for OSMutexPendAbort() */
-
-
- /* --------------------------- MESSAGE QUEUES -------------------------- */
- #define OS_CFG_Q_EN 1u /* Enable (1) or Disable (0) code generation for QUEUES */
- #define OS_CFG_Q_DEL_EN 1u /* Include code for OSQDel() */
- #define OS_CFG_Q_FLUSH_EN 1u /* Include code for OSQFlush() */
- #define OS_CFG_Q_PEND_ABORT_EN 1u /* Include code for OSQPendAbort() */
-
-
- /* ----------------------------- SEMAPHORES ---------------------------- */
- #define OS_CFG_SEM_EN 1u /* Enable (1) or Disable (0) code generation for SEMAPHORES */
- #define OS_CFG_SEM_DEL_EN 1u /* Include code for OSSemDel() */
- #define OS_CFG_SEM_PEND_ABORT_EN 1u /* Include code for OSSemPendAbort() */
- #define OS_CFG_SEM_SET_EN 1u /* Include code for OSSemSet() */
-
-
- /* -------------------------- TASK MANAGEMENT -------------------------- */
- #define OS_CFG_STAT_TASK_EN 1u /* Enable (1) or Disable(0) the statistics task */
- #define OS_CFG_STAT_TASK_STK_CHK_EN 1u /* Check task stacks from statistic task */
-
- #define OS_CFG_TASK_CHANGE_PRIO_EN 1u /* Include code for OSTaskChangePrio() */
- #define OS_CFG_TASK_DEL_EN 1u /* Include code for OSTaskDel() */
- #define OS_CFG_TASK_Q_EN 1u /* Include code for OSTaskQXXXX() */
- #define OS_CFG_TASK_Q_PEND_ABORT_EN 1u /* Include code for OSTaskQPendAbort() */
- #define OS_CFG_TASK_PROFILE_EN 1u /* Include variables in OS_TCB for profiling */
- #define OS_CFG_TASK_REG_TBL_SIZE 1u /* Number of task specific registers */
- #define OS_CFG_TASK_SEM_PEND_ABORT_EN 1u /* Include code for OSTaskSemPendAbort() */
- #define OS_CFG_TASK_SUSPEND_EN 1u /* Include code for OSTaskSuspend() and OSTaskResume() */
-
-
- /* -------------------------- TIME MANAGEMENT -------------------------- */
- #define OS_CFG_TIME_DLY_HMSM_EN 1u /* Include code for OSTimeDlyHMSM() */
- #define OS_CFG_TIME_DLY_RESUME_EN 1u /* Include code for OSTimeDlyResume() */
-
-
- /* ------------------------- TIMER MANAGEMENT -------------------------- */
- #define OS_CFG_TMR_EN 1u /* Enable (1) or Disable (0) code generation for TIMERS */
- #define OS_CFG_TMR_DEL_EN 1u /* Enable (1) or Disable (0) code generation for OSTmrDel() */
-
- #endif
复制代码 这个配置文件比较的重要,主要用于μCOS-III源码中相关函数的配置。
|
11.7 临界段
11.7.1 临界段基本概念
11.3.2 初始化函数OSInit()
11.9.2 中断级任务切换OSIntExit()
11.9.3 函数使用举例
11.1.3 os_cfg_app.h配置文件
11.1.5 cpu_cfg.h配置文件
11.4.1 启动
5. 可接受凤险 (ACCeptable risk)
11.10.2 调度器解锁OSSchedUnlock
11.10.3 调度器被锁时间测量OS_SchedLockTimeMeas
11.10.4 函数使用举例