#include "CAN_XHY.h"
// #include "GPIO.h"
#include "Include.h"
#include "NM_Protocol.h"

// #define CANMsg_0X0CFE6CEE_CYCLE 25//24//50	 //50MS     24*2.048 =   49.152
// #define CANMsg_0X18FEC1EE_CYCLE 500//488//1000 //1000MS
// #define CANMsg_0X18FEFC17_CYCLE 500//488//1000 //1000MS     //488*2.048= 999.424
// #define CANMsg_0X0CFE6C17_CYCLE  25//24//50	 //50MS     24*2.048 =   49.152
/*#define CANMsg_0X18FF4117_CYCLE  	50
#define CANMsg_0X18FF4117_20_CYCLE  10
#define CANMsg_0X18FEE617_CYCLE  	250
#define CANMsg_0X18FF4017_CYCLE    	50
#define CANMsg_0X18FEC117_CYCLE    	500
#define CANMsg_0x18FF6F17_CYCLE    	50
#define CANMsg_0x18FEAE30_CYCLE    	500
#define CANMsg_0x18FEE6EE_CYCLE    	500*/

///////////////////�������ú����貹������ //////////////////////////////////////////////////////////////////////////////////
// ���ſ���
void MSCAN0_TJA1042_STB_SET(INT8U ON_OFF)
{
	PIN_54_P = ON_OFF;
}
// ���ŵ�ƽ��ȡ
INT8U MSCAN0_TJA1042_STB_GET(void)
{
	return PIN_54_P;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/*void CAN_TX_Data_K30_Init(void)
{
}*/

#if 0 // shican
void CAN_18FF4117_Import_Export_Service(void)
{
	CANMsg_IC_18FF4117_Status = 0;		     

	//��ʻ��δ��
	if (Line_In_Get_Status(LINE_IN_CabLock))
	{
		TX_IC_DriveCabReverseSW = 1;
	}
	else
	{
		TX_IC_DriveCabReverseSW = 0;
	}

	//����ȼ�
	if((Get_CAN_CH0_ID_18FE4021_Sig_BCM_TractorMarkerLight() ==1) ||(Get_CAN_CH0_ID_18FE4021_Sig_BCM_TractorMarkerLight() == 2))
	{
		TX_Backlightadjust  = BackLightCtrl.Level;
	}
	else
	{
		TX_Backlightadjust  = 0;
	}
	CANMsg_IC_18FF4117_Status = 1;
	memcpy(CANMsg_IC_18FF4117_Last.Data, CANMsg_IC_18FF4117.Data, 8);
}

void CAN_18FEE617_Import_Export_Service(void)
{
	CANMsg_IC_18FEE617_Status = 0;
	
	TX_Second = RTC_SECOND*4 ;
	TX_Minute = RTC_MINUTE ;
	TX_Hour = RTC_HOUR ;
	TX_Day = RTC_DATE*4 ;
	TX_Month = RTC_MONTH ;
	if(RTC_YEAR < 1985)
	{
		TX_Year = 0;	
	}
	else
	{
		TX_Year = RTC_YEAR-1985 ;	
	}
	CANMsg_IC_18FEE617_Status = 1;
	memcpy(CANMsg_IC_18FEE617.Data, CANMsg_IC_18FEE617.Data, 8);
}

void CAN_18FF4017_Import_Export_Service(void)
{
	uint16_t u16AvgSpd = 0;
	uint16_t u16Maintodo = 0;
	uint16_t u16AirPress1 = 0;
	uint16_t u16AirPress2 = 0;
	
	CANMsg_IC_18FF4017_Status = 0;	
	
	if(SYS_OPR_STAT_IGN_ON)
	{
		//����ʻ��ȫ��		
		if(Line_In_Get_Status(LINE_IN_Driver_SafeBelt))
		{
			TX_DrvSeatbeltBucklestatus = 0x01;
		}
		else
		{
			TX_DrvSeatbeltBucklestatus = 0x00;
		}

		//����ʻ��ȫ��(Ԥ��)
		if(Line_In_Get_Status(LINE_IN_Passenger_SafeBelt))
		{
			TX_PassSeatbeltWarning = 0x01;
		}
		else											    
		{
			TX_PassSeatbeltWarning = 0x00;
		}

		//�м䰲ȫ��
		if(Line_In_Get_Status(LINE_IN_Middle_SafeBelt))
		{
			TX_PassSeatMidPosbeltWarning = 0x01;
		}
		else
		{
			TX_PassSeatMidPosbeltWarning = 0x00;
		}

		//��ȫ����(Ԥ��)
		//����������ָʾ�ƣ� Ԥ����
		//�ƶ�Һλ��
		if ((Val_EOL_F101_Break() == 1))//Һɲ
		{
			if (Line_In_Get_Status(LINE_IN_BrakeLiquid))
			{
				TX_BrakeFuleLowWarn = 1;
			}
			else
			{
				TX_BrakeFuleLowWarn = 0;
			}
		}
		else
		{
			TX_BrakeFuleLowWarn = 0;
		}

		if(Val_EOL_F101_Break() == 2)//��ɲ	
		{
			//��ѹǰ��1
			if(AirPress_Front.u8_PressValid == 1)
			{
				u16AirPress1 = AirPress_Front.u16Press_Val/5;
			}
			else
			{
				u16AirPress1 = 1250/5;	
			}	 
			TX_FrontAirTankPressure =  u16AirPress1;
			//��ѹ����2		
			if(AirPress_Rear.u8_PressValid == 1)
			{
				u16AirPress2 = AirPress_Rear.u16Press_Val/5;
			}
			else
			{
				u16AirPress2 = 1250/5;	
			}
			TX_RearAirTankPressure =  u16AirPress2;
		}
		else
		{
			TX_FrontAirTankPressure = 0;
			TX_RearAirTankPressure = 0;
		}			
		
		//ȼ���ⷢ(Ԥ��)
		//ƽ������		
		if(DataAvgSpd.Display_Valid)
		{
			u16AvgSpd = DataAvgSpd.Display_Value;
			
			TX_AverageVehSpd = (INT8U)(u16AvgSpd);
		}
		else
		{
			TX_AverageVehSpd = 0;
		}

		//IC����ģʽ	
		if(Val_EOL_F101_Language() == 1)	
		{
			if(Get_CAN_CH0_ID_18FF4C41_Sig_MMI_IC_LanguageSet() == 1)
			{
				TX_LanguageMode = 0;
			}
			else if(Get_CAN_CH0_ID_18FF4C41_Sig_MMI_IC_LanguageSet() == 2)
			{
				TX_LanguageMode = 1;
			}
		}	
		else if(Val_EOL_F101_Language() == 2)	
		{
			if(Get_CAN_CH0_ID_18FF4C41_Sig_MMI_IC_LanguageSet() == 1)
			{
				TX_LanguageMode = 0;
			}
			else if(Get_CAN_CH0_ID_18FF4C41_Sig_MMI_IC_LanguageSet() == 2)
			{
				TX_LanguageMode = 1;
			}
		}

		//�������			
		u16Maintodo = (INT16U )(DataServiceInterval/10);
		if(u16Maintodo >50000)
		{
			u16Maintodo = 50000;	
		}
		TX_ServiceMaintainInterval_L = (uint8_t)(u16Maintodo & 0xFF);
		TX_ServiceMaintainInterval_H = (uint8_t)((u16Maintodo >> 8) & 0xFF);
		
		//��������				     
		if( (DataServiceWarning == 1)||(DataServiceDistanceWarning == 1)	 )
		{
			TX_MaintanceWarningSts = 0x01;
		}
		else
		{
			TX_MaintanceWarningSts = 0x00;	
		}
		
	}
	else
	{
		TX_DrvSeatbeltBucklestatus = 0;   
		//TX_PassSeatbeltWarning = 0;
		TX_PassSeatMidPosbeltWarning = 0;
		TX_BrakeFuleLowWarn = 0;
		TX_FrontAirTankPressure = 0;
		TX_RearAirTankPressure = 0;
		TX_AverageVehSpd = 0;
		TX_ServiceMaintainInterval_L = 0;
		TX_ServiceMaintainInterval_H = 0;
		TX_MaintanceWarningSts = 0; 
		
	}
	CANMsg_IC_18FF4017_Status = 1;
	memcpy(CANMsg_IC_18FF4017.Data, CANMsg_IC_18FF4017.Data, 8);
}

void CAN_18FEC117_Import_Export_Service(void)
{
	uint32_t u32temp = 0;
	CANMsg_IC_18FEC117_Status = 0;		     

	u32temp = NVM_ODO_VALUE;        //0.005
	u32temp *= 20;
	IC_TotalVehicle_Distance_1 =  (uint8_t)(u32temp & 0xFF);
	IC_TotalVehicle_Distance_2 = (uint8_t)((u32temp>>8) & 0xFF);
	IC_TotalVehicle_Distance_3 = (uint8_t)((u32temp>>16) & 0xFF);
	IC_TotalVehicle_Distance_4 =  (uint8_t)((u32temp>>24) & 0xFF);

	u32temp = DispTrip;
	u32temp *= 20;
	IC_Trip_Distance_1 =  (uint8_t)(u32temp & 0xFF);
	IC_Trip_Distance_2 = (uint8_t)((u32temp>>8) & 0xFF);
	IC_Trip_Distance_3 = (uint8_t)((u32temp>>16) & 0xFF);
	IC_Trip_Distance_4 =  (uint8_t)((u32temp>>24) & 0xFF);

	CANMsg_IC_18FEC117_Status = 1;
	memcpy(CANMsg_IC_18FEC117.Data, CANMsg_IC_18FEC117.Data, 8);
}


void CAN_18FF6F17_Import_Export_Service(void)
{
	CANMsg_IC_18FF6F17_Status = 0;		     

	if(SYS_OPR_STAT_IGN_ON)
	{
		if(TMPS_Learnning_Flag == TMPS_Export_LF)
		{
			IC_Manual_Learn_Location =  0x17;
			IC_Manual_Learn_Set  = 0x01;
		}
		else if(TMPS_Learnning_Flag == TMPS_Export_RF)
		{
			IC_Manual_Learn_Location = 0x19;	
			IC_Manual_Learn_Set  = 0x01;
		}
		else if(TMPS_Learnning_Flag == TMPS_Export_LR)
		{
			IC_Manual_Learn_Location = 0x26;	
			IC_Manual_Learn_Set  = 0x01;
		}
		else if(TMPS_Learnning_Flag == TMPS_Export_MLR)
		{
			IC_Manual_Learn_Location = 0x27;
			IC_Manual_Learn_Set  = 0x01;	
		}
		else if(TMPS_Learnning_Flag == TMPS_Export_MRR)
		{
			IC_Manual_Learn_Location = 0x29;	
			IC_Manual_Learn_Set  = 0x01;
		}
		else if(TMPS_Learnning_Flag == TMPS_Export_RR)
		{
			IC_Manual_Learn_Location = 0x2A;	
			IC_Manual_Learn_Set  = 0x01;
		}
		else if(TMPS_Learnning_Flag == TMPS_Export_Over)
		{
		 	IC_Manual_Learn_Set  = 0x00;
		}
	}
	else
	{
	}
	CANMsg_IC_18FF6F17_Status = 1;
	memcpy(CANMsg_IC_18FF6F17.Data, CANMsg_IC_18FF6F17.Data, 8);
}

void CAN_18FEAE30_Import_Export_Service(void)
{
	uint16_t u16AirPress1 = 0;
	uint16_t u16AirPress2 = 0;
	CANMsg_IC_18FEAE30_Status = 0;

	if(SYS_OPR_STAT_IGN_ON)
	{
		if(Val_EOL_F101_Break() == 2)//��ɲ	
		{
			//������ѹ��·1
			if(AirPress_Rear.u8_PressValid == 1)
			{
				u16AirPress1 = AirPress_Rear.u16Press_Val/8;
			}
			else
			{
				u16AirPress1 = 2000/8;	
			}	 
			TX_IC_ServiceBrakeCircuit1AirPressure =  u16AirPress1;
			//ǰ����ѹ��·2
			if(AirPress_Front.u8_PressValid == 1)
			{
				u16AirPress2 = (AirPress_Front.u16Press_Val)/8;	  
			}
			else
			{
				u16AirPress2 = 2000/8;	
			}
			TX_IC_ServiceBrakeCircuit2AirPressure =  u16AirPress2;
		}
		else
		{
			TX_IC_ServiceBrakeCircuit1AirPressure = 0x00;  
			TX_IC_ServiceBrakeCircuit2AirPressure = 0x00;     
		}   
	}
	else
	{
		TX_IC_ServiceBrakeCircuit1AirPressure = 0x00;  
		TX_IC_ServiceBrakeCircuit2AirPressure = 0x00;     
	}    
	CANMsg_IC_18FEAE30_Status = 1;
	memcpy(CANMsg_IC_18FEAE30.Data, CANMsg_IC_18FEAE30.Data, 8);
}

void CAN_18FEE6EE_Import_Export_Service(void)
{
	CANMsg_IC_18FEE6EE_Status = 0;
	
	TX_IC_Second = RTC_SECOND*4 ;
	TX_IC_Minute = RTC_MINUTE ;
	TX_IC_Hour = RTC_HOUR ;
	TX_IC_Month = RTC_DATE*4 ;
	TX_IC_Day = RTC_MONTH ;
	if(RTC_YEAR < 1985)
	{
		TX_IC_Year = 0;	
	}
	else
	{
		TX_IC_Year = RTC_YEAR-1985 ;	
	}
	CANMsg_IC_18FEE6EE_Status = 1;
	memcpy(CANMsg_IC_18FEE6EE.Data, CANMsg_IC_18FEE6EE.Data, 8);
}
#endif

#if 0
void CAN_0CFE6CEE_Import_Export_Service(void)
{
	uint32_t u32temp = 0;
	
	CANMsg_IC_0CFE6CEE_Status = 0; 

	if(SYS_OPR_STAT_IGN_ON)
	{
		if(DataVSpeedValid)
		{
			u32temp = DataVSpeedActual;
			u32temp *= 256;
			u32temp /= 10;
			if(u32temp >= 0XFFFF)
			{
				u32temp = 0XFFFF;
			}

			if(DataVSpeedActual >= 10)
			{
				CANMsg_IC_0CFE6CEE.BIT.VehSpeed_L = (uint8_t)(u32temp & 0xFF);
				CANMsg_IC_0CFE6CEE.BIT.VehSpeed_H = (uint8_t)((u32temp >> 8) & 0xFF);
			}
			else
			{
				CANMsg_IC_0CFE6CEE.BIT.VehSpeed_L = 0;
				CANMsg_IC_0CFE6CEE.BIT.VehSpeed_H = 0; 
			}
		}
		else
		{
			CANMsg_IC_0CFE6CEE.BIT.VehSpeed_L = 0;
			CANMsg_IC_0CFE6CEE.BIT.VehSpeed_H = 0; 
		}
	}
	else
	{
		CANMsg_IC_0CFE6CEE.BIT.VehSpeed_L = 0;
		CANMsg_IC_0CFE6CEE.BIT.VehSpeed_H = 0; 
	}
	//���ݸ�������
	CANMsg_IC_0CFE6CEE_Status =1;
	memcpy(CANMsg_IC_0CFE6CEE_Last.Data, CANMsg_IC_0CFE6CEE.Data, 8); 
	
}
#endif

#if 0
void CAN_18FEC1EE_Import_Export_Service(void)
{
	uint32_t u32temp = 0;
	//�����
	CANMsg_IC_18FEC1EE_Status = 0;

	u32temp = NVM_ODO_VALUE;        //0.005
	u32temp *= 20;
	CANMsg_IC_18FEC1EE.BIT.ODO_L =  (uint8_t)(u32temp & 0xFF);
	CANMsg_IC_18FEC1EE.BIT.ODO_M2 = (uint8_t)((u32temp>>8) & 0xFF);
	CANMsg_IC_18FEC1EE.BIT.ODO_M1 = (uint8_t)((u32temp>>16) & 0xFF);
	CANMsg_IC_18FEC1EE.BIT.ODO_H =  (uint8_t)((u32temp>>24) & 0xFF);


	u32temp = DispTrip;
	u32temp *= 20;
	CANMsg_IC_18FEC1EE.BIT.Trip_L =  (uint8_t)(u32temp & 0xFF);
	CANMsg_IC_18FEC1EE.BIT.Trip_M2 = (uint8_t)((u32temp>>8) & 0xFF);
	CANMsg_IC_18FEC1EE.BIT.Trip_M1 = (uint8_t)((u32temp>>16) & 0xFF);
	CANMsg_IC_18FEC1EE.BIT.Trip_H =  (uint8_t)((u32temp>>24) & 0xFF);


	CANMsg_IC_18FEC1EE_Status = 1;            
	memcpy(CANMsg_IC_18FEC1EE_Last.Data, CANMsg_IC_18FEC1EE.Data, 8);
}
#endif

#if 0
void CAN_0CFE6C17_Import_Export_Service(void)
{
	uint32_t u32temp = 0;

	CANMsg_IC_0CFE6C17_Status = 0; 
	if(SYS_OPR_STAT_IGN_ON)
	{
		if(DataVSpeedValid)
		{
			u32temp = DataVSpeedDisp;
			u32temp *= 256;
			u32temp /= 10;
			if(u32temp >= 0XFFFF)
			{
				u32temp = 0XFFFF;
			}

			if(DataVSpeedDisp >= 10)
			{
				TX_DisVehSpeed_L = (uint8_t)(u32temp & 0xFF);
				TX_DisVehSpeed_H = (uint8_t)((u32temp >> 8) & 0xFF);
			}
			else
			{
				TX_DisVehSpeed_L = 0;
				TX_DisVehSpeed_H = 0; 
			}
		}
		else
		{
			TX_DisVehSpeed_L = 0;
			TX_DisVehSpeed_H = 0; 
		}
	}
	else
	{
		TX_DisVehSpeed_L = 0;
		TX_DisVehSpeed_H = 0; 
	}
	//���ݸ�������
	CANMsg_IC_0CFE6C17_Status =1;
	memcpy(CANMsg_IC_0CFE6C17_Last.Data, CANMsg_IC_0CFE6C17.Data, 8); 
}
#endif

#if 0
void CAN_18FEFC17_Import_Export_Service(void)
{
	//ȼ�Ͱٷֱ�
	CANMsg_IC_18FEFC17_Status= 0;
	TX_FuelLevel1r = FuelData.Precent_Val*5/2 ;
	CANMsg_IC_18FEFC17_Status = 1;       
	memcpy(CANMsg_IC_18FEFC17_Last.Data, CANMsg_IC_18FEFC17.Data, 8);

	
}
#endif

#if 0
void CAN_18FEFA17_Import_Export_Service(void)
{
	CANMsg_IC_18FEFA17_Status = 0;		     

	if(SYS_OPR_STAT_IGN_ON)
	{
		if(Val_EOL_F110_EPB_Air() == 0)
		{
			if(Line_In_Get_Status(LINE_IN_Parking_Brake))
			{
				TX_ParkingBrakeActuator = 0x01;
			}
			else
			{
				TX_ParkingBrakeActuator = 0x00;
			}
		}
		else 
		{
			if(EPB_ParkBrake_Sts == 1)
			{
				TX_ParkingBrakeActuator = 0x01;
			}
			else
			{
				TX_ParkingBrakeActuator = 0x00;	
			}
			
		}
	}
	else
	{
		//TX_ParkingBrakeActuator = 0x00;    
	}  

	CANMsg_IC_18FEFA17_Status = 1;
	memcpy(CANMsg_IC_18FEFA17.Data, CANMsg_IC_18FEFA17.Data, 8);
	
}
#endif

#if 0
void CAN_18FEAE17_Import_Export_Service(void)
{
	uint16_t u16AirPress1 = 0;
	uint16_t u16AirPress2 = 0;
	CANMsg_IC_18FEAE17_Status = 0;		     



	if(SYS_OPR_STAT_IGN_ON)
	{
	//����1
		if(AirPress_Rear.u8_PressValid == 1)
		{
			u16AirPress1 = AirPress_Rear.u16Press_Val/8;
		}
		else
		{
			u16AirPress1 = 2000/8;	
		}	 
		IC_ServiceBrakeCircuit1_AirPre =  u16AirPress1;
		

	//ǰ��2		
		
		if(AirPress_Front.u8_PressValid == 1)
		{
			u16AirPress2 = (AirPress_Front.u16Press_Val)/8;	  
		}
		else
		{
			u16AirPress2 = 2000/8;	
		}
		IC_ServiceBrakeCircuit2_AirPre =  u16AirPress2;
	}
	else
	{
		IC_ServiceBrakeCircuit2_AirPre = 0x00;  
		IC_ServiceBrakeCircuit1_AirPre = 0x00;     
	}    

	CANMsg_IC_18FEAE17_Status = 1;
	memcpy(CANMsg_IC_18FEAE17.Data, CANMsg_IC_18FEAE17.Data, 8);
	
}
#endif



/*void CAN_APP_TX_Time_Reset(void)
{
	// CANMsg_IC_0CFE6CEE_Time_Count = CANMsg_0X0CFE6CEE_CYCLE;
	// CANMsg_IC_18FEC1EE_Time_Count = CANMsg_0X18FEC1EE_CYCLE-1;
	// CANMsg_IC_0CFE6C17_Time_Count = CANMsg_0X0CFE6C17_CYCLE-3;
	// CANMsg_IC_18FEFA17_Time_Count = CANMsg_0X18FEFA17_CYCLE-5;
	// CANMsg_IC_18FEFC17_Time_Count = CANMsg_0X18FEFC17_CYCLE-7;
	// CANMsg_IC_18FEAE17_Time_Count = CANMsg_0X18FEAE17_CYCLE-9 ;

	// CANMsg_IC_18FF4117_Time_Count = CANMsg_0X18FF4117_CYCLE;
	// CANMsg_IC_18FEE617_Time_Count = CANMsg_0X18FEE617_CYCLE-1;
	// CANMsg_IC_18FF4017_Time_Count = CANMsg_0X18FF4017_CYCLE-2;
	// CANMsg_IC_18FEC117_Time_Count = CANMsg_0X18FEC117_CYCLE-3;
	// CANMsg_IC_18FF6F17_Time_Count = CANMsg_0x18FF6F17_CYCLE-4;
	// CANMsg_IC_18FEAE30_Time_Count = CANMsg_0x18FEAE30_CYCLE-5;
	// CANMsg_IC_18FEE6EE_Time_Count = CANMsg_0x18FEE6EE_CYCLE-6;
}*/

// uint8_t RollingCounter_18FF1BEE;

// �ⷢ���ľ��巢�ͺ���
/*void CAN_Tx_Service(void)
{
	// CAN_TX_Data_K30_Init();

	// IGONʱ���Է���//IGOFF�����з���ʱ�����������

	if ((CAN_Control.APP_TX_Enable == Enable))
	{
#if 0 // shican
		//0X18FF4117										
		if((OTA_Ctrl.MsgExpEn == 1) ||(AvgEVCons.MsgExpEn == 1))
		{
			CANMsg_IC_18FF4117_Time =  CANMsg_0X18FF4117_20_CYCLE;
		}
		else
		{
			CANMsg_IC_18FF4117_Time =  CANMsg_0X18FF4117_CYCLE;	
		}
	
		CANMsg_IC_18FF4117_Time_Count++;
		
		if(CANMsg_IC_18FF4117_Time_Count >= CANMsg_IC_18FF4117_Time)
		{
			CANMsg_IC_18FF4117_Time_Count = 0;
			
			if((OTA_Ctrl.MsgExpEn !=1)&&(AvgEVCons.MsgExpEn != 1))
			{
				if(CANMsg_IC_18FF4117_Status)   
				{
					CAN0_TX_BUF_Set(CANMsg_IC_18FF4117.Data, 0X18FF4117, 8);
				}
				else
				{
					CAN0_TX_BUF_Set(CANMsg_IC_18FF4117_Last.Data, 0X18FF4117, 8);
				}	
				
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FF4117.Data, 0X18FF4117, 8);		
			}
			
			if(OTA_Ctrl.MsgExpEn)
			{
				OTA_Ctrl.MsgCnt++;
				if(OTA_Ctrl.MsgCnt >= 3)
				{
					OTA_Ctrl.MsgExpEn = 0;
					OTA_Ctrl.MsgCnt = 0;
					CANMsg_IC_18FF4117.Data[2] = 0x00;
					CANMsg_IC_18FF4117.Data[3] = 0x00;
				}	
			}
		         
			if(AvgEVCons.MsgExpEn )
			{
				AvgEVCons.MsgCnt++;
				if(AvgEVCons.MsgCnt >= 3)
				{
					AvgEVCons.MsgExpEn = 0;
					AvgEVCons.MsgCnt = 0;
					CANMsg_IC_18FF4117.BIT.IC_ResetAveragePowerConsumption = 0;
				}
			}	
		}


		// 0X18FEE617
		CANMsg_IC_18FEE617_Time_Count++;
		if(CANMsg_IC_18FEE617_Time_Count >= CANMsg_0X18FEE617_CYCLE)
		{
			CANMsg_IC_18FEE617_Time_Count = 0;
			if(CANMsg_IC_18FEE617_Status) 
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEE617.Data, 0X18FEE617, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEE617_Last.Data, 0X18FEE617, 8);
			}
		}

		//0X18FF4017
		CANMsg_IC_18FF4017_Time_Count++ ;
		if(CANMsg_IC_18FF4017_Time_Count >= CANMsg_0X18FF4017_CYCLE)
		{
			CANMsg_IC_18FF4017_Time_Count = 0; 
			if(CANMsg_IC_18FF4017_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FF4017.Data, 0X18FF4017, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FF4017_Last.Data, 0X18FF4017, 8);
			}
		}

		//0x18FEC117
		CANMsg_IC_18FEC117_Time_Count++ ;
		if(CANMsg_IC_18FEC117_Time_Count >= CANMsg_0X18FEC117_CYCLE)
		{
			CANMsg_IC_18FEC117_Time_Count = 0; 
			if(CANMsg_IC_18FEC117_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEC117.Data, 0X18FEC117, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEC117_Last.Data, 0X18FEC117, 8);
			}
		}

		//0x18FF6F17
		if(TMPS_Learnning_Flag != TMPS_Export_Max)
		{
			CANMsg_IC_18FF6F17_Time_Count++ ;
			if(CANMsg_IC_18FF6F17_Time_Count >= CANMsg_0x18FF6F17_CYCLE)
			{
				CANMsg_IC_18FF6F17_Time_Count = 0; 
				CAN_18FF6F17_Import_Export_Service();
				if(CANMsg_IC_18FF6F17_Status)
				{
					CAN0_TX_BUF_Set(CANMsg_IC_18FF6F17.Data, 0x18FF6F17, 8);
				}
				else
				{
					CAN0_TX_BUF_Set(CANMsg_IC_18FF6F17_Last.Data, 0x18FF6F17, 8);
				}
				
				if(TMPS_Learnning_Flag == TMPS_Export_Over)
				{
					TMPS_Learnning_Flag = TMPS_Export_Max;	
				}
			}
		}
		else
		{
			CANMsg_IC_18FF6F17_Time_Count = 0;	
		}

		//0x18FEAE30
		CANMsg_IC_18FEAE30_Time_Count++ ;   
		if(CANMsg_IC_18FEAE30_Time_Count >= CANMsg_0x18FEAE30_CYCLE)
		{
			CANMsg_IC_18FEAE30_Time_Count = 0; 
			if(CANMsg_IC_18FEAE30_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEAE30.Data, 0X18FEAE30, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEAE30_Last.Data, 0X18FEAE30, 8);
			}
		}

		//0x18FEE6EE
		CANMsg_IC_18FEE6EE_Time_Count++ ;   
		if(CANMsg_IC_18FEE6EE_Time_Count >= CANMsg_0x18FEE6EE_CYCLE)
		{
			CANMsg_IC_18FEE6EE_Time_Count = 0; 
			if(CANMsg_IC_18FEE6EE_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEE6EE.Data, 0X18FEE6EE, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEE6EE_Last.Data, 0X18FEE6EE, 8);
			}
		}
#endif

#if 0
		//0X0CFE6CEE
		CANMsg_IC_0CFE6CEE_Time_Count++;
		if (CANMsg_IC_0CFE6CEE_Time_Count >= CANMsg_0X0CFE6CEE_CYCLE)
		{
			CANMsg_IC_0CFE6CEE_Time_Count = 0;
			if (CANMsg_IC_0CFE6CEE_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_0CFE6CEE.Data, 0XCFE6CEE, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_0CFE6CEE_Last.Data, 0XCFE6CEE, 8);
			}
		}
#endif

#if 0
		//0X18FEC1EE
		CANMsg_IC_18FEC1EE_Time_Count++;
		if (CANMsg_IC_18FEC1EE_Time_Count >= CANMsg_0X18FEC1EE_CYCLE)
		{
			CANMsg_IC_18FEC1EE_Time_Count = 0;
			if (CANMsg_IC_18FEC1EE_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEC1EE.Data, 0X18FEC1EE, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEC1EE_Last.Data, 0X18FEC1EE, 8);
			}
		}
#endif

#if 0	
		//0X0CFE6C17	
		CANMsg_IC_0CFE6C17_Time_Count++;										   
		if (CANMsg_IC_0CFE6C17_Time_Count >= CANMsg_0X0CFE6C17_CYCLE)        
		{
			CANMsg_IC_0CFE6C17_Time_Count = 0;
			if (CANMsg_IC_0CFE6C17_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_0CFE6C17.Data, 0X0CFE6C17, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_0CFE6C17_Last.Data, 0X0CFE6C17, 8);
			}
		}
#endif
#if 0
		//0X18FEFC17	
		CANMsg_IC_18FEFC17_Time_Count++;
		if (CANMsg_IC_18FEFC17_Time_Count>= CANMsg_0X18FEFC17_CYCLE)
		{
			CANMsg_IC_18FEFC17_Time_Count= 0;
			if (CANMsg_IC_18FEFC17_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEFC17.Data, 0X18FEFC17, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEFC17_Last.Data, 0X18FEFC17, 8);
			}
		}
#endif

#if 0
		//0x18FEFA17
		CANMsg_IC_18FEFA17_Time_Count++ ;
		if(CANMsg_IC_18FEFA17_Time_Count >= CANMsg_0X18FEFA17_CYCLE)
		{
			CANMsg_IC_18FEFA17_Time_Count = 0; 
			if(CANMsg_IC_18FEFA17_Status)
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEFA17.Data, 0X18FEFA17, 8);
			}
			else
			{
				CAN0_TX_BUF_Set(CANMsg_IC_18FEFA17_Last.Data, 0X18FEFA17, 8);
			}
		}
#endif

		// #if(IC_Current == LD02)
		// CANMsg_IC_18FEAE17_Time_Count++ ;
		// if(CANMsg_IC_18FEAE17_Time_Count >= CANMsg_0X18FEAE17_CYCLE)
		//{
		//	CANMsg_IC_18FEAE17_Time_Count = 0;
		//	if(CANMsg_IC_18FEAE17_Status)
		//	{
		//		CAN0_TX_BUF_Set(CANMsg_IC_18FEAE17.Data, 0X18FEAE17, 8);
		//	}
		//	else
		//	{
		//		CAN0_TX_BUF_Set(CANMsg_IC_18FEC117_Last.Data, 0X18FEAE17, 8);
		//	}
		// }
		// #endif
	}
}*/

#define MSCAN0_BUS_OFF_LV1 0x00
#define MSCAN0_BUS_OFF_LV2 0x01

#define MSCAN0_BUS_OFF_LV1_RECOVERY_TIME 100  // 50             //Bus-off??????,??��ms
#define MSCAN0_BUS_OFF_LV2_RECOVERY_TIME 1000 // Bus-off??????,??��ms
typedef struct
{
	INT8U Status;
	INT8U Timer;
	INT8U Cnt;
	INT8U Event;
} MSCAN0BusoffMonitorStruct;
MSCAN0BusoffMonitorStruct MSCAN0Busoff;
INT8U TxCounter;
void BusoffRecover(void)
{
	if (TxCounter > 0)
	{
		TxCounter--;
	}
	else
	{
		MSCAN0Busoff.Status = MSCAN0_BUS_OFF_LV1;
		MSCAN0Busoff.Timer = 0;
		MSCAN0Busoff.Cnt = 0;
		BusOffEvent = 0u;
	}
	// MSCAN0Busoff.Event=0;//????????????????????????????
}
// 10ms????
void NM_D_BusOff_Recover(void)
{
	//???Bus-off????
	if (GetBusOffState())
	{
		TxCounter = 4;
		MSCAN0Busoff.Timer++;
		BusOffEvent = 1u;
		if (MSCAN0Busoff.Status == MSCAN0_BUS_OFF_LV1)
		{
			if (MSCAN0Busoff.Timer >= MSCAN0_BUS_OFF_LV1_RECOVERY_TIME / 10)
			{
				MSCAN0Busoff.Timer = 0;
				AbortAllCan0Tx();

				CAN0TIER_TXEIE = 0x00;

				CAN0_TX_BUF_Clear();
				// ���շ��ͱ��ĵļ�ʱ��ظ�λ
				// CAN_APP_RX_Time_Reset();//shican
				//CAN_APP_TX_Time_Reset();
				CAN0MISC_BOHOLD = 1; // д1����,����ָ�Bus-off
				MSCAN0Busoff.Event = 1;
				MSCAN0Busoff.Cnt++;
				if (MSCAN0Busoff.Cnt >= 10)
				{
					BUS_OFF_FLAG = 1U;
					MSCAN0Busoff.Status = MSCAN0_BUS_OFF_LV2;
				}
				Can_Write_Fun(&CAN_CH0_CanMsgTxOp, 50000u);

					//NM_ACTION_4
			}
		}
		else if (MSCAN0Busoff.Status == MSCAN0_BUS_OFF_LV2)
		{
			if (MSCAN0Busoff.Timer >= MSCAN0_BUS_OFF_LV2_RECOVERY_TIME / 10)
			{
				MSCAN0Busoff.Timer = 0;
				if (MSCAN0Busoff.Cnt < 50)
				{
					MSCAN0Busoff.Cnt++;
				}

				AbortAllCan0Tx();

				CAN0TIER_TXEIE = 0x00;

				CAN0_TX_BUF_Clear();
				// ���շ��ͱ��ĵļ�ʱ��ظ�λ
				// CAN_APP_RX_Time_Reset();//shican
				//CAN_APP_TX_Time_Reset();
				CAN0MISC_BOHOLD = 1; // д1����,����ָ�Bus-off
				MSCAN0Busoff.Event = 1;
				Can_Write_Fun(&CAN_CH0_CanMsgTxOp, 50000u);
					//NM_ACTION_4
			}
		}
	}
	else // ��������,û��Bus-off����
	{
		// MSCAN0Busoff.Status = MSCAN0_BUS_OFF_LV1;
		// MSCAN0Busoff.Timer = 0;
		// MSCAN0Busoff.Cnt = 0;
		// BusOffEvent = 0u;
		//	MSCAN0Busoff.Event=0;//????????????????????????????
	}
}
//////////////
#pragma CODE_SEG __NEAR_SEG NON_BANKED // �������жϺ�������FLASH�ķǷ�ҳ��

/******************************************************************************
���i����MSCAN0_RX_ISR
��  �ܣ�MSCAN0�����G�Ϸs���i��
		���GCAN�����ʹ�ʱ������CAN����
��  �i���S
����ֵ���S
******************************************************************************/

// CAN0 ����Ϊ��׼֡ʱ�Ľ����жϺ���
//  void interrupt MSCAN0_Standard_RX_ISR(void)
//  {
//  	INT32U CANMsgID;
//  	INT8U i;
//  	INT8U *P_Data = &CAN0RXDSR0;
//  	CAN0RIER_RXFIE = 0;					  //�Ƚ�ֹMSCAN�����ж�
//  	if (CAN0RXIDR1_IDE || CAN0RXIDR1_SRR) //����յ�������Ϊ��չ֡,��ΪԶ��֡
//  	{
//  		CAN0RFLG_RXF = 1;	//������жϱ�־
//  		CAN0RIER_RXFIE = 1; //��������MSCAN�����ж�

// 		return;
// 	}

// 	CANMsgID = MSCAN0_GET_Stand_ID();

// 	//////////////////////////////////////////////////////////////////////////////////////////////////
// 	//////////////////////////////////////////////////////////////////////////////////////////////////
// 	/////////////�������Ӿ�����մ�������/////////////////////////////////////////////////////////////////////////////////////

// 	// if ((CANMsgID >= NM_CAN_ID_BASE) && (CANMsgID <= NM_CAN_ID_BOTTOM)) //�����������
// 	// {
// 	// 	NM_Receive_isr_Fun(CANMsgID, &CAN0RXDSR0);
// 	// }
// 	// else if ((CANMsgID == DIAG_ID_Rx_PHY) || (CANMsgID == DIAG_ID_Rx_FUN))
// 	// {
// 	// 	DoCAN_L_Data_Indication(CANMsgID, (CAN0RXDLR_DLC), &CAN0RXDSR0);
// 	// }
// 	// else
// 	// {
// 	// 	CoCAN_L_Data_Indication(CANMsgID, (CAN0RXDLR_DLC), &CAN0RXDSR0);
// 	// }

// 	/////////////////�������Ӿ�����մ�������/////////////////////////////////////////////////////////////////////////////////
// 	//////////////////////////////////////////////////////////////////////////////////////////////////
// 	//////////////////////////////////////////////////////////////////////////////////////////////////

// 	CAN0RFLG_RXF = 1;	//������жϱ�־
// 	CAN0RIER_RXFIE = 1; //�������������ж�
// }

// CAN0 ����Ϊ��չ֡ʱ�Ľ����жϺ���
void interrupt MSCAN0_Extended_RX_ISR(void)
{
	INT32U CANMsgID;
	NetworkMode_State_t m_condition1;
	NM_Mode_t m_condition2;
	//	INT32U CANMsgPGN;

	// INT8U i;
	// INT8U *P_Data = &CAN0RXDSR0;
	CAN0RIER_RXFIE = 0;							 // �Ƚ�ֹMSCAN�����ж�
	if ((0 == CAN0RXIDR1_IDE) || CAN0RXIDR3_RTR) // ����յ�������Ϊ��׼֡,��ΪԶ��֡
	{
		CAN0RFLG_RXF = 1;	// ������жϱ�־
		CAN0RIER_RXFIE = 1; // ��������MSCAN�����ж�

		return;
	}
	CANMsgID = MSCAN0_GET_Extended_ID();

	//////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////

	if ((CANMsgID >= 0x18FFFA00) && (CANMsgID <= 0x18FFFAFF))
	{
		// �����������
		CanNm_Data_Indication(CANMsgID, (CAN0RXDLR_DLC), &CAN0RXDSR0);
	}
	else if ((CANMsgID == DIAG_ID_Rx_PHY) || (CANMsgID == DIAG_ID_Rx_FUN))
	{
		/*�յ��������֮��ά���������*/
		m_condition1 = CanNm_GetState();
		m_condition2 = CanNm_GetMode();
		if (m_condition2 == Network_Mode)
		{
			if ((m_condition1 == Ready_Sleep_State) || (m_condition1 == Normal_Operation_State))
			{
				CanNm_NetworkRequest(NM_REQ_DIAG_RECVD);
				NMReStartTimer(NM_Tdiag_Timer);
			}
			DoCAN_L_Data_Indication(CANMsgID, (CAN0RXDLR_DLC), &CAN0RXDSR0);
		}
	}
	else
	{
  	if((CAN_Control.APP_RX_Enable & APP_Enable) == APP_Enable) 
  	{
  	    Co_Can_Buff_Set(&CAN_CH0_CanMsgOp, (uint16_t)Co_Can_ConvertSubID_CAN_CH0(CANMsgID), CAN0RXDLR_DLC, &CAN0RXDSR0);
  	}
		
		ReceivedMsg(CANMsgID, CAN0RXDLR_DLC);
	}
	// Power_Wait_Bus_Sleep_Count=0;
	Power_OFF_Delay_Time = 0;
	MSCAN0_Set_Exist_Frame();

	//////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////

	CAN0RFLG_RXF = 1;	// ������жϱ�־
	CAN0RIER_RXFIE = 1; // �������������ж�
}
/******************************************************************************
���i����MSCAN0_WAKEUP_ISR
��  �ܣ�MSCAN0��G��G��
��  �i���S
����ֵ���S
******************************************************************************/
void interrupt MSCAN0_WAKEUP_ISR(void)
{

	MSCAN0_Exit_Low_Power_Mode();
	//	MSCAN0_Init();
	//	Power_Wait_Bus_Sleep_Count=0;
	Power_OFF_Delay_Time = 0;
	CAN0RFLG_WUPIF = 1; // ���־
}
/******************************************************************************
���i����MSCAN0_TX_ISR
��  �ܣ�MSCAN0�����G�Ϸs���i��
		���GCAN���ķ�����ϻ��Gֹ����ʱ��ִGG�˺��i
��  �i���S
����ֵ���S
******************************************************************************/
void interrupt MSCAN0_TX_ISR(void)
{
	CAN0_TX_DATA_BUF();
}

#pragma CODE_SEG DEFAULT