/********************************************************************************
 * 常州易控汽车电子股份有限公司
 * (c) Copyright 2009-2015 ECTEK
 * 保留所有权利
 ********************************************************************************
 * 工程名称：整车管理
 * 文件名： CANNet.c
 * 功能描述 ： CAN总线管理
 * 引用记录：
 * 备注：
 * 修订记录：
 *		日期		      版本		  作者		描述
 *   2011.07.14          00.01.00      李中     第一版
 *******************************************************************************/

#include "HardwareLib.h"
#include "CANNet.h"
#include "AccPedCD.h"
#include "BleCanProtocolFunc.h"
#include "BcuCanProtocolFunc.h"
#include "VcuCanProtocolFunc.h"
MCANDrv_infoMsgConfS_TYPE CANNet_infoMsgTxConfSA[3];
MCAN_stRetE_TYPE CAN1Result;
uint16 CANNet_idxSchedUB;
MCANDrv_infoMsgConfS_TYPE CANNet_InfoRxMsgConfSA[128];
uint8 CANNet_datRxMsgBufM[128][9];
void CANNet_Init(void)
{
	/*示例代码*/

	/*各个CAN节点初始化*/
	MCANDrv_SetBaudrate(_CAN_INDEX_CAN0, 250000);
	MCANDrv_SetCANMode(_CAN_INDEX_CAN0, MCAN_NODEMODE_BUF);
	MCANDrv_SetCallbackFunc(_CAN_INDEX_CAN0, (pFunc_RxMsgCallback)CANNet_RecvMsgCAN0, NULL_PTR);

	MCANDrv_SetBaudrate(_CAN_INDEX_CAN1, 250000);
	MCANDrv_SetCANMode(_CAN_INDEX_CAN1, MCAN_NODEMODE_BUF);
	MCANDrv_SetCallbackFunc(_CAN_INDEX_CAN1, (pFunc_RxMsgCallback)CANNet_RecvMsgCAN1, NULL_PTR);

	/*第2路CAN用于CCP、UDS协议，由底层配置*/
	// MCANDrv_SetCallbackFunc(_CAN_INDEX_CAN2, (pFunc_RxMsgCallback)CANNet_RecvMsgCAN2, NULL_PTR);

	/*接收报文初始化，主要配置CAN2*/
	CANNet_InfoRxMsgConfSA[0].infoMsgIDUI = 0x1CEF0000UL;
	CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.CAN = 2;
	CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.MO = 3;
	CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.DIR = _MCAN_DIR_RX;
	CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.DLC = 8;
	CANNet_InfoRxMsgConfSA[0].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
	MCANDrv_SetMsgObj(CANNet_InfoRxMsgConfSA[0], CANNet_RecvMsgCAN2);

	CANNet_InfoRxMsgConfSA[1].infoMsgIDUI = 0x1CEF00F1UL;
	CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.CAN = 2;
	CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.MO = 4;
	CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.DIR = _MCAN_DIR_RX;
	CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.DLC = 8;
	CANNet_InfoRxMsgConfSA[1].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
	MCANDrv_SetMsgObj(CANNet_InfoRxMsgConfSA[1], CANNet_RecvMsgCAN2);

	/*发送报文初始化*/
	CANNet_infoMsgTxConfSA[0].infoMsgIDUI = 0x1F000024UL;
	CANNet_infoMsgTxConfSA[0].infoMsgParU.B.CAN = 0;
	CANNet_infoMsgTxConfSA[0].infoMsgParU.B.MO = 0;
	CANNet_infoMsgTxConfSA[0].infoMsgParU.B.DIR = _MCAN_DIR_TX;
	CANNet_infoMsgTxConfSA[0].infoMsgParU.B.DLC = 8;
	CANNet_infoMsgTxConfSA[0].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
	// MCANDrv_SetMsgObj(CANNet_infoMsgTxConfSA[0], NULL_PTR);

	CANNet_infoMsgTxConfSA[1].infoMsgIDUI = 0x1F000025UL;
	CANNet_infoMsgTxConfSA[1].infoMsgParU.B.CAN = 1;
	CANNet_infoMsgTxConfSA[1].infoMsgParU.B.MO = 1;
	CANNet_infoMsgTxConfSA[1].infoMsgParU.B.DIR = _MCAN_DIR_TX;
	CANNet_infoMsgTxConfSA[1].infoMsgParU.B.DLC = 8;
	CANNet_infoMsgTxConfSA[1].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
	// MCANDrv_SetMsgObj(CANNet_infoMsgTxConfSA[1], NULL_PTR);

	CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x1F000026UL;
	CANNet_infoMsgTxConfSA[2].infoMsgParU.B.CAN = 2;
	CANNet_infoMsgTxConfSA[2].infoMsgParU.B.MO = 2;
	CANNet_infoMsgTxConfSA[2].infoMsgParU.B.DIR = _MCAN_DIR_TX;
	CANNet_infoMsgTxConfSA[2].infoMsgParU.B.DLC = 8;
	CANNet_infoMsgTxConfSA[2].infoMsgParU.B.IDE = _MCAN_IDE_EXT;
}

// CAN0接收消息处理函数 VCU
void CANNet_RecvMsgCAN0(MCANDrv_infoMsgS_TYPE infoMsgS)
{
	// CAN接收样例
	//	uint8 idxIncUB, idxByteUB;
	//	for (idxIncUB = 0; idxIncUB < 128; idxIncUB++)
	//	{
	//		if (infoMsgS.infoConfS.infoMsgIDUI == CANNet_InfoRxMsgConfSA[idxIncUB].infoMsgIDUI)
	//		{
	//			for (idxByteUB = 0; idxByteUB < 8; idxByteUB++)
	//			{
	//				CANNet_datRxMsgBufM[idxIncUB][idxByteUB] = infoMsgS.datMsgUBA[idxByteUB];
	//			}
	//			CANNet_datRxMsgBufM[idxIncUB][8]++;
	//		}
	//	}
	VcuDecodeFunction(infoMsgS.infoConfS.infoMsgIDUI, infoMsgS.datMsgUBA);
}
// CAN1接收消息处理函数
void CANNet_RecvMsgCAN1(MCANDrv_infoMsgS_TYPE infoMsgS)
{
	// CAN接收样例
	//	uint8 idxIncUB, idxByteUB;
	//	for (idxIncUB = 0; idxIncUB < 128; idxIncUB++)
	//	{
	//		if (infoMsgS.infoConfS.infoMsgIDUI == CANNet_InfoRxMsgConfSA[idxIncUB].infoMsgIDUI)
	//		{
	//			for (idxByteUB = 0; idxByteUB < 8; idxByteUB++)
	//			{
	//				CANNet_datRxMsgBufM[idxIncUB][idxByteUB] = infoMsgS.datMsgUBA[idxByteUB];
	//			}
	//
	//			CANNet_datRxMsgBufM[idxIncUB][8]++;
	//		}
	//	}
	BcuDecodeFunction(infoMsgS.infoConfS.infoMsgIDUI, infoMsgS.datMsgUBA);
}
/*备注，CAN2接收报文时，需要配置接收报文的MO号，如果不配置，将进入不了CAN中断*/
void CANNet_RecvMsgCAN2(MCANDrv_infoMsgS_TYPE infoMsgS)
{
#ifdef _APP_RELEASE_CODE
	Can2ProtocolDecode(infoMsgS.infoConfS.infoMsgIDUI, infoMsgS.datMsgUBA);
#endif
#ifdef _APP_TEST_MOTOR_CODE
	switch (infoMsgS.infoConfS.infoMsgIDUI)
	{
	case 0x0000007E3:
	{
		MotorControlEnable = infoMsgS.datMsgUBA[0] & 0x0F;
		MotorControlCmd = (infoMsgS.datMsgUBA[0] & 0xF0) >> 4;
		MotorDebugCmd = infoMsgS.datMsgUBA[1] & 0xFF;
		MotorLifeTestCmd = infoMsgS.datMsgUBA[2] & 0xFF;
		break;
	}
	case 0x0000007E4:
	{
		LockDelay = MAX((UINT16)((infoMsgS.datMsgUBA[1] << 8) | (infoMsgS.datMsgUBA[0])), 100);
		RunFailedDelay = MAX((UINT16)(((infoMsgS.datMsgUBA[3] << 8) | infoMsgS.datMsgUBA[2])), 1000);
		break;
	}
	default:
		break;
	}
#endif
}
//发送消息管理
void CAN0Net_Manage1ms(void)
{
	static UINT32 Can0TimerCounter = 0;
	static UINT8 VcuIDIdx = 0;
	MCANDrv_infoMsgS_TYPE infoMsgS;
	static BOOL ChkFlg = FALSE;
	if (Can0TimerCounter % 100 == 0)
	{
		if (Can0TimerCounter % (Ebc2VcuOutTable[VcuIDIdx][1]) == 0)
		{
			CANNet_infoMsgTxConfSA[0].infoMsgIDUI = Ebc2VcuOutTable[VcuIDIdx][0];
			infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[0];
			memset(infoMsgS.datMsgUBA, 0x00, 8);
			VcuEncodeFunction(Ebc2VcuOutTable[VcuIDIdx][0], &(infoMsgS.datMsgUBA[0]));
			MCANDrv_SendMsg(infoMsgS);
		}
		VcuIDIdx++;
		ChkFlg = FALSE;
	}
	if (VcuIDIdx >= (sizeof(Ebc2VcuOutTable) / 8))
	{
		VcuIDIdx = 0;
		ChkFlg = TRUE;
	}
	if (ChkFlg)
	{
		Can0TimerCounter = Can0TimerCounter + 1;
	}
}
void CAN1Net_Manage1ms(void)
{
	static UINT32 Can1TimerCounter = 0;
	static UINT8 BcuIDIdx = 0;
	static BOOL BcuChkFlg = FALSE;
	MCANDrv_infoMsgS_TYPE infoMsgS;
	if (Can1TimerCounter % 100 == 0)
	{
		if (Can1TimerCounter % (Ebc2BcuOutTable[BcuIDIdx][1]) == 0)
		{
			CANNet_infoMsgTxConfSA[1].infoMsgIDUI = Ebc2BcuOutTable[BcuIDIdx][0];
			infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[1];
			memset(infoMsgS.datMsgUBA, 0x00, 8);
			BcuEncodeFunction(Ebc2BcuOutTable[BcuIDIdx][0], &(infoMsgS.datMsgUBA[0]));
			MCANDrv_SendMsg(infoMsgS);
		}
		BcuIDIdx++;
		BcuChkFlg = FALSE;
	}
	if (BcuIDIdx >= (sizeof(Ebc2BcuOutTable) / 8))
	{
		BcuIDIdx = 0;
		BcuChkFlg = TRUE;
	}
	if (BcuChkFlg)
	{
		Can1TimerCounter = Can1TimerCounter + 1;
	}
}
void CAN2Net_Manage1ms(void)
{
	MCANDrv_infoMsgS_TYPE infoMsgS;
	static UINT32 Can2TimerCounter = 0;
	Can2TimerCounter = Can2TimerCounter + 1;
#ifdef _APP_RELEASE_CODE
	uint8 cmdIdx = 0x00;
	uint8 DataIdx = 0x00;
	static uint8 AnsNum = 0x00; //应答次数
	static uint8 AnsInterval = 0;
	DataIdx = ebcd_st_toBleAns;
	AnsInterval++;
	if (ebcd_flg_Can2SendEnbale == TRUE && DataIdx > 0 && AnsInterval > 5)
	{
		if (AnsNum < DataPackCounter[DataIdx])
		{
			Can2ProtocolCode(cmdIdx, DataIdx, (DataPackBegin[DataIdx] + AnsNum), &(CANNet_infoMsgTxConfSA[2].infoMsgIDUI), &(infoMsgS.datMsgUBA[0]));
			infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
			MCANDrv_SendMsg(infoMsgS);
			AnsNum++;
			AnsInterval = 0;
		}
		else
		{
			AnsNum = 0;
			ebcd_st_toBleAns = 0;
			ebcd_flg_Can2SendEnbale = FALSE;
		}
	}
	else if (Can2TimerCounter % 100 == 1)
	{
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x1CEE00F0;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = (uint8)(ebcd_st_lockSensor | (ebcd_st_unlockSensor << 4));
		infoMsgS.datMsgUBA[1] = ebcd_st_pedstSensor;
		infoMsgS.datMsgUBA[2] = ebcd_P_airSensor;
		infoMsgS.datMsgUBA[3] = ebcd_flg_ebcEolDone;
		infoMsgS.datMsgUBA[4] = ebcd_T_plugin[0];
		infoMsgS.datMsgUBA[5] = ebcd_T_plugin[1];
		infoMsgS.datMsgUBA[6] = ebcd_T_plugin[2];
		infoMsgS.datMsgUBA[7] = ebcd_T_plugin[3];
		MCANDrv_SendMsg(infoMsgS);
	}
	else if (Can2TimerCounter % 100 == 2)
	{
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x1CEE00F1;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = Control_Times;
		infoMsgS.datMsgUBA[1] = Control_Times>>8;
		infoMsgS.datMsgUBA[2] = Control_Times>>16;
		infoMsgS.datMsgUBA[3] = Control_Times>>24;
		infoMsgS.datMsgUBA[4] = Test_LockPressure;
		infoMsgS.datMsgUBA[5] = Test_LockPressure>>8;
		infoMsgS.datMsgUBA[6] = 0;
		infoMsgS.datMsgUBA[7] = 0;
		MCANDrv_SendMsg(infoMsgS);
	}
#endif
#ifdef _APP_TEST_MOTOR_CODE
	if (Can2TimerCounter % 100 == 1)
	{
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x7F3;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = (uint8)(ebcd_st_lockSensor | (ebcd_st_unlockSensor << 4));
		infoMsgS.datMsgUBA[1] = MotorControlEnable;
		infoMsgS.datMsgUBA[2] = MotorControlCmd;
		infoMsgS.datMsgUBA[3] = MotorDebugCmd;
		infoMsgS.datMsgUBA[4] = MotorControlLockNum;
		infoMsgS.datMsgUBA[5] = MotorControlunLockNum;
		infoMsgS.datMsgUBA[6] = ebcd_P_airSensor;
		infoMsgS.datMsgUBA[7] = ebcd_P_airSensor >> 8;
		MCANDrv_SendMsg(infoMsgS);
	}
	else if (Can2TimerCounter % 100 == 2)
	{
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x7F4;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = LockDelay;
		infoMsgS.datMsgUBA[1] = LockDelay >> 8;
		infoMsgS.datMsgUBA[2] = RunFailedDelay;
		infoMsgS.datMsgUBA[3] = RunFailedDelay >> 8;
		infoMsgS.datMsgUBA[4] = LifeTestCounter;
		infoMsgS.datMsgUBA[5] = LifeTestCounter >> 8;
		infoMsgS.datMsgUBA[6] = 0x00;
		infoMsgS.datMsgUBA[7] = 0x00;
		MCANDrv_SendMsg(infoMsgS);
	}
	Can2TimerCounter = Can2TimerCounter + 1;
#endif
#ifdef _APP_TEST_CODE
	switch (CANNet_idxSchedUB)
	{
	case 0:
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x7F0;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = (uint8)(PressureValue >> 24);
		infoMsgS.datMsgUBA[1] = (uint8)(PressureValue >> 16);
		infoMsgS.datMsgUBA[2] = (uint8)(PressureValue >> 8);
		infoMsgS.datMsgUBA[3] = (uint8)(PressureValue >> 0);
		infoMsgS.datMsgUBA[4] = (uint8)(Distence1 >> 8);
		infoMsgS.datMsgUBA[5] = (uint8)(Distence1);
		infoMsgS.datMsgUBA[6] = (uint8)(Distence2 >> 8);
		infoMsgS.datMsgUBA[7] = (uint8)(Distence2);
		MCANDrv_SendMsg(infoMsgS);
		break;
	case 1:
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x7F1;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = (uint8)(Switch1);
		infoMsgS.datMsgUBA[1] = (uint8)(Switch2);
		infoMsgS.datMsgUBA[2] = 0xFF;
		infoMsgS.datMsgUBA[3] = 0xFF;
		infoMsgS.datMsgUBA[4] = (uint8)(PressureValue >> 24);
		infoMsgS.datMsgUBA[5] = (uint8)(PressureValue >> 16);
		infoMsgS.datMsgUBA[6] = (uint8)(PressureValue >> 8);
		infoMsgS.datMsgUBA[7] = (uint8)(PressureValue >> 0);
		MCANDrv_SendMsg(infoMsgS);
		break;
	case 2:
		CANNet_infoMsgTxConfSA[2].infoMsgIDUI = 0x7F2;
		infoMsgS.infoConfS = CANNet_infoMsgTxConfSA[2];
		infoMsgS.datMsgUBA[0] = ControlState;
		infoMsgS.datMsgUBA[1] = 0xFF;
		infoMsgS.datMsgUBA[2] = 0xFF;
		infoMsgS.datMsgUBA[3] = 0xFF;
		infoMsgS.datMsgUBA[4] = (uint8)(Control_Times >> 24);
		infoMsgS.datMsgUBA[5] = (uint8)(Control_Times >> 16);
		infoMsgS.datMsgUBA[6] = (uint8)(Control_Times >> 8);
		infoMsgS.datMsgUBA[7] = (uint8)(Control_Times);
		MCANDrv_SendMsg(infoMsgS);
		break;
	default:
		break;
	}
	CANNet_idxSchedUB = (uint16)((CANNet_idxSchedUB + 1) % 100); // 100ms一个完整循环
#endif
}
