01. µCOS-Ⅲ的简介

一、µC/OS-Ⅲ的简介

1.1、什么是RTOS

  操作系统是允许多个任务 “同时运行” 的,操作系统的这个特性被称为多任务。然而实际上,一个 CPU 核心在某一时刻只能运行一个任务,而操作系统中任务调度器的责任就是决定在某一时刻 CPU 究竟要运行哪一个任务,任务调度器使得 CPU 在各个任务之间来回切换并处理任务,由于切换处理任务的速度非常快,因此就给人造成了一种同一时刻有多个任务同时运行的错觉。

  操作系统的分类方式可以由任务调度器的工作方式决定,比如有的操作系统给每个任务分配同样的运行时间,时间到了就切换到下一个任务,Unix 操作系统就是这样的。RTOS 的任务调度器被设计为可预测的,而这正是嵌入式实时操作系统所需要的。在实时环境中,要求操作系统必须实时地对某一个事件做出响应,因此任务调度器的行为必须是可预测的。像 µC/OS-Ⅲ 这种传统的 RTOS 类操作系统是由用户给每个任务分配一个任务优先级,任务调度器就可以根据此优先级来决定下一刻应该运行哪个任务。

  RTOS 全称为 Real Time OS,就是实时操作系统,它将功能分而治之,划分为多个任务,并且通过延时函数进行任务调度。其中,高优先级任务抢占低优先级任务,并且每个任务都有自己的栈空间。

任务调度

中断可以打断任意任务。

任务可以同等优先级。

1.2、什么是µC/OS-Ⅲ

  µC/OS-Ⅲ 是基于 C 语言编写的第三代小型操作系统,这里所说的第三代是相对于 µC/OS 的前两个版本 µC/OS 和 µC/OS-Ⅱ 而言的。这三个版本的特征比较如下表所示:

µCOS各版本特征比较

µC/OS-Ⅲ 是一个可裁剪、可剥夺的多任务系统,没有任务数目的限制,是 µC/OS 的第三代内核操作系统。

二、任务调度简介

  调度器就是使用相关的调度算法来决定当前需要执行哪个任务,µC/OS-Ⅲ 支持以下任务调度方式:抢占式调度时间片调度

  • 抢占式调度:主要针对优先级不同的任务,每个任务都有一个优先级,优先级高的任务可以抢占优先级低的任务。
  • 时间片调度:主要针对优先级相同的任务,当多个任务的优先级相同且就绪时,任务调度器会根据用户所设置的时间片轮流的运行这些任务。

时间片是以一次系统节拍位单位。如 µC/OS-Ⅲ 默认设置的任务片为 100,则 µC/OS-Ⅲ 会在当前任务运行 100 次系统时钟节拍时的时间后,切换到另一个相同任务优先级的任务中运行。

2.1、抢占式调度

  在 µC/OS-Ⅲ 中,高优先级任务,优先执行,并且高优先级任务不停止,低优先级任务无法执行。被抢占的任务将会进入就绪态。

抢占式调度

  首先,这里创建三个任务:Task1、Task2、Task3。Task1、Task2、Task3 的优先级分别为 3、2、1;在 µC/OS-Ⅲ 中任务设置的数值越小,优先级越高,所以 Task3 的优先级最高。

  该程序在运行时,首先 Task1 在运行中,在这个过程中 Task2 就绪了,在抢占式调度器的作用下 Task2 会抢占 Task1 的运行。然后,在 Task2 运行过程中,Task3 就绪了,在抢占式调度器的作用下 Task3 会抢占 Task2 的运行。接着,Task3 运行过程中,Task3 阻塞了(系统延时或等待信号量等),此时就绪态中,优先级最高的任务 Task2 执行。随后,Task3 阻塞解除了(延时到了或者接收到信号量),此时 Task3 恢复到就绪态中,抢占 TasK2 的运行。

2.2、时间片调度

  同等优先级任务轮流地享有相同的 CPU 时间(可设置), 叫时间片,在 µC/OS-Ⅲ 中,一个时间片就等于 SysTick 中断周期。没有用完的时间片不会再使用,下次任务得到执行还是按照一个时间片的时钟节拍运行。

时间片调度

  首先,这里创建三个任务:Task1、Task2、Task3。Task1、Task2、Task3 的优先级均为 1,即 3 个任务同等优先级。然后,我们将三个任务的时间片都设置为 100。

  该程序在运行时,首先 Task1 运行完 100 个时间片后,切换至 Task2 运行。然后,Task2 运行完 100 个时间片后,切换至 Task3 运行。如果在 Task3 运行过程中(还不到 100 个时间片),Task3 阻塞了(系统延时或等待信号量等),此时直接切换到下一个任务 Task1。接着,Task1 运行完 100 个时间片后,切换至 Task2 运行,依次循环运行下去。

注意没有用完的时间片不会再使用,下次任务 Task3 得到执行还是按照 100 个时间片的时钟节拍运行。

三、任务状态

  µC/OS-Ⅲ 中任务共存在 5 种任务状态:运行态就绪态挂起态休眠态中断态

  • 运行态:正在执行的任务,该任务就处于运行态,注意在 STM32 中,同一时间仅一个任务处于运行态。
  • 就绪态:如果该任务已经能够被执行,但当前还未被执行,那么该任务处于就绪态。
  • 挂起态:如果一个运行态的任务因延迟或等待某一事件发生时被挂起,那么这个任务就处于挂起态。
  • 休眠态:任务已经存在于 CPU 的内存中(任务被删除了),但是还没有交给 µC/OS-Ⅲ 内核管理。
  • 中断态:当处于运行态的任务被中断打断,CPU 跳转去执行中中断服务函数时,原本属于运行态的任务会切换到中断态,直到中断结束,在切换运行态进行运行。

任务状态转换图

被创建的任务,初始状态均为就绪态。

被删除的任务,会转换为休眠态。

仅就绪态和中断态可以转变为运行态。

  µC/OS-Ⅲ 主要有三大类任务列表用于跟踪任务状态:

  这四种状态中,除了运行态,其他三种任务状态的任务都有其对应的任务状态列表。

  • 就绪列表:准备运行的任务将放在就绪列表:OSRdyList[x],其中 x 代表任务优先级数值。
  • Tick 列表:正在等待延迟超时或挂起的对象超时的任务,将放在 OSTickList。
  • 挂起列表:当任务等待信号量、事件时,任务将放置在挂起列表 PendList。

调度器总是在所有处于就绪列表的任务中,选择具有最高优先级的任务来执行。

相同优先级的任务会连接在同一个就绪列表上。

四、µC/OS-Ⅲ的移植

4.1、µC/OS-Ⅲ源码的下载

  我们可以从 GitHub 上下载 µC/OS-Ⅲ 的源码,uC-OS3:https://github.com/weston-embedded/uC-OS3,uC-CPU:https://github.com/weston-embedded/uC-CPU,uC-LIB:https://github.com/weston-embedded/uC-LIB

  【uC-OS3】的目录结构如下:

uC-OS3目录结构

  其中,主要的文件夹及其存放的文件说明如下:

目录文件 说明
Cfg 包含的是 µC/OS-Ⅲ 配置文件的模板文件
Ports 包含的是与硬件相关的移植文件
Source µC/OS-Ⅲ 的源码文件
Template 仅包含了一个文件,这个文件是与动态 Tick 管理相关的文件
TLS TLS 是 Thread Local Storage(线程本地存储)的缩写
Trace 用来存放一些调试工具的,调试工具需要用户另外自行下载
LICENSE µC/OS-Ⅲ 的许可文件
NOTICE µC/OS-Ⅲ 的注意事项
README.rst µC/OS-Ⅲ 的 README 文件

  【uC-CPU】的目录结构如下:

uC-CPU目录结构

  该目录下是不同内核的 CPU 相关的移植文件,如前导置零指令、大小端模式设置等,这里,我们只需要关注有关 STM32F4 系列的内核文件,即【ARM-Cortex-M】文件夹下的文件。

  【uC-LIB】的目录结构如下:

uC-LIB目录结构

  【uC-LIB】目录:官方提供的 ASCII 字符操作、数学、内存管理、字符串操作的库。

4.2、添加µC/OS-Ⅲ的源码

  在基础工程的【Middleware】文件夹中新建一个【uC-OS3】子文件夹。接着就可以将 µC/OS-Ⅲ 及其相关组件的源代码添加到刚刚新建的【uC-OS3】子文件夹中了。接着就可以将 µC/OS-III 及其相关组件的源代码添加到刚刚新建的【uC-OS3】子文件夹中了。然后,我们裁剪掉不用的文件,裁剪后的文件结构如下:

精简后的uC-CPU文件夹

精简后的uC-LIB文件夹

精简后的uC-OS3文件夹

4.3、修改SysTick相关的文件

  µC/OS-Ⅲ 系统使用 SysTick 作为任务切换的时基单元,因此,我们需要修改有关 SysTick 的代码。

  修改 stm32f4xx_it.c 文件中关于 SysTick 的中断服务函数:

HAL库默认的SysTick中断服务函数

void SysTick_Handler(void)
{
    if (OSRunning == OS_STATE_OS_RUNNING)                                       // 当UCOS系统运行了,才执行正常的调度处理
    {
        // 在硬件回调滴答定时器时,就直接调用UCOS中的时钟函数
        // 1s内调用多少次,是由配置参数 OS_CFG_TICK_RATE_HZ 决定的
        OS_CPU_SysTickHandler();              
    }
    HAL_IncTick();
}

  修改基础工程上的关于延迟初始化函数:

uint16_t g_frequency_us = 0;                                                    // us延时倍乘数

/**
 * @brief 延迟初始化函数
 * 
 * @param clock 系统时钟频率,单位为MHz
 */
void Delay_Init(uint16_t clock)
{
    uint32_t reload = 0;


    SysTick->CTRL = 0;                                                          // SysTick控制寄存器清零

    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);                        // 设置SysTick时钟源为HCLK
    g_frequency_us = clock;                                                     // 1us定时的计数频率

    reload = clock;                                                             // 每秒钟的计数次数 单位为M

    // 根据delay_ostickspersec设定溢出时间,reload为24位寄存器,最大值:16777216,在168M下,约合0.09986s左右
    reload *= 1000000 / OSCfg_TickRate_Hz;

    SysTick->CTRL |= 1 << 1;                                                    // 开启SysTick中断
    SysTick->LOAD = reload;                                                     // 设置计数器的值
    SysTick->CTRL |= 1 << 0;                                                    // 开启计数器
}

  修改微秒级延迟函数:

/**
 * @brief 微秒级延迟函数
 * 
 * @param time 要延迟的微秒数
 */
void Delay_us(uint32_t time)
{
    uint32_t tick = 0;                                         
    uint32_t t_old = 0, t_now = 0, t_count = 0;
    uint32_t reload = SysTick->LOAD;                                            // LOAD的值

    OS_ERR err;                                                                 // 定义用于接收错误代码的变量

    tick = time * g_frequency_us;                                               // 延迟时间加载

    OSSchedLock(&err);                                                          // 锁定 uC/OS-III 的任务调度器

    t_old = SysTick->VAL;                                       

    while (1)
    {
        t_now = SysTick->VAL;                                                   // tnow用于记录当前的SysTick->VAL 值
        if (t_now < t_old)                                                      // 在一轮内,t_count加等于t_old到t_now的差值
        {
            t_count += t_old - t_now;
        }
        else                                                                    // 超过一轮内,t_count加等于重装值减t_now到t_old的差值,即VAL-(t_now-t_old)
        {
            t_count += reload - t_now + t_old;
        }
        t_old = t_now;                                                          // t_old用于记录最近一次的SysTick->VAL值
        if (t_count >= tick)                                                    // 时间超过或等于要延迟的时间,则定时时间到,退出
        {
            break;
        }
    }

    OSSchedUnlock(&err);                                                        // 恢复 uC/OS-III 的任务调度器
}

  修改毫秒级延迟函数:

/**
 * @brief 毫秒级延迟函数
 * 
 * @param time 要延迟的毫秒数
 */
void Delay_ms(uint32_t time)
{
    for (uint32_t i = 0; i < time; i++)
    {
        Delay_us(1000);
    }
  
}

4.4、修改有关任务切换的文件

  STM32 的 HAL 库默认的任务切换的中断服务函数为 PendSV() 中断服务函数,而 µC/OS-Ⅲ 提供的任务切换的中断服务函数为 OS_CPU_PendSVHandler()。

  HAL 库默认定义的任务切换在 stm32f4xx_it.c 文件中,它的内容如下:

HAL库默认的任务切换的中断服务函数

  我们需要将 STM32 的 startup_stm32f407xx.s 启动文件中的 PendSV 任务切换的中断服务函数名替换成 µC/OS-Ⅲ 默认的任务切换中断服务函数名 OS_CPU_PendSVHandler。

需要替换的任务切换的中断服务函数名1

需要替换的任务切换的中断服务函数名2

4.5、配置µC/OS及其相关组件

  µC/OS-Ⅲ 是可裁剪的,那么移植 µC/OS-Ⅲ 就需要涉及到对 µC/OS-Ⅲ 的配置,当然,同时 µC/CPU 和 µC/LIB 也是需要配置的。为了让 µC/OS-Ⅲ 在开发板上 “跑起来” 只需要修改 µC/CPU 的配置文件即可。µC/CPU 的配置文件的路径在 uC-CPU/Cfg/Template/cpu_cfg.h,打开该文件,并将配置文件中用于配置配置 CPU 中断优先级寄存器使用位数的宏开关置 1 即可。

配置CPU中断优先级寄存器

#if 1
#define  CPU_CFG_NVIC_PRIO_BITS                            4u
#endif

  对 µC/OS-Ⅲ 及其相关组件的配置,一共涉及到四个配置文件,这四个配置文件的文件名和对应的路径如下表所示:

配置文件

  cpu_cfg.h 配置文件主要用于配置 µC/OS-Ⅲ 内核的一些功能,比如:消息队列、信号量、事件标志等。修改后的内容如下:

/*
*********************************************************************************************************
*                                               uC/CPU
*                                    CPU CONFIGURATION & PORT LAYER
*
*                    Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
*
*                                 SPDX-License-Identifier: APACHE-2.0
*
*               This software is subject to an open source license and is distributed by
*                Silicon Laboratories Inc. pursuant to the terms of the Apache License,
*                    Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                       CPU CONFIGURATION FILE
*
*                                              TEMPLATE
*
* Filename : cpu_cfg.h
* Version  : V1.32.01
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                               MODULE
*********************************************************************************************************
*/

#ifndef  CPU_CFG_MODULE_PRESENT
#define  CPU_CFG_MODULE_PRESENT


/*
*********************************************************************************************************
*                                       CPU NAME CONFIGURATION
*
* Note(s) : (1) Configure CPU_CFG_NAME_EN to enable/disable CPU host name feature :
*
*               (a) CPU host name storage
*               (b) CPU host name API functions
*
*           (2) Configure CPU_CFG_NAME_SIZE with the desired ASCII string size of the CPU host name,
*               including the terminating NULL character.
*
*               See also 'cpu_core.h  GLOBAL VARIABLES  Note #1'.
*********************************************************************************************************
*/

                                                                /* Configure CPU host name feature (see Note #1) :      */
#define  CPU_CFG_NAME_EN                        DEF_ENABLED
                                                                /*   DEF_DISABLED  CPU host name DISABLED               */
                                                                /*   DEF_ENABLED   CPU host name ENABLED                */

                                                                /* Configure CPU host name ASCII string size ...        */
#define  CPU_CFG_NAME_SIZE                                16    /* ... (see Note #2).                                   */


/*
*********************************************************************************************************
*                                     CPU TIMESTAMP CONFIGURATION
*
* Note(s) : (1) Configure CPU_CFG_TS_xx_EN to enable/disable CPU timestamp features :
*
*               (a) CPU_CFG_TS_32_EN   enable/disable 32-bit CPU timestamp feature
*               (b) CPU_CFG_TS_64_EN   enable/disable 64-bit CPU timestamp feature
*
*           (2) (a) Configure CPU_CFG_TS_TMR_SIZE with the CPU timestamp timer's word size :
*
*                       CPU_WORD_SIZE_08         8-bit word size
*                       CPU_WORD_SIZE_16        16-bit word size
*                       CPU_WORD_SIZE_32        32-bit word size
*                       CPU_WORD_SIZE_64        64-bit word size
*
*               (b) If the size of the CPU timestamp timer is not a binary multiple of 8-bit octets
*                   (e.g. 20-bits or even 24-bits), then the next lower, binary-multiple octet word
*                   size SHOULD be configured (e.g. to 16-bits).  However, the minimum supported word
*                   size for CPU timestamp timers is 8-bits.
*
*                   See also 'cpu_core.h  FUNCTION PROTOTYPES  CPU_TS_TmrRd()  Note #2a'.
*********************************************************************************************************
*/

                                                                /* Configure CPU timestamp features (see Note #1) :     */
#define  CPU_CFG_TS_32_EN                       DEF_ENABLED
#define  CPU_CFG_TS_64_EN                       DEF_DISABLED
                                                                /*   DEF_DISABLED  CPU timestamps DISABLED              */
                                                                /*   DEF_ENABLED   CPU timestamps ENABLED               */

                                                                /* Configure CPU timestamp timer word size ...          */
                                                                /* ... (see Note #2) :                                  */
#define  CPU_CFG_TS_TMR_SIZE                    CPU_WORD_SIZE_32


/*
*********************************************************************************************************
*                        CPU INTERRUPTS DISABLED TIME MEASUREMENT CONFIGURATION
*
* Note(s) : (1) (a) Configure CPU_CFG_INT_DIS_MEAS_EN to enable/disable measuring CPU's interrupts
*                   disabled time :
*
*                   (a)  Enabled,       if CPU_CFG_INT_DIS_MEAS_EN      #define'd in 'cpu_cfg.h'
*
*                   (b) Disabled,       if CPU_CFG_INT_DIS_MEAS_EN  NOT #define'd in 'cpu_cfg.h'
*
*                   See also 'cpu_core.h  FUNCTION PROTOTYPES  Note #1'.
*
*               (b) Configure CPU_CFG_INT_DIS_MEAS_OVRHD_NBR with the number of times to measure &
*                   average the interrupts disabled time measurements overhead.
*
*                   See also 'cpu_core.c  CPU_IntDisMeasInit()  Note #3a'.
*********************************************************************************************************
*/

#if 0                                                           /* Configure CPU interrupts disabled time ...           */
#define  CPU_CFG_INT_DIS_MEAS_EN                                /* ... measurements feature (see Note #1a).             */
#endif

                                                                /* Configure number of interrupts disabled overhead ... */
#define  CPU_CFG_INT_DIS_MEAS_OVRHD_NBR                    1u   /* ... time measurements (see Note #1b).                */


/*
*********************************************************************************************************
*                                    CPU COUNT ZEROS CONFIGURATION
*
* Note(s) : (1) (a) Configure CPU_CFG_LEAD_ZEROS_ASM_PRESENT  to define count leading  zeros bits
*                   function(s) in :
*
*                   (1) 'cpu_a.asm',  if CPU_CFG_LEAD_ZEROS_ASM_PRESENT       #define'd in 'cpu.h'/
*                                         'cpu_cfg.h' to enable assembly-optimized function(s)
*
*                   (2) 'cpu_core.c', if CPU_CFG_LEAD_ZEROS_ASM_PRESENT   NOT #define'd in 'cpu.h'/
*                                         'cpu_cfg.h' to enable C-source-optimized function(s) otherwise
*
*               (b) Configure CPU_CFG_TRAIL_ZEROS_ASM_PRESENT to define count trailing zeros bits
*                   function(s) in :
*
*                   (1) 'cpu_a.asm',  if CPU_CFG_TRAIL_ZEROS_ASM_PRESENT      #define'd in 'cpu.h'/
*                                         'cpu_cfg.h' to enable assembly-optimized function(s)
*
*                   (2) 'cpu_core.c', if CPU_CFG_TRAIL_ZEROS_ASM_PRESENT  NOT #define'd in 'cpu.h'/
*                                         'cpu_cfg.h' to enable C-source-optimized function(s) otherwise
*********************************************************************************************************
*/

#if 1                                                           /* Configure CPU count leading  zeros bits ...          */
#define  CPU_CFG_LEAD_ZEROS_ASM_PRESENT                         /* ... assembly-version (see Note #1a).                 */
#endif

#if 1                                                           /* Configure CPU count trailing zeros bits ...          */
#define  CPU_CFG_TRAIL_ZEROS_ASM_PRESENT                        /* ... assembly-version (see Note #1b).                 */
#endif


/*
*********************************************************************************************************
*                                      CPU ENDIAN TYPE OVERRIDE
*
* Note(s) : (1) Configure CPU_CFG_ENDIAN_TYPE to override the default CPU endian type defined in cpu.h.
*
*               (a) CPU_ENDIAN_TYPE_BIG         Big-   endian word order (CPU words' most  significant
*                                                                         octet @ lowest memory address)
*               (b) CPU_ENDIAN_TYPE_LITTLE      Little-endian word order (CPU words' least significant
*                                                                         octet @ lowest memory address)
*
*           (2) Defining CPU_CFG_ENDIAN_TYPE here is only valid for supported bi-endian architectures.
*               See  'cpu.h  CPU WORD CONFIGURATION  Note #3' for details
*********************************************************************************************************
*/

#if 1
#define  CPU_CFG_ENDIAN_TYPE            CPU_ENDIAN_TYPE_LITTLE     /* Defines CPU data    word-memory order (see Note #2). */
#endif


/*
*********************************************************************************************************
*                                          CACHE MANAGEMENT
*
* Note(s) : (1) Configure CPU_CFG_CACHE_MGMT_EN to enable the cache management API.
*
*           (2) This option only enables the cache management functions.
*               It does not enable any hardware caches, which should be configured in startup code.
*               Caches must be configured and enabled by the time CPU_Init() is called.
*
*           (3) This option is usually required for device drivers which use a DMA engine to transmit
*               buffers that are located in cached memory.
*********************************************************************************************************
*/

#define  CPU_CFG_CACHE_MGMT_EN            DEF_DISABLED          /* Defines CPU data    word-memory order (see Note #1). */


/*
*********************************************************************************************************
*                                      KERNEL AWARE IPL BOUNDARY
*
* Note(s) : (1) Determines the IPL level that establishes the boundary for ISRs that are kernel-aware and
*               those that are not.  All ISRs at this level or lower are kernel-aware.
*
*           (2) ARMv7-M: Since the port is using BASEPRI to separate kernel vs non-kernel aware ISR, please
*               make sure your external interrupt priorities are set accordingly. For example, if
*               CPU_CFG_KA_IPL_BOUNDARY is set to 4 then external interrupt priorities 4-15 will be kernel
*               aware while priorities 0-3 will be use as non-kernel aware.
*********************************************************************************************************
*/

#define  CPU_CFG_KA_IPL_BOUNDARY                           4u


/*
*********************************************************************************************************
*                                            ARM CORTEX-M
*
* Note(s) : (1) Determines the interrupt programmable priority levels. This is normally specified in the
*               Microcontroller reference manual. 4-bits gives us 16 programmable priority levels.
*
*                     Example 1                                       Example 2
*                     NVIC_IPRx                                       NVIC_IPRx
*                 7                0                              7                0
*                +------------------+                            +------------------+
*                |       PRIO       |                            |       PRIO       |
*                +------------------+                            +------------------+
*
*                Bits[7:4] Priority mask bits                    Bits[7:6] Priority mask bits
*                Bits[3:0] Reserved                              Bits[5:0] Reserved
*
*                Example 1: CPU_CFG_NVIC_PRIO_BITS should be set to 4 due to the processor
*                           implementing only bits[7:4].
*
*                Example 2: CPU_CFG_NVIC_PRIO_BITS should be set to 2 due to the processor
*                           implementing only bits[7:6].
*********************************************************************************************************
*/
#if 1
#define  CPU_CFG_NVIC_PRIO_BITS                            4u
#endif


/*
*********************************************************************************************************
*                                             MODULE END
*********************************************************************************************************
*/

#endif                                                          /* End of CPU cfg module include.                       */

  lib_cfg.h 配置文件主要配置 µC/LIB 组件,如:使能内存库中优化的内存相关操作函数。修改后的内容如下:

/*
*********************************************************************************************************
*                                            EXAMPLE CODE
*
*               This file is provided as an example on how to use Micrium products.
*
*               Please feel free to use any application code labeled as 'EXAMPLE CODE' in
*               your application products.  Example code may be used as is, in whole or in
*               part, or may be used as a reference only. This file can be modified as
*               required to meet the end-product requirements.
*
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                  CUSTOM LIBRARY CONFIGURATION FILE
*
*                                              TEMPLATE
*
* Filename : lib_cfg.h
* Version  : V1.39.01
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                               MODULE
*********************************************************************************************************
*/

#ifndef  LIB_CFG_MODULE_PRESENT
#define  LIB_CFG_MODULE_PRESENT


/*
*********************************************************************************************************
*********************************************************************************************************
*                                    MEMORY LIBRARY CONFIGURATION
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                             MEMORY LIBRARY ARGUMENT CHECK CONFIGURATION
*
* Note(s) : (1) Configure LIB_MEM_CFG_ARG_CHK_EXT_EN to enable/disable the memory library suite external
*               argument check feature :
*
*               (a) When ENABLED,     arguments received from any port interface provided by the developer
*                   or application are checked/validated.
*
*               (b) When DISABLED, NO arguments received from any port interface provided by the developer
*                   or application are checked/validated.
*********************************************************************************************************
*/

                                                                /* External argument check.                             */
                                                                /* Indicates if arguments received from any port ...    */
                                                                /* ... interface provided by the developer or ...       */
                                                                /* ... application are checked/validated.               */
#define  LIB_MEM_CFG_ARG_CHK_EXT_EN     DEF_DISABLED


/*
*********************************************************************************************************
*                         MEMORY LIBRARY ASSEMBLY OPTIMIZATION CONFIGURATION
*
* Note(s) : (1) Configure LIB_MEM_CFG_OPTIMIZE_ASM_EN to enable/disable assembly-optimized memory function(s).
*********************************************************************************************************
*/

                                                                /* Assembly-optimized function(s).                      */
                                                                /* Enable/disable assembly-optimized memory ...         */
                                                                /* ... function(s). [see Note #1]                       */
#define  LIB_MEM_CFG_OPTIMIZE_ASM_EN    DEF_DISABLED


/*
*********************************************************************************************************
*                                   MEMORY ALLOCATION CONFIGURATION
*
* Note(s) : (1) Configure LIB_MEM_CFG_DBG_INFO_EN to enable/disable memory allocation usage tracking
*               that associates a name with each segment or dynamic pool allocated.
*
*           (2) (a) Configure LIB_MEM_CFG_HEAP_SIZE with the desired size of heap memory (in octets).
*
*               (b) Configure LIB_MEM_CFG_HEAP_BASE_ADDR to specify a base address for heap memory :
*
*                   (1) Heap initialized to specified application memory, if LIB_MEM_CFG_HEAP_BASE_ADDR
*                                                                                #define'd in 'lib_cfg.h';
*                                                                         CANNOT #define to address 0x0
*
*                   (2) Heap declared to Mem_Heap[] in 'lib_mem.c',       if LIB_MEM_CFG_HEAP_BASE_ADDR
*                                                                            NOT #define'd in 'lib_cfg.h'
*********************************************************************************************************
*/

                                                                /* Allocation debugging information.                    */
                                                                /* Enable/disable allocation of debug information ...   */
                                                                /* ... associated to each memory allocation.            */
#define  LIB_MEM_CFG_DBG_INFO_EN        DEF_DISABLED


                                                                /* Heap memory size (in bytes).                         */
                                                                /* Configure the desired size of the heap memory. ...   */
                                                                /* ... Set to 0 to disable heap allocation features.    */
#define  LIB_MEM_CFG_HEAP_SIZE                  0u


                                                                /* Heap memory padding alignment (in bytes).            */
                                                                /* Configure the desired size of padding alignment ...  */
                                                                /* ... of each buffer allocated from the heap.          */
#define  LIB_MEM_CFG_HEAP_PADDING_ALIGN    LIB_MEM_PADDING_ALIGN_NONE

#if 0                                                           /* Remove this to have heap alloc at specified addr.    */
#define  LIB_MEM_CFG_HEAP_BASE_ADDR       0x00000000            /* Configure heap memory base address (see Note #2b).   */
#endif


/*
*********************************************************************************************************
*********************************************************************************************************
*                                    STRING LIBRARY CONFIGURATION
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                 STRING FLOATING POINT CONFIGURATION
*
* Note(s) : (1) Configure LIB_STR_CFG_FP_EN to enable/disable floating point string function(s).
*
*           (2) Configure LIB_STR_CFG_FP_MAX_NBR_DIG_SIG to configure the maximum number of significant
*               digits to calculate &/or display for floating point string function(s).
*
*               See also 'lib_str.h  STRING FLOATING POINT DEFINES  Note #1'.
*********************************************************************************************************
*/

                                                                /* Floating point feature(s).                           */
                                                                /* Enable/disable floating point to string functions.   */
#define  LIB_STR_CFG_FP_EN                      DEF_DISABLED


                                                                /* Floating point number of significant digits.         */
                                                                /* Configure the maximum number of significant ...      */
                                                                /* ... digits to calculate &/or display for ...         */
                                                                /* ... floating point string function(s).               */
#define  LIB_STR_CFG_FP_MAX_NBR_DIG_SIG         LIB_STR_FP_MAX_NBR_DIG_SIG_DFLT


/*
*********************************************************************************************************
*                                             MODULE END
*********************************************************************************************************
*/

#endif                                                          /* End of lib cfg module include.                       */

  os_cfg.h 配置文件主要配置 CPU 相关的一些宏定义,如:时间戳、前导置零指令相关等内容。修改后的内容如下:

/*
*********************************************************************************************************
*                                              uC/OS-III
*                                        The Real-Time Kernel
*
*                    Copyright 2009-2021 Silicon Laboratories Inc. www.silabs.com
*
*                                 SPDX-License-Identifier: APACHE-2.0
*
*               This software is subject to an open source license and is distributed by
*                Silicon Laboratories Inc. pursuant to the terms of the Apache License,
*                    Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                          CONFIGURATION FILE
*
* Filename : os_cfg.h
* Version  : V3.08.01
*********************************************************************************************************
*/

#ifndef OS_CFG_H
#define OS_CFG_H

                                                                /* --------------------------- MISCELLANEOUS --------------------------- */
#define OS_CFG_APP_HOOKS_EN                        0u           /* 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) or Disable (0) debug code/variables                        */
#define OS_CFG_TICK_EN                             1u           /* Enable (1) or Disable (0) the kernel tick                             */
#define OS_CFG_DYN_TICK_EN                         0u           /* Enable (1) or Disable (0) the Dynamic Tick                            */
#define OS_CFG_INVALID_OS_CALLS_CHK_EN             1u           /* Enable (1) or Disable (0) checks for invalid kernel calls             */
#define OS_CFG_OBJ_TYPE_CHK_EN                     1u           /* Enable (1) or Disable (0) object type checking                        */
#define OS_CFG_OBJ_CREATED_CHK_EN                  1u           /* Enable (1) or Disable (0) object created checks                       */
#define OS_CFG_TS_EN                               1u           /* Enable (1) or Disable (0) time stamping                               */

#define OS_CFG_PRIO_MAX                           32u           /* 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                1u           /* 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 the 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 the 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_IDLE_EN                        1u           /* Include the idle task                                                 */
#define OS_CFG_TASK_PROFILE_EN                     1u           /* Include variables in OS_TCB for profiling                             */
#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_REG_TBL_SIZE                   0u           /* Number of task specific registers                                     */

#define OS_CFG_TASK_STK_REDZONE_EN                 1u           /* Enable (1) or Disable (0) stack redzone                               */
#define OS_CFG_TASK_STK_REDZONE_DEPTH              8u           /* Depth of the stack redzone                                            */

#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()                   */


                                                                /* ------------------ TASK LOCAL STORAGE MANAGEMENT -------------------  */
#define OS_CFG_TLS_TBL_SIZE                        0u           /* Include code for Task Local Storage (TLS) registers                   */


                                                                /* ------------------------- 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()              */


                                                                /* ------------------------- TRACE RECORDER ---------------------------- */
#define OS_CFG_TRACE_EN                            0u           /* Enable (1) or Disable (0) uC/OS-III Trace instrumentation             */
#define OS_CFG_TRACE_API_ENTER_EN                  0u           /* Enable (1) or Disable (0) uC/OS-III Trace API enter instrumentation   */
#define OS_CFG_TRACE_API_EXIT_EN                   0u           /* Enable (1) or Disable (0) uC/OS-III Trace API exit  instrumentation   */

#endif

  os_cfg_app.h 配置文件用于配置系统的应用配置,如空闲任务、任务统计任务、软件定时器任务的参数配置等。修改后的内容如下:

/*
*********************************************************************************************************
*                                              uC/OS-III
*                                        The Real-Time Kernel
*
*                    Copyright 2009-2021 Silicon Laboratories Inc. www.silabs.com
*
*                                 SPDX-License-Identifier: APACHE-2.0
*
*               This software is subject to an open source license and is distributed by
*                Silicon Laboratories Inc. pursuant to the terms of the Apache License,
*                    Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                               OS CONFIGURATION (APPLICATION SPECIFICS)
*
* Filename : os_cfg_app.h
* Version  : V3.08.01
*********************************************************************************************************
*/

#ifndef OS_CFG_APP_H
#define OS_CFG_APP_H

/*
**************************************************************************************************************************
*                                                      CONSTANTS
**************************************************************************************************************************
*/
                                                                /* ------------------ MISCELLANEOUS ------------------- */
                                                                /* Stack size of ISR stack (number of CPU_STK elements) */
#define  OS_CFG_ISR_STK_SIZE                             128u
                                                                /* Maximum number of messages                           */
#define  OS_CFG_MSG_POOL_SIZE                             32u
                                                                /* Stack limit position in percentage to empty          */
#define  OS_CFG_TASK_STK_LIMIT_PCT_EMPTY                  10u


                                                                /* -------------------- IDLE TASK --------------------- */
                                                                /* Stack size (number of CPU_STK elements)              */
#define  OS_CFG_IDLE_TASK_STK_SIZE                        64u


                                                                /* ------------------ STATISTIC TASK ------------------ */
                                                                /* Priority                                             */
#define  OS_CFG_STAT_TASK_PRIO  ((OS_PRIO)(OS_CFG_PRIO_MAX-2u))
                                                                /* Rate of execution (1 to 10 Hz)                       */
#define  OS_CFG_STAT_TASK_RATE_HZ                         10u
                                                                /* Stack size (number of CPU_STK elements)              */
#define  OS_CFG_STAT_TASK_STK_SIZE                       100u


                                                                /* ---------------------- TICKS ----------------------- */
                                                                /* Tick rate in Hertz (10 to 1000 Hz)                   */
#define  OS_CFG_TICK_RATE_HZ                            1000u


                                                                /* --------------------- TIMERS ----------------------- */
                                                                /* Priority of 'Timer Task'                             */
#define  OS_CFG_TMR_TASK_PRIO   ((OS_PRIO)(OS_CFG_PRIO_MAX-3u))
                                                                /* Stack size (number of CPU_STK elements)              */
#define  OS_CFG_TMR_TASK_STK_SIZE                        128u

                                                                /* DEPRECATED - Rate for timers (10 Hz Typ.)            */
                                                                /* The timer task now calculates its timeouts based     */
                                                                /* on the timers in the list. It no longer runs at a    */
                                                                /* static frequency.                                    */
                                                                /* This define is included for compatibility reasons.   */
                                                                /* It will determine the period of a timer tick.        */
                                                                /* We recommend setting it to OS_CFG_TICK_RATE_HZ       */
                                                                /* for new projects.                                    */
#define  OS_CFG_TMR_TASK_RATE_HZ                          10u


#endif
posted @ 2024-02-14 19:09  星光樱梦  阅读(4)  评论(0编辑  收藏  举报