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
This commit is contained in:
+72
-7
@@ -20,10 +20,68 @@
|
||||
#include "log.h"
|
||||
#include <time.h>
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user