Skip to content

Commit d6dc070

Browse files
applications: 93m1_ppp: General cleanup of the 93m1_ppp application
A general cleanup of the 93m1_ppp application. This includes removing unused code, and moving logic to be more uniform. Signed-off-by: Syver Haraldsen <syver.haraldsen@nordicsemi.no>
1 parent 2df0171 commit d6dc070

9 files changed

Lines changed: 452 additions & 210 deletions

File tree

applications/93m1_ppp/src/main.c

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#else
2121
#define FOTA_CHANNEL(X)
2222
#endif
23+
#if defined(CONFIG_APP_LOCATION)
24+
#include "modules/location/location.h"
25+
#endif
26+
#if defined(CONFIG_APP_BATTERY)
27+
#include "modules/battery/battery.h"
28+
#endif
2329

2430
LOG_MODULE_REGISTER(main, CONFIG_APP_MAIN_LOG_LEVEL);
2531

@@ -66,7 +72,44 @@ struct main_state {
6672
uint8_t msg_buf[MAX_MSG_SIZE];
6773
};
6874

69-
static const struct smf_state states[];
75+
#if defined(CONFIG_APP_LOCATION)
76+
static void location_fix_handler(struct k_work *work);
77+
K_WORK_DELAYABLE_DEFINE(location_fix_work, location_fix_handler);
78+
#endif
79+
#if defined(CONFIG_APP_BATTERY)
80+
static void battery_sample_handler(struct k_work *work);
81+
K_WORK_DELAYABLE_DEFINE(battery_sample_work, battery_sample_handler);
82+
#endif
83+
84+
static enum smf_state_result init_run(void *obj);
85+
static void running_entry(void *obj);
86+
static enum smf_state_result running_run(void *obj);
87+
88+
static const struct smf_state states[] = {
89+
[STATE_INIT] = SMF_CREATE_STATE(NULL, init_run, NULL, NULL, NULL),
90+
[STATE_RUNNING] = SMF_CREATE_STATE(running_entry, running_run, NULL, NULL, NULL),
91+
};
92+
93+
#if defined(CONFIG_APP_LOCATION)
94+
static void location_fix_handler(struct k_work *work)
95+
{
96+
struct location_msg msg = { .type = LOCATION_FIX_REQUEST, .mode = LOCATION_MODE_ALL };
97+
98+
(void)zbus_chan_pub(&location_chan, &msg, PUB_TIMEOUT);
99+
k_work_reschedule(&location_fix_work, K_SECONDS(CONFIG_APP_LOCATION_INTERVAL_SECONDS));
100+
}
101+
#endif
102+
103+
#if defined(CONFIG_APP_BATTERY)
104+
static void battery_sample_handler(struct k_work *work)
105+
{
106+
struct battery_msg msg = { .type = BATTERY_SAMPLE };
107+
108+
(void)zbus_chan_pub(&battery_chan, &msg, PUB_TIMEOUT);
109+
k_work_reschedule(&battery_sample_work,
110+
K_SECONDS(CONFIG_APP_BATTERY_SAMPLE_INTERVAL_SECONDS));
111+
}
112+
#endif
70113

71114
static enum smf_state_result init_run(void *obj)
72115
{
@@ -90,6 +133,10 @@ static void running_entry(void *obj)
90133
ARG_UNUSED(obj);
91134

92135
LOG_INF("Serial Modem Host 93m1 starting");
136+
137+
#if defined(CONFIG_APP_BATTERY)
138+
k_work_reschedule(&battery_sample_work, K_NO_WAIT);
139+
#endif
93140
}
94141

95142
static enum smf_state_result running_run(void *obj)
@@ -103,9 +150,16 @@ static enum smf_state_result running_run(void *obj)
103150
switch (msg->type) {
104151
case NETWORK_CONNECTED:
105152
LOG_INF("Network connected");
153+
#if defined(CONFIG_APP_LOCATION)
154+
k_work_reschedule(&location_fix_work,
155+
K_SECONDS(CONFIG_APP_LOCATION_BOOT_DELAY_SECONDS));
156+
#endif
106157
break;
107158
case NETWORK_DISCONNECTED:
108159
LOG_INF("Network disconnected");
160+
#if defined(CONFIG_APP_LOCATION)
161+
k_work_cancel_delayable(&location_fix_work);
162+
#endif
109163
#if defined(CONFIG_APP_FOTA)
110164
{
111165
struct fota_msg fota_msg = { .type = FOTA_NETWORK_DISCONNECTED };
@@ -159,11 +213,6 @@ static enum smf_state_result running_run(void *obj)
159213
return SMF_EVENT_HANDLED;
160214
}
161215

162-
static const struct smf_state states[] = {
163-
[STATE_INIT] = SMF_CREATE_STATE(NULL, init_run, NULL, NULL, NULL),
164-
[STATE_RUNNING] = SMF_CREATE_STATE(running_entry, running_run, NULL, NULL, NULL),
165-
};
166-
167216
static void main_wdt_callback(int channel_id, void *user_data)
168217
{
169218
LOG_ERR("Main watchdog expired, channel: %d, thread: %s",

applications/93m1_ppp/src/modules/battery/battery.c

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
#include <zephyr/kernel.h>
88
#include <zephyr/logging/log.h>
9+
#include <zephyr/zbus/zbus.h>
910
#include <zephyr/drivers/sensor.h>
1011
#include <zephyr/drivers/sensor/npm13xx_charger.h>
1112
#include <nrf_fuel_gauge.h>
1213

14+
#include "app_common.h"
15+
#include "battery.h"
1316
#include "lp803448_model.h"
1417

1518
LOG_MODULE_REGISTER(battery, CONFIG_APP_BATTERY_LOG_LEVEL);
@@ -20,12 +23,33 @@ LOG_MODULE_REGISTER(battery, CONFIG_APP_BATTERY_LOG_LEVEL);
2023
#define CHG_STATUS_CC_MASK BIT(3)
2124
#define CHG_STATUS_CV_MASK BIT(4)
2225

23-
#define SAMPLE_INTERVAL K_SECONDS(CONFIG_APP_BATTERY_SAMPLE_INTERVAL_SECONDS)
26+
ZBUS_CHAN_DEFINE(battery_chan,
27+
struct battery_msg,
28+
NULL,
29+
NULL,
30+
ZBUS_OBSERVERS_EMPTY,
31+
ZBUS_MSG_INIT(0));
2432

25-
static const struct device *const charger = DEVICE_DT_GET(DT_NODELABEL(npm1300_charger));
33+
ZBUS_MSG_SUBSCRIBER_DEFINE(battery);
2634

27-
static int read_sensors(float *voltage, float *current, float *temp, int32_t *chg_status,
28-
bool *vbus)
35+
#define CHANNEL_LIST(X) X(battery_chan, struct battery_msg)
36+
37+
#define MAX_MSG_SIZE MAX_MSG_SIZE_FROM_LIST(CHANNEL_LIST)
38+
39+
#define ADD_OBSERVERS(_chan, _type) ZBUS_CHAN_ADD_OBS(_chan, battery, 0);
40+
41+
CHANNEL_LIST(ADD_OBSERVERS)
42+
43+
static int read_sensors(const struct device *charger, float *voltage, float *current,
44+
float *temp, int32_t *chg_status, bool *vbus);
45+
static void update_charge_state(int32_t chg_status, int32_t *prev);
46+
static int fuel_gauge_setup(const struct device *charger);
47+
static void battery_sample(const struct device *charger, int64_t *ref_time,
48+
int32_t *prev_chg_status);
49+
static void battery_thread(void);
50+
51+
static int read_sensors(const struct device *charger, float *voltage, float *current,
52+
float *temp, int32_t *chg_status, bool *vbus)
2953
{
3054
int err;
3155
struct sensor_value val = {0};
@@ -96,15 +120,15 @@ static void update_charge_state(int32_t chg_status, int32_t *prev)
96120
&ext);
97121
}
98122

99-
static int fuel_gauge_setup(void)
123+
static int fuel_gauge_setup(const struct device *charger)
100124
{
101125
int err;
102126
int32_t chg_status;
103127
bool vbus;
104128
struct nrf_fuel_gauge_init_parameters params = { .model = &battery_model };
105129
struct sensor_value desired;
106130

107-
err = read_sensors(&params.v0, &params.i0, &params.t0, &chg_status, &vbus);
131+
err = read_sensors(charger, &params.v0, &params.i0, &params.t0, &chg_status, &vbus);
108132
if (err) {
109133
return err;
110134
}
@@ -139,55 +163,69 @@ static int fuel_gauge_setup(void)
139163
/* Owns the fuel gauge: init once, then keep it fed. Memfault reads SoC/SoH via
140164
* nrf_fuel_gauge_soc_get()/soh_get() on its own heartbeat.
141165
*/
166+
static void battery_sample(const struct device *charger, int64_t *ref_time,
167+
int32_t *prev_chg_status)
168+
{
169+
int err;
170+
float voltage;
171+
float current;
172+
float temp;
173+
float soc;
174+
int32_t chg_status;
175+
bool vbus;
176+
177+
err = read_sensors(charger, &voltage, &current, &temp, &chg_status, &vbus);
178+
if (err) {
179+
LOG_WRN("read_sensors, error: %d", err);
180+
return;
181+
}
182+
183+
(void)nrf_fuel_gauge_ext_state_update(
184+
vbus ? NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_CONNECTED
185+
: NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_DISCONNECTED, NULL);
186+
update_charge_state(chg_status, prev_chg_status);
187+
188+
float delta = (float)k_uptime_delta(ref_time) / 1000.0f;
189+
190+
err = nrf_fuel_gauge_process(voltage, current, temp, delta, &soc, NULL);
191+
if (err) {
192+
LOG_WRN("nrf_fuel_gauge_process, error: %d", err);
193+
return;
194+
}
195+
196+
LOG_DBG("SoC %d%%, %d mV, %s", (int)soc, (int)(voltage * 1000),
197+
vbus ? "VBUS" : "battery");
198+
}
199+
142200
static void battery_thread(void)
143201
{
144202
int err;
203+
const struct zbus_channel *chan;
204+
uint8_t msg_buf[MAX_MSG_SIZE];
145205
int64_t ref_time;
146206
int32_t prev_chg_status = -1;
207+
const struct device *const charger = DEVICE_DT_GET(DT_NODELABEL(npm1300_charger));
147208

148209
if (!device_is_ready(charger)) {
149210
LOG_ERR("Charger device not ready");
150211
return;
151212
}
152213

153-
err = fuel_gauge_setup();
214+
err = fuel_gauge_setup(charger);
154215
if (err) {
155216
LOG_ERR("fuel_gauge_setup, error: %d", err);
156217
return;
157218
}
158219
ref_time = k_uptime_get();
159220

160221
while (true) {
161-
float voltage;
162-
float current;
163-
float temp;
164-
float soc;
165-
int32_t chg_status;
166-
bool vbus;
167-
168-
k_sleep(SAMPLE_INTERVAL);
169-
170-
err = read_sensors(&voltage, &current, &temp, &chg_status, &vbus);
171-
if (err) {
172-
LOG_WRN("read_sensors, error: %d", err);
173-
continue;
174-
}
175-
176-
(void)nrf_fuel_gauge_ext_state_update(
177-
vbus ? NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_CONNECTED
178-
: NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_DISCONNECTED, NULL);
179-
update_charge_state(chg_status, &prev_chg_status);
180-
181-
float delta = (float)k_uptime_delta(&ref_time) / 1000.0f;
182-
183-
err = nrf_fuel_gauge_process(voltage, current, temp, delta, &soc, NULL);
222+
err = zbus_sub_wait_msg(&battery, &chan, msg_buf, K_FOREVER);
184223
if (err) {
185-
LOG_WRN("nrf_fuel_gauge_process, error: %d", err);
186-
continue;
224+
LOG_ERR("zbus_sub_wait_msg, error: %d", err);
225+
return;
187226
}
188227

189-
LOG_DBG("SoC %d%%, %d mV, %s", (int)soc, (int)(voltage * 1000),
190-
vbus ? "VBUS" : "battery");
228+
battery_sample(charger, &ref_time, &prev_chg_status);
191229
}
192230
}
193231

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2026 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef BATTERY_H_
8+
#define BATTERY_H_
9+
10+
#include <zephyr/zbus/zbus.h>
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
enum battery_msg_type {
17+
BATTERY_SAMPLE,
18+
};
19+
20+
struct battery_msg {
21+
enum battery_msg_type type;
22+
};
23+
24+
ZBUS_CHAN_DECLARE(battery_chan);
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif
29+
30+
#endif /* BATTERY_H_ */

0 commit comments

Comments
 (0)