Zephyr API Documentation 4.2.0-rc1
A Scalable Open Source RTOS
 4.2.0-rc1
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
fuel_gauge.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
3 * Copyright 2023 Microsoft Corporation
4 * Copyright (c) 2025 Philipp Steiner <philipp.steiner1987@gmail.com>
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef ZEPHYR_INCLUDE_DRIVERS_BATTERY_H_
10#define ZEPHYR_INCLUDE_DRIVERS_BATTERY_H_
11
21#ifdef __cplusplus
22extern "C" {
23#endif /* __cplusplus */
24
25#include <errno.h>
26#include <stdbool.h>
27#include <stddef.h>
28#include <stdint.h>
29
30#include <zephyr/device.h>
31
122
124
195
199#define SBS_GAUGE_MANUFACTURER_NAME_MAX_SIZE 20
200#define SBS_GAUGE_DEVICE_NAME_MAX_SIZE 20
201#define SBS_GAUGE_DEVICE_CHEMISTRY_MAX_SIZE 4
202
207
212
217
224typedef int (*fuel_gauge_get_property_t)(const struct device *dev, fuel_gauge_prop_t prop,
225 union fuel_gauge_prop_val *val);
226
233typedef int (*fuel_gauge_set_property_t)(const struct device *dev, fuel_gauge_prop_t prop,
234 union fuel_gauge_prop_val val);
235
242typedef int (*fuel_gauge_get_buffer_property_t)(const struct device *dev,
243 fuel_gauge_prop_t prop_type, void *dst,
244 size_t dst_len);
245
252typedef int (*fuel_gauge_battery_cutoff_t)(const struct device *dev);
253
254/* Caching is entirely on the onus of the client */
255
268
278__syscall int fuel_gauge_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
279 union fuel_gauge_prop_val *val);
280
281static inline int z_impl_fuel_gauge_get_prop(const struct device *dev, fuel_gauge_prop_t prop,
282 union fuel_gauge_prop_val *val)
283{
284 const struct fuel_gauge_driver_api *api = (const struct fuel_gauge_driver_api *)dev->api;
285
286 if (api->get_property == NULL) {
287 return -ENOSYS;
288 }
289
290 return api->get_property(dev, prop, val);
291}
292
308__syscall int fuel_gauge_get_props(const struct device *dev, const fuel_gauge_prop_t *props,
309 union fuel_gauge_prop_val *vals, size_t len);
310static inline int z_impl_fuel_gauge_get_props(const struct device *dev,
311 const fuel_gauge_prop_t *props,
312 union fuel_gauge_prop_val *vals, size_t len)
313{
314 const struct fuel_gauge_driver_api *api = (const struct fuel_gauge_driver_api *)dev->api;
315
316 for (size_t i = 0; i < len; i++) {
317 int ret = api->get_property(dev, props[i], vals + i);
318
319 if (ret) {
320 return ret;
321 }
322 }
323
324 return 0;
325}
326
336__syscall int fuel_gauge_set_prop(const struct device *dev, fuel_gauge_prop_t prop,
337 union fuel_gauge_prop_val val);
338
339static inline int z_impl_fuel_gauge_set_prop(const struct device *dev, fuel_gauge_prop_t prop,
340 union fuel_gauge_prop_val val)
341{
342 const struct fuel_gauge_driver_api *api = (const struct fuel_gauge_driver_api *)dev->api;
343
344 if (api->set_property == NULL) {
345 return -ENOSYS;
346 }
347
348 return api->set_property(dev, prop, val);
349}
362__syscall int fuel_gauge_set_props(const struct device *dev, const fuel_gauge_prop_t *props,
363 const union fuel_gauge_prop_val *vals, size_t len);
364
365static inline int z_impl_fuel_gauge_set_props(const struct device *dev,
366 const fuel_gauge_prop_t *props,
367 const union fuel_gauge_prop_val *vals, size_t len)
368{
369 for (size_t i = 0; i < len; i++) {
370 int ret = fuel_gauge_set_prop(dev, props[i], vals[i]);
371
372 if (ret) {
373 return ret;
374 }
375 }
376
377 return 0;
378}
379
391__syscall int fuel_gauge_get_buffer_prop(const struct device *dev, fuel_gauge_prop_t prop_type,
392 void *dst, size_t dst_len);
393
394static inline int z_impl_fuel_gauge_get_buffer_prop(const struct device *dev,
395 fuel_gauge_prop_t prop_type, void *dst,
396 size_t dst_len)
397{
398 const struct fuel_gauge_driver_api *api = (const struct fuel_gauge_driver_api *)dev->api;
399
400 if (api->get_buffer_property == NULL) {
401 return -ENOSYS;
402 }
403
404 return api->get_buffer_property(dev, prop_type, dst, dst_len);
405}
406
415__syscall int fuel_gauge_battery_cutoff(const struct device *dev);
416
417static inline int z_impl_fuel_gauge_battery_cutoff(const struct device *dev)
418{
419 const struct fuel_gauge_driver_api *api = (const struct fuel_gauge_driver_api *)dev->api;
420
421 if (api->battery_cutoff == NULL) {
422 return -ENOSYS;
423 }
424
425 return api->battery_cutoff(dev);
426}
427
432#ifdef __cplusplus
433}
434#endif /* __cplusplus */
435
436#include <zephyr/syscalls/fuel_gauge.h>
437
438#endif /* ZEPHYR_INCLUDE_DRIVERS_BATTERY_H_ */
System error numbers.
#define SBS_GAUGE_DEVICE_NAME_MAX_SIZE
Definition fuel_gauge.h:200
uint16_t fuel_gauge_prop_t
Definition fuel_gauge.h:123
int(* fuel_gauge_battery_cutoff_t)(const struct device *dev)
Callback API for doing a battery cutoff.
Definition fuel_gauge.h:252
int fuel_gauge_battery_cutoff(const struct device *dev)
Have fuel gauge cutoff its associated battery.
int fuel_gauge_get_buffer_prop(const struct device *dev, fuel_gauge_prop_t prop_type, void *dst, size_t dst_len)
Fetch a battery fuel-gauge buffer property.
#define SBS_GAUGE_MANUFACTURER_NAME_MAX_SIZE
Data structures for reading SBS buffer properties.
Definition fuel_gauge.h:199
int fuel_gauge_set_prop(const struct device *dev, fuel_gauge_prop_t prop, union fuel_gauge_prop_val val)
Set a battery fuel-gauge property.
int fuel_gauge_get_prop(const struct device *dev, fuel_gauge_prop_t prop, union fuel_gauge_prop_val *val)
Fetch a battery fuel-gauge property.
int fuel_gauge_set_props(const struct device *dev, const fuel_gauge_prop_t *props, const union fuel_gauge_prop_val *vals, size_t len)
Set a battery fuel-gauge property.
fuel_gauge_prop_type
Definition fuel_gauge.h:32
int(* fuel_gauge_set_property_t)(const struct device *dev, fuel_gauge_prop_t prop, union fuel_gauge_prop_val val)
Callback API for setting a fuel_gauge property.
Definition fuel_gauge.h:233
int(* fuel_gauge_get_property_t)(const struct device *dev, fuel_gauge_prop_t prop, union fuel_gauge_prop_val *val)
Callback API for getting a fuel_gauge property.
Definition fuel_gauge.h:224
int(* fuel_gauge_get_buffer_property_t)(const struct device *dev, fuel_gauge_prop_t prop_type, void *dst, size_t dst_len)
Callback API for getting a fuel_gauge buffer property.
Definition fuel_gauge.h:242
int fuel_gauge_get_props(const struct device *dev, const fuel_gauge_prop_t *props, union fuel_gauge_prop_val *vals, size_t len)
Fetch multiple battery fuel-gauge properties.
#define SBS_GAUGE_DEVICE_CHEMISTRY_MAX_SIZE
Definition fuel_gauge.h:201
@ FUEL_GAUGE_CURRENT
Battery current (uA); negative=discharging.
Definition fuel_gauge.h:45
@ FUEL_GAUGE_STATUS
Alarm, Status and Error codes (flags)
Definition fuel_gauge.h:81
@ FUEL_GAUGE_SBS_MFR_ACCESS
Retrieve word from SBS1.1 ManufactuerAccess.
Definition fuel_gauge.h:65
@ FUEL_GAUGE_CONNECT_STATE
Connect state of battery.
Definition fuel_gauge.h:51
@ FUEL_GAUGE_FLAGS
General Error/Runtime Flags.
Definition fuel_gauge.h:53
@ FUEL_GAUGE_CYCLE_COUNT
Cycle count in 1/100ths (number of charge/discharge cycles)
Definition fuel_gauge.h:49
@ FUEL_GAUGE_COMMON_COUNT
Reserved to demark end of common fuel gauge properties.
Definition fuel_gauge.h:112
@ FUEL_GAUGE_MANUFACTURER_NAME
Manufacturer of pack (1 byte length + 20 bytes data)
Definition fuel_gauge.h:99
@ FUEL_GAUGE_SBS_MODE
Battery Mode (flags)
Definition fuel_gauge.h:75
@ FUEL_GAUGE_SBS_ATRATE
AtRate (mA or 10 mW)
Definition fuel_gauge.h:87
@ FUEL_GAUGE_DESIGN_VOLTAGE
Design Voltage (mV)
Definition fuel_gauge.h:85
@ FUEL_GAUGE_AVG_CURRENT
Runtime Dynamic Battery Parameters.
Definition fuel_gauge.h:40
@ FUEL_GAUGE_RUNTIME_TO_FULL
Remaining time in minutes until battery reaches full charge.
Definition fuel_gauge.h:63
@ FUEL_GAUGE_CHARGE_CURRENT
Battery desired Max Charging Current (uA)
Definition fuel_gauge.h:77
@ FUEL_GAUGE_CUSTOM_BEGIN
Reserved to demark downstream custom properties - use this value as the actual value may change over ...
Definition fuel_gauge.h:117
@ FUEL_GAUGE_DEVICE_CHEMISTRY
Chemistry (1 byte length + 4 bytes data)
Definition fuel_gauge.h:103
@ FUEL_GAUGE_PROP_MAX
Reserved to demark end of valid enum properties.
Definition fuel_gauge.h:120
@ FUEL_GAUGE_SBS_ATRATE_TIME_TO_EMPTY
AtRateTimeToEmpty (minutes)
Definition fuel_gauge.h:91
@ FUEL_GAUGE_DESIGN_CAPACITY
Design Capacity (mAh or 10mWh)
Definition fuel_gauge.h:83
@ FUEL_GAUGE_VOLTAGE
Battery voltage (uV)
Definition fuel_gauge.h:73
@ FUEL_GAUGE_SBS_REMAINING_TIME_ALARM
Remaining Time Alarm (minutes)
Definition fuel_gauge.h:97
@ FUEL_GAUGE_CHARGE_CUTOFF
Whether the battery underlying the fuel-gauge is cut off from charge.
Definition fuel_gauge.h:47
@ FUEL_GAUGE_CHARGE_VOLTAGE
Battery desired Max Charging Voltage (uV)
Definition fuel_gauge.h:79
@ FUEL_GAUGE_ABSOLUTE_STATE_OF_CHARGE
Absolute state of charge (percent, 0-100) - expressed as % of design capacity.
Definition fuel_gauge.h:67
@ FUEL_GAUGE_DEVICE_NAME
Name of pack (1 byte length + 20 bytes data)
Definition fuel_gauge.h:101
@ FUEL_GAUGE_SBS_ATRATE_TIME_TO_FULL
AtRateTimeToFull (minutes)
Definition fuel_gauge.h:89
@ FUEL_GAUGE_STATE_OF_CHARGE_ALARM
Remaining state of charge alarm (percent, 0-100)
Definition fuel_gauge.h:107
@ FUEL_GAUGE_TEMPERATURE
Temperature in 0.1 K.
Definition fuel_gauge.h:71
@ FUEL_GAUGE_FULL_CHARGE_CAPACITY
Full Charge Capacity in uAh (might change in some implementations to determine wear)
Definition fuel_gauge.h:55
@ FUEL_GAUGE_RUNTIME_TO_EMPTY
Remaining battery life time in minutes.
Definition fuel_gauge.h:61
@ FUEL_GAUGE_PRESENT_STATE
Is the battery physically present.
Definition fuel_gauge.h:57
@ FUEL_GAUGE_REMAINING_CAPACITY
Remaining capacity in uAh.
Definition fuel_gauge.h:59
@ FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM
Remaining Capacity Alarm (mAh or 10mWh)
Definition fuel_gauge.h:95
@ FUEL_GAUGE_LOW_VOLTAGE_ALARM
Low Cell Voltage Alarm (uV)
Definition fuel_gauge.h:109
@ FUEL_GAUGE_BATTERY_CUTOFF
Used to cutoff the battery from the system - useful for storage/shipping of devices.
Definition fuel_gauge.h:43
@ FUEL_GAUGE_CURRENT_DIRECTION
Battery current direction (flags)
Definition fuel_gauge.h:105
@ FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE
Relative state of charge (percent, 0-100) - expressed as % of full charge capacity.
Definition fuel_gauge.h:69
@ FUEL_GAUGE_SBS_ATRATE_OK
AtRateOK (boolean)
Definition fuel_gauge.h:93
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
#define UINT16_MAX
Definition stdint.h:28
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT16_TYPE__ int16_t
Definition stdint.h:73
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
Definition fuel_gauge.h:256
fuel_gauge_battery_cutoff_t battery_cutoff
Definition fuel_gauge.h:266
fuel_gauge_set_property_t set_property
Definition fuel_gauge.h:264
fuel_gauge_get_property_t get_property
Note: Historically this API allowed drivers to implement a custom multi-get/set property function,...
Definition fuel_gauge.h:263
fuel_gauge_get_buffer_property_t get_buffer_property
Definition fuel_gauge.h:265
Definition fuel_gauge.h:213
char device_chemistry[4]
Definition fuel_gauge.h:215
uint8_t device_chemistry_length
Definition fuel_gauge.h:214
Definition fuel_gauge.h:208
uint8_t device_name_length
Definition fuel_gauge.h:209
char device_name[20]
Definition fuel_gauge.h:210
Definition fuel_gauge.h:203
uint8_t manufacturer_name_length
Definition fuel_gauge.h:204
char manufacturer_name[20]
Definition fuel_gauge.h:205
Property field to value/type union.
Definition fuel_gauge.h:126
uint16_t design_volt
FUEL_GAUGE_DESIGN_VOLTAGE.
Definition fuel_gauge.h:175
uint16_t design_cap
FUEL_GAUGE_DESIGN_CAPACITY.
Definition fuel_gauge.h:173
uint32_t runtime_to_full
FUEL_GAUGE_RUNTIME_TO_FULL.
Definition fuel_gauge.h:153
uint8_t absolute_state_of_charge
FUEL_GAUGE_ABSOLUTE_STATE_OF_CHARGE.
Definition fuel_gauge.h:157
uint16_t temperature
FUEL_GAUGE_TEMPERATURE.
Definition fuel_gauge.h:161
uint8_t relative_state_of_charge
FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE.
Definition fuel_gauge.h:159
bool present_state
FUEL_GAUGE_PRESENT_STATE.
Definition fuel_gauge.h:147
uint16_t current_direction
FUEL_GAUGE_CURRENT_DIRECTION.
Definition fuel_gauge.h:189
uint16_t fg_status
FUEL_GAUGE_STATUS.
Definition fuel_gauge.h:171
uint32_t chg_current
FUEL_GAUGE_CHARGE_CURRENT.
Definition fuel_gauge.h:167
uint16_t sbs_at_rate_time_to_empty
FUEL_GAUGE_SBS_ATRATE_TIME_TO_EMPTY
Definition fuel_gauge.h:181
uint16_t sbs_mode
FUEL_GAUGE_SBS_MODE.
Definition fuel_gauge.h:165
uint16_t sbs_remaining_capacity_alarm
FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM.
Definition fuel_gauge.h:185
uint32_t chg_voltage
FUEL_GAUGE_CHARGE_VOLTAGE.
Definition fuel_gauge.h:169
uint16_t sbs_mfr_access_word
FUEL_GAUGE_SBS_MFR_ACCESS.
Definition fuel_gauge.h:155
bool sbs_at_rate_ok
FUEL_GAUGE_SBS_ATRATE_OK.
Definition fuel_gauge.h:183
int current
FUEL_GAUGE_CURRENT.
Definition fuel_gauge.h:137
uint32_t connect_state
FUEL_GAUGE_CONNECT_STATE.
Definition fuel_gauge.h:141
uint16_t sbs_remaining_time_alarm
FUEL_GAUGE_SBS_REMAINING_TIME_ALARM.
Definition fuel_gauge.h:187
uint32_t full_charge_capacity
FUEL_GAUGE_FULL_CHARGE_CAPACITY.
Definition fuel_gauge.h:145
int16_t sbs_at_rate
FUEL_GAUGE_SBS_ATRATE.
Definition fuel_gauge.h:177
uint8_t state_of_charge_alarm
FUEL_GAUGE_STATE_OF_CHARGE_ALARM.
Definition fuel_gauge.h:191
int voltage
FUEL_GAUGE_VOLTAGE.
Definition fuel_gauge.h:163
uint32_t cycle_count
FUEL_GAUGE_CYCLE_COUNT.
Definition fuel_gauge.h:139
bool cutoff
FUEL_GAUGE_CHARGE_CUTOFF.
Definition fuel_gauge.h:135
int avg_current
FUEL_GAUGE_AVG_CURRENT.
Definition fuel_gauge.h:133
uint32_t flags
FUEL_GAUGE_FLAGS.
Definition fuel_gauge.h:143
uint32_t remaining_capacity
FUEL_GAUGE_REMAINING_CAPACITY.
Definition fuel_gauge.h:149
uint32_t runtime_to_empty
FUEL_GAUGE_RUNTIME_TO_EMPTY.
Definition fuel_gauge.h:151
uint16_t sbs_at_rate_time_to_full
FUEL_GAUGE_SBS_ATRATE_TIME_TO_FULL.
Definition fuel_gauge.h:179
uint32_t low_voltage_alarm
FUEL_GAUGE_LOW_VOLTAGE_ALARM.
Definition fuel_gauge.h:193