#ifndef	__AFE_H
#define	__AFE_H

#define AFE_ID              (0x1B<<1)



//Define AFE register addr
#define AFE_FLAG1			0x00
#define AFE_FLAG2	    	0x01
#define AFE_BSTATUS	    	0x02
#define AFE_INT_EN		    0x03
#define AFE_SCONF1 			0x04
#define AFE_SCONF2			0x05
#define AFE_SCONF3    		0x06
#define AFE_SCONF4			0x07
#define AFE_SCONF5			0x08
#define AFE_SCONF6			0x09
#define AFE_SCONF7			0x0A
#define AFE_SCONF8			0x0B
#define AFE_SCONF9			0x0C
#define AFE_SCONF10			0x0D

#define AFE_CELL1H			0x0E
#define AFE_CELL1L			0x0F
#define AFE_CELL2H			0x10
#define AFE_CELL2L			0x11
#define AFE_CELL3H			0x12									    
#define AFE_CELL3L			0x13
#define AFE_CELL4H			0x14
#define AFE_CELL4L			0x15
#define AFE_CELL5H			0x16
#define AFE_CELL5L			0x17
#define AFE_CELL6H			0x18
#define AFE_CELL6L			0x19
#define AFE_CELL7H			0x1A
#define AFE_CELL7L			0x1B
#define AFE_CELL8H			0x1C
#define AFE_CELL8L			0x1D
#define AFE_CELL9H			0x1E
#define AFE_CELL9L			0x1F
#define AFE_CELL10H			0x20
#define AFE_CELL10L			0x21
#define AFE_TS1H			0x22
#define AFE_TS1L			0x23
#define AFE_TS2H			0x24
#define AFE_TS2L			0x25
#define AFE_TEMP1H			0x26
#define AFE_TEMP1L			0x27
#define AFE_TEMP2H			0x28
#define AFE_TEMP2L			0x29
#define AFE_CURH			0x2A
#define AFE_CURL  			0x2B

#define AFE_ALARM_SC		0x40
#define AFE_ALARM_OV		0x20
#define AFE_ALARM_CD		0x10
#define AFE_ALARM_CADC		0x08
#define AFE_ALARM_VADC		0x04
#define AFE_ALARM_WDT		0x02
#define AFE_ALARM_TWI		0x01

typedef struct	_SYSINFOR_                          //data for up-computer monitor	
{
	U16		uiPackStatus;                           //packstatus include 
	U16		uiBatStatus;
	U16		uiVCell[10];
	U16		ulVoltage;
	S32		slCurr;
	U16		uiTS[2];
	U16		uiICTempe[2];
	U32		ulFCC;									
	U32		ulRC;
	U16		uiRSOC;
	U16		uiCycleCount;
	U16		uiPackConfig;
	U16		uiManuCommand;
}SYSINFOR;
extern SYSINFOR	xdata	Info;

typedef struct	_AFEDATA_                           //data  get form AFE 
{
	U16 uiCell[10];
	U16 uiTS[2];                                    //TS1 is external tempe
	U16 uiICTempe[2];                               //DieTempe
	S16	siCurr;
}AFEDATA;
extern AFEDATA	xdata	AFE;                        //AFE寄存器读取的值

typedef struct	_AFEREG_                            //data for AFE set in init
{
	U8  AFEFLAG1;
	U8  AFEFLAG2;
	U8  AFEBSTATUS;
	U8  AFEINTEN;
	U8  AFESCONF1;
	U8  AFESCONF2;
	U8  AFESCONF3;
	U8  AFESCONF4;
	U8  AFESCONF5;
	U8  AFESCONF6;
	U8  AFESCONF7;
	U8  AFESCONF8;
	U8  AFESCONF9;
	U8  AFESCONF10;
}AFEREG;
extern AFEREG	xdata	REG;                        //AFE寄存器预设值


//AFE相关的变量
extern	BOOL	bAfeAlarmFlg;
extern	BOOL	bCADCFlg;
extern	BOOL	bVADCFlg;
extern	BOOL	bAfeI2CRWErrFlg;
extern	BOOL	bAfeErr;
extern	U8	data	ucCellNum;				        //当前硬件电芯串数
extern	U8	data	ucCellNumOffset;		        //当前硬件电芯连接方式对应的偏移，用于程序读取AFE对应寄存器
extern	U16	data	uiCellVmax;				        //电芯电压最大值
extern	U16	data	uiCellVmin;				        //电芯电压最小值
extern	U16	xdata	uiVadcVmax;				        //ADC采集最大值
extern	U16	xdata	uiVadcVmin;				        //ADC采集最小值
extern	U8	xdata 	ucTempeMiddle;
extern	U16 xdata	uiTempeMin;
extern	U16 xdata	uiTempeMax;
extern	S32 xdata   slCadcCurrent;
extern	S32 xdata   slCadcCurAverage;
extern	U8  xdata 	ucCadcCnt;
extern	S32 xdata 	slCadcCurBuf[16];
extern	U8	xdata 	ucAfeI2CErrDelayCnt;



extern BOOL AFEWriteReg(U8 Reg, U8 Length, U8 xdata *WrBuf);
extern BOOL AFEReadReg(U8 Reg, U8 Length, U8 xdata *RdBuf);
extern BOOL AFEInit(void);
extern void AFERdFlg(void);
extern void AFEClrFlg(void);
extern void AFERamCheck(void);
extern void AFEInitReg(void);
extern void AFEMosCtl(U8 MosStatus);
extern void AFEEnterPD(void);
extern void AFERdStatus(void);
extern void AFEChgerCheckEn(void);
extern void AFEChgerCheckDis(void);
extern void AFELoadCheckEn(void);
extern void AFELoadCheckDis(void);
extern void AfeBalCtl(U16 BalChTemp);
extern void AfeWDTEn(void);
extern void AfeWDTDis(void);
extern void AfeTempADCEn(void);
extern void AfeTempADCDis(void);
extern void AFETwiCheck(void);
extern void AfeAlarmEn(U8 AlarmFlg);
extern void AfeAlarmDis(U8 AlarmFlg);

#endif