/****************************************************************************\
 *           Copyright (C) 2013 Texas Instruments Incorporated.             *
 *                           All Rights Reserved                            *
 *                                                                          *
 * GENERAL DISCLAIMER                                                       *
 * ------------------                                                       *
 * All software and related documentation is provided "AS IS" and without   *
 * warranty or support of any kind and Texas Instruments expressly disclaims*
 * all other warranties, express or implied, including, but not limited to, *
 * the implied warranties of merchantability and fitness for a particular   *
 * purpose.  Under no circumstances shall Texas Instruments be liable for   *
 * any incidental, special or consequential damages that result from the    *
 * use or inability to use the software or related documentation, even if   *
 * Texas Instruments has been advised of the liability.                     *
***************************************************************************/
#include <stdio.h>
#include <csl_bootcfgAux.h>
#include <aintc_inth.h>

/*delay (in millisecond) before the timer generate one-shot pulse*/
#define PULSE_DELAY_MS 			2000
/*period (in millisecond) of the clock generated by timer*/
#define CLOCK_PERIOD_MS 		1
/*number of the clocks generated in this test*/
#define NUM_CLOCKS_GENERATED 	500
/*period (in millisecond) of the watch-dog timer*/
#define WATCH_DOG_PERIOD_MS 	3000


volatile Uint32 uiTick = 0;
extern Uint32 glbArmVectID;
#define RAW_SPI_NUM 4

CSL_Cortexa15GICRegs* ptrGIC400_Handle = 0x02561000;

//Interrupt service routine
void Interrupt_ISR(Uint32 arg)
{
	//printf("The SPI number is %d !\n", glbArmVectID);
    if(glbArmVectID == RAW_SPI_NUM+32)	// Check the GICC_IAR whether the SPI is from IPCGR8
    {
    	if((*((unsigned int *)0x02620260)>>4) == 0xFF)	// Check the IPCGR8 whether the interrupt source tag is from DSP Core0
    	{
    		uiTick++;
    		*((unsigned int *)0x026202A0) = 0xFF<<4;	// Clear the acknowledge in IPCAR8
    	    printf("Received interrupt from DSP Core0!\n");
    	}
    }
    return;
}

void Interrupt_Init(void)
{
	/* ARM initialization */
	arm_enable_irq();

    /* Initialize SW INTR table */
    INTH_InitHandler();

    /* Setup AINTC for event and plugin handler. */
    INTH_SetupInterrupt(RAW_SPI_NUM+32, Interrupt_ISR, 0, 5);

    /* Un-mask interrupt in AINTC */
    INTH_EnableInterrupt(RAW_SPI_NUM+32);
}

void main()
{
	Interrupt_Init();

	//GICD_Set_Pending(ptrGIC400_Handle, RAW_SPI_NUM+32);

    *((unsigned int *)0x02620240) = (0x5A<<4)|0x1;  // Set tag = 0x5A and Trigger the IPCGR0 to DSP Core0

    printf("Trigger the IPCGR0 to generate the interrupt to DSP Core0!\n");

    printf("Waiting for interrupt from DSP Core0!\n");
    
    while(uiTick == 0);

    printf("ARM Code finished");
    
    while(1);
    
}

