/****************************************************************************
                           IPC test program
 Attention : IPC is part of Chip INTC.
 ****************************************************************************
 * Created by :                                                             *
 *            Zhaowei 	                                                    *
 *            DML403                                                        *
 *            Jul 23, 2012                                                  *
****************************************************************************/
#include <stdio.h>
#include <src\intc\csl_intc.h>
#include <csl_bootcfgAux.h>
#include <csl_chipAux.h>
//Intc variable declarartion
CSL_IntcContext             intcContext;
CSL_IntcEventHandlerRecord  EventHandler[30];
CSL_IntcObj                 intcObj;
CSL_IntcHandle              hTest;
CSL_IntcGlobalEnableState   state;
CSL_IntcEventHandlerRecord  EventRecord;
CSL_IntcParam               vectId;
void test_ipc_handler(void* handler)
{
	volatile uint32_t read_ipcgr;
	uint32_t CoreNum;
	CoreNum = CSL_chipReadReg(CSL_CHIP_DNUM);
	read_ipcgr = ((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[CoreNum];
	((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCAR[CoreNum] = read_ipcgr;
	printf("Core %d Receiving 0x%x\n",CoreNum,read_ipcgr);
	printf("You made it!\n");
}
void main(void)
{
	uint32_t core;
	int i;
	//init intc module
	core = CSL_chipReadReg(CSL_CHIP_DNUM);
	intcContext.eventhandlerRecord = EventHandler;
	intcContext.numEvtEntries = 30;
	CSL_intcInit(&intcContext);
	CSL_intcGlobalNmiEnable();
	CSL_intcGlobalEnable(&state);

	//open intc module
	vectId = CSL_INTC_VECTID_4;
	hTest = CSL_intcOpen(&intcObj, 91, &vectId, NULL);
	//Register an call-back handler which is invoked when the event occurs.
	EventRecord.handler = &test_ipc_handler;
	EventRecord.arg = 0;
	CSL_intcPlugEventHandler(hTest, &EventRecord);
	//Enabling the events.
	//CSL_intcHwControl(hTest, CSL_INTC_CMD_EVTCLEAR, NULL);
	CSL_intcHwControl(hTest, CSL_INTC_CMD_EVTENABLE, NULL);
	/*
	//Open the handle to the CPINT Instance
	hnd = CSL_CPINTC_open(0);
	//Disable all host interrupts.
	CSL_CPINTC_disableAllHostInterrupt(hnd);
	//Configure no nesting support in the CPINTC Module.
	CSL_CPINTC_setNestingMode(hnd, CPINTC_NO_NESTING);
	//Map System Interrupt 2 to channel 3
	//CSL_CPINTC_mapSystemIntrToChannel(hnd, 0, 3);
	//CSL_CPINTC_mapSystemIntrToChannel(hnd, 1, 3);
	CSL_CPINTC_mapSystemIntrToChannel(hnd, 2, 3);
	//CSL_CPINTC_mapSystemIntrToChannel(hnd, 3, 3);
	//Enable system interrupt 2
	//CSL_CPINTC_enableSysInterrupt(hnd, 0);
	//CSL_CPINTC_enableSysInterrupt(hnd, 1);
	CSL_CPINTC_enableSysInterrupt(hnd, 2);
	//CSL_CPINTC_enableSysInterrupt(hnd, 3);
	//Enable host interrupts.
	CSL_CPINTC_enableHostInterrupt(hnd, 3);
	//Enable all host interrupts also.
	CSL_CPINTC_enableAllHostInterrupt(hnd);
	*/

	//Unlock the BOOTCFG
	//*(volatile uint32_t *)0x02620038 = 0x83e70b13;
	//*(volatile uint32_t *)0x0262003c = 0x95a4f1e0;
	CSL_BootCfgUnlockKicker();
	//Generate the system interrupts */
	core = CSL_chipReadReg(CSL_CHIP_DNUM);
	if(0 == core)
	{
		int counter;
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[1] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[2] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[3] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[4] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[5] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[6] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
		((CSL_BootcfgRegs*)CSL_BOOT_CFG_REGS)->IPCGR[7] = 0xffffffff;
		for (counter = 0; counter < 50; counter++)
			asm(" nop 5");
	}

	while (1)
	{
		asm(" NOP 9");
	};
}
