Skip to main content

Crate embedded_batteries

Crate embedded_batteries 

Source
Expand description

§embedded-batteries

A blocking Hardware Abstraction Layer (HAL) for battery fuel gauges and battery chargers used in embedded systems.

§Intro

This library is a set of traits designed to standardize interactions with battery systems in portable electronics. It follows the Smart Battery System specification to define generic functions that all battery chargers and all smart batteries (AKA fuel gauge, gas gauge) must implement to enforce a standard interface.

Additionally, the ACPI Specification’s Power Source and Power Meter Devices battery control messages are defined as an supplementary superset of the Smart Battery specification, for users building systems compliant with ACPI.

§Design Principles

§Generic Design Diagram

    classDiagram
        Application Level Task --> ChargerDriver: non-std calls (ex. MFG)
        Application Level Task --> SmartBatteryDriver: non-std calls (ex. MFG)
        Application Level Task: -> charging FSMs
        Application Level Task: -> monitoring state of charge
        Application Level Task: -> calls std and non-std fn's
        Application Level Task: +do_startup()
        Application Level Task: +enable_autocharging()
        Application Level Task: +get_cell_voltages()
        Application Level Task: +...()
        Application Level Task --> embedded-batteries-charger: std calls
        Application Level Task --> embedded-batteries-smart-battery: std calls
        embedded-batteries-smart-battery <|-- SmartBatteryDriver: implements
        embedded-batteries-charger <|-- ChargerDriver: implements
        embedded-batteries-charger: -> Platform/HW agnostic charger fn's
        embedded-batteries-charger: -> Conforms to SBS spec
        embedded-batteries-charger: +charging_current()
        embedded-batteries-charger: +charging_voltage()
        embedded-batteries-smart-battery: -> Platform/HW agnostic fuel gauge fn's
        embedded-batteries-smart-battery: -> Conforms to SBS spec
        embedded-batteries-smart-battery: +voltage()
        embedded-batteries-smart-battery: +current()
        embedded-batteries-smart-battery: +battery_status()
        embedded-batteries-smart-battery: +temperature()
        embedded-batteries-smart-battery: +cycle_count()
        embedded-batteries-smart-battery: +...()
        <<interface>> embedded-batteries-charger
        <<interface>> embedded-batteries-smart-battery
        ChargerDriver --> platform-specific-hal
        ChargerDriver: -> Specific to charging chip
        ChargerDriver: -> Conforms to chip datasheet
        ChargerDriver: -> has non-std MFG specific fn's
        ChargerDriver: +[std] charging_current()
        ChargerDriver: +[std] charging_voltage()
        ChargerDriver: +[non-std] auto_charge()
        ChargerDriver: +[non-std] ...()
        SmartBatteryDriver --> platform-specific-hal
        SmartBatteryDriver: -> Specific to charging chip
        SmartBatteryDriver: -> Conforms to chip datasheet
        SmartBatteryDriver: -> has non-std MFG specific fn's
        SmartBatteryDriver: +[std] voltage()
        SmartBatteryDriver: +[std] current()
        SmartBatteryDriver: +[std] battery_status()
        SmartBatteryDriver: +[std] ...()
        SmartBatteryDriver: +[non-std] unseal()
        SmartBatteryDriver: +[non-std] ...()
        class platform-specific-hal["platform-specific-hal comm system"]
        platform-specific-hal: -> communication system agnostic
        platform-specific-hal: -> can be uC, kernel driver, etc.
        platform-specific-hal: +read_word()
        platform-specific-hal: +write_word()
        platform-specific-hal: +...()

§Specific Example Design

    classDiagram
        class battery-service["battery-service"]
        click battery-service href "https://github.com/OpenDevicePartnership/embedded-services"
        battery-service --> BQ25773: non-std calls (ex. MFG)
        battery-service --> BQ40Z50: non-std calls (ex. MFG)
        battery-service: +do_startup()
        battery-service: +enable_autocharging()
        battery-service: +get_cell_voltages()
        battery-service: +...()
        battery-service --> embedded-batteries-charger: std calls
        battery-service --> embedded-batteries-smart-battery: std calls
        embedded-batteries-smart-battery <|-- BQ40Z50: implements
        embedded-batteries-charger <|-- BQ25773: implements
        embedded-batteries-charger: +charging_current()
        embedded-batteries-charger: +charging_voltage()
        embedded-batteries-smart-battery: +voltage()
        embedded-batteries-smart-battery: +current()
        embedded-batteries-smart-battery: +battery_status()
        embedded-batteries-smart-battery: +temperature()
        embedded-batteries-smart-battery: +cycle_count()
        embedded-batteries-smart-battery: +...()
        <<interface>> embedded-batteries-charger
        <<interface>> embedded-batteries-smart-battery
        class BQ25773["BQ25773"]
        click BQ25773 href "https://github.com/OpenDevicePartnership/bq25773"
        BQ25773 --> platform-specific-hal
        BQ25773: +[std] charging_current()
        BQ25773: +[std] charging_voltage()
        BQ25773: +[non-std] auto_charge()
        BQ25773: +[non-std] ...()
        class BQ40Z50["BQ40Z50"]
        click BQ40Z50 href "https://github.com/OpenDevicePartnership/bq40z50"
        BQ40Z50 --> platform-specific-hal
        BQ40Z50: +[std] voltage()
        BQ40Z50: +[std] current()
        BQ40Z50: +[std] battery_status()
        BQ40Z50: +[std] ...()
        BQ40Z50: +[non-std] unseal()
        BQ40Z50: +[non-std] ...()
        class platform-specific-hal["embassy-imxrt I2C driver"]
        click platform-specific-hal href "https://github.com/OpenDevicePartnership/embassy-imxrt"
        platform-specific-hal: +read()
        platform-specific-hal: +write()

§Assumptions + Deviations from spec

As there is such a wide range of battery chargers and smart batteries, as well as different ways to electrically connect your smart battery system, a guiding principle of embedded-batteries is to make as little assumptions as possible. The SBS spec, which is the basis of the traits, is used as a guide rather than a strict constraint. Some common deviations and their solutions are listed below:

§Chargers and Fuel Gauges partially/not implementing the SBS spec

Charger and smart battery driver authors are given the flexibility on how their driver implements each SBS function. For example, if the smart battery does not have thermistors onboard, a Result with an “not implemented” error can be returned. Application level code must be able to handle the error accordingly.

§Smart Battery acting as a master

The SBS spec defines that the smart battery should be able to take control of the communication bus and broadcast alert messages. This functionality requires implementation of bus arbitration and as such, is not planned to be supported.

§Spec Coverage

§Charger

For detailed descriptions on how to use each function, please refer to the Smart Battery System spec.

FunctionsCodeAccessDataImplemented
charging_current0x14wmA (u16)
charging_voltage0x15wmV (u16)

§Fuel Gauge

For detailed descriptions on how to use each function, please refer to the Smart Battery System spec.

FunctionsCodeAccessDataImplemented
manufacturer_access*0x00r/wword-
remaining_capacity_alarm0x01r/wmAh or 10mWh
remaining_time_alarm0x02r/wminutes
battery_mode0x03rbit flags
at_rate0x04r/wmA or 10mW
at_rate_time_to_full0x05rminutes
at_rate_time_to_empty0x06rminutes
at_rate_ok0x07rBoolean
temperature0x08r0.1°K
voltage0x09rmV
current0x0armA
average_current0x0brmA
max_error0x0crpercent
relative_state_of_charge0x0drpercent
absolute_state_of_charge0x0erpercent
remaining_capacity0x0frmAh or 10mWh
full_charge_capacity0x10rmAh or 10mWh
run_time_to_empty0x11rminutes
average_time_to_empty0x12rminutes
average_time_to_full0x13rminutes
charging_current0x14rmA
charging_voltage0x15rmV
battery_status0x16rbit flags
cycle_count0x17rcount
design_capacity0x18rmAh or 10mWh
design_voltage0x19rmV
specification_info0x1arunsigned int
manufacture_date0x1brunsigned int
serial_number0x1crnumber
reserved0x1d - 0x1f-
manufacturer_name0x20rstring
device_name0x21rstring
device_chemistry0x22rstring
manufacturer_data*0x23rdata-
reserved0x25 - 0x2e-
optional_mfg_function_5*0x2fr/wdata-
reserved0x30 - 0x3b-
optional_mfg_function_4*0x3cr/wword-
optional_mfg_function_3_1*0x3d - 0x3fr/wword-

* refers to optional fn’s that will not be implemented

§ACPI Battery Control Methods

For detailed descriptions on how to use each method, please refer to the ACPI specification.

§Table 10.1 Battery Control Methods

ObjectDescriptionImplemented
_BCTReturns battery estimated charging time.
_BIFReturns static information about a battery (in other words, model number, serial number, design voltage, and so on).
_BIXReturns extended static information about a battery (in other words, model number, serial number, design voltage, and so on).
_BMASets the averaging interval of the battery capacity measurement, in milliseconds.
_BMCControl calibration and charging.
_BMDReturns battery information related to battery recalibration and charging control.
_BMSSets the sampling time of the battery capacity measurement, in milliseconds.
_BPCReturns static variables that are associated with system power characteristics on the battery path and power threshold support settings.
_BPSReturns the power delivery capabilities of the battery at the present time.
_BPTControl method to set a Battery Power Threshold.
_BSTReturns the current battery status (in other words, dynamic information about the battery, such as whether the battery is currently charging or discharging, an estimate of the remaining battery capacity, and so on).
_BTHCommunicates battery thermal throttle limit set by battery thermal zone.
_BTMReturns battery estimated runtime at the present average rate of drain, or the runtime at a specified rate.
_BTPSets the Battery Trip point, which generates an SCI when battery capacity reaches the specified point.
_OSCOSPM Capabilities conveyance for batteries.
_PSRReturns whether this power source device is currently online.
_PIFReturns static information about a power source.

Modules§

acpi
Advanced Configuration and Power Interface (ACPI) Power Source and Power Meter Devices module
charger
Blocking Smart Battery Charger module
smart_battery
Blocking Smart Battery module

Macros§

impl_smart_battery_for_wrapper_type
Helper macro to implement SmartBattery and ErrorType for wrapper types that just call an inner type’s SmartBattery methods.

Type Aliases§

MilliAmps
Charging current is measured in milliamps, where 1mA is 1
MilliAmpsSigned
Charging current is measured in milliamps, where 1mA is 1
MilliVolts
Charging voltage is measured in millivolts, where 1mV is 1
MilliVoltsSigned
Charging voltage is measured in millivolts, where 1mV is 1