/******************************************************************************
 * @file      bmu_adbms1818.h
 * @brief     LT6820-ADBMS1818菊花链采集
 * @version   V1.0
 * @author    Gary 2024-11-13
 * @copyright    
 ******************************************************************************/
#include "bmu_adbms1818.h"

BmuItem bmu_data;

static uint8_t adapt_falg = 0;
//adapt ic number
void bmu_adapt_ic_num(void)
{
	uint8_t i = 0;
	for(i = 0;i < 10;i++)
	{
		run_command(1,BMU_MAX_NUM*BMU_MAX_IC_NUM);
	}

	for(i = 0;i < BMU_MAX_NUM*BMU_MAX_IC_NUM;i++)
	{
		if(bmu_data.ic[i].config.rx_pec_match == 1)
		{
			bmu_data.total_ic_adapt_num = i + 1;
			break;
		}
	}
	
	bmu_data.total_ic_num = bmu_data.total_ic_adapt_num;
}


//
void bmu_adapt_volt_temp(void)
{
	uint16_t icIdx = 0,_cellIdx = 0,_ntcIdx = 0,i = 0;
	if(bmu_data.cellntc_nomatch == 1 || adapt_falg == 1)
	{
		for(icIdx = 0;icIdx < BMU_MAX_IC_NUM * BMU_MAX_NUM;icIdx++)
		{
			for(i = 0;i < IC_MAX_CELL_NUM;i++)
			{
				if(bmu_data.ic[icIdx].cells.c_codes[i] / 10 < IC_CROSS_CELL_VOLT)
				{
					//This condition indicates a crossover point
				}
				else
				{
					bmu_data.cell_actual_pos[_cellIdx] = i;
					bmu_data.cell_actual_ic[_cellIdx] = icIdx;
					_cellIdx++;
				}
			}
		}

		bmu_data.total_cell_num = _cellIdx;

		//calc temp data
		for(icIdx = 0;icIdx < BMU_MAX_IC_NUM * BMU_MAX_NUM;icIdx++)
		{
			for(i = 0;i < IC_MAX_NTC_NUM;i++)
			{
				if(bmu_data.ic[icIdx].aux.a_codes[i] / 10 > IC_CROSS_TEMP_FLOAT)
				{
					//This condition indicates a crossover point
				}
				else
				{
					bmu_data.ntc_actual_pos[_ntcIdx] = i;
					bmu_data.ntc_actual_ic[_ntcIdx] = icIdx;
					_ntcIdx++;
				}
			}
		}

		bmu_data.total_temp_num = _ntcIdx;
	}
	
	//bmu_ic_num is must be config right
	bmu_data.bmu_ic_num = get_eeprom_data(kEep_AfeNum_CellNum, kEepromDataType_High);
	
	bmu_data.bmu_cell_num = bmu_data.total_cell_num / bmu_data.bmu_ic_num;
	bmu_data.bmu_temp_num = bmu_data.total_temp_num / bmu_data.bmu_ic_num;

}

void bmu_init_isoSpi(void)
{
	ADBMS1818_init(bmu_data.bmu_num * bmu_data.bmu_ic_num, bmu_data.ic);
}



//初始化bmu
void bmu_config_init(void)
{
	uint8_t idx = 0;//,NtcNum = 0;
	uint16_t cell_num =0, temp_num = 0;
	drv_gpio_set_pin_status(kGpioType_SP1_Cs_En1, kGpioStatus_High);
	
	bmu_data.bmu_num = get_eeprom_data(kEep_DevAddr_SlaverNum, kEepromDataType_Low);
	if (bmu_data.bmu_num > BMU_MAX_NUM || bmu_data.bmu_num == 0)
	{
		bmu_data.bmu_num = BMU_MAX_NUM;
	}
	//NtcNum = get_eeprom_data(kEep_reserver_NtcNum, kEepromDataType_Low);
	bmu_data.bmu_ic_num = get_eeprom_data(kEep_AfeNum_CellNum, kEepromDataType_High);
	bmu_data.total_ic_num = bmu_data.bmu_num * bmu_data.bmu_ic_num;
	bmu_data.ic = bsp_adbms1818_global();
	bmu_data.total_cell_num = get_eeprom_data(kEep_CellNum, kEepromDataType_Full);
	bmu_data.total_temp_num = get_eeprom_data(kEep_TempNum, kEepromDataType_Full);
	
	//set num from hmi =>adapt num for furture
	cell_num = bmu_data.total_cell_num / bmu_data.bmu_num;
	temp_num = bmu_data.total_temp_num / bmu_data.bmu_num;

	bmu_data.bmu_cell_num = cell_num;
	bmu_data.bmu_temp_num = temp_num;

	uint8_t i = 0;
	cell_num =0, temp_num = 0;
	
	//统计ic中的电压和ntc对应的真实序号
	uint32_t cell_actual_pos[IC_MAX_CELL_NUM] = {0},ntc_actual_pos[IC_MAX_CELL_NUM] = {0},low_value = 0,high_value = 0,order = 0;
	for(idx = 0;idx < bmu_data.bmu_ic_num;idx++)
	{
		low_value = get_eeprom_data(kEep_FirstIAfe_CellPos + idx * 2, kEepromDataType_Full);
		high_value = get_eeprom_data(kEep_FirstIAfe_CellPos + idx * 2 + 1, kEepromDataType_Full);
		cell_actual_pos[idx] =  low_value + (high_value << 16);
		
		for(i = 0; i < IC_MAX_CELL_NUM;i++)
		{
			if(KIT_GET_BIT_VAL(cell_actual_pos[idx],i) == 1)
			{
				bmu_data.cell_actual_pos[order] = i;
				bmu_data.cell_actual_ic[order] = idx;
				order++;
			}
		}
		//cell num of crossover configuration >= The configured number of cell
			//then Based on the configured number of voltages.
		if(order > bmu_data.bmu_cell_num)
		{
			bmu_data.cellntc_nomatch = 1;
			break;
		}
	}
	
	//统计ic中的电压和ntc对应的真实序号
	for(idx = 0;idx < bmu_data.bmu_ic_num;idx++)
	{
		low_value = get_eeprom_data(kEep_FirstIAfe_NtcPos + idx * 2, kEepromDataType_Full);
		high_value = get_eeprom_data(kEep_FirstIAfe_NtcPos + idx * 2 + 1, kEepromDataType_Full);
		ntc_actual_pos[idx] =  low_value + (high_value << 16);
		
		for(i = 0; i < IC_MAX_NTC_NUM;i++)
		{
			if(KIT_GET_BIT_VAL(ntc_actual_pos[idx],i) == 1)
			{
				bmu_data.ntc_actual_pos[order] = i;
				bmu_data.ntc_actual_ic[order] = i;
				order++;
			}
		}
		//ntc num of crossover configuration >= The configured number of ntc
			//then Based on the configured number of ntc.
		if(order > bmu_data.bmu_temp_num)
		{
			bmu_data.cellntc_nomatch = 1;
			break;
		}
	}
}

void bmu_unit_data_handler()
{
	uint8_t i = 0;
	uint16_t cell_pos = 0, temp_pos = 0;
	for (i = 0; i < bmu_data.bmu_num; i++)
	{
		BmuUnit* pBmu = NULL;
		pBmu = &bmu_data.bmu_unit[i];
		pBmu->ic_num = get_eeprom_data(kEep_AfeNum_CellNum, kEepromDataType_High);
		
		pBmu->data[kBmuData_CellNum] = bmu_data.bmu_cell_num;
		pBmu->data[kBmuData_TempNum] = bmu_data.bmu_temp_num;
			
		pBmu->cell_volt_buf = &bmu_data.cell_volt_total_buf[cell_pos];
		pBmu->cell_temp_buf = &bmu_data.cell_temp_total_buf[temp_pos];

		cell_pos += bmu_data.bmu_cell_num;
		temp_pos += bmu_data.bmu_temp_num;
	}

	bmu_data.pole_temp_buf = &bmu_data.cell_temp_total_buf[temp_pos];
}


void bmu_comm_offline_check()
{
	
}


void bmu_vlot_breakline_check()
{
	
}

void bmu_temp_breakline_check()
{
	
}


//real-time calc bmu data
void bmu_data_handler(void)
{
	uint8_t bmuIdx = 0,cellIdx = 0,_cellIdx = 0,_bmuIdx = 0,_ntcIdx = 0;
	//calc cell volt
	for(bmuIdx = 0;bmuIdx < bmu_data.bmu_num;bmuIdx++)
	{
		for(cellIdx = 0;cellIdx < bmu_data.bmu_cell_num;cellIdx++)
		{
			_cellIdx = bmu_data.cell_actual_pos[cellIdx];
			_bmuIdx  = bmu_data.cell_actual_ic[cellIdx];
			bmu_data.bmu_unit[bmuIdx].cell_volt_buf[cellIdx] = bmu_data.ic[_bmuIdx].cells.c_codes[_cellIdx];
		}
	}
	
	//calc temp data
	for(bmuIdx = 0;bmuIdx < bmu_data.bmu_num;bmuIdx++)
	{
		for(cellIdx = 0;cellIdx < bmu_data.bmu_temp_num;cellIdx++)
		{
			_ntcIdx = bmu_data.ntc_actual_pos[cellIdx];
			_bmuIdx  = bmu_data.ntc_actual_ic[cellIdx];
			bmu_data.bmu_unit[bmuIdx].cell_volt_buf[cellIdx] = bmu_data.ic[_bmuIdx].cells.c_codes[_ntcIdx];
		}
	}
	
	//alarm monitor
	bmu_comm_offline_check();
	bmu_vlot_breakline_check();
	bmu_temp_breakline_check();
	
}

void bms_bmu_init()
{
	 bmu_config_init();
	 bmu_unit_data_handler();
	
	 //Automatic Recognition
	 if(adapt_falg == 1)
	 {
		 bmu_adapt_ic_num();
		 bmu_adapt_volt_temp();
		 bmu_unit_data_handler();
	 }
	 
}

void bmu_task_handler(uint32_t base_time)
{
	//Automatic Recognition
	if(adapt_falg == 1)
	{
		bmu_adapt_ic_num();
		bmu_adapt_volt_temp();
		bmu_unit_data_handler();
	}
	run_command(3,bmu_data.bmu_num*bmu_data.bmu_ic_num);
	run_command(4,bmu_data.bmu_num*bmu_data.bmu_ic_num);
	bmu_data_handler();
	
	run_command(5,bmu_data.bmu_num*bmu_data.bmu_ic_num);
	run_command(6,bmu_data.bmu_num*bmu_data.bmu_ic_num);
	bmu_data_handler();
}



