View Source Soleil.BQ27427 (soleil v0.1.1)

Driver for the Texas Instruments BQ27427 Battery Fuel Gauge.

This module provides a complete interface to the BQ27427 fuel gauge, including:

  • Battery measurements (voltage, current, power)
  • State of charge and capacity readings
  • Temperature measurements
  • Status flags and control operations
  • Configuration mode management

Summary

Types

The battery chemistry type. According to the datasheet, the types correspond to the following battery properties

Functions

Get the chemical ID of the battery profile.

Configures the GPOUT pin as a State of Charge interrupt with the given threshold.

Get control status as a map.

Get the average current in amps. Positive values indicate charging, negative values indicate discharging.

Enter configuration mode. The device must be unsealed before this can be used.

Get all status flags as a map.

Get the full charge capacity in mAh.

Gets the design capacity of the battery in mAh. The device must be unsealed to use this function

Gets the design capacity of the battery in mWh. The device must be unsealed to use this function

Get the average power in watts. Positive values indicate charging, negative values indicate discharging.

Get the remaining capacity in mAh.

Perform a hard reset of the device. This will reset all registers to their default values.

Seal the device to prevent configuration changes.

Sets the bit to configure sign of the CC_Gain parameter. The default firmware for the BQ27427 for some reason shipped with the wrong value, and isn't documented in the datasheet. See the TI support issue for more information

Sets the chemistry ID of the battery profile. The device must be unsealed to use this function

Sets the design capacity of the battery in mAh. The device must be unsealed to use this function

Sets the design energy of the battery in mWh. The device must be unsealed to use this function

Perform a soft reset of the device. This maintains configuration but resets calculations. Used to exit configuration mode (has the side effect of re-sealing the device)

Get the state of charge as a percentage (0-100).

Get the internal chip temperature in degrees Celsius. Future versions of hardware may support reading temperature from the battery internal NTC sensor.

Unseal the device to allow configuration changes.

Gets the battery voltage in volts.

Types

@type chem_id() :: :chemistry_a | :chemistry_b | :chemistry_c

The battery chemistry type. According to the datasheet, the types correspond to the following battery properties:

:chemistry_a: 4.35 V :chemistry_b: 4.2 V :chemistry_c: 4.4 V

Note: :chemistry_b corresponds to LiPo batteries, which is the only battery chemistry supported by the battery charger in the Soleil board. Future versions may allow for use with other battery chemistries

@type i2c_bus() :: Circuits.I2C.bus()

Functions

@spec chemistry_id(i2c_bus()) :: {:ok, chem_id()} | {:error, term()}

Get the chemical ID of the battery profile.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.chemistry_id(ref)
{:ok, :chemistry_b} # using chemistry B for SOC algorithm
Link to this function

configure_soc1_interrupt(i2c_ref, threshold, clear_threshold)

View Source
@spec configure_soc1_interrupt(i2c_bus(), integer(), integer()) ::
  :ok | {:error, term()}

Configures the GPOUT pin as a State of Charge interrupt with the given threshold.

Parameters

  • threshold: Percentage (0-100) at which to trigger the interrupt
  • clear_threshold: Percentage (0-100) at which to clear the interrupt

Example

iex> Soleil.BQ27427.configure_soc1_interrupt(i2c, 20, 25)  # Trigger at 20%, clear at 25%
@spec control_status(i2c_bus()) :: {:ok, map()} | {:error, term()}

Get control status as a map.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.control_status(ref)
{:ok, %{
  sleep: false,
  bca: false,
  calmode: false,
  cca: false,
  chemchange: false,
  initcomp: true,
  ldmd: true,
  qmax_up: false,
  res_up: false,
  rup_dis: true,
  sealed: false,
  shutdown_enabled: false,
  vok: false,
  wdreset: false
}}
@spec current(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Get the average current in amps. Positive values indicate charging, negative values indicate discharging.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.current(ref)
{:ok, -0.250} # Discharging at 250mA
Link to this function

enter_config_mode(i2c_ref)

View Source
@spec enter_config_mode(i2c_bus()) :: :ok | {:error, term()}

Enter configuration mode. The device must be unsealed before this can be used.

@spec flags(i2c_bus()) :: {:ok, map()} | {:error, term()}

Get all status flags as a map.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.flags(ref)
{:ok, %{
  over_temp_flag: false,
  under_temp_flag: false,
  full_charge: false,
  charging: true,
  ocv_taken: true,
  dod_correct: true,
  itpor: false,
  cfgupmode: false,
  bat_det: true,
  soc1: false,
  socf: false,
  discharging: true
}}
Link to this function

full_charge_capacity(i2c_ref)

View Source
@spec full_charge_capacity(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Get the full charge capacity in mAh.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.full_charge_capacity(ref)
{:ok, 1980} # battery has an estimated capacity of 1980mAh at full charge
Link to this function

get_design_capacity(i2c_ref)

View Source
@spec get_design_capacity(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Gets the design capacity of the battery in mAh. The device must be unsealed to use this function

Example

iex> Soleil.BQ27427.get_design_capacity(i2c)
{:ok, 1200} # 1200 mAh battery
Link to this function

get_design_energy(i2c_ref)

View Source
@spec get_design_energy(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Gets the design capacity of the battery in mWh. The device must be unsealed to use this function

Example

iex> Soleil.BQ27427.get_design_energy(i2c)
{:ok, 3000} # 3000 mWh battery
@spec power(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Get the average power in watts. Positive values indicate charging, negative values indicate discharging.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.current(ref)
{:ok, -0.850} # Discharging at 850mW
Link to this function

read_data_memory(i2c_ref, class, block, offset, length)

View Source
@spec read_data_memory(i2c_bus(), integer(), integer(), integer(), integer()) ::
  {:ok, binary()} | {:error, term()}
Link to this function

remaining_capacity(i2c_ref)

View Source
@spec remaining_capacity(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Get the remaining capacity in mAh.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.remaining_capacity(ref)
{:ok, 1128} # battery has an estimated 1128mAh remaining
@spec reset(i2c_bus()) :: :ok | {:error, term()}

Perform a hard reset of the device. This will reset all registers to their default values.

@spec seal(i2c_bus()) :: :ok | {:error, term()}

Seal the device to prevent configuration changes.

Link to this function

set_charge_direction(i2c_ref)

View Source

Sets the bit to configure sign of the CC_Gain parameter. The default firmware for the BQ27427 for some reason shipped with the wrong value, and isn't documented in the datasheet. See the TI support issue for more information:

https://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/1215460/bq27427evm-misbehaving-stateofcharge

Example

iex> Soleil.BQ27427.set_charge_direction(i2c)
:ok
Link to this function

set_chemistry_id(i2c_ref, chem_id)

View Source
@spec set_chemistry_id(i2c_bus(), chem_id()) :: :ok | {:error, term()}

Sets the chemistry ID of the battery profile. The device must be unsealed to use this function

Example

iex> {:ok, i2c} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.set_chemistry_id(i2c, :chemistry_a)
:ok
Link to this function

set_design_capacity(i2c_ref, capacity)

View Source
@spec set_design_capacity(i2c_bus(), integer()) :: :ok | {:error, term()}

Sets the design capacity of the battery in mAh. The device must be unsealed to use this function

Example

iex> Soleil.BQ27427.set_design_capacity(i2c, 1200)  # 1200mAh battery
Link to this function

set_design_energy(i2c_ref, energy)

View Source
@spec set_design_energy(i2c_bus(), integer()) :: :ok | {:error, term()}

Sets the design energy of the battery in mWh. The device must be unsealed to use this function

Example

iex> Soleil.BQ27427.set_design_energy(i2c, 4440)  # 4440mWh battery
@spec soft_reset(i2c_bus()) :: :ok | {:error, term()}

Perform a soft reset of the device. This maintains configuration but resets calculations. Used to exit configuration mode (has the side effect of re-sealing the device)

Link to this function

state_of_charge(i2c_ref)

View Source
@spec state_of_charge(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Get the state of charge as a percentage (0-100).

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.state_of_charge(ref)
{:ok, 37} # battery is at 37%
@spec temperature(i2c_bus()) :: {:ok, float()} | {:error, term()}

Get the internal chip temperature in degrees Celsius. Future versions of hardware may support reading temperature from the battery internal NTC sensor.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.temperature(ref)
{:ok, 25.7} # chip temperature is  25.7C
@spec unseal(i2c_bus()) :: :ok | {:error, term()}

Unseal the device to allow configuration changes.

@spec voltage(i2c_bus()) :: {:ok, integer()} | {:error, term()}

Gets the battery voltage in volts.

Examples

iex> {:ok, ref} = Circuits.I2C.open("i2c-1")
iex> Soleil.BQ27427.voltage(ref)
{:ok, 3.750}