📄 os_cpu_a.s90
字号:
;********************************************************************************************************
; uC/OS-II
; The Real-Time Kernel
;
; AVR Specific code
; (IAR AA90 V1.30 or later)
;
;
; File : OS_CPU_A.S90
; By : Ole Saether
;
; $Modtime: 22.07.01 22:06 $
; $Revision: 3 $
;
;********************************************************************************************************
; I/O PORT ADDRESSES
;********************************************************************************************************
SREG = 0x3F
SPH = 0x3E
SPL = 0x3D
TCNT0 = 0x32
;********************************************************************************************************
; PUBLIC DECLARATIONS
;********************************************************************************************************
NAME OS_CPU_A
PUBLIC OSStartHighRdy
PUBLIC OSCtxSw
PUBLIC OSIntCtxSw
;********************************************************************************************************
; EXTERNAL DECLARATIONS
;********************************************************************************************************
EXTERN OSIntExit
EXTERN OSIntNesting
EXTERN OSPrioCur
EXTERN OSPrioHighRdy
EXTERN OSRunning
EXTERN OSTaskSwHook
EXTERN OSTCBCur
EXTERN OSTCBHighRdy
EXTERN OSTimeTick
#include "ATMACRO.S90"
RSEG CODE
;********************************************************************************************************
; START HIGHEST PRIORITY TASK READY-TO-RUN
;
; Description : This function is called by OSStart() to start the highest priority task that was created
; by your application before calling OSStart().
;
; Note(s) : 1) The (data)stack frame is assumed to look as follows:
;
; OSTCBHighRdy->OSTCBStkPtr --> LSB of (return) stack pointer (Low memory)
; SPH of (return) stack pointer
; Flags to load in status register
; R31
; R30
; R27
; .
; .
; .
; R0 (High memory)
;
; where the stack pointer points to the task start address.
;
;
; 2) OSStartHighRdy() MUST:
; a) Call OSTaskSwHook() then,
; b) Set OSRunning to TRUE,
; c) Switch to the highest priority task.
;
;********************************************************************************************************
OSStartHighRdy: RCALL OSTaskSwHook ; Invoke user defined context switch hook
LDS R16,OSRunning ; Indicate that we are multitasking
INC R16 ;
STS OSRunning,R16 ;
LDS R30,OSTCBHighRdy ; Let Z point to TCB of highest priority task
LDS R31,OSTCBHighRdy+1 ; ready to run
LD R28,Z+ ; Load Y (R29:R28) pointer
LD R29,Z+ ;
POPSP ; Restore stack pointer
POPSREG ; Restore status register
POPRS ; Restore all registers
RET ; Start task
;********************************************************************************************************
; TASK LEVEL CONTEXT SWITCH
;
; Description : This function is called when a task makes a higher priority task ready-to-run.
;
; Note(s) : 1) Upon entry,
; OSTCBCur points to the OS_TCB of the task to suspend
; OSTCBHighRdy points to the OS_TCB of the task to resume
;
; 2) The stack frame of the task to suspend looks as follows:
;
; SP+0 --> LSB of task code address
; +1 MSB of task code address (High memory)
;
; 3) The saved context of the task to resume looks as follows:
;
; OSTCBHighRdy->OSTCBStkPtr --> LSB of (return) stack pointer (Low memory)
; SPH of (return) stack pointer
; Flags to load in status register
; R31
; R30
; R27
; .
; .
; .
; R0 (High memory)
;********************************************************************************************************
OSCtxSw: PUSHRS ; Save current task's context
PUSHSREG
PUSHSP
LDS R30,OSTCBCur ; Z = OSTCBCur->OSTCBStkPtr
LDS R31,OSTCBCur+1 ;
ST Z+,R28 ; Save Y (R29:R28) pointer
ST Z+,R29 ;
RCALL OSTaskSwHook ; Call user defined task switch hook
LDS R16,OSPrioHighRdy ; OSPrioCur = OSPrioHighRdy
STS OSPrioCur,R16
LDS R30,OSTCBHighRdy ; Let Z point to TCB of highest priority task
LDS R31,OSTCBHighRdy+1 ; ready to run
STS OSTCBCur,R30 ; OSTCBCur = OSTCBHighRdy
STS OSTCBCur+1,R31 ;
LD R28,Z+ ; Restore Y pointer
LD R29,Z+ ;
POPSP ; Restore stack pointer
POPSREG ; Restore status register
POPRS ; Restore all registers
RET
;*********************************************************************************************************
; INTERRUPT LEVEL CONTEXT SWITCH
;
; Description : This function is called by OSIntExit() to perform a context switch to a task that has
; been made ready-to-run by an ISR.
;
; Note(s) : 1) Upon entry,
; OSTCBCur points to the OS_TCB of the task to suspend
; OSTCBHighRdy points to the OS_TCB of the task to resume
;
; 2) The stack frame of the task to suspend looks as follows:
;
; SP+0 --> LSB of return address of OSIntCtxSw() (Low memory)
; +1 MSB of return address of OSIntCtxSw()
; +2 LSB of return address of OSIntExit()
; +3 MSB of return address of OSIntExit()
; +4 Saved SREG (when OS_CRITICAL_METHOD == 2)
; +5 LSB of task code address
; +6 MSB of task code address (High memory)
;
; 3) The saved context of the task to resume looks as follows:
;
; OSTCBHighRdy->OSTCBStkPtr --> LSB of (return) stack pointer (Low memory)
; MSB of (return) stack pointer
; Flags to load in status register
; R31
; R30
; R27
; .
; .
; .
; R0 (High memory)
;*********************************************************************************************************
OSIntCtxSw: LDS R30,OSTCBCur ; Z = OSTCBCur->OSTCBStkPtr
LDS R31,OSTCBCur+1 ;
ST Z+,R28 ; Save Y pointer
ST Z+,R29 ;
RCALL OSTaskSwHook ; Call user defined task switch hook
LDS R16,OSPrioHighRdy ; OSPrioCur = OSPrioHighRdy
STS OSPrioCur,R16 ;
LDS R30,OSTCBHighRdy ; Z = OSTCBHighRdy->OSTCBStkPtr
LDS R31,OSTCBHighRdy+1 ;
STS OSTCBCur,R30 ; OSTCBCur = OSTCBHighRdy
STS OSTCBCur+1,R31 ;
LD R28,Z+ ; Restore Y pointer
LD R29,Z+ ;
POPSP ; Restore stack pointer
POPSREG ; Restore status register
POPRS ; Restore saved registers
RET
;********************************************************************************************************
; SYSTEM TICK ISR
;
; Description : This function is the ISR used to notify uC/OS-II that a system tick has occurred.
;
;
;********************************************************************************************************
OSTickISR: PUSHRS ; Save interrupted task's context
IN R16,SREG
SBR R16,0x80 ; Set interrupt bit in status reg and save
ST -Y,R16
PUSHSP
LDI R16,256-(7372800/50/1024) ; Reload timer to overflow at a rate of 50Hz
OUT TCNT0,R16 ; at a prescaler of 1024 and 7.3728 MHz AVR clock
LDS R16,OSIntNesting ; Notify uC/OS-II of ISR
INC R16 ;
STS OSIntNesting,R16 ;
SEI ; Enable interrupts
RCALL OSTimeTick ; Call uC/OS-II's tick updating function
RCALL OSIntExit ; Notify uC/OS-II about end of ISR
POPSP
POPSREG ; Restore status register
POPRS ; Restore all registers
RET ; Note: RET instead of RETI
;********************************************************************************************************
; INTVEC
;
; Description : Set up the AVR interrupt vector table so that the TIMER0 overflow interrupt
; vectors to the function above.
;
;********************************************************************************************************
COMMON INTVEC
DS 14
RJMP OSTickISR
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -