Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

[CCGx Power SDK] Default Battery Management state machine in CYPD3171-24LQXQ_pb_add_pb

[CCGx Power SDK] Default Battery Management state machine in CYPD3171-24LQXQ_pb_add_pb

10 likes given 250 sign-ins 1000 replies posted


If the CCG3PA CYPD3171 design is DRP and with battery supply, the CYPD3171-24LQXQ_pb_add_pb is correct example code beginning to customize firmware.  There is an default battery state machine example in file power_bank.c. 

The APIs in the file power_bank.c is below.

(1) void pb_typec_set_current (uint16_t cur_10mA)

>>This function is for setting current limit in sink mode using PWM by default. The PWM is based on CY4532 hardware design. Based on hardware design, it is possible to set as GPIO drive, I2C transition, .... 

The parameter  uint16_t cur_10mA comes current of PD negotation. 


(2) static void pb_typec_configure_sink(uint8_t port)

>>This function is for configuring Type-C power role from Source to Sink by force. 


(3)void pb_event_handler(uint8_t port, uint32_t evt)

>> This function handles events from dpm. There are two cases handling:

See if we are waiting for disconnect as source when battery is weak. If yes, configure TYPE-C port as sink.

If CCG3PA locked as as sink on TYPE-C port then initiate a PR SWAP if battery is  in normal mode and port partner is DRP and not externally powered.


(4)static void pb_vbus_dis_cb (uint8_t port)

>> This function is callback function of power bank VBUS disable process, but the function is NULL by default.  This function could add some operations after VBUS discharge or adding ADC here for VBUS Safe voltage monitoring. 


(5) void pb_task_init(void)

>> This function is PowerBank task initialization routine. At initialization, Battery can be in following states:

1) Battery is normal and powering the system.

2) Battery is dead and TYPE-C VBUS is powering the system.


(6)static void pb_pr_swap_cb(uint8_t port, resp_status_t resp, const pd_packet_t* pkt_ptr)

>> This function is call back for PR_SWAP. If PR SWAP successfully transmitted, clear the flag.


(7) static void pb_debounce_cb(uint8_t port, timer_id_t id)

>> This function is adding additional debounce time  if the battery state need be changed by hit the threshold of each battery states.


(8) void pb_bat_monitor(void)

>> This function is handle power bank state based on battery status, the battery status was defined by measured battery voltage and currently battery states. 


The Battery state machine running in pb_bat_monitor(), below parameters need be input. 

1) Battery real voltage.  Relates API: static uint16_t pb_get_battery_voltage(void)

2) Battery charging parameters. Relates structure define:

typedef struct
uint8_t table_len; /**< Battery charger parameter table length in bytes*/
uint8_t reserved_0; /**< Reserved for future use*/
uint16_t vbatt_max_volt; /**< Maximum battery voltage in mV. */
uint16_t vbatt_cutoff_volt; /**< Minimum battery voltage below which device should stop discharging. */
uint16_t vbatt_dischg_en_volt; /**< Battery voltage at which discharge can be re-enabled. */
uint16_t vbatt_max_cur; /**< Maximum charging current allowed for the battery in mA. */
uint16_t reserved_1; /**< Reserved for future use*/
} bat_chg_params_t;


There are four possible battery states as below.

typedef enum
PB_BATTERY_STATE_NORMAL = 0, /* Battery in normal/charged state. */
PB_BATTERY_STATE_DB_TYPEC_CHARGING = 1, /* Battery is weak and getting charged over TYPE-C. */
PB_BATTERY_STATE_DB_NOT_CHARGING = 2, /* Battery is weak and nothing connected. */
PB_BATTERY_STATE_DB_TYPEC_WAIT_DC = 3, /* Battery is weak, TYPE-C port is source and waiting for disconnect. */
} pb_battery_state_t;


The Simple State machine flow chat.