/*
 * Backup_Battery_Core.h
 *
 *  Created on: 2020?11?19?
 *      Author: zheng_s2
 */

#ifndef MIDDLEWARE_BKBATTERY_BACKUP_BATTERY_CORE_H_
#define MIDDLEWARE_BKBATTERY_BACKUP_BATTERY_CORE_H_

#include "Config/Include/APP_Config.h"
#include "App/Power/Power_Os.h"
#include "Abstraction/Include/OS_Interface.h"
#include "Abstraction/OS/Kernel.h"
#include "Abstraction/Driver/GPIO/Gpio_abs.h"
#include "Driver/GPIO/Gpio_drv.h"
#include "Driver/IIC/LPI2C_I2cDrv.h"

/*Register Map define(1 command/reg byte)*/
#define CHIP_ID 	0x00    /* (R) */
#define CHGR_STATUS 0x01    /* (R) */
#define GEN_STATUS 	0x02    /* (R) */
#define EN_INT 		0x03    /* (R/W) */
#define CONTROL 	0x04    /* (R/W) */
#define CHGR_CC 	0x05    /* (R/W) */
#define CHGR_CV 	0x06    /* (R/W) */
#define SOH 		0x07    /* (R/W) */
#define SW_RST 		0x08    /* (W) */
#define NO_OP 		0xFF    /* (W) */
/*more info see MAX20094 datasheet page 21*/

#define TEMP_RESISTANCE_TBL (166)


/*battery status*/
enum
{
	BATTERY_RESET 	   = 0x00,  	/*0000 0000*/
	BATTERY_STANDBY    = 0x02,		/*0000 0010*/
	BATTERY_KL15_IRQ   = 0x04,		/*0000 0100*/
	BATTERY_BOOST 	   = 0x08,  	/*0000 1000*/
	BATTERY_CONNECT    = 0x10,		/*0001 0000*/
	BATTERY_HEALTH     = 0x20,		/*0010 0000*/
	BATTERY_ACTIVE	   = 0x40,		/*0100 0000*/
};

/*battery task event*/
#define	EVENT_BACKUP_BATT_MSG_STBYOUT  		0x0060
#define EVENT_BACKUP_BATT_MSG_STBYIN  		0x0061
#define EVENT_BACKUP_BATT_KL15ON_IRQ		0x0062
#define EVENT_BACKUP_BATT_KL15OFF_IRQ		0x0063
#define EVENT_BACKUP_BATT_DTC_RESTART		0x0064
#define EVENT_BACKUP_TIMEOUT_CCCHGR			0x0065
#define EVENT_BACKUP_TIMEOUT_TRICKCHGR		0x0066
#define EVENT_BACKUP_TIMEOUT_CHECK			0x0067
#define EVENT_BACKUP_BATTERY_ON				0x0068
#define EVENT_BACKUP_BATTERY_OFF			0x0069
/*battery spi event*/
#define	EVENT_BACKUP_MPU_GET_VOLT  			0x2501
#define	EVENT_BACKUP_MCU_GET_HEALTH  		0x2502
#define	EVENT_BACKUP_MPU_VOLT_SEND  		0xA503
#define	EVENT_BACKUP_MCU_HEALTH_SEND  		0xA504

/*battery connect status*/
#define BATTERY_ENABLE			0x01
#define BATTERY_DISABLE			0x00

/*battery gpio status*/
#define BATTERY_GPIO_HI			0x01
#define BATTERY_GPIO_LO			0x00

/*battery voltage lev*/
#define BATTERY_VOLT_UNCHECK	(5.28)
#define BATTERY_VOLT_LOW		(4.96)/*<100mA,15H;>100mA,8H*/
#define BATTERY_VOLT_MID		(5.28)/*50mA,8H*/

/*battery health check temp lev*/
#define HEALTH_TEMP_MIN		(0)
#define HEALTH_TEMP_MAX		(60)

/*max connect temp*/
#define CONNECT_TEMP_MAX	4010

/*battery charger mode*/
#define CHARGER_STOP			0x00
#define CHARGER_100MA			0x01
#define CHARGER_50MA			0x02
#define CHARGER_DONE			0x40
#define CHARGER_CC				0x60
#define CHARGER_CV				0x70

#define CHARGER_BIT				0x70
#define CONTROL_BIT				0x08
#define CONTROL_EN				0x08

/*battery charger time*/
#define CHARGER_LONGTIME		15
#define CHARGER_MIDTIME			8
#define CHARGER_SHORTTIME		8
#define CHARGER_END				0

#define CHARGER_ONCE			0
#define CHARGER_TWICE			1

/*BackupBattery Config*/
#define BATTERY_TEMP_NORMAL			0x00
#define BATTERY_TEMP_ERROR			0x01
#define BATTERY_TEMP_TO_HIGH		0x02
#define BATTERY_TEMP_HI_REMV		0x03
#define	BATTERY_TEMP_UNREAD			0xFE
#define BATTERY_TEMP_INIT			0xFF
#define BATTERY_TEMP_DI				70
#define BATTERY_TEMP_HI				60
#define BATTERY_TEMP_LO				0

#define BATTERY_VOLT_NORMAL			0x00
#define BATTERY_VOLT_ERROR			0x01
#define BATTERY_VOLT_INIT			0xFF
#define BATTERY_VOLT_HI				6
#define BATTERY_VOLT_LO				3.2

#define BATTERY_DISCONNECT_TIME		3
#define BATTERY_REVERT_DONE			0x01
#define BATTERY_REVERT_NONE			0x00
#define BATTERY_REVERT_INIT			0xFF


void BackupBatteryInit(void);
void BackupBatteryConnectCheck(void);
void BackupBatteryBoostSet(void);
void BackupBatteryBoostDisable(void);
void BackupBatteryHealthCheck(void);
void BackupBatteryChargerCheck(void);
void BackupBatteryChargerStart(void);
void BackupBatteryChargerStop(void);
void BackupBatteryCvSet(void);
void BackupBatteryCc100mASet(void);
void BackupBatteryCc50mASet(void);
void BackupBatteryStatusCheck(void);
void BackupBatteryTempCheck(void);
void BackupBatteryVoltCheck(void);
void BackupBatteryDtcSend(void);
int8_t BackupBatteryTempRead(void);
uint8_t GetBatteryStatus(void);

extern void BackupBatteryCharger(void);
extern void BackupBatteryStatusCheck(void);
extern void BackupBatteryStandbyout(void);
extern void BackupBatteryStandbyin(void);

#endif /* MIDDLEWARE_BKBATTERY_BACKUP_BATTERY_CORE_H_ */
