From f254c77ab6793b393439f74d2e2ea31045fc450b Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Sat, 9 Nov 2019 16:22:54 +0100 Subject: [PATCH] Refactoring of load control and state machine - State machine and control merged to one function - Load states marked as deprecated, global error flags used instead - Reduced duplicated code and made load control function easier to read --- src/adc_dma.cpp | 11 +- src/data_objects.cpp | 2 +- src/device_status.h | 53 +++-- src/leds.cpp | 20 +- src/leds.h | 2 +- src/load.cpp | 323 ++++++++++++---------------- src/load.h | 36 ++-- src/main.cpp | 4 +- src/pcbs/pcb_stub.h | 4 +- src/uext/uext_oled.cpp | 36 ++-- src/uext/uext_wifi.cpp | 4 +- test/tests_adc_dma.cpp | 8 +- test/tests_load.cpp | 463 ++++++++++++++++++++--------------------- 13 files changed, 452 insertions(+), 514 deletions(-) diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index f53f102..c8b3049 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -283,11 +283,8 @@ void high_voltage_alert() void low_voltage_alert() { - // disable load output - load.emergency_stop(LOAD_STATE_OFF_OVERCURRENT); - charger.port->neg_current_limit = 0; - - dev_stat.set_error(ERR_BAT_UNDERVOLTAGE); + // the battery undervoltage must have been caused by a load current peak + load.stop(ERR_LOAD_VOLTAGE_DIP); print_error("Low voltage alert, ADC reading: %d limit: %d\n", adc_readings[ADC_POS_V_BAT], adc_alerts_lower[ADC_POS_V_BAT].limit); @@ -306,8 +303,8 @@ uint16_t adc_get_alert_limit(float scale, float limit) const float limit_scaled = limit * scale; // even if we have a higher voltage limit, we must limit it // to the max value the ADC will be able to deliver - return (limit_scaled > adclimit ? 0x0FFF : (uint16_t)(limit_scaled)) << 4; - // shift 4 bits left to generate left aligned 16bit value + return (limit_scaled > adclimit ? 0x0FFF : (uint16_t)(limit_scaled)) << 4; + // shift 4 bits left to generate left aligned 16bit value } void adc_set_lv_alerts(float upper, float lower) diff --git a/src/data_objects.cpp b/src/data_objects.cpp index dfec76e..2f7e56f 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -96,7 +96,7 @@ const data_object_t data_objects[] = { {0x43, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_reconnect), "LoadReconnect_V"}, //{0x44, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_disconnect), "UsbDisconnect_V"}, //{0x45, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_reconnect), "UsbReconnect_V"}, - {0x46, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(load.overcurrent_recovery_delay), "LoadOCRecovery_s"}, + {0x46, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(load.oc_recovery_delay), "LoadOCRecovery_s"}, {0x47, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 0, (void*) &(load.lvd_recovery_delay), "LoadUVRecovery_s"}, // other configuration items diff --git a/src/device_status.h b/src/device_status.h index d0f62db..d903b65 100644 --- a/src/device_status.h +++ b/src/device_status.h @@ -36,97 +36,112 @@ * Each enum must represent a unique power of 2 number */ enum ErrorFlag { + /** Battery voltage too low * * Set directly in ISR after ADC conversion finished, cleared in Charger::discharge_control() + * when voltage reached higher level again. */ ERR_BAT_UNDERVOLTAGE = 1U << 0, /** Battery voltage too high * - * Set directly in ISR after ADC conversion finished, cleared in Charger::charge_control() + * Set directly in ISR after ADC conversion finished, cleared in Charger::charge_control() when + * voltage reached lower level again. */ ERR_BAT_OVERVOLTAGE = 1U << 1, /** Battery discharge overcurrent * - * Not used yet + * Not used yet, reserved for future. */ ERR_BAT_DIS_OVERCURRENT = 1U << 2, /** Battery charge overcurrent * - * Not used yet + * Not used yet, reserved for future. */ ERR_BAT_CHG_OVERCURRENT = 1U << 3, /** Temperature below discharge minimum limit * - * Set and cleared in Charger::discharge_control + * Set and cleared in Charger::discharge_control (with 2°C hysteresis) */ ERR_BAT_DIS_UNDERTEMP = 1U << 4, /** Temperature above discharge maximum limit * - * Set and cleared in Charger::discharge_control + * Set and cleared in Charger::discharge_control (with 2°C hysteresis) */ ERR_BAT_DIS_OVERTEMP = 1U << 5, /** Temperature below charge minimum limit * - * Set and cleared in Charger::discharge_control + * Set and cleared in Charger::charge_control (with 2°C hysteresis) */ ERR_BAT_CHG_UNDERTEMP = 1U << 6, /** Temperature above charge maximum limit * - * Set and cleared in Charger::discharge_control + * Set and cleared in Charger::charge_control (with 2°C hysteresis) */ 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 in LoadOutput::control() and cleared in LoadOutput::state_machine() + * Set and cleared in LoadOutput::control() */ - ERR_LOAD_OVERVOLTAGE = 1U << 8, + ERR_LOAD_OVERVOLTAGE = 1U << 9, /** Long-term overcurrent of load port * - * Set in LoadOutput::control() and cleared in LoadOutput::state_machine() + * Set in LoadOutput::control() and cleared after configurable delay. */ - ERR_LOAD_OVERCURRENT = 1U << 9, + ERR_LOAD_OVERCURRENT = 1U << 10, /** Short circuit detected at load port * - * Set by LoadOutput::control() after overcurrent comparator triggered, cleared in - * LoadOutput::state_machine() + * Set by LoadOutput::control() after overcurrent comparator triggered, cleared only if + * load output is manually disabled and enabled again. */ - ERR_LOAD_SHORT_CIRCUIT = 1U << 10, + ERR_LOAD_SHORT_CIRCUIT = 1U << 11, /** Overcurrent identified via voltage dip (may be caused by too small battery) * - * Set in LoadOutput::control() and cleared in LoadOutput::state_machine(). Reported as - * overcurrent in load state machine. + * Set and cleared in LoadOutput::control(). Treated same as load overcurrent. */ - ERR_LOAD_VOLTAGE_DIP = 1U << 11, + ERR_LOAD_VOLTAGE_DIP = 1U << 12, /** Charge controller internal temperature too high * * Set and cleared by ADC update_measurements() */ - ERR_INT_OVERTEMP = 1U << 12, + ERR_INT_OVERTEMP = 1U << 13, /** Short-circuit in HS MOSFET * * Set in Dcdc::control() and never cleared */ - ERR_DCDC_HS_MOSFET_SHORT = 1U << 13, + ERR_DCDC_HS_MOSFET_SHORT = 1U << 14, /** Mask to catch all error flags (up to 32 errors) */ 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 diff --git a/src/leds.cpp b/src/leds.cpp index 3425d15..62722e5 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -14,25 +14,26 @@ * limitations under the License. */ -#ifndef UNIT_TEST #include "leds.h" #include "pcb.h" -#include "mbed.h" - -static int led_states[NUM_LEDS]; // must be one of led_state_t +static int led_states[NUM_LEDS]; // must be one of enum LedState static int trigger_timeout[NUM_LEDS]; // seconds until trigger should end static int blink_state = 1; // global state of all blinking LEDs -static bool flicker_state = 1; // global state of all flickering LEDs static bool charging = false; +#ifndef UNIT_TEST + +#include "mbed.h" + static void charlieplexing() { static int led_count = 0; static int flicker_count = 0; + static bool flicker_state = 1; // could be increased to value >NUM_LEDS to reduce on-time if (led_count >= NUM_LEDS) { @@ -156,6 +157,8 @@ void leds_init(bool enabled) timer_start(NUM_LEDS * 60); // 60 Hz } +#endif /* UNIT_TEST */ + void leds_set_charging(bool enabled) { charging = enabled; @@ -263,10 +266,3 @@ void leds_update_soc(int soc, bool load_off_low_soc) } #endif } - -#else - -// dummy functions -void leds_flicker(int led, int timeout) {} - -#endif /* UNIT_TEST */ diff --git a/src/leds.h b/src/leds.h index 28cc0c1..df4e2c0 100644 --- a/src/leds.h +++ b/src/leds.h @@ -25,7 +25,7 @@ /** LED state type */ -enum led_state_t { +enum LedState { LED_STATE_OFF = 0, LED_STATE_ON, LED_STATE_BLINK, diff --git a/src/load.cpp b/src/load.cpp index 46e655d..e89f5ac 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -22,11 +22,9 @@ #include "device_status.h" #include "debug.h" -volatile bool short_circuit = false; - extern DeviceStatus dev_stat; -#ifndef UNIT_TEST +extern LoadOutput load; // necessary to call emergency stop function #if defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2 static void lptim_init() @@ -132,8 +130,7 @@ extern "C" void ADC1_COMP_IRQHandler(void) if (COMP2->CSR & COMP_CSR_COMP2VALUE) { // Load should be switched off by LPTIM trigger already. This interrupt // is mainly used to indicate the failure. - leds_flicker(LED_LOAD); - short_circuit = true; + load.stop(ERR_LOAD_SHORT_CIRCUIT); } // clear interrupt flag @@ -143,13 +140,14 @@ extern "C" void ADC1_COMP_IRQHandler(void) #endif // PIN_I_LOAD_COMP -void LoadOutput::switch_set(bool enabled) +void LoadOutput::switch_set(bool status) { - leds_set(LED_LOAD, enabled); + pgood = status; + leds_set(LED_LOAD, status); #ifdef PIN_LOAD_EN DigitalOut load_enable(PIN_LOAD_EN); - if (enabled) { + if (status == true) { load_enable = 1; } else { @@ -159,56 +157,48 @@ void LoadOutput::switch_set(bool enabled) #ifdef PIN_LOAD_DIS DigitalOut load_disable(PIN_LOAD_DIS); - #ifdef PIN_I_LOAD_COMP - if (enabled) { + if (status == true) { + #ifdef PIN_I_LOAD_COMP lptim_init(); - } - #else - if (enabled) { + #else load_disable = 0; + #endif } - #endif else { load_disable = 1; } - print_info("Load enabled = %d\n", enabled); #endif + print_info("Load pgood = %d, state = %u\n", status, state); } -void LoadOutput::usb_set(bool enabled) +void LoadOutput::usb_set(bool status) { + usb_pgood = status; + #ifdef PIN_USB_PWR_EN DigitalOut usb_pwr_en(PIN_USB_PWR_EN); - if (enabled) usb_pwr_en = 1; + if (status == true) usb_pwr_en = 1; else usb_pwr_en = 0; #endif #ifdef PIN_USB_PWR_DIS DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS); - if (enabled) usb_pwr_dis = 0; + if (status == true) usb_pwr_dis = 0; else usb_pwr_dis = 1; #endif + print_info("USB pgood = %d, state = %u, en = %d\n", status, usb_state, usb_enable); } -#else /* UNIT_TEST */ - -// dummy functions -static void short_circuit_comp_init() {;} -void LoadOutput::switch_set(bool enabled) {;} -void LoadOutput::usb_set(bool enabled) {;} - -#endif - -LoadOutput::LoadOutput(PowerPort *pwr_port) +LoadOutput::LoadOutput(PowerPort *pwr_port) : + port(pwr_port) { - port = pwr_port; enable = true; usb_enable = true; - switch_set(false); - usb_set(false); state = LOAD_STATE_DISABLED; usb_state = LOAD_STATE_DISABLED; + switch_set(false); + usb_set(false); junction_temperature = 25; // starting point: 25°C - overcurrent_recovery_delay = 5*60; // default: 5 minutes + oc_recovery_delay = 5*60; // default: 5 minutes lvd_recovery_delay = 60*60; // default: 1 hour // analog comparator to detect short circuits and trigger immediate load switch-off @@ -218,9 +208,9 @@ LoadOutput::LoadOutput(PowerPort *pwr_port) 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. Battey overvoltage should also be handled by + // 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 above failure states for the USB output. Only low SOC and exceeded + // 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: @@ -247,188 +237,141 @@ void LoadOutput::usb_state_machine() break; case LOAD_STATE_OFF_LOW_SOC: case LOAD_STATE_OFF_TEMPERATURE: - if (state == LOAD_STATE_ON || state == LOAD_STATE_DISABLED) { + 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; } } -void LoadOutput::state_machine() +// deprecated function for legacy load state output +int LoadOutput::determine_load_state() { - switch (state) { - case LOAD_STATE_DISABLED: - if (enable == true) { - if (port->pos_current_limit > 0 && - !dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE - | ERR_INT_OVERTEMP | ERR_BAT_DIS_OVERTEMP | ERR_BAT_DIS_UNDERTEMP)) - { - switch_set(true); - state = LOAD_STATE_ON; - } - else { - if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) { - state = LOAD_STATE_OFF_LOW_SOC; - } - else if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE)) { - state = LOAD_STATE_OFF_OVERVOLTAGE; - } - else if (dev_stat.has_error(ERR_INT_OVERTEMP) - || dev_stat.has_error(ERR_BAT_DIS_OVERTEMP) - || dev_stat.has_error(ERR_BAT_DIS_UNDERTEMP)) - { - state = LOAD_STATE_OFF_TEMPERATURE; - } - } - } - break; - case LOAD_STATE_ON: - if (enable == false) { - switch_set(false); - state = LOAD_STATE_DISABLED; - } - else if (port->pos_current_limit == 0) { // float == is allowed for 0.0 - switch_set(false); - // find out reason why load current is not allowed - if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) { - lvd_timestamp = time(NULL); - state = LOAD_STATE_OFF_LOW_SOC; - } - else if (dev_stat.has_error(ERR_BAT_OVERVOLTAGE)) { - state = LOAD_STATE_OFF_OVERVOLTAGE; - } - else if (dev_stat.has_error(ERR_INT_OVERTEMP) - || dev_stat.has_error(ERR_BAT_DIS_OVERTEMP) - || dev_stat.has_error(ERR_BAT_DIS_UNDERTEMP)) - { - state = LOAD_STATE_OFF_TEMPERATURE; - } - else { - // for safety reasons if other error flags are added in the future - state = LOAD_STATE_DISABLED; - } - } - break; - case LOAD_STATE_OFF_LOW_SOC: - // wait at least configured time - if (time(NULL) - lvd_timestamp > lvd_recovery_delay && - !(dev_stat.has_error(ERR_BAT_UNDERVOLTAGE))) - { - state = LOAD_STATE_DISABLED; // switch to normal mode again - } - break; - case LOAD_STATE_OFF_OVERCURRENT: - // wait configured time - if (time(NULL) - overcurrent_timestamp > overcurrent_recovery_delay) { - dev_stat.clear_error(ERR_LOAD_OVERCURRENT); - dev_stat.clear_error(ERR_LOAD_VOLTAGE_DIP); - state = LOAD_STATE_DISABLED; // switch to normal mode again - } - break; - case LOAD_STATE_OFF_OVERVOLTAGE: - if (port->voltage < (port->sink_voltage_max - 0.5) && - port->voltage < (LOW_SIDE_VOLTAGE_MAX - 0.5)) - { - dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE); - state = LOAD_STATE_DISABLED; // switch to normal mode again - } - break; - case LOAD_STATE_OFF_SHORT_CIRCUIT: - // stay here until the charge controller is reset or load is switched off remotely - if (enable == false) { - short_circuit = false; - dev_stat.clear_error(ERR_LOAD_SHORT_CIRCUIT); - state = LOAD_STATE_DISABLED; // switch to normal mode again - } - break; - case LOAD_STATE_OFF_TEMPERATURE: - if (!(dev_stat.has_error(ERR_INT_OVERTEMP) - || dev_stat.has_error(ERR_BAT_DIS_OVERTEMP) - || dev_stat.has_error(ERR_BAT_DIS_UNDERTEMP))) - { - state = LOAD_STATE_DISABLED; - } - break; + 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; } - - usb_state_machine(); } // this function is called more often than the state machine void LoadOutput::control() { - if (short_circuit) { - if (state != LOAD_STATE_OFF_SHORT_CIRCUIT) { - state = LOAD_STATE_OFF_SHORT_CIRCUIT; - dev_stat.set_error(ERR_LOAD_SHORT_CIRCUIT); - print_error("Load short circuit detected\n"); + if (pgood) { + + // junction temperature calculation model for overcurrent detection + junction_temperature = junction_temperature + ( + dev_stat.internal_temp - junction_temperature + + port->current * port->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); + oc_timestamp = time(NULL); } - return; - } - // junction temperature calculation model for overcurrent detection - junction_temperature = junction_temperature + ( - dev_stat.internal_temp - junction_temperature + - port->current * port->current / - (LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) * - (MOSFET_MAX_JUNCTION_TEMP - INTERNAL_MAX_REFERENCE_TEMP) - ) / (MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY); + if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) { + dev_stat.set_error(ERR_LOAD_LOW_SOC); + lvd_timestamp = time(NULL); + } - if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP - || port->current > LOAD_CURRENT_MAX * 2) - { - switch_set(false); - leds_flicker(LED_LOAD); - state = LOAD_STATE_OFF_OVERCURRENT; - dev_stat.set_error(ERR_LOAD_OVERCURRENT); - print_error("Load overcurrent detected\n"); - overcurrent_timestamp = time(NULL); - return; - } + // long-term overvoltage (overvoltage transients are detected as an ADC alert and switch + // off the solar input instead of the load output) + static int debounce_counter = 0; + if (port->voltage > port->sink_voltage_max || + port->voltage > LOW_SIDE_VOLTAGE_MAX) + { + debounce_counter++; + if (debounce_counter >= CONTROL_FREQUENCY) { // waited 1s before setting the flag + dev_stat.set_error(ERR_LOAD_OVERVOLTAGE); + } + } + else { + debounce_counter = 0; + } - // internal overtemperature - if (dev_stat.has_error(ERR_INT_OVERTEMP)) { - switch_set(false); - state = LOAD_STATE_OFF_TEMPERATURE; - print_error("Device overtemperature detected\n"); - return; - } + if (dev_stat.has_error(ERR_LOAD_ANY)) { + stop(); + } - // overcurrent detected because of voltage dip by 25% for around 100ms - if (port->voltage < voltage_prev * 0.75 && voltage_prev != 0) { - switch_set(false); - leds_flicker(LED_LOAD); - state = LOAD_STATE_OFF_OVERCURRENT; - dev_stat.set_error(ERR_LOAD_VOLTAGE_DIP); - print_error("Load voltage dip detected\n"); - overcurrent_timestamp = time(NULL); - return; - } - voltage_prev = port->voltage; - - static int debounce_counter = 0; - if (port->voltage > port->sink_voltage_max || - port->voltage > LOW_SIDE_VOLTAGE_MAX) - { - debounce_counter++; - if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag + if (enable == false) { switch_set(false); - print_error("Load overvoltage detected\n"); - state = LOAD_STATE_OFF_OVERVOLTAGE; - overcurrent_timestamp = time(NULL); - dev_stat.set_error(ERR_LOAD_OVERVOLTAGE); - return; } } else { - debounce_counter = 0; + // 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) && + time(NULL) - lvd_timestamp > lvd_recovery_delay) + { + dev_stat.clear_error(ERR_LOAD_LOW_SOC); + } + + if (dev_stat.has_error(ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP) && + time(NULL) - oc_timestamp > oc_recovery_delay) + { + dev_stat.clear_error(ERR_LOAD_OVERCURRENT); + dev_stat.clear_error(ERR_LOAD_VOLTAGE_DIP); + } + + if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE) && + port->voltage < (port->sink_voltage_max - 0.5) && + port->voltage < (LOW_SIDE_VOLTAGE_MAX - 0.5)) + { + dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE); + } + + if (dev_stat.has_error(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); + } + + // finally switch on if all errors were resolved + if (enable == true && !dev_stat.has_error(ERR_LOAD_ANY) && port->pos_current_limit > 0) { + switch_set(true); + } } + + // deprecated load state variable, will be removed in the future + state = (int)determine_load_state(); + + usb_state_machine(); } -void LoadOutput::emergency_stop(uint16_t next_state) +void LoadOutput::stop(uint32_t error_flag) { switch_set(false); - leds_flicker(LED_LOAD); - state = next_state; + dev_stat.set_error(error_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)) { + leds_flicker(LED_LOAD); + oc_timestamp = time(NULL); + } } diff --git a/src/load.h b/src/load.h index f7bc730..e73be11 100644 --- a/src/load.h +++ b/src/load.h @@ -32,6 +32,9 @@ /** 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. */ enum LoadState { LOAD_STATE_DISABLED = 0, ///< Actively disabled @@ -66,17 +69,13 @@ public: */ void usb_set(bool enabled); - /** State machine, called every second. - */ - void state_machine(); - /** USB state machine, called every second. */ void usb_state_machine(); /** Main load control function, should be called by control timer * - * Performs time-critical checks like overcurrent and overvoltage + * This function includes the load state machine */ void control(); @@ -84,21 +83,26 @@ public: * * May be called from an ISR which detected overvoltage / overcurrent conditions * - * @param next_state Select state the load should go to + * @param error_flag Optional error flag that should be set */ - void emergency_stop(uint16_t next_state = LOAD_STATE_DISABLED); + void stop(uint32_t error_flag = 0); - uint16_t state; ///< Current state of load output switch - uint16_t usb_state; ///< Current state of USB output + int state; ///< Current state of load output switch (DEPRECATED) + int usb_state; ///< Current state of USB output (DEPRECATED) PowerPort *port; ///< Pointer to DC bus containting actual voltage and current ///< measurement of (external) load output terminal - float voltage_prev = 0; ///< voltage from previous call of control function + bool enable; ///< Target on state set via communication port (overruled if + ///< battery is empty or any errors occured) + bool usb_enable; ///< Target on state for USB output - time_t overcurrent_timestamp; ///< Time when last overcurrent event occured - int overcurrent_recovery_delay; ///< Seconds before we attempt to re-enable the load - ///< after an overcurrent event + 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 + int oc_recovery_delay; ///< Seconds before we attempt to re-enable the load + ///< after an overcurrent event time_t lvd_timestamp; ///< Time when last low voltage disconnect happened int lvd_recovery_delay; ///< Seconds before we re-enable the load after a low voltage @@ -107,9 +111,9 @@ public: float junction_temperature; ///< calculated using thermal model based on current and ambient ///< temperature measurement (unit: °C) - bool enable; ///< target setting defined via communication port (overruled if - ///< battery is empty) - bool usb_enable; ///< same for USB output +private: + int determine_load_state(); + }; #endif /* LOAD_H */ diff --git a/src/main.cpp b/src/main.cpp index bb31d4e..5194a4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -163,8 +163,6 @@ int main() bat_terminal.pass_voltage_targets(&pwm_port_int); #endif - load.state_machine(); - // update regularly to cover changed battery configurations adc_set_lv_alerts(bat_conf.voltage_absolute_max * charger.num_batteries, bat_conf.voltage_absolute_min * charger.num_batteries); @@ -172,7 +170,7 @@ int main() eeprom_update(); leds_update_1s(); - leds_update_soc(charger.soc, load.state == LOAD_STATE_OFF_LOW_SOC); + leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC)); uext.process_1s(); ts_interfaces.process_1s(); diff --git a/src/pcbs/pcb_stub.h b/src/pcbs/pcb_stub.h index 77134df..1a2eab2 100644 --- a/src/pcbs/pcb_stub.h +++ b/src/pcbs/pcb_stub.h @@ -48,8 +48,8 @@ #define PIN_SWD_TX 0 #define PIN_SWD_RX 0 -#define PIN_LOAD_DIS 0 -#define PIN_USB_PWR_DIS 0 +//#define PIN_LOAD_DIS 0 +//#define PIN_USB_PWR_DIS 0 enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 1f89ad8..44d2e3c 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -57,31 +57,31 @@ void UExtOled::enable() { uext_dis = 0; #endif /** - * 1.Display is OFF - * 2.128 x 64 Display Mode - * 3.Normal segment and display data column address and row address mapping + * 1.Display is OFF + * 2.128 x 64 Display Mode + * 3.Normal segment and display data column address and row address mapping * (SEG0 mapped to address 00h and COM0 mapped to address 00h) - * - * 4.Shift register data clear in serial interface - * 5.Display start line is set at display RAM address 0 - * 6.Column address counter is set at 0 - * 7.Normal scan direction of the COM outputs - * 8.Contrast control register is set at 7Fh + * + * 4.Shift register data clear in serial interface + * 5.Display start line is set at display RAM address 0 + * 6.Column address counter is set at 0 + * 7.Normal scan direction of the COM outputs + * 8.Contrast control register is set at 7Fh * 9.Normal display mode (Equivalent to A4h command) - */ + */ - // make sure that + // make sure that // display has not shift // even without power on reset oled.command(0x22); oled.command(0x00); oled.command(0x07); -#ifdef OLED_BRIGHTNESS - // reduce brightness to minimum +#ifdef OLED_BRIGHTNESS + // reduce brightness to minimum oled.command(0x81); oled.command(OLED_BRIGHTNESS); -#endif +#endif } void UExtOled::process_asap() {} @@ -99,7 +99,7 @@ void UExtOled::process_1s() { oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1); } - if (load.state == LOAD_STATE_ON) { + if (load.pgood) { oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1); } else { @@ -108,7 +108,7 @@ void UExtOled::process_1s() { oled.drawRect(52, 2, 18, 9, 1); // battery shape oled.drawRect(69, 3, 3, 7, 1); // battery terminal - + if (charger.soc >= 20) { oled.drawRect(54, 4, 2, 5, 1); // bar 1 } @@ -170,14 +170,14 @@ void UExtOled::process_1s() { #else bool pwm_enabled = half_bridge_enabled(); float duty_cycle = half_bridge_get_duty_cycle(); -#endif +#endif float temp = charger.ext_temp_sensor ? charger.bat_temperature:dev_stat.internal_temp; char tC = charger.ext_temp_sensor ? 'T' : 't'; if (pwm_enabled == true) { oled.printf("%c %.0fC PWM %.0f%% SOC %d%%", tC, temp, duty_cycle * 100.0, charger.soc); - } + } else { oled.printf("%c %.0fC PWM OFF SOC %d%%", tC, temp, charger.soc); } diff --git a/src/uext/uext_wifi.cpp b/src/uext/uext_wifi.cpp index 8da0677..49c7997 100644 --- a/src/uext/uext_wifi.cpp +++ b/src/uext/uext_wifi.cpp @@ -177,7 +177,7 @@ void UExtWifi::process_1s() { static WifiState state = STATE_WIFI_RESET; static int error_counter = 0; - if (load.usb_state != LOAD_STATE_ON) { + if (!load.usb_pgood) { wifi_enable = 0; state = STATE_WIFI_IDLE; } @@ -255,7 +255,7 @@ void UExtWifi::process_1s() { error_counter++; break; case STATE_WIFI_IDLE: - if (load.usb_state == LOAD_STATE_ON) { + if (load.usb_pgood) { state = STATE_WIFI_RESET; } break; diff --git a/test/tests_adc_dma.cpp b/test/tests_adc_dma.cpp index 31e68a4..7d2fa82 100644 --- a/test/tests_adc_dma.cpp +++ b/test/tests_adc_dma.cpp @@ -76,9 +76,11 @@ void adc_alert_undervoltage_triggering() adcval.battery_voltage = bat_conf.voltage_absolute_min - 0.1; prepare_adc_readings(adcval); adc_update_value(ADC_POS_V_BAT); - TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)); + TEST_ASSERT_EQUAL(false, dev_stat.has_error(ERR_LOAD_VOLTAGE_DIP)); adc_update_value(ADC_POS_V_BAT); - TEST_ASSERT_EQUAL(true, dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)); + 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); // reset values @@ -163,4 +165,4 @@ void adc_tests() RUN_TEST(adc_alert_overflow_prevention); UNITY_END(); -} \ No newline at end of file +} diff --git a/test/tests_load.cpp b/test/tests_load.cpp index 21d3d11..a1d77ff 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -9,312 +9,295 @@ #include "main.h" -void disabled_to_on_if_everything_fine() +void load_init(LoadOutput *l) { - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 10; - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); + l->port->init_load(14.6); + l->port->pos_current_limit = 10; + l->port->voltage = 14; + l->junction_temperature = 25; } -void disabled_to_off_low_soc_if_error_flag_set() +void control_off_to_pgood_if_everything_fine() { - PowerPort port; + PowerPort port = {}; LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 0; + load_init(&load); dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_UNDERVOLTAGE); - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state); + + load.control(); + TEST_ASSERT_EQUAL(0, dev_stat.error_flags); + TEST_ASSERT_EQUAL(true, load.pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated } -void disabled_to_off_bat_temp_if_error_flag_set() +void control_pgood_to_off_overvoltage() { - PowerPort port; + PowerPort port = {}; LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 0; - - // overtemp - load.state = LOAD_STATE_DISABLED; - dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_DIS_OVERTEMP); - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); - - // undertemp - load.state = LOAD_STATE_DISABLED; - dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP); - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); -} - -void off_low_soc_to_on_after_delay() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - load.state = LOAD_STATE_OFF_LOW_SOC; - load.usb_state = LOAD_STATE_OFF_LOW_SOC; - port.pos_current_limit = 10; - dev_stat.clear_error(ERR_ANY_ERROR); - - load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay + 1; - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state); - - load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay - 1; - load.state_machine(); - load.state_machine(); // call twice as it goes through disabled state - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); -} - -void off_overcurrent_to_on_after_delay() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - load.state = LOAD_STATE_OFF_OVERCURRENT; - load.usb_state = LOAD_STATE_ON; - port.pos_current_limit = 10; - dev_stat.clear_error(ERR_ANY_ERROR); - - load.overcurrent_timestamp = time(NULL) - load.overcurrent_recovery_delay + 1; - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overcurrent - - load.overcurrent_timestamp = time(NULL) - load.overcurrent_recovery_delay - 1; - load.state_machine(); - load.state_machine(); // call twice as it goes through disabled state - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); -} - -void off_overvoltage_to_on_at_lower_voltage() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 10; - load.state = LOAD_STATE_OFF_OVERVOLTAGE; - load.usb_state = LOAD_STATE_ON; - port.voltage = port.sink_voltage_max + 0.1; - dev_stat.clear_error(ERR_ANY_ERROR); - - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage - - port.voltage = port.sink_voltage_max - 0.1; // test hysteresis - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); - - port.voltage = port.sink_voltage_max - 0.6; - load.state_machine(); - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); -} - -void off_short_circuit_to_disabled() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 10; - load.state = LOAD_STATE_OFF_SHORT_CIRCUIT; - load.usb_state = LOAD_STATE_ON; - - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage - - load.enable = false; // this is like a manual reset - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); -} - -void on_to_off_low_soc_if_error_flag_set() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - load.state = LOAD_STATE_ON; - load.usb_state = LOAD_STATE_ON; - port.pos_current_limit = 0; - dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_UNDERVOLTAGE); - - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state); -} - -void on_to_off_bat_temp_if_error_flag_set() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 0; - - load.state = LOAD_STATE_ON; - load.usb_state = LOAD_STATE_ON; - dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_DIS_OVERTEMP); - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); - + load_init(&load); load.state = LOAD_STATE_ON; dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP); - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); -} - -void on_to_disabled_if_enable_false() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 10; - - load.state = LOAD_STATE_ON; - load.usb_state = LOAD_STATE_ON; - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); - - load.enable = false; - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); - - load.usb_enable = false; - load.state_machine(); - TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.usb_state); -} - -void control_off_overvoltage() -{ - PowerPort port; - LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 10; - port.voltage = 14.7; - load.state = LOAD_STATE_ON; - load.voltage_prev = port.voltage; - dev_stat.clear_error(ERR_ANY_ERROR); + port.voltage = 14.8; // increase debounce counter to 1 before limit for (int i = 0; i < CONTROL_FREQUENCY; i++) { load.control(); } - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); + TEST_ASSERT_EQUAL(true, load.pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated + load.control(); // once more - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); + TEST_ASSERT_EQUAL(false, load.pgood); TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); // deprecated } -void control_off_overcurrent() +void control_pgood_to_off_overcurrent() { - PowerPort port; + PowerPort port = {}; LoadOutput load(&port); - port.init_load(14.6); - port.pos_current_limit = 10; // this is currently not considered, as it is lower than hardware limit + load_init(&load); port.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately - port.voltage = 14; load.state = LOAD_STATE_ON; - load.voltage_prev = port.voltage; dev_stat.internal_temp = 25; - load.junction_temperature = 25; dev_stat.clear_error(ERR_ANY_ERROR); load.control(); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); + TEST_ASSERT_EQUAL(true, load.pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated // 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.control(); } - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); + TEST_ASSERT_EQUAL(false, load.pgood); TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); // deprecated } -void control_off_voltage_dip() +void control_pgood_to_off_voltage_dip() { - PowerPort port; + PowerPort port = {}; LoadOutput load(&port); - port.init_load(14.6); - port.current = 0; + load_init(&load); load.state = LOAD_STATE_ON; - load.junction_temperature = 25; dev_stat.internal_temp = 25; dev_stat.clear_error(ERR_ANY_ERROR); - port.voltage = 14; - load.voltage_prev = port.voltage; load.control(); - TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); + TEST_ASSERT_EQUAL(true, load.pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated - port.voltage = 14 * 0.74; + load.stop(ERR_LOAD_VOLTAGE_DIP); load.control(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); + TEST_ASSERT_EQUAL(false, load.pgood); TEST_ASSERT_EQUAL(ERR_LOAD_VOLTAGE_DIP, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); // deprecated } -void control_off_temperature() +void control_pgood_to_off_int_temp() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + load.state = LOAD_STATE_ON; + dev_stat.internal_temp = 25; + dev_stat.clear_error(ERR_ANY_ERROR); + + load.control(); + TEST_ASSERT_EQUAL(true, load.pgood); + dev_stat.set_error(ERR_INT_OVERTEMP); + load.control(); + TEST_ASSERT_EQUAL(false, load.pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); // deprecated +} + +void control_pgood_to_off_bat_temp() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + + // overtemp + load.state = LOAD_STATE_ON; + load.usb_state = LOAD_STATE_ON; + dev_stat.clear_error(ERR_ANY_ERROR); + dev_stat.set_error(ERR_BAT_DIS_OVERTEMP); + load.control(); + TEST_ASSERT_EQUAL(false, load.pgood); + TEST_ASSERT_EQUAL(false, load.usb_pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); // deprecated + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); // deprecated + + // undertemp + load.state = LOAD_STATE_ON; + load.usb_state = LOAD_STATE_ON; + dev_stat.clear_error(ERR_ANY_ERROR); + dev_stat.set_error(ERR_BAT_DIS_UNDERTEMP); + load.control(); + TEST_ASSERT_EQUAL(false, load.pgood); + TEST_ASSERT_EQUAL(false, load.usb_pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); // deprecated + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.usb_state); // deprecated +} + +void control_pgood_to_off_low_soc() { PowerPort port; LoadOutput load(&port); - port.init_load(14.6); - port.current = 0; - load.voltage_prev = port.voltage; load.state = LOAD_STATE_ON; dev_stat.internal_temp = 25; - load.junction_temperature = 25; dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_INT_OVERTEMP); load.control(); - TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); + TEST_ASSERT_EQUAL(true, load.pgood); + dev_stat.set_error(ERR_BAT_UNDERVOLTAGE); + load.control(); + TEST_ASSERT_EQUAL(false, load.pgood); + TEST_ASSERT_EQUAL(ERR_LOAD_LOW_SOC | ERR_BAT_UNDERVOLTAGE, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); // deprecated +} + +void control_pgood_to_off_if_enable_false() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + dev_stat.clear_error(ERR_ANY_ERROR); + + load.state = LOAD_STATE_ON; + load.control(); + TEST_ASSERT_EQUAL(true, load.pgood); + TEST_ASSERT_EQUAL(true, load.usb_pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); + + load.enable = false; + load.control(); + TEST_ASSERT_EQUAL(false, load.pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); + + load.usb_enable = false; + load.control(); + TEST_ASSERT_EQUAL(false, load.usb_pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.usb_state); +} + +void control_off_low_soc_to_on_after_delay() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + load.state = LOAD_STATE_OFF_LOW_SOC; + load.usb_state = LOAD_STATE_OFF_LOW_SOC; + dev_stat.error_flags = ERR_LOAD_LOW_SOC; + + load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay + 1; + load.control(); + TEST_ASSERT_EQUAL(ERR_LOAD_LOW_SOC, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.state); // deprecated + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_LOW_SOC, load.usb_state); // deprecated + + load.lvd_timestamp = time(NULL) - load.lvd_recovery_delay - 1; + load.control(); + TEST_ASSERT_EQUAL(0, dev_stat.error_flags); + TEST_ASSERT_EQUAL(true, load.pgood); + TEST_ASSERT_EQUAL(true, load.usb_pgood); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // deprecated +} + +void control_off_overcurrent_to_on_after_delay() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + load.state = LOAD_STATE_OFF_OVERCURRENT; + load.usb_state = LOAD_STATE_ON; + dev_stat.error_flags = ERR_LOAD_OVERCURRENT; + + load.oc_timestamp = time(NULL) - load.oc_recovery_delay + 1; + load.control(); + TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overcurrent + + load.oc_timestamp = time(NULL) - load.oc_recovery_delay - 1; + load.control(); + TEST_ASSERT_EQUAL(0, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // deprecated +} + +void control_off_overvoltage_to_on_at_lower_voltage() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + load.state = LOAD_STATE_OFF_OVERVOLTAGE; + load.usb_state = LOAD_STATE_ON; + port.voltage = port.sink_voltage_max + 0.1; + dev_stat.error_flags = ERR_LOAD_OVERVOLTAGE; + + load.control(); + TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage + + port.voltage = port.sink_voltage_max - 0.1; // test hysteresis + load.control(); + TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); + + port.voltage = port.sink_voltage_max - 0.6; + load.control(); + TEST_ASSERT_EQUAL(0, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // deprecated +} + +void control_off_short_circuit_flag_reset() +{ + PowerPort port = {}; + LoadOutput load(&port); + load_init(&load); + load.state = LOAD_STATE_OFF_SHORT_CIRCUIT; + load.usb_state = LOAD_STATE_ON; + dev_stat.error_flags = ERR_LOAD_SHORT_CIRCUIT; + + load.control(); + TEST_ASSERT_EQUAL(ERR_LOAD_SHORT_CIRCUIT, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_OFF_SHORT_CIRCUIT, load.state); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); // not affected by overvoltage + + load.enable = false; // this is like a manual reset + load.control(); + TEST_ASSERT_EQUAL(0, dev_stat.error_flags); + TEST_ASSERT_EQUAL(LOAD_STATE_DISABLED, load.state); + TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.usb_state); } void load_tests() { UNITY_BEGIN(); - // state_machine tests - RUN_TEST(disabled_to_on_if_everything_fine); - RUN_TEST(disabled_to_off_low_soc_if_error_flag_set); - RUN_TEST(disabled_to_off_bat_temp_if_error_flag_set); - RUN_TEST(off_low_soc_to_on_after_delay); - RUN_TEST(off_overcurrent_to_on_after_delay); - RUN_TEST(off_overvoltage_to_on_at_lower_voltage); - RUN_TEST(off_short_circuit_to_disabled); - RUN_TEST(on_to_off_low_soc_if_error_flag_set); - RUN_TEST(on_to_off_bat_temp_if_error_flag_set); - RUN_TEST(on_to_disabled_if_enable_false); - // control tests - RUN_TEST(control_off_overvoltage); - RUN_TEST(control_off_overcurrent); - RUN_TEST(control_off_voltage_dip); - RUN_TEST(control_off_temperature); + 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_low_soc_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); // ToDo: What to do if port current is above the limit, but the hardware can still handle it?