ucosii笔记1:移植

Posted Phil的博客园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ucosii笔记1:移植相关的知识,希望对你有一定的参考价值。

前言

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

ucosii的代码结构

与cpu无关的代码              与cpu无关的代码
os_config.h 操作系统配置文件 os_cpu.h 处理器相关头文件
ucos_ii.h 操作系统头文件 os_cpu.c 处理器相关c文件
os_core.c 内核 os_cpu_a.asm 处理器相关汇编文件
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 定时器管理    

 

 

 

 

 

 

 

 

 

 

需要修改的文件:

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é?μ?ò??2′ú??Có???2?·?£?
*                  °üà¨è???????3?ê??ˉ′ú??oí13×óoˉêyμè

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

#ifndef    __OS_CPU_H__
#define    __OS_CPU_H__

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

/******************************************************************************
*                    ?¨ò?ó?±àò??÷?T1?μ?êy?YààDí
******************************************************************************/

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?????íμ?,?aà?OS_STKoíOS_CPU_SR??ó|???a32??êy?YààDí
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
*******************************************************************************
*/


//?¨ò???μ???3¤·??ò.
//CM3?D,??ê?óé??μ??·?òμíμ??·??3¤μ?,?ùò?OS_STK_GROWTHéè???a1
#define  OS_STK_GROWTH        1      /* Stack grows from HIGH to LOW memory on ARM    */
//è????D??oê,óé??±àêμ??.
#define  OS_TASK_SW()         OSCtxSw()

/*
*******************************************************************************
*                               PROTOTYPES
*                           (see OS_CPU_A.ASM)
*******************************************************************************
*/
//OS_CRITICAL_METHOD = 1 :?±?óê1ó?′|àí?÷μ??a1??D????á?à′êμ??oê 
//OS_CRITICAL_METHOD = 2 :à?ó?????±£′?oí???′CPUμ?×′ì? 
//OS_CRITICAL_METHOD = 3 :à?ó?±àò??÷à??11|?ü??μ?3ìDò×′ì?×?£?±£′??ú??2?±?á?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

数据类型的定义:

技术分享
/******************************************************************************
*                    ?¨ò?ó?±àò??÷?T1?μ?êy?YààDí
******************************************************************************/

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?????íμ?,?aà?OS_STKoíOS_CPU_SR??ó|???a32??êy?YààDí
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 :?±?óê1ó?′|àí?÷μ??a1??D????á?à′êμ??oê 
//OS_CRITICAL_METHOD = 2 :à?ó?????±£′?oí???′CPUμ?×′ì? 
//OS_CRITICAL_METHOD = 3 :à?ó?±àò??÷à??11|?ü??μ?3ìDò×′ì?×?£?±£′??ú??2?±?á?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的值设置为真,然后将对战寄存器的值设置为该任务堆栈的地址,转到任务地址去执行。 

4、os_cpu_a.asm

 

以上是关于ucosii笔记1:移植的主要内容,如果未能解决你的问题,请参考以下文章

STM32开发 -- UCOSII移植

STM32开发 -- UCOSII移植

STM32开发 -- UCOSII移植

STM32开发 -- UCOSII移植

想在STM32上移植UCOSII和UCGUI,需要多大的空间资源

UCOSII框架