/*
This is the default Startup for STR71x devices for the GNU toolchain

It has been designed by ST Microelectronics and modified by Raisonance.

You can use it, modify it, distribute it freely but without any waranty.

*/



.extern main


;/* the following are useful for initializing the .data section */
.extern _sidata ;/* start address for the initialization values of the .data section. defined in linker script */
.extern _sdata ;/* start address for the .data section. defined in linker script */
.extern _edata ;/* end address for the .data section. defined in linker script */

;/* the following are useful for initializing the .bss section */
.extern _sbss ;/* start address for the .bss section. defined in linker script */
.extern _ebss ;/* end address for the .bss section. defined in linker script */

;/* Standard definitions of Mode bits and Interrupt (I & F) flags in PSRs */
.set  Mode_USR, 0x10            ;/* User Mode */
.set  Mode_FIQ, 0x11            ;/* FIQ Mode */
.set  Mode_IRQ, 0x12            ;/* IRQ Mode */
.set  Mode_SVC, 0x13            ;/* Supervisor Mode */
.set  Mode_ABT, 0x17            ;/* Abort Mode */
.set  Mode_UNDEF, 0x1B          ;/* Undefined Mode */
.set  Mode_SYS, 0x1F            ;/* System Mode */

.equ  I_Bit, 0x80               ;/* when I bit is set, IRQ is disabled */
.equ  F_Bit, 0x40               ;/* when F bit is set, FIQ is disabled */

/*; --- System memory locations */

;/* init value for the stack pointer. defined in linker script */
.extern _estack

;/* Stack Sizes. The default values are in the linker script, but they can be overriden. */
.extern _UND_Stack_Init
.extern _SVC_Stack_Init
.extern _ABT_Stack_Init
.extern _FIQ_Stack_Init
.extern _IRQ_Stack_Init
.extern _USR_Stack_Init

.extern _UND_Stack_Size
.extern _SVC_Stack_Size
.extern _ABT_Stack_Size
.extern _FIQ_Stack_Size
.extern _IRQ_Stack_Size
.extern _USR_Stack_Size

SVC_Stack           =     _SVC_Stack_Init /*_estack*/           /*; 256 byte SVC stack at*/
                                              /*; top of memory */
                                              
IRQ_Stack           =     _IRQ_Stack_Init /*SVC_Stack - 256*/     /*; followed by IRQ stack */
USR_Stack           =     _USR_Stack_Init /*IRQ_Stack-1024*/    /*; followed by USR stack */
FIQ_Stack           =     _FIQ_Stack_Init /*USR_Stack-1024*/    /*; followed by FIQ stack*/
ABT_Stack           =     _ABT_Stack_Init /*FIQ_Stack-256*/     /*; followed by ABT stack */
UNDEF_Stack         =     _UND_Stack_Init /*ABT_Stack-256*/     /*; followed by UNDEF stack */

.extern EIC_BASE         /*; EIC base address */
ICR_OFFSET        =    0x00              /* ; Interrupt Control register offset*/
CICR_OFFSET       =    0x04      /*; Current Interrupt Channel Register.*/
CIPR_OFFSET       =    0x08              /* ; Current Interrupt Priority Register offset*/
IVR_OFFSET        =    0x18              /* ; Interrupt Vector Register offset */
FIR_OFFSET        =    0x1C              /* ; Fast Interrupt Register offset*/
IER_OFFSET        =    0x20               /*; Interrupt Enable Register offset*/
IPR_OFFSET        =    0x40               /*; Interrupt Pending Bit Register offset*/
SIR0_OFFSET       =    0x60               /*; Source Interrupt Register 0 */

.extern EMI_BASE        /* ; EMI base address */
BCON0_OFFSET      =    0x00               /*; Bank 0 configuration register offset */
BCON1_OFFSET      =    0x04              /* ; Bank 1 configuration register offset */
BCON2_OFFSET      =    0x08              /* ; Bank 2 configuration register offset */
BCON3_OFFSET      =    0x0C              /* ; Bank 3 configuration register offset*/

.extern GPIO2_BASE         /*; GPIO2 base address*/
PC0_OFFSET        =    0x00               /*; Port Configuration Register 0 offset*/
PC1_OFFSET        =    0x04              /* ; Port Configuration Register 1 offset */
PC2_OFFSET        =    0x08               /*; Port Configuration Register 2 offset*/
PD_OFFSET         =    0x0C               /*; Port Data Register offset*/

.extern CPM_BASE         /*; CPM Base Address*/
BOOTCONF_OFFSET   =    0x10               /*; CPM - Boot Configuration Register*/



/***************************************************************************************/


.globl _start
.globl _startup

.text
_startup:
_start:
        LDR     PC, Reset_Addr
        LDR     PC, Undefined_Addr
        LDR     PC, SWI_Addr
        LDR     PC, Prefetch_Addr
        LDR     PC, Abort_Addr
        NOP                          /*; Reserved vector*/
        LDR     PC, IRQ_Addr
        LDR     PC, FIQ_Addr





Reset_Addr      : .long     Reset_Handler
Undefined_Addr  : .long     UndefinedHandler
SWI_Addr        : .long     SWIHandler
Prefetch_Addr   : .long     PrefetchAbortHandler
Abort_Addr      : .long     DataAbortHandler
                  .long 0      /*; Reserved vector*/
IRQ_Addr        : .long     IRQHandler
FIQ_Addr        : .long     FIQHandler

/*;*******************************************************************************
;              Peripherals IRQ handlers address table
;********************************************************************************/

T0TIMI_Addr     : .long  T0TIMIIRQHandler
FLASH_Addr      : .long  FLASHIRQHandler
RCCU_Addr       : .long  RCCUIRQHandler
RTC_Addr        : .long  RTCIRQHandler
WDG_Addr        : .long  WDGIRQHandler
XTI_Addr        : .long  XTIIRQHandler
USBHP_Addr      : .long  USBHPIRQHandler
I2C0ITERR_Addr  : .long  I2C0ITERRIRQHandler
I2C1ITERR_ADDR  : .long  I2C1ITERRIRQHandler
UART0_Addr      : .long  UART0IRQHandler
UART1_Addr      : .long  UART1IRQHandler
UART2_ADDR      : .long  UART2IRQHandler
UART3_ADDR      : .long  UART3IRQHandler
BSPI0_ADDR      : .long  BSPI0IRQHandler
BSPI1_Addr      : .long  BSPI1IRQHandler
I2C0_Addr       : .long  I2C0IRQHandler
I2C1_Addr       : .long  I2C1IRQHandler
CAN_Addr        : .long  CANIRQHandler
ADC12_Addr      : .long  ADC12IRQHandler
T1TIMI_Addr     : .long  T1TIMIIRQHandler
T2TIMI_Addr     : .long  T2TIMIIRQHandler
T3TIMI_Addr     : .long  T3TIMIIRQHandler
                  .long 0                /*; reserved*/
                  .long 0                /*; reserved*/
                  .long 0                /*; reserved*/ 
HDLC_Addr       : .long  HDLCIRQHandler
USBLP_Addr      : .long  USBLPIRQHandler
                  .long 0                /*; reserved*/
                  .long 0                /*; reserved*/
T0TOI_Addr      : .long  T0TOIIRQHandler
T0OC1_Addr      : .long  T0OC1IRQHandler
T0OC2_Addr      : .long  T0OC2IRQHandler
/*;*******************************************************************************
;* Macro Name     : SaveContext
;* Description    : This macro used to save the context before entering
;                   an exception handler.
;* Input          : The range of registers to store.
;* Output         : none
;********************************************************************************/

       .macro SaveContext $r0,$r12
        STMFD  sp!,{r0-r12,lr} /*; Save The workspace plus the current return*/
                               /*; address lr_ mode into the stack.*/
        MRS    r1,spsr         /*; Save the spsr_mode into r1.*/
        STMFD  sp!,{r1}        /*; Save spsr.*/
        .endm

/*;*******************************************************************************
;* Macro Name     : RestoreContext
;* Description    : This macro used to restore the context to return from
;                   an exception handler and continue the program execution.
;* Input          : The range of registers to restore.
;* Output         : none
;********************************************************************************/

        .macro RestoreContext $r0,$r12
        LDMFD   sp!,{r1}        /*; Restore the saved spsr_mode into r1.*/
        MSR     spsr_cxsf,r1    /*; Restore spsr_mode.*/
        LDMFD   sp!,{r0-r12,pc}^/*; Return to the instruction following...*/
                                /*; ...the exception interrupt.*/
        .endm



/*;*******************************************************************************
;* Function Name  : UndefinedHandler
;* Description    : This function called when undefined instruction
;                   exception is entered.
;* Input          : none
;* Output         : none
;*********************************************************************************/

UndefinedHandler:
        SaveContext r0,r12    /*; Save the workspace plus the current*/
                              /*; return address lr_ und and spsr_und.*/
        BL      Undefined_Handler/*; Branch to Undefined_Handler*/
        RestoreContext r0,r12 /*; Return to the instruction following...*/
                              /*; ...the undefined instruction.*/

/*;*******************************************************************************
;* Function Name  : SWIHandler
;* Description    : This function called when SWI instruction executed.
;* Input          : none
;* Output         : none
;********************************************************************************/

SWIHandler:
        SaveContext r0,r12    /*; Save the workspace plus the current*/
                              /*; return address lr_ svc and spsr_svc.*/
        BL       SWI_Handler  /*; Branch to SWI_Handler*/
        RestoreContext r0,r12 /*; Return to the instruction following...*/
                              /*; ...the SWI instruction.*/

/*;*******************************************************************************
;* Function Name  : IRQHandler
;* Description    : This function called when IRQ exception is entered.
;* Input          : none
;* Output         : none
;********************************************************************************/


IRQHandler:
        SUB    lr,lr,#4       /*; Update the link register*/
        SaveContext r0,r12    /*; Save the workspace plus the current*/
                              /* return address lr_ irq and spsr_irq.*/
        LDR    lr, =ReturnAddress /*; Read the return address.*/
        LDR    r0, =EIC_BASE
        LDR    r1, =IVR_OFFSET
        ADD    pc,r0,r1      /*; Branch to the IRQ handler.*/
        
ReturnAddress:
                                        /*; Clear pending bit in EIC (using the proper IPRx)*/
        LDR    r0, =EIC_BASE
        LDR    r2, [r0, #CICR_OFFSET]   /*; Get the IRQ channel number*/
        MOV    r3,#1
        MOV    r3,r3,LSL r2
        STR    r3,[r0, #IPR_OFFSET]     /*; Clear the corresponding IPR bit.*/

        RestoreContext r0,r12           /* ; Restore the context and return to the...*/
                                        /* ; ...program execution. */

/*;*******************************************************************************
;* Function Name  : PrefetchAbortHandler
;* Description    : This function called when Prefetch Abort
;                   exception is entered.
;* Input          : none
;* Output         : none
;*********************************************************************************/

PrefetchAbortHandler:
        SUB    lr,lr,#4       /*; Update the link register.*/
        SaveContext r0,r12    /*; Save the workspace plus the current*/
                              /*; return address lr_abt and spsr_abt.*/
        BL     Prefetch_Handler/*; Branch to Prefetch_Handler.*/
        RestoreContext r0,r12 /*; Return to the instruction following that...*/
                              /*; ...has generated the prefetch abort exception.*/

/*;*******************************************************************************
;* Function Name  : DataAbortHandler
;* Description    : This function is called when Data Abort
;                   exception is entered.
;* Input          : none
;* Output         : none
;********************************************************************************/

DataAbortHandler:
        SUB    lr,lr,#8       /*; Update the link register.*/
        SaveContext r0,r12    /*; Save the workspace plus the current*/
                              /*; return address lr_ abt and spsr_abt.*/
        BL     Abort_Handler  /*; Branch to Abort_Handler.*/
        RestoreContext r0,r12 /*; Return to the instruction following that...*/
                              /*; ...has generated the data abort exception.*/

/*;*******************************************************************************
;* Function Name  : FIQHandler
;* Description    : This function is called when FIQ
;                   exception is entered.
;* Input          : none
;* Output         : none
;********************************************************************************/

FIQHandler:
        SUB    lr,lr,#4       /*; Update the link register.*/
        SaveContext r0,r7     /*; Save the workspace plus the current*/
                              /*; return address lr_ fiq and spsr_fiq.*/
        BL      FIQ_Handler   /*; Branch to FIQ_Handler.*/
        RestoreContext r0,r7  /*; Restore the context and return to the...*/
                              /*; ...program execution.*/

/*;*******************************************************************************
;* Macro Name     : IRQ_to_SYS
;* Description    : This macro used to switch form IRQ mode to SYS mode
;* Input          : none.
;* Output         : none
;*******************************************************************************/
       .macro IRQ_to_SYS

        MSR    cpsr_c,#0x1F
        STMFD  sp!,{lr}
       .endm
/*;*******************************************************************************
;* Macro Name     : SYS_to_IRQ
;* Description    : This macro used to switch from SYS mode to IRQ mode
;                   then to return to IRQHnadler routine.
;* Input          : none.
;* Output         : none.
;*******************************************************************************/
      .macro SYS_to_IRQ
       LDMFD  sp!,{lr}      /*; Restore the link register. */
        MSR    cpsr_c,#0xD2  /*; Switch to IRQ mode.*/
        MOV    pc,lr         /*; Return to IRQHandler routine to clear the*/
                             /*; pending bit.*/
       .endm
/*;*******************************************************************************
;* Function Name  : T0TIMIIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T0TIMI_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T0TIMI_IRQHandler function termination.
;* Input          : none.
;* Output         : none.
;********************************************************************************/
T0TIMIIRQHandler:
        IRQ_to_SYS   
        BL     T0TIMI_IRQHandler
       SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : FLASHIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the FLASH_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   FLASH_IRQHandler function termination.
;* Input          : none
;* Output         : none
;********************************************************************************/
FLASHIRQHandler:
        IRQ_to_SYS
        BL     FLASH_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : RCCUIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the RCCU_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   RCCU_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
RCCUIRQHandler:
        IRQ_to_SYS
        BL     RCCU_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : RTCIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the RTC_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   RTC_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
RTCIRQHandler:
        IRQ_to_SYS
        BL     RTC_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : WDGIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the WDG_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   WDG_IRQHandler function termination.
;* Input          : none
;* Output         : none
/*;*******************************************************************************/
WDGIRQHandler:
        IRQ_to_SYS
        BL     WDG_IRQHandler
        SYS_to_IRQ
/*;******************************************************************************* /
;* Function Name  : XTIIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the XTI_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   XTI_IRQHandler function termination.
;* Input          : none
;* Output         : none 
**************************************************************************************/

XTIIRQHandler:
        IRQ_to_SYS
        BL     XTI_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : USBHPIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the USBHP_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   USBHP_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
USBHPIRQHandler:
        IRQ_to_SYS
        BL     USBHP_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : I2C0ITERRIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the I2C0ITERR_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   I2C0ITERR_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
I2C0ITERRIRQHandler:
        IRQ_to_SYS
        BL     I2C0ITERR_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : I2C1ITERRIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the I2C1ITERR_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   I2C1ITERR_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
I2C1ITERRIRQHandler:
        IRQ_to_SYS
        BL     I2C1ITERR_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : UART0IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the UART0_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   UART0_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
UART0IRQHandler:
        IRQ_to_SYS
        BL     UART0_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : UART1IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the UART1_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   UART1_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
UART1IRQHandler:
        IRQ_to_SYS
        BL     UART1_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : UART2IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the UART2_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   UART2_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
UART2IRQHandler:
        IRQ_to_SYS
        BL     UART2_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : UART3IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the UART3IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   UART3IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
UART3IRQHandler:
        IRQ_to_SYS
        BL     UART3_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : BSPI0IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the BSPI0IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   BSPI0IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
BSPI0IRQHandler:
        IRQ_to_SYS
        BL     BSPI0_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : BSPI1IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the BSPI1_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   BSPI1_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
BSPI1IRQHandler:
        IRQ_to_SYS
        BL     BSPI1_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : I2C0IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the I2C0_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   I2C0_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
I2C0IRQHandler:
        IRQ_to_SYS
        BL     I2C0_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : I2C1IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the I2C1_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   I2C1_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
I2C1IRQHandler:
        IRQ_to_SYS
        BL     I2C1_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : CANIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the CAN_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   CAN_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
CANIRQHandler:
        IRQ_to_SYS
        BL     CAN_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : ADC12IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the ADC12_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   ADC12_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
ADC12IRQHandler:
        IRQ_to_SYS
        BL     ADC12_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : T1TIMIIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T1TIMI_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T1TIMI_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
T1TIMIIRQHandler:
        IRQ_to_SYS    /*; Save the link register.*/
        BL     T1TIMI_IRQHandler
        SYS_to_IRQ   /*; Return to IRQHandler routine to clear the*/
                             /*; pending bit.*/
/*;*******************************************************************************
;* Function Name  : T2TIMIIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T2TIMI_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T2TIMI_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
T2TIMIIRQHandler:
        IRQ_to_SYS
        BL     T2TIMI_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : T3TIMIIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T3TIMI_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T3TIMI_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
T3TIMIIRQHandler:
        IRQ_to_SYS
        BL     T3TIMI_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : HDLCIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the HDLC_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   HDLC_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
HDLCIRQHandler:
        IRQ_to_SYS
        BL     HDLC_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : USBLPIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the USBLP_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   USBLP_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
USBLPIRQHandler:
        IRQ_to_SYS
        BL     USBLP_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : T0TOIIRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T0TOI_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T0TOI_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
T0TOIIRQHandler:
        IRQ_to_SYS
        BL     T0TOI_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : T0OC1IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T0OC1_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T0OC1_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
T0OC1IRQHandler:
        IRQ_to_SYS
        BL     T0OC1_IRQHandler
        SYS_to_IRQ
/*;*******************************************************************************
;* Function Name  : T0OC2IRQHandler
;* Description    : This function used to switch to SYS mode before entering
;                   the T0OC2_IRQHandler function located in 71x_it.c.
;                   Then to return to IRQ mode after the
;                   T0OC2_IRQHandler function termination.
;* Input          : none
;* Output         : none
;*******************************************************************************/
T0OC2IRQHandler:
        IRQ_to_SYS
        BL     T0OC2_IRQHandler
        SYS_to_IRQ



/***********************************************************************************************/
Reset_Handler:
        LDR     pc, =NextInst

NextInst:

        NOP   /*; Wait for OSC stabilization*/
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP

        MSR     CPSR_c, #Mode_ABT|F_Bit|I_Bit
        LDR     SP, =ABT_Stack

        MSR     CPSR_c, #Mode_UNDEF|F_Bit|I_Bit
        LDR     SP, =UNDEF_Stack

        MSR     CPSR_c, #Mode_SVC|F_Bit|I_Bit
        LDR     SP, =_estack /*RAM_Limit*/
      

/*******************************************************************************
;REMAPPING
;Description  : Remapping  memory whether RAM,FLASH or External memory
;               at Address 0x0 after the application has started executing.
;               Remapping is generally done to allow RAM  to replace FLASH
;               or EXTMEM at 0x0.
;               the remapping of RAM allow copying of vector table into RAM
;******************************************************************************/
 
EIC_INIT :
        ldr     r3, =EIC_BASE
        ldr     r4, =0x00000000
        str   r4, [r3, #ICR_OFFSET]   /*; Disable FIQ and IRQ */
        str     r4, [r3, #IER_OFFSET]   /*; Disable all channels interrupts */
        ldr     r4, =0xFFFFFFFF
        STR     r4, [r3, #IPR_OFFSET]   /*; Clear all IRQ pending bits */
        ldr     r4, =0x0C
        str    r4, [r3, #FIR_OFFSET]   /*; Disable FIQ channels and clear FIQ pending bits */
        ldr     r4, =0x00000000
        str     r4, [r3, #CIPR_OFFSET]  /*; Reset the current priority register*/
        ldr     r4, =0xE59F0000
        str     r4, [r3, #IVR_OFFSET]   /*; Write the LDR pc,pc,#offset instruction code in IVR[31:16] */
        ldr     r2, =32                   /*; 32 Channel to initialize*/
        ldr     r0, =T0TIMI_Addr          /*; Read the address of the IRQs address table*/
        ldr     r1, =0x00000FFF
        and     r0,r0,r1
        ldr     r5, =SIR0_OFFSET        /*; Read SIR0 address */
        sub     r4,r0,#8                  /*; subtract 8 for prefetch*/
        ldr     r1, =0xF7E8               /*; add the offset to the 0x00000000 address(IVR address + 7E8 = 0x00000000)*/
                                          /*; 0xF7E8 used to complete the LDR pc,pc,#offset opcode */
        add     r1,r4,r1                  /*; compute the jump offset*/
EIC_INI :
        MOV     r4, r1, LSL #16           /*; Left shift the result*/
        STR     r4, [r3, r5]              /*; Store the result in SIRx register*/
        ADD     r1, r1, #4                /*; Next IRQ address*/
        ADD     r5, r5, #4                /*; Next SIR*/
        SUBS    r2, r2, #1                /*; Decrement the number of SIR registers to initialize*/
        BNE     EIC_INI                   /*; If more then continue*/


       	msr    CPSR_c, #Mode_FIQ        /*; Change to FIQ mode*/
         ldr     SP, =FIQ_Stack           /*; Initialize FIQ stack pointer */

       	msr     CPSR_c, #Mode_IRQ        /*; Change to IRQ mode  */
        ldr     SP, =IRQ_Stack           /*; Initialize IRQ stack pointer*/
        MSR     CPSR_c, #Mode_USR        /*; Change to User mode, Enable IRQ and FIQ */
        ldr     SP, =USR_Stack           /*; Initialize USR stack pointer*/

        msr     CPSR_c, #Mode_SYS       /* ; Change to System mode*/

        /*IMPORT  __main */

  /* ;copy the initial values for .data section from FLASH to RAM */
	ldr	R1, =_sidata
	ldr	R2, =_sdata
	ldr	R3, =_edata
_reset_inidata_loop:
	cmp	R2, R3
	ldrlO	R0, [R1], #4
	strlO	R0, [R2], #4
	blO	_reset_inidata_loop

	;/* Clear the .bss section */
	mov   r0,#0						;/* get a zero */
	ldr   r1,=_sbss				;/* point to bss start */
	ldr   r2,=_ebss				;/* point to bss end */
_reset_inibss_loop:
	cmp   r1,r2						;/* check if some data remains to clear */
	strlo r0,[r1],#4				;/* clear 4 bytes */
	blo   _reset_inibss_loop	;/* loop until done */



/************************************************************************************************/

/*; --- Now enter the C code */
        B       main   /*; Note : use B not BL, because an application will*/
                         /*; never return this way*/





        
        
        /*LTORG


        END
;*************
****** (C) COPYRIGHT 2003 STMicroelectronics *****
****** (C) COPYRIGHT 2005 RAISONANCE *****

END OF FILE****/











