current position:Home>Self cultivation of coder - batterymanager design

Self cultivation of coder - batterymanager design

2022-05-15 07:37:29Night drift ice

For electronic products powered by batteries , You need a battery management module , be called BatteryManager.

take BatteryManager(BM) Designed as a class , Used to manage batteries , Including charging 、LED Show 、 Power management, etc .

The hardware involved here , There are batteries ( Usually rechargeable lithium batteries ), Charge IC(Charge IC), There could be Gauge IC( Electricity meter ), And some related AD The signal .

BM Class dependency

establish BM Class needs to pass in some parameters , Give an example according to the general design , It may vary in specific circumstances .

first , What we need to rely on is Hardware class .

This class is the hardware management class of the current product , It contains various hardware functions and resources , In order to offer BM Use .

the second , Event manager EventManager.

In system operation , Each module needs to send and receive events , By this class .

Third , Some other information , For example, the charging chip uses IIC Port number .( commonly charge IC All are IIC Interface )

The fourth one , Other external functions that need to be used , For example, it may depend on the function of an external module , Carry out some operations according to the requirements .

BM Class

1, One init Initialization function .

2, Threads that can receive and process events .

For example, receive system level events or events of other modules , need BM To deal with .

3, One polling function , To maintain BM The necessary function of .

There is no need to use a thread resource , in the light of BM The situation of , A general periodic function can meet the demand .

this polling The function needs to be placed in the main part of the product loop In circulation .

stay polling Function , As the main processing function of this module , You can use state machines to manage .

4, Functions to obtain various information .

For example, get the battery voltage 、 Battery type 、 Battery status 、 Percentage of electricity 、 Temperature and other information .

5,Charge IC / Gauge IC The operation of .

adopt IIC To operate , Read / Set registers, etc .

6, State machine management .

In the design of state machine , Define the status of battery management , such as , uninitialized 、 In discharge 、 Precharge 、 Fast charging, etc .

Also define the related events to be processed , And the actions to be performed according to the status and events .

These operations can be defined as a series of Action function , Easy to manage .

7, Judge whether the current module meets the sleep conditions .

Before the system goes to sleep , You need to query whether each module meets the sleep conditions .

If you want to reduce the coupling , Is to use event mode , Send an event asking if you can sleep , Wait for each module to respond .

Or the conditions for judging dormancy are limited , Just a few modules , Just call the relevant module interface directly .

Each module that needs to process sleep judgment conditions can also be used , Register them in the system status management module .

Function definition

1,BMPolling

Periodic polling function .

2,BatteryManagerEvent

Function of event management thread .

Battery status definition

About the state of the battery , No battery detected 、 Normal discharge 、 There are three main states of charging .

The charging state is divided into precharge 、 Quick charge 、 Fully charged 、 The charging function is prohibited 、 Charging protection 、 Sub states such as charging error .

Here we use only one level of state machine , So put all the charged electronic states and undetected batteries 、 Normal discharge put together .

These states are mutually exclusive , Together, it forms the whole life cycle management of the battery .

typedef enum {

/* Assign the values to make this status value can be shared with external device, 

    or as a external info to be used maybe by user.

    So please don't change the entry, just delete or add new ones.

 */

    BATTERY_STATUS_NOT_DETECTED = 0, /* battery not detected */

    BATTERY_STATUS_DISCHARGE = 1,  /* Battery is detected, and no power supply for charging. */

    /* forbidden due to authentication failed, maybe the battery info from gauge or EEPROM is wrong */

    BATTERY_STATUS_AUTHENTICATION_FAILED = 2,

    /* following the status are for charging. */

    BATTERY_STATUS_CHARGE_UPDATING = 3, /* charge status is in updating when start charge or change charge settings. */

    BATTERY_STATUS_CHARGE_PREPARE = 4,  /* charging when battery voltage is too low, so with very little charging current. */

    BATTERY_STATUS_CHARGE_FAST = 5,  /* Constant current charge -> Constant voltage charge. */

    /* Change IC indicate that the power supply for charge is shutdown, no charge current. */

    BATTERY_STATUS_CHARGE_DONE = 6,  

    BATTERY_STATUS_CHARGE_DISABLED = 7,  /* charge function disabled due to control settings. */

    BATTERY_STATUS_CHARGE_NOT_ALLOWED = 8, /* forbidden by low power supply, e.g 500mA USB cable, but charging current capacity is set 1.5A. */

    BATTERY_STATUS_CHARGE_PROTECTED = 9,  /* forbidden due to temperature out of range (cold or hot) */

} BatteryStatus_t;

copyright notice
author[Night drift ice],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102134355303.html

Random recommended