⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucos8051.txt

📁 ucos for c8051的移植
💻 TXT
📖 第 1 页 / 共 2 页
字号:
    
 
  
            
[经验代码]->[RTOS(UC/OS)]->uCOS-II的移植代码 for 8051 [绝对原著]  
 
  
uCOS-II的移植代码 for 8051 [绝对原著] 
-- 我们的技术是你的!www.icwin.net


uCOS-II的移植代码 for 8051 [绝对原著] 
www.icwin.net 
作者:电子牛 文章来源:本站原创 点击数: 更新时间:2005-5-18 

移植说明:

我没有移植uCOS前,再网上下了一些案例可是大部分都不能直接运行,而且有好多还都有错误。

还发现很多"移植者"是忠实的操洗者,并且还操错了。这个移植代码是我领会不少代码的长短根据自己的经验编制,绝对原著!!!因为有好多与大多数移植代码不同之处,故帖出来共享,希望能对将要移植uCOS的人以启发和帮助。

由于我个人时间不多,所以没有写什么移植的"前因后果"

但:
移植的详细细节在程序中的注释部分已经非常详尽了,这里就不多说了。
如果那位大虾,能更有好的建议,请跟我联系,不胜感激!

也可以跟我索要全部移植代码,我将给你一个keil工程,你只需电击一下编译按扭,在软件仿真运行即可在串口1窗口里看到uCOS-II的风采!(不需任何编程,当然除了你以后要添加应用任务)

注意不要将本移植代码应用于商业!!!

如果那位大虾,能更有好的建议,请跟我联系,不胜感激!

//-- 我们的技术是你的!www.icwin.net

QQ: 75011221 呢称:电子牛
EMAIL: niuyimail@126.com


OS_CPU_C.C

/*
********************************************************************************
*************************
* 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
*
* os_pdata 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_???).
*
* 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) : Interrupts are enabled when your task starts executing. You can 
change this by setting the
* PSW to 0x0002 instead. In this case, interrupts would be 
disabled upon task startup. The
* application code would be responsible for enabling interrupts at 
the beginning of the task
* code. You will need to modify OSTaskIdle() and OSTaskStat() so 
that they enable 
* interrupts. Failure to do this will make your system crash!
********************************************************************************
*************************
*/
/*
at89c55wd PDL(sp form LOW to HIGH): (堆栈指针所指的栈顶数据为有效数
据)
装入堆栈指
针ptos到stk

stk--> 入栈:task底\高 
(高内存)
入 || 入栈:ACC
栈 || 入栈:B
顺 || 入栈:DPH 
//暂时忽略第二指针
序 || 入栈:DPL
| \/ | 入栈:PSW
| | 入栈:R7-
R4
|______| 入栈:os_pdata(R3,R2,R1)用寄存
器传递os_pdata
入栈:R0 
(底内存)

// 入栈:
DPS?? //双数据指针选择寄存器??
// 入栈:PC
自己应该参照编译器定义入栈/出栈顺序,写出来备以后查阅
*/
OS_STK *OSTaskStkInit (void (*task)(void *pd)LG_REENTRANT, void *os_pdata, 
OS_STK *ptos, INT16U opt) LG_REENTRANT
{
//INT16U *stk;
OS_STK *stk;

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

//装入堆栈指针ptos到stk

*stk = 0; 
//入栈:R0
stk -= sizeof(void *);
*(void **)stk = (void *)os_pdata; //用寄存器传递
os_pdata
*--stk = 4; 
//入栈:R4-R7
*--stk = 5;
*--stk = 6;
*--stk = 7; 


*--stk = PSW; 
//入栈:PSW
*--stk = 'L'; 
//入栈:DPL
*--stk = 'H'; 
//入栈:DPH //暂时忽略第二指针
*--stk = 'B'; 
//入栈:B
*--stk = 'A'; 
//入栈:ACC

*--stk = ((INT16U)task & 0xff00) >> 8; //保存任务地址
*--stk = (INT16U)task & 0x00ff;

*--stk = 15; 
//入栈个数

//END
return ((OS_STK *)stk);
}

/*$PAGE*/
/*
********************************************************************************
*************************
* INITIALIZE A TASK'S STACK FOR FLOATING POINT EMULATION
*
********************************************************************************
*************************
*/

/*$PAGE*/
//void OSTaskStkInit_FPE_x86 (OS_STK **pptos, OS_STK **ppbos, INT32U *psize)
//{
//}

/*$PAGE*/
/*
********************************************************************************
*************************
* 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 
void OSTaskSwHook (void) LG_REENTRANT
{
}
#endif

/*
********************************************************************************
*************************
* OSTCBInit() 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) LG_REENTRANT
{
ptcb = ptcb; /* Prevent Compiler 
warning */
}
#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 
void OSTimeTickHook (void) LG_REENTRANT
{
}
#endif


/*
********************************************************************************
*************************
* 
中断服务程序外挂
* NOTE : 注意:所有中断外挂都在临界区之外,用户可以自行控制进/退
临界区
********************************************************************************
*************************
*/
#if OS_ISR_T1_EN > 0
void OSISR_T1HOOK (void) LG_REENTRANT
{

/* 请在这里输入中断服务程序 
*/

}
#endif

#if OS_ISR_INT0_EN > 0
void OSISR_INT0HOOK (void) LG_REENTRANT
{

/* 请在这里输入中断服务程序 
*/

}
#endif

#if OS_ISR_INT1_EN > 0
void OSISR_INT1HOOK (void) LG_REENTRANT
{

/* 请在这里输入中断服务程序 
*/

}
#endif

#if OS_ISR_S0_EN > 0
void OSISR_S0HOOK (void) LG_REENTRANT
{

/* 请在这里输入中断服务程序 
*/



}
#endif

----------------

;*******************************************************************************
*************************
; uC/OS-II
; The Real-Time Kernel
;
; (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
; All Rights Reserved
;
;
; at89c55wd Specific code
; LARGE MEMORY MODEL
;
; keil C/C++ V7.2
; (at89c55wd Compatible Target)
;
; File : OS_CPU_A.ASM
; By : Jean J. Labrosse
; Port by : 牛毅 2005-04-25->2005-04-28 
QQ:75011221 niuyimail@126.com
; 修补 : 2005-05-02-01:30 (增加了中断管理和临界区宏)
; 修补 : 2005-05-16-13:00 (修补_CopySPtoC_XBP的漏洞, 有标记
处%%%%????%%%%%%)
;*******************************************************************************
*************************
#include "OS_CFG.H"

NAME OS_CPU_A

;*******************************************************************************
*************************
; 要使用的外部函数和公共数据
;*******************************************************************************
*************************
EXTRN CODE (_?OSIntExit)
EXTRN CODE (_?OSTimeTick)
EXTRN CODE (_?OSTaskSwHook)

IF OS_ISR_T1_EN <> 0
EXTRN CODE (_?OSISR_T1HOOK)
ENDIF
IF OS_ISR_INT0_EN <> 0
EXTRN CODE (_?OSISR_INT0HOOK)
ENDIF
IF OS_ISR_INT1_EN <> 0
EXTRN CODE (_?OSISR_INT1HOOK)
ENDIF
IF OS_ISR_S0_EN <> 0
EXTRN CODE (_?OSISR_S0HOOK)
ENDIF

EXTRN DATA (?C_XBP)
;DT?C_XBP SEGMENT DATA
; RSEG DT?C_XBP
;?C_XBP:
;C_XBP:
; DS 1
?STACK SEGMENT IDATA
RSEG ?STACK
;?STACK:
Stack:
DS 40 

EXTRN BIT (OSRunning)
EXTRN XDATA (OSIntNesting)
; EXTRN XDATA (OSTickDOSCtr)
EXTRN XDATA (OSPrioHighRdy)
EXTRN XDATA (OSPrioCur)
EXTRN XDATA (OSTCBCur) 
EXTRN XDATA (OSTCBHighRdy)

;*******************************************************************************
*************************
; MACRO DEFINE
;*******************************************************************************
*************************
PUSHALL MACRO
;PUSH PC 
PUSH ACC
PUSH B
PUSH DPH
PUSH DPL
PUSH PSW
IRP REG, <R7, R6, R5, R4, R3, R2, R1, R0>
MOV A, REG
PUSH ACC
ENDM
ENDM

POPALL MACRO
IRP REG, <R0, R1, R2, R3, R4, R5, R6, R7>
POP ACC
MOV REG, A
ENDM
POP PSW
POP DPL
POP DPH
POP B
POP ACC
;RET/RETI
ENDM

OS_ENTER_CRITICAL MACRO
MOV B,IE ;因为所有程序没有使用
B寄存器,所以使用了B寄存器作为IE暂存
CLR EA
ENDM
OS_EXIT_CRITICAL MACRO
MOV IE,B ;因为所有程序没有使用
B寄存器,所以使用了B寄存器作为IE暂存
ENDM
;*******************************************************************************
*************************
; 供外部使用的函数
;*******************************************************************************
*************************
PUBLIC _?OSTickISR
PUBLIC _?OSStartHighRdy
PUBLIC _?OSCtxSw
PUBLIC _?OSIntCtxSw
/*$PAGE*/
;*******************************************************************************
*************************
; 函数体段
;*******************************************************************************
*************************
;*******************************************************************************
**************************
; 用户中断程序区 之 定时器中断
1 OSISR_T1()
;*******************************************************************************
**************************
IF OS_ISR_T1_EN <> 0
CSEG AT 001BH ;设置T0中断向量
LJMP _OSISR_T1

?PR?_OSISR_T1?OS_CPU_A SEGMENT CODE
RSEG ?PR?_OSISR_T1?OS_CPU_A
_OSISR_T1:
OS_ENTER_CRITICAL
;(1)保存处理器寄存器 先PUSH到SP在COPY到?C_XBP
PUSHALL
LCALL _CopySPtoC_XBP ;COPY SP 的内容到?
C_XBP

;(2)记忆中断的嵌套层OSIntNesting++ 进入中断
;(3) if (OSIntNesting == 1)
; OSTCBCur->OSTCBStkPtr <= ?C_XBP
LCALL _Enter_INT
OS_EXIT_CRITICAL 

;(4)调用中断服务外挂
LCALL _?OSISR_T1HOOK ;在这里进行中断服务

OS_ENTER_CRITICAL
;(5) Call OSTimeTick();
LCALL _?OSTimeTick

;(6) Call OSIntExit();
LCALL _?OSIntExit

;(7) 恢复处理器寄存器 ;没有高优先级任务在就
绪队列中
;LCALL _ComCtxSw2
LCALL _CopyC_XBPtoSP
POPALL

OS_EXIT_CRITICAL
RETI
ENDIF
;*******************************************************************************
**************************
; 用户中断程序区 之 外部中断0 
OSISR_INT0()
;*******************************************************************************
**************************
IF OS_ISR_INT0_EN <> 0
CSEG AT 0003H ;设置T0中断向量
LJMP _OSISR_INT0

?PR?_OSISR_INT0?OS_CPU_A SEGMENT CODE
RSEG ?PR?_OSISR_INT0?OS_CPU_A
_OSISR_INT0:
OS_ENTER_CRITICAL
;(1)保存处理器寄存器 先PUSH到SP在COPY到?C_XBP
PUSHALL
LCALL _CopySPtoC_XBP ;COPY SP 的内容到?
C_XBP

;(2)记忆中断的嵌套层OSIntNesting++ 进入中断
;(3) if (OSIntNesting == 1)
; OSTCBCur->OSTCBStkPtr <= ?C_XBP
LCALL _Enter_INT 
OS_EXIT_CRITICAL

;(4)调用中断服务外挂
LCALL _?OSISR_INT0HOOK ;在这里进行中断服务

OS_ENTER_CRITICAL
;(5) Call OSTimeTick();
LCALL _?OSTimeTick

;(6) Call OSIntExit();
LCALL _?OSIntExit

;(7) 恢复处理器寄存器 ;没有高优先级任务在就绪队列中
;LCALL _ComCtxSw2
LCALL _CopyC_XBPtoSP
POPALL
OS_EXIT_CRITICAL
RETI
ENDIF
;*******************************************************************************
**************************
; 用户中断程序区 之 外部中断1 
OSISR_INT1()
;*******************************************************************************
**************************
IF OS_ISR_INT1_EN <> 0
CSEG AT 0013H ;设置T0中断向量
LJMP _OSISR_INT1

?PR?_OSISR_INT1?OS_CPU_A SEGMENT CODE
RSEG ?PR?_OSISR_INT1?OS_CPU_A
_OSISR_INT1:
OS_ENTER_CRITICAL
;(1)保存处理器寄存器 先PUSH到SP在COPY到?C_XBP
PUSHALL
LCALL _CopySPtoC_XBP ;COPY SP 的内容到?
C_XBP

;(2)记忆中断的嵌套层OSIntNesting++ 进入中断
;(3) if (OSIntNesting == 1)
; OSTCBCur->OSTCBStkPtr <= ?C_XBP
LCALL _Enter_INT
OS_EXIT_CRITICAL

;(4)调用中断服务外挂
LCALL _?OSISR_INT1HOOK ;在这里进行中断服务

OS_ENTER_CRITICAL
;(5) Call OSTimeTick();
LCALL _?OSTimeTick

;(6) Call OSIntExit();
LCALL _?OSIntExit

;(7) 恢复处理器寄存器 ;没有高优先级任务在就绪队列中
;LCALL _ComCtxSw2
LCALL _CopyC_XBPtoSP
POPALL
OS_EXIT_CRITICAL

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -