app: convert device status to C struct
DeviceStatus becomes struct device_status with prefixed functions. The error flag helpers are static inline functions in the header. The implementation stays in C++ for now, as it reads data from objects which are not converted yet. The header doesn't include the headers of these objects anymore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3cc24411b5
commit
a428892916
+16
-15
@@ -18,8 +18,9 @@ LOG_MODULE_REGISTER(bat_charger, CONFIG_BAT_LOG_LEVEL);
|
||||
#include "board.h"
|
||||
#include "device_status.h"
|
||||
#include "helper.h"
|
||||
#include "load.h"
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
extern LoadOutput load;
|
||||
|
||||
// DISCHARGE_CURRENT_MAX used to estimate current-compensation of load disconnect voltage
|
||||
@@ -439,16 +440,16 @@ void Charger::discharge_control(BatConf *bat_conf)
|
||||
< dc_bus_src_control_voltage(port->bus, bat_conf->absolute_min_voltage))
|
||||
{
|
||||
port->neg_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_UNDERVOLTAGE);
|
||||
}
|
||||
|
||||
if (bat_temperature > bat_conf->discharge_temp_max) {
|
||||
port->neg_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_DIS_OVERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_DIS_OVERTEMP);
|
||||
}
|
||||
else if (bat_temperature < bat_conf->discharge_temp_min) {
|
||||
port->neg_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_DIS_UNDERTEMP);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -457,17 +458,17 @@ void Charger::discharge_control(BatConf *bat_conf)
|
||||
if (port->bus->voltage
|
||||
>= dc_bus_src_control_voltage(port->bus, bat_conf->absolute_min_voltage + 0.1F))
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_UNDERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_UNDERVOLTAGE);
|
||||
}
|
||||
|
||||
if (bat_temperature < bat_conf->discharge_temp_max - 1
|
||||
&& bat_temperature > bat_conf->discharge_temp_min + 1)
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_DIS_OVERTEMP | ERR_BAT_DIS_UNDERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_DIS_OVERTEMP | ERR_BAT_DIS_UNDERTEMP);
|
||||
}
|
||||
|
||||
if (!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_DIS_OVERTEMP
|
||||
| ERR_BAT_DIS_UNDERTEMP))
|
||||
if (!device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE | ERR_BAT_DIS_OVERTEMP
|
||||
| ERR_BAT_DIS_UNDERTEMP))
|
||||
{
|
||||
// discharge current is stored as absolute value in bat_conf, but defined
|
||||
// as negative current for power port
|
||||
@@ -482,20 +483,20 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
// check battery temperature for charging direction
|
||||
if (bat_temperature > bat_conf->charge_temp_max) {
|
||||
port->pos_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_CHG_OVERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_CHG_OVERTEMP);
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
else if (bat_temperature < bat_conf->charge_temp_min) {
|
||||
port->pos_current_limit = 0;
|
||||
dev_stat.set_error(ERR_BAT_CHG_UNDERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_CHG_UNDERTEMP);
|
||||
enter_state(CHG_STATE_IDLE);
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE)
|
||||
if (device_status_has_error(&dev_stat, ERR_BAT_OVERVOLTAGE)
|
||||
&& port->bus->voltage
|
||||
< (bat_conf->absolute_max_voltage - 0.5F) * port->bus->series_multiplier)
|
||||
{
|
||||
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_OVERVOLTAGE);
|
||||
}
|
||||
|
||||
if (state != CHG_STATE_FOLLOWER && (uptime() - time_last_ctrl_msg) <= 1) {
|
||||
@@ -520,9 +521,9 @@ void Charger::charge_control(BatConf *bat_conf)
|
||||
port->pos_current_limit = bat_conf->charge_current_max;
|
||||
target_current_control = port->pos_current_limit;
|
||||
full = false;
|
||||
dev_stat.clear_error(ERR_BAT_CHG_OVERTEMP);
|
||||
dev_stat.clear_error(ERR_BAT_CHG_UNDERTEMP);
|
||||
dev_stat.clear_error(ERR_BAT_OVERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_CHG_OVERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_CHG_UNDERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_BAT_OVERVOLTAGE);
|
||||
enter_state(CHG_STATE_BULK);
|
||||
}
|
||||
break;
|
||||
|
||||
+6
-5
@@ -205,11 +205,12 @@ void daq_update()
|
||||
}
|
||||
|
||||
if (dev_stat.internal_temp > 80) {
|
||||
dev_stat.set_error(ERR_INT_OVERTEMP);
|
||||
device_status_set_error(&dev_stat, ERR_INT_OVERTEMP);
|
||||
}
|
||||
else if (dev_stat.internal_temp < 70 && (dev_stat.has_error(ERR_INT_OVERTEMP))) {
|
||||
else if (dev_stat.internal_temp < 70 && (device_status_has_error(&dev_stat, ERR_INT_OVERTEMP)))
|
||||
{
|
||||
// remove error flag with hysteresis of 10°C
|
||||
dev_stat.clear_error(ERR_INT_OVERTEMP);
|
||||
device_status_clear_error(&dev_stat, ERR_INT_OVERTEMP);
|
||||
}
|
||||
// else: keep previous setting
|
||||
}
|
||||
@@ -226,7 +227,7 @@ static void lv_overvoltage_alert(const struct device *dev, int channel)
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
|
||||
dev_stat.set_error(ERR_BAT_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_BAT_OVERVOLTAGE);
|
||||
|
||||
LOG_ERR("Low-side overvoltage alert");
|
||||
}
|
||||
@@ -258,7 +259,7 @@ static void hv_overvoltage_alert(const struct device *dev, int channel)
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
charger.state = CHG_STATE_IDLE;
|
||||
|
||||
dev_stat.set_error(ERR_DCDC_HS_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_DCDC_HS_OVERVOLTAGE);
|
||||
|
||||
LOG_ERR("High-side overvoltage alert");
|
||||
}
|
||||
|
||||
+5
-5
@@ -39,7 +39,7 @@ LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
|
||||
#define HV_OUT_NODE DT_CHILD(DT_PATH(outputs), hv_out)
|
||||
#endif
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
|
||||
Dcdc::Dcdc(const struct device *hb, struct dc_bus *high, struct dc_bus *low,
|
||||
DcdcOperationMode op_mode)
|
||||
@@ -164,7 +164,7 @@ __weak DcdcOperationMode Dcdc::check_start_conditions()
|
||||
if (enable == false
|
||||
|| hvb->voltage > hs_voltage_max // also critical for buck mode because of ringing
|
||||
|| lvb->voltage > ls_voltage_max || lvb->voltage < ls_voltage_min
|
||||
|| dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|
||||
|| device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|
||||
|| (int)uptime() < (off_timestamp + (int)restart_interval))
|
||||
{
|
||||
return DCDC_MODE_OFF;
|
||||
@@ -202,14 +202,14 @@ bool Dcdc::check_hs_mosfet_short()
|
||||
}
|
||||
else if (now - first_time_detected > 10) {
|
||||
// waited approx 10s before setting the flag
|
||||
dev_stat.set_error(ERR_DCDC_HS_MOSFET_SHORT);
|
||||
device_status_set_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
}
|
||||
else {
|
||||
first_time_detected = 0;
|
||||
}
|
||||
|
||||
return dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT);
|
||||
return device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
|
||||
bool Dcdc::startup_inhibit(bool reset)
|
||||
@@ -227,7 +227,7 @@ bool Dcdc::startup_inhibit(bool reset)
|
||||
|
||||
__weak void Dcdc::control()
|
||||
{
|
||||
if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
// DC/DC must not be operated anymore and the low-side switch might be forced on to blow
|
||||
// the fuse, so don't touch the half bridge
|
||||
return;
|
||||
|
||||
+56
-48
@@ -6,6 +6,11 @@
|
||||
|
||||
#include "device_status.h"
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "dcdc.h"
|
||||
#include "load.h"
|
||||
#include "power_port.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
@@ -16,7 +21,7 @@
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
void DeviceStatus::update_energy()
|
||||
void device_status_update_energy(struct device_status *ds)
|
||||
{
|
||||
// static variables so that it is not reset for each function call
|
||||
static int seconds_zero_solar = 0;
|
||||
@@ -35,13 +40,13 @@ void DeviceStatus::update_energy()
|
||||
static bool first_call = true;
|
||||
if (first_call) {
|
||||
// initialize values with values we got from EEPROM
|
||||
solar_in_total_Wh_prev = solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = bat_dis_total_Wh;
|
||||
solar_in_total_Wh_prev = ds->solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = ds->load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = ds->bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = ds->bat_dis_total_Wh;
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_import_total_Wh_prev = grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = grid_export_total_Wh;
|
||||
grid_import_total_Wh_prev = ds->grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = ds->grid_export_total_Wh;
|
||||
#endif
|
||||
first_call = false;
|
||||
}
|
||||
@@ -58,14 +63,14 @@ void DeviceStatus::update_energy()
|
||||
// solar voltage > battery voltage after 5 hours of night time means sunrise in the morning
|
||||
// --> reset daily energy counters
|
||||
if (seconds_zero_solar > 60 * 60 * 5) {
|
||||
day_counter++;
|
||||
solar_in_total_Wh_prev = solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = bat_dis_total_Wh;
|
||||
ds->day_counter++;
|
||||
solar_in_total_Wh_prev = ds->solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = ds->load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = ds->bat_chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = ds->bat_dis_total_Wh;
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_import_total_Wh_prev = grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = grid_export_total_Wh;
|
||||
grid_import_total_Wh_prev = ds->grid_import_total_Wh;
|
||||
grid_export_total_Wh_prev = ds->grid_export_total_Wh;
|
||||
#endif
|
||||
solar_terminal.neg_energy_Wh = 0.0;
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
@@ -82,83 +87,86 @@ void DeviceStatus::update_energy()
|
||||
}
|
||||
#endif
|
||||
|
||||
bat_chg_total_Wh =
|
||||
ds->bat_chg_total_Wh =
|
||||
bat_chg_total_Wh_prev + (bat_terminal.pos_energy_Wh > 0 ? bat_terminal.pos_energy_Wh : 0);
|
||||
bat_dis_total_Wh =
|
||||
ds->bat_dis_total_Wh =
|
||||
bat_dis_total_Wh_prev + (bat_terminal.neg_energy_Wh > 0 ? bat_terminal.neg_energy_Wh : 0);
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
solar_in_total_Wh = solar_in_total_Wh_prev
|
||||
+ (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0);
|
||||
ds->solar_in_total_Wh = solar_in_total_Wh_prev
|
||||
+ (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0);
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
load_out_total_Wh = load_out_total_Wh_prev + (load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0);
|
||||
ds->load_out_total_Wh =
|
||||
load_out_total_Wh_prev + (load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0);
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_import_total_Wh = grid_import_total_Wh_prev
|
||||
+ (grid_terminal.neg_energy_Wh > 0 ? grid_terminal.neg_energy_Wh : 0);
|
||||
grid_export_total_Wh = grid_export_total_Wh_prev
|
||||
+ (grid_terminal.pos_energy_Wh > 0 ? grid_terminal.pos_energy_Wh : 0);
|
||||
ds->grid_import_total_Wh =
|
||||
grid_import_total_Wh_prev
|
||||
+ (grid_terminal.neg_energy_Wh > 0 ? grid_terminal.neg_energy_Wh : 0);
|
||||
ds->grid_export_total_Wh =
|
||||
grid_export_total_Wh_prev
|
||||
+ (grid_terminal.pos_energy_Wh > 0 ? grid_terminal.pos_energy_Wh : 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void DeviceStatus::update_min_max_values()
|
||||
void device_status_update_min_max_values(struct device_status *ds)
|
||||
{
|
||||
if (bat_terminal.bus->voltage > battery_voltage_max) {
|
||||
battery_voltage_max = bat_terminal.bus->voltage;
|
||||
if (bat_terminal.bus->voltage > ds->battery_voltage_max) {
|
||||
ds->battery_voltage_max = bat_terminal.bus->voltage;
|
||||
}
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR
|
||||
if (solar_terminal.bus->voltage > solar_voltage_max) {
|
||||
solar_voltage_max = solar_terminal.bus->voltage;
|
||||
if (solar_terminal.bus->voltage > ds->solar_voltage_max) {
|
||||
ds->solar_voltage_max = solar_terminal.bus->voltage;
|
||||
}
|
||||
#elif CONFIG_PWM_TERMINAL_SOLAR
|
||||
if (pwm_switch.ext_voltage > solar_voltage_max) {
|
||||
solar_voltage_max = pwm_switch.ext_voltage;
|
||||
if (pwm_switch.ext_voltage > ds->solar_voltage_max) {
|
||||
ds->solar_voltage_max = pwm_switch.ext_voltage;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_DCDC
|
||||
if (dcdc.inductor_current > dcdc_current_max) {
|
||||
dcdc_current_max = dcdc.inductor_current;
|
||||
if (dcdc.inductor_current > ds->dcdc_current_max) {
|
||||
ds->dcdc_current_max = dcdc.inductor_current;
|
||||
}
|
||||
|
||||
if (dcdc.temp_mosfets > mosfet_temp_max) {
|
||||
mosfet_temp_max = dcdc.temp_mosfets;
|
||||
if (dcdc.temp_mosfets > ds->mosfet_temp_max) {
|
||||
ds->mosfet_temp_max = dcdc.temp_mosfets;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.current > load_current_max) {
|
||||
load_current_max = load.current;
|
||||
if (load.current > ds->load_current_max) {
|
||||
ds->load_current_max = load.current;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
if (-solar_terminal.power > solar_power_max_day) {
|
||||
solar_power_max_day = -solar_terminal.power;
|
||||
if (solar_power_max_day > solar_power_max_total) {
|
||||
solar_power_max_total = solar_power_max_day;
|
||||
if (-solar_terminal.power > ds->solar_power_max_day) {
|
||||
ds->solar_power_max_day = -solar_terminal.power;
|
||||
if (ds->solar_power_max_day > ds->solar_power_max_total) {
|
||||
ds->solar_power_max_total = ds->solar_power_max_day;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BOARD_HAS_LOAD_OUTPUT
|
||||
if (load.power > load_power_max_day) {
|
||||
load_power_max_day = load.power;
|
||||
if (load_power_max_day > load_power_max_total) {
|
||||
load_power_max_total = load_power_max_day;
|
||||
if (load.power > ds->load_power_max_day) {
|
||||
ds->load_power_max_day = load.power;
|
||||
if (ds->load_power_max_day > ds->load_power_max_total) {
|
||||
ds->load_power_max_total = ds->load_power_max_day;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (charger.bat_temperature > static_cast<float>(bat_temp_max)) {
|
||||
bat_temp_max = charger.bat_temperature;
|
||||
if (charger.bat_temperature > static_cast<float>(ds->bat_temp_max)) {
|
||||
ds->bat_temp_max = charger.bat_temperature;
|
||||
}
|
||||
|
||||
if (internal_temp > static_cast<float>(int_temp_max)) {
|
||||
int_temp_max = internal_temp;
|
||||
if (ds->internal_temp > static_cast<float>(ds->int_temp_max)) {
|
||||
ds->int_temp_max = ds->internal_temp;
|
||||
}
|
||||
}
|
||||
|
||||
+60
-42
@@ -13,13 +13,13 @@
|
||||
* Device-level data storage and functions (like max/min values, error flags, etc.)
|
||||
*/
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "dcdc.h"
|
||||
#include "load.h"
|
||||
#include "power_port.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** Error Flags
|
||||
*
|
||||
* When adding new flags, please make sure to use only up to 32 errors
|
||||
@@ -110,17 +110,8 @@ enum ErrorFlag
|
||||
*
|
||||
* Stores error counters and some maximum ever measured values to EEPROM
|
||||
*/
|
||||
class DeviceStatus
|
||||
struct device_status
|
||||
{
|
||||
public:
|
||||
/** Updates the total energy counters for solar, battery and load bus
|
||||
*/
|
||||
void update_energy();
|
||||
|
||||
/** Updates the logged min/max values for voltages, power, temperatures etc.
|
||||
*/
|
||||
void update_min_max_values();
|
||||
|
||||
// total energy
|
||||
uint32_t bat_chg_total_Wh;
|
||||
uint32_t bat_dis_total_Wh;
|
||||
@@ -149,34 +140,61 @@ public:
|
||||
// instantaneous device-level data
|
||||
uint32_t error_flags; ///< Currently detected errors
|
||||
float internal_temp; ///< Internal temperature (measured in MCU)
|
||||
|
||||
/**
|
||||
* @brief sets one or more error flags in device state
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
inline void set_error(uint32_t e)
|
||||
{
|
||||
error_flags |= e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief clears one or more error flags in device state
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
void clear_error(uint32_t e)
|
||||
{
|
||||
error_flags &= ~e;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief queries one or more error flags in device state
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
* @return true if any of the error flags given in e are set in device state
|
||||
*/
|
||||
bool has_error(uint32_t e)
|
||||
{
|
||||
return (error_flags & e) != 0;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the total energy counters for solar, battery and load bus
|
||||
*
|
||||
* Must be called exactly once per second, otherwise energy calculation gets wrong.
|
||||
*
|
||||
* @param ds Device status
|
||||
*/
|
||||
void device_status_update_energy(struct device_status *ds);
|
||||
|
||||
/**
|
||||
* Updates the logged min/max values for voltages, power, temperatures etc.
|
||||
*
|
||||
* @param ds Device status
|
||||
*/
|
||||
void device_status_update_min_max_values(struct device_status *ds);
|
||||
|
||||
/**
|
||||
* Sets one or more error flags in device state
|
||||
*
|
||||
* @param ds Device status
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
static inline void device_status_set_error(struct device_status *ds, uint32_t e)
|
||||
{
|
||||
ds->error_flags |= e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears one or more error flags in device state
|
||||
*
|
||||
* @param ds Device status
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*/
|
||||
static inline void device_status_clear_error(struct device_status *ds, uint32_t e)
|
||||
{
|
||||
ds->error_flags &= ~e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries one or more error flags in device state
|
||||
*
|
||||
* @param ds Device status
|
||||
* @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY
|
||||
*
|
||||
* @return true if any of the error flags given in e are set in device state
|
||||
*/
|
||||
static inline bool device_status_has_error(const struct device_status *ds, uint32_t e)
|
||||
{
|
||||
return (ds->error_flags & e) != 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DEVICE_STATUS_H */
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ LOG_MODULE_REGISTER(load, CONFIG_LOAD_LOG_LEVEL);
|
||||
#define PCB_MOSFETS_TAU_JA DT_PROP(DT_PATH(pcb), mosfets_tau_ja)
|
||||
#define PCB_INTERNAL_TREF_MAX DT_PROP(DT_PATH(pcb), internal_tref_max)
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
|
||||
LoadOutput::LoadOutput(struct dc_bus *dc_bus, const struct device *sw, int led_pos)
|
||||
: power_switch(sw), led(led_pos)
|
||||
|
||||
+3
-3
@@ -106,12 +106,12 @@ int main(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
dev_stat.update_energy();
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_energy(&dev_stat);
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
charger.update_soc(&bat_conf);
|
||||
|
||||
#if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && BOARD_HAS_DCDC
|
||||
if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
if (device_status_has_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT)) {
|
||||
dcdc.fuse_destruction();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -97,7 +97,7 @@ void PwmSwitch::control()
|
||||
else if (bus->voltage > dc_bus_sink_control_voltage(bus, 0) + 0.3F) {
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
dev_stat.set_error(ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
LOG_INF("PWM charger stop, overvoltage.");
|
||||
}
|
||||
else if (bus->voltage > dc_bus_sink_control_voltage(bus, 0) // bus voltage above target
|
||||
@@ -117,7 +117,7 @@ void PwmSwitch::control()
|
||||
power_switch_off(power_switch);
|
||||
off_timestamp = uptime();
|
||||
// consider this as overvoltage in order to start again with 5% duty cycle
|
||||
dev_stat.set_error(ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
device_status_set_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
LOG_INF("PWM charger stop, no further derating possible.");
|
||||
}
|
||||
else {
|
||||
@@ -138,10 +138,10 @@ void PwmSwitch::control()
|
||||
}
|
||||
}
|
||||
|
||||
if (dev_stat.has_error(ERR_PWM_SWITCH_OVERVOLTAGE)
|
||||
if (device_status_has_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE)
|
||||
&& bus->voltage < dc_bus_sink_control_voltage(bus, 0) - 0.5F)
|
||||
{
|
||||
dev_stat.clear_error(ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
device_status_clear_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -153,7 +153,7 @@ void PwmSwitch::control()
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 50 ms
|
||||
daq_inhibit_alert(DAQ_DEV, DAQ_CH(v_low), DAQ_ALERT_UPPER, 50);
|
||||
|
||||
if (dev_stat.has_error(ERR_PWM_SWITCH_OVERVOLTAGE)) {
|
||||
if (device_status_has_error(&dev_stat, ERR_PWM_SWITCH_OVERVOLTAGE)) {
|
||||
// start with minimum duty cycle in order to prevent another overvoltage event
|
||||
power_switch_set_duty_cycle(power_switch, 0.05F);
|
||||
power_switch_on(power_switch);
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@ Charger charger(&bat_terminal);
|
||||
BatConf bat_conf; // actual (used) battery configuration
|
||||
BatConf bat_conf_user; // temporary storage where the user can write to
|
||||
|
||||
DeviceStatus dev_stat;
|
||||
struct device_status dev_stat;
|
||||
|
||||
// current unix timestamp (independent of time(NULL), as it is user-configurable)
|
||||
// uint32_t considered large enough, so we avoid 64-bit math (overflow in year 2106)
|
||||
|
||||
+2
-1
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "board.h"
|
||||
#include "dcdc.h"
|
||||
#include "device_status.h"
|
||||
#include "load.h"
|
||||
#include "power_port.h"
|
||||
@@ -33,7 +34,7 @@ extern struct power_port &bat_terminal;
|
||||
extern struct power_port &solar_terminal;
|
||||
extern struct power_port &grid_terminal;
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
extern struct device_status dev_stat;
|
||||
extern Charger charger;
|
||||
extern BatConf bat_conf;
|
||||
extern BatConf bat_conf_user;
|
||||
|
||||
+10
-10
@@ -43,7 +43,7 @@ void check_temperature_readings()
|
||||
|
||||
ZTEST(daq, test_adc_alert_lv_undervoltage_triggering)
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
device_status_clear_error(&dev_stat, ERR_ANY_ERROR);
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
daq_set_lv_limits(bat_conf.absolute_max_voltage, bat_conf.absolute_min_voltage);
|
||||
prepare_adc_filtered();
|
||||
@@ -53,7 +53,7 @@ ZTEST(daq, test_adc_alert_lv_undervoltage_triggering)
|
||||
adcval.battery_voltage = bat_conf.absolute_min_voltage - 0.1f;
|
||||
prepare_adc_readings(adcval);
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
zassert_equal(dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP), false);
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_LOAD_VOLTAGE_DIP), false);
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
load.control();
|
||||
zassert_equal(flags_check(&load.error_flags, ERR_LOAD_VOLTAGE_DIP), true);
|
||||
@@ -66,12 +66,12 @@ ZTEST(daq, test_adc_alert_lv_undervoltage_triggering)
|
||||
daq_update();
|
||||
|
||||
charger.discharge_control(&bat_conf);
|
||||
zassert_equal(dev_stat.has_error(ERR_BAT_UNDERVOLTAGE), false); // ToDo
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_BAT_UNDERVOLTAGE), false); // ToDo
|
||||
}
|
||||
|
||||
ZTEST(daq, test_adc_alert_lv_overvoltage_triggering)
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
device_status_clear_error(&dev_stat, ERR_ANY_ERROR);
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
daq_set_lv_limits(bat_conf.absolute_max_voltage, bat_conf.absolute_min_voltage);
|
||||
prepare_adc_filtered();
|
||||
@@ -83,9 +83,9 @@ ZTEST(daq, test_adc_alert_lv_overvoltage_triggering)
|
||||
adcval.battery_voltage = bat_conf.absolute_max_voltage + 0.1f;
|
||||
prepare_adc_readings(adcval);
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
zassert_equal(dev_stat.has_error(ERR_BAT_OVERVOLTAGE), false);
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_BAT_OVERVOLTAGE), false);
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_low));
|
||||
zassert_equal(dev_stat.has_error(ERR_BAT_OVERVOLTAGE), true);
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_BAT_OVERVOLTAGE), true);
|
||||
// zassert_equal(pwm_switch.active(), false);
|
||||
zassert_equal(dcdc.state, DCDC_CONTROL_OFF);
|
||||
|
||||
@@ -97,12 +97,12 @@ ZTEST(daq, test_adc_alert_lv_overvoltage_triggering)
|
||||
|
||||
charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
|
||||
charger.charge_control(&bat_conf);
|
||||
zassert_equal(dev_stat.has_error(ERR_BAT_OVERVOLTAGE), false);
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_BAT_OVERVOLTAGE), false);
|
||||
}
|
||||
|
||||
ZTEST(daq, test_adc_alert_hv_overvoltage_triggering)
|
||||
{
|
||||
dev_stat.clear_error(ERR_ANY_ERROR);
|
||||
device_status_clear_error(&dev_stat, ERR_ANY_ERROR);
|
||||
daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max));
|
||||
prepare_adc_filtered();
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_high));
|
||||
@@ -111,9 +111,9 @@ ZTEST(daq, test_adc_alert_hv_overvoltage_triggering)
|
||||
adcval.solar_voltage = 85.0F;
|
||||
prepare_adc_readings(adcval);
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_high));
|
||||
zassert_equal(dev_stat.has_error(ERR_DCDC_HS_OVERVOLTAGE), false);
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_DCDC_HS_OVERVOLTAGE), false);
|
||||
daq_emul_process(DAQ_DEV, DAQ_CH(v_high));
|
||||
zassert_equal(dev_stat.has_error(ERR_DCDC_HS_OVERVOLTAGE), true);
|
||||
zassert_equal(device_status_has_error(&dev_stat, ERR_DCDC_HS_OVERVOLTAGE), true);
|
||||
zassert_equal(dcdc.state, DCDC_CONTROL_OFF);
|
||||
}
|
||||
|
||||
|
||||
@@ -420,7 +420,7 @@ ZTEST(dcdc, test_boost_correct_mppt_operation)
|
||||
ZTEST(dcdc, test_fuse_destruction_forces_low_side_on)
|
||||
{
|
||||
start_buck();
|
||||
dev_stat.set_error(ERR_DCDC_HS_MOSFET_SHORT);
|
||||
device_status_set_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
|
||||
|
||||
// fuse destruction is delayed by 21 calls (seconds) to be able to send out data
|
||||
for (int i = 0; i < 21; i++) {
|
||||
@@ -444,7 +444,7 @@ ZTEST(dcdc, test_fuse_destruction_forces_low_side_on)
|
||||
zassert_equal(half_bridge_get_compare(dcdc.half_bridge), 0);
|
||||
|
||||
dcdc.stop();
|
||||
dev_stat.clear_error(ERR_DCDC_HS_MOSFET_SHORT);
|
||||
device_status_clear_error(&dev_stat, ERR_DCDC_HS_MOSFET_SHORT);
|
||||
}
|
||||
|
||||
ZTEST_SUITE(dcdc, NULL, NULL, NULL, NULL, NULL);
|
||||
|
||||
@@ -25,7 +25,7 @@ ZTEST(device_status, test_reset_counters_at_start_of_day)
|
||||
|
||||
// 5 hours without sun
|
||||
for (int i = 0; i <= 5 * 60 * 60; i++) {
|
||||
dev_stat.update_energy();
|
||||
device_status_update_energy(&dev_stat);
|
||||
}
|
||||
|
||||
zassert_equal(dev_stat.solar_in_total_Wh, 10);
|
||||
@@ -43,7 +43,7 @@ ZTEST(device_status, test_reset_counters_at_start_of_day)
|
||||
|
||||
// now solar power comes back
|
||||
solar_terminal.bus->voltage = bat_terminal.bus->voltage + 1;
|
||||
dev_stat.update_energy();
|
||||
device_status_update_energy(&dev_stat);
|
||||
|
||||
// day counter should be increased and daily energy counters reset
|
||||
zassert_equal(dev_stat.day_counter, 1);
|
||||
@@ -56,35 +56,35 @@ ZTEST(device_status, test_reset_counters_at_start_of_day)
|
||||
ZTEST(device_status, test_dev_stat_new_solar_voltage_max)
|
||||
{
|
||||
solar_terminal.bus->voltage = 40;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.solar_voltage_max, 40);
|
||||
}
|
||||
|
||||
ZTEST(device_status, test_dev_stat_new_bat_voltage_max)
|
||||
{
|
||||
bat_terminal.bus->voltage = 31;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.battery_voltage_max, 31);
|
||||
}
|
||||
|
||||
ZTEST(device_status, test_dev_stat_new_dcdc_current_max)
|
||||
{
|
||||
dcdc.inductor_current = 21;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.dcdc_current_max, 21);
|
||||
}
|
||||
|
||||
ZTEST(device_status, test_dev_stat_new_load_current_max)
|
||||
{
|
||||
load.current = 21;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.load_current_max, 21);
|
||||
}
|
||||
|
||||
ZTEST(device_status, test_dev_stat_solar_power_max)
|
||||
{
|
||||
solar_terminal.power = -50;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.solar_power_max_day, 50);
|
||||
zassert_equal(dev_stat.solar_power_max_total, 50);
|
||||
}
|
||||
@@ -92,7 +92,7 @@ ZTEST(device_status, test_dev_stat_solar_power_max)
|
||||
ZTEST(device_status, test_dev_stat_load_power_max)
|
||||
{
|
||||
load.power = 50;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.load_power_max_day, 50);
|
||||
zassert_equal(dev_stat.load_power_max_total, 50);
|
||||
}
|
||||
@@ -100,14 +100,14 @@ ZTEST(device_status, test_dev_stat_load_power_max)
|
||||
ZTEST(device_status, test_dev_stat_new_mosfet_temp_max)
|
||||
{
|
||||
dcdc.temp_mosfets = 80;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.mosfet_temp_max, 80);
|
||||
}
|
||||
|
||||
ZTEST(device_status, test_dev_stat_new_bat_temp_max)
|
||||
{
|
||||
charger.bat_temperature = 45;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.bat_temp_max, 45);
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ ZTEST(device_status, test_dev_stat_new_int_temp_max)
|
||||
{
|
||||
dev_stat.int_temp_max = 20;
|
||||
dev_stat.internal_temp = 22;
|
||||
dev_stat.update_min_max_values();
|
||||
device_status_update_min_max_values(&dev_stat);
|
||||
zassert_equal(dev_stat.int_temp_max, 22);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user