Driver for the Linear Tech LTC4150 Coulomb Counter (a.k.a. battery gauge sensor or power consumption sensor)
More...
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.
|
int | ltc4150_init (ltc4150_dev_t *dev, const ltc4150_params_t *params) |
| Initialize the LTC4150 driver. More...
|
|
int | ltc4150_reset_counters (ltc4150_dev_t *dev) |
| Clear current counters of the given LTC4150 device. More...
|
|
int | ltc4150_shutdown (ltc4150_dev_t *dev) |
| Disable the interrupt handler and turn the chip off. More...
|
|
int | ltc4150_charge (ltc4150_dev_t *dev, uint32_t *charged, uint32_t *discharged) |
| Get the measured charge since boot or last reset in millicoulomb. More...
|
|
int | ltc4150_avg_current (ltc4150_dev_t *dev, int16_t *dest) |
| Get the average current drawn in E-01 milliampere. More...
|
|
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. More...
|
|
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. More...
|
|
◆ 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 78 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 96 of file ltc4150.h.
◆ ltc4150_avg_current()
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
-
| dev | The LTC4150 device to read data from |
[out] | dest | Store the average current drawn in E-01 milliampere here |
- Return values
-
0 | Success |
-EINVAL | Called with an invalid argument |
-EAGAIN | Called 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
-
| dev | The LTC4150 device to read data from |
[out] | charged | The charge transferred in charging direction |
[out] | discharged | The charge transferred in discharging direction |
- Return values
-
0 | Success |
-EINVAL | Called with an invalid argument |
Passing NULL
for charged
or discharged
is allowed, if only one information is of interest.
◆ ltc4150_init()
Initialize the LTC4150 driver.
- Parameters
-
dev | Device to initialize |
params | Information on how the LTC4150 is conntected |
- Return values
-
◆ ltc4150_last_minute_charge()
Get the measured charge in the last minute.
- Parameters
-
| dev | The LTC4150 device to read data from |
| data | The data recorded by ltc4150_last_minute |
[out] | charged | The charge transferred in charging direction |
[out] | discharged | The charge transferred in discharging direction |
- Return values
-
0 | Success |
-EINVAL | Called 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
-
| dev | LTC4150 device the data was received from |
[out] | charged | Charge in charging direction is stored here |
[out] | discharged | Charge in discharging direction is stored here |
[in] | raw_charged | Number of pulses in charging direction |
[in] | raw_discharged | Number of pulses in discharging direction |
◆ ltc4150_reset_counters()
Clear current counters of the given LTC4150 device.
- Parameters
-
dev | The LTC4150 device to clear current counters from |
- Return values
-
0 | Success |
-EINVAL | Called with an invalid argument |
◆ ltc4150_shutdown()
Disable the interrupt handler and turn the chip off.
- Parameters
-
dev | Previously initialized device to power off |
- Return values
-
0 | Success |
-EINVAL | Called with invalid argument(s) |
The driver can be reinitialized to power on the LTC4150 chip again