#ifndef __define__
#define __define__

#include "math.h"


#define EVB_BOARD       1
#define Pllab_BOARD     2

#define BOARD_TYPE      EVB_BOARD


// #define DEBUG_TEST

// #define EMC_TEST

/* ------------------------------------------------------------------------ */

typedef unsigned long			u64;
typedef unsigned int			u32;
typedef int						s32;
typedef unsigned short			u16;
typedef unsigned char			u8;
typedef signed char				s8;

/* ------------------------------------------------------------------------ */
#ifndef	TRUE
#define	TRUE		1   		
#endif

#ifndef	FALSE
#define	FALSE		0   		
#endif

/* ------------------------------------------------------------------------ */

#define APP_TRUE				0
#define APP_FALSE				1
#define APP_ERROR				2

#define APP_ON					1
#define APP_OFF					0

#define APP_HIGH				1
#define APP_LOW					0

/* ------------------------------------------------------------------------ */

#define MAX_COMMAND_BUF			256

/* ------------------------------------------------------------------------ */

//CAM FD MB 0~23 TX_MB 0~3 RX_MB 4~23
#define RX_MB_IDX			MAX_TX_MB
#define TX_MB_IDX			0U

#define RX_MB_IDX_1ch		MAX_TX_MB_1ch

#define MAX_RX_MB			32U
#define MAX_TX_MB			14U

#define MAX_RX_MB_1ch		32U
#define MAX_TX_MB_1ch		28U


#define MAX_TX_RING			200
#define MAX_RX_RING			100

#define MAX_CAN_BUFFER		32 //max can length

/* ------------------------------------------------------------------------ */

#define P_WR(ch)					CAN_ch[ch].p_wr
#define P_RD(ch)					CAN_ch[ch].p_rd

/* ------------------------------------------------------------------------ */

//�궗�뼇�꽌�쓽 寃쎌슦 1~6梨꾨꼸, 肄붾뱶�긽�뿉�꽌�뒗 0~5梨꾨꼸, 肄붾뱶湲곗��쑝濡� �옉�꽦�븯���쓬


/*ECU3 CH0*/
/*tx id*/
#define ECU3_CH0_RWA_VCU_RC_Signal1		0x050
#define ECU3_CH0_RWA_VCU_RC_Signal2 	0x051
#define ECU3_CH0_RWS_VCU_RC_Signal1 	0x060
#define ECU3_CH0_RWS_VCU_RC_Signal2 	0x061
#define ECU3_CH0_VCU_BCM_Signal_1		0x150
#define ECU3_CH0_VCU_MCU_Signals1		0x250
#define ECU3_CH0_VCU_RC_Signals1		0x010
#define ECU3_CH0_VCU_RC_Signals2		0x011
#define ECU3_VCU_EPAM					0x220


/*rx id*/
#define ECU3_CH0_BCM_VCU_Signal1		0x570
#define ECU3_CH0_BCM_VCU_Signal2		0x580
#define ECU3_CH0_BCM_VCU_Signal3		0x590
#define ECU3_CH0_BCM_VCU_Signal4		0x595
#define ECU3_CH0_BCM_VCU_Signal5		0x585
#define ECU3_CH0_MCU_VCU_Signals1		0x300
#define ECU3_CH0_MCU_VCU_Signals2		0x301
#define ECU3_CH0_RC_VCU_Signal1			0x040
#define ECU3_CH0_RC_VCU_Signal2			0x041
#define ECU3_CH0_RX_IMU_TX1				0x174
#define ECU3_CH0_RX_IMU_TX2				0x178
#define ECU3_CH0_RX_IMU_TX3				0x17C
#define ECU3_RC_VCU_BCM_Signal_1		0x155
#define ECU3_EPAM_VCU					0x221
#define ECU3_CAR_ARC					0x012
#define ECU3_Receive_Debugging			0x777
#define ECU3_RC_VCU_Fault_Flag			0x042


/*ECU3 CH1*/
/*tx id*/
#define ECU3_CH1_BCM_VCU_ACU_Signal1	0x570
#define ECU3_CH1_BCM_VCU_ACU_Signal2	0x580
#define ECU3_CH1_BCM_VCU_ACU_Signal3	0x590
#define ECU3_CH1_BCM_VCU_ACU_Signal4	0x595
#define ECU3_CH1_BCM_VCU_ACU_Signal5	0x585
#define ECU3_CH1_RWA_VCU_ACU_Signal1	0x050
#define ECU3_CH1_RWA_VCU_ACU_Signal2	0x051
#define ECU3_CH1_RWS_VCU_ACU_Signal1	0x060
#define ECU3_CH1_RWS_VCU_ACU_Signal2	0x061
#define ECU3_CH1_VCU_ACU_Signal1		0x013
#define ECU3_CH1_VCU_ACU_Signal2		0x014

/*rx id*/
#define ECU3_CH1_ACU_VCU_Signal1		0x091
#define ECU3_CH1_ACU_VCU_Signal2		0x092
#define ECU3_CH1_ACU_VCU_Signal3		0x093
#define ECU3_CH1_ACU_BCM_Signal_1		0x151


/*ECU3 CH2*/
/*tx id*/
#define ECU3_CH2_BSC_IDB_BrakeCommand	0x064
#define ECU3_CH2_BSC_IMU_				0x070
#define ECU3_CH2_BSC_IDB_StartupCommand 0x032
#define ECU3_CH2_CH3_B_I_U_Message_1	0x797
#define ECU3_CH2_BSC_RegenCommand		0x06A
#define ECU3_CH2_BSC_RWA_RackCommand	0x040
#define ECU3_CH2_BSC_RWA_Sister			0x350
#define ECU3_CH2_BSC_RWA_StartupCommand 0x310
#define ECU3_CH2_BSC_VehicleEnvironment 0x384
#define ECU3_CH2_IDB_WheelSpeedStatus	0x088


/*rx id*/
#define ECU3_CH2_IDB_BrakeStatus1		0x09A
#define ECU3_CH2_IDB_BrakeStatus2		0x0C8
#define ECU3_CH2_IDB_PressureStatus		0x0A0
#define ECU3_CH2_IDB_RegenTcsRequest	0x082
#define ECU3_CH2_IDB_Status				0x092
#define ECU3_CH2_IDB_TireSlipStatus		0x096
#define ECU3_CH2_IDB_UdsResponse		0x798
#define ECU3_CH2_IDB_WheelStatus		0x08C
#define ECU3_CH2_RWA_Status1			0x072
#define ECU3_CH2_RWA_Status2			0x073
#define ECU3_CH2_RWA_Status3			0x0AA

/*ECU3 CH3*/
/*tx id*/
#define ECU3_CH3_TX_BSC_IMU					0x071
#define ECU3_CH3_TX_BSC_RCU_BrakeCommand	0x065
#define ECU3_CH3_TX_BSC_RWA_RackCommand		0x040
#define ECU3_CH3_TX_BSC_RWA_Sister			0x350
#define ECU3_CH3_TX_BSC_RWA_StartupCommand	0x310
#define ECU3_CH3_TX_BSC_RCU_StartupCommand	0x033
#define ECU3_CH3_TX_BSC_RCU_UdsRequest		0x797
#define ECU3_CH3_TX_BSC_VehicleEnvironment	0x385
#define ECU3_CH3_TX_RCU_WheelSpeedStatus	0x089

/*rx id*/
#define ECU3_CH3_RX_RCU_BrakeStatus1	0x09B
#define ECU3_CH3_RX_RCU_BrakeStatus2	0x0C9
#define ECU3_CH3_RX_RCU_PressureStatus	0x0A1
#define ECU3_CH3_RX_RWA_Status1			0x072
#define ECU3_CH3_RX_RWA_Status2			0x073
#define ECU3_CH3_RX_RWA_Status3			0x0AA
#define ECU3_CH3_RX_RCU_TireSlipStatus	0x097
#define ECU3_CH3_RX_RCU_WheelStatus		0x08D
#define ECU3_CH3_RX_RCU_Status			0x093
#define ECU3_CH3_RX_RCU_UdsResponse		0x799


/*ECU3 CH4*/
/*tx id*/
#define ECU3_CH4_VCU_RWS_Rack_Cmd		0x040
#define ECU3_CH4_VCU_RWS_StartupCommand 0x310

/*rx id*/
#define ECU3_CH4_RX_RWS_VCU_Rack_AngleFbk	0x073
#define ECU3_CH4_RX_RWS_VCU_stat_Signal		0x072

/*ECU3 CH5*/
/*tx id*/
#define ECU3_CH5_VCU_RWS_Rack_Cmd		0x040
#define ECU3_CH5_VCU_RWS_StartupCommand 0x310

/*rx id*/
#define ECU3_CH5_RWS_VCU_Rack_AngleFbk	0x073
#define ECU3_CH5_RWS_VCU_stat_Signal	0x072


/* ------------------------------------------------------------------------ */

typedef	void (*user_callback)( int param );

/* ------------------------------------------------------------------------ */


#define PIT_INST_0				0U
#define CH_0					0U
//#define PIT_PERIOD				40000		// 1ms
#define PIT_PERIOD				20000		// 500Usec

/* ------------------------------------------------------------------------ */

#define ADC_INST_0				0U
#define ADC_INST_1				1U
#define ADC_INST_2				2U

#define MAX_ADC_RETRY			10

#define ADC_AN_6				6U
#define ADC_AN_2				2U
#define ADC_AN_3				3U
#define ADC_AN_4				4U
#define ADC_AN_5				5U
#define ADC_AN_43				43U

/* ------------------------------------------------------------------------ */

#define MAX_CAN_INST			6
#define CAN_INST_0				0U
#define CAN_INST_1				1U
#define CAN_INST_2				2U
#define CAN_INST_3				3U
#define CAN_INST_4				4U
#define CAN_INST_5				5U

/* ------------------------------------------------------------------------ */
#define DIN_0               0U
#define DIN_1               1U
#define DIN_2               2U
#define DIN_3               3U
#define DIN_4               4U
#define DIN_5               5U
#define DIN_6               6U
#define DIN_7               7U

#define DOUT_0              0U
#define DOUT_1              1U
#define DOUT_2              2U
#define DOUT_3              3U
#define DOUT_4              4U
#define DOUT_5              5U
#define DOUT_6              6U
#define DOUT_7              7U

#define ADC_0               0U
#define ADC_1               1U
#define ADC_2               2U
#define ADC_3               3U
#define ADC_4               4U
#define AIN_BAT             5U

/* ------------------------------------------------------------------------ */
#define MAX_PWM					8

#define PWM_EMIOS_0         0U

#define PWM_CH0             0U
#define PWM_CH1             1U
#define PWM_CH2             2U
#define PWM_CH3             3U

#define PWM_FREQ				50000					// 50KHz
#define PWM_INIT_MAX_TICK		160000000/PWM_FREQ

/* ------------------------------------------------------------------------ */

#define HW_LPI2C1				1		// Hardware I2C
#define I2C_SLAVE    1U
#define I2C_MASTER   0U
#define TRANSFER_SIZE 8U


/* ------------------------------------------------------------------------ */
/* --- software I2C --- */
//#define I2C_CROSS
#ifndef I2C_CROSS
	#define I2C_DELAY				500
	#define I2C_SDA(x)				{if(x) Siul2_Dio_Ip_WritePin(I2C0_SDA_PORT, I2C0_SDA_PIN, 1U); 	else Siul2_Dio_Ip_WritePin(I2C0_SDA_PORT, I2C0_SDA_PIN, 0U);}
	#define I2C_SCL(x)				{if(x) Siul2_Dio_Ip_WritePin(I2C0_SCL_PORT, I2C0_SCL_PIN, 1U); 	else Siul2_Dio_Ip_WritePin(I2C0_SCL_PORT, I2C0_SCL_PIN, 0U);}
	#define I2C_SDI					((Siul2_Dio_Ip_ReadPins(I2C0_SDA_PORT)&0x00000001)?1:0)
#else
	#define I2C_DELAY				500
	#define I2C_SDA(x)				{if(x) Siul2_Dio_Ip_WritePin(I2C0_SCL_PORT, I2C0_SCL_PIN, 1U); 	else Siul2_Dio_Ip_WritePin(I2C0_SCL_PORT, I2C0_SCL_PIN, 0U);}
	#define I2C_SCL(x)				{if(x) Siul2_Dio_Ip_WritePin(I2C0_SDA_PORT, I2C0_SDA_PIN, 1U); 	else Siul2_Dio_Ip_WritePin(I2C0_SDA_PORT, I2C0_SDA_PIN, 0U);}
	#define I2C_SDI					((Siul2_Dio_Ip_ReadPins(I2C0_SCL_PORT)&0x00000002)?1:0)
#endif

/* ------------------------------------------------------------------------ */


#define I2C_START		{ I2C_SDA(APP_HIGH); i2c_delay(I2C_DELAY); I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY);\
						  I2C_SDA(APP_LOW);  i2c_delay(I2C_DELAY); I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY);}

#define I2C_STOP		{ I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY); I2C_SDA(APP_LOW);  i2c_delay(I2C_DELAY);\
						  I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY);   \
						  I2C_SDA(APP_HIGH); i2c_delay(I2C_DELAY);   }

#define I2C_WDATA(a)	{ I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY); I2C_SDA(a);        i2c_delay(I2C_DELAY);\
						  I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY); I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY);}

#define I2C_RDATA(a)	{ I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY);   \
						  a<<=1; a|=I2C_SDI; i2c_delay(I2C_DELAY);   \
						  I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY);   }

#define I2C_GETACK(a)	{ i2c_delay(I2C_DELAY); I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY);\
						  a|=I2C_SDI; i2c_delay(I2C_DELAY); I2C_SCL(APP_LOW); i2c_delay(I2C_DELAY);}

#define I2C_SETNACK		{ I2C_SDA(APP_HIGH); i2c_delay(I2C_DELAY); I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY);\
						  I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY); I2C_SDA(APP_HIGH); i2c_delay(I2C_DELAY);}

#define I2C_SETACK		{ I2C_SDA(APP_LOW);  i2c_delay(I2C_DELAY); I2C_SCL(APP_HIGH); i2c_delay(I2C_DELAY);\
						  I2C_SCL(APP_LOW);  i2c_delay(I2C_DELAY); I2C_SDA(APP_HIGH); i2c_delay(I2C_DELAY);}

/* ------------------------------------------------------------------------ */

#define LIN_INSTANCE_1 0
#define LIN_INSTANCE_2 1

#define ID_1 0x1A
#define ID_2 0x2A
#define BUFFER_SIZE         3U
#define T_LIN_TIME_OUT      400000

#endif


