battery: support additional properties

Support additional properties from the linux `battery.yaml`.

The `ocv-capacity-table-0` definition differs from the Linux version as
we do not have a good way to generate variable length arrays in our
config structs, or a good way to separate out the percentage vs voltage
in the mapping.

We reduce the flexibility by enforcing a step size of 10%, which removes
the need for the percentage to be in the array and solves the variable
length problem.

To simplify the effort of defining these voltage curves, default curves
for a variety of chemistries have been added, extracted from datasheet
graphs of discharge curves from reputable manufacturers.

Signed-off-by: Jordan Yates <jordan@embeint.com>
This commit is contained in:
Jordan Yates 2024-05-17 22:02:51 +10:00 committed by Anas Nashif
parent 291fc517e3
commit 597ed491c7
3 changed files with 152 additions and 0 deletions

View file

@ -8,6 +8,34 @@ description: |
linux/Documentation/devicetree/bindings/power/supply/battery.yaml
properties:
device-chemistry:
type: string
description: This describes the chemical technology of the battery.
The "lithium-ion" value is a blanket type for all lithium-ion batteries.
If the specific chemistry is unknown, this value can be used instead of
the precise "lithium-ion-X" options.
enum:
- "nickel-cadmium"
- "nickel-metal-hydride"
- "lithium-ion"
- "lithium-ion-polymer"
- "lithium-ion-iron-phosphate"
- "lithium-ion-manganese-oxide"
ocv-capacity-table-0:
type: array
description: |
An array providing the open circuit voltage (OCV) , which is used to look
up battery capacity according to current OCV value. The OCV unit is
microvolts.
Unlike the linux equivalent this array is required to be 11 elements
long, representing the voltages for 0-100% charge in 10% steps.
charge-full-design-microamp-hours:
type: int
description: battery design capacity
re-charge-voltage-microvolt:
type: int
description: limit to automatically start charging again

View file

@ -0,0 +1,87 @@
/*
* Copyright 2024 Embeint Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_H_
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_H_
#include <stdint.h>
#include <errno.h>
#include <zephyr/devicetree.h>
#include <zephyr/math/interpolation.h>
#include <zephyr/sys/util_macro.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief battery API
* @defgroup battery_apis battery APIs
* @{
*/
/* Battery chemistry enumeration.
* Value names must match those from dts/bindings/battery.yaml
*/
enum battery_chemistry {
BATTERY_CHEMISTRY_UNKNOWN = 0,
BATTERY_CHEMISTRY_NICKEL_CADMIUM,
BATTERY_CHEMISTRY_NICKEL_METAL_HYDRIDE,
BATTERY_CHEMISTRY_LITHIUM_ION,
BATTERY_CHEMISTRY_LITHIUM_ION_POLYMER,
BATTERY_CHEMISTRY_LITHIUM_ION_IRON_PHOSPHATE,
BATTERY_CHEMISTRY_LITHIUM_ION_MANGANESE_OXIDE,
};
/* Length of open circuit voltage table */
#define BATTERY_OCV_TABLE_LEN 11
/**
* @brief Get the battery chemistry enum value
*
* @param node_id node identifier
*/
#define BATTERY_CHEMISTRY_DT_GET(node_id) \
UTIL_CAT(BATTERY_CHEMISTRY_, DT_STRING_UPPER_TOKEN_OR(node_id, device_chemistry, UNKNOWN))
/**
* @brief Get the OCV curve for a given table
*
* @param node_id node identifier
* @param table table to retrieve
*/
#define BATTERY_OCV_TABLE_DT_GET(node_id, table) \
COND_CODE_1(DT_NODE_HAS_PROP(node_id, table), \
({DT_FOREACH_PROP_ELEM_SEP(node_id, table, DT_PROP_BY_IDX, (,))}), ({-1}))
/**
* @brief Convert an OCV table and battery voltage to a charge percentage
*
* @param ocv_table Open circuit voltage curve
* @param voltage_uv Battery voltage in microVolts
*
* @returns Battery state of charge in milliPercent
*/
static inline int32_t battery_soc_lookup(const int32_t ocv_table[BATTERY_OCV_TABLE_LEN],
uint32_t voltage_uv)
{
static const int32_t soc_axis[BATTERY_OCV_TABLE_LEN] = {
0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000};
/* Convert voltage to SoC */
return linear_interpolate(ocv_table, soc_axis, BATTERY_OCV_TABLE_LEN, voltage_uv);
}
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_H_ */

View file

@ -0,0 +1,37 @@
/*
* Copyright 2024 Embeint Inc
*
* SPDX-License-Identifier: Apache-2.0
*
* The following open-circuit voltage curves have been extracted from the datasheets of the
* listed battery parts. They will not be 100% correct for all batteries of the chemistry,
* but should provide a good baseline. These curves will also be affected by ambient temperature
* and discharge current.
*
* Each curve is 11 elements representing the OCV voltage in microvolts for each charge percentage
* from 0% to 100% inclusive in 10% increments.
*/
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_BATTERY_BATTERY_H_
#define ZEPHYR_INCLUDE_DT_BINDINGS_BATTERY_BATTERY_H_
/* Panasonic KR-1800SCE */
#define BATTERY_OCV_CURVE_NICKEL_CADMIUM_DEFAULT \
800000 1175000 1207000 1217000 1221000 1226000 1233000 1245000 1266000 1299000 1366000
/* Panasonic BK-1100FHU */
#define BATTERY_OCV_CURVE_NICKEL_METAL_HYDRIDE_DEFAULT \
1004000 1194000 1231000 1244000 1254000 1257000 1263000 1266000 1274000 1315000 1420000
/* Panasonic NCR18650BF */
#define BATTERY_OCV_CURVE_LITHIUM_ION_POLYMER_DEFAULT \
2502000 3146000 3372000 3449000 3532000 3602000 3680000 3764000 3842000 3936000 4032000
/* Drypower IFR18650 E1600 */
#define BATTERY_OCV_CURVE_LITHIUM_IRON_PHOSPHATE_DEFAULT \
2013000 3068000 3159000 3194000 3210000 3221000 3229000 3246000 3256000 3262000 3348000
/* FDK CR14250SE */
#define BATTERY_OCV_CURVE_LITHIUM_MANGANESE_DEFAULT \
1906000 2444000 2689000 2812000 2882000 2927000 2949000 2955000 2962000 2960000 2985000
#endif /* ZEPHYR_INCLUDE_DT_BINDINGS_BATTERY_BATTERY_H_ */