;/**********************************************************************/
;/*                                                                    */
;/* File name: uip_kernel_isr_cm.s                                     */
;/*                                                                    */
;/* Since:     2013-08-27                                              */
;/*                                                                    */
;/* Version:   uip Kernel v1.09                                        */
;/*            Copyright (C) 2006..2018 Wang Baojin.                   */
;/*                                                                    */
;/* Author:    Designed by Wang Baojin(王保进)                         */
;/*                                                                    */
;/*                                                                    */
;/* Purpose:   Definition of interrupt handler architecture functions. */
;/*            中断处理服务框架的汇编语言部分                          */
;/*                                                                    */
;/* Distribution: This file is part of uip Kernel.                     */
;/*            uip Kernel is free software, you can redistribute it    */
;/*            and/or modify it under the terms of the GNU General     */
;/*            Public License as published by the Free Software        */
;/*            Foundation either version 2, or (at your option)        */
;/*            any later version.                                      */
;/*                                                                    */
;/*            uip Kernel is distributed in the hope that it will be   */
;/*            useful, but WITHOUT ANY WARRANTY without even the       */
;/*            implied warranty of MERCHANTABILITY or FITNESS FOR A    */
;/*            PuipICULAR PURPOSE.  See the GNU General Public         */
;/*            License for more details.                               */
;/*                                                                    */
;/*            You should have received a copy of the GNU General      */
;/*            Public License along with gpsim see the file            */
;/*            COPYING.txt. If not, write to the Free Software         */
;/*            Foundation, 59 Temple Place - Suite 330,                */
;/*            Boston, MA 02111-1307, USA.                             */
;/*                                                                    */
;/*          > A special exception to the GPL can be applied should    */
;/*            you wish to distribute a combined work that includes    */
;/*            uip Kernel, without being obliged to provide the        */
;/*            source code for any proprietary components.             */
;/*                                                                    */
;/* History:                                                           */
;/*   2013/08/27  [WBJ] Create this file.                              */
;/*                                                                    */
;/**********************************************************************/

UIP_CORTEX_M_NVIC_INT_CTRL_REG              EQU      0xE000ED04
UIP_CORTEX_M_NVIC_PENDSVSET_VAL             EQU      0x10000000

    IMPORT  uipCurrentTaskTCBPtr
	IMPORT  uipPreContexSwitch
	
    EXPORT  uipDisableAllInterrupts
    EXPORT  uipIntMaskStateRestore

    ;EXPORT  uipGeneralCounterISR

    EXPORT  uipPendSVHandler
    ;EXPORT  uipContextSwitch     

    PRESERVE8
    THUMB

    AREA CODE, CODE, READONLY

;/**********************************************************************/ 
;/*                                                                    */ 
;/*  Function name                                       Version       */ 
;/*                                                                    */ 
;/*    uipDisableAllInterrupts                             1.09        */ 
;/*                                                                    */ 
;/*  Author                                                            */ 
;/*                                                                    */ 
;/*    Wang Baojin                                                     */ 
;/*                                                                    */ 
;/*  Description                                                       */ 
;/*                                                                    */ 
;/*    Disable all interrupts and save current state of interrupts     */
;/*    (in CPSR) to global variable.                                   */ 
;/*                                                                    */ 
;/*  Parameter(In)                                                     */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Parameter(Out)                                                    */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Return Status                                                     */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Call functions                                                    */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Called by                                                         */ 
;/*                                                                    */ 
;/*    uip_KERNEL_DISABLE_INTS                                         */ 
;/*                                                                    */ 
;/*  Release                                                           */ 
;/*                                                                    */ 
;/*  author name         release date              Description         */ 
;/*                                                                    */ 
;/*  Wang Baojin          2013.08.27            Initial Version 1.09   */ 
;/*                                                                    */ 
;/**********************************************************************/ 
uipDisableAllInterrupts
    MRS     R0, PRIMASK
    CPSID   I
    BX      LR                                 ; Return to caller
 
;/**********************************************************************/ 
;/*                                                                    */ 
;/*  Function name                                       Version       */ 
;/*                                                                    */ 
;/*    uipIntMaskStateRestore                              1.09        */ 
;/*                                                                    */ 
;/*  Author                                                            */ 
;/*                                                                    */ 
;/*    Wang Baojin                                                     */ 
;/*                                                                    */ 
;/*  Description                                                       */ 
;/*                                                                    */ 
;/*    Restore saved current state of interrupts (in CPSR) from        */
;/*    global variable.                                                */ 
;/*                                                                    */ 
;/*  Parameter(In)                                                     */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Parameter(Out)                                                    */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Return Status                                                     */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Call functions                                                    */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Called by                                                         */ 
;/*                                                                    */ 
;/*    uip_KERNEL_RESTORE_INTS                                         */ 
;/*                                                                    */ 
;/*  Release                                                           */ 
;/*                                                                    */ 
;/*  author name         release date              Description         */ 
;/*                                                                    */ 
;/*  Wang Baojin          2013.08.27            Initial Version 1.09   */ 
;/*                                                                    */ 
;/**********************************************************************/ 
uipIntMaskStateRestore
    MSR     PRIMASK, R0
    BX      LR

;/**********************************************************************/ 
;/*                                                                    */ 
;/*  Function name                                       Version       */ 
;/*                                                                    */ 
;/*    uipPendSVHandler                                   1.09         */ 
;/*                                                                    */ 
;/*  Author                                                            */ 
;/*                                                                    */ 
;/*    Wang Baojin                                                     */ 
;/*                                                                    */ 
;/*  Description                                                       */ 
;/*                                                                    */ 
;/*    ISR for context switch in Cortex-M                              */ 
;/*    实现任务现场切换的中断处理程序                                  */ 
;/*    能判断是否不需要保存被换出任务的现场，                          */
;/*    用于 TerminateTask与 ChainTask 的执行逻辑中                     */ 
;/*                                                                    */ 
;/*  Parameter(In)                                                     */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Parameter(Out)                                                    */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Return Status                                                     */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Call functions                                                    */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Called by                                                         */ 
;/*                                                                    */ 
;/*    None                                                            */ 
;/*                                                                    */ 
;/*  Release                                                           */ 
;/*                                                                    */ 
;/*  author name         release date              Description         */ 
;/*                                                                    */ 
;/*  Wang Baojin          2013.08.27           Initial Version 1.09    */ 
;/*                                                                    */ 
;/**********************************************************************/ 
uipPendSVHandler
    CPSID   I
    LDR     R3, =uipCurrentTaskTCBPtr                   ; Pickup address of current task's TCB pointer
    LDR     R2, [R3]
    ;判断是否当前任务控制块的指针值为0；若是，则不保存被换出任务的现场
    CMP     R2, #0
    BEQ     uipKernelOnlySwitchNextTaskContext

    MRS     R0, PSP
	SUBS    R0, #32			
	STR     R0, [R2, #0x0C]		
	STMIA   R0!, {R4-R7}		
	MOV     R4, R8				
	MOV     R5, R9
	MOV     R6, R10
	MOV     R7, R11
	STMIA   R0!, {R4-R7}

uipKernelOnlySwitchNextTaskContext
	PUSH    {R3, LR}
	BL      uipPreContexSwitch
	POP     {R2, R3}
	
	MOVS    R1, #0x04
	ORRS    R3, R3, R1 
	
    LDR     R1, [R2]
	LDR     R0, [R1, #0x0C]
		
	ADDS    R0, #16			
	LDMIA   R0!, {R4-R7}		
	MOV     R8, R4
	MOV     R9, R5
	MOV     R10, R6
	MOV     R11, R7

	MSR     PSP, R0				

	SUBS    R0, #32			
	LDMIA   R0!, {R4-R7}      

    CPSIE   I
    BX      R3 
	
    ALIGN
		
    END

;/***************************************************************************************/
;/*                           End of File : uip_kernel_isr.s                            */
;/***************************************************************************************/
