-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpower.c
More file actions
280 lines (224 loc) · 6.98 KB
/
power.c
File metadata and controls
280 lines (224 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
#include <zephyr/drivers/sensor/npm1300_charger.h>
#include <zephyr/sys/util.h>
#include <nrf_fuel_gauge.h>
#include <date_time.h>
#include <math.h>
#include <zephyr/task_wdt/task_wdt.h>
#include <zephyr/smf.h>
#include "lp803448_model.h"
#include "message_channel.h"
#include "modules_common.h"
#include "power.h"
/* Register log module */
LOG_MODULE_REGISTER(power, CONFIG_APP_POWER_LOG_LEVEL);
/* Register subscriber */
ZBUS_MSG_SUBSCRIBER_DEFINE(power);
/* Define channels provided by this module */
ZBUS_CHAN_DEFINE(POWER_CHAN,
struct power_msg,
NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(0)
);
/* Observe channels */
ZBUS_CHAN_ADD_OBS(POWER_CHAN, power, 0);
#define MAX_MSG_SIZE sizeof(struct power_msg)
BUILD_ASSERT(CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS >
CONFIG_APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS,
"Watchdog timeout must be greater than maximum message processing time");
/* nPM1300 register bitmasks */
/* CHARGER.BCHGCHARGESTATUS.TRICKLECHARGE */
#define NPM1300_CHG_STATUS_TC_MASK BIT(2)
/* CHARGER.BCHGCHARGESTATUS.CONSTANTCURRENT */
#define NPM1300_CHG_STATUS_CC_MASK BIT(3)
/* CHARGER.BCHGCHARGESTATUS.CONSTANTVOLTAGE */
#define NPM1300_CHG_STATUS_CV_MASK BIT(4)
static const struct device *charger = DEVICE_DT_GET(DT_NODELABEL(npm1300_charger));
/* Forward declarations */
static int charger_read_sensors(float *voltage, float *current, float *temp, int32_t *chg_status);
static void sample(int64_t *ref_time);
/* State machine */
/* Defininig the module states.
*
* STATE_RUNNING: The power module is initializing and waiting battery percentage to be requested.
*/
enum power_module_state {
STATE_RUNNING,
};
/* User defined state object.
* Used to transfer data between state changes.
*/
struct power_state {
/* This must be first */
struct smf_ctx ctx;
/* Last channel type that a message was received on */
const struct zbus_channel *chan;
/* Buffer for last zbus message */
uint8_t msg_buf[MAX_MSG_SIZE];
/* Fuel gauge reference time */
int64_t fuel_gauge_ref_time;
};
/* Forward declarations of state handlers */
static void state_running_entry(void *o);
static void state_running_run(void *o);
static struct power_state power_state_object;
static const struct smf_state states[] = {
[STATE_RUNNING] =
SMF_CREATE_STATE(state_running_entry, state_running_run, NULL, NULL, NULL),
};
/* State handlers */
static void state_running_entry(void *o)
{
int err;
struct sensor_value value;
struct nrf_fuel_gauge_init_parameters parameters = {
.model = &battery_model
};
int32_t chg_status;
struct power_state *state_object = o;
if (!device_is_ready(charger)) {
LOG_ERR("Charger device not ready.");
SEND_FATAL_ERROR();
return;
}
err = charger_read_sensors(¶meters.v0, ¶meters.i0, ¶meters.t0, &chg_status);
if (err < 0) {
LOG_ERR("charger_read_sensors, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = nrf_fuel_gauge_init(¶meters, NULL);
if (err) {
LOG_ERR("nrf_fuel_gauge_init, error: %d", err);
SEND_FATAL_ERROR();
return;
}
state_object->fuel_gauge_ref_time = k_uptime_get();
err = sensor_channel_get(charger, SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT, &value);
if (err) {
LOG_ERR("sensor_channel_get, DESIRED_CHARGING_CURRENT, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static void state_running_run(void *o)
{
struct power_state *state_object = o;
if (&POWER_CHAN == state_object->chan) {
struct power_msg msg = MSG_TO_POWER_MSG(state_object->msg_buf);
if (msg.type == POWER_BATTERY_PERCENTAGE_SAMPLE_REQUEST) {
LOG_DBG("Battery percentage sample request received, getting battery data");
sample(&state_object->fuel_gauge_ref_time);
}
}
}
/* End of state handling */
static int charger_read_sensors(float *voltage, float *current, float *temp, int32_t *chg_status)
{
struct sensor_value value;
int err;
err = sensor_sample_fetch(charger);
if (err < 0) {
return err;
}
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_VOLTAGE, &value);
*voltage = (float)value.val1 + ((float)value.val2 / 1000000);
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_TEMP, &value);
*temp = (float)value.val1 + ((float)value.val2 / 1000000);
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_AVG_CURRENT, &value);
*current = (float)value.val1 + ((float)value.val2 / 1000000);
sensor_channel_get(charger, (enum sensor_channel)SENSOR_CHAN_NPM1300_CHARGER_STATUS,
&value);
*chg_status = value.val1;
return 0;
}
static void sample(int64_t *ref_time)
{
int err;
int chg_status;
bool charging;
float voltage;
float current;
float temp;
float state_of_charge;
float delta;
err = charger_read_sensors(&voltage, ¤t, &temp, &chg_status);
if (err) {
LOG_ERR("charger_read_sensors, error: %d", err);
SEND_FATAL_ERROR();
return;
}
delta = (float)k_uptime_delta(ref_time) / 1000.f;
charging = (chg_status & (NPM1300_CHG_STATUS_TC_MASK |
NPM1300_CHG_STATUS_CC_MASK |
NPM1300_CHG_STATUS_CV_MASK)) != 0;
state_of_charge = nrf_fuel_gauge_process(voltage, current, temp, delta, NULL);
LOG_DBG("State of charge: %f", (double)roundf(state_of_charge));
LOG_DBG("The battery is %s", charging ? "charging" : "not charging");
struct power_msg msg = {
.type = POWER_BATTERY_PERCENTAGE_SAMPLE_RESPONSE,
.percentage = (double)roundf(state_of_charge)
};
err = zbus_chan_pub(&POWER_CHAN, &msg, K_NO_WAIT);
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
static void task_wdt_callback(int channel_id, void *user_data)
{
LOG_ERR("Watchdog expired, Channel: %d, Thread: %s",
channel_id, k_thread_name_get((k_tid_t)user_data));
SEND_FATAL_ERROR_WATCHDOG_TIMEOUT();
}
static void power_task(void)
{
int err;
int task_wdt_id;
const uint32_t wdt_timeout_ms =
(CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
const uint32_t execution_time_ms =
(CONFIG_APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC);
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
LOG_DBG("Power module task started");
task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get());
STATE_SET_INITIAL(power_state_object, STATE_RUNNING);
while (true) {
err = task_wdt_feed(task_wdt_id);
if (err) {
LOG_ERR("task_wdt_feed, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = zbus_sub_wait_msg(&power,
&power_state_object.chan,
power_state_object.msg_buf,
zbus_wait_ms);
if (err == -ENOMSG) {
continue;
} else if (err) {
LOG_ERR("zbus_sub_wait_msg, error: %d", err);
SEND_FATAL_ERROR();
return;
}
err = STATE_RUN(power_state_object);
if (err) {
LOG_ERR("handle_message, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}
}
K_THREAD_DEFINE(power_task_id,
CONFIG_APP_POWER_THREAD_STACK_SIZE,
power_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);