

#include <machine/intrinsics.h>
#include <machine/cint.h>
#include <machine/wdtcon.h>

#include <tc27xc/IfxCan_reg.h>
#include <tc27xc/IfxCan_regdef.h>

#include <tc27xc/IfxScu_reg.h>
#include <tc27xc/IfxScu_regdef.h>

#include <tc27xc/IfxSrc_reg.h>
#include <tc27xc/IfxSrc_regdef.h>

#include <tc27xc/IfxPort_reg.h>
#include <tc27xc/IfxPort_regdef.h>

#include <tc27xc/IfxPort_reg.h>
#include <tc27xc/IfxPort_regdef.h>
/*Function prototypes*/

void ClcCanEnable(void);
void NewMessageAllocation(unsigned int MessageNumber,unsigned int ListIndex, unsigned int Command );
void SetUpMessage(unsigned int MessageNumber,unsigned int TransmitReception);
void InitIrqCanN(void);
void LoadDataToMessage(void);
void TestCanNode(void);
void UnlockWrite2CanNode(void);
void LockkWrite2CanNode(void);
void SendMessage(void);

void RxCAN1(int arg);

volatile unsigned int read = 0;
volatile unsigned int count = 0;
volatile unsigned int RxLow = 0;
volatile unsigned int RxHigh = 0;
volatile unsigned int DataLow = 0x1f1f1f1f;
volatile unsigned int DataHigh= 0x1b1b1b1b;


int main()
{

	ClcCanEnable();
	UnlockWrite2CanNode();
	NewMessageAllocation(0,1, 0x02 );
	NewMessageAllocation(1,2, 0x02 );
	SetUpMessage(0,1);
	SetUpMessage(1,0);
	InitIrqCanN();
	TestCanNode();
	LoadDataToMessage();
	LockkWrite2CanNode();

	SendMessage();

	return 0;
}


void RxCAN1(int arg)
{
	(void) arg;
	count +=1;
	while ((MODULE_CAN.MO[1].STAT.B.NEWDAT) !=1)//New Data, waiting for new data
				;
	RxHigh = MODULE_CAN.MO[1].DATAH.U;//Received data
	RxLow = MODULE_CAN.MO[1].DATAL.U;//Received data
}


void ClcCanEnable(void)
{

	unlock_wdtcon ();
		MODULE_CAN.CLC.U = 0;/*enable CAN Clock Control Register*/
		read = MODULE_CAN.CLC.U;/*read back written value*/
		/*********************MultiCAN+ Module Clock Generation***page 2322*********/
		MODULE_CAN.FDR.B.DM = 0b01;/*applies to normal divider mode*/
		MODULE_CAN.FDR.B.STEP = 0x3FF;/*Step Value Reload or addition value for the result.*/
	lock_wdtcon ();
	MODULE_CAN.MCR.B.CLKSEL = 1;/*Baud Rate Logic Clock Select*/

	MODULE_SRC.CAN.CAN[0].INT[1].B.SRPN = 10;//Interrupt prioriy
	MODULE_SRC.CAN.CAN[0].INT[1].B.SRE = 1;//Interrupt enable
	MODULE_SRC.CAN.CAN[0].INT[1].B.TOS = 0;//Cpu 0 servicing irq
	_install_int_handler(10,RxCAN1 ,0);
	_enable ();



	MODULE_CAN.N[0].BTR.U = 0x3ec9;//Bit Timing Register
	MODULE_CAN.N[1].BTR.U = 0x3ec9;//Bit Timing Register


}

void NewMessageAllocation(unsigned int MessageNumber,unsigned int ListIndex, unsigned int Command )
{
	while( (MODULE_CAN.PANCTR.B.BUSY |MODULE_CAN.PANCTR.B.RBUSY) == 1);//waiting to complete panel comand
	MODULE_CAN.PANCTR.B.PANAR1 = MessageNumber;/*Argument: Message Object Number */
	MODULE_CAN.PANCTR.B.PANAR2 = ListIndex;/*Argument: List Index */
	MODULE_CAN.PANCTR.B.PANCMD = Command; /*Panel Command*/



}
void SetUpMessage(unsigned int MessageNumber,unsigned int TransmitReception)
{
	if (TransmitReception == 1)
	{
		MODULE_CAN.MO[MessageNumber].FCR.B.DLC = 8;//Data Length Code
		MODULE_CAN.MO[MessageNumber].AR.B.PRI = 0b10;//Priority Class
		MODULE_CAN.MO[0].CTR.U = (1<<27)|(1<<26)|(1<<25)|(1<<21); //set DIR, TXEN1, TXEN0, MSGVAL, messge enable for transmition
	}
	if (TransmitReception == 0)
	{
		MODULE_CAN.MO[1].CTR.U = (1<<23)|(1<<21);// set up Set RXEN, SET MSGVAL, message set up for reception
	}
	MODULE_CAN.MO[MessageNumber].AR.B.ID = 0xff;//CAN Identifier of Message Object n
	MODULE_CAN.MO[MessageNumber].AR.B.IDE = 0 ;//1B Message object n handles extended frames with 29-bit identifier.
	MODULE_CAN.MO[MessageNumber].AR.B.PRI = 0b01;//Priority Class

}

void InitIrqCanN(void)
{

	MODULE_CAN.MO[1].IPR.B.RXINP = 0x1; //Receive Interrupt Node Pointer
	MODULE_CAN.MO[1].FCR.B.RXIE = 1;//Receive Interrupt Enable

}

void LoadDataToMessage(void)
{
	if ((MODULE_CAN.MO[0].STAT.B.TXEN0 & MODULE_CAN.MO[0].STAT.B.TXEN1 & MODULE_CAN.MO[0].STAT.B.DIR & MODULE_CAN.MO[0].STAT.B.MSGVAL ) == 1)
	{
		MODULE_CAN.MO[0].DATAH.U = DataHigh; //load data to registers, Upper and lower part
		MODULE_CAN.MO[0].DATAL.U = DataLow;
	}

}
void TestCanNode(void)
{
	MODULE_CAN.N[1].PCR.B.LBM = 1;//Loop Back Mode
	MODULE_CAN.N[0].PCR.B.LBM = 1;//Loop Back MODE
}

void UnlockWrite2CanNode(void)
{
	MODULE_CAN.N[0].CR.B.CCE = 1;//Configuration Change Enable
	MODULE_CAN.N[0].CR.B.INIT = 1;//Node Initialization
	MODULE_CAN.N[1].CR.B.CCE = 1;//Configuration Change Enable
	MODULE_CAN.N[1].CR.B.INIT = 1;//Node Initialization
}
void LockkWrite2CanNode(void)
{
	MODULE_CAN.N[0].CR.B.CCE = 0;//Configuration Change disable
	MODULE_CAN.N[0].CR.B.INIT = 0;//participation of the node in the CAN traffic.
	MODULE_CAN.N[1].CR.B.CCE = 0;//Configuration Change disable
	MODULE_CAN.N[1].CR.B.INIT = 0;//participation of the node in the CAN traffic.
}

void SendMessage(void)
{
	MODULE_CAN.MO[0].CTR.U = (1<<24)|(1<<19)|(1<<6); //Transmit message:SETTXRQ transmit request, SETNEWDAT,RESRTSEL,
}






