;;*****************************************************************************
;;
;; sys_intvec.asm : The interrupt vector which handles HW INT, and call all the
;;                  major functions used in bootloader
;; Author         : QJ Wang. qjwang@ti.com
;; Date           : 5-9-2013
;;
;; Copyright (c) 2006-2011 Texas Instruments Incorporated.  All rights reserved.
;; Software License Agreement
;;
;; Texas Instruments (TI) is supplying this software for use solely and
;; exclusively on TI's microcontroller products. The software is owned by
;; TI and/or its suppliers, and is protected under applicable copyright
;; laws. You may not combine this software with "viral" open-source
;; software in order to form a larger program.
;;
;; THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
;; NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
;; NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
;; CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
;; DAMAGES, FOR ANY REASON WHATSOEVER.
;;
;;*****************************************************************************


;-------------------------------------------------------------------------------
; import reference for interrupt routines
;/****************************************************************************/
;/* INTVECS.ASM - Cortex_M3 INTERRUPT VECTORS TABLE SETUP FILE               */
;/****************************************************************************/
; import reference for interrupt routines

    .cdecls C, NOLIST, WARN
    %{
        #include "bl_config.h"
        #include "reg_m3core.h"
        #include "system.h"
        #include "bl_can.h"
        #include "bl_check.h"
    %}

    .global _c_int00, Delay, Vectors

  	.ref esmLowInterrupt
  	.ref systemInit
  	.ref _init
  	.ref main

    .ref    __STACK_TOP
 
    .sect ".intvecs"
	.word  __STACK_TOP	        ; Address of the default stack on reset
	.word _c_int00				; Power-on or warm reset
	.word NmiSR					; Non Maskable Interrupt
	.word FaultISR			    ; Repeated or unhandlable fault
	.word Dummy_ISR	            ; MPU related fault
	.word Dummy_ISR			    ; Memory address/data related fault
	.word Dummy_ISR		    	; Undef. instruction or similar fault
	.word Reserved				; Reserved
	.word Reserved				; Reserved
	.word Reserved				; Reserved
	.word Reserved				; Reserved
	.word UpdateHandler			; System call with SVC instruction
	.word Dummy_ISR  		    ; Non-halting debug monitor
	.word Reserved				; Reserved
	.word Dummy_ISR				; Software pendable service request
	.word SysTickIntHandler  	; System tick timer interrupt

	.word Dummy_ISR                 ; Channel 0
	.word Dummy_ISR   			    ; Channel 1 
	.word Dummy_ISR	                ; esmLowInterrupt           ; Channel 2
	.word Dummy_ISR			        ; Channel 3 
	.word Dummy_ISR			        ; Channel 4 
	.word Dummy_ISR			        ; Channel 5 
	.word Dummy_ISR			        ; Channel 6 
	.word Dummy_ISR                 ; Channel 7 
	.word Dummy_ISR                 ; Channel 8 
	.word Dummy_ISR                 ; Channel 9 
	.word Dummy_ISR                 ; Channel 10 
	.word Dummy_ISR                 ; Channel 11 
	.word Dummy_ISR                 ; Channel 12 
	.word Dummy_ISR                 ; Channel 13 
	.word Dummy_ISR                 ; Channel 14 
	.word Dummy_ISR                 ; Channel 15 
	.word Dummy_ISR                 ; Channel 16 
	.word Dummy_ISR                 ; Channel 17 
	.word Dummy_ISR                 ; Channel 18
	.word Dummy_ISR                 ; Channel 19
	.word Dummy_ISR                 ; Channel 20
	.word Dummy_ISR                 ; Channel 21
	.word Dummy_ISR                 ; Channel 22
	.word Dummy_ISR                 ; Channel 23
	.word Dummy_ISR                 ; Channel 24 
	.word Dummy_ISR                 ; Channel 25 
	.word Dummy_ISR                 ; Channel 26 
	.word Dummy_ISR                 ; Channel 27 
	.word Dummy_ISR                 ; Channel 28 
	.word Dummy_ISR                 ; Channel 29
	.word Dummy_ISR                 ; Channel 30
	.word Dummy_ISR                 ; Channel 31 
	.word Dummy_ISR                 ; Channel 31
	.word Dummy_ISR                 ; Channel 31
	.word Dummy_ISR                 ; Channel 34
	.word Dummy_ISR                 ; Channel 35
	.word Dummy_ISR                 ; Channel 36
	.word Dummy_ISR                 ; Channel 37
	.word Dummy_ISR                 ; Channel 38
	.word Dummy_ISR                 ; Channel 39 
	.word Dummy_ISR                 ; Channel 40
	.word Dummy_ISR                 ; Channel 42
	.word Dummy_ISR                 ; Channel 42
	.word Dummy_ISR                 ; Channel 43
	.word Dummy_ISR                 ; Channel 44
	.word Dummy_ISR                 ; Channel 45
	.word Dummy_ISR                 ; Channel 46
	.word Dummy_ISR                 ; Channel 47 

    
;;*****************************************************************************
;;
;; This portion of the file goes into the text section.
;;
;;*****************************************************************************
    .thumb
    .text

;-------------------------------------------------------------------------------
;
; Copy the Flash API from flash to SRAM.
;

    .def     _copyAPI2RAM_
    .asmfunc

_copyAPI2RAM_

   .ref    api_load
flash_load   .word api_load
   .ref    api_run
flash_run   .word api_run
    .ref    api_size
flash_size  .word api_size

     ldr    r0, flash_load
     ldr    r1, flash_run
     ldr    r2, flash_size
     add    r2, r1, r2
copy_loop1:
     ldr     r3, [r0], #4
     str     r3, [r1], #4
     cmp     r1, r2
     blt     copy_loop1
      bx     lr
    .endasmfunc

;;*****************************************************************************
;;
;; The reset handler, which gets called when the processor starts.
;;
;;*****************************************************************************

mainSp .word __STACK_TOP    ;_Stack_Table_Pointer

    .thumbfunc _c_int00
_c_int00: .asmfunc
    ;;*------------------------------------------------------
	;; Initialize the Stack Pointer
   	;;*------------------------------------------------------
    ldr   r0, mainSp
    msr   msp, r0

    ;;
    ;; Initialize the processor, PLL clock, etc
    ;;
    bl      systemInit

   	;;
  	;; PERFORM AUTO-INITIALIZATION
   	;;
   	bl    _init

   	;;
   	;; Copy the Flash API from Flash to SRAM
   	;;
    bl    _copyAPI2RAM_

    ;;
    ;; Call main() function in which sciInit() is called
    ;;
   	bl    main

    ;;
    ;; See if an update should be performed.
    ;;
    .ref    CheckForceUpdate
    bl      CheckForceUpdate
    cbz     r0, CallApplication   ;is r0 (return value of CheckForceUpdate) is 0, jump to application

    ;;
    ;; If r0!=0, update is required. Configure the microcontroller.
    ;;
    .thumbfunc EnterBootLoader
EnterBootLoader:
 .if $$defined(CAN_ENABLE_UPDATE)
    .ref    ConfigureCANDevice
    bl      ConfigureCANDevice

    ;;
    ;; Branch to the update handler.
    ;;
    .ref    UpdaterCAN
    b       UpdaterCAN
 .endif

    .endasmfunc    ;;end of _c_int00
;;====== end of _c_int00 ========================================================
    ;;
    ;; This is a second symbol to allow starting the application from the boot
    ;; loader the linker may not like the perceived jump.
    ;;
    .global StartApplication
    .thumbfunc StartApplication
StartApplication:
    ;;
    ;; Call the application via the reset handler in its vector table.  Load
    ;; the address of the application vector table.
    ;;
    .thumbfunc CallApplication
CallApplication: .asmfunc
    ;;
    ;; Copy the application's vector table to the target address if necessary.
    ;; Note that incorrect boot loader configuration could cause this to
    ;; corrupt the code!  Setting VTABLE_START_ADDRESS to 0x0800_0000 (the start
    ;; of SRAM) is safe since this will use the same memory that the boot loader
    ;; already uses for its vector table.  Great care will have to be taken if
    ;; other addresses are to be used.
    ;;
 .if (APP_START_ADDRESS != VTABLE_START_ADDRESS)
    movw    r0, #(VTABLE_START_ADDRESS & 0xffff)
 .if (VTABLE_START_ADDRESS > 0xffff)
    movt    r0, #(VTABLE_START_ADDRESS >> 16)
 .endif
    movw    r1, #(APP_START_ADDRESS & 0xffff)
 .if (APP_START_ADDRESS > 0xffff)
    movt    r1, #(APP_START_ADDRESS >> 16)
 .endif

    ;;
    ;; Calculate the end address of the vector table assuming that it has the
    ;; maximum possible number of vectors.  We don't know how many the app has
    ;; populated so this is the safest approach though it may copy some non
    ;; vector data if the app table is smaller than the maximum.
    ;;
    movw    r2, #(70 * 4)
    adds    r2, r2, r0
VectorCopyLoop:
        ldr     r3, [r1], #4
        str     r3, [r0], #4
        cmp     r0, r2
        blt     VectorCopyLoop
 .endif

    ;;
    ;; Set the application's vector table start address.  Typically this is the
    ;; application start address but in some cases an application may relocate
    ;; this so we can't assume that these two addresses are equal.
    ;;
    movw    r0, #(VTABLE_START_ADDRESS & 0xffff)
 .if (VTABLE_START_ADDRESS > 0xffff)
    movt    r0, #(VTABLE_START_ADDRESS >> 16)
 .endif
    movw    r1, #(NVIC_VTABLE & 0xffff)
    movt    r1, #(NVIC_VTABLE >> 16)
    str     r0, [r1]

    ;;
    ;; Load the stack pointer from the application's vector table.
    ;;
 .if (APP_START_ADDRESS != VTABLE_START_ADDRESS)
    movw    r0, #(APP_START_ADDRESS & 0xffff)
 .if (APP_START_ADDRESS > 0xffff)
    movt    r0, #(APP_START_ADDRESS >> 16)
 .endif
 .endif
    ldr     sp, [r0]

    ;;
    ;; Load the initial PC from the application's vector table and branch to
    ;; the application's entry point.
    ;;
    ldr     r0, [r0, #4]
    bx      r0
    .endasmfunc    ;;callAppication


;;*****************************************************************************
;;
;; The NMI handler.
;;
;;*****************************************************************************
    .thumbfunc NmiSR
NmiSR: .asmfunc
    ;;
    ;; Loop forever since there is nothing that we can do about a NMI.
    ;;
    b       NmiSR
    .endasmfunc


;;*****************************************************************************
;;
;; The update handler, which gets called when the application would like to
;; start an update.
;;
;;*****************************************************************************
    .thumbfunc UpdateHandler
UpdateHandler: .asmfunc
    ;;
    ;; Initialize the processor.
    ;;
    bl      systemInit

   	;;
  	;; PERFORM AUTO-INITIALIZATION
   	;;
   	bl    _init

   	;;
   	;; Copy the Flash API from Flash to SRAM
   	;;
    bl    _copyAPI2RAM_

    ;;
    ;; Call main() function in which sciInit() is called
    ;;
   	bl    main
    ;;
    ;; Load the stack pointer from the vector table.
    ;;
    movs    r0, #0x0000
    ldr     sp, [r0]

    ;;
    ;; Branch to the update handler.
    ;;
 .if $$defined(CAN_ENABLE_UPDATE)
    .ref    AppUpdaterCAN
    b       AppUpdaterCAN
 .else
    b       Updater
 .endif
    .endasmfunc


;;*****************************************************************************
;;
;; The hard fault handler.
;;
;;*****************************************************************************
    .thumbfunc FaultISR
FaultISR: .asmfunc
    ;;
    ;; Loop forever since there is nothing that we can do about a hard fault.
    ;;
    b       FaultISR
    .endasmfunc

;;*****************************************************************************
;;
;; The SysTickIntHandler.
;;
;;*****************************************************************************
    .thumbfunc SysTickIntHandler
SysTickIntHandler: .asmfunc
    ;;
    ;; Loop forever since there is nothing that we can do about SysTickIntHandler.
    ;;
    b       SysTickIntHandler
    .endasmfunc

;;*****************************************************************************
;;
;; The Reserved.
;;
;;*****************************************************************************
    .thumbfunc  Reserved
Reserved: .asmfunc
    ;;
    ;; Loop forever since there is nothing that we can do about  Reserved
    ;;
    b        Reserved
    .endasmfunc

;;*****************************************************************************
;;
;; The Dummy_ISR
;;
;;*****************************************************************************
    .thumbfunc Dummy_ISR
Dummy_ISR: .asmfunc
    ;;
    ;; Loop forever since there is nothing that we can do about an unexpected
    ;; interrupt.
    ;;
    b       Dummy_ISR
    .endasmfunc


