#include "kit_data.h"

#include "bsp_can.h"
#include "bsp_eeprom.h"

#include "bmu_manager.h"
#include "iso_check.h"
#include "version_manager.h"
#include "soc_calculate.h"
#include "sop_calculate.h"
#include "fault_manager.h"
#include "hv_adc_manager.h"
#include "eeprom_manager.h"
#include "dido_manager.h"
#include "statistic_manager.h"
#include "run_status.h"
#include "bmu_manager.h"
#include "signal_manager.h"
#include "protocol_comm.h"

extern uint8_t canID_devNumber;

//BCU广播报文1
//void bcu_send_0x18010000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;
//	uint8_t tmp_8u,status;
//	static uint8_t heart_bit = 0;
//	
//	switch(bms_get_run_status())
//	{
//		case kRunStatus_Init:
//			status = 0;	
//			break;
//		case kRunStatus_Standby:
//			status = 1;
//			break;
//		case kRunStatus_Chg:
//			status = 2;
//			break;
//		case kRunStatus_Dis:
//			status = 3;
//		default:
//			break;
//	}
//	
//	//电池组总电压
//	tmp_16u = bms_get_statistic_data(kStatisticData_TotalVolt);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//电池组充/放电总电流
//	tmp_16u = bms_get_current() + 32000;
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//电池模块SOC
//	tmp_8u = (uint8_t )(bms_get_soc()/100);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//电池模块SOH
//	tmp_8u = (uint8_t)(bms_get_soh()/100);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//BMS工作状态
//	tmp_8u = status;
//	WRITE_LT_INT8U(buf, len, tmp_8u);	
//	//BMS心跳
//	tmp_8u = heart_bit++;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//    bsp_can_ext_data_sync_send(can, 0x18010000, buf, len);
//}

////BCU广播报文2
//void bcu_send_0x18020000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;

//	//电池总容量
//	tmp_16u = get_eeprom_data(kEep_RatedCapacity, kEepromDataType_Full);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//电池剩余容量
//	tmp_16u = (uint32_t)bms_get_soc()*get_eeprom_data(kEep_RatedCapacity, kEepromDataType_Full)/10000;;
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//系统额定电量
//	tmp_16u = get_eeprom_data(kEep_RatedCapacity, kEepromDataType_Full)*get_eeprom_data(kEep_RatedTotalVolt, kEepromDataType_Full)/10000;
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//预留
//	WRITE_LT_INT16U(buf, len, 0);	
//    bsp_can_ext_data_sync_send(can, 0x18020000, buf, len);
//}

////BCU广播报文3
//void bcu_send_0x18030000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;

//	//在线电池总数
//	tmp_16u = bms_get_statistic_data(kStatisticData_OnlineCellNum);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//在线温感总数
//	tmp_16u = get_eeprom_data(kEep_TempNum, kEepromDataType_Full);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//循环次数
//	tmp_16u = bms_get_cycle();
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//预留
//	tmp_16u = 0;
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	
//    bsp_can_ext_data_sync_send(can, 0x18030000, buf, len);
//}

////BCU广播报文4
//void bcu_send_0x18040000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;

//	//最大允许充电电流
//	tmp_16u = bms_get_sop_data(kSopData_ChgCur);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//最大允许放电电流
//	tmp_16u = bms_get_sop_data(kSopData_DisCur);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//最大允许充电功率
//	tmp_16u = bms_get_sop_data(kSopData_ChgCur) * bms_get_statistic_data(kStatisticData_TotalVolt) / 10000;
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//最大允许放电功率
//	tmp_16u = get_eeprom_data(kEep_DisLTVoltLTAlarm1, kEepromDataType_Full);
//	WRITE_LT_INT16U(buf, len, tmp_16u);	
//    bsp_can_ext_data_sync_send(can, 0x18040000, buf, len);
//}

////BCU广播报文5
//void bcu_send_0x18050000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;

//	//最高单体电压
//	tmp_16u = bms_get_statistic_data(kStatisticData_MaxVolt);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//最低单体电压
//	tmp_16u = bms_get_statistic_data(kStatisticData_MinVolt);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//最大压差
//	tmp_16u = bms_get_statistic_data(kStatisticData_VoltDiff);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//平均电压
//	tmp_16u = bms_get_statistic_data(kStatisticData_AvgVolt);
//	WRITE_LT_INT16U(buf, len, tmp_16u);	
//    bsp_can_ext_data_sync_send(can, 0x18050000, buf, len);
//}


////BCU广播报文6
//void bcu_send_0x18060000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;

//	//最高单体温度
//	tmp_16u = bms_get_statistic_data(kStatisticData_MaxTemp);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//最低单体温度
//	tmp_16u = bms_get_statistic_data(kStatisticData_MinTemp);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//单体温差
//	tmp_16u = bms_get_statistic_data(kStatisticData_TempDiff);
//	WRITE_LT_INT16U(buf, len, tmp_16u);
//	//平均温度
//	tmp_16u = bms_get_statistic_data(kStatisticData_AvgTemp);
//	WRITE_LT_INT16U(buf, len, tmp_16u);	
//    bsp_can_ext_data_sync_send(can, 0x1806FF00, buf, len);
//}

////BCU广播报文7
//void bcu_send_0x18070000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint8_t tmp_8u;

//	//最高单体电压从机序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MaxVoltBmuIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最高单体电压电池序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MaxVoltIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最低单体电压从机序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MinVoltBmuIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最低单体电压电池序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MinVoltIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最高单体温度从机序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MaxTempBmuIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最高单体温度温感序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MaxTempIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最低单体温度从机序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MinTempBmuIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//最低单体温度温感序号
//	tmp_8u = bms_get_statistic_data(kStatisticData_MinTempIdx) + 1;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//    bsp_can_ext_data_sync_send(can, 0x18070000, buf, len);
//}
//#include "signal_manager.h"
////BCU广播报文8
//void bcu_send_0x18080000(can_dev_e can)
//{
//    uint8_t len = 0, buf[8];
//	uint8_t tmp_8u;

//	//故障最高等级
//	tmp_8u = bms_get_max_fault_level();
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//断继电器故障
//	tmp_8u = (bms_get_fault_relay_off_bit() != 0);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//禁充标志
//	tmp_8u = (bms_get_signal(kSignalIdx_ForbidChg) == kSignalStatus_High);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	//禁放标志
//	tmp_8u = (bms_get_signal(kSignalIdx_ForbidDis) == kSignalStatus_High);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	
//	WRITE_LT_INT32U(buf, len, 0);

//    bsp_can_ext_data_sync_send(can, 0x18080000, buf, len);
//}

//void bcu_can_receive(CanMsg *msg, can_dev_e can)
//{
//	uint8_t code = 0,value = 0;
//    if(msg->id.value == 0x181CFFF4)
//    {	
//		code = msg->data.byte[0];
//		value = msg->data.byte[1];
//		if(code == 1)
//		{
//			bsp_eeprom_save_data(kEep_ChgDisChangeBasis_RelayOnOffMode, value,kEepromDataType_Low);
//		}
//        if(code == 0 && get_eeprom_data(kEep_ChgDisChangeBasis_RelayOnOffMode, kEepromDataType_Low) == kRunCtrl_Cmd)
//        {
//			bms_set_circuit_cmd_status((CircuitStatus)value);
//        }
//    }
//}

//void bcu_can_send(uint32_t base_time,can_dev_e can)
//{
//	static uint32_t dly = 0;
//	
//    dly += base_time;
//	if((dly % 100) == 0)
//	{
//		dly = 0;
//        bcu_send_0x18010000(can);
//		bcu_send_0x18020000(can);
//		bcu_send_0x18030000(can);
//		bcu_send_0x18040000(can);
//		bcu_send_0x18050000(can);
//		bcu_send_0x18060000(can);
//		bcu_send_0x18070000(can);
//		bcu_send_0x18080000(can);
//	}
//}

//void protocol_can_bcu_init(can_dev_e can)
//{
//    protocol_can_set_call(can, bcu_can_receive, bcu_can_send,NULL);
//}

/*********************pylon can*********************/
/*********************pylon can*********************/
/*********************pylon can*********************/
void bcu_send_0x42100000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;
	uint8_t tmp_8u;
	uint32_t canID_dev;
	canID_dev = 0x00004210 + canID_devNumber -1;
	
	//电池组总电压
	tmp_16u = bms_get_statistic_data(kStatisticData_TotalVolt);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//电池组充/放电总电流
	tmp_16u = (bms_get_current()* 10 + 3000);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//主控温度--我们点表中没有，用单体平均温度替代
	tmp_16u = bms_get_statistic_data(kStatisticData_AvgTemp);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//电池模块SOC
	tmp_8u = (uint8_t )(bms_get_soc()/100);
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//电池模块SOH
	tmp_8u = (uint8_t)(bms_get_soh()/100);
	WRITE_LT_INT8U(buf, len, tmp_8u);
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42200000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;
	
	uint32_t canID_dev;
	canID_dev = 0x00004220 + canID_devNumber -1;

	//充电截止电压
	tmp_16u = get_eeprom_data(kEep_ChgHTVoltHTAlarm1, kEepromDataType_Full);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//放点截止电压
	tmp_16u = get_eeprom_data(kEep_DisLTVoltHTAlarm1, kEepromDataType_Full);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最大充电电流
	tmp_16u = bms_get_sop_data(kSopData_ChgCur);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最大放电电流
	tmp_16u = bms_get_sop_data(kSopData_DisCur);
	WRITE_LT_INT16U(buf, len, tmp_16u);	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42300000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;
	uint32_t canID_dev;
	canID_dev = 0x00004230 + canID_devNumber -1;
	
	//最高单体电池电压 
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxVolt);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低单体电池电压
	tmp_16u = bms_get_statistic_data(kStatisticData_MinVolt);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最高单体电池电压编号 
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxVoltIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低单体电池电压编号 
	tmp_16u = bms_get_statistic_data(kStatisticData_MinVoltIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42400000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;

	uint32_t canID_dev;
	canID_dev = 0x00004240 + canID_devNumber -1;
	
	//最高单体电池温度 
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxTemp);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低单体电池温度 
	tmp_16u = bms_get_statistic_data(kStatisticData_MinTemp);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最高单体电池温度编号
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxTempIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低单体电池温度编号
	tmp_16u = bms_get_statistic_data(kStatisticData_MinTempIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42500000(can_dev_e can)
{
    uint8_t len = 0, buf[8];
		uint16_t tmp_16u;
		uint8_t tmp_8u, status;
		uint16_t temp_second;
		uint16_t temp_first;
	
		uint32_t canID_dev;
		canID_dev = 0x00004250 + canID_devNumber -1;
	
	/*	基本状态
		pylon  bluesun
	0	休眠	初始化
	1	充电	待机
	2	放电	充电
	3	搁置	放电
	*/
	switch(bms_get_run_status())
	{
		case kRunStatus_Standby:
			status = 0;	
			break;
		case kRunStatus_Chg:
			status = 1;
			break;
		case kRunStatus_Dis:
			status = 2;
			break;
		case kRunStatus_PwrOff:
			status = 3;
		default:
			break;
	}

	//基本状态
	tmp_8u = status;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//循环周期 
	tmp_16u = bms_get_cycle();
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//故障--无
	tmp_8u = 0;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//告警--一二级放入告警中
	//每个告警的位的位置需要测一下
	
	temp_second = set_bit_based_on_source_pylon(status, kFaultLevel_Second);
	temp_first = set_bit_based_on_source_pylon(status, kFaultLevel_First);
	
	tmp_16u = temp_second | temp_first;
	WRITE_LT_INT16U(buf, len, tmp_16u);	
	//保护--三级放入保护中
	//每个告警的位的位置需要测一下
	
	tmp_16u = set_bit_based_on_source_pylon(status, kFaultLevel_Third);
	
	WRITE_LT_INT16U(buf, len, tmp_16u);	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42600000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;
	
	uint32_t canID_dev;
	canID_dev = 0x00004260 + canID_devNumber -1;

	//最高电池模块电压--无,用单体平均电压×16代替
	tmp_16u = bms_get_statistic_data(kStatisticData_AvgVolt) * 16;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低电池模块电压--无，用单体平均电压×16代替
	tmp_16u = bms_get_statistic_data(kStatisticData_AvgVolt) * 16;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最高电池模块电压编号--无，用单体从机号代替 
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxVoltBmuIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低电池模块电压编号--无，用单体从机号代替   
	tmp_16u = bms_get_statistic_data(kStatisticData_MinVoltBmuIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42700000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;

	uint32_t canID_dev;
	canID_dev = 0x00004270 + canID_devNumber -1;
	
	//最高电池模块温度--无,用最高单体温度代替
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxTemp);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低电池模块温度--无,用最低单体温度代替 
	tmp_16u = bms_get_statistic_data(kStatisticData_MinTemp);
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最高电池模块温度编号--无，用单体从机号代替 
	tmp_16u = bms_get_statistic_data(kStatisticData_MaxTempBmuIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//最低电池模块温度编号--无，用单体从机号代替 
	tmp_16u = bms_get_statistic_data(kStatisticData_MinTempBmuIdx) + 1;
	WRITE_LT_INT16U(buf, len, tmp_16u);	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x42800000(can_dev_e can)
{
		uint8_t len = 0, buf[8];
		uint16_t tmp_16u;
		uint8_t tmp_8u;
		uint16_t bms_stu = 0;
	
		uint32_t canID_dev;
		canID_dev = 0x00004280 + canID_devNumber -1;

    if(bms_get_run_status() == kRunStatus_Init)
    {
        bms_stu = 0;
    }
    else if(bms_get_run_status() == kRunStatus_Standby)
    {
        bms_stu |= 0x1;
    }
    else if(bms_get_run_status() == kRunStatus_Chg)
    {
        bms_stu |= 0x2;
    }
    else if(bms_get_run_status() == kRunStatus_Dis)
    {
        bms_stu |= 0x3;
    }
    if(bms_get_signal(kSignalIdx_ForbidChg) == kSignalStatus_High)
    {
        bms_stu |= 0x10;
    }
    if(bms_get_signal(kSignalIdx_ForbidDis) == kSignalStatus_High)
    {
        bms_stu |= 0x20;
    }
	
	//禁止充电标志
	
	if ((bms_stu & 0x10) >> 4 == 1)
	{
		tmp_8u = 0xAA;
	}
	else
	{
		tmp_8u = 0;
	}
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//禁止放电标志
	if ((bms_stu & 0x10) >> 5 == 1)
	{
		tmp_8u = 0xAA;
	}
	else
	{
		tmp_8u = 0;
	}
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//预留
	tmp_16u = 0;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//预留
	tmp_16u = 0;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	//预留
	tmp_16u = 0;
	WRITE_LT_INT16U(buf, len, tmp_16u);
	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}

void bcu_send_0x73100000(can_dev_e can)
{
  uint8_t len = 0, buf[8];
	uint16_t tmp_16u;
	uint8_t tmp_8u;
	
	uint32_t canID_dev;
	canID_dev = 0x00007310 + canID_devNumber -1;
	
	//Hardware Version 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//预留
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//硬件版本-V 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//硬件版本-R 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//软件版本-V（主版本Major） 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//软件版本-V（子版本Minor） 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//开发主版本 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	//开发子版本 
	tmp_8u = 1;
	WRITE_LT_INT8U(buf, len, tmp_8u);
	
    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
}


//void bcu_send_0x73200000(can_dev_e can)
//{
//  uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;
//	uint8_t tmp_8u;
//	
//	uint32_t canID_dev;
//	canID_dev = 0x00007320 + canID_devNumber -1;
//	
//	tmp_8u = 0;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	
//    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
//}

//void bcu_send_0x73300000(can_dev_e can)
//{
//  uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;
//	uint8_t tmp_8u;
//	
//	uint32_t canID_dev;
//	canID_dev = 0x00007330 + canID_devNumber -1;

//	tmp_8u = 0;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	
//    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
//}

//void bcu_send_0x42900000(can_dev_e can)
//{
//  uint8_t len = 0, buf[8];
//	uint16_t tmp_16u;
//	uint8_t tmp_8u;
//	
//	uint32_t canID_dev;
//	canID_dev = 0x00004290 + canID_devNumber -1;
//	
//	tmp_8u = 0;
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	WRITE_LT_INT8U(buf, len, tmp_8u);
//	
//    bsp_can_ext_data_sync_send(can, canID_dev, buf, len);
//}

void bcu_can_send_pylon(uint32_t base_time,can_dev_e can)
{
	static uint32_t dly = 0;
	
    dly += base_time;
	if((dly % 100) == 0)
	{
		dly = 0;
    bcu_send_0x42100000(can);
		bcu_send_0x42200000(can);
		bcu_send_0x42300000(can);
		bcu_send_0x42400000(can);
		bcu_send_0x42500000(can);
		bcu_send_0x42600000(can);
		bcu_send_0x42700000(can);
		bcu_send_0x42800000(can);
		bcu_send_0x73100000(can);
//		bcu_send_0x73200000(can);
//		bcu_send_0x42900000(can);
//		bcu_send_0x73300000(can);
	}
}

//void bcu_can_send_pylon(uint32_t base_time, can_dev_e can)
//{
//    static uint32_t dly = 0;
//    static uint8_t msg_index = 0;
//    static uint8_t temp = 0;  // 新增计数器
//    
//    dly += base_time;
//    if ((dly % 100) == 0)
//    {
//        dly = 0;
//        temp++;  
//        
//        if (temp == 4)  
//        {
//            temp = 0;  
//            
//            switch (msg_index)
//            {
//                case 0:  bcu_send_0x42100000(can); break;
//                case 1:  bcu_send_0x42200000(can); break;
//                case 2:  bcu_send_0x42300000(can); break;
//                case 3:  bcu_send_0x42400000(can); break;
//                case 4:  bcu_send_0x42500000(can); break;
//                case 5:  bcu_send_0x42600000(can); break;
//                case 6:  bcu_send_0x42700000(can); break;
//                case 7:  bcu_send_0x42800000(can); break;
//                case 8:  bcu_send_0x73100000(can); break;
//                case 9:  bcu_send_0x73200000(can); break;
//                case 10: bcu_send_0x42900000(can); break;
//                case 11: bcu_send_0x73300000(can); break;
//            }
//            
//            msg_index++;
//            if (msg_index >= 12)  
//            {
//                msg_index = 0;
//            }
//        }
//    }
//}

uint16_t set_bit_based_on_source_pylon(uint8_t status, FaultLevel alarmLevel) 
{
	uint16_t tmp_16u = 0;
  if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 0)) //一级总压过压--电池组充电高压保护
	{
		if (status == 1)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 3);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 0, tmp_16u, 3);
		}
	}
	if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 1)) //一级总压欠压--电池组放电低压保护
	{
		if (status == 2)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 2);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 1, tmp_16u, 2);
		}
	}
	
	if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 2)) //一级单体过压--电池单体高压保护
	{
		KIT_SET_BIT_MASK_32(tmp_16u, 1);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 2, tmp_16u, 1);
	}
	if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 3)) //一级单体欠压--电池单体低压保护
	{
		KIT_SET_BIT_MASK_32(tmp_16u, 0);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 3, tmp_16u, 0);
	}
	
	if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 4)) //一级单体过温--充电高温保护 && 放电高温保护
	{
		if (status == 1)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 5);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 4, tmp_16u, 5);
		}
		else if (status == 2)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 7);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 4, tmp_16u, 7);
		}
	}
	if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 5)) //一级单体低温--充电低温保护 && 放电低温保护
	{
		if (status == 1)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 4);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 5, tmp_16u, 4);
		}
		else if (status == 2)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 6);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 5, tmp_16u, 6);
		}
	}
	
	if (KIT_GET_BIT_32(bms_get_fault_single_bit(0 , alarmLevel), 8)) //一级电流过大--电池组充电过流保护 && 电池组放电过流保护
	{
		if (status == 1)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 8);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 8, tmp_16u, 8);
		}
		else if (status == 2)
		{
			KIT_SET_BIT_MASK_32(tmp_16u, 9);
			//tmp_16u = set_bit_based_on_source_pylon(bms_get_fault_single_bit(0 , kFaultLevel_First), 8, tmp_16u, 9);
		}
	}
	
	return tmp_16u;
}

//pylon接收函数，休眠和唤醒控制，充电放电命令
void bcu_can_receive_pylon(CanMsg *msg, can_dev_e can)
{
		uint8_t code = 0,value = 0, statusChg = 0, statusDischg = 0;
    if(msg->id.value == 0x620)
    {	
			code = msg->data.byte[0];
			if(code == 0x55)
			{
				//控制设备进入休眠状态，断开总正总负继电器
				bms_crtl_do_status(kDoType_Positive, kDoCtrlStatus_Off, 100);
				bms_crtl_do_status(kDoType_Negative, kDoCtrlStatus_Off, 100);
			}
			else if (code == 0xAA)
			{
				//控制设备退出休眠状态，闭合总正总负继电器
				bms_crtl_do_status(kDoType_Positive, kDoCtrlStatus_On, 100);
				bms_crtl_do_status(kDoType_Negative, kDoCtrlStatus_On, 100);
			}
    }
		
		if(msg->id.value == 0x621)
    {	
			statusChg = msg->data.byte[0];
			statusDischg = msg->data.byte[1];
			if(statusChg == 0xAA)
			{
				//充电命令，闭合总正总负继电器
				bms_crtl_do_status(kDoType_Positive, kDoCtrlStatus_On, 100);
				bms_crtl_do_status(kDoType_Negative, kDoCtrlStatus_On, 100);
			}
			else if (statusDischg == 0xAA)
			{
				//放电命令，闭合总正总负继电器
				bms_crtl_do_status(kDoType_Positive, kDoCtrlStatus_On, 100);
				bms_crtl_do_status(kDoType_Negative, kDoCtrlStatus_On, 100);
			}
    }
}

void protocol_pylon_can_init(can_dev_e can)
{
	protocol_can_set_call(can, bcu_can_receive_pylon, bcu_can_send_pylon, NULL);
}


