Small RTOS OSSched 函数解析
变量解析:
OSTaskRuning:
初始值:对应的比特位为0 ,表示该任务 休眠,
对应的比特位为1 ,表示该任务 准备执行
uint8 OSTaskRuning = 0xff;
OSTaskID :
初始值为0 当前运行的任务的序号
uint8 data OSTaskID = 0;
OSMapTbl[ ]:
一些常数: bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7
uint8 const OSMapTbl[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00};
OSIntNesting
uint8 OSIntNesting;
OSNextTaskID 等待运行的下一个 要切换的任务的ID
uint8 data OSNextTaskID = 0;
OSWaitTick[ ]
uint8 OSWaitTick[OS_MAX_TASKS];
OS_TASK_SW()
#define OS_TASK_SW() OSCtxSw() /* 任务切换函数 */
Os_Enter_Sum
uint8 data Os_Enter_Sum = 0;
OSFastSwap
uint8 data OSFastSwap = 0xff; /* 任务是否可以快速切换 */
#if EN_OS_INT_ENTER >0
#define OS_INT_ENTER() OSIntNesting++,EA=1
#endif
#define OS_ENTER_CRITICAL() EA = 0,Os_Enter_Sum++ /* 禁止中断 */
#define OS_EXIT_CRITICAL() if (--Os_Enter_Sum==0) EA = 1 /* 允许中断 */
/* 查找处于就绪状态的任务中优先级最高的任务 */
然后进入切换:
#define OS_TASK_SW() OSCtxSw() /* 任务切换函数 */
OSCtxSw()
LJMP C_OSCtxSw
C:0138H PUBLIC _OSWait
OSCtxSw
RSEG ?PR?OSCtxSw?OS_CPU_A
OSCtxSw:
USING 0
;设置标志:任务再次恢复运行时不必恢复所有寄存器
MOV DPTR,#OSMapTbl
MOV A,OSTaskID
#if OS_MAX_TASKS < 9
MOVC A,@A+DPTR // A = OSMapTbl[OSTaskID]
ORL A,OSFastSwap // A = A|0xFF
MOV OSFastSwap,A // OSFastSwap = A
#else
CLR C
SUBB A,#8
JC OSCtxSw_1
MOVC A,@A+DPTR
ORL A,OSFastSwap
MOV OSFastSwap,A
LJMP C_OSCtxSw
OSCtxSw_1:
MOV A,OSTaskID
MOVC A,@A+DPTR
ORL A,OSFastSwap+1
MOV OSFastSwap+1,A
#endif
LJMP C_OSCtxSw
C_OSCtxSw
RSEG ?PR?C_OSCtxSw?OS_CPU_C
C_OSCtxSw:
PUSH Os_Enter_Sum ;保存关中断计数器 ;首先SP =SP+1 ,然后 赋值到 SP地址
mov r2,sp ; SP寄存器的内容是一个数字,该数字表示一个地址
; cp1 = (unsigned char idata *)SP +1;
MOV R0,SP
IF EN_SP2 <> 0
mov sp,#(Sp2-1) ;堆栈指向临时空间,允许“软非屏蔽中断”
ENDIF
INC R0 ; 数字加1
; temp = (unsigned char )OSTsakStackBotton[OSNextTaskID+1];
MOV A,#LOW (OSTsakStackBotton+01H) ; OSTsakStackBotton = 0x10 ,A=0x11
ADD A,OSNextTaskID ; A = OSTsakStackBotton+01H + OSNextTaskID
MOV R1,A ; R1 = OSTsakStackBotton+01H + OSNextTaskID
MOV A,@R1 ;A = OSTsakStackBotton+01H + OSNextTaskID 这个地址存放的值
MOV R7,A ; R7 = OSTsakStackBotton+01H + OSNextTaskID 这个地址存放的值
; cp2 = OSTsakStackBotton[OSTaskID+1];
MOV A,#LOW (OSTsakStackBotton+01H); OSTsakStackBotton = 0x10 ,A=0x11
ADD A,OSTaskID ; A = OSTsakStackBotton+01H + OSTaskID
MOV R1,A ; R1 = OSTsakStackBotton+01H + OSTaskID
MOV A,@R1 ; A = OSTsakStackBotton+01H + OSTaskID 这个地址存放的值
MOV R1,A ;R1 = OSTsakStackBotton+01H + OSTaskID 这个地址存放的值
; if( OSNextTaskID > OSTaskID)
MOV A,OSNextTaskID ; A = OSNextTaskID
SETB C ; 进位位设置为1
SUBB A,OSTaskID ; A = OSNextTaskID- OSTaskID - Cy
JC ?C0001 ; 如果进位位 = 1 就跳转
; {
; while(cp2 != (unsigned char idata *)temp)
; {
; *cp1++ = *cp2++;
; }
MOV A,R7 ;A = R7 = OSTsakStackBotton+01H + OSNextTaskID 这个地址存放的值
CLR C ; 清进位位
SUBB A,R1 ; A = A -R1 -Cy; A= OSTsakStackBotton+01H + OSNextTaskID 这个地址存放的值
减去
OSTsakStackBotton+01H + OSTaskID 这个地址存放的值
MOV R6,A ; R6 = 上面的差值
?C0002:
MOV A,@R1 ;A7 = OSTsakStackBotton+01H + OSTaskID 这个地址存放的值 对应的地址的值
MOV @R0,A ;R0存放的数字对应的地址 赋值为 A7,也就是 SP 指向的 Stack存入 A7那个值
INC R0 ;R0 = R0 +1
INC R1 ;R1 = R1 +1
DJNZ R6,?C0002 ; SP赋值 多次
?C0003:
; temp = OSTsakStackBotton[OSTaskID+1] - (unsigned char idata *)SP-1;
MOV A,#LOW (OSTsakStackBotton+1) ;A = OSTsakStackBotton+01H
ADD A,OSTaskID ; A = OSTsakStackBotton + 01H + OSTaskID
MOV R1,A ; R1 = OSTsakStackBotton + 01H + OSTaskID
MOV A,@R1 ;A = OSTsakStackBotton + 01H + OSTaskID地址存放的值
SETB C ;进位位设置为1
;SUBB A,sp
SUBB A,r2 ; A =( OSTsakStackBotton + 01H + OSTaskID地址存放的值) - r2 - Cy
MOV R7,A ; R7 = ( OSTsakStackBotton + 01H + OSTaskID地址存放的值) - r2 - Cy
; SP = (unsigned char )cp1 - 1;
DEC R0;
MOV SP,R0 // 设置SP堆栈指针的地址
; for(i = OSTaskID+1;i < OSNextTaskID+1; i++)
; {
; OSTsakStackBotton[i] -= temp;
; }
MOV A,OSNextTaskID ; A = OSNextTaskID
CLR C
SUBB A,OSTaskID ; A = OSNextTaskID - OSTaskID
MOV R6,A ; R6 = OSNextTaskID - OSTaskID
JZ ?C0005 ; 累加器为0 则跳转
MOV A,#LOW (OSTsakStackBotton)
ADD A,OSTaskID
MOV R1,A ; R1 = OSTaskID + OSTsakStackBotton
MOV A,R7 ;A =
CPL A ; 进位位取反
INC A ;加1
MOV R7,A ;
?C0004:
INC R1
MOV A,R7
ADD A,@R1
MOV @R1,A
DJNZ R6,?C0004
?C0005:
; OSTaskID = OSNextTaskID;
MOV OSTaskID,OSNextTaskID
; LoadCtx();
LJMP LoadCtx
; }
?C0001:
;
; if( OSNextTaskID != OSTaskID)
MOV A,OSNextTaskID
XRL A,OSTaskID
JZ ?C000r
; {
; cp2–;
; cp1–;
; while(cp2 != (unsigned char idata *)temp)
; {
; *cp2-- = *cp1–;
; }
;MOV A,R7
;CLR C
;SUBB A,R1
;MOV R6,A
mov a,r0
clr c
subb a,r7
mov r6,a
?C0008:
DEC R0
DEC R1
MOV A,@R0
MOV @R1,A
DJNZ R6,?C0008
?C0009:
; temp = OSTsakStackBotton[OSTaskID+1] - (unsigned char idata *)SP-1;
MOV A,#LOW (OSTsakStackBotton+01H)
ADD A,OSTaskID
MOV R1,A
MOV A,@R1
SETB C
;SUBB A,SP
SUBB A,r2
MOV R7,A
; SP = (unsigned char )OSTsakStackBotton[OSNextTaskID+1];
MOV A,#LOW (OSTsakStackBotton+01H)
ADD A,OSNextTaskID
MOV R1,A
MOV A,@R1
MOV SP,A
; for(i = OSNextTaskID+1;i < OSTaskID+1; i++)
; {
; OSTsakStackBotton[i] += temp;
; }
MOV A,OSTaskID
CLR C
SUBB A,OSNextTaskID
JZ ?C0011
MOV R6,A
MOV A,#LOW (OSTsakStackBotton)
ADD A,OSNextTaskID
MOV R1,A
?C0010:
INC R1
MOV A,R7
ADD A,@R1
MOV @R1,A
DJNZ R6,?C0010
?C0011:
; OSTaskID = OSNextTaskID;
MOV OSTaskID,OSNextTaskID
; SP–;
DEC SP
; }
?C0007:
; LoadCtx();
LJMP LoadCtx
?C000r:
IF EN_SP2 <> 0
mov SP,r2
ENDIF
LJMP LoadCtx