//2013.10.25
/*******************************************************************************
*   ���W���[������      �FAPP01 - CMD from DMC to DPU (for 'MEA' task)         *
*   �쐬���E�쐬��      �F2011/10/08    Y.K.                                   *
*                                                                              *
*   �Q��                �F  app01_DPU.c ���Q�Ƃ̂���                           *
*   ����				�F  EDIT�����́AMPPE/MEA�g                             *
********************************************************************************/
#include 	"app_common.h"
#include 	"app_MPPE-MEA.h"
#include 	"app_MPPE-MIA.h"

// *** Prototype - MEA1 ***
unsigned char	app01_MEA1            ( unsigned int,  unsigned char * , int);
//
unsigned char	app01_InitVar_MEA1    ( void );
unsigned char	app01_ErrClr_MEA1     ( void );

// *** Prototype - MEA2 ***
unsigned char	app01_MEA2            ( unsigned int,  unsigned char * , int);
//
unsigned char	app01_InitVar_MEA2    ( void );
unsigned char	app01_ErrClr_MEA2     ( void );

unsigned char	app01_cmdexe_MEA(unsigned char, unsigned int,  unsigned char * , int);


// **************************************
// *** MEA1: CMD analyses / execution ***
// **************************************
unsigned char	app01_MEA1  ( unsigned int ui_Cntl, unsigned char uc_Data[], int i_Size )
{
	unsigned char	uc_flag;
	unsigned char	uc_sens;
	uc_flag = 0;
	uc_sens = Gd_N_MEA1; //set CMD to MEA1
	uc_flag = app01_cmdexe_MEA(uc_sens, ui_Cntl, uc_Data, i_Size);

	return( uc_flag );
}
// **********************************************************************************************************



// **************************************
// *** MEA2: CMD analyses / execution ***
// **************************************
unsigned char	app01_MEA2  ( unsigned int ui_Cntl, unsigned char uc_Data[], int i_Size )
{
	unsigned char	uc_flag;
	unsigned char	uc_sens;
	uc_flag = 0;
	uc_sens = Gd_N_MEA2; //set CMD to MEA2
	uc_flag = app01_cmdexe_MEA(uc_sens, ui_Cntl, uc_Data, i_Size);

	return( uc_flag );
}



unsigned char	app01_cmdexe_MEA(unsigned char uc_sens, unsigned int ui_Cntl, unsigned char uc_Data[], int i_Size)
{
	int i;
	unsigned char	uc_flag = 0;
	struct _s_MEA_CtrlParam		*pCtrl;		//����p�̃t���O�y�уp�����[�^
	struct _s_MEA_Command		*pCmd;

	if 		(uc_sens == Gd_N_MEA1) { pCtrl = &Gst_MEA1_ctrl; pCmd = &Gst_MEA1_cmd;}
	else if (uc_sens == Gd_N_MEA2) { pCtrl = &Gst_MEA2_ctrl; pCmd = &Gst_MEA2_cmd;}
	else	return(0xFF);

	pCmd->uc_cnt++;
	pCmd->uc_ans[0] = ui_Cntl >> 8 & 0xff;
	pCmd->uc_ans[1] = ui_Cntl      & 0xff;
	if (i_Size > 0) pCmd->uc_ans[2] = uc_Data[0];
	else			pCmd->uc_ans[2] = 0xee;
	if (i_Size > 1) pCmd->uc_ans[3] = uc_Data[1];
	else			pCmd->uc_ans[3] = 0xec;
	
	//EXE
	//Does not check the size of CMD
	if		(ui_Cntl == 0x0030)		uc_flag = app01_Init_MEA(uc_sens);			//MEA_SWINIT ������
	else if	(ui_Cntl == 0x0031)	{	pCmd->uc_err_cnt	= 0;					//MEA_ERR_CLR
									pCtrl->uc_HK_anmly	= 0;					}
	else if	(ui_Cntl == 0x0032)		pCtrl->uc_readrom	= TRUE;					//MEA_ROM_READ
	else if	(ui_Cntl == 0x0010)		pCtrl->uc_snap_ave	= 1;					//MEA_PROC_SNAPSHOT
	else if	(ui_Cntl == 0x0011)		pCtrl->uc_snap_ave	= 0;					//MEA_PROC_AVERAGE
	else if	(ui_Cntl == 0x0012)	{	pCtrl->uc_eng_sci	= (uc_Data[0]) ? 1 : 0;	//MEA_DATA_MODE
									pCtrl->uc_mode		= (uc_Data[1] & 0x0f);	}
	else if	(ui_Cntl == 0x0013)		pCtrl->uc_pot_mode	= (uc_Data[1]) ? 1 : 0;	//MEA_POT_MODE
	else if	(ui_Cntl == 0x0015)	{	pCtrl->us_pot_A		= uc_Data[0]*0x100		//MEA_POT_PARAM
														+ uc_Data[1];
									pCtrl->us_pot_B		= uc_Data[2]*0x100
														+ uc_Data[3];			}
	else if	(ui_Cntl == 0x0016)	for (i = 0; i < 9; ++i)							//MEA_SET_ETPAP
									pCtrl->uc_ep[i]		= uc_Data[i];
	else if	(ui_Cntl == 0x0018)		pCtrl->ui_eng_vmf	= uc_Data[0]*0x1000000	//MEA_SET_VM_AL
														+ uc_Data[1]*0x10000
														+ uc_Data[2]*0x100
														+ uc_Data[3];
	else if	(ui_Cntl == 0x0019)		pCtrl->uc_en_tbl	=(!uc_Data[1])? 1:uc_Data[1]&0x03;//MEA_SET_ETBL
	else if	(ui_Cntl == 0x0020)		pCtrl->uc_HKmon_ena	= uc_Data[1];			//MEA_SET_HK_MON
	else if	(ui_Cntl == 0x0021)	{	pCtrl->us_TMCP_lmt[0]	= uc_Data[0]*0x100	//MEA_SET_TMCP_LIM
															+ uc_Data[1];
									pCtrl->us_TMCP_lmt[1]	= uc_Data[2]*0x100
															+ uc_Data[3];		}
	else if	(ui_Cntl == 0x0022)	{	pCtrl->us_MCP_lmt[0]	= uc_Data[0]*0x100	//MEA_SET_MCP_LIM
															+ uc_Data[1];
									pCtrl->us_MCP_lmt[1]	= uc_Data[2]*0x100
															+ uc_Data[3];		}
	else if	(ui_Cntl == 0x0023)	{	pCtrl->us_IMCP_lmt[0]	= uc_Data[0]*0x100	//MEA_SET_IMCP_LIM
															+ uc_Data[1];
									pCtrl->us_IMCP_lmt[1]	= uc_Data[2]*0x100
															+ uc_Data[3];		}
	else if	(ui_Cntl == 0x0024)	{	pCtrl->us_TFPG_lmt[0]	= uc_Data[0]*0x100	//MEA_SET_TFPG_LIM
															+ uc_Data[1];
									pCtrl->us_TFPG_lmt[1]	= uc_Data[2]*0x100
															+ uc_Data[3];		}
	else if	(ui_Cntl == 0x0025)	{	pCtrl->us_IP5V_lmt[0]	= uc_Data[0]*0x100	//MEA_SET_IP5V_LIM
															+ uc_Data[1];
									pCtrl->us_IP5V_lmt[1]	= uc_Data[2]*0x100
															+ uc_Data[3];		}
	else if	(ui_Cntl == 0x0026)		pCtrl->uc_stpe_ena		= (uc_Data[1]) ? 1 : 0;	//MEA_SND_STPE_AUT
	else if	(ui_Cntl == 0x0027)	{	pCtrl->uc_gf_spn		= uc_Data[0];			//MEA_SND_GF_AUT
									pCtrl->uc_gf_ena		= (uc_Data[1]) ? 1 : 0;	}
	else if (ui_Cntl == 0x0028)	{	pCtrl->uc_MCP_V[0]		= uc_Data[0];			//MEA_SET_MCPV
									pCtrl->uc_MCP_V[1]		= uc_Data[1]; }
	else if (ui_Cntl == 0x0029)	{	pCtrl->uc_B_mode		= (uc_Data[0]) ? 1 : 0;	//MEA_SET_MCPV
									pCtrl->uc_B_sec			= uc_Data[1] & 0x07; }
	else if	(ui_Cntl == 0x0002)	{												//MEA_CNT1_SET
			if		(uc_Data[1] == 0x01) {	pCtrl->uc_swcal_prm[0] = (uc_Data[2]) ? 1 : 0;
											pCtrl->uc_swcal_prm[1] = uc_Data[3];	//EnStep
											pCtrl->uc_swcal_prm[2] = uc_Data[4];	//CH
											pCtrl->uc_swcal_prm[3] = uc_Data[5];	//SECTOR
											pCtrl->uc_swcal_prm[4] = uc_Data[6]; }	//Count
			else if	(uc_Data[1] == 0x04) {	pCtrl->uc_vet_cmp_ena = (uc_Data[3]) ? 1 : 0;
											Guc_TLMm_cmp[uc_sens] = (uc_Data[3]) ? 0 : 3;
											Guc_TLMh_cmp[uc_sens] = (uc_Data[3]) ? 0 : 3; }
			else if (uc_Data[1] == 0x02)	uc_flag = app_PiCmd(uc_sens, &uc_Data[2], 2);	//SEND MEA CMD via MDP
			else	uc_flag = 0xFF;	// *** Error: CMD not found ***
	}					
	else	uc_flag = 0xFF;	// *** Error: CMD not found ***
	
	if (uc_flag) pCmd->uc_err_cnt++;	//�G���[�J�E���^
	return( uc_flag );
}

unsigned char	app01_Init_MEA(unsigned char uc_sens)
{
	int i;
	struct _s_MEA_CtrlParam		*pCtrl;		//����p�̃t���O�y�уp�����[�^
	struct _s_MEA_Command		*pCmd;
	struct _s_MEA_Product		*pDat;		//�v���_�N�g

	if (uc_sens == Gd_N_MEA1) { pCtrl = &Gst_MEA1_ctrl; pCmd = &Gst_MEA1_cmd;}
	else                      { pCtrl = &Gst_MEA2_ctrl; pCmd = &Gst_MEA2_cmd;}

	pDat =  &Gst_MEA_dat;
	
	//����p�̃t���O�y�уp�����[�^�A�R�}���h�ɂ��ύX������
	pCtrl->uc_snap_ave = 1;	//1:Snapshot, 0:Average
	pCtrl->uc_eng_sci = 0;		//1:Eng, 0:Sci
	pCtrl->uc_mode = 0;			//1:Eng(0-4), 0:Sci(0-8)
	//S/C potential
	pCtrl->uc_pot_mode = 0;		//0: not use; 1:use
	pCtrl->us_pot_A = 0x7ff;	// (A-0x7ff)*pot + (B - 0x7ff)
	pCtrl->us_pot_B = 0x7ff;	//2252(10V);
	//MGF
	pCtrl->uc_B_mode = 0;		//0: not use; 1:use
	pCtrl->uc_B_sec = 0;		//0: not use; 1:use
	
	pCtrl->uc_HKmon_ena = 0x00;		//1:ENA, 0:DIS  (from Higher bit, MHV, SVS, SVG, MCPI, ASCT, IF-T, ASIC_SEL)
	pCtrl->uc_HK_anmly = 0;
	pCtrl->uc_stpe_ena = FALSE;
	pCtrl->uc_stpe_no = 32;			//1-32
	pCtrl->uc_gf_ena = FALSE;
	pCtrl->uc_gf_spn = 10;
	pCtrl->uc_gf_spn_dec = 0;
	pCtrl->uc_gf_spn_inc = 0;
	pCtrl->uc_gf_no = 0;
	pCtrl->uc_readrom = FALSE;		//�������Ŏ����I��E2PROM��ǂ܂Ȃ�
	pCtrl->uc_got_e2p_prm = FALSE;	//E2PROM����ǂނƂ��̃t���O������
	
	pCtrl->uc_ep[0]  = 0;			//EtPAP[4en][16ch] & EtPAPe[8en][16ch]	//
	for (i = 1; i < 9; ++i)			//0:Width, 1-8: Starting step#
		pCtrl->uc_ep[i]  = 3 + 6*i;		

	pCtrl->ui_eng_vmf = 0xffffffff;
	pCtrl->uc_en_tbl = 1;			//1-3: 32step for M2; 0:64step for M4
	
	//pCtrl->uc_pac_add = 0;
	//SCNT1 ����^�p
	//���k
	pCtrl->uc_vet_cmp_ena = FALSE;	//FALSE(���k����)
	Guc_TLMm_cmp[uc_sens] = 3;		//3
	Guc_TLMh_cmp[uc_sens] = 3;		//3
	//SWcal
	pCtrl->uc_swcal_prm[0] = FALSE; //TRUE;	//�{����FALSE(CAL off)
	pCtrl->uc_swcal_prm[1] = 0xff;	//EnStep	0xff = All
	pCtrl->uc_swcal_prm[2] = 0xff; //3;		//CH
	pCtrl->uc_swcal_prm[3] = 0xff; //3;		//Sector
	pCtrl->uc_swcal_prm[4] = 0x1;
	
	//Products
	pCtrl->us_spinNo = 0xffff;	//SpinNo
	pCtrl->us_Llen_ttl = 0;		//Data length
	
	//MCP Vol
	pCtrl->uc_MCP_V[0] = 2;
	pCtrl->uc_MCP_V[1] = 1;

	//�R�}���h�J�E���^
	pCmd->uc_cnt = 0;
	pCmd->uc_err_cnt = 0;
	for (i = 0; i < 4; ++i) pCmd->uc_ans[i] = 0;
	pCmd->uc_mcp_off = 0;
	pCmd->uc_psu_off = 0;
	pCmd->uc_cnt_en = 0;
	
	//E2PROM����̃p�����[�^�������Œl�͓���Ă���
	//���E2PROM�̃f�[�^�ŏ㏑������
	pCtrl->us_TMCP_lmt[0] = 0;	pCtrl->us_TMCP_lmt[1] = 0x0fff;
	pCtrl->us_MCP_lmt[0]  = 0;	pCtrl->us_MCP_lmt[1]  = 0x0fff;
	pCtrl->us_IMCP_lmt[0] = 0;	pCtrl->us_IMCP_lmt[1] = 0x0fff;
	pCtrl->us_TFPG_lmt[0] = 0;	pCtrl->us_TFPG_lmt[1] = 0x0fff;
	pCtrl->us_IP5V_lmt[0] = 0;	pCtrl->us_IP5V_lmt[1] = 0x0fff;	
	
	//�c��̃p�����[�^��������APP02�ɂčs���B
	pCtrl->uc_settbl_n = 1;
	

	return 0;
}

// **********************************************************************************************************