Loading...
Searching...
No Matches
LTC4150 coulomb counter

Driver for the Linear Tech LTC4150 Coulomb Counter (a.k.a. More...

Detailed Description

Driver for the Linear Tech LTC4150 Coulomb Counter (a.k.a.

battery gauge sensor or power consumption sensor)

Wiring the LTC4150

Hint: M Grusin thankfully created an open hardware breakout board. As a result, virtually all LTC4150 breakout boards are using this schematic. Whenever this documentation refers to a breakout board, this open hardware board is meant. Of course, this driver works with the "bare" LTC4150 as well.

Please note that this driver works interrupt driven and does not clear the signal. Thus, the /CLR and /INT pins on the LTC4150 need to be connected (in case of the breakout board: close solder jumper SJ1), so that the signal is automatically cleared.

Hint: The breakout board uses external pull up resistors on /INT, POL and /SHDN. Therefore /SHDN can be left unconnected and no internal pull ups are required for /INT and POL. In case your board uses 3.3V logic the solder jumpers SJ2 and SJ3 have to be closed, in case of 5V they have to remain open. Connect the VIO pin to the logic level, GND to ground, IN+ and IN- to the power supply and use OUT+ and OUT- to power your board.

In the easiest case only the /INT pin needs to be connected to a GPIO, and (in case of external pull ups) /SHDN and POL can be left unconnected. The GPIO /INT is connected to support for interrupts, /SHDN and POL (if connected) do not require interrupt support.

In case a battery is used the POL pin connected to another GPIO. This allows to distinguish between charge drawn from the battery and charge transferred into the battery (used to load it).

In case support to power off the LTC4150 is desired, the /SHDN pin needs to be connected to a third GPIO.

Things to keep in mind

The LTC4150 creates pulses with a frequency depending on the current drawn. Thus, more interrupts need to be handled when more current is drawn, which in turn increases system load (and power consumption). The interrupt service routing is quite short and even when used outside of specification less than 20 ticks per second will occur. Hence, this effect should hopefully be negligible.

Files

file  ltc4150.h
 LTC4150 coulomb counter.
 
file  ltc4150_params.h
 Default configuration for LTC4150 coulomb counters.
 

Data Structures

struct  ltc4150_recorder_t
 Interface to allow recording of the drawn current in a user defined resolution. More...
 
struct  ltc4150_params_t
 Parameters required to set up the LTC4150 coulomb counter. More...
 
struct  ltc4150_dev
 LTC4150 coulomb counter. More...
 
struct  ltc4150_last_minute_data_t
 Data structure used by ltc4150_last_minute. More...
 

Typedefs

typedef struct ltc4150_dev ltc4150_dev_t
 LTC4150 coulomb counter.
 

Enumerations

enum  { LTC4150_INT_EXT_PULL_UP = 0x01 , LTC4150_POL_EXT_PULL_UP = 0x02 , LTC4150_EXT_PULL_UP = LTC4150_INT_EXT_PULL_UP | LTC4150_POL_EXT_PULL_UP }
 Configuration flags of the LTC4150 coulomb counter. More...
 
enum  ltc4150_dir_t { LTC4150_CHARGE , LTC4150_DISCHARGE }
 Enumeration of directions in which the charge can be transferred. More...
 

Functions

int ltc4150_init (ltc4150_dev_t *dev, const ltc4150_params_t *params)
 Initialize the LTC4150 driver.
 
int ltc4150_reset_counters (ltc4150_dev_t *dev)
 Clear current counters of the given LTC4150 device.
 
int ltc4150_shutdown (ltc4150_dev_t *dev)
 Disable the interrupt handler and turn the chip off.
 
int ltc4150_charge (ltc4150_dev_t *dev, uint32_t *charged, uint32_t *discharged)
 Get the measured charge since boot or last reset in millicoulomb.
 
int ltc4150_avg_current (ltc4150_dev_t *dev, int16_t *dest)
 Get the average current drawn in E-01 milliampere.
 
int ltc4150_last_minute_charge (ltc4150_dev_t *dev, ltc4150_last_minute_data_t *data, uint32_t *charged, uint32_t *discharged)
 Get the measured charge in the last minute.
 
void ltc4150_pulses2c (const ltc4150_dev_t *dev, uint32_t *charged, uint32_t *discharged, uint32_t raw_charged, uint32_t raw_discharged)
 Convert the raw data (# pulses) acquired by the LTC4150 device to charge information in millicoulomb.
 

Variables

const ltc4150_recorder_t ltc4150_last_minute
 Records the charge transferred within the last minute using.
 

Typedef Documentation

◆ ltc4150_dev_t

typedef struct ltc4150_dev ltc4150_dev_t

LTC4150 coulomb counter.

Definition at line 103 of file ltc4150.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Configuration flags of the LTC4150 coulomb counter.

Enumerator
LTC4150_INT_EXT_PULL_UP 

External pull on the /INT pin is present.

LTC4150_POL_EXT_PULL_UP 

External pull on the /POL pin is present.

LTC4150_EXT_PULL_UP 

External pull on the /INT and the /POL pin is present.

Definition at line 77 of file ltc4150.h.

◆ ltc4150_dir_t

Enumeration of directions in which the charge can be transferred.

Enumerator
LTC4150_CHARGE 

The battery is charged.

LTC4150_DISCHARGE 

Charge is drawn from the battery.

Definition at line 95 of file ltc4150.h.

Function Documentation

◆ ltc4150_avg_current()

int ltc4150_avg_current ( ltc4150_dev_t dev,
int16_t *  dest 
)

Get the average current drawn in E-01 milliampere.

This will return the average current drawn since boot or last reset until the last pulse from the LTC4150 was received. The value might thus be a bit outdated (0.8 seconds for the breakout board and a current of 100mA, 79 seconds for a current of 1mA).

Parameters
devThe LTC4150 device to read data from
[out]destStore the average current drawn in E-01 milliampere here
Return values
0Success
-EINVALCalled with an invalid argument
-EAGAINCalled before enough data samples have been acquired. (Wait for at least one second or one pulse from the LTC4150, whichever takes longer.)

◆ ltc4150_charge()

int ltc4150_charge ( ltc4150_dev_t dev,
uint32_t *  charged,
uint32_t *  discharged 
)

Get the measured charge since boot or last reset in millicoulomb.

Parameters
devThe LTC4150 device to read data from
[out]chargedThe charge transferred in charging direction
[out]dischargedThe charge transferred in discharging direction
Return values
0Success
-EINVALCalled with an invalid argument

Passing NULL for charged or discharged is allowed, if only one information is of interest.

◆ ltc4150_init()

int ltc4150_init ( ltc4150_dev_t dev,
const ltc4150_params_t params 
)

Initialize the LTC4150 driver.

Parameters
devDevice to initialize
paramsInformation on how the LTC4150 is conntected
Return values
0Success
-EINVALCalled with invalid argument(s)
-EIOIO failure (gpio_init()/gpio_init_int() failed)

◆ ltc4150_last_minute_charge()

int ltc4150_last_minute_charge ( ltc4150_dev_t dev,
ltc4150_last_minute_data_t data,
uint32_t *  charged,
uint32_t *  discharged 
)

Get the measured charge in the last minute.

Parameters
devThe LTC4150 device to read data from
dataThe data recorded by ltc4150_last_minute
[out]chargedThe charge transferred in charging direction
[out]dischargedThe charge transferred in discharging direction
Return values
0Success
-EINVALCalled with an invalid argument
Warning
The returned data may be outdated up to ten seconds

Passing NULL for charged or discharged is allowed, if only one information is of interest.

◆ ltc4150_pulses2c()

void ltc4150_pulses2c ( const ltc4150_dev_t dev,
uint32_t *  charged,
uint32_t *  discharged,
uint32_t  raw_charged,
uint32_t  raw_discharged 
)

Convert the raw data (# pulses) acquired by the LTC4150 device to charge information in millicoulomb.

Note
This function will make writing data recorders (see ltc4150_recorder_t) easier, but is not intended for end users
Parameters
devLTC4150 device the data was received from
[out]chargedCharge in charging direction is stored here
[out]dischargedCharge in discharging direction is stored here
[in]raw_chargedNumber of pulses in charging direction
[in]raw_dischargedNumber of pulses in discharging direction

◆ ltc4150_reset_counters()

int ltc4150_reset_counters ( ltc4150_dev_t dev)

Clear current counters of the given LTC4150 device.

Parameters
devThe LTC4150 device to clear current counters from
Return values
0Success
-EINVALCalled with an invalid argument

◆ ltc4150_shutdown()

int ltc4150_shutdown ( ltc4150_dev_t dev)

Disable the interrupt handler and turn the chip off.

Parameters
devPreviously initialized device to power off
Return values
0Success
-EINVALCalled with invalid argument(s)

The driver can be reinitialized to power on the LTC4150 chip again