From 5a264e933273954aea6d8ac23d9fbe7089bcfe2c Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Mon, 27 May 2019 16:23:22 +0200 Subject: [PATCH] Load short circuit protection using analog COMP2 - Immediate load cut-off in case of current > 2x rated current (still to be fine-tuned) - Using built-in comparator of STM32 MCU - Currently only supported by PWM charge controller PCB, as it doesn't have an internal fuse --- src/load.cpp | 79 +++++++++++++++++++++++++++++++++++++++---- src/load.h | 6 ++-- src/pcbs/pcb_pwm_02.h | 1 + 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/load.cpp b/src/load.cpp index f3ccd9a..c97cec2 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -20,10 +20,68 @@ #include "log.h" #include +static void short_circuit_comp_init() +{ +#ifdef PIN_I_LOAD_COMP + MBED_ASSERT(PIN_I_LOAD_COMP == PB_4); + + // set GPIO pin to analog + RCC->IOPENR |= RCC_IOPENR_GPIOBEN; + GPIOB->MODER &= ~(GPIO_MODER_MODE4); + + // enable SYSCFG clock + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; + + // enable VREFINT buffer + SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP; + + // select PB4 as positive input + COMP2->CSR |= COMP_INPUT_PLUS_IO2; + + // select VREFINT divider as negative input + COMP2->CSR |= COMP_INPUT_MINUS_1_2VREFINT; + + // normal polarity + //COMP2->CSR |= COMP_CSR_COMP2POLARITY; + + // enable COMP2 + COMP2->CSR |= COMP_CSR_COMP2EN; + + // enable EXTI software interrupt / event + EXTI->IMR |= EXTI_IMR_IM22; + EXTI->EMR |= EXTI_EMR_EM22; + EXTI->RTSR |= EXTI_RTSR_RT22; + EXTI->FTSR |= EXTI_FTSR_FT22; + EXTI->SWIER |= EXTI_SWIER_SWI22; + + // 1 = second-highest priority of STM32L0/F0 + NVIC_SetPriority(ADC1_COMP_IRQn, 1); + NVIC_EnableIRQ(ADC1_COMP_IRQn); +#endif +} + +volatile bool short_circuit = false; + +#ifdef PIN_I_LOAD_COMP + +extern "C" void ADC1_COMP_IRQHandler(void) +{ + // interrupt called because of COMP2? + if (COMP2->CSR & COMP_CSR_COMP2VALUE) { + hw_load_switch(false); + hw_usb_out(false); + short_circuit = true; + } + + // clear interrupt flag + EXTI->PR |= EXTI_PR_PIF22; +} + +#endif // PIN_I_LOAD_COMP + void load_init(load_output_t *load) { load->current_max = LOAD_CURRENT_MAX; - //load->enabled = false; load->enabled_target = true; load->usb_enabled_target = true; hw_load_switch(false); @@ -31,6 +89,9 @@ void load_init(load_output_t *load) load->switch_state = LOAD_STATE_DISABLED; load->usb_state = LOAD_STATE_DISABLED; load->junction_temperature = 25; + + // analog comparator to detect short circuits and trigger immediate load switch-off + short_circuit_comp_init(); } @@ -85,19 +146,16 @@ void load_state_machine(load_output_t *load, bool source_enabled) case LOAD_STATE_DISABLED: if (source_enabled == true && load->enabled_target == true) { hw_load_switch(true); - //load->enabled = true; load->switch_state = LOAD_STATE_ON; } break; case LOAD_STATE_ON: if (load->enabled_target == false) { hw_load_switch(false); - //load->enabled = false; load->switch_state = LOAD_STATE_DISABLED; } else if (source_enabled == false) { hw_load_switch(false); - //load->enabled = false; load->switch_state = LOAD_STATE_OFF_LOW_SOC; } break; @@ -105,7 +163,6 @@ void load_state_machine(load_output_t *load, bool source_enabled) if (source_enabled == true) { if (load->enabled_target == true) { hw_load_switch(true); - //load->enabled = true; load->switch_state = LOAD_STATE_ON; } else { @@ -123,6 +180,12 @@ void load_state_machine(load_output_t *load, bool source_enabled) load->switch_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 (load->enabled_target == false) { + load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again + } + break; } usb_state_machine(load); @@ -134,6 +197,10 @@ extern float mcu_temp; // this function is called more often than the state machine void load_control(load_output_t *load) { + if (short_circuit) { + load->switch_state = LOAD_STATE_OFF_SHORT_CIRCUIT; + } + // junction temperature calculation model for overcurrent detection load->junction_temperature = load->junction_temperature + ( mcu_temp - load->junction_temperature + @@ -143,7 +210,6 @@ void load_control(load_output_t *load) if (load->junction_temperature > MOSFET_MAX_JUNCTION_TEMP) { hw_load_switch(false); hw_usb_out(false); - //load->enabled = false; load->switch_state = LOAD_STATE_OFF_OVERCURRENT; load->overcurrent_timestamp = time(NULL); } @@ -154,7 +220,6 @@ void load_control(load_output_t *load) if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag hw_load_switch(false); hw_usb_out(false); - //load->enabled = false; load->switch_state = LOAD_STATE_OFF_OVERVOLTAGE; load->overcurrent_timestamp = time(NULL); log_data.error_flags |= (1 << ERR_BAT_OVERVOLTAGE); diff --git a/src/load.h b/src/load.h index 288b899..9a756c5 100644 --- a/src/load.h +++ b/src/load.h @@ -35,7 +35,8 @@ enum load_state_t { 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_OVERVOLTAGE, ///< Off to protect loads (overrules target setting) + LOAD_STATE_OFF_SHORT_CIRCUIT ///< Off to protect charge controller (overrules target setting) }; /** Load output type @@ -54,12 +55,11 @@ typedef struct { float junction_temperature; ///< calculated using thermal model based on current and ambient temperature measurement (unit: °C) - //bool enabled; ///< actual status bool enabled_target; ///< target setting defined via communication port (overruled if battery is empty) bool usb_enabled_target; ///< same for USB output } load_output_t; -/** Initialize load_output_t struct +/** Initialize load_output_t struct and overcurrent / short circuit protection comparator (if existing) */ void load_init(load_output_t *load); diff --git a/src/pcbs/pcb_pwm_02.h b/src/pcbs/pcb_pwm_02.h index d19dcf4..6033d2f 100644 --- a/src/pcbs/pcb_pwm_02.h +++ b/src/pcbs/pcb_pwm_02.h @@ -40,6 +40,7 @@ #define PIN_LOAD_DIS PB_2 #define PIN_USB_PWR_DIS PB_5 +#define PIN_I_LOAD_COMP PB_4 #define PIN_REF_I_DCDC PA_4