Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ add_subdirectory(src/modules/fota)
add_subdirectory(src/modules/button)

# Optional modules
add_subdirectory_ifdef(CONFIG_APP_BATTERY src/modules/battery)
add_subdirectory_ifdef(CONFIG_APP_POWER src/modules/power)
add_subdirectory_ifdef(CONFIG_APP_ENVIRONMENTAL src/modules/environmental)
add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
add_subdirectory_ifdef(CONFIG_APP_SHELL src/modules/shell)
Expand Down
2 changes: 1 addition & 1 deletion app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ config APP_PAYLOAD_CHANNEL_BUFFER_MAX_SIZE
Contains encoded CBOR data sampled and encoded in the various modules.

rsource "src/Kconfig.main"
rsource "src/modules/battery/Kconfig.battery"
rsource "src/modules/power/Kconfig.power"
rsource "src/modules/network/Kconfig.network"
rsource "src/modules/cloud/Kconfig.cloud"
rsource "src/modules/location/Kconfig.location"
Expand Down
6 changes: 3 additions & 3 deletions app/boards/thingy91x_nrf9151_ns.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ CONFIG_APP_ENVIRONMENTAL=y
# Enable BME680
CONFIG_BME680=y

# Battery module
CONFIG_APP_BATTERY=y
# Power module
CONFIG_APP_POWER=y

# LED module
CONFIG_APP_LED=y
Expand Down Expand Up @@ -48,5 +48,5 @@ CONFIG_LOCATION_METHOD_WIFI_SCANNING_RESULTS_MAX_CNT=10
CONFIG_NRF_WIFI_SCAN_MAX_BSS_CNT=10

# Debug logging
CONFIG_APP_BATTERY_LOG_LEVEL_DBG=y
CONFIG_APP_POWER_LOG_LEVEL_DBG=y
CONFIG_APP_ENVIRONMENTAL_LOG_LEVEL_DBG=y
10 changes: 5 additions & 5 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#endif /* CONFIG_APP_ENVIRONMENTAL */

#if defined(CONFIG_APP_BATTERY)
#include "battery.h"
#include "power.h"
#endif /* CONFIG_APP_BATTERY */

/* Register log module */
Expand Down Expand Up @@ -239,11 +239,11 @@ static void sensor_and_poll_triggers_send(void)
#endif /* CONFIG_APP_REQUEST_NETWORK_QUALITY */

#if defined(CONFIG_APP_BATTERY)
struct battery_msg battery_msg = {
.type = BATTERY_PERCENTAGE_SAMPLE_REQUEST,
struct power_msg power_msg = {
.type = POWER_BATTERY_PERCENTAGE_SAMPLE_REQUEST,
};

err = zbus_chan_pub(&BATTERY_CHAN, &battery_msg, K_SECONDS(1));
err = zbus_chan_pub(&POWER_CHAN, &power_msg, K_SECONDS(1));
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
Expand All @@ -252,7 +252,7 @@ static void sensor_and_poll_triggers_send(void)
#endif /* CONFIG_APP_BATTERY */

#if defined(CONFIG_APP_ENVIRONMENTAL)
struct battery_msg environmental_msg = {
struct environmental_msg environmental_msg = {
.type = ENVIRONMENTAL_SENSOR_SAMPLE_REQUEST,
};

Expand Down
12 changes: 6 additions & 6 deletions app/src/modules/cloud/cloud_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "network.h"

#if defined(CONFIG_APP_BATTERY)
#include "battery.h"
#include "power.h"
#endif /* CONFIG_APP_BATTERY */

#if defined(CONFIG_APP_ENVIRONMENTAL)
Expand All @@ -37,7 +37,7 @@ LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
#define CUSTOM_JSON_APPID_VAL_BATTERY "BATTERY"

#if defined(CONFIG_APP_BATTERY)
#define BAT_MSG_SIZE sizeof(struct battery_msg)
#define BAT_MSG_SIZE sizeof(struct power_msg)
#else
#define BAT_MSG_SIZE 0
#endif /* CONFIG_APP_BATTERY */
Expand Down Expand Up @@ -69,7 +69,7 @@ ZBUS_CHAN_ADD_OBS(ENVIRONMENTAL_CHAN, cloud, 0);
#endif /* CONFIG_APP_ENVIRONMENTAL */

#if defined(CONFIG_APP_BATTERY)
ZBUS_CHAN_ADD_OBS(BATTERY_CHAN, cloud, 0);
ZBUS_CHAN_ADD_OBS(POWER_CHAN, cloud, 0);
#endif /* CONFIG_APP_BATTERY */

/* Define channels provided by this module */
Expand Down Expand Up @@ -568,10 +568,10 @@ static void state_connected_ready_run(void *o)
}

#if defined(CONFIG_APP_BATTERY)
if (state_object->chan == &BATTERY_CHAN) {
struct battery_msg msg = MSG_TO_BATTERY_MSG(state_object->msg_buf);
if (state_object->chan == &POWER_CHAN) {
struct power_msg msg = MSG_TO_POWER_MSG(state_object->msg_buf);

if (msg.type == BATTERY_PERCENTAGE_SAMPLE_RESPONSE) {
if (msg.type == POWER_BATTERY_PERCENTAGE_SAMPLE_RESPONSE) {
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_BATTERY,
msg.percentage,
NRF_CLOUD_NO_TIMESTAMP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/battery.c)
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/power.c)
target_include_directories(app PRIVATE .)
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menu "Battery"
menu "Power"

config APP_BATTERY
bool "Battery module"
config APP_POWER
bool "Power module"

if APP_BATTERY
if APP_POWER

config APP_BATTERY_THREAD_STACK_SIZE
config APP_POWER_THREAD_STACK_SIZE
int "Thread stack size"
default 1536

config APP_BATTERY_WATCHDOG_TIMEOUT_SECONDS
config APP_POWER_WATCHDOG_TIMEOUT_SECONDS
int "Watchdog timeout"
default 120
help
Timeout in seconds for the battery module watchdog.
Timeout in seconds for the power module watchdog.
The timeout given in this option covers both:
* Waiting for an incoming message in zbus_sub_wait_msg().
* Time spent processing the message, defined by
CONFIG_APP_BATTERY_MSG_PROCESSING_TIMEOUT_SECONDS.
Ensure that this value exceeds CONFIG_APP_BATTERY_MSG_PROCESSING_TIMEOUT_SECONDS.
CONFIG_APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS.
Ensure that this value exceeds CONFIG_APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS.
A small difference between the two can mean more frequent watchdog feeds, which increases
power consumption.

config APP_BATTERY_MSG_PROCESSING_TIMEOUT_SECONDS
config APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS
int "Maximum message processing time"
default 3
help
Maximum time allowed for processing a single message in the module's state machine.
The value must be smaller than CONFIG_APP_BATTERY_WATCHDOG_TIMEOUT_SECONDS.
The value must be smaller than CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS.

module = APP_BATTERY
module-str = Battery
module = APP_POWER
module-str = Power
source "subsys/logging/Kconfig.template.log_config"

endif # APP_BATTERY
endif # APP_POWER

endmenu # Battery
endmenu # Power
68 changes: 34 additions & 34 deletions app/src/modules/battery/battery.c → app/src/modules/power/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@
#include "lp803448_model.h"
#include "message_channel.h"
#include "modules_common.h"
#include "battery.h"
#include "power.h"

/* Register log module */
LOG_MODULE_REGISTER(battery, CONFIG_APP_BATTERY_LOG_LEVEL);
LOG_MODULE_REGISTER(power, CONFIG_APP_POWER_LOG_LEVEL);

/* Register subscriber */
ZBUS_MSG_SUBSCRIBER_DEFINE(battery);
ZBUS_MSG_SUBSCRIBER_DEFINE(power);

/* Define channels provided by this module */
ZBUS_CHAN_DEFINE(BATTERY_CHAN,
struct battery_msg,
ZBUS_CHAN_DEFINE(POWER_CHAN,
struct power_msg,
NULL,
NULL,
ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(0)
);

/* Observe channels */
ZBUS_CHAN_ADD_OBS(BATTERY_CHAN, battery, 0);
ZBUS_CHAN_ADD_OBS(POWER_CHAN, power, 0);

#define MAX_MSG_SIZE sizeof(struct battery_msg)
#define MAX_MSG_SIZE sizeof(struct power_msg)

BUILD_ASSERT(CONFIG_APP_BATTERY_WATCHDOG_TIMEOUT_SECONDS >
CONFIG_APP_BATTERY_MSG_PROCESSING_TIMEOUT_SECONDS,
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 */
Expand All @@ -63,16 +63,16 @@ static void sample(int64_t *ref_time);

/* Defininig the module states.
*
* STATE_RUNNING: The battery module is initializing and waiting battery percentage to be requested.
* STATE_RUNNING: The power module is initializing and waiting battery percentage to be requested.
*/
enum battery_module_state {
enum power_module_state {
STATE_RUNNING,
};

/* User defined state object.
* Used to transfer data between state changes.
*/
struct battery_state {
struct power_state {
/* This must be first */
struct smf_ctx ctx;

Expand All @@ -90,7 +90,7 @@ struct battery_state {
static void state_running_entry(void *o);
static void state_running_run(void *o);

static struct battery_state battery_state_object;
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),
Expand All @@ -106,7 +106,7 @@ static void state_running_entry(void *o)
.model = &battery_model
};
int32_t chg_status;
struct battery_state *state_object = o;
struct power_state *state_object = o;

if (!device_is_ready(charger)) {
LOG_ERR("Charger device not ready.");
Expand All @@ -132,20 +132,20 @@ static void state_running_entry(void *o)

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);
LOG_ERR("sensor_channel_get, DESIRED_CHARGING_CURRENT, error: %d", err);
SEND_FATAL_ERROR();
return;
}
}

static void state_running_run(void *o)
{
struct battery_state *state_object = o;
struct power_state *state_object = o;

if (&BATTERY_CHAN == state_object->chan) {
struct battery_msg msg = MSG_TO_BATTERY_MSG(state_object->msg_buf);
if (&POWER_CHAN == state_object->chan) {
struct power_msg msg = MSG_TO_POWER_MSG(state_object->msg_buf);

if (msg.type == BATTERY_PERCENTAGE_SAMPLE_REQUEST) {
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);
}
Expand Down Expand Up @@ -209,12 +209,12 @@ static void sample(int64_t *ref_time)
LOG_DBG("State of charge: %f", (double)roundf(state_of_charge));
LOG_DBG("The battery is %s", charging ? "charging" : "not charging");

struct battery_msg msg = {
.type = BATTERY_PERCENTAGE_SAMPLE_RESPONSE,
struct power_msg msg = {
.type = POWER_BATTERY_PERCENTAGE_SAMPLE_RESPONSE,
.percentage = (double)roundf(state_of_charge)
};

err = zbus_chan_pub(&BATTERY_CHAN, &msg, K_NO_WAIT);
err = zbus_chan_pub(&POWER_CHAN, &msg, K_NO_WAIT);
if (err) {
LOG_ERR("zbus_chan_pub, error: %d", err);
SEND_FATAL_ERROR();
Expand All @@ -230,21 +230,21 @@ static void task_wdt_callback(int channel_id, void *user_data)
SEND_FATAL_ERROR_WATCHDOG_TIMEOUT();
}

static void battery_task(void)
static void power_task(void)
{
int err;
int task_wdt_id;
const uint32_t wdt_timeout_ms =
(CONFIG_APP_BATTERY_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
(CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
const uint32_t execution_time_ms =
(CONFIG_APP_BATTERY_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC);
(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("Battery module task started");
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(battery_state_object, STATE_RUNNING);
STATE_SET_INITIAL(power_state_object, STATE_RUNNING);

while (true) {
err = task_wdt_feed(task_wdt_id);
Expand All @@ -254,9 +254,9 @@ static void battery_task(void)
return;
}

err = zbus_sub_wait_msg(&battery,
&battery_state_object.chan,
battery_state_object.msg_buf,
err = zbus_sub_wait_msg(&power,
&power_state_object.chan,
power_state_object.msg_buf,
zbus_wait_ms);
if (err == -ENOMSG) {
continue;
Expand All @@ -266,7 +266,7 @@ static void battery_task(void)
return;
}

err = STATE_RUN(battery_state_object);
err = STATE_RUN(power_state_object);
if (err) {
LOG_ERR("handle_message, error: %d", err);
SEND_FATAL_ERROR();
Expand All @@ -275,6 +275,6 @@ static void battery_task(void)
}
}

K_THREAD_DEFINE(battery_task_id,
CONFIG_APP_BATTERY_THREAD_STACK_SIZE,
battery_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);
K_THREAD_DEFINE(power_task_id,
CONFIG_APP_POWER_THREAD_STACK_SIZE,
power_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);
Loading
Loading