/*
 *******************************************************************************
 *
 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
 * ALL RIGHTS RESERVED
 *
 *******************************************************************************
 */

/**
  ******************************************************************************
 * \file system_dcan.c
 *
 *******************************************************************************
 */

/*******************************************************************************
 *  INCLUDE FILES
 *******************************************************************************
 */
#include "include/link_api/grpxSrcLink.h"
#include "include/link_api/dcanCtrl_api.h"
#include "utils_dcan.h"
#include <math.h>

#define SYSTEM_DCAN_TX_PRD_MS            (1000)
#define SYSTEM_DCAN_TX_TSK_PRI           (13)
#define SYSTEM_DCAN_RX_TSK_PRI           (13)
#define SYSTEM_DCAN_INTR_ID              (28)
/** \brief DCAN input clock - 20MHz */
#define SYSTEM_DCAN_INPUT_CLK            (20000000U)
/** \brief DCAN output bit rate - 1MHz */
#define SYSTEM_DCAN_BIT_RATE             (500000U)

/** \brief Used for enabling board-to-board DCAN */
#define ENABLE_B2B_DCAN_TEST             (1U)


static Void System_dcanRxMsgHandler(dcanMsg_t *rxMsg);
static Void System_dcanRxAckMsgHandler(dcanMsg_t *rxMsg);

dcanConfig_t System_dcanConfig;

static Void System_dcanInitCfgStruct(dcanConfig_t * dcanConfig)
{
    Int i = 0;
    UInt8 msgData[] = {0xAA,0xEE,0xBB,0xFF,0xCC,0xDD,0x55,0xAA};

#if ENABLE_B2B_DCAN_TEST
    dcanConfig->enableLoopback     = FALSE;
#else
    dcanConfig->enableLoopback     = TRUE;
#endif
    dcanConfig->enablePeriodicTx   = TRUE;
    dcanConfig->rxMsgId            = 0x500;
    dcanConfig->rxMsgId1           = 0x501;
    dcanConfig->rxMsgId2           = 0x502;
    dcanConfig->rxMsgId3           = 0x503;
    dcanConfig->rxMsgId4           = 0x504;
    dcanConfig->rxMsgId5           = 0x505;
    dcanConfig->rxMsgId6           = 0x506;
    dcanConfig->rxMsgId7           = 0x507;
    dcanConfig->rxMsgId8           = 0x508;
    dcanConfig->rxMsgId9           = 0x509;
    dcanConfig->rxMsgId10          = 0x50A;
    dcanConfig->rxMsgId11          = 0x50B;
    dcanConfig->rxMsgId12          = 0x50C;
    dcanConfig->rxMsgId13          = 0x50D;
    dcanConfig->rxMsgId14          = 0x50E;
    dcanConfig->rxMsgId15          = 0x50F;  
    dcanConfig->rxMsgId16          = 0x510;
    dcanConfig->rxMsgId17          = 0x511;
    dcanConfig->rxMsgId18          = 0x512;
    dcanConfig->rxMsgId19          = 0x513;
    dcanConfig->rxMsgId20          = 0x514;
    dcanConfig->rxMsgId21          = 0x515;
    dcanConfig->rxMsgId22          = 0x516;
    dcanConfig->rxMsgId23          = 0x517;
    dcanConfig->rxMsgId24          = 0x518;
    dcanConfig->rxMsgId25          = 0x519;
    dcanConfig->rxMsgId26          = 0x51A;
    dcanConfig->rxMsgId27          = 0x51B;
    dcanConfig->rxMsgId28          = 0x51C;
    dcanConfig->rxMsgId29          = 0x51D;
    dcanConfig->rxMsgId30          = 0x51E;
    dcanConfig->rxMsgId31          = 0x51F;
    dcanConfig->rxMsgId32          = 0x520;
    dcanConfig->rxMsgId33          = 0x521;
    dcanConfig->rxMsgId34          = 0x522;
    dcanConfig->rxMsgId35          = 0x523;
    dcanConfig->rxMsgId36          = 0x524;
    dcanConfig->rxMsgId37          = 0x525;
    dcanConfig->rxMsgId38          = 0x526;
    dcanConfig->rxMsgId39          = 0x527;
    dcanConfig->rxMsgId40          = 0x528;
    dcanConfig->rxMsgId41          = 0x529;
    dcanConfig->rxMsgId42          = 0x52A;
    dcanConfig->rxMsgId43          = 0x52B;
    dcanConfig->rxMsgId44          = 0x52C;
    dcanConfig->rxMsgId45          = 0x52D;
    dcanConfig->rxMsgId46          = 0x52E;
    dcanConfig->rxMsgId47          = 0x52F;
    dcanConfig->rxMsgId48          = 0x530;
    dcanConfig->rxMsgId49          = 0x531;
    dcanConfig->rxMsgId50          = 0x532;
    dcanConfig->rxMsgId51          = 0x533;
    dcanConfig->rxMsgId52          = 0x534;
    dcanConfig->rxMsgId53          = 0x535;
    dcanConfig->rxMsgId54          = 0x536;
    dcanConfig->rxMsgId55          = 0x537;
    dcanConfig->rxMsgId56          = 0x538;
    dcanConfig->rxMsgId57          = 0x539;
    dcanConfig->rxMsgId58          = 0x53A;
    dcanConfig->rxMsgId59          = 0x53B;
    dcanConfig->rxMsgId60          = 0x53C;
    dcanConfig->rxMsgId61          = 0x53D;
    dcanConfig->rxMsgId62          = 0x53E;
    dcanConfig->rxMsgId63          = 0x53F;
    dcanConfig->txMsgId            = 0xC2;
    dcanConfig->enableSendRxAck    = TRUE;
    dcanConfig->enableReceiveRxAck = FALSE;
    dcanConfig->txAckMsgId         = 0xC4;
    dcanConfig->rxAckMsgId         = 0xC5;
    dcanConfig->rxMsgCb            = System_dcanRxMsgHandler;
    dcanConfig->rxAckMsgCb         = System_dcanRxAckMsgHandler;
    dcanConfig->txMsgPeriod_ms     = SYSTEM_DCAN_TX_PRD_MS;
    dcanConfig->dcanCntrlIntrId    = SYSTEM_DCAN_INTR_ID;
    dcanConfig->dcanTxTskPri       = SYSTEM_DCAN_TX_TSK_PRI;
    dcanConfig->dcanRxTskPri       = SYSTEM_DCAN_RX_TSK_PRI;
    dcanConfig->dcanInputClk_hz    = SYSTEM_DCAN_INPUT_CLK;
    dcanConfig->dcanBaudRate_hz    = SYSTEM_DCAN_BIT_RATE;
    dcanConfig->enableTxMsgCycle   = TRUE;
    dcanConfig->dcanTxPrdMsg.dataLength = UTILS_ARRAYSIZE(msgData);
    for ( i = 0 ; i < UTILS_ARRAYSIZE(msgData); i++)
    {
        dcanConfig->dcanTxPrdMsg.msgData[i] = msgData[i];//send id data init
    }
}


UInt32 rock_can_send_cmd = 0;
UInt32 rock_can_test_data_rec_cnt = 0;
UInt32 rock_can_test_data_rec_err_cnt = 0;


static Void System_dcanRxMsgHandler(dcanMsg_t *rxMsg)
{
     
    Vps_printf("0x4AE3 C000: 0x%02x ",*(Uint32*)0x4AE3C000);
    Vps_printf("0x4AE3 C004: 0x%02x ",*(Uint32*)0x4AE3C004);
    Vps_printf("0x4AE3 C008: 0x%02x ",*(Uint32*)0x4AE3C008);
    Vps_printf("0x4AE3 C00C: 0x%02x ",*(Uint32*)0x4AE3C00C);
    Vps_printf("0x4AE3 C010: 0x%02x ",*(Uint32*)0x4AE3C010);
    Vps_printf("0x4AE3 C014: 0x%02x ",*(Uint32*)0x4AE3C014);
    Vps_printf("0x4AE3 C01C: 0x%02x ",*(Uint32*)0x4AE3C01C);
    Vps_printf("0x4AE3 C020: 0x%02x ",*(Uint32*)0x4AE3C020);
    Vps_printf("0x4AE3 C024: 0x%02x ",*(Uint32*)0x4AE3C024);
    Vps_printf("0x4AE3 C028: 0x%02x ",*(Uint32*)0x4AE3C028);
    Vps_printf("0x4AE3 C02C: 0x%02x ",*(Uint32*)0x4AE3C02C);
    Vps_printf("0x4AE3 C030: 0x%02x ",*(Uint32*)0x4AE3C030);
    Vps_printf("0x4AE3 C080: 0x%02x ",*(Uint32*)0x4AE3C080);
    Vps_printf("0x4AE3 C084: 0x%02x ",*(Uint32*)0x4AE3C084);
    Vps_printf("0x4AE3 C088: 0x%02x ",*(Uint32*)0x4AE3C088);
    Vps_printf("0x4AE3 C08C: 0x%02x ",*(Uint32*)0x4AE3C08C);
    Vps_printf("0x4AE3 C090: 0x%02x ",*(Uint32*)0x4AE3C090);
    Vps_printf("0x4AE3 C094: 0x%02x ",*(Uint32*)0x4AE3C094);
    Vps_printf("0x4AE3 C098: 0x%02x ",*(Uint32*)0x4AE3C098);
    Vps_printf("0x4AE3 C09C: 0x%02x ",*(Uint32*)0x4AE3C09C);
    Vps_printf("0x4AE3 C0A0: 0x%02x ",*(Uint32*)0x4AE3C0A0);
    Vps_printf("0x4AE3 C0A4: 0x%02x ",*(Uint32*)0x4AE3C0A4);
    Vps_printf("0x4AE3 C0A8: 0x%02x ",*(Uint32*)0x4AE3C0A8);
    Vps_printf("0x4AE3 C0AC: 0x%02x ",*(Uint32*)0x4AE3C0AC);
    Vps_printf("0x4AE3 C0B0: 0x%02x ",*(Uint32*)0x4AE3C0B0);
    Vps_printf("0x4AE3 C0B4: 0x%02x ",*(Uint32*)0x4AE3C0B4);
    Vps_printf("0x4AE3 C0B8: 0x%02x ",*(Uint32*)0x4AE3C0B8);
    Vps_printf("0x4AE3 C0BC: 0x%02x ",*(Uint32*)0x4AE3C0BC);
    Vps_printf("0x4AE3 C0C0: 0x%02x ",*(Uint32*)0x4AE3C0C0);
    Vps_printf("0x4AE3 C0C4: 0x%02x ",*(Uint32*)0x4AE3C0C4);
    Vps_printf("0x4AE3 C0C8: 0x%02x ",*(Uint32*)0x4AE3C0C8);
    Vps_printf("0x4AE3 C0CC: 0x%02x ",*(Uint32*)0x4AE3C0CC);
    Vps_printf("0x4AE3 C0D0: 0x%02x ",*(Uint32*)0x4AE3C0D0);
    Vps_printf("0x4AE3 C0D8: 0x%02x ",*(Uint32*)0x4AE3C0D8);
    Vps_printf("0x4AE3 C0DC: 0x%02x ",*(Uint32*)0x4AE3C0DC);
    Vps_printf("0x4AE3 C0E0: 0x%02x ",*(Uint32*)0x4AE3C0E0);
    Vps_printf("0x4AE3 C0E4: 0x%02x ",*(Uint32*)0x4AE3C0E4);
    Vps_printf("0x4AE3 C100: 0x%02x ",*(Uint32*)0x4AE3C100);
    Vps_printf("0x4AE3 C104: 0x%02x ",*(Uint32*)0x4AE3C104);
	Vps_printf("0x4AE3 C108: 0x%02x ",*(Uint32*)0x4AE3C108);
	Vps_printf("0x4AE3 C10C: 0x%02x ",*(Uint32*)0x4AE3C10C);
	Vps_printf("0x4AE3 C110: 0x%02x ",*(Uint32*)0x4AE3C110);	
	Vps_printf("0x4AE3 C114: 0x%02x ",*(Uint32*)0x4AE3C114);
	Vps_printf("0x4AE3 C120: 0x%02x ",*(Uint32*)0x4AE3C120);
	Vps_printf("0x4AE3 C124: 0x%02x ",*(Uint32*)0x4AE3C124);
	Vps_printf("0x4AE3 C128: 0x%02x ",*(Uint32*)0x4AE3C128);
	Vps_printf("0x4AE3 C12C: 0x%02x ",*(Uint32*)0x4AE3C12C);
	Vps_printf("0x4AE3 C130: 0x%02x ",*(Uint32*)0x4AE3C130);
	Vps_printf("0x4AE3 C134: 0x%02x ",*(Uint32*)0x4AE3C134);
	Vps_printf("0x4AE3 C140: 0x%02x ",*(Uint32*)0x4AE3C140);
	Vps_printf("0x4AE3 C144: 0x%02x ",*(Uint32*)0x4AE3C144);
	Vps_printf("0x4AE3 C148: 0x%02x ",*(Uint32*)0x4AE3C148);
	Vps_printf("0x4AE3 C14C: 0x%02x ",*(Uint32*)0x4AE3C14C);
	Vps_printf("0x4AE3 C150: 0x%02x ",*(Uint32*)0x4AE3C150);
	Vps_printf("0x4AE3 C154: 0x%02x ",*(Uint32*)0x4AE3C154);
	Vps_printf("0x4AE3 C160: 0x%02x ",*(Uint32*)0x4AE3C160);
	Vps_printf("0x4AE3 C164: 0x%02x ",*(Uint32*)0x4AE3C164);
	Vps_printf("0x4AE3 C168: 0x%02x ",*(Uint32*)0x4AE3C168);
	Vps_printf("0x4AE3 C16C: 0x%02x ",*(Uint32*)0x4AE3C16C);
	Vps_printf("0x4AE3 C1E0: 0x%02x ",*(Uint32*)0x4AE3C1E0);
	Vps_printf("0x4AE3 C1E4: 0x%02x ",*(Uint32*)0x4AE3C1E4);

    Vps_printf("CAN_ID: 0x%02x ",rxMsg->msgId);

	
#if ENABLE_B2B_DCAN_TEST
     GrpxSrcLink_StringRunTimePrintParams printPrms;
#endif

	if (rxMsg->appMsgPrms.dataLength)
	{
#if ENABLE_B2B_DCAN_TEST
         UTILS_COMPILETIME_ASSERT(UTILS_ARRAYSIZE(printPrms.stringInfo.string) >  0);
#endif

#if ENABLE_B2B_DCAN_TEST
/*
        snprintf(printPrms.stringInfo.string,
                 sizeof(printPrms.stringInfo.string) - 1,
                 "DCAN MSG ID:[0x%X]\n"
                 "DCAN MSG:0x%2X, 0x%2X, 0x%2X, 0x%2X, 0x%2X, 0x%2X, 0x%2X, 0x%2X",
                 rxMsg->msgId,
                 rxMsg->appMsgPrms.msgData[0],
                 rxMsg->appMsgPrms.msgData[1],
                 rxMsg->appMsgPrms.msgData[2],
                 rxMsg->appMsgPrms.msgData[3],
                 rxMsg->appMsgPrms.msgData[4],
                 rxMsg->appMsgPrms.msgData[5],
                 rxMsg->appMsgPrms.msgData[6],
                 rxMsg->appMsgPrms.msgData[7]);
*/


/* mask test code*/
/*
		if(rock_can_send_cmd == 1)
		{
			rock_can_test_data_rec_cnt++;
			if((rxMsg->appMsgPrms.msgData[0] == 0x12)&&(rxMsg->appMsgPrms.msgData[1] == 0x34)
				&&(rxMsg->appMsgPrms.msgData[2] == 0x56)&&(rxMsg->appMsgPrms.msgData[3] == 0x78)
				&&(rxMsg->appMsgPrms.msgData[4] == 0x9A)&&(rxMsg->appMsgPrms.msgData[5] == 0xBC)
				&&(rxMsg->appMsgPrms.msgData[6] == 0xDE)&&(rxMsg->appMsgPrms.msgData[7] == 0xF0))
				{
					//nop
				}
			else
				{
					rock_can_test_data_rec_err_cnt++;
				}

		}

		if((rxMsg->appMsgPrms.msgData[0] == 0x11)&&(rxMsg->appMsgPrms.msgData[1] == 0x22)
			&&(rxMsg->appMsgPrms.msgData[2] == 0x33)&&(rxMsg->appMsgPrms.msgData[3] == 0xAA)
			&&(rxMsg->appMsgPrms.msgData[4] == 0xBB)&&(rxMsg->appMsgPrms.msgData[5] == 0xCC)
			&&(rxMsg->appMsgPrms.msgData[6] == 0xDD)&&(rxMsg->appMsgPrms.msgData[7] == 0xFF))
			{
				Vps_printf("receive stop send cmd\n");
				rock_can_send_cmd = 0;
				rock_can_test_data_rec_cnt--;
				rock_can_test_data_rec_err_cnt--;
			}

		if(rock_can_send_cmd == 0)
		{
			if((rxMsg->appMsgPrms.msgData[0] == 0x11)&&(rxMsg->appMsgPrms.msgData[1] == 0x22)
				&&(rxMsg->appMsgPrms.msgData[2] == 0x33)&&(rxMsg->appMsgPrms.msgData[3] == 0xAA)
				&&(rxMsg->appMsgPrms.msgData[4] == 0xBB)&&(rxMsg->appMsgPrms.msgData[5] == 0xCC)
				&&(rxMsg->appMsgPrms.msgData[6] == 0xDD)&&(rxMsg->appMsgPrms.msgData[7] == 0xEE))
				{
					Vps_printf("receive send cmd\n");
					rock_can_send_cmd = 1;
				}
		}

	*/

        printPrms.stringInfo.string[sizeof(printPrms.stringInfo.string) - 1] = 0;
        printPrms.duration_ms = SYSTEM_DCAN_CTRL_DISPLAY_DURATION_MS;
        printPrms.stringInfo.fontType = SYSTEM_DCAN_CTRL_DISPLAY_FONTID;
        printPrms.stringInfo.startX  = SYSTEM_DCAN_CTRL_DISPLAY_STARTX;
        printPrms.stringInfo.startY  = SYSTEM_DCAN_CTRL_DISPLAY_STARTY;
        System_linkControl(IPU1_0_LINK(SYSTEM_LINK_ID_GRPX_SRC_0),
                           GRPX_SRC_LINK_CMD_PRINT_STRING,
                           &printPrms,
                           sizeof(printPrms),
                           TRUE);
#endif
    }
}


static Void System_dcanRxAckMsgHandler(dcanMsg_t *rxMsg)
{
    Vps_printf("DCAN TX ACk MsgId:%d,Length:%d,Payload[0]:0x%2x",
               rxMsg->msgId,
               rxMsg->appMsgPrms.dataLength,
               rxMsg->appMsgPrms.msgData[0]);
}


Void System_dcanInit()
{
    System_dcanInitCfgStruct(&System_dcanConfig);
    Utils_dcanInit(&System_dcanConfig);
}

Void System_dcanDeInit()
{
    Utils_dcanDeInit();
}



