Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1887147
  • 博文数量: 496
  • 博客积分: 12043
  • 博客等级: 上将
  • 技术积分: 4778
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-27 14:26
文章分类

全部博文(496)

文章存档

2014年(8)

2013年(4)

2012年(181)

2011年(303)

2010年(3)

分类: 嵌入式

2011-06-28 14:16:31

uC/OSII移植步骤
1.    μC/OS-Ⅱ概述
  μC/OS-Ⅱ在特定处理器上的移植大部分工作集中在多任务切换的实现上,这部分代码主要用来保存和恢复处理器的现场。但许多操作如读/写寄存器不能用C语言而只能用汇编来实现。
  将μC/OS-Ⅱ移植到ARM处理器上,只需要修改与处理器相关的3个文件: OS_CPU.H, OS_CPU_C.C, OS_CPU_A.ASM 。
2.    OS_CPU.H的移植
 1) 数据类型的定义
typedef  unsigned  char   BOOLEAN;
typedef  unsigned  char   INT8U;
typedef  signed    char   INT8S;
typedef  unsigned  short  INT16U;
typedef  signed    short  INT16S;
typedef  unsigned  int    INT32U;
typedef  signed    int    INT32S;
typedef  float            FP32;
typedef  double           FP64;
typedef  unsigned  int    OS_STK;
typedef  unsigned  int    OS_CPU_SR;
 2) ARM处理器相关的宏定义
#define  OS_ENTER_CRITICAL()  ARMDisableINT
#define  OS_EXIT_CRITICAL()   ARMEnableINT
 3) 堆栈增长方向的定义
#define  OS_STK_GROWTH        1
3.    OS_CPU_C.C的移植
 1)  任务椎栈初始化
任务椎栈初始化函数由OSTaskCreat()或OSTaskCreatEXT()调用,用来初始化任务并返回新的堆栈指针STK.初始状态的堆栈模拟发生一次中断后的堆栈结构,在ARM体系结构下,任务堆栈空间由高到低将依次保存着PC,LR,R12…R0,CPSR,SPSR。堆栈初始化结束后,OSTaskSTKInit()返回新的堆栈栈顶指针OSTaskCreat()或OSTaskCreatEXT()将新的指针保存的OS_TCB中。
OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
{
    OS_STK *stk;
    opt    = opt;             
    stk    = ptos;                
    *stk   = (OS_STK)task;    
    *--stk = 0;               
    *--stk = 0;               
    *--stk = 0;              
    *--stk = 0;              
    *--stk = 0;              
    *--stk = 0;              
    *--stk = 0;              
    *--stk = 0;              
    *--stk = 0;               
    *--stk = 0;               
    *--stk = 0;               
    *--stk = 0;               
    *--stk = 0;               
    *--stk = unsigned int pdata;
    *--stk = USER_USING_MODE|0X00;
    *--stk = 0;              
    return (stk);
}
 2)  系统Hook()函数
这些函数在特定的系统动作时被调用,允许执行函数中的用户代码。这些函数默认是空函数,用户根据实际情况添加相关代码。
OSInitHookBegin()
OSInitHookEnd()
OSTaskCreateHook()
OSTaskDelHook()
OSTaskIdleHook()
OSTaskStatHook()
OSTaskStkInit()
OSTaskSwHook()
OSTCBInitHook()
OSTimeTickHook()
4.    OS_CPU_A.ASM的移植
1)    退出临界区和进入临界区代码
它们分别是退出临界区和进入临界区代码的宏实现,主要用于在进入临界区之前关闭中断,在退出临界区后恢复原来的中断状态。
ARMDisableINT
        MRS     R0,CPSR                     ; Set IRQ and FIQ bits in CPSR to disable all interrupts
        ORR     R1,R0,#NO_INT
        MSR     CPSR_c,R1
        MRS     R1,CPSR                     ; Confirm that CPSR contains the proper interrupt disable flags
        AND     R1,R1,#NO_INT
        CMP     R1,#NO_INT
        BNE     OS_CPU_SR_Save              ; Not properly disabled (try again)
        BX      LR                          ; Disabled, return the original CPSR contents in R0
ARMEnableINT
       MSR     CPSR_c,R0
        BX      LR
2)    任务级任务切换
任务级任务切换函数OS_TasK_Sw()是当前任务因为被阻塞而主动请求CPU高度时被执行的,由于此时的任务切换都是在非异常模式直进行的,因此区别于中断级别的任务切换。它的工作是先将当前任务的CPU现场保存到该任务的堆栈中,然后获得最高优先级任务的堆栈指针,从该堆栈中恢复此任务的CPU现场,使之继续运行,从而完成任务切换。
OSCtxSw
                                        ; SAVE CURRENT TASK'S CONTEXT
        STMFD   SP!, {LR}               ; Push return address
        STMFD   SP!, {LR}
        STMFD   SP!, {R0-R12}           ; Push registers
        MRS     R4,  CPSR               ; Push current CPSR
        TST     LR, #1                  ; See if called from Thumb mode
        ORRNE   R4,  R4, #0x20          ; If yes, Set the T-bit
        STMFD   SP!, {R4}
        LDR     R4, OS_TCBCur           ; OSTCBCur->OSTCBStkPtr = SP;
        LDR     R5, [R4]
        STR     SP, [R5]
        LDR     R0,  OS_TaskSwHook      ; OSTaskSwHook();
        MOV     LR, PC
        BX      R0
        LDR     R4,  OS_PrioCur         ; OSPrioCur = OSPrioHighRdy
        LDR     R5,  OS_PrioHighRdy
        LDRB    R6, [R5]
        STRB    R6, [R4]
        LDR     R4, OS_TCBCur           ; OSTCBCur  = OSTCBHighRdy;
        LDR     R6, OS_TCBHighRdy
        LDR     R6, [R6]
        STR     R6, [R4]
        LDR     SP, [R6]                ; SP = OSTCBHighRdy->OSTCBStkPtr;

                                        ;STORE NEW TASK'S CONTEXT
        LDMFD   SP!, {R4}               ; Pop new task's CPSR
        MSR     SPSR_cxsf, R4
        LDMFD   SP!, {R0-R12,LR,PC}^    ; Pop new task's context
 3)  中断级任务切换函数
  ① 该函数由OSIntExit()和O***IntExit()调用,它若在时钟中断ISR中发现有高优先级任务等特的时候信号到来,则需要在中断退出后并不返回被中断的,的而是直接调度就绪的高高优先级任务执行.这样做的目的主要是能够尽快的让优先级高的任务得到响应,进而保证系统的实时性。
OSIntCtxSw
        LDR     R0, OS_TaskSwHook      ; OSTaskSwHook();
        MOV     LR, PC
        BX      R0
        LDR     R4, OS_PrioCur          ; OSPrioCur = OSPrioHighRdy
        LDR     R5, OS_PrioHighRdy
        LDRB    R6,[R5]
        STRB    R6,[R4]        
        LDR     R4,OS_TCBCur            ; OSTCBCur  = OSTCBHighRdy;
        LDR     R6,OS_TCBHighRdy
        LDR     R6,[R6]
        STR     R6,[R4]
        LDR     SP,[R6]                 ; SP = OSTCBHighRdy->OSTCBStkPtr;
                                        ; RESTORE NEW TASK'S CONTEXT
        LDMFD   SP!, {R4}               ; Pop new task's CPSR
        MSR     SPSR_cxsf, R4
        LDMFD   SP!, {R0-R12,LR,PC}^    ; Pop new task's context
 ② 两种形式的中断程序
OS_CPU_IRQ_ISR
        STMFD   SP!, {R1-R3}                   ; PUSH WORKING REGISTERS ONTO IRQ STACK
        MOV     R1, SP                         ; Save   IRQ stack pointer
        ADD     SP, SP,#12                     ; Adjust IRQ stack pointer
        SUB     R2, LR,#4                      ; Adjust PC for return address to task
        MRS     R3, SPSR                       ; Copy SPSR (i.e. interrupted task's CPSR) to R3
        MSR     CPSR_c, #(NO_INT | SVC32_MODE) ; Change to SVC mode
                                               ; SAVE TASK'S CONTEXT ONTO TASK'S STACK
        STMFD   SP!, {R2}                      ; Push task's Return PC
        STMFD   SP!, {LR}                      ; Push task's LR
        STMFD   SP!, {R4-R12}                  ; Push task's R12-R4
        LDMFD   R1!, {R4-R6}                   ; Move task's R1-R3 from IRQ stack to SVC stack
        STMFD   SP!, {R4-R6}
        STMFD   SP!, {R0}                      ; Push task's R0    onto task's stack
        STMFD   SP!, {R3}                      ; Push task's CPSR (i.e. IRQ's SPSR)                                             
        LDR     R0,   OS_IntNesting            ; OSIntNesting++;
        LDRB    R1, [R0]
        ADD     R1, R1,#1
        STRB    R1, [R0]
        CMP     R1, #1                         ; if (OSIntNesting == 1) {
        BNE     OS_CPU_IRQ_ISR_1
        LDR     R4,  OS_TCBCur                 ; OSTCBCur->OSTCBStkPtr = SP
        LDR     R5, [R4]
        STR     SP, [R5]                       ; }
OS_CPU_IRQ_ISR_1
        MSR     CPSR_c, #(NO_INT | IRQ32_MODE) ; Change to IRQ mode (to use the IRQ stack to handle interrupt)
        LDR     R0,  OS_CPU_IRQ_ISR_Handler    ; OS_CPU_IRQ_ISR_Handler();
        MOV     LR, PC
        BX      R0         
        MSR     CPSR_c, #(NO_INT | SVC32_MODE) ; Change to SVC mode
        LDR     R0,  OS_IntExit                ; OSIntExit();
        MOV     LR, PC
        BX      R0                             ; RESTORE NEW TASK'S CONTEXT
        LDMFD   SP!, {R4}                      ; Pop new task's CPSR
        MSR     SPSR_cxsf, R4
        LDMFD   SP!, {R0-R12,LR,PC}^           ; Pop new task's context
        RSEG CODE:CODE:NOROOT(2)
        CODE32
OS_CPU_FIQ_ISR
        STMFD   SP!, {R1-R3}                   ; PUSH WORKING REGISTERS ONTO FIQ STACK
        MOV     R1, SP                         ; Save   FIQ stack pointer
        ADD     SP, SP,#12                     ; Adjust FIQ stack pointer
        SUB     R2, LR,#4                      ; Adjust PC for return address to task
        MRS     R3, SPSR                       ; Copy SPSR (i.e. interrupted task's CPSR) to R3
        MSR     CPSR_c, #(NO_INT | SVC32_MODE) ; Change to SVC mode
                                               ; SAVE TASK'S CONTEXT ONTO TASK'S STACK
        STMFD   SP!, {R2}                      ; Push task's Return PC
        STMFD   SP!, {LR}                      ; Push task's LR
        STMFD   SP!, {R4-R12}                  ; Push task's R12-R4
        LDMFD   R1!, {R4-R6}                   ; Move task's R1-R3 from FIQ stack to SVC stack
        STMFD   SP!, {R4-R6}
        STMFD   SP!, {R0}                      ; Push task's R0    onto task's stack
        STMFD   SP!, {R3}                      ; Push task's CPSR (i.e. FIQ's SPSR)
                                               ; HANDLE NESTING COUNTER
        LDR     R0, OS_IntNesting              ; OSIntNesting++;
        LDRB    R1, [R0]
        ADD     R1, R1,#1
        STRB    R1, [R0]
        CMP     R1, #1                         ; if (OSIntNesting == 1){
        BNE     OS_CPU_FIQ_ISR_1
        LDR     R4, OS_TCBCur                  ; OSTCBCur->OSTCBStkPtr = SP
        LDR     R5, [R4]
        STR     SP, [R5]                       ; }
OS_CPU_FIQ_ISR_1
        MSR     CPSR_c, #(NO_INT | FIQ32_MODE) ; Change to FIQ mode (to use the FIQ stack to handle interrupt)
        LDR     R0, ??OS_CPU_FIQ_ISR_Handler   ; OS_CPU_FIQ_ISR_Handler();
        MOV     LR, PC
        BX      R0
        MSR     CPSR_c, #(NO_INT | SVC32_MODE) ; Change to SVC mode
        LDR     R0,  OS_IntExit                 ; OSIntExit();
        MOV     LR, PC
        BX      R0                             ; RESTORE NEW TASK'S CONTEXT
        LDMFD   SP!, {R4}                      ; Pop new task's CPSR
        MSR     SPSR_cxsf, R4
        LDMFD   SP!, {R0-R12,LR,PC}^           ; Pop new task's context
 4) OSStartHighRdy()函数
该函数是在OSStart()多任务启动后,负责从最高优先级任务的TCB控制块中获得该任务的堆栈指针SP通过SP依次将CPU现场恢复。这时系统就将控制权交给用户创建的该任务进程,直到该任务被阻塞或者被更高优先级的任务抢占CPU。该函数仅仅在多任务启动时被执行一次,用来启动第一个也即最高优先级任务。
OSStartHighRdy  
        MSR     CPSR_cxsf, #0xD3        ; Switch to SVC mode with IRQ and FIQ disabled
        LDR     R0, ??OS_TaskSwHook     ; OSTaskSwHook();
        MOV     LR, PC
        BX      R0
        LDR     R4,  OS_Running         ; OSRunning = TRUE
        MOV     R5, #1
        STRB    R5, [R4]
                                        ; SWITCH TO HIGHEST PRIORITY TASK
        LDR     R4,  OS_TCBHighRdy      ;    Get highest priority task TCB address
        LDR     R4, [R4]                ;    get stack pointer
        LDR     SP, [R4]                ;    switch to the new stack
        LDR     R4,  [SP], #4           ;    pop new task's CPSR
        MSR     SPSR_cxsf,R4
        LDMFD   SP!, {R0-R12,LR,PC}^    ;    pop new task's context     
2.  多任务应用程序的编写
 1)  C语言入口函数
函数Main()为C语言入口函数,所有C程序从这里开始运行,在该函数中进行如下操作:
③    调用函数ARMTaskgetInit初始化ARM处理器
④    调用OSInit初始化系统
⑤    调用OSTaskCreat函数创建任务:Task1和Task2
⑥    调用ARMTaskgetStart函数启动时钟节拍中断
⑦    调用OSStart启动系统任务调度
#i nclude “config.h”
OS_STK  TaskStartStk[TASK_STK_SIZE];
OS_STK  TaskStk[TASK_STK_SIZE];
int Main(void){
    OSInit();
    OSTaskCreate(Task1,(void*)0,&TaskStartStk[TASK_STK_SIZE-1],0);
    OSStart();
    return();
}
 2) 任务处理函数
①   Task1
void Task1(void *pdata){
    pdata=pdata;
    TargetInit();
    For(;;){
        OSTimeDly(OS_TICKS_PER_SEC/50);
        If(GetKey()!=KEY1)    {
            continue;
        }
        OSTaskCreate(Task2,(void *)0,&TaskStk[TASK_STK_SIZE-1],10);
        While(GetKey()!=0)    {
            OSTimeDly(OS_TICKS_PER_SEC/50);
}
}
}
②   Task2
void Task2(void *pdata){
        pdata=pdata;
        BeeMoo();
OSTimeDly(OS_TICKS_PER_SEC/8);
        BeeMoo();
OSTimeDly(OS_TICKS_PER_SEC/4);
        BeeMoo();
OSTimeDly(OS_TICKS_PER_SEC/8);
        OSTaskDel(OS_PRIO_SELF);
}
阅读(1269) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~