From bec02cb25b6903b37077425fbe33410c91bccfa1 Mon Sep 17 00:00:00 2001 From: hogthrob Date: Mon, 4 Nov 2019 09:19:06 +0100 Subject: [PATCH] Switched to use power of 2 error numbers (#72) In addition to the normal error flags we now have ERR_ANY_ERROR which may be used to clear all errors at once or to use in has_error to see if there is any error flag set. --- src/dcdc.cpp | 2 +- src/device_status.h | 55 +++++++++++++++++++++++++++++++-------------- src/load.cpp | 26 ++++++++++----------- test/tests_load.cpp | 26 ++++++++++----------- 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 3288f3f..87a60fb 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -199,7 +199,7 @@ void Dcdc::control() // what to do... (e.g. call dcdc_self_destruction) current_debounce_counter++; if (current_debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag - dev_stat.error_flags |= (1 << ERR_DCDC_HS_MOSFET_SHORT); + dev_stat.set_error(ERR_DCDC_HS_MOSFET_SHORT); } } else { // hardware is fine diff --git a/src/device_status.h b/src/device_status.h index e3d99fe..cd3c75b 100644 --- a/src/device_status.h +++ b/src/device_status.h @@ -31,23 +31,29 @@ #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 = 0, ///< Battery voltage too low - ERR_BAT_OVERVOLTAGE = 1, ///< Battery voltage too high - ERR_BAT_DIS_OVERCURRENT = 2, ///< Battery discharge overcurrent - ERR_BAT_CHG_OVERCURRENT = 3, ///< Battery charge overcurrent - ERR_BAT_DIS_UNDERTEMP = 4, ///< Temperature below discharge minimum limit - ERR_BAT_DIS_OVERTEMP = 5, ///< Temperature above discharge maximum limit - ERR_BAT_CHG_UNDERTEMP = 6, ///< Temperature below charge minimum limit - ERR_BAT_CHG_OVERTEMP = 7, ///< Temperature above charge maximum limit - ERR_LOAD_OVERVOLTAGE = 8, ///< To high voltage for load so that it was switched off - ERR_LOAD_OVERCURRENT = 9, ///< Long-term overcurrent of load port - ERR_LOAD_SHORT_CIRCUIT = 10, ///< Short circuit detected at load port - ERR_LOAD_VOLTAGE_DIP = 11, ///< Overcurrent identified via voltage dip (may be because of + 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 = 12, ///< Charge controller internal temperature too high - ERR_DCDC_HS_MOSFET_SHORT = 13, ///< Short-circuit in HS MOSFET + ERR_INT_OVERTEMP = 1U << 12, ///< Charge controller internal temperature too high + ERR_DCDC_HS_MOSFET_SHORT = 1U << 13, ///< Short-circuit in HS MOSFET + + // we use this as mask to catch all error flags (up to 32 errors) + ERR_ANY_ERROR = UINT32_MAX, }; /** Device Status data @@ -91,9 +97,24 @@ public: uint32_t error_flags; ///< Currently detected errors float internal_temp; ///< Internal temperature (measured in MCU) - void set_error(ErrorFlag e) { error_flags |= (1U << e); } - void clear_error(ErrorFlag e) { error_flags &= ~(1U << e); } - bool has_error(ErrorFlag e) { return (error_flags & (1U << e)) != 0; } + /** + * @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; } }; #endif /* DEVICE_STATUS_H */ diff --git a/src/load.cpp b/src/load.cpp index a830bec..c5e2f34 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -265,7 +265,7 @@ void LoadOutput::state_machine() state = LOAD_STATE_ON; } else { - if (dev_stat.error_flags & (1 << ERR_BAT_UNDERVOLTAGE)) { + if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) { state = LOAD_STATE_OFF_LOW_SOC; } else { @@ -283,7 +283,7 @@ void LoadOutput::state_machine() 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.error_flags & (1 << ERR_BAT_UNDERVOLTAGE)) { + if (dev_stat.has_error(ERR_BAT_UNDERVOLTAGE)) { lvd_timestamp = time(NULL); state = LOAD_STATE_OFF_LOW_SOC; } @@ -296,7 +296,7 @@ void LoadOutput::state_machine() case LOAD_STATE_OFF_LOW_SOC: // wait at least configured time if (time(NULL) - lvd_timestamp > lvd_recovery_delay && - !(dev_stat.error_flags & (1 << ERR_BAT_UNDERVOLTAGE))) + !(dev_stat.has_error(ERR_BAT_UNDERVOLTAGE))) { state = LOAD_STATE_DISABLED; // switch to normal mode again } @@ -304,8 +304,8 @@ void LoadOutput::state_machine() case LOAD_STATE_OFF_OVERCURRENT: // wait configured time if (time(NULL) - overcurrent_timestamp > overcurrent_recovery_delay) { - dev_stat.error_flags &= ~(1 << ERR_LOAD_OVERCURRENT); - dev_stat.error_flags &= ~(1 << ERR_LOAD_VOLTAGE_DIP); + 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; @@ -313,7 +313,7 @@ void LoadOutput::state_machine() if (port->voltage < (port->sink_voltage_max - 0.5) && port->voltage < (LOW_SIDE_VOLTAGE_MAX - 0.5)) { - dev_stat.error_flags &= ~(1 << ERR_LOAD_OVERVOLTAGE); + dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE); state = LOAD_STATE_DISABLED; // switch to normal mode again } break; @@ -321,12 +321,12 @@ void LoadOutput::state_machine() // stay here until the charge controller is reset or load is switched off remotely if (enable == false) { short_circuit = false; - dev_stat.error_flags &= ~(1 << ERR_LOAD_SHORT_CIRCUIT); + 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.error_flags & (1U << ERR_INT_OVERTEMP)) && + if (!(dev_stat.has_error(ERR_INT_OVERTEMP)) && port->pos_current_limit > 0) { state = LOAD_STATE_DISABLED; @@ -343,7 +343,7 @@ void LoadOutput::control() if (short_circuit) { if (state != LOAD_STATE_OFF_SHORT_CIRCUIT) { state = LOAD_STATE_OFF_SHORT_CIRCUIT; - dev_stat.error_flags |= (1 << ERR_LOAD_SHORT_CIRCUIT); + dev_stat.set_error(ERR_LOAD_SHORT_CIRCUIT); print_error("Load short circuit detected\n"); } return; @@ -363,14 +363,14 @@ void LoadOutput::control() switch_set(false); leds_flicker(LED_LOAD); state = LOAD_STATE_OFF_OVERCURRENT; - dev_stat.error_flags |= (1 << ERR_LOAD_OVERCURRENT); + dev_stat.set_error(ERR_LOAD_OVERCURRENT); print_error("Load overcurrent detected\n"); overcurrent_timestamp = time(NULL); return; } // internal overtemperature - if (dev_stat.error_flags & (1U << ERR_INT_OVERTEMP)) { + if (dev_stat.has_error(ERR_INT_OVERTEMP)) { switch_set(false); state = LOAD_STATE_OFF_TEMPERATURE; print_error("Device overtemperature detected\n"); @@ -382,7 +382,7 @@ void LoadOutput::control() switch_set(false); leds_flicker(LED_LOAD); state = LOAD_STATE_OFF_OVERCURRENT; - dev_stat.error_flags |= (1 << ERR_LOAD_VOLTAGE_DIP); + dev_stat.set_error(ERR_LOAD_VOLTAGE_DIP); print_error("Load voltage dip detected\n"); overcurrent_timestamp = time(NULL); return; @@ -399,7 +399,7 @@ void LoadOutput::control() print_error("Load overvoltage detected\n"); state = LOAD_STATE_OFF_OVERVOLTAGE; overcurrent_timestamp = time(NULL); - dev_stat.error_flags |= (1 << ERR_LOAD_OVERVOLTAGE); + dev_stat.set_error(ERR_LOAD_OVERVOLTAGE); return; } } diff --git a/test/tests_load.cpp b/test/tests_load.cpp index 028fb57..2268fb7 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -25,7 +25,7 @@ void disabled_to_off_low_soc_if_error_flag_set() LoadOutput load(&port); port.init_load(14.6); port.pos_current_limit = 0; - dev_stat.error_flags = 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); @@ -40,7 +40,7 @@ void disabled_to_off_bat_temp_if_error_flag_set() port.pos_current_limit = 0; // overtemp - dev_stat.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); dev_stat.set_error(ERR_BAT_CHG_OVERTEMP); load.state_machine(); TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); @@ -48,7 +48,7 @@ void disabled_to_off_bat_temp_if_error_flag_set() // undertemp load.state = LOAD_STATE_DISABLED; - dev_stat.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); dev_stat.set_error(ERR_BAT_CHG_UNDERTEMP); load.state_machine(); TEST_ASSERT_EQUAL(LOAD_STATE_OFF_TEMPERATURE, load.state); @@ -149,7 +149,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.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); dev_stat.set_error(ERR_BAT_UNDERVOLTAGE); load.state_machine(); @@ -166,14 +166,14 @@ void on_to_off_bat_temp_if_error_flag_set() load.state = LOAD_STATE_ON; load.usb_state = LOAD_STATE_ON; - dev_stat.error_flags = 0; + 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.state = LOAD_STATE_ON; - dev_stat.error_flags = 0; + 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); @@ -212,7 +212,7 @@ void control_off_overvoltage() port.voltage = 14.7; load.state = LOAD_STATE_ON; load.voltage_prev = port.voltage; - dev_stat.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); // increase debounce counter to 1 before limit for (int i = 0; i < CONTROL_FREQUENCY; i++) { @@ -222,7 +222,7 @@ void control_off_overvoltage() TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); load.control(); // once more TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERVOLTAGE, load.state); - TEST_ASSERT_EQUAL(1 << ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags); + TEST_ASSERT_EQUAL(ERR_LOAD_OVERVOLTAGE, dev_stat.error_flags); } void control_off_overcurrent() @@ -237,7 +237,7 @@ void control_off_overcurrent() load.voltage_prev = port.voltage; dev_stat.internal_temp = 25; load.junction_temperature = 25; - dev_stat.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); load.control(); TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); @@ -248,7 +248,7 @@ void control_off_overcurrent() load.control(); } TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); - TEST_ASSERT_EQUAL(1 << ERR_LOAD_OVERCURRENT, dev_stat.error_flags); + TEST_ASSERT_EQUAL(ERR_LOAD_OVERCURRENT, dev_stat.error_flags); } void control_off_voltage_dip() @@ -260,7 +260,7 @@ void control_off_voltage_dip() load.state = LOAD_STATE_ON; load.junction_temperature = 25; dev_stat.internal_temp = 25; - dev_stat.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); port.voltage = 14; load.voltage_prev = port.voltage; @@ -270,7 +270,7 @@ void control_off_voltage_dip() port.voltage = 14 * 0.74; load.control(); TEST_ASSERT_EQUAL(LOAD_STATE_OFF_OVERCURRENT, load.state); - TEST_ASSERT_EQUAL(1 << ERR_LOAD_VOLTAGE_DIP, dev_stat.error_flags); + TEST_ASSERT_EQUAL(ERR_LOAD_VOLTAGE_DIP, dev_stat.error_flags); } void control_off_temperature() @@ -283,7 +283,7 @@ void control_off_temperature() load.state = LOAD_STATE_ON; dev_stat.internal_temp = 25; load.junction_temperature = 25; - dev_stat.error_flags = 0; + dev_stat.clear_error(ERR_ANY_ERROR); dev_stat.set_error(ERR_INT_OVERTEMP); load.control();