diff --git a/src/load.cpp b/src/load.cpp index e89f5ac..f393013 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -200,6 +200,7 @@ LoadOutput::LoadOutput(PowerPort *pwr_port) : 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 short_circuit_comp_init(); @@ -341,8 +342,8 @@ void LoadOutput::control() } if (dev_stat.has_error(ERR_LOAD_OVERVOLTAGE) && - port->voltage < (port->sink_voltage_max - 0.5) && - port->voltage < (LOW_SIDE_VOLTAGE_MAX - 0.5)) + port->voltage < (port->sink_voltage_max - ov_hysteresis) && + port->voltage < (LOW_SIDE_VOLTAGE_MAX - ov_hysteresis)) { dev_stat.clear_error(ERR_LOAD_OVERVOLTAGE); } diff --git a/src/load.h b/src/load.h index e73be11..be22633 100644 --- a/src/load.h +++ b/src/load.h @@ -111,6 +111,7 @@ public: float junction_temperature; ///< calculated using thermal model based on current and ambient ///< temperature measurement (unit: °C) + float ov_hysteresis; ///< Hysteresis to switch back on after an overvoltage event private: int determine_load_state(); diff --git a/src/main.cpp b/src/main.cpp index 5194a4c..e8b6863 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,7 +28,7 @@ #include "config.h" // user-specific configuration #include "half_bridge.h" // PWM generation for DC/DC converter -#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. +#include "hardware.h" // hardware-related functions like watchdog, etc. #include "dcdc.h" // DC/DC converter control (hardware independent) #include "pwm_switch.h" // PWM charge controller #include "bat_charger.h" // battery settings and charger state machine @@ -133,13 +133,9 @@ int main() control_timer_start(CONTROL_FREQUENCY); wait(0.1); // necessary to prevent MCU from randomly getting stuck here if PV panel is connected before battery - - sleep_manager_lock_deep_sleep(); // required to have sleep returning. - /* - The mBed Serial class calls this internal during "attach", this is why it work with ThingSet Serial enabled even - without this statement. Might be an issue of the particular STM32F072 mBed code or may affect - also STM32L073 platforms on mBed - */ + // The Mbed Serial class calls sleep_manager_lock_deep_sleep() internally during attach. If no + // Serial is enabled, sleep does not always return in STM32F072, so we lock deep sleep manually + sleep_manager_lock_deep_sleep(); // the main loop is suitable for slow tasks like communication (even blocking wait allowed) time_t last_call = timestamp; @@ -149,9 +145,8 @@ int main() uext.process_asap(); time_t now = timestamp; - if (now >= last_call + 1 || now < last_call) { // called once per second (or slower if blocking wait occured somewhere) - - //printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode); + if (now >= last_call + 1 || now < last_call) { + // called once per second (or slower if blocking wait occured somewhere) charger.discharge_control(&bat_conf); charger.charge_control(&bat_conf); @@ -163,10 +158,6 @@ int main() bat_terminal.pass_voltage_targets(&pwm_port_int); #endif - // 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); - eeprom_update(); leds_update_1s(); @@ -193,6 +184,9 @@ void system_control() // convert ADC readings to meaningful measurement values update_measurements(); + // alerts should trigger only for transients, so update based on actual voltage + adc_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8); + #if FEATURE_PWM_SWITCH ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal); pwm_switch.control(); diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index f7d1c7a..ff2f802 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -83,8 +83,11 @@ extern "C" void TIM3_IRQHandler(void) { TIM3->SR &= ~TIM_SR_UIF; // clear update interrupt flag - // turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms - adc_upper_alert_inhibit(ADC_POS_V_BAT, 10); + if ((int)TIM3->CCR4 < _pwm_resolution) { + // turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms + // at each rising edge if switch is not continuously on + adc_upper_alert_inhibit(ADC_POS_V_BAT, 10); + } } void pwm_signal_set_duty_cycle(float duty) @@ -180,6 +183,7 @@ void PwmSwitch::control() if (_pwm_active) { if (port_int->pos_current_limit == 0 || terminal->neg_current_limit == 0 || terminal->current > 0 // discharging battery into solar panel --> stop + || port_int->voltage < 9.0 // not enough voltage for MOSFET drivers anymore || enabled == false) { pwm_signal_stop(); diff --git a/test/tests_load.cpp b/test/tests_load.cpp index a1d77ff..e6a4922 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -37,7 +37,7 @@ void control_pgood_to_off_overvoltage() load_init(&load); load.state = LOAD_STATE_ON; dev_stat.clear_error(ERR_ANY_ERROR); - port.voltage = 14.8; + port.voltage = port.sink_voltage_max + 0.6; // increase debounce counter to 1 before limit for (int i = 0; i < CONTROL_FREQUENCY; i++) { @@ -252,7 +252,7 @@ void control_off_overvoltage_to_on_at_lower_voltage() 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; + port.voltage = port.sink_voltage_max - load.ov_hysteresis - 0.1; load.control(); TEST_ASSERT_EQUAL(0, dev_stat.error_flags); TEST_ASSERT_EQUAL(LOAD_STATE_ON, load.state); // deprecated