1 /*
2 * Copyright (c) 2020 Linumiz
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/sys/printk.h>
11
bq274xx_show_values(const char * type,struct sensor_value value)12 static void bq274xx_show_values(const char *type, struct sensor_value value)
13 {
14 if ((value.val2 < 0) && (value.val1 >= 0)) {
15 value.val2 = -(value.val2);
16 printk("%s: -%d.%06d\n", type, value.val1, value.val2);
17 } else if ((value.val2 > 0) && (value.val1 < 0)) {
18 printk("%s: %d.%06d\n", type, value.val1, value.val2);
19 } else if ((value.val2 < 0) && (value.val1 < 0)) {
20 value.val2 = -(value.val2);
21 printk("%s: %d.%06d\n", type, value.val1, value.val2);
22 } else {
23 printk("%s: %d.%06d\n", type, value.val1, value.val2);
24 }
25 }
26
do_main(const struct device * dev)27 static void do_main(const struct device *dev)
28 {
29 int status = 0;
30 struct sensor_value voltage, current, state_of_charge,
31 full_charge_capacity, remaining_charge_capacity, avg_power,
32 int_temp, current_standby, current_max_load, state_of_health;
33
34 while (1) {
35 status = sensor_sample_fetch_chan(dev,
36 SENSOR_CHAN_GAUGE_VOLTAGE);
37 if (status < 0) {
38 printk("Unable to fetch the voltage\n");
39 return;
40 }
41
42 status = sensor_channel_get(dev, SENSOR_CHAN_GAUGE_VOLTAGE,
43 &voltage);
44 if (status < 0) {
45 printk("Unable to get the voltage value\n");
46 return;
47 }
48
49 printk("Voltage: %d.%06dV\n", voltage.val1, voltage.val2);
50
51 status = sensor_sample_fetch_chan(dev,
52 SENSOR_CHAN_GAUGE_AVG_CURRENT);
53 if (status < 0) {
54 printk("Unable to fetch the Average current\n");
55 return;
56 }
57
58 status = sensor_channel_get(dev, SENSOR_CHAN_GAUGE_AVG_CURRENT,
59 ¤t);
60 if (status < 0) {
61 printk("Unable to get the current value\n");
62 return;
63 }
64
65 bq274xx_show_values("Avg Current in Amps", current);
66
67 status = sensor_sample_fetch_chan(dev,
68 SENSOR_CHAN_GAUGE_STDBY_CURRENT);
69 if (status < 0) {
70 printk("Unable to fetch Standby Current\n");
71 return;
72 }
73
74 status = sensor_channel_get(dev,
75 SENSOR_CHAN_GAUGE_STDBY_CURRENT,
76 ¤t_standby);
77 if (status < 0) {
78 printk("Unable to get the current value\n");
79 return;
80 }
81
82 bq274xx_show_values("Standby Current in Amps", current_standby);
83
84 status = sensor_sample_fetch_chan(dev,
85 SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT);
86 if (status < 0) {
87 printk("Unable to fetch Max Load Current\n");
88 return;
89 }
90
91 status = sensor_channel_get(dev,
92 SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT,
93 ¤t_max_load);
94 if (status < 0) {
95 printk("Unable to get the current value\n");
96 return;
97 }
98
99 bq274xx_show_values("Max Load Current in Amps",
100 current_max_load);
101
102 status = sensor_sample_fetch_chan(dev,
103 SENSOR_CHAN_GAUGE_STATE_OF_CHARGE);
104 if (status < 0) {
105 printk("Unable to fetch State of Charge\n");
106 return;
107 }
108
109 status = sensor_channel_get(dev,
110 SENSOR_CHAN_GAUGE_STATE_OF_CHARGE,
111 &state_of_charge);
112 if (status < 0) {
113 printk("Unable to get state of charge\n");
114 return;
115 }
116
117 printk("State of charge: %d%%\n", state_of_charge.val1);
118
119 status = sensor_sample_fetch_chan(dev,
120 SENSOR_CHAN_GAUGE_STATE_OF_HEALTH);
121 if (status < 0) {
122 printk("Failed to fetch State of Health\n");
123 return;
124 }
125
126 status = sensor_channel_get(dev,
127 SENSOR_CHAN_GAUGE_STATE_OF_HEALTH,
128 &state_of_health);
129 if (status < 0) {
130 printk("Unable to get state of charge\n");
131 return;
132 }
133
134 printk("State of health: %d%%\n", state_of_health.val1);
135
136 status = sensor_sample_fetch_chan(dev,
137 SENSOR_CHAN_GAUGE_AVG_POWER);
138 if (status < 0) {
139 printk("Unable to fetch Avg Power\n");
140 return;
141 }
142
143 status = sensor_channel_get(dev, SENSOR_CHAN_GAUGE_AVG_POWER,
144 &avg_power);
145 if (status < 0) {
146 printk("Unable to get avg power\n");
147 return;
148 }
149
150 bq274xx_show_values("Avg Power in Watt", avg_power);
151
152 status = sensor_sample_fetch_chan(dev,
153 SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY);
154 if (status < 0) {
155 printk("Failed to fetch Full Charge Capacity\n");
156 return;
157 }
158
159 status = sensor_channel_get(dev,
160 SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY,
161 &full_charge_capacity);
162 if (status < 0) {
163 printk("Unable to get full charge capacity\n");
164 return;
165 }
166
167 printk("Full charge capacity: %d.%06dAh\n",
168 full_charge_capacity.val1, full_charge_capacity.val2);
169
170 status = sensor_sample_fetch_chan(dev,
171 SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY);
172 if (status < 0) {
173 printk("Unable to fetch Remaining Charge Capacity\n");
174 return;
175 }
176
177 status = sensor_channel_get(dev,
178 SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY,
179 &remaining_charge_capacity);
180 if (status < 0) {
181 printk("Unable to get remaining charge capacity\n");
182 return;
183 }
184
185 printk("Remaining charge capacity: %d.%06dAh\n",
186 remaining_charge_capacity.val1,
187 remaining_charge_capacity.val2);
188
189 status = sensor_sample_fetch_chan(dev, SENSOR_CHAN_GAUGE_TEMP);
190 if (status < 0) {
191 printk("Failed to fetch Gauge Temp\n");
192 return;
193 }
194
195 status = sensor_channel_get(dev, SENSOR_CHAN_GAUGE_TEMP,
196 &int_temp);
197 if (status < 0) {
198 printk("Unable to read internal temperature\n");
199 return;
200 }
201
202 printk("Gauge Temperature: %d.%06d C\n", int_temp.val1,
203 int_temp.val2);
204
205 k_sleep(K_MSEC(5000));
206 }
207 }
208
main(void)209 int main(void)
210 {
211 const struct device *const dev = DEVICE_DT_GET_ONE(ti_bq274xx);
212
213 if (!device_is_ready(dev)) {
214 printk("Device %s is not ready\n", dev->name);
215 return 0;
216 }
217
218 printk("device is %p, name is %s\n", dev, dev->name);
219
220 do_main(dev);
221 return 0;
222 }
223