Separate load and USB out, improve state handling

Identified issues:
- The load output and USB charging port shared the same state machine,
  which did not allow to set different switch-off thresholds.
- Load error codes were global, so it was not possible to have multiple
  load instances.

This commit introduces load-specific error_flags, makes the LoadOutput
class inherit from PowerPort (to improve code readability), separates
the load driver better from higher level implementation and adds a
second LoadOutput object for the USB charging port.
This commit is contained in:
Martin Jäger
2020-02-21 18:27:36 +01:00
parent 3c0bc41164
commit fa9d15e796
18 changed files with 341 additions and 474 deletions
+6 -6
View File
@@ -165,15 +165,15 @@ void daq_update()
adc_scaled(ADC_POS_V_PWM, vcc, ADC_GAIN_V_PWM);
#endif
load_terminal.current =
adc_scaled(ADC_POS_I_LOAD, vcc, ADC_GAIN_I_LOAD) + load_current_offset;
load.current = adc_scaled(ADC_POS_I_LOAD, vcc, ADC_GAIN_I_LOAD) + load_current_offset;
#if CONFIG_HAS_PWM_SWITCH
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power
// calculation
pwm_switch.current = -pwm_switch.get_duty_cycle() * (
adc_scaled(ADC_POS_I_PWM, vcc, ADC_GAIN_I_PWM) + solar_current_offset);
lv_terminal.current = -pwm_switch.current - load_terminal.current;
lv_terminal.current = -pwm_switch.current - load.current;
pwm_switch.power = pwm_switch.bus->voltage * pwm_switch.current;
#endif
@@ -182,7 +182,7 @@ void daq_update()
dcdc_lv_port.current =
adc_scaled(ADC_POS_I_DCDC, vcc, ADC_GAIN_I_DCDC) + solar_current_offset;
lv_terminal.current = dcdc_lv_port.current - load_terminal.current;
lv_terminal.current = dcdc_lv_port.current - load.current;
hv_terminal.current = -dcdc_lv_port.current * lv_terminal.bus->voltage / hv_terminal.bus->voltage;
@@ -190,7 +190,7 @@ void daq_update()
hv_terminal.power = hv_terminal.bus->voltage * hv_terminal.current;
#endif
lv_terminal.power = lv_terminal.bus->voltage * lv_terminal.current;
load_terminal.power = load_terminal.bus->voltage * load_terminal.current;
load.power = load.bus->voltage * load.current;
#ifdef PIN_ADC_TEMP_BAT
// battery temperature calculation
+9 -6
View File
@@ -152,7 +152,7 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_BOOL(0x40, "LoadEnDefault", &load.enable,
TS_CONF, TS_READ_ALL | TS_WRITE_ALL),
TS_DATA_OBJ_BOOL(0x41, "UsbEnDefault", &load.usb_enable,
TS_DATA_OBJ_BOOL(0x41, "UsbEnDefault", &usb_pwr.enable,
TS_CONF, TS_READ_ALL | TS_WRITE_ALL),
TS_DATA_OBJ_FLOAT(0x42, "LoadDisconnect_V", &bat_conf_user.voltage_load_disconnect, 2,
@@ -179,7 +179,7 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_BOOL(0x60, "LoadEn", &load.enable,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
TS_DATA_OBJ_BOOL(0x61, "UsbEn", &load.usb_enable,
TS_DATA_OBJ_BOOL(0x61, "UsbEn", &usb_pwr.enable,
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
#if CONFIG_HAS_DCDC_CONVERTER
@@ -207,7 +207,7 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_UINT32(0x04, "LoadState", &load.state,
TS_OUTPUT, TS_READ_ALL),
TS_DATA_OBJ_UINT32(0x05, "UsbState", &load.usb_state,
TS_DATA_OBJ_UINT32(0x05, "UsbState", &usb_pwr.state,
TS_OUTPUT, TS_READ_ALL),
TS_DATA_OBJ_UINT16(0x06, "SOC_%", &charger.soc, // output will be uint8_t
@@ -225,7 +225,7 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_FLOAT(0x72, "Bat_A", &bat_terminal.current, 2,
TS_OUTPUT, TS_READ_ALL),
TS_DATA_OBJ_FLOAT(0x73, "Load_A", &load_terminal.current, 2,
TS_DATA_OBJ_FLOAT(0x73, "Load_A", &load.current, 2,
TS_OUTPUT, TS_READ_ALL),
TS_DATA_OBJ_FLOAT(0x74, "Bat_degC", &charger.bat_temperature, 1,
@@ -269,7 +269,7 @@ const data_object_t data_objects[] = {
TS_OUTPUT, TS_READ_ALL),
#endif
TS_DATA_OBJ_FLOAT(0x7F, "Load_W", &load_terminal.power, 2,
TS_DATA_OBJ_FLOAT(0x7F, "Load_W", &load.power, 2,
TS_OUTPUT, TS_READ_ALL),
#if CONFIG_HV_TERMINAL_NANOGRID
@@ -280,6 +280,9 @@ const data_object_t data_objects[] = {
TS_DATA_OBJ_UINT32(0x90, "ErrorFlags", &dev_stat.error_flags,
TS_OUTPUT, TS_READ_ALL),
TS_DATA_OBJ_UINT32(0x91, "LoadErrorFlags", &load.error_flags,
TS_OUTPUT, TS_READ_ALL),
// RECORDED DATA ///////////////////////////////////////////////////////
// using IDs >= 0xA0
@@ -316,7 +319,7 @@ const data_object_t data_objects[] = {
TS_REC, TS_READ_ALL | TS_WRITE_MAKER),
#endif
TS_DATA_OBJ_FLOAT(0xA1, "LoadOutDay_Wh", &load_terminal.pos_energy_Wh, 2,
TS_DATA_OBJ_FLOAT(0xA1, "LoadOutDay_Wh", &load.pos_energy_Wh, 2,
TS_REC, TS_READ_ALL | TS_WRITE_MAKER),
TS_DATA_OBJ_FLOAT(0xA2, "BatChgDay_Wh", &bat_terminal.pos_energy_Wh, 2,
+6 -6
View File
@@ -53,7 +53,7 @@ void DeviceStatus::update_energy()
bat_chg_total_Wh_prev = bat_chg_total_Wh;
bat_dis_total_Wh_prev = bat_dis_total_Wh;
solar_terminal.neg_energy_Wh = 0.0;
load_terminal.pos_energy_Wh = 0.0;
load.pos_energy_Wh = 0.0;
bat_terminal.pos_energy_Wh = 0.0;
bat_terminal.neg_energy_Wh = 0.0;
}
@@ -70,7 +70,7 @@ void DeviceStatus::update_energy()
(solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0);
#endif
load_out_total_Wh = load_out_total_Wh_prev +
(load_terminal.pos_energy_Wh > 0 ? load_terminal.pos_energy_Wh : 0);
(load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0);
}
void DeviceStatus::update_min_max_values()
@@ -95,8 +95,8 @@ void DeviceStatus::update_min_max_values()
}
#endif
if (load_terminal.current > load_current_max) {
load_current_max = load_terminal.current;
if (load.current > load_current_max) {
load_current_max = load.current;
}
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
@@ -108,8 +108,8 @@ void DeviceStatus::update_min_max_values()
}
#endif
if (load_terminal.power > load_power_max_day) {
load_power_max_day = load_terminal.power;
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;
}
-38
View File
@@ -77,38 +77,6 @@ enum ErrorFlag {
*/
ERR_BAT_CHG_OVERTEMP = 1U << 7,
/** SOC too low so that load was switched off
*
* Set in LoadOutput::control() in case of ERR_BAT_UNDERVOLTAGE, cleared after reconnect
* delay passed and undervoltage error is resolved.
*/
ERR_LOAD_LOW_SOC = 1U << 8,
/** To high voltage for load so that it was switched off
*
* Set and cleared in LoadOutput::control()
*/
ERR_LOAD_OVERVOLTAGE = 1U << 9,
/** Long-term overcurrent of load port
*
* Set in LoadOutput::control() and cleared after configurable delay.
*/
ERR_LOAD_OVERCURRENT = 1U << 10,
/** Short circuit detected at load port
*
* Set by LoadOutput::control() after overcurrent comparator triggered, cleared only if
* load output is manually disabled and enabled again.
*/
ERR_LOAD_SHORT_CIRCUIT = 1U << 11,
/** Overcurrent identified via voltage dip (may be caused by too small battery)
*
* Set and cleared in LoadOutput::control(). Treated same as load overcurrent.
*/
ERR_LOAD_VOLTAGE_DIP = 1U << 12,
/** Charge controller internal temperature too high
*
* Set and cleared by daq_update()
@@ -126,12 +94,6 @@ enum ErrorFlag {
ERR_ANY_ERROR = UINT32_MAX,
};
/** Error flags that require load to be switched off
*/
const uint32_t ERR_LOAD_ANY = ERR_BAT_DIS_OVERTEMP | ERR_BAT_DIS_UNDERTEMP |
ERR_LOAD_LOW_SOC | ERR_LOAD_OVERVOLTAGE | ERR_LOAD_OVERCURRENT |
ERR_LOAD_SHORT_CIRCUIT | ERR_LOAD_VOLTAGE_DIP | ERR_INT_OVERTEMP;
/** Device Status data
*
* Stores error counters and some maximum ever measured values to EEPROM
+4 -4
View File
@@ -107,7 +107,7 @@ void ExtOled::process_1s()
oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1);
}
if (load.pgood) {
if (load.state == LOAD_STATE_ON) {
oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1);
}
else {
@@ -170,16 +170,16 @@ void ExtOled::process_1s()
// load data
oled.setTextCursor(90, 18);
len = snprintf(buf, sizeof(buf), "%5.1fW",
(abs(load_terminal.power) < 0.1) ? 0 : load_terminal.power); // remove negative zeros
(abs(load.power) < 0.1) ? 0 : load.power); // remove negative zeros
oled.writeString(buf, len);
oled.setTextCursor(90, 26);
len = snprintf(buf, sizeof(buf), "%5.1fA\n",
(abs(load_terminal.current) < 0.1) ? 0 : load_terminal.current);
(abs(load.current) < 0.1) ? 0 : load.current);
oled.writeString(buf, len);
oled.setTextCursor(0, 36);
len = snprintf(buf, sizeof(buf), "Day +%5.0fWh -%5.0fWh",
in_terminal.neg_energy_Wh, fabs(load_terminal.pos_energy_Wh));
in_terminal.neg_energy_Wh, fabs(load.pos_energy_Wh));
oled.writeString(buf, len);
len = snprintf(buf, sizeof(buf), "Tot +%4.1fkWh -%4.1fkWh",
dev_stat.solar_in_total_Wh / 1000.0, fabs(dev_stat.load_out_total_Wh) / 1000.0);
+3 -4
View File
@@ -11,6 +11,7 @@
#include "load.h"
#include "half_bridge.h"
#include "leds.h"
#include "setup.h"
#if defined(STM32F0)
#define SYS_MEM_START 0x1FFFC800
@@ -27,8 +28,6 @@
#include "mbed.h"
extern LoadOutput load;
#if defined(STM32F0)
void control_timer_start(int freq_Hz) // max. 10 kHz
@@ -156,8 +155,8 @@ void init_watchdog(float timeout) {
void mbed_die(void)
{
half_bridge_stop();
load.switch_set(false);
load.usb_set(false);
load.stop();
usb_pwr.stop();
leds_init(false);
+25
View File
@@ -34,6 +34,31 @@ float interpolate(const float a[], const float b[], size_t size, float value_a);
*/
uint32_t uptime();
/**
* Sets one or more flags in given field
*
* @param field pointer to the field that will be manipulated
* @param mask a single flag or bitwise OR-ed flags
*/
inline void flags_set(uint32_t *field, uint32_t mask) { *field |= mask; }
/**
* Clears one or more flags in the bit field
*
* @param field pointer to the field that will be manipulated
* @param mask a single flag or bitwise OR-ed flags
*/
inline void flags_clear(uint32_t *field, uint32_t mask) { *field &= ~mask; }
/**
* Queries one or more flags in the bit field
*
* @param field pointer to the field that will be manipulated
* @param mask a single flag or bitwise OR-ed
* @returns true if any of the flags given in mask are set in the bit field
*/
inline bool flags_check(uint32_t *field, uint32_t mask) { return (*field & mask) != 0; }
#ifdef __cplusplus
}
#endif
+43 -125
View File
@@ -8,7 +8,6 @@
#include "pcb.h"
#include "config.h"
#include "hardware.h"
#include "leds.h"
#include "device_status.h"
#include "debug.h"
@@ -16,200 +15,119 @@
extern DeviceStatus dev_stat;
LoadOutput::LoadOutput(PowerPort *pwr_port) :
port(pwr_port)
LoadOutput::LoadOutput(DcBus *dc_bus, void (*drv_switch_fn)(bool), void (*drv_init_fn)()) :
PowerPort(dc_bus),
switch_set(drv_switch_fn)
{
enable = true;
usb_enable = true;
state = LOAD_STATE_DISABLED;
usb_state = LOAD_STATE_DISABLED;
state = LOAD_STATE_OFF;
#ifdef __ZEPHYR__
get_bindings();
#endif
// call driver initialization function
drv_init_fn();
switch_set(false);
usb_set(false);
junction_temperature = 25; // starting point: 25°C
oc_recovery_delay = 5*60; // default: 5 minutes
lvd_recovery_delay = 60*60; // default: 1 hour
ov_hysteresis = 0.3;
// analog comparator to detect short circuits and trigger immediate load switch-off
#ifdef __MBED__
// for Zephyr some interrupt conflicts have still to be resolved
short_circuit_comp_init();
#endif
}
void LoadOutput::usb_state_machine()
{
// We don't have any overcurrent detection mechanism for USB and the buck converter IC should
// reduce output power itself in case of failure. Battery overvoltage should also be handled by
// the DC/DC up to a certain degree.
// So, we don't use load failure states for the USB output. Only low SOC and exceeded
// temperature limits force the USB output to be turned off.
switch (usb_state) {
case LOAD_STATE_DISABLED:
if (usb_enable == true) {
if (port->bus->src_current_margin < 0) {
usb_set(true);
usb_state = LOAD_STATE_ON;
}
else if (state == LOAD_STATE_OFF_LOW_SOC || state == LOAD_STATE_OFF_TEMPERATURE) {
usb_state = state;
}
}
break;
case LOAD_STATE_ON:
// currently still same cut-off SOC limit as the load
if (state == LOAD_STATE_OFF_LOW_SOC || state == LOAD_STATE_OFF_TEMPERATURE) {
usb_set(false);
usb_state = state;
}
else if (usb_enable == false) {
usb_set(false);
usb_state = LOAD_STATE_DISABLED;
}
break;
case LOAD_STATE_OFF_LOW_SOC:
case LOAD_STATE_OFF_TEMPERATURE:
if (state == LOAD_STATE_ON) {
// also go back to normal operation with the USB charging port
usb_set(true);
usb_state = LOAD_STATE_ON;
}
else if (state == LOAD_STATE_DISABLED) {
usb_state = LOAD_STATE_DISABLED;
}
break;
}
}
// deprecated function for legacy load state output
int LoadOutput::determine_load_state()
{
if (pgood) {
return LOAD_STATE_ON;
}
else if (dev_stat.has_error(ERR_LOAD_LOW_SOC)) {
return LOAD_STATE_OFF_LOW_SOC;
}
else if (dev_stat.has_error(ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP)) {
return LOAD_STATE_OFF_OVERCURRENT;
}
else if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE)) {
return LOAD_STATE_OFF_OVERVOLTAGE;
}
else if (dev_stat.has_error(ERR_LOAD_SHORT_CIRCUIT)) {
return LOAD_STATE_OFF_SHORT_CIRCUIT;
}
else if (enable == false) {
return LOAD_STATE_DISABLED;
}
else {
return LOAD_STATE_OFF_TEMPERATURE;
}
enable = true; // switch on in next control() call if everything is fine
}
// this function is called more often than the state machine
void LoadOutput::control()
{
if (pgood) {
if (state == LOAD_STATE_ON) {
// junction temperature calculation model for overcurrent detection
junction_temperature = junction_temperature + (
dev_stat.internal_temp - junction_temperature +
port->current * port->current /
current * current /
(LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) *
(MOSFET_MAX_JUNCTION_TEMP - INTERNAL_MAX_REFERENCE_TEMP)
) / (MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY);
if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP
|| port->current > LOAD_CURRENT_MAX * 2)
{
dev_stat.set_error(ERR_LOAD_OVERCURRENT);
if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP || current > LOAD_CURRENT_MAX * 2) {
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
oc_timestamp = uptime();
}
if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE) ||
port->bus->voltage < disconnect_voltage)
{
dev_stat.set_error(ERR_LOAD_LOW_SOC);
// negative margin means sourcing current from bus is allowed
if (bus->src_current_margin > -0.1F) {
flags_set(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
}
if (bus->voltage < bus->src_droop_voltage(disconnect_voltage)) {
flags_set(&error_flags, ERR_LOAD_SHEDDING);
lvd_timestamp = uptime();
}
// long-term overvoltage (overvoltage transients are detected as an ADC alert and switch
// off the solar input instead of the load output)
if (port->bus->voltage > overvoltage ||
port->bus->voltage > LOW_SIDE_VOLTAGE_MAX)
{
if (bus->voltage > overvoltage || bus->voltage > LOW_SIDE_VOLTAGE_MAX) {
ov_debounce_counter++;
if (ov_debounce_counter >= CONTROL_FREQUENCY) { // waited 1s before setting the flag
dev_stat.set_error(ERR_LOAD_OVERVOLTAGE);
if (ov_debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
flags_set(&error_flags, ERR_LOAD_OVERVOLTAGE);
}
}
else {
ov_debounce_counter = 0;
}
if (dev_stat.has_error(ERR_LOAD_ANY)) {
if (error_flags) {
stop();
}
if (enable == false) {
switch_set(false);
state = LOAD_STATE_OFF;
}
}
else {
// load is off: check if errors are resolved and if load can be switched on
if (dev_stat.has_error(ERR_LOAD_LOW_SOC) &&
!dev_stat.has_error(ERR_BAT_UNDERVOLTAGE) &&
port->bus->voltage > reconnect_voltage &&
if (flags_check(&error_flags, ERR_LOAD_SHEDDING) &&
bus->voltage > bus->src_droop_voltage(reconnect_voltage) &&
uptime() - lvd_timestamp > lvd_recovery_delay)
{
dev_stat.clear_error(ERR_LOAD_LOW_SOC);
flags_clear(&error_flags, ERR_LOAD_SHEDDING);
}
if (dev_stat.has_error(ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP) &&
if (flags_check(&error_flags, ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP) &&
uptime() - oc_timestamp > oc_recovery_delay)
{
dev_stat.clear_error(ERR_LOAD_OVERCURRENT);
dev_stat.clear_error(ERR_LOAD_VOLTAGE_DIP);
flags_clear(&error_flags, ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP);
}
if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE) &&
port->bus->voltage < (overvoltage - ov_hysteresis) &&
port->bus->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis))
if (flags_check(&error_flags, ERR_LOAD_OVERVOLTAGE) &&
bus->voltage < (overvoltage - ov_hysteresis) &&
bus->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis))
{
dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE);
flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE);
}
if (dev_stat.has_error(ERR_LOAD_SHORT_CIRCUIT) && enable == false) {
if (flags_check(&error_flags, ERR_LOAD_SHORT_CIRCUIT) && enable == false) {
// stay here until the charge controller is reset or load is manually switched off
dev_stat.clear_error(ERR_LOAD_SHORT_CIRCUIT);
flags_clear(&error_flags, ERR_LOAD_SHORT_CIRCUIT);
}
// finally switch on if all errors were resolved
if (enable == true && !dev_stat.has_error(ERR_LOAD_ANY) && port->bus->src_current_margin < 0) {
// finally switch on if all errors were resolved and at least 1A src current is available
if (enable == true && !error_flags && bus->src_current_margin < -1.0F) {
switch_set(true);
state = LOAD_STATE_ON;
}
}
// deprecated load state variable, will be removed in the future
state = (int)determine_load_state();
usb_state_machine();
}
void LoadOutput::stop(uint32_t error_flag)
void LoadOutput::stop(uint32_t flag)
{
switch_set(false);
dev_stat.set_error(error_flag);
state = LOAD_STATE_OFF;
flags_set(&error_flags, flag);
// flicker the load LED if failure was most probably caused by the user
if (error_flag & (ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) {
if (flags_check(&error_flags,
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT))
{
#ifdef LED_LOAD
leds_flicker(LED_LOAD);
#endif
+81 -49
View File
@@ -19,49 +19,85 @@
#include "power_port.h"
/** Load/USB output states
*
* Used for USB or normal load outputs connecting to battery via switch.
*
* DEPRECATED: These states will be removed in the future and the status will be determined only
* by the error flags.
/**
* Load/USB output states
*/
enum LoadState {
LOAD_STATE_DISABLED = 0, ///< Actively disabled
LOAD_STATE_ON, ///< Normal state: On
LOAD_STATE_OFF_LOW_SOC, ///< Off to protect battery (overrules target setting)
LOAD_STATE_OFF_OVERCURRENT, ///< Off to protect charge controller (overrules target setting)
LOAD_STATE_OFF_OVERVOLTAGE, ///< Off to protect loads (overrules target setting)
LOAD_STATE_OFF_SHORT_CIRCUIT, ///< Off to protect charge controller (overrules target setting)
LOAD_STATE_OFF_TEMPERATURE ///< Off because of battery or charge controller temperature
LOAD_STATE_OFF = 0, ///< Actively disabled
LOAD_STATE_ON = 1, ///< Normal state: On
};
/** Load error flags
*
* When adding new flags, please make sure to use only up to 32 errors
* Each enum must represent a unique power of 2 number
*/
enum LoadErrorFlag {
/**
* Available energy or power too low
*
* Switching off the load can be triggered either by a low battery voltage or by low state of
* charge (SOC) in case of more advanced battery management.
*
* Set in LoadOutput::control() and cleared after reconnect delay passed and voltage is above
* reconnect threshold again.
*/
ERR_LOAD_SHEDDING = 1U << 0,
/**
* Too high voltage for load
*
* Set and cleared in LoadOutput::control()
*/
ERR_LOAD_OVERVOLTAGE = 1U << 1,
/**
* Long-term overcurrent at load port
*
* Set in LoadOutput::control() and cleared after configurable delay.
*/
ERR_LOAD_OVERCURRENT = 1U << 2,
/**
* Short circuit detected at load port
*
* Set by LoadOutput::control() after overcurrent comparator triggered, cleared only if
* load output is manually disabled and enabled again.
*/
ERR_LOAD_SHORT_CIRCUIT = 1U << 3,
/**
* Overcurrent identified via voltage dip (may be caused by too small battery)
*
* Set and cleared in LoadOutput::control(). Treated same as load overcurrent.
*/
ERR_LOAD_VOLTAGE_DIP = 1U << 4,
/**
* The bus the load is connected to disabled sourcing current from it
*
* Reasons can be that battery temperature limits were exceeded. Voltage limits should be
* covered by the load directly.
*/
ERR_LOAD_BUS_SRC_CURRENT = 1U << 5
};
/** Load output type
*
* Stores status of load output incl. 5V USB output (if existing on PCB)
*/
class LoadOutput
class LoadOutput : public PowerPort
{
public:
/** Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing)
/**
* Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing)
*
* @param load Load output struct
* @param bus_int Internal DC bus the load switch is connected to (e.g. battery and DC/DC junction)
* @param terminal Load terminal DC bus (external interface)
* @param dc_bus DC bus the load is connected to
* @param switch_set Pointer to function for enabling/disabling load switch
* @param init Pointer to function for load driver initialization
*/
LoadOutput(PowerPort *pwr_port);
/** Enable/disable load switch
*/
void switch_set(bool enabled);
/** Enable/disable USB output
*/
void usb_set(bool enabled);
/** USB state machine, called every second.
*/
void usb_state_machine();
LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)());
/** Main load control function, should be called by control timer
*
@@ -86,18 +122,13 @@ public:
*/
void set_voltage_limits(float lvd, float lvr, float ov);
uint32_t state; ///< Current state of load output switch (DEPRECATED)
uint32_t usb_state; ///< Current state of USB output (DEPRECATED)
uint32_t state; ///< Current state of load output switch (negative value means
///< error state, indicated by LoadErrorFlags)
PowerPort *port; ///< Pointer to DC bus containting actual voltage and current
///< measurement of (external) load output terminal
uint32_t error_flags = 0;
bool enable = false; ///< Target on state set via communication port (overruled if
///< battery is empty or any errors occured)
bool usb_enable = false; ///< Target on state for USB output
bool pgood = false; ///< Power good flag that is true if load switch is on
bool usb_pgood = false; ///< Power good flag for USB output
time_t oc_timestamp; ///< Time when last overcurrent event occured
int32_t oc_recovery_delay; ///< Seconds before we attempt to re-enable the load
@@ -113,21 +144,22 @@ public:
float junction_temperature; ///< calculated using thermal model based on current and ambient
///< temperature measurement (unit: °C)
float overvoltage = 0;
float overvoltage = 0; ///< Upper voltage limit
float ov_hysteresis; ///< Hysteresis to switch back on after an overvoltage event
private:
int determine_load_state();
void short_circuit_comp_init();
/**
* Pointer to the load switch function
*/
void (*switch_set)(bool);
int ov_debounce_counter = 0;
#ifdef __ZEPHYR__
void get_bindings();
struct device *dev_load;
struct device *dev_usb;
#endif
};
void load_out_init();
void usb_out_init();
void load_out_set(bool);
void usb_out_set(bool);
#endif /* LOAD_H */
+19 -18
View File
@@ -20,6 +20,12 @@
#include <stm32g4xx_ll_system.h>
#endif
static struct device *dev_load;
#ifdef DT_SWITCH_USB_PWR_GPIOS_CONTROLLER
static struct device *dev_usb;
#endif
#endif // __ZEPHYR__
#include "pcb.h"
@@ -85,7 +91,7 @@ static void lptim_init()
}
#endif
void LoadOutput::short_circuit_comp_init()
void short_circuit_comp_init()
{
#if defined(PIN_I_LOAD_COMP)
@@ -154,9 +160,8 @@ extern "C" void ADC1_COMP_IRQHandler(void)
#endif // PIN_I_LOAD_COMP
void LoadOutput::switch_set(bool status)
void load_out_set(bool status)
{
pgood = status;
#ifdef LED_LOAD
leds_set(LED_LOAD, status);
#endif
@@ -199,14 +204,10 @@ void LoadOutput::switch_set(bool status)
gpio_pin_set(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, 0);
}
#endif
print_info("Load pgood = %d, state = %" PRIu32 "\n", status, state);
}
void LoadOutput::usb_set(bool status)
void usb_out_set(bool status)
{
usb_pgood = status;
#if defined(__MBED__) && defined(PIN_USB_PWR_EN)
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
if (status == true) usb_pwr_en = 1;
@@ -228,21 +229,21 @@ void LoadOutput::usb_set(bool status)
gpio_pin_set(dev_usb, DT_SWITCH_USB_PWR_GPIOS_PIN, 0);
}
#endif
print_info("USB pgood = %d, state = %" PRIu32 ", en = %d\n", status, usb_state, usb_enable);
}
#ifdef __ZEPHYR__
void load_out_init()
{
#ifdef DT_SWITCH_LOAD_GPIOS_CONTROLLER
dev_load = device_get_binding(DT_SWITCH_LOAD_GPIOS_CONTROLLER);
#endif
void LoadOutput::get_bindings()
// analog comparator to detect short circuits and trigger immediate load switch-off
short_circuit_comp_init();
}
void usb_out_init()
{
#ifdef DT_SWITCH_USB_PWR_GPIOS_CONTROLLER
dev_usb = device_get_binding(DT_SWITCH_USB_PWR_GPIOS_CONTROLLER);
#endif
#ifdef DT_SWITCH_LOAD_GPIOS_CONTROLLER
dev_load = device_get_binding(DT_SWITCH_LOAD_GPIOS_CONTROLLER);
#endif
}
#endif
+3 -2
View File
@@ -17,6 +17,7 @@
#include "pcb.h" // hardware-specific settings
#include "config.h" // user-specific configuration
#include "setup.h"
#include "helper.h"
#include "half_bridge.h" // PWM generation for DC/DC converter
#include "hardware.h" // hardware-related functions like watchdog, etc.
@@ -100,7 +101,7 @@ int main()
eeprom_update();
leds_update_1s();
leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC));
leds_update_soc(charger.soc, flags_check(&load.error_flags, ERR_LOAD_SHEDDING));
ext_mgr.process_1s();
@@ -162,7 +163,7 @@ void system_control()
#endif
lv_terminal.energy_balance();
load_terminal.energy_balance();
load.energy_balance();
dev_stat.update_energy();
dev_stat.update_min_max_values();
charger.update_soc(&bat_conf);
+3 -2
View File
@@ -15,6 +15,7 @@
#include "pcb.h" // hardware-specific settings
#include "config.h" // user-specific configuration
#include "setup.h"
#include "helper.h"
#include "half_bridge.h" // PWM generation for DC/DC converter
#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc.
@@ -72,7 +73,7 @@ void main(void)
while (1) {
leds_update_1s();
leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC));
leds_update_soc(charger.soc, flags_check(&load.error_flags, ERR_LOAD_SHEDDING));
eeprom_update();
@@ -120,7 +121,7 @@ void control_thread()
#endif
lv_terminal.energy_balance();
load_terminal.energy_balance();
load.energy_balance();
dev_stat.update_energy();
dev_stat.update_min_max_values();
charger.update_soc(&bat_conf);
+2 -2
View File
@@ -47,8 +47,8 @@ PwmSwitch pwm_switch(&lv_bus);
#endif
#if CONFIG_HAS_LOAD_OUTPUT
PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus)
LoadOutput load(&load_terminal);
LoadOutput load(&lv_bus, &load_out_set, &load_out_init);
LoadOutput usb_pwr(&lv_bus, &usb_out_set, &usb_out_init);
#endif
#if CONFIG_HV_TERMINAL_SOLAR
+1 -1
View File
@@ -17,7 +17,6 @@
extern DcBus lv_bus;
extern PowerPort lv_terminal;
extern PowerPort load_terminal;
#if CONFIG_HAS_DCDC_CONVERTER
extern DcBus hv_bus;
@@ -40,6 +39,7 @@ extern BatConf bat_conf;
extern BatConf bat_conf_user;
extern LoadOutput load;
extern LoadOutput usb_pwr;
extern ThingSet ts; // defined in data_objects.cpp
+5 -6
View File
@@ -7,7 +7,7 @@
#include "tests.h"
#include "daq.h"
#include "daq_stub.h"
#include "helper.h"
#include "setup.h"
#include <stdint.h>
@@ -54,8 +54,8 @@ void check_bat_terminal_readings()
void check_load_terminal_readings()
{
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.bus->voltage * 10) / 10);
TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_terminal.current * 10) / 10);
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load.bus->voltage * 10) / 10);
TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load.current * 10) / 10);
}
void check_lv_bus_int_readings()
@@ -84,9 +84,8 @@ void adc_alert_undervoltage_triggering()
TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP));
adc_update_value(ADC_POS_V_LOW);
load.control();
TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP));
TEST_ASSERT_EQUAL(false, load.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state);
TEST_ASSERT_EQUAL(true, flags_check(&load.error_flags, ERR_LOAD_VOLTAGE_DIP));
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load.state);
// reset values
adcval.battery_voltage = 13;
+5 -5
View File
@@ -21,7 +21,7 @@ void reset_counters_at_start_of_day()
solar_terminal.neg_energy_Wh = 10.0;
bat_terminal.neg_energy_Wh = 3.0;
bat_terminal.pos_energy_Wh = 4.0;
load_terminal.pos_energy_Wh = 9.0;
load.pos_energy_Wh = 9.0;
// 5 houurs without sun
for (int i = 0; i <= 5 * 60 * 60; i++) {
@@ -36,7 +36,7 @@ void reset_counters_at_start_of_day()
TEST_ASSERT_EQUAL(10, solar_terminal.neg_energy_Wh);
TEST_ASSERT_EQUAL(3, bat_terminal.neg_energy_Wh);
TEST_ASSERT_EQUAL(4, bat_terminal.pos_energy_Wh);
TEST_ASSERT_EQUAL(9, load_terminal.pos_energy_Wh);
TEST_ASSERT_EQUAL(9, load.pos_energy_Wh);
// solar didn't come back yet
TEST_ASSERT_EQUAL(0, dev_stat.day_counter);
@@ -50,7 +50,7 @@ void reset_counters_at_start_of_day()
TEST_ASSERT_EQUAL(0, solar_terminal.neg_energy_Wh);
TEST_ASSERT_EQUAL(0, bat_terminal.neg_energy_Wh);
TEST_ASSERT_EQUAL(0, bat_terminal.pos_energy_Wh);
TEST_ASSERT_EQUAL(0, load_terminal.pos_energy_Wh);
TEST_ASSERT_EQUAL(0, load.pos_energy_Wh);
}
void dev_stat_new_solar_voltage_max()
@@ -76,7 +76,7 @@ void dev_stat_new_dcdc_current_max()
void dev_stat_new_load_current_max()
{
load.port->current = 21;
load.current = 21;
dev_stat.update_min_max_values();
TEST_ASSERT_EQUAL(21, dev_stat.load_current_max);
}
@@ -91,7 +91,7 @@ void dev_stat_solar_power_max()
void dev_stat_load_power_max()
{
load.port->power = 50;
load.power = 50;
dev_stat.update_min_max_values();
TEST_ASSERT_EQUAL(50, dev_stat.load_power_max_day);
TEST_ASSERT_EQUAL(50, dev_stat.load_power_max_total);
+122 -196
View File
@@ -14,293 +14,220 @@
#include "setup.h"
void load_init(LoadOutput *l)
static bool output_on;
static void load_drv_set(bool status)
{
output_on = status;
}
static void load_drv_init()
{
output_on = false;
}
static void load_init(LoadOutput *l, bool on = false)
{
l->overvoltage = 14.6;
l->port->current = 0;
l->port->pos_current_limit = 10;
l->port->bus->voltage = 14;
l->port->bus->sink_voltage_bound = 14.4;
l->port->bus->src_voltage_bound = 12;
l->port->bus->sink_current_margin = 10;
l->port->bus->src_current_margin = -10;
l->current = 0;
l->pos_current_limit = 10;
l->bus->voltage = 14;
l->bus->sink_voltage_bound = 14.4;
l->bus->src_voltage_bound = 12;
l->bus->sink_current_margin = 10;
l->bus->src_current_margin = -10;
l->junction_temperature = 25;
l->error_flags = 0;
l->enable = true;
if (on) {
load_drv_set(true);
l->state = LOAD_STATE_ON;
// check if it stays on
l->control();
TEST_ASSERT_EQUAL(LOAD_STATE_ON, l->state);
}
}
void control_off_to_pgood_if_everything_fine()
static void control_off_to_on_if_everything_fine()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out);
dev_stat.clear_error(ERR_ANY_ERROR);
load_out.enable = true;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(0, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
}
void control_pgood_to_off_overvoltage()
static void control_on_to_off_shedding()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_ON;
dev_stat.clear_error(ERR_ANY_ERROR);
port.bus->voltage = port.bus->sink_voltage_bound + 0.6;
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out, true);
bus.voltage = load_out.disconnect_voltage - 0.1;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_SHEDDING, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
}
void control_on_to_off_overvoltage()
{
DcBus bus = {};
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out, true);
bus.voltage = bus.sink_voltage_bound + 0.6;
// increase debounce counter to 1 before limit
for (int i = 0; i < CONTROL_FREQUENCY; i++) {
load_out.control();
}
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(0, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
load_out.control(); // once more
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load_out.state); // deprecated
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
}
void control_pgood_to_off_overcurrent()
void control_on_to_off_overcurrent()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
port.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately
load_out.state = LOAD_STATE_ON;
dev_stat.internal_temp = 25;
dev_stat.clear_error(ERR_ANY_ERROR);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out, true);
load_out.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
// almost 2x current = 4x heat generation: Should definitely trigger after waiting one time constant
int trigger_steps = MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY;
for (int i = 0; i <= trigger_steps; i++) {
load_out.control();
}
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load_out.state); // deprecated
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
}
void control_pgood_to_off_voltage_dip()
void control_on_to_off_voltage_dip()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_ON;
dev_stat.internal_temp = 25;
dev_stat.clear_error(ERR_ANY_ERROR);
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out, true);
load_out.stop(ERR_LOAD_VOLTAGE_DIP);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_VOLTAGE_DIP, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load_out.state); // deprecated
TEST_ASSERT_EQUAL(ERR_LOAD_VOLTAGE_DIP, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
}
void control_pgood_to_off_int_temp()
void control_on_to_off_bus_limit()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
load_out.state = LOAD_STATE_ON;
dev_stat.internal_temp = 25;
dev_stat.clear_error(ERR_ANY_ERROR);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out, true);
load_out.bus->src_current_margin = 0;
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
dev_stat.set_error(ERR_INT_OVERTEMP);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.state); // deprecated
TEST_ASSERT_EQUAL(ERR_LOAD_BUS_SRC_CURRENT, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
}
void control_pgood_to_off_bat_temp()
void control_on_to_off_if_enable_false()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
// overtemp
load_out.state = LOAD_STATE_ON;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.clear_error(ERR_ANY_ERROR);
dev_stat.set_error(ERR_BAT_DIS_OVERTEMP);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(false, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.usb_state); // deprecated
// undertemp
load_out.state = LOAD_STATE_ON;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.clear_error(ERR_ANY_ERROR);
dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(false, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load_out.usb_state); // deprecated
}
void control_pgood_to_off_low_soc()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
dev_stat.clear_error(ERR_ANY_ERROR);
load_out.state = LOAD_STATE_ON;
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
dev_stat.set_error(ERR_BAT_UNDERVOLTAGE);
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(ERR_LOAD_LOW_SOC | ERR_BAT_UNDERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load_out.state); // deprecated
}
void control_pgood_to_off_if_enable_false()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
load_init(&load_out);
dev_stat.clear_error(ERR_ANY_ERROR);
load_out.state = LOAD_STATE_ON;
load_out.control();
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(true, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out, true);
load_out.enable = false;
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state);
load_out.usb_enable = false;
load_out.control();
TEST_ASSERT_EQUAL(false, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load_out.usb_state);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
TEST_ASSERT_EQUAL(0, load_out.error_flags);
}
void control_off_low_soc_to_on_after_delay()
void control_off_shedding_to_on_after_delay()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_LOW_SOC;
load_out.usb_state = LOAD_STATE_OFF_LOW_SOC;
dev_stat.error_flags = ERR_LOAD_LOW_SOC;
load_out.error_flags = ERR_LOAD_SHEDDING;
load_out.lvd_timestamp = time(NULL) - load_out.lvd_recovery_delay + 1;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_LOW_SOC, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load_out.usb_state); // deprecated
TEST_ASSERT_EQUAL(ERR_LOAD_SHEDDING, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
load_out.lvd_timestamp = time(NULL) - load_out.lvd_recovery_delay - 1;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(true, load_out.pgood);
TEST_ASSERT_EQUAL(true, load_out.usb_pgood);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // deprecated
TEST_ASSERT_EQUAL(0, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
}
void control_off_overcurrent_to_on_after_delay()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_OVERCURRENT;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.error_flags = ERR_LOAD_OVERCURRENT;
load_out.error_flags = ERR_LOAD_OVERCURRENT;
load_out.oc_timestamp = time(NULL) - load_out.oc_recovery_delay + 1;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // not affected by overcurrent
TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
load_out.oc_timestamp = time(NULL) - load_out.oc_recovery_delay - 1;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // deprecated
TEST_ASSERT_EQUAL(0, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
}
void control_off_overvoltage_to_on_at_lower_voltage()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_OVERVOLTAGE;
load_out.usb_state = LOAD_STATE_ON;
port.bus->voltage = load_out.overvoltage + 0.1;
dev_stat.error_flags = ERR_LOAD_OVERVOLTAGE;
bus.voltage = load_out.overvoltage + 0.1;
load_out.error_flags = ERR_LOAD_OVERVOLTAGE;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // not affected by overvoltage
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
port.bus->voltage = load_out.overvoltage - 0.1; // test hysteresis
bus.voltage = load_out.overvoltage - 0.1; // test hysteresis
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load_out.state);
TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
port.bus->voltage = load_out.overvoltage - load_out.ov_hysteresis - 0.1;
bus.voltage = load_out.overvoltage - load_out.ov_hysteresis - 0.1;
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // deprecated
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // deprecated
TEST_ASSERT_EQUAL(0, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
}
void control_off_short_circuit_flag_reset()
{
DcBus bus = {};
PowerPort port(&bus);
LoadOutput load_out(&port);
LoadOutput load_out(&bus, &load_drv_set, &load_drv_init);
load_init(&load_out);
load_out.state = LOAD_STATE_OFF_SHORT_CIRCUIT;
load_out.usb_state = LOAD_STATE_ON;
dev_stat.error_flags = ERR_LOAD_SHORT_CIRCUIT;
load_out.error_flags = ERR_LOAD_SHORT_CIRCUIT;
load_out.control();
TEST_ASSERT_EQUAL(ERR_LOAD_SHORT_CIRCUIT, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state); // not affected by overvoltage
TEST_ASSERT_EQUAL(ERR_LOAD_SHORT_CIRCUIT, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
load_out.enable = false; // this is like a manual reset
load_out.control();
TEST_ASSERT_EQUAL(0, dev_stat.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load_out.state);
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.usb_state);
TEST_ASSERT_EQUAL(0, load_out.error_flags);
TEST_ASSERT_EQUAL(LOAD_STATE_OFF, load_out.state);
load_out.enable = true;
load_out.control();
TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state);
}
void load_tests()
@@ -308,16 +235,15 @@ void load_tests()
UNITY_BEGIN();
// control tests
RUN_TEST(control_off_to_pgood_if_everything_fine);
RUN_TEST(control_pgood_to_off_overvoltage);
RUN_TEST(control_pgood_to_off_overcurrent);
RUN_TEST(control_pgood_to_off_voltage_dip);
RUN_TEST(control_pgood_to_off_int_temp);
RUN_TEST(control_pgood_to_off_bat_temp);
RUN_TEST(control_pgood_to_off_low_soc);
RUN_TEST(control_pgood_to_off_if_enable_false);
RUN_TEST(control_off_to_on_if_everything_fine);
RUN_TEST(control_on_to_off_shedding);
RUN_TEST(control_on_to_off_overvoltage);
RUN_TEST(control_on_to_off_overcurrent);
RUN_TEST(control_on_to_off_voltage_dip);
RUN_TEST(control_on_to_off_bus_limit);
RUN_TEST(control_on_to_off_if_enable_false);
RUN_TEST(control_off_low_soc_to_on_after_delay);
RUN_TEST(control_off_shedding_to_on_after_delay);
RUN_TEST(control_off_overcurrent_to_on_after_delay);
RUN_TEST(control_off_overvoltage_to_on_at_lower_voltage);
RUN_TEST(control_off_short_circuit_flag_reset);
+4 -4
View File
@@ -28,7 +28,7 @@ void energy_calculation_init()
dev_stat.bat_chg_total_Wh = 0;
dev_stat.bat_dis_total_Wh = 0;
hv_terminal.neg_energy_Wh = 0.0;
load_terminal.pos_energy_Wh = 0.0;
load.pos_energy_Wh = 0.0;
lv_terminal.pos_energy_Wh = 0.0;
lv_terminal.neg_energy_Wh = 0.0;
@@ -49,7 +49,7 @@ void energy_calculation_init()
for (int i = 0; i < 60*60*sun_hours; i++) {
hv_terminal.energy_balance();
lv_terminal.energy_balance();
load_terminal.energy_balance();
load.energy_balance();
}
// disable DC/DC = solar charging
@@ -61,7 +61,7 @@ void energy_calculation_init()
for (int i = 0; i < 60*60*night_hours; i++) {
hv_terminal.energy_balance();
lv_terminal.energy_balance();
load_terminal.energy_balance();
load.energy_balance();
}
}
@@ -88,7 +88,7 @@ void solar_input_energy_calculation_valid()
void load_output_energy_calculation_valid()
{
energy_calculation_init();
TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load_terminal.pos_energy_Wh));
TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load.pos_energy_Wh));
}
void power_port_tests()