#include <string.h>
#include "delay.h"
#include "DebugDataDefine.h"
#include "CommandsDefine.h"
#include "AnalogChannelCtrl_Dictionary.h"
#include "ProjectConfig.h"
#include "Probe_MSO7000X_2d5g.h"
#include "AnalogChannelCtrl_8g.h"
#include "Helper.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////
// 			2024/7/X GHz_wcj MSO8000HD 8G模拟通道控制
//////////////////////////////////////////////////////////////////////////////

u8 Get4094CtrlWord(struct Paras595 params, u32* ctrlWord)
{
	u32 _ctrlWord = 0;
	
	//衰减
	if(params.att == 0) //直通
	{
		_ctrlWord|= Bit_595_42542_V1;
	}
	else if(params.att == 1) //放大
	{
		_ctrlWord|= (Bit_595_42542_V1 | Bit_595_42542_V2 | Bit_595_1144_IN1);
	}
	else if(params.att==2)	//衰减
	{
		_ctrlWord|=(Bit_595_42542_V2 | Bit_595_1144_IN2);
	}
	else if(params.att==3)	//参考信号
	{
		_ctrlWord|=(Bit_595_1144_IN1 | Bit_595_1144_IN2);
	}
	else
	{
		return 0; //error
	}
	
	ctrlWord[0]=_ctrlWord;
	return 1;
}

u8 AnalogChannelSet_Check_Len(u16 dataLen)
{
	if(dataLen<PACKAGE_CHANNELSET_INDEX_BYTES+PACKAGE_CHANNELSET_DATA_BYTES+PACKAGE_CHANNELSET_CHECK_AREA_BYTES+PACKAGE_CHANNELSET_FRAMEID_BYTES)
	{
		return 0;
	}
	if((dataLen-PACKAGE_CHANNELSET_INDEX_BYTES-PACKAGE_CHANNELSET_CHECK_AREA_BYTES-PACKAGE_CHANNELSET_FRAMEID_BYTES)%PACKAGE_CHANNELSET_DATA_BYTES!=0)
	{
		return 0;
	}
	return 1;
}

void AnalogChannel_GetAll4094Word(u8 *pData,u8 channelId){
	//控制HC595/CD4094
	struct Paras595 params595;
	params595.att =  pData[OFFSET_IN_DATA_ATT_START] ;		//衰减

	u32 ctrlWord595 = 0;
	u8 result = Get4094CtrlWord(params595, &ctrlWord595);
	if(result==0){ //参数错误
		return;
	}

	Low32HC595 |= (ctrlWord595 << 8*(channelId-1));
}

void AnalogChannel_SetAll4094(u8 *pData){
	u8 channelId = pData[0];
	Low32HC595 = Low32HC595 & ((u32)1<<31);

	u8 gotChData = 0;
	for(int i=0; i<MAX_CHANNEL_NUM; i++)
	{
		if(((channelId>>i) & 0x01) == 1){
			AnalogChannel_GetAll4094Word(pData + PACKAGE_CHANNELSET_INDEX_BYTES + gotChData * PACKAGE_CHANNELSET_DATA_BYTES, i+1);
			gotChData++;
		}
	}		
	CD_4094(Low32HC595, 4);
	delay_ms(2);
}

void AnalogChannelSet(u8 *pData,u8 channelId)
{
	u8 gain1 = pData[OFFSET_IN_DATA_GAIN_START] ; 
	u8 gain2 = pData[OFFSET_IN_DATA_GAIN_START + 1] ; 
	u16 fgn = pData[OFFSET_IN_DATA_FGN_START] + (pData[OFFSET_IN_DATA_FGN_START + 1]<<8); 	
	u16 fgp = pData[OFFSET_IN_DATA_FGP_START] + (pData[OFFSET_IN_DATA_FGP_START + 1]<<8); 	
	u16 fdn = pData[OFFSET_IN_DATA_FDN_START] + (pData[OFFSET_IN_DATA_FDN_START + 1]<<8); 	
	u16 fdp = pData[OFFSET_IN_DATA_FDP_START] + (pData[OFFSET_IN_DATA_FDP_START + 1]<<8); 	

	u32 ConfigWord;
	u8 dac_index;

	SpiLines spiLines; //SPI总线定义
	GPIOPin csLine; 
	GPIOPin dataLine; 
	GPIOPin clkLine; 
	spiLines.csLine = &csLine;
	spiLines.dataLine = &dataLine;
	spiLines.clkLine = &clkLine;

	//控制字
	ParamsDAC_8G paramsDAC;
	paramsDAC.chnlId = channelId;

	//fgn
	paramsDAC.type = FGN;
	paramsDAC.data = fgn; //bias
	ConfigWord = GetDACConfigWord_8G(paramsDAC,&dac_index);
	DAC_AD5675(ConfigWord,dac_index);

	//fgp
	paramsDAC.type = FGP;
	paramsDAC.data = fgp; //bias
	ConfigWord = GetDACConfigWord_8G(paramsDAC,&dac_index);
	DAC_AD5675(ConfigWord,dac_index);

	//fdn
	paramsDAC.type = FDN;
	paramsDAC.data = fdn; //bias
	ConfigWord = GetDACConfigWord_8G(paramsDAC,&dac_index);
	DAC_AD5675(ConfigWord,dac_index);

	//fdp
	paramsDAC.type = FDP;
	paramsDAC.data = fdp; //bias
	ConfigWord = GetDACConfigWord_8G(paramsDAC,&dac_index);
	DAC_AD5675(ConfigWord,dac_index);

	//dsa1 -> gain1 
	GetDsaSpiLine(channelId, DSA0_8G, &spiLines);
	DSA_PE43711_8G(LMH43711_ADDR_GAIN, gain1, &spiLines); //LMH6401_ADDR_GAIN

	//dsa2 -> gain2
	GetDsaSpiLine(channelId, DSA1_8G, &spiLines);
	DSA_PE43711_8G(LMH43711_ADDR_GAIN, gain2, &spiLines); 
}

void CMD0x22_Request_AnalogChannelSet_process(u8 *pData,u16 dataLen,u8 bIsContentBitInverted_Recv,u8 bIsContentBitInverted_SendBack)
{
	if(IsChannelSetting!=0)
	{
		return;
	}
	IsChannelSetting = 1;
#ifdef RUNNING_WHERE_AT_BOOT	
	Command_SendbackErrorMessage(Error0x02_CannotRunAtBoot,1);
#else
	memcpy(G_DebugData_AnalogChannel_LastRecvData,pData,dataLen);
	if(AnalogChannelSet_Check_Len(dataLen)==0)
	{ 
		IsChannelSetting=0;
		Command_SendbackErrorMessage(Error0x05_CMD_ParaLentghError,1);
		return;
	}
	//pData[0]，bit0-bit3:表示通道读热码；
	u8 channelIds = pData[0] & 0x0f;

	AnalogChannel_SetAll4094(pData);
	u8 gotChData = 0;
	for(int i=0; i<MAX_CHANNEL_NUM; i++)
	{
		u8 openFlag = (channelIds>>i) & 0x01;
		if(openFlag == 1)
		{
			AnalogChannelSet(pData + PACKAGE_CHANNELSET_INDEX_BYTES + gotChData * PACKAGE_CHANNELSET_DATA_BYTES,i+1);
			gotChData++;
		}
	}	
	
	//Call back
	u8 dataBytes=PACKAGE_CHANNELSET_FRAMEID_BYTES;

	for(u8 i=0;i<PACKAGE_CHANNELSET_FRAMEID_BYTES;i++)
	{
		USART_WR_BUF[i] =  pData[dataLen-PACKAGE_CHANNELSET_FRAMEID_BYTES+i];
	}
	 
	USART6_Send(CMD0x22_Request_AnalogChannelSet,dataBytes,bIsContentBitInverted_SendBack);	
	//end
	IsChannelSetting = 0;
#endif	
}

