From 328215dc8ea879e61fa23bccd08443cc303e53ea Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Thu, 7 Nov 2019 15:53:37 +0100 Subject: [PATCH] Load overvoltage detection corrected and error flag comments added --- src/device_status.h | 111 ++++++++++++++++++++++++++++++++++++-------- src/load.cpp | 30 +++++++++--- test/tests_load.cpp | 14 ++++-- 3 files changed, 123 insertions(+), 32 deletions(-) diff --git a/src/device_status.h b/src/device_status.h index cd3c75b..d0f62db 100644 --- a/src/device_status.h +++ b/src/device_status.h @@ -31,28 +31,99 @@ #include /** 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 ErrorFlag { - ERR_BAT_UNDERVOLTAGE = 1U << 0 , ///< Battery voltage too low - ERR_BAT_OVERVOLTAGE = 1U << 1, ///< Battery voltage too high - ERR_BAT_DIS_OVERCURRENT = 1U << 2, ///< Battery discharge overcurrent - ERR_BAT_CHG_OVERCURRENT = 1U << 3, ///< Battery charge overcurrent - ERR_BAT_DIS_UNDERTEMP = 1U << 4, ///< Temperature below discharge minimum limit - ERR_BAT_DIS_OVERTEMP = 1U << 5, ///< Temperature above discharge maximum limit - ERR_BAT_CHG_UNDERTEMP = 1U << 6, ///< Temperature below charge minimum limit - ERR_BAT_CHG_OVERTEMP = 1U << 7, ///< Temperature above charge maximum limit - ERR_LOAD_OVERVOLTAGE = 1U << 8, ///< To high voltage for load so that it was switched off - ERR_LOAD_OVERCURRENT = 1U << 9, ///< Long-term overcurrent of load port - ERR_LOAD_SHORT_CIRCUIT = 1U << 10, ///< Short circuit detected at load port - ERR_LOAD_VOLTAGE_DIP = 1U << 11, ///< Overcurrent identified via voltage dip (may be because of - ///< too small battery) - ERR_INT_OVERTEMP = 1U << 12, ///< Charge controller internal temperature too high - ERR_DCDC_HS_MOSFET_SHORT = 1U << 13, ///< Short-circuit in HS MOSFET + /** Battery voltage too low + * + * Set directly in ISR after ADC conversion finished, cleared in Charger::discharge_control() + */ + ERR_BAT_UNDERVOLTAGE = 1U << 0, - // we use this as mask to catch all error flags (up to 32 errors) + /** Battery voltage too high + * + * Set directly in ISR after ADC conversion finished, cleared in Charger::charge_control() + */ + ERR_BAT_OVERVOLTAGE = 1U << 1, + + /** Battery discharge overcurrent + * + * Not used yet + */ + ERR_BAT_DIS_OVERCURRENT = 1U << 2, + + /** Battery charge overcurrent + * + * Not used yet + */ + ERR_BAT_CHG_OVERCURRENT = 1U << 3, + + /** Temperature below discharge minimum limit + * + * Set and cleared in Charger::discharge_control + */ + ERR_BAT_DIS_UNDERTEMP = 1U << 4, + + /** Temperature above discharge maximum limit + * + * Set and cleared in Charger::discharge_control + */ + ERR_BAT_DIS_OVERTEMP = 1U << 5, + + /** Temperature below charge minimum limit + * + * Set and cleared in Charger::discharge_control + */ + ERR_BAT_CHG_UNDERTEMP = 1U << 6, + + /** Temperature above charge maximum limit + * + * Set and cleared in Charger::discharge_control + */ + ERR_BAT_CHG_OVERTEMP = 1U << 7, + + /** To high voltage for load so that it was switched off + * + * Set in LoadOutput::control() and cleared in LoadOutput::state_machine() + */ + ERR_LOAD_OVERVOLTAGE = 1U << 8, + + /** Long-term overcurrent of load port + * + * Set in LoadOutput::control() and cleared in LoadOutput::state_machine() + */ + ERR_LOAD_OVERCURRENT = 1U << 9, + + /** Short circuit detected at load port + * + * Set by LoadOutput::control() after overcurrent comparator triggered, cleared in + * LoadOutput::state_machine() + */ + ERR_LOAD_SHORT_CIRCUIT = 1U << 10, + + /** 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. + */ + ERR_LOAD_VOLTAGE_DIP = 1U << 11, + + /** Charge controller internal temperature too high + * + * Set and cleared by ADC update_measurements() + */ + ERR_INT_OVERTEMP = 1U << 12, + + /** Short-circuit in HS MOSFET + * + * Set in Dcdc::control() and never cleared + */ + ERR_DCDC_HS_MOSFET_SHORT = 1U << 13, + + /** Mask to catch all error flags (up to 32 errors) + */ ERR_ANY_ERROR = UINT32_MAX, }; @@ -100,20 +171,20 @@ public: /** * @brief sets one or more error flags in device state * @param e a single ErrorFlag or "bitwise ORed" ERR_XXX | ERR_YYY - */ + */ 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; } }; diff --git a/src/load.cpp b/src/load.cpp index a49f7b3..46e655d 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -261,7 +261,8 @@ void LoadOutput::state_machine() case LOAD_STATE_DISABLED: if (enable == true) { if (port->pos_current_limit > 0 && - !dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)) + !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; @@ -270,8 +271,13 @@ void LoadOutput::state_machine() if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) { state = LOAD_STATE_OFF_LOW_SOC; } - else { - // must be battery over/under temperature then + 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; } } @@ -289,10 +295,19 @@ void LoadOutput::state_machine() lvd_timestamp = time(NULL); state = LOAD_STATE_OFF_LOW_SOC; } - else { - // must be battery over/under temperature then + 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: @@ -328,8 +343,9 @@ void LoadOutput::state_machine() } break; case LOAD_STATE_OFF_TEMPERATURE: - if (!(dev_stat.has_error(ERR_INT_OVERTEMP)) && - port->pos_current_limit > 0) + 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; } diff --git a/test/tests_load.cpp b/test/tests_load.cpp index 2268fb7..21d3d11 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -40,16 +40,17 @@ void disabled_to_off_bat_temp_if_error_flag_set() port.pos_current_limit = 0; // overtemp + load.state = LOAD_STATE_DISABLED; dev_stat.clear_error(ERR_ANY_ERROR); - dev_stat.set_error(ERR_BAT_CHG_OVERTEMP); + 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_CHG_UNDERTEMP); + 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); @@ -63,6 +64,7 @@ void off_low_soc_to_on_after_delay() 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(); @@ -84,6 +86,7 @@ void off_overcurrent_to_on_after_delay() 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(); @@ -106,6 +109,7 @@ void off_overvoltage_to_on_at_lower_voltage() 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); @@ -149,7 +153,7 @@ void on_to_off_low_soc_if_error_flag_set() 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.clear_error(ERR_ANY_ERROR); dev_stat.set_error(ERR_BAT_UNDERVOLTAGE); load.state_machine(); @@ -166,7 +170,7 @@ void on_to_off_bat_temp_if_error_flag_set() load.state = LOAD_STATE_ON; load.usb_state = LOAD_STATE_ON; - dev_stat.clear_error(ERR_ANY_ERROR); + 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);