1. 程式人生 > >【ucosii】筆記1:移植

【ucosii】筆記1:移植

err color border 工作 mrc pro read cfg mut

前言

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:移植