//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>

#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/hal/Hwi.h>
#include <ti/sysbios/knl/Swi.h>
#include <ti/sysbios/knl/Mailbox.h>
#include <ti/sysbios/knl/Semaphore.h>


#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_can.h"
#include "can.h"
#include "interrupt.h"
#include "sysctl.h"
#include "gpio.h"
#include "pin_map.h"

#include "basic_type.h"
#include "rom.h"
#include "rom_map.h"
#include "mydriver_type.h"
#include "os_framework.h"
#include "hw_ints.h"

#ifdef NEUSOFT_SET_CAN   //CAN module driver
extern void cfg_can0( void );
extern void cfg_can1( void );
extern void init_can0can1_input();
extern void init_can1_input();
//Type_uByte CAN1ERRORSTATA;
//Type_uByte CAN3ERRORSTATA;




/**********************MCU side functions**************************/
Type_uByte wubCANDrv_StandbyOut(void)
{

	 Type_uByte i ;
	//CAN1ERRORSTATA=0;
	//CAN3ERRORSTATA=0;


     //CAN initialization for CAN1
     cfg_can1() ;

     //SPI initialization for CAN2 by SSI1
     /*cfg_can2_spi1() ;*/		/* 20140724 for bdnt */

     //SPI initialization for CAN4 by SSI3
     //cfg_can3_spi3() ;


     GPIOIntDisable(GPIO_PORTE_BASE, GPIO_PIN_6);

     /*init_can2();*/	/* 20140724 for bdnt */

   //  init_can3();

     //config PE6 interrupt for can2&can3
     cfg_PE6_interrupt() ;

     init_can1_input() ;
	 
#if 1
     /*GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0) ;*//* 20140724 delete can2 */
     GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0) ;
#endif

     return RES_OK ;

}



void init_can1_input()
{

     tCANMsgObject sCANMsgObject ;
   
     //CAN1 Configure
     sCANMsgObject.ui32MsgID = 0x068;               // CAN msg ID
     sCANMsgObject.ui32MsgIDMask = 0xFFFFF;          // mask, all 20 bits must match
     sCANMsgObject.ui32Flags = (MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER  );
     sCANMsgObject.ui32MsgLen = 8;

     //CAN1 REC setting
     ROM_CANMessageSet(CAN1_BASE, 1, &sCANMsgObject, MSG_OBJ_TYPE_RX);

     
	 /* wqs add for Geely */

	  sCANMsgObject.ui32MsgID = 0x2A1;
     ROM_CANMessageSet(CAN1_BASE, 2, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x094;
     ROM_CANMessageSet(CAN1_BASE, 3, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x28A;
     ROM_CANMessageSet(CAN1_BASE, 4, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x115;
     ROM_CANMessageSet(CAN1_BASE, 5, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x08B;
     ROM_CANMessageSet(CAN1_BASE, 6, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x0A1;
     ROM_CANMessageSet(CAN1_BASE, 7, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x26D;
     ROM_CANMessageSet(CAN1_BASE, 8, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x268;
	  sCANMsgObject.ui32MsgLen = 3;
     ROM_CANMessageSet(CAN1_BASE, 9, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 	sCANMsgObject.ui32MsgLen = 8;

	  sCANMsgObject.ui32MsgID = 0x230;
     ROM_CANMessageSet(CAN1_BASE, 10, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 
	  sCANMsgObject.ui32MsgID = 0x2A6;
     ROM_CANMessageSet(CAN1_BASE, 11, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  sCANMsgObject.ui32MsgID = 0x36A;
     ROM_CANMessageSet(CAN1_BASE, 12, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	   sCANMsgObject.ui32MsgID = 0x36C;
     ROM_CANMessageSet(CAN1_BASE, 13, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	/* guoshuming add for CAN Diagnostic Start*/

	  sCANMsgObject.ui32MsgID = 0x741;
     ROM_CANMessageSet(CAN1_BASE, 14, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	   sCANMsgObject.ui32MsgID = 0x7df;
     ROM_CANMessageSet(CAN1_BASE, 15, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	/* guoshuming add for CAN Diagnostic End*/
	sCANMsgObject.ui32MsgID = 0x377;
     ROM_CANMessageSet(CAN1_BASE, 16, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 
	sCANMsgObject.ui32MsgID = 0x618;
     ROM_CANMessageSet(CAN1_BASE, 17, &sCANMsgObject, MSG_OBJ_TYPE_RX);


	 sCANMsgObject.ui32MsgID = 0x3B1;
     ROM_CANMessageSet(CAN1_BASE, 18, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 
	 sCANMsgObject.ui32MsgID = 0x381;
     ROM_CANMessageSet(CAN1_BASE, 19, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 sCANMsgObject.ui32MsgID = 0x3B3;
     ROM_CANMessageSet(CAN1_BASE, 20, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	  sCANMsgObject.ui32MsgID = 0x356;
     ROM_CANMessageSet(CAN1_BASE, 21, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 	 sCANMsgObject.ui32MsgID = 0x0e2;
     ROM_CANMessageSet(CAN1_BASE, 22, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	  sCANMsgObject.ui32MsgID = 0x611;
     ROM_CANMessageSet(CAN1_BASE, 23, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	  // sCANMsgObject.ui32MsgID = 0x27A;
    // ROM_CANMessageSet(CAN1_BASE, 24, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 

	 
	 sCANMsgObject.ui32MsgID = 0x0E2;
     ROM_CANMessageSet(CAN1_BASE, 24, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 sCANMsgObject.ui32MsgID = 0x7f0;
	 ROM_CANMessageSet(CAN1_BASE, 25, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	   sCANMsgObject.ui32MsgID = 0x088;
     ROM_CANMessageSet(CAN1_BASE, 26, &sCANMsgObject, MSG_OBJ_TYPE_RX);

	 	   sCANMsgObject.ui32MsgID = 0x36e;
     ROM_CANMessageSet(CAN1_BASE, 27, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 	 	   sCANMsgObject.ui32MsgID = 0x082;
     ROM_CANMessageSet(CAN1_BASE, 28, &sCANMsgObject, MSG_OBJ_TYPE_RX);
	 	 	 	   sCANMsgObject.ui32MsgID = 0x621;
     ROM_CANMessageSet(CAN1_BASE, 29, &sCANMsgObject, MSG_OBJ_TYPE_RX);


     return ;
}


#endif

void cfg_PE6_interrupt()
{
     //PE6 work as interrupt, Low lever trigger, for CAN2
     ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE) ;
     ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_6) ;

     //Set weak pull up for PE6 interrupt low level trigger
     ROM_GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_6, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
     //GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_6, GPIO_LOW_LEVEL) ;// GPIO_FALLING_EDGE
     ROM_GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_6, GPIO_LOW_LEVEL ) ; //GPIO_FALLING_EDGE
     GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_6) ;
     ROM_IntEnable(INT_GPIOE) ;
     ROM_IntPrioritySet(INT_GPIOE, 0x00);

     return ;
}


void cfg_can1( void )
{


     //For JTA1043T configuration on CAN1 port
	ROM_SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA ) ;

	ROM_GPIOPinConfigure( GPIO_PA0_CAN1RX ) ;
	ROM_GPIOPinConfigure( GPIO_PA1_CAN1TX ) ;

	ROM_GPIOPinTypeCAN( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1) ;

     //PA2 work as CAN_ERR_N
	ROM_GPIOPinTypeGPIOInput( GPIO_PORTA_BASE, GPIO_PIN_2) ;

     //PA3 work as CAN_EN
	ROM_GPIOPinTypeGPIOOutput( GPIO_PORTA_BASE, GPIO_PIN_3) ;
	ROM_GPIOPinTypeGPIOOutput( GPIO_PORTA_BASE, GPIO_PIN_4) ;

     //Enable TJA1043T U3004 part
	ROM_GPIOPinWrite( GPIO_PORTA_BASE, GPIO_PIN_3 | GPIO_PIN_4, GPIO_PIN_3 | GPIO_PIN_4) ;

     //Enable CAN0 port
	ROM_SysCtlPeripheralEnable( SYSCTL_PERIPH_CAN1 ) ;
	ROM_SysCtlPeripheralReset( SYSCTL_PERIPH_CAN1 ) ;
	ROM_CANInit( CAN1_BASE ) ;
	ROM_CANBitRateSet( CAN1_BASE, SysCtlClockGet(), 500000) ;
     //  (*((volatile Type_uWord *)(0x4004100c)))=0x1649;
       
     #if 1
       Type_uHWord ui16CANCTL;
	 ui16CANCTL=0x00;
	 ui16CANCTL = HWREG(CAN1_BASE + CAN_O_CTL);
                HWREG(CAN1_BASE + CAN_O_CTL) = ui16CANCTL | CAN_CTL_INIT |
                                              CAN_CTL_CCE;
     HWREG(CAN1_BASE + CAN_O_BIT) =0x164F ; //sample point 80% for geely
 
     
     HWREG(CAN1_BASE + CAN_O_CTL) =ui16CANCTL;

#endif
 
	 
	   

	ROM_CANIntEnable( CAN1_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS) ;
	ROM_IntEnable( INT_CAN1 ) ;
	ROM_CANEnable( CAN1_BASE ) ;

     //Disable the Status Interrupt to avoid too many interrupt
     //Because all transmit and recieve get interrupt no matter with filter situation
     HWREG(CAN1_BASE + CAN_O_CTL) &=~0x04 ;

     return ;

}

Void hwi_CAN1_fxn(UArg arg)
{

     unsigned long ulStatus;
 		 ulStatus = ROM_CANIntStatus(CAN1_BASE, CAN_INT_STS_CAUSE);
		 OS_SetEventFlag(FLG_ID_UDS, 1);
		 ROM_CANIntClear(CAN1_BASE, ulStatus);

     return ;

}




