/********************************************************************************
Copyright (C), Sinowealth Electronic. Ltd.
Author: 	Sino
Version: 	V0.0
Date: 		2020/04/26
History:
	V2.0	2020/04/26		 Preliminary
********************************************************************************/
#include "Main.h"


//*****************************DATA MEMORY START***************************//
U8 idata STACK[0x100-STACK_ADDR]  	_at_	STACK_ADDR;	//堆栈

//*****************************BIT MEMORY START***************************//
U16 bdata uiPackConfig;	
sbit bCellNum1		=	uiPackConfig^8;
sbit bCellNum2		=	uiPackConfig^9;
sbit bCellNum3		=	uiPackConfig^10;	
sbit bLEDNum0		=	uiPackConfig^11;		//预留，程序未使用
sbit bLEDNum1		=	uiPackConfig^12;
sbit bTempNum		=	uiPackConfig^13;
sbit bChgerLock		=	uiPackConfig^14;		//是否支持充电器锁定
sbit bLoadLock		=	uiPackConfig^15;		//是否支持负载锁定

sbit bSC_EN			=	uiPackConfig^0;			//00101011
sbit bOV_EN			=	uiPackConfig^1;			 
sbit bOCRC_EN		=	uiPackConfig^2;			//auto reset or nor when OCC
sbit bBAL_EN		=	uiPackConfig^3;
sbit bPF_EN			=	uiPackConfig^4;			//disable
sbit bCTO_EN		=	uiPackConfig^5;			//cell on
sbit bOCPM			=	uiPackConfig^6;			//disable
sbit bEnEEPRomBK	=	uiPackConfig^7;

U16 bdata uiPackStatus;				
sbit bCHGMOS		=	uiPackStatus^8;			 
sbit bDSGMOS		=	uiPackStatus^9;			
sbit bCHGING		=	uiPackStatus^10;
sbit bDSGING		=	uiPackStatus^11;
sbit bSlowDischarge	=	uiPackStatus^12;
sbit bMidDischarge	=	uiPackStatus^13;
sbit bFastDischarge	= 	uiPackStatus^14;

sbit bFC	        =	uiPackStatus^0;			 
sbit bFD	        =	uiPackStatus^1;
sbit bVDQ        	=	uiPackStatus^2;


U16 bdata uiBatStatus;
sbit bOV			=	uiBatStatus^8;
sbit bUV			=	uiBatStatus^9;
sbit bOCC 			= 	uiBatStatus^10;
sbit bOCD1 			= 	uiBatStatus^11;
sbit bOCD2 			= 	uiBatStatus^12;

sbit bOTC 			=	uiBatStatus^0;
sbit bOTD 			= 	uiBatStatus^1;
sbit bUTC			=	uiBatStatus^2;
sbit bUTD			=	uiBatStatus^3;
sbit bAFE_OV		=	uiBatStatus^4;
sbit bAFE_SC		=	uiBatStatus^5;
sbit bCTO			=	uiBatStatus^6; 

U8 bdata ucAFEStatus;
sbit bAfeChger 		=	ucAFEStatus^0;
sbit bAfeLoad 		= 	ucAFEStatus^1;
sbit bAfeChging		=	ucAFEStatus^2;
sbit bAfeDsging		=	ucAFEStatus^3;
sbit bAfeCHG		=	ucAFEStatus^4;
sbit bAfeDSG		=	ucAFEStatus^5;

/**************************************************************************************/
//DataFlash中区块地址定义
/**************************************************************************************/
//系统信息区开始 SubClassID=0x00		langth=48
U16 xdata 	E2uiPackConfigMap		_at_ 	SYS_PARA_MAP_ADDR; 
U16 xdata	E2uiVOC[10]             _at_ 	SYS_PARA_MAP_ADDR+2;
U32 xdata	E2ulDesignCapacity	    _at_ 	SYS_PARA_MAP_ADDR+22;
U32 xdata	E2ulFCC					_at_ 	SYS_PARA_MAP_ADDR+26;
U32 xdata	E2ulCycleThreshold		_at_ 	SYS_PARA_MAP_ADDR+30;
U16 xdata	E2uiCycleCount			_at_ 	SYS_PARA_MAP_ADDR+34;
U16 xdata	E2uiLearnLowTempe		_at_ 	SYS_PARA_MAP_ADDR+36;
U16 xdata	E2Reserve				_at_ 	SYS_PARA_MAP_ADDR+38;//与上位机参数对应，预留占位
S16 xdata	E2siDfilterCur			_at_ 	SYS_PARA_MAP_ADDR+40;
U8  xdata	E2ucLowPowerDeley       _at_ 	SYS_PARA_MAP_ADDR+42;
U8  xdata	E2ucChgBKDelay			_at_	SYS_PARA_MAP_ADDR+43;
S16 xdata	E2siChgBKCur			_at_	SYS_PARA_MAP_ADDR+44;
U8  xdata	E2ucRTCBKDelay			_at_	SYS_PARA_MAP_ADDR+46;
U8  xdata	E2ucRamCheckFlg0        _at_	SYS_PARA_MAP_ADDR+47;       //ucRamCheckFlg + SubClassID

//用户自定义参数区开始 SubClassID=0x01	langth=50
U16 xdata	E2uiSWVersion       	_at_ 	SYSINFO_MAP_ADDR;			
U16 xdata	E2uiHWVersion       	_at_ 	SYSINFO_MAP_ADDR+2;			
U8  xdata	E2ucID              	_at_ 	SYSINFO_MAP_ADDR+4;				
U8  xdata	E2ucMNFName[12]     	_at_ 	SYSINFO_MAP_ADDR+5;			
U32 xdata	E2ulMNFDate				_at_ 	SYSINFO_MAP_ADDR+17;					
U16 xdata	E2uiSerialNum    		_at_ 	SYSINFO_MAP_ADDR+21;			
U8  xdata	E2ucDeviceName[12]  	_at_ 	SYSINFO_MAP_ADDR+23;	
U8  xdata	E2ucDeviceChem[12]  	_at_ 	SYSINFO_MAP_ADDR+35;		
U16 xdata	E2uiChemID          	_at_ 	SYSINFO_MAP_ADDR+47;
U8  xdata	E2ucRamCheckFlg1        _at_	SYSINFO_MAP_ADDR+49;

//充电参数区开始 SubClassID=0x02		langth=18		
U16 xdata  	E2uiOVvol				_at_ 	CHG_PARA_MAP_ADDR;
U16 xdata  	E2uiOVRvol         		_at_ 	CHG_PARA_MAP_ADDR+2;
U8  xdata  	E2ucOVDelay       		_at_ 	CHG_PARA_MAP_ADDR+4;
U8  xdata  	E2ucOVRDelay    		_at_ 	CHG_PARA_MAP_ADDR+5;
U16 xdata   E2uiChgEndVol     		_at_ 	CHG_PARA_MAP_ADDR+6;
S16 xdata	E2siChgEndCurr			_at_ 	CHG_PARA_MAP_ADDR+8;
U8  xdata	E2ucChgEndDelay			_at_ 	CHG_PARA_MAP_ADDR+10;
S32 xdata	E2slOCCCurrent 			_at_ 	CHG_PARA_MAP_ADDR+11;
U8  xdata   E2ucOCCDelay			_at_ 	CHG_PARA_MAP_ADDR+15;
U8  xdata  	E2ucOCCRDelay			_at_ 	CHG_PARA_MAP_ADDR+16;
U8  xdata	E2ucRamCheckFlg2        _at_	CHG_PARA_MAP_ADDR+17;

//放电参数区开始 SubClassID=0x03		langth=21
U16 xdata  	E2uiUVvol          		_at_ 	DSG_PARA_MAP_ADDR;
U16 xdata  	E2uiUVRvol          	_at_ 	DSG_PARA_MAP_ADDR+2;
U8  xdata  	E2ucUVDelay        		_at_ 	DSG_PARA_MAP_ADDR+4;    //all the delay unit is 500mS
U8  xdata  	E2ucUVRDelay        	_at_ 	DSG_PARA_MAP_ADDR+5;
U16 xdata  	E2uiDsgEndVol			_at_ 	DSG_PARA_MAP_ADDR+6;
U8  xdata  	E2ucDsgEndDelay			_at_ 	DSG_PARA_MAP_ADDR+8;
S32 xdata	E2slOCD1Current		  	_at_ 	DSG_PARA_MAP_ADDR+9;
U8	xdata	E2ucOCD1Delay	 	  	_at_ 	DSG_PARA_MAP_ADDR+13;
S32 xdata	E2slOCD2Current		  	_at_ 	DSG_PARA_MAP_ADDR+14;
U8  xdata	E2ucOCD2Delay	 	  	_at_ 	DSG_PARA_MAP_ADDR+18;
U8  xdata 	E2ucLoadRDelay	 	  	_at_ 	DSG_PARA_MAP_ADDR+19;
U8  xdata	E2ucRamCheckFlg3        _at_	DSG_PARA_MAP_ADDR+20;

//预留参数区开始 SubClassID=0x04
//Reserved

//放电PWM参数区开始 SubClassID=0x05		langth=5
U16 xdata 	E2uiDSG1PWMFreq	   		_at_ 	DSG_PWM_PARA_MAP_ADDR;
U8  xdata  	E2ucDSG1PWMRatioL   	_at_ 	DSG_PWM_PARA_MAP_ADDR+2;
U8  xdata  	E2ucDSG1PWMRatioH   	_at_ 	DSG_PWM_PARA_MAP_ADDR+3;
U8  xdata	E2ucRamCheckFlg5        _at_ 	DSG_PWM_PARA_MAP_ADDR+4;

//充电温度保护参数开始 SubClassID=0x06	langth=11
U16 xdata  	E2uiTempOTC      		_at_ 	CHG_TEMP_PARA_MAP_ADDR;
U16 xdata  	E2uiTempOTCR      		_at_ 	CHG_TEMP_PARA_MAP_ADDR+2;
U16 xdata  	E2uiTempUTC        	    _at_ 	CHG_TEMP_PARA_MAP_ADDR+4;
U16 xdata  	E2uiTempUTCR       	    _at_ 	CHG_TEMP_PARA_MAP_ADDR+6;
U8  xdata  	E2ucTempDelay      		_at_ 	CHG_TEMP_PARA_MAP_ADDR+8;
U8  xdata  	E2ucTempRDelay	      	_at_ 	CHG_TEMP_PARA_MAP_ADDR+9;
U8  xdata	E2ucRamCheckFlg6        _at_ 	CHG_TEMP_PARA_MAP_ADDR+10;

//放电温度保护参数开始 SubClassID=0x07	langth=9
U16 xdata  	E2uiTempOTD        		_at_ 	DSG_TEMP_PARA_MAP_ADDR;
U16 xdata  	E2uiTempOTDR       		_at_ 	DSG_TEMP_PARA_MAP_ADDR+2;
U16 xdata  	E2uiTempUTD        		_at_ 	DSG_TEMP_PARA_MAP_ADDR+4;
U16 xdata  	E2uiTempUTDR       		_at_ 	DSG_TEMP_PARA_MAP_ADDR+6;
U8  xdata	E2ucRamCheckFlg7        _at_ 	DSG_TEMP_PARA_MAP_ADDR+8;

//平衡参数区开始 SubClassID=0x08		langth=8
U16 xdata	E2uiBalanceVol      	_at_	BAL_PARA_MAP_ADDR;
U16 xdata	E2uiBalanceVolDiff  	_at_	BAL_PARA_MAP_ADDR+2;
S16 xdata	E2siBalCurrent			_at_	BAL_PARA_MAP_ADDR+4;
U8	xdata	E2ucBalanceDelay    	_at_	BAL_PARA_MAP_ADDR+6;
U8  xdata	E2ucRamCheckFlg8        _at_ 	BAL_PARA_MAP_ADDR+7;

//容量统计参数开始 SubClassID=0x09				length=17
U8	xdata	E2ucSOC					_at_	SOC_PARA_MAP_ADDR;
U32	xdata	E2ulDfRC				_at_	SOC_PARA_MAP_ADDR+1;
S32 xdata   E2slDsgEndCurr          _at_ 	SOC_PARA_MAP_ADDR+5;        //放电截止电流
U32	xdata	E2ulCycleThresholdCount	_at_	SOC_PARA_MAP_ADDR+9;
U16 xdata	E2uiLastCCount			_at_	SOC_PARA_MAP_ADDR+13;
U8	xdata	E2ucDsgEndFlg			_at_ 	SOC_PARA_MAP_ADDR+15;       //放电截至标志
U8	xdata	E2ucRamCheckFlg9        _at_	SOC_PARA_MAP_ADDR+16;


//AFE参数区开始 SubClassID=0x0A			langth=4
U8	xdata	E2ucAFEProtectConfig	_at_	AFE_PARA_MAP_ADDR;
U16 xdata	E2uiAFEOVvol			_at_	AFE_PARA_MAP_ADDR+1;
U8  xdata	E2ucRamCheckFlgA        _at_ 	AFE_PARA_MAP_ADDR+3;

//校准参数区开始 SubClassID=0x0B		langth=12
U16 xdata 	E2uiVPackGain			_at_	CALI_PARA_MAP_ADDR;
S16 xdata 	E2siCadcGain			_at_ 	CALI_PARA_MAP_ADDR+2;
S16 xdata 	E2siCadcOffset			_at_	CALI_PARA_MAP_ADDR+4;				
S16 xdata 	E2siTS0Offset			_at_	CALI_PARA_MAP_ADDR+6;
S16 xdata 	E2siTS1Offset			_at_	CALI_PARA_MAP_ADDR+8;
U8  xdata	E2ucCalibrated			_at_	CALI_PARA_MAP_ADDR+10;
U8  xdata	E2ucRamCheckFlgB        _at_	CALI_PARA_MAP_ADDR+11;

U8  xdata   Reserved[RESERVED_DATA_LEN]     _at_    RESERVED_DATA_MAP_ADDR;

//DataflashCheck
U16 xdata	E2uiCheckFlag			_at_    XRAM_MAP_ADDR+510;		

