Skip to content

Commit 9f1364c

Browse files
author
mkuettner97
committed
add shell command for battery info and fuel gauge setup
1 parent 1eb9bce commit 9f1364c

5 files changed

Lines changed: 78 additions & 6 deletions

File tree

boards/teco/openearable_v2/openearable_v2_nrf5340_cpuapp_common-pinctrl.dtsi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@
8787
group1 {
8888
psels = <NRF_PSEL(UART_TX, 1, 4)>,
8989
<NRF_PSEL(UART_RX, 0, 20)>,
90-
<NRF_PSEL(UART_RTS, 0, 27)>,
91-
<NRF_PSEL(UART_CTS, 0, 25)>;
90+
<NRF_PSEL_DISCONNECTED(UART_RTS)>,
91+
<NRF_PSEL_DISCONNECTED(UART_CTS)>;
9292
};
9393
};
9494

9595
uart0_sleep: uart0_sleep {
9696
group1 {
9797
psels = <NRF_PSEL(UART_TX, 1, 4)>,
9898
<NRF_PSEL(UART_RX, 0, 20)>,
99-
<NRF_PSEL(UART_RTS, 0, 27)>,
100-
<NRF_PSEL(UART_CTS, 0, 25)>;
99+
<NRF_PSEL_DISCONNECTED(UART_RTS)>,
100+
<NRF_PSEL_DISCONNECTED(UART_CTS)>;
101101
low-power-enable;
102102
};
103103
};

prj.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ CONFIG_KERNEL_SHELL=y
4646
CONFIG_USE_SEGGER_RTT=y
4747
## Disable logs on RTT
4848
CONFIG_SHELL_RTT_INIT_LOG_LEVEL_NONE=y
49-
CONFIG_SHELL_BACKEND_RTT=y
50-
CONFIG_SHELL_BACKEND_SERIAL=n
49+
CONFIG_SHELL_BACKEND_RTT=n
50+
CONFIG_SHELL_BACKEND_SERIAL=y
5151
CONFIG_SHELL_VT100_COMMANDS=y
5252
CONFIG_SHELL_VT100_COLORS=y
5353
CONFIG_SHELL_STACK_SIZE=4096

src/Battery/PowerManager.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <zephyr/sys/poweroff.h>
66
#include <zephyr/sys/reboot.h>
7+
#include <zephyr/shell/shell.h>
78

89
#include <zephyr/pm/pm.h>
910
#include <zephyr/pm/state.h>
@@ -558,4 +559,71 @@ void PowerManager::charge_task() {
558559
last_charging_state = charging_state;
559560
}
560561

562+
int cmd_setup_fuel_gauge(const struct shell *shell, size_t argc, const char **argv) {
563+
ARG_UNUSED(argc);
564+
ARG_UNUSED(argv);
565+
566+
fuel_gauge.setup(power_manager._battery_settings);
567+
568+
power_manager.reboot();
569+
570+
return 0;
571+
}
572+
573+
static int cmd_battery_info(const struct shell *shell, size_t argc, const char **argv) {
574+
ARG_UNUSED(argc);
575+
ARG_UNUSED(argv);
576+
577+
shell_print(shell, "------------------ Battery Info ------------------");
578+
// Battery fuel gauge status
579+
bat_status status = fuel_gauge.battery_status();
580+
shell_print(shell, "Battery Status:");
581+
shell_print(shell, " Present: %i, Full Charge: %i, Full Discharge: %i",
582+
status.BATTPRES, status.FC, status.FD);
583+
584+
// Basic measurements
585+
shell_print(shell, "Basic Measurements:");
586+
shell_print(shell, " Voltage: %.3f V", fuel_gauge.voltage());
587+
shell_print(shell, " Temperature: %.1f °C", fuel_gauge.temperature());
588+
shell_print(shell, " Current: %.1f mA (avg: %.1f mA)",
589+
fuel_gauge.current(), fuel_gauge.average_current());
590+
shell_print(shell, " State of Charge: %.1f%%", fuel_gauge.state_of_charge());
591+
592+
// Capacity info
593+
shell_print(shell, "Capacity Information:");
594+
shell_print(shell, " Design Capacity: %.1f mAh", fuel_gauge.design_cap());
595+
shell_print(shell, " Full Charge Capacity: %.1f mAh", fuel_gauge.capacity());
596+
shell_print(shell, " Remaining Capacity: %.1f mAh", fuel_gauge.remaining_cap());
597+
598+
// Time estimates
599+
float ttf = fuel_gauge.time_to_full();
600+
float tte = fuel_gauge.time_to_empty();
601+
shell_print(shell, "Time Estimates:");
602+
shell_print(shell, " Time to Full: %ih %02dmin", (int)ttf / 60, (int)ttf % 60);
603+
shell_print(shell, " Time to Empty: %ih %02dmin", (int)tte / 60, (int)tte % 60);
604+
605+
// Battery controller status
606+
battery_controller.exit_high_impedance();
607+
608+
shell_print(shell, "Charging Information:");
609+
uint16_t charging_state = battery_controller.read_charging_state() >> 6;
610+
shell_print(shell, " Charging State: %i", charging_state);
611+
shell_print(shell, " Power Good: %i", battery_controller.power_connected());
612+
613+
struct chrg_state charge_ctrl = battery_controller.read_charging_control();
614+
shell_print(shell, " Charge Control: enabled=%i, current=%.1f mA",
615+
charge_ctrl.enabled, charge_ctrl.mAh);
616+
617+
battery_controller.enter_high_impedance();
618+
619+
return 0;
620+
}
621+
622+
SHELL_STATIC_SUBCMD_SET_CREATE(battery_cmd,
623+
SHELL_COND_CMD(CONFIG_SHELL, info, NULL, "Print battery info", cmd_battery_info),
624+
SHELL_COND_CMD(CONFIG_SHELL, setup, NULL, "Setup fuel gauge", cmd_setup_fuel_gauge),
625+
SHELL_SUBCMD_SET_END);
626+
627+
SHELL_CMD_REGISTER(power_manager, &battery_cmd, "Power Manager Commands", NULL);
628+
561629
PowerManager power_manager;

src/Battery/PowerManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class PowerManager {
7272
};
7373

7474
const struct gpio_dt_spec error_led = GPIO_DT_SPEC_GET(DT_NODELABEL(led_error), gpios);
75+
76+
friend int cmd_setup_fuel_gauge(const struct shell *shell, size_t argc, const char **argv);
7577
};
7678

7779
extern PowerManager power_manager;

unicast_server/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <zephyr/kernel.h>
88
#include <zephyr/device.h>
99
#include <zephyr/devicetree.h>
10+
#include <zephyr/shell/shell.h>
11+
#include <zephyr/shell/shell_uart.h>
1012

1113
//#include "../src/modules/sd_card.h"
1214

0 commit comments

Comments
 (0)