#ifndef APP_H
#define APP_H

//#define IS_CHARGE_IN()			!gpio_input_bit_get(GPIOC,GPIO_PIN_4)
#define IS_CHARGER_ON() 		gpio_output_bit_get(GPIOB,GPIO_PIN_12)
#define CHARG_PROTECT_OPEN(x)   gpio_bit_write(GPIOB,GPIO_PIN_12,(bit_status)(x))
#define BAT1_IS_OPEN()          !gpio_input_bit_get(GPIOC,GPIO_PIN_2)
#define BAT2_IS_OPEN()          !gpio_input_bit_get(GPIOC,GPIO_PIN_1)

#define S10_ENABLE(x)  //gpio_bit_write()
#define S11_ENABLE(x)  gpio_bit_write(GPIOB,GPIO_PIN_13,(bit_status)(x))
#define S20_ENABLE(x)  //gpio_bit_write()
#define S21_ENABLE(x)  gpio_bit_write(GPIOB,GPIO_PIN_15,(bit_status)(x))
#define SS__ENABLE(x)  gpio_bit_write(GPIOB,GPIO_PIN_14,(bit_status)(x))

#define BATTERY_RESISTANCE_OHM    (0.1F)
#define PARRALLEL_DELTA_VOL    	  (2500)
#define PARRALLEL_BL_DELTA_VOL    (100)

#define CHARGE_DELAY_TIME_OUT_COUNT   (10000)

#define ONE_BATTERY_DELTA_VOL    (500)

enum
{
	PRE_PARRALLEL,
	PRE_SERIES,
	

	PRE_MAX
};

enum
{
	CFG_BAT_NO,
	CFG_BAT1,
	CFG_BAT2,
	CFG_BAT1_BAT2_PARRALLEL,
	CFG_BAT1_BAT2_SERIES,


	
	CFG_MAX
};



enum
{
	CB_BAT_NO,
	CB_BAT1,
	CB_BAT2,
	CB_BAT1_BAT2_PARRALLEL,
	CB_BAT1_BAT2_SERIES,


	
	CB_MAX
};

enum
{
	CB_NO_ERR,
	CB_NO_BAT1,
	CB_NO_BAT2,
	CB_NO_OPERATE,

	CB_MAX_ERR
};

typedef struct
{
	uint8_t s10_bit:1;
	uint8_t s11_bit:1;
	uint8_t s20_bit:1;
	uint8_t s21_bit:1;
	uint8_t ss__bit:1;
}CB_VAL;


extern CB_VAL cb_val_last;
extern uint8_t cb_operate_state;
extern uint8_t Check_CB_oper_sta_start;
extern DELAY_COMMON Check_CB_oper_sta_delay;
extern uint8_t is_intelligent;
extern uint8_t CB_OPERATE_PRECEDENCE_Config;

__inline void Check_CB_Oper_Sta_Delay(void)
{
	if(Check_CB_oper_sta_delay.set)
	{
		if(++Check_CB_oper_sta_delay.count >= 2000)
		{
			Check_CB_oper_sta_delay.count = 0;
			Check_CB_oper_sta_start = 1;
		}
	}
		
}

#define CHECK_SERIES_VOL  (80000)

#ifdef HAN_GUO_VERSION
#define CHECK_SERIES_PROTECT_VOL  (20000)
#else
#define CHECK_SERIES_PROTECT_VOL  (40000)
#endif

#define SERIES_PROTECT_VOL  (CHECK_SERIES_PROTECT_VOL + 3000)
#define SERIES_UNDER_VOL_TIME_OUT  (4000)
#define NEXT_SERIES_TIME_OUT  (5000)
#define SELECT_ONE_BATTERY_VOL  (65000)
#define SERIES_UNDER_XX_PERCENT  (20)

enum
{
	D_BMS_ERROR_NO,
	D_BMS_ERROR_SERISE_CD_OFF,


	D_BMS_ERROR_MAX
};
extern uint8_t define_bms_1_error;
extern uint8_t define_bms_2_error;

enum
{
	OBCS_CHARGER_OUT,
	OBCS_CHARGER_IN,
	OBCS_CHARGER_CHECK_FINISH,


	OBCS_CHARGER_MAX
};
extern uint8_t one_bat_charge_status;
extern uint8_t serise_low_enable;
extern uint8_t serise_low_qd_status;

enum
{
	UPDATE_BAT_NO,
	UPDATE_BAT_1,
	UPDATE_BAT_2,

	UPDATE_BAT_MAX
};
enum
{
	UPDATE_STEP_NO,
	UPDATE_STEP_REQ,
	UPDATE_STEP_DATA,

	UPDATE_STEP_MAX
};

#pragma  pack (push,1)  
#define UB_DATA_MAX   (128)
typedef struct
{
	uint8_t ub_step;
	uint8_t ub_bat;
	uint16_t ub_total;
	uint16_t ub_sq;
	uint16_t ub_len;
	uint8_t ub_data[UB_DATA_MAX];
}UPDATE_BAT;
#pragma pack(pop)
extern UPDATE_BAT update_bat;


#define NENG_HAO_BI_MIN   (300)
#define NENG_HAO_BI_MAX   (700)
#define SY_VALID_FLAG_KEY   (0x0325)
typedef struct
{
	uint16_t sy_valid_flag;
	double neng_hao_bi;
}SYLC_SAVE_PARM;

typedef struct
{
	uint32_t sy_dan_ci_li_cheng_temp;
	uint32_t sy_dan_ci_li_cheng;
	uint8_t sy_percent_1;
	uint8_t sy_percent_2;
	uint8_t sy_percent_dlta;
	uint8_t sy_percent_total;
	uint8_t sy_yu_ji_ke_xing_shi_li_cheng;
	SYLC_SAVE_PARM sy_ss_parm;
}SHENG_YU_LI_CHENG;
extern SHENG_YU_LI_CHENG sheng_yu_li_cheng;
extern uint8_t power_switch_from;
extern uint8_t work_normal;

void App_Initial(void);

void CB_Operate_Initial(void);

void Misc_Initial(void);

void Battery_CB_Operate(CB_VAL *cb_val);

int8_t Battery_CB_Switch(uint8_t cb_operate);

uint8_t Is_BAT1_Lock(void);

uint8_t Is_BAT2_Lock(void);

void Check_Charge_In(void);

void Check_CB_Operate_State(void);

uint8_t Battery_Change_Mode(uint8_t cfg_mode);

void Power_On_Normal(uint8_t enable,uint8_t from);

void Intelligent_Management_Battery(void);

void Check_Battery_Small_Current(void);

uint8_t Is_Soak(void);

void Series_Delay_Timeout(void);

void Cal_Sheng_Yu_Li_Cheng(void);

void Initial_Neng_Hao_Bi(void);

extern void Flash_flag_clear(void);

void Save_Param_Time_Out(void)
;

void Save_Param(void);

#endif

