Zephyr API Documentation
4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
battery.h
Go to the documentation of this file.
1
/*
2
* Copyright 2024 Embeint Inc
3
*
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_H_
8
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_H_
9
10
#include <
stdint.h
>
11
#include <
errno.h
>
12
13
#include <
zephyr/devicetree.h
>
14
#include <
zephyr/math/interpolation.h
>
15
#include <
zephyr/sys/util_macro.h
>
16
17
#ifdef __cplusplus
18
extern
"C"
{
19
#endif
20
27
/* Battery chemistry enumeration.
28
* Value names must match those from dts/bindings/battery.yaml
29
*/
30
enum
battery_chemistry
{
31
BATTERY_CHEMISTRY_UNKNOWN
= 0,
32
BATTERY_CHEMISTRY_NICKEL_CADMIUM
,
33
BATTERY_CHEMISTRY_NICKEL_METAL_HYDRIDE
,
34
BATTERY_CHEMISTRY_LITHIUM_ION
,
35
BATTERY_CHEMISTRY_LITHIUM_ION_POLYMER
,
36
BATTERY_CHEMISTRY_LITHIUM_ION_IRON_PHOSPHATE
,
37
BATTERY_CHEMISTRY_LITHIUM_ION_MANGANESE_OXIDE
,
38
};
39
40
/* Length of open circuit voltage table */
41
#define BATTERY_OCV_TABLE_LEN 11
42
48
#define BATTERY_CHEMISTRY_DT_GET(node_id) \
49
UTIL_CAT(BATTERY_CHEMISTRY_, DT_STRING_UPPER_TOKEN_OR(node_id, device_chemistry, UNKNOWN))
50
57
#define BATTERY_OCV_TABLE_DT_GET(node_id, table) \
58
COND_CODE_1(DT_NODE_HAS_PROP(node_id, table), \
59
({DT_FOREACH_PROP_ELEM_SEP(node_id, table, DT_PROP_BY_IDX, (,))}), ({-1}))
60
69
static
inline
int32_t
battery_soc_lookup
(
const
int32_t
ocv_table[
BATTERY_OCV_TABLE_LEN
],
70
uint32_t
voltage_uv)
71
{
72
static
const
int32_t
soc_axis[
BATTERY_OCV_TABLE_LEN
] = {
73
0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000};
74
75
/* Convert voltage to SoC */
76
return
linear_interpolate
(ocv_table, soc_axis,
BATTERY_OCV_TABLE_LEN
, voltage_uv);
77
}
78
83
#ifdef __cplusplus
84
}
85
#endif
86
87
#endif
/* ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_H_ */
devicetree.h
Devicetree main header.
errno.h
System error numbers.
BATTERY_OCV_TABLE_LEN
#define BATTERY_OCV_TABLE_LEN
Definition
battery.h:41
battery_soc_lookup
static int32_t battery_soc_lookup(const int32_t ocv_table[11], uint32_t voltage_uv)
Convert an OCV table and battery voltage to a charge percentage.
Definition
battery.h:69
battery_chemistry
battery_chemistry
Definition
battery.h:30
BATTERY_CHEMISTRY_NICKEL_CADMIUM
@ BATTERY_CHEMISTRY_NICKEL_CADMIUM
Definition
battery.h:32
BATTERY_CHEMISTRY_LITHIUM_ION_MANGANESE_OXIDE
@ BATTERY_CHEMISTRY_LITHIUM_ION_MANGANESE_OXIDE
Definition
battery.h:37
BATTERY_CHEMISTRY_NICKEL_METAL_HYDRIDE
@ BATTERY_CHEMISTRY_NICKEL_METAL_HYDRIDE
Definition
battery.h:33
BATTERY_CHEMISTRY_LITHIUM_ION
@ BATTERY_CHEMISTRY_LITHIUM_ION
Definition
battery.h:34
BATTERY_CHEMISTRY_UNKNOWN
@ BATTERY_CHEMISTRY_UNKNOWN
Definition
battery.h:31
BATTERY_CHEMISTRY_LITHIUM_ION_IRON_PHOSPHATE
@ BATTERY_CHEMISTRY_LITHIUM_ION_IRON_PHOSPHATE
Definition
battery.h:36
BATTERY_CHEMISTRY_LITHIUM_ION_POLYMER
@ BATTERY_CHEMISTRY_LITHIUM_ION_POLYMER
Definition
battery.h:35
interpolation.h
Provide linear interpolation functions.
linear_interpolate
static int32_t linear_interpolate(const int32_t *x_axis, const int32_t *y_axis, uint8_t len, int32_t x)
Perform a linear interpolation across an arbitrary curve.
Definition
interpolation.h:37
stdint.h
uint32_t
__UINT32_TYPE__ uint32_t
Definition
stdint.h:90
int32_t
__INT32_TYPE__ int32_t
Definition
stdint.h:74
util_macro.h
Macro utilities.
zephyr
drivers
sensor
battery.h
Generated on Mon Nov 18 2024 03:14:08 for Zephyr API Documentation by
1.12.0