/** @file sys_startup.c 
*   @brief System startup File
*   @date 15.March.2013
*   @version 03.05.00
*
*   This file contains:
*   - API Functions
*   .
*   which are relevant for the system Startup.
*/

/* (c) Texas Instruments 2009-2013, All rights reserved. */


/* Include Files */
#include "sys_common.h"
#include "system.h"
#include "sys_vim.h"

/* USER CODE BEGIN (0) */
/* USER CODE END */

typedef void (*handler_fptr)(const uint8 * in, uint8 * out);

#pragma WEAK(__TI_Handler_Table_Base)
#pragma WEAK(__TI_Handler_Table_Limit)
#pragma WEAK(__TI_CINIT_Base)
#pragma WEAK(__TI_CINIT_Limit)

extern uint32 __TI_Handler_Table_Base;
extern uint32 __TI_Handler_Table_Limit;
extern uint32 __TI_CINIT_Base;
extern uint32 __TI_CINIT_Limit;
extern uint32 *   __binit__;

#define NVIC  0xE000E000U

/*------------------------------------------------------------------------------*/
/* C auto-initialization 							*/
void _init()
{
/* initialize copy table */
    if ((uint32 *)&__binit__ != (uint32 *)0xFFFFFFFFU)
    {
        extern void copy_in(void *binit);
        copy_in((void *)&__binit__);
    }

    /* initialize the C global variables */
    if (&__TI_Handler_Table_Base < &__TI_Handler_Table_Limit)
    {
        uint8 **tablePtr   = (uint8 **)&__TI_CINIT_Base;
        uint8 **tableLimit = (uint8 **)&__TI_CINIT_Limit;

        while (tablePtr < tableLimit)
        {
            uint8 * loadAdr = *tablePtr++;
            uint8 * runAdr  = *tablePtr++;
            uint8  idx     = *loadAdr++;
            handler_fptr   handler = (handler_fptr)(&__TI_Handler_Table_Base)[idx];

            (*handler)((const uint8 *)loadAdr, runAdr);
        }
    }
}
/*------------------------------------------------------------------------------*/

/* USER CODE BEGIN (1) */
/* USER CODE END */

/*------------------------------------------------------------------------------*/
/* Enable interrupt Handling in NVIC 		                       	*/
void NVIC_enable()
{
	uint32 *SETINT0;  /* Irq 0 to 31 Set Enable Register	     	*/
	uint32 *PRIOR0;	  /* Irq 0 to 3 Priority Register			    */
	uint32 *PRIOR1;   /* Irq 4 to 7 Priority Register			    */
	uint32 *SYSHDLR;  /* System Handler Control and State Register 	*/


	SETINT0 	= (uint32 *)(NVIC + 0x100U);
	PRIOR0 		= (uint32 *)(NVIC + 0x400U);
	PRIOR1 		= (uint32 *)(NVIC + 0x404U);
	SYSHDLR     = (uint32 *)(NVIC + 0xD24U);

	*PRIOR0 	= 0x80a0c0e0U;
	*PRIOR1 	= 0x00204060U;
	*SYSHDLR    = 0x00070000U; 
	*SETINT0 	= 0x000000FFU;
	
#if VIM_CONFIG	
	vimCfg();	/* Configure M3VIM Channels properties */
#endif

#if (VIM_ENABLE_NESTED_INTERRUPTS  == TRUE)
	nestEnable();
#endif 
}
/*------------------------------------------------------------------------------*/

/* USER CODE BEGIN (2) */
/* USER CODE END */

/*------------------------------------------------------------------------------*/
/* Set ECP_CLK Pin  		                             	                */

void _set_ecp_clk()
{
  systemREG->SYSPC1  = 0x1U;                  /* ECP Pin as Functional */

  if(SYS_PLL == SYS_VCLK)
  {
     *(uint32 *)0xFFFFFFD4 = 0x00800000U;
  }
  else
  {	  
     systemREG->CLKTEST = (0x5U << 16U) | SYS_PLL;
  }
}

/* USER CODE BEGIN (3) */
/* USER CODE END */

/*- End of sys_startup.c--------------------------------------------------------*/



