#ifndef _GW3323_CHARGE_H
#define _GW3323_CHARGE_H

#include "include.h"

// charge mode enum
typedef enum {
    CHARGE_MODE_FULL_KEEP       = 0x00,
    CHARGE_MODE_FULL_DISCONNECT = 0x01,
} CHARGE_MODE_TYPEDEF;

// charge dc in state enum
typedef enum {
    CHARGE_DCIN_RESET_DIS       = 0x00,
    CHARGE_DCIN_RESET_EN        = 0x40,
} CHARGE_DCIN_RESERT_TYPEDEF;

// charge status enum
typedef enum {
    CHARGE_STA_UNINIT           = 0x00,     // uninitialized.
    CHARGE_STA_OFF              = 0x01,     // charge off.
    CHARGE_STA_OFF_VBUS_PATH    = 0x02,     // charge off but still connect to DC5V.
    CHARGE_STA_ON_TRICKLE       = 0x03,     // trickle charging.
    CHARGE_STA_ON_CON_CURR      = 0x04,     // constant current charging.
    CHARGE_STA_ON_CON_VOL       = 0x05,     // constant voltage charging.
} CHARGE_STATUS_TYPEDEF;

// current of constant current charging enum
typedef enum {
    CHARGE_CONS_CUR_10mA        = 0x00,
    CHARGE_CONS_CUR_20mA        = 0x01,
    CHARGE_CONS_CUR_30mA        = 0x02,
    CHARGE_CONS_CUR_40mA        = 0x03,
    CHARGE_CONS_CUR_50mA        = 0x04,
    CHARGE_CONS_CUR_60mA        = 0x05,
    CHARGE_CONS_CUR_70mA        = 0x06,
    CHARGE_CONS_CUR_80mA        = 0x07,
    CHARGE_CONS_CUR_90mA        = 0x08,
    CHARGE_CONS_CUR_100mA       = 0x09,
    CHARGE_CONS_CUR_110mA       = 0x0a,
    CHARGE_CONS_CUR_120mA       = 0x0b,
    CHARGE_CONS_CUR_140mA       = 0x0c,
    CHARGE_CONS_CUR_160mA       = 0x0d,
    CHARGE_CONS_CUR_180mA       = 0x0e,
    CHARGE_CONS_CUR_200mA       = 0x0f,
} CHARGE_CONS_CUR_TYPEDEF;

// current of trickle charging enum
typedef enum {
    CHARGE_TRI_CUR_10mA        = 0x00,
    CHARGE_TRI_CUR_20mA        = 0x01,
    CHARGE_TRI_CUR_30mA        = 0x02,
} CHARGE_TRICKLE_CUR_TYPEDEF;

// current of cutoff charging enum
typedef enum {
    CHARGE_CUTOFF_CUR_2mA5      = 0x0000,
    CHARGE_CUTOFF_CUR_5mA       = 0x0800,
    CHARGE_CUTOFF_CUR_10mA      = 0x1000,
    CHARGE_CUTOFF_CUR_15mA      = 0x1800,
    CHARGE_CUTOFF_CUR_20mA      = 0x2000,
    CHARGE_CUTOFF_CUR_25mA      = 0x2800,
    CHARGE_CUTOFF_CUR_30mA      = 0x3000,
    CHARGE_CUTOFF_CUR_35mA      = 0x3800,
} CHARGE_CUTOFF_CUR_TYPEDEF;

// voltage of cutoff charging enum
typedef enum {
    CHARGE_CUTOFF_VOL_4V2       = 0x0000,
    CHARGE_CUTOFF_VOL_4V3       = 0x0300,
} CHARGE_CUTOFF_VOL_TYPEDEF;

// voltage of trickle charging enum
typedef enum {
    CHARGE_TRI_OFF_VOL_2V9      = 0x00,
    CHARGE_TRI_OFF_VOL_3V       = 0x40,
} CHARGE_TRI_OFF_VOL_TYPEDEF;

// charging initial struct
typedef struct {
    CHARGE_STATUS_TYPEDEF       charge_sta;             // charge status.
    CHARGE_DCIN_RESERT_TYPEDEF  dcin_reset;             // reset if DC insert.
    CHARGE_CONS_CUR_TYPEDEF     const_curr;             // the current of the constant currrent charging.
    CHARGE_TRICKLE_CUR_TYPEDEF  trick_curr;             // the current of the trickle charging.
    CHARGE_CUTOFF_CUR_TYPEDEF   cutoff_curr;            // charging cutoff current.
    CHARGE_CUTOFF_VOL_TYPEDEF   cutoff_volt;            // charging cutoff voltage.
    FUNCTIONAL_STATE            trick_curr_en;          // trickle charging enable.
    CHARGE_TRI_OFF_VOL_TYPEDEF  trick_cutoff_volt;      // trickle charging cutoff voltage.
    CHARGE_MODE_TYPEDEF         mode;                   // disconnect or not when charge full.
} charge_init_typedef;

//--------------- Function used to configure charging ---------------//
void charge_init(charge_init_typedef *charge_init_struct);
void charge_process(void);
FUNCTIONAL_STATE charge_detect_dc(void);
void charge_insert_dc(void);
void charge_status_update(u8 status);
bool charge_detect_off_condition(void);
uint8_t charge_get_status(void);
void charge_change_mode(u8 mode);
uint16_t charge_get_level(u8 mode);
void charge_power_level_config(u8 level);
FLAG_STATE charge_cutoff_cur_condition(void);
FLAG_STATE charge_cutoff_vol_condition(void);
FLAG_STATE charge_trickle_vol_condition(void);

#endif // _GW3323_CHARGE_H
