【uC/OS-II】笔记2----移植

前言

ucosii的代码,可以分为两部分:与cpu无关的代码,与cpu有关。移植的主要工作就是修改与cpu有关的部分代码。

uC/OS-II的代码结构

分类 文件 说明
配置文件 os_config.h 操作系统配置文件
操作系统源文件 ucos_ii.h 操作系统头文件
os_core.c 内核
os_task.c 任务管理
os_time.c 时间管理
os_sem.c 信号量管理
os_mutex.c 互斥信号量管理
os_mbox.c 消息邮箱管理
os_q.c 消息队列管理
os_flg.c 事件标志组管理
os_mem.c 内存管理
os_tmr.c 定时器管理
移植文件 os_cpu.h 处理器相关头文件
os_cpu.c 处理器相关c文件
os_cpu_a.asm 处理器相关汇编文件

uC/OS-III系统文件结构

分类 文件 说明
配置文件        os_cfg.h                 全部是宏定义,配置着呢哥哥操作系统,例如是否使能信号量管理,是否使能任务删除 
os_cfg_app.h 任务堆栈的大小,任务优先级的定义
os_cfg_app.c 定义了一些可配置的常量
操作系统头文件        os.h 操作系统头文件,包括宏定义、数据类型定义、结构体定义和函数声明
os_type.h 数据类型的定义,如OS_CPU_USAGE
操作系统源文件 os_core.c 内核代码
os_task.c 任务管理代码
os_sem.c 信号量管理代码
os_mutex.c 互斥信号量管理代码
os_flg.c 事件标志组管理代码
os_q.c 消息队列管理代码,不单独提供消息邮箱
os_msg.c
os_time.c 时间管理代码 
os_mem.c 内存管理代码
os_tmr.c 操作系统软件定时代码
os_var.c 变量定义文件
os_stat.c 统计任务代码
os_pend,multi.c 任务等待多事件代码
os_prio.c 优先级管理代码
Os_int.c 中断管理代码
Os_app_hooks.c 钩子函数的独立文件,用户可以加入代码
Os_dbg.c 系统调试用到的代码
移植文件 os_cpu.h 移植代码头文件
os_cpu_a.s 移植代码汇编文件
os_cpu_c..c 移植代码c文件

需要修改的文件:

1、os_config.h

系统配置文件。系统是可裁剪的,可以通过修改里面的宏定义,裁剪掉一部分功能,节省资源。

2、os_cpu.h

/************************ (C) COPYLEFT 2010 Leafgrass *************************

* File Name        : os_cpu_c.c 
* Author        : Librae
* Date            : 06/10/2010
* Description    : ¦ÌCOS-IIÔÚSTM32ÉϵÄÒÆÖ²´úÂëCÓïÑÔ²¿·Ö£¬
*                  °üÀ¨ÈÎÎñ¶ÑÕ»³õʼ»¯´úÂëºÍ¹³×Óº¯ÊýµÈ

******************************************************************************/

#ifndef    __OS_CPU_H__
#define    __OS_CPU_H__

#ifdef  OS_CPU_GLOBALS
#define OS_CPU_EXT
#else
#define OS_CPU_EXT  extern
#endif

/******************************************************************************
*                    ¶¨ÒåÓë±àÒëÆ÷Î޹صÄÊý¾ÝÀàÐÍ
******************************************************************************/

typedef unsigned char  BOOLEAN;
typedef unsigned char  INT8U;            /* Unsigned  8 bit quantity       */
typedef signed   char  INT8S;            /* Signed    8 bit quantity       */
typedef unsigned short INT16U;            /* Unsigned 16 bit quantity       */
typedef signed   short INT16S;            /* Signed   16 bit quantity       */
typedef unsigned int   INT32U;            /* Unsigned 32 bit quantity       */
typedef signed   int   INT32S;            /* Signed   32 bit quantity       */
typedef float          FP32;            /* Single precision floating point*/
typedef double         FP64;            /* Double precision floating point*/

//STM32ÊÇ32λλ¿íµÄ,ÕâÀïOS_STKºÍOS_CPU_SR¶¼Ó¦¸ÃΪ32λÊý¾ÝÀàÐÍ
typedef unsigned int   OS_STK;            /* Each stack entry is 32-bit wide*/
typedef unsigned int   OS_CPU_SR;        /* Define size of CPU status register*/
/* 
*******************************************************************************
*                             Cortex M3
*                     Critical Section Management
*******************************************************************************
*/


/*
*******************************************************************************
*                          ARM Miscellaneous
*******************************************************************************
*/


//¶¨ÒåÕ»µÄÔö³¤·½Ïò.
//CM3ÖÐ,Õ»ÊÇÓɸߵØÖ·ÏòµÍµØÖ·Ôö³¤µÄ,ËùÒÔOS_STK_GROWTHÉèÖÃΪ1
#define  OS_STK_GROWTH        1      /* Stack grows from HIGH to LOW memory on ARM    */
//ÈÎÎñÇл»ºê,ÓÉ»ã±àʵÏÖ.
#define  OS_TASK_SW()         OSCtxSw()

/*
*******************************************************************************
*                               PROTOTYPES
*                           (see OS_CPU_A.ASM)
*******************************************************************************
*/
//OS_CRITICAL_METHOD = 1 :Ö±½ÓʹÓô¦ÀíÆ÷µÄ¿ª¹ØÖжÏÖ¸ÁîÀ´ÊµÏÖºê 
//OS_CRITICAL_METHOD = 2 :ÀûÓöÑÕ»±£´æºÍ»Ö¸´CPUµÄ״̬ 
//OS_CRITICAL_METHOD = 3 :ÀûÓñàÒëÆ÷À©Õ¹¹¦ÄÜ»ñµÃ³ÌÐò״̬×Ö£¬±£´æÔÚ¾Ö²¿±äÁ¿cpu_sr

#define  OS_CRITICAL_METHOD   3         //½øÈëÁÙ½ç¶ÎµÄ·½·¨

#if OS_CRITICAL_METHOD == 3
#define  OS_ENTER_CRITICAL()  {cpu_sr = OS_CPU_SR_Save();}
#define  OS_EXIT_CRITICAL()   {OS_CPU_SR_Restore(cpu_sr);}
#endif

void       OSCtxSw(void);
void       OSIntCtxSw(void);
void       OSStartHighRdy(void);

void       OSPendSV(void);

#if OS_CRITICAL_METHOD == 3u                      /* See OS_CPU_A.ASM                                  */
OS_CPU_SR  OS_CPU_SR_Save(void);
void       OS_CPU_SR_Restore(OS_CPU_SR cpu_sr);
#endif
OS_CPU_EXT INT32U OSInterrputSum;

#endif

/************************ (C) COPYLEFT 2010 Leafgrass ************************/
View Code

数据类型的定义:

/******************************************************************************
*                    ¶¨ÒåÓë±àÒëÆ÷Î޹صÄÊý¾ÝÀàÐÍ
******************************************************************************/

typedef unsigned char  BOOLEAN;
typedef unsigned char  INT8U;            /* Unsigned  8 bit quantity       */
typedef signed   char  INT8S;            /* Signed    8 bit quantity       */
typedef unsigned short INT16U;            /* Unsigned 16 bit quantity       */
typedef signed   short INT16S;            /* Signed   16 bit quantity       */
typedef unsigned int   INT32U;            /* Unsigned 32 bit quantity       */
typedef signed   int   INT32S;            /* Signed   32 bit quantity       */
typedef float          FP32;            /* Single precision floating point*/
typedef double         FP64;            /* Double precision floating point*/

//STM32ÊÇ32λλ¿íµÄ,ÕâÀïOS_STKºÍOS_CPU_SR¶¼Ó¦¸ÃΪ32λÊý¾ÝÀàÐÍ
typedef unsigned int   OS_STK;            /* Each stack entry is 32-bit wide*/
typedef unsigned int   OS_CPU_SR;        /* Define size of CPU status register*/
View Code

此文件是需要修改的重要内容,因为不同的硬件、开发工具等,同样的数据结构类型定义可能是不同的。

进入临界区的方法:

目的是为了配置在访问临界区时,关中断和恢复中断的方法。

/*
*******************************************************************************
*                               PROTOTYPES
*                           (see OS_CPU_A.ASM)
*******************************************************************************
*/
//OS_CRITICAL_METHOD = 1 :Ö±½ÓʹÓô¦ÀíÆ÷µÄ¿ª¹ØÖжÏÖ¸ÁîÀ´ÊµÏÖºê 
//OS_CRITICAL_METHOD = 2 :ÀûÓöÑÕ»±£´æºÍ»Ö¸´CPUµÄ״̬ 
//OS_CRITICAL_METHOD = 3 :ÀûÓñàÒëÆ÷À©Õ¹¹¦ÄÜ»ñµÃ³ÌÐò״̬×Ö£¬±£´æÔÚ¾Ö²¿±äÁ¿cpu_sr

#define  OS_CRITICAL_METHOD   3         //½øÈëÁÙ½ç¶ÎµÄ·½·¨

#if OS_CRITICAL_METHOD == 3
#define  OS_ENTER_CRITICAL()  {cpu_sr = OS_CPU_SR_Save();}
#define  OS_EXIT_CRITICAL()   {OS_CPU_SR_Restore(cpu_sr);}
#endif

void       OSCtxSw(void);
void       OSIntCtxSw(void);
void       OSStartHighRdy(void);

void       OSPendSV(void);

#if OS_CRITICAL_METHOD == 3u                      /* See OS_CPU_A.ASM                                  */
OS_CPU_SR  OS_CPU_SR_Save(void);
void       OS_CPU_SR_Restore(OS_CPU_SR cpu_sr);
#endif
OS_CPU_EXT INT32U OSInterrputSum;

#endif
View Code

3、os_cpu.c

此文件是移植中需要修改最多的。

/*
*********************************************************************************************************
*                                               uC/OS-II
*                                         The Real-Time Kernel
*
*
*                                (c) Copyright 2006, Micrium, Weston, FL
*                                          All Rights Reserved
*
*                                           ARM Cortex-M3 Port
*
* File      : OS_CPU_C.C
* Version   : V2.86
* By        : Jean J. Labrosse
*
* For       : ARMv7M Cortex-M3
* Mode      : Thumb2
* Toolchain : RealView Development Suite
*             RealView Microcontroller Development Kit (MDK)
*             ARM Developer Suite (ADS)
*             Keil uVision
*********************************************************************************************************
*/

#define  OS_CPU_GLOBALS

#include "includes.h"
/*
*********************************************************************************************************
*                                          LOCAL VARIABLES
*********************************************************************************************************
*/

#if OS_TMR_EN > 0
static  INT16U  OSTmrCtr;
#endif


/*
*********************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                            (BEGINNING)
*
* Description: This function is called by OSInit() at the beginning of OSInit().
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/
#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSInitHookBegin (void)
{
#if OS_TMR_EN > 0
    OSTmrCtr = 0;
#endif
}
#endif

/*
*********************************************************************************************************
*                                       OS INITIALIZATION HOOK
*                                               (END)
*
* Description: This function is called by OSInit() at the end of OSInit().
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts should be disabled during this call.
*********************************************************************************************************
*/
#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSInitHookEnd (void)
{
}
#endif

/*
*********************************************************************************************************
*                                          TASK CREATION HOOK
*
* Description: This function is called when a task is created.
*
* Arguments  : ptcb   is a pointer to the task control block of the task being created.
*
* Note(s)    : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
#if OS_CPU_HOOKS_EN > 0
void  OSTaskCreateHook (OS_TCB *ptcb)
{
#if OS_APP_HOOKS_EN > 0
    App_TaskCreateHook(ptcb);
#else
    (void)ptcb;                                  /* Prevent compiler warning                           */
#endif
}
#endif


/*
*********************************************************************************************************
*                                           TASK DELETION HOOK
*
* Description: This function is called when a task is deleted.
*
* Arguments  : ptcb   is a pointer to the task control block of the task being deleted.
*
* Note(s)    : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
#if OS_CPU_HOOKS_EN > 0
void  OSTaskDelHook (OS_TCB *ptcb)
{
#if OS_APP_HOOKS_EN > 0
    App_TaskDelHook(ptcb);
#else
    (void)ptcb;                                  /* Prevent compiler warning                           */
#endif
}
#endif

/*
*********************************************************************************************************
*                                             IDLE TASK HOOK
*
* Description: This function is called by the idle task.  This hook has been added to allow you to do
*              such things as STOP the CPU to conserve power.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts are enabled during this call.
*********************************************************************************************************
*/
#if OS_CPU_HOOKS_EN > 0 && OS_VERSION >= 251
void  OSTaskIdleHook (void)
{
#if OS_APP_HOOKS_EN > 0
    App_TaskIdleHook();
#endif
}
#endif

/*
*********************************************************************************************************
*                                           STATISTIC TASK HOOK
*
* Description: This function is called every second by uC/OS-II's statistics task.  This allows your
*              application to add functionality to the statistics task.
*
* Arguments  : none
*********************************************************************************************************
*/

#if OS_CPU_HOOKS_EN > 0
void  OSTaskStatHook (void)
{
#if OS_APP_HOOKS_EN > 0
    App_TaskStatHook();
#endif
}
#endif

/*
*********************************************************************************************************
*                                        INITIALIZE A TASK'S STACK
*
* Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
*              stack frame of the task being created.  This function is highly processor specific.
*
* Arguments  : task          is a pointer to the task code
*
*              p_arg         is a pointer to a user supplied data area that will be passed to the task
*                            when the task first executes.
*
*              ptos          is a pointer to the top of stack.  It is assumed that 'ptos' points to
*                            a 'free' entry on the task stack.  If OS_STK_GROWTH is set to 1 then
*                            'ptos' will contain the HIGHEST valid address of the stack.  Similarly, if
*                            OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
*                            of the stack.
*
*              opt           specifies options that can be used to alter the behavior of OSTaskStkInit().
*                            (see uCOS_II.H for OS_TASK_OPT_xxx).
*
* Returns    : Always returns the location of the new top-of-stack once the processor registers have
*              been placed on the stack in the proper order.
*
* Note(s)    : 1) Interrupts are enabled when your task starts executing.
*              2) All tasks run in Thread mode, using process stack.
*********************************************************************************************************
*/

OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
{
    OS_STK *stk;


    (void)opt;                                   /* 'opt' is not used, prevent warning                 */
    stk       = ptos;                            /* Load stack pointer                                 */

                                                 /* Registers stacked as if auto-saved on exception    */
    *(stk)    = (INT32U)0x01000000L;             /* xPSR                                               */
    *(--stk)  = (INT32U)task;                    /* Entry Point                                        */
    *(--stk)  = (INT32U)0xFFFFFFFEL;             /* R14 (LR) (init value will cause fault if ever used)*/
    *(--stk)  = (INT32U)0x12121212L;             /* R12                                                */
    *(--stk)  = (INT32U)0x03030303L;             /* R3                                                 */
    *(--stk)  = (INT32U)0x02020202L;             /* R2                                                 */
    *(--stk)  = (INT32U)0x01010101L;             /* R1                                                 */
    *(--stk)  = (INT32U)p_arg;                   /* R0 : argument                                      */

                                                 /* Remaining registers saved on process stack         */
    *(--stk)  = (INT32U)0x11111111L;             /* R11                                                */
    *(--stk)  = (INT32U)0x10101010L;             /* R10                                                */
    *(--stk)  = (INT32U)0x09090909L;             /* R9                                                 */
    *(--stk)  = (INT32U)0x08080808L;             /* R8                                                 */
    *(--stk)  = (INT32U)0x07070707L;             /* R7                                                 */
    *(--stk)  = (INT32U)0x06060606L;             /* R6                                                 */
    *(--stk)  = (INT32U)0x05050505L;             /* R5                                                 */
    *(--stk)  = (INT32U)0x04040404L;             /* R4                                                 */

    return (stk);
}

/*
*********************************************************************************************************
*                                           TASK SWITCH HOOK
*
* Description: This function is called when a task switch is performed.  This allows you to perform other
*              operations during a context switch.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts are disabled during this call.
*              2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
*                 will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
*                 task being switched out (i.e. the preempted task).
*********************************************************************************************************
*/
#if (OS_CPU_HOOKS_EN > 0) && (OS_TASK_SW_HOOK_EN > 0)
void  OSTaskSwHook (void)
{
#if OS_APP_HOOKS_EN > 0
    App_TaskSwHook();
#endif
}
#endif

/*
*********************************************************************************************************
*                                           OS_TCBInit() HOOK
*
* Description: This function is called by OS_TCBInit() after setting up most of the TCB.
*
* Arguments  : ptcb    is a pointer to the TCB of the task being created.
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSTCBInitHook (OS_TCB *ptcb)
{
#if OS_APP_HOOKS_EN > 0
    App_TCBInitHook(ptcb);
#else
    (void)ptcb;                                  /* Prevent compiler warning                           */
#endif
}
#endif


/*
*********************************************************************************************************
*                                               TICK HOOK
*
* Description: This function is called every tick.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
#if (OS_CPU_HOOKS_EN > 0) && (OS_TIME_TICK_HOOK_EN > 0)
void  OSTimeTickHook (void)
{
#if OS_APP_HOOKS_EN > 0
    App_TimeTickHook();
#endif

#if OS_TMR_EN > 0
    OSTmrCtr++;
    if (OSTmrCtr >= (OS_TICKS_PER_SEC / OS_TMR_CFG_TICKS_PER_SEC)) {
        OSTmrCtr = 0;
        OSTmrSignal();
    }
#endif
}
#endif

#if OS_CPU_HOOKS_EN > 0u && OS_VERSION > 290u 

void OSTaskReturnHook(OS_TCB *ptcb)
{ 
    (void)ptcb; 
} 

#endif






/*----------------------- (C) COPYRIGHT @ 2012 liycobl -----------------  end of file -----------------*/
View Code

OSTaskStkInit()

在创建任务时,对任务对战进行初始化,功能是将任务参数地址、任务函数入口地址、各cpu寄存器地址压入任务对战。

OSStartHighRdy()

在程序初次运行时,还没有任务运行,OSStart()将指针指向当前优先级最高的就绪状态的任务。此函数将OSRunning的值设置为真,然后将对战寄存器的值设置为该任务堆栈的地址,转到任务地址去执行。 

OSCtxSw()

非中断处理情况下的任务切换函数。使用OS_Sched()调用此函数。

/*生命如此美好。认真工作之余,不要忘了认真对待生活,认真对待身边人!*/
原文地址:https://www.cnblogs.com/isha2088/p/6882909.html