Faster load short circuit protection via LPTIM

The PWM charge controllers don't have a fuse and need a reliable load short
circuit protection implemented in the microcontroller. The comparator
monitoring the load current now triggers LPTIM connected to the load
disable pin. This allows to switch off the load with a defined time delay
and glitch filter.

During tests, switching off the load in an ISR triggered by COMP2 did
not lead to reliable delays (especially in case of wake-up from sleep),
hence this alternative solution using LPTIM was implemented.

The load switch functions were moved from hardware.cpp to load.cpp.
This commit is contained in:
Martin Jaeger
2019-06-12 09:45:19 +02:00
parent f0301625d7
commit 0e91c834ac
5 changed files with 194 additions and 115 deletions
+5 -3
View File
@@ -27,9 +27,11 @@
//#define DCDC_MODE_INIT MODE_NANOGRID
// basic battery configuration
#define BATTERY_TYPE BAT_TYPE_GEL // GEL most suitable for general batteries (see battery.h for other types)
#define BATTERY_NUM_CELLS 6 // For lead-acid batteries: 6 for 12V system, 12 for 24V system
#define BATTERY_CAPACITY 40 // Cell capacity or sum of parallel cells capacity (Ah)
#define BATTERY_TYPE BAT_TYPE_GEL ///< GEL most suitable for general batteries (see battery.h for other types)
#define BATTERY_NUM_CELLS 6 ///< For lead-acid batteries: 6 for 12V system, 12 for 24V system
#define BATTERY_CAPACITY 40 ///< Cell capacity or sum of parallel cells capacity (Ah)
#define LOAD_OVERCURRENT_RECOVERY_TIMEOUT (5*30) ///< Seconds to wait until load is switched back on after an overcurrent event
#define DEVICE_ID 12345678
+58 -84
View File
@@ -18,59 +18,13 @@
#include "hardware.h"
#include "pcb.h"
#include "load.h"
#include "mbed.h"
#include "half_bridge.h"
#include "us_ticker_data.h"
#include "leds.h"
#ifdef PIN_LOAD_EN
DigitalOut load_enable(PIN_LOAD_EN);
#else
DigitalOut load_disable(PIN_LOAD_DIS);
#endif
#ifdef PIN_USB_PWR_EN
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
#endif
#ifdef PIN_USB_PWR_DIS
DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS);
#endif
#ifdef PIN_UEXT_DIS
DigitalOut uext_dis(PIN_UEXT_DIS);
#endif
//----------------------------------------------------------------------------
void hw_load_switch(bool enabled)
{
#ifdef PIN_LOAD_EN
if (enabled) load_enable = 1;
else load_enable = 0;
#endif
#ifdef PIN_LOAD_DIS
printf("Load enabled = %d\n", enabled);
if (enabled) load_disable = 0;
else load_disable = 1;
#endif
leds_set_load(enabled);
}
//----------------------------------------------------------------------------
void hw_usb_out(bool enabled)
{
#ifdef PIN_USB_PWR_EN
if (enabled) usb_pwr_en = 1;
else usb_pwr_en = 0;
#endif
#ifdef PIN_USB_PWR_DIS
if (enabled) usb_pwr_dis = 0;
else usb_pwr_dis = 1;
#endif
}
#if defined(STM32F0)
void control_timer_start(int freq_Hz) // max. 10 kHz
@@ -198,8 +152,8 @@ void init_watchdog(float timeout) {
void mbed_die(void)
{
half_bridge_stop();
hw_load_switch(false);
hw_usb_out(false);
load_switch_set(false);
load_usb_set(false);
leds_init(false);
@@ -219,70 +173,90 @@ void mbed_die(void)
#elif defined(STM32L0)
#define SYS_MEM_START 0x1FF00000
#define SRAM_END 0X20004FFF // 20k
#define FLASH_LAST_PAGE 0x0802FF80 // start address of last page (192 kbyte cat 5 devices)
#endif
#define MAGIC_CODE 0xDEADBEEF
#define MAGIC_CODE_ADDR (SRAM_END - 0xF) // where the magic code is stored
void start_dfu_bootloader()
{
// place magic code at end of RAM and initiate reset
*((unsigned long *)(SRAM_END - 0xF)) = MAGIC_CODE;
*((uint32_t *)(MAGIC_CODE_ADDR)) = 0xDEADBEEF;
NVIC_SystemReset();
}
// https://stackoverflow.com/questions/42020893/stm32l073rz-rev-z-iap-jump-to-bootloader-system-memory
// This function should be called at the beginning of SystemInit in system_clock.c (mbed target directory)
// As mbed does not provide this functionality, you have to patch the file manually
extern "C" void system_init_hook()
{
if (*((unsigned long *)(SRAM_END - 0xF)) == MAGIC_CODE) {
if (*((uint32_t *)(MAGIC_CODE_ADDR)) == 0xDEADBEEF) {
*((unsigned long *)(SRAM_END - 0xF)) = 0xCAFEFEED; // reset the trigger
uint32_t jump_address;
// jump function pointer to be initialized and called later
void (*SysMemJump)(void);
#ifdef STM32L0
// Trying to implement this solution, but doesn't work properly...
// https://stackoverflow.com/questions/42020893/stm32l073rz-rev-z-iap-jump-to-bootloader-system-memory
if (*((uint32_t *)(MAGIC_CODE_ADDR + 4)) == 0) { // first jump
// TODO for STM32L0
/*
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = Data_Address;
EraseInitStruct.NbPages = 1;
First_jump = *(__IO uint32_t *)(Data_Address);
if (First_jump == 0) {
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Data_Address, 0xAAAAAAAA);
HAL_FLASH_Lock();
*((uint32_t *)(MAGIC_CODE_ADDR + 4)) = 0xAAAAAAAA;
// Reinitialize the Stack pointer and jump to application address
//JumpAddress = *(__IO uint32_t *)(0x1FF00004);
SysMemJump = (void (*)(void)) (*((uint32_t *) (SYS_MEM_START + 4)));
jump_address = *((uint32_t *) (SYS_MEM_START + 4));
}
else {
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);
HAL_FLASH_Lock();
*((uint32_t *)(MAGIC_CODE_ADDR)) = 0x00000000; // reset the first trigger
*((uint32_t *)(MAGIC_CODE_ADDR + 4)) = 0x00000000; // reset the second trigger
// Reinitialize the Stack pointer and jump to application address
//JumpAddress = (0x1FF00369);
SysMemJump = (void (*)(void)) (*((uint32_t *) (0x1FF00369)));
jump_address = (*((uint32_t *) (0x1FF00369)));
}
*/
//__set_MSP(0x20002250); // set stack pointer for STM32F0
#else // STM32F0 and possibly others
*((uint32_t *)(MAGIC_CODE_ADDR)) = 0x00000000; // reset the trigger
jump_address = (*((uint32_t *) (SYS_MEM_START + 4)));
#endif
// reinitialize stack pointer
__set_MSP(SYS_MEM_START);
//__set_MSP(*(__IO uint32_t *)(0x1FF00000));
// point the PC to the System Memory reset vector (+4)
SysMemJump = (void (*)(void)) (*((uint32_t *) (SYS_MEM_START + 4))); // for STM32F0
SysMemJump();
// jump to specified address
void (*jump)(void);
jump = (void (*)(void)) jump_address;
jump();
while (42);
while (42); // should never be reached
}
}
/*
// alternative approach suggested by STM (without reset and magic bytes)
void BootLoaderInit(uint32_t BootLoaderStatus)
{
void (*SysMemJump)(void);
SysMemJump = (void (*)(void)) (*((uint32_t *) (SYS_MEM_START + 4))); // for STM32F0
if (BootLoaderStatus == 1) {
printf("Trying to jump to bootloader!\n");
// shut down any tasks running
HAL_RCC_DeInit();
SysTick->CTRL = 0; // reset Systick Timer
SysTick->LOAD = 0;
SysTick->VAL = 0;
//__set_PRIMASK(1); // disable interrupts
__set_MSP(SYS_MEM_START);
SysMemJump();
while(42);
}
}
*/
#else
// dummy functions for unit tests
+1 -9
View File
@@ -20,17 +20,9 @@
/** @file
*
* @brief
* Hardware-specific functions like timers, output switches, bootloader
* Hardware-specific functions like timers, watchdog, bootloader
*/
/** Enable/disable load switch
*/
void hw_load_switch(bool enabled);
/** Enable/disable USB output
*/
void hw_usb_out(bool enabled);
/** Initialization of IWDG
*
* @param timeout Timeout in seconds
+122 -19
View File
@@ -14,12 +14,67 @@
* limitations under the License.
*/
#ifndef UNIT_TEST
#include "load.h"
#include "pcb.h"
#include "config.h"
#include "hardware.h"
#include "leds.h"
#include "log.h"
#include <time.h>
#if defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2
static void lptim_init()
{
// Enable peripheral clock of GPIOB
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
// Enable LPTIM clock
RCC->APB1ENR |= RCC_APB1ENR_LPTIM1EN;
// Select alternate function mode on PB2 (first bit _1 = 1, second bit _0 = 0)
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE2)) | GPIO_MODER_MODE2_1;
// Select AF2 (LPTIM_OUT) on PB2
GPIOB->AFR[0] |= 0x2U << GPIO_AFRL_AFRL2_Pos;
// Set prescaler to 32 (resulting in 1 MHz timer frequency)
LPTIM1->CFGR |= 0x5U << LPTIM_CFGR_PRESC_Pos;
// Enable trigger (rising edge)
LPTIM1->CFGR |= LPTIM_CFGR_TRIGEN_0;
// Select trigger 7 (COMP2_OUT)
LPTIM1->CFGR |= 0x7U << LPTIM_CFGR_TRIGSEL_Pos;
//LPTIM1->CFGR |= LPTIM_CFGR_WAVPOL;
LPTIM1->CFGR |= LPTIM_CFGR_PRELOAD;
// Glitch filter of 8 cycles
LPTIM1->CFGR |= LPTIM_CFGR_TRGFLT_0 | LPTIM_CFGR_TRGFLT_1;
// Enable set-once mode
LPTIM1->CFGR |= LPTIM_CFGR_WAVE;
// Enable timer (must be done *before* changing ARR or CMP, but *after*
// changing CFGR)
LPTIM1->CR |= LPTIM_CR_ENABLE;
// Auto Reload Register
LPTIM1->ARR = 1000;
// Set load switch-off delay in microseconds
// (actually takes approx. 4 us longer than this setting)
LPTIM1->CMP = 10;
// Continuous mode
//LPTIM1->CR |= LPTIM_CR_CNTSTRT;
//LPTIM1->CR |= LPTIM_CR_SNGSTRT;
}
#endif
static void short_circuit_comp_init()
{
#ifdef PIN_I_LOAD_COMP
@@ -39,11 +94,17 @@ static void short_circuit_comp_init()
COMP2->CSR |= COMP_INPUT_PLUS_IO2;
// select VREFINT divider as negative input
COMP2->CSR |= COMP_INPUT_MINUS_1_2VREFINT;
COMP2->CSR |= COMP_INPUT_MINUS_VREFINT;
// propagate comparator value to LPTIM input
COMP2->CSR |= COMP_CSR_COMP2LPTIM1IN1;
// normal polarity
//COMP2->CSR |= COMP_CSR_COMP2POLARITY;
// set high-speed mode (1.2us instead of 2.5us propagation delay, but 3.5uA instead of 0.5uA current consumption)
//COMP2->CSR |= COMP_CSR_COMP2SPEED;
// enable COMP2
COMP2->CSR |= COMP_CSR_COMP2EN;
@@ -68,8 +129,9 @@ 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);
// Load should be switched off by LPTIM trigger already. This interrupt
// is mainly used to indicate the failure.
load_switch_set(false); // to update also LEDs
short_circuit = true;
}
@@ -79,13 +141,50 @@ extern "C" void ADC1_COMP_IRQHandler(void)
#endif // PIN_I_LOAD_COMP
void load_switch_set(bool enabled)
{
#ifdef PIN_LOAD_EN
DigitalOut load_enable(PIN_LOAD_EN);
if (enabled) load_enable = 1;
else load_enable = 0;
#endif
#ifdef PIN_LOAD_DIS
DigitalOut load_disable(PIN_LOAD_DIS);
#ifdef PIN_I_LOAD_COMP
if (enabled) lptim_init();
#else
if (enabled) load_disable = 0;
#endif
else load_disable = 1;
printf("Load enabled = %d\n", enabled);
#endif
leds_set_load(enabled);
}
void load_usb_set(bool enabled)
{
#ifdef PIN_USB_PWR_EN
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
if (enabled) usb_pwr_en = 1;
else usb_pwr_en = 0;
#endif
#ifdef PIN_USB_PWR_DIS
DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS);
if (enabled) usb_pwr_dis = 0;
else usb_pwr_dis = 1;
#endif
}
void load_init(load_output_t *load)
{
load->current_max = LOAD_CURRENT_MAX;
load->enabled_target = true;
load->usb_enabled_target = true;
hw_load_switch(false);
hw_usb_out(false);
load_switch_set(false);
load_usb_set(false);
load->switch_state = LOAD_STATE_DISABLED;
load->usb_state = LOAD_STATE_DISABLED;
load->junction_temperature = 25;
@@ -100,22 +199,22 @@ void usb_state_machine(load_output_t *load)
switch (load->usb_state) {
case LOAD_STATE_DISABLED:
if (load->usb_enabled_target == true) {
hw_usb_out(true);
load_usb_set(true);
load->usb_state = LOAD_STATE_ON;
}
break;
case LOAD_STATE_ON:
// currently still same cut-off SOC limit as the load
if (load->switch_state == LOAD_STATE_OFF_LOW_SOC) {
hw_usb_out(false);
load_usb_set(false);
load->usb_state = LOAD_STATE_OFF_LOW_SOC;
}
else if (load->switch_state == LOAD_STATE_OFF_OVERCURRENT) {
hw_usb_out(false);
load_usb_set(false);
load->usb_state = LOAD_STATE_OFF_OVERCURRENT;
}
else if (load->usb_enabled_target == false) {
hw_usb_out(false);
load_usb_set(false);
load->usb_state = LOAD_STATE_DISABLED;
}
break;
@@ -123,7 +222,7 @@ void usb_state_machine(load_output_t *load)
// currently still same cut-off SOC limit as the load
if (load->switch_state == LOAD_STATE_ON) {
if (load->usb_enabled_target == true) {
hw_usb_out(true);
load_usb_set(true);
load->usb_state = LOAD_STATE_ON;
}
else {
@@ -145,24 +244,24 @@ void load_state_machine(load_output_t *load, bool source_enabled)
switch (load->switch_state) {
case LOAD_STATE_DISABLED:
if (source_enabled == true && load->enabled_target == true) {
hw_load_switch(true);
load_switch_set(true);
load->switch_state = LOAD_STATE_ON;
}
break;
case LOAD_STATE_ON:
if (load->enabled_target == false) {
hw_load_switch(false);
load_switch_set(false);
load->switch_state = LOAD_STATE_DISABLED;
}
else if (source_enabled == false) {
hw_load_switch(false);
load_switch_set(false);
load->switch_state = LOAD_STATE_OFF_LOW_SOC;
}
break;
case LOAD_STATE_OFF_LOW_SOC:
if (source_enabled == true) {
if (load->enabled_target == true) {
hw_load_switch(true);
load_switch_set(true);
load->switch_state = LOAD_STATE_ON;
}
else {
@@ -171,7 +270,7 @@ void load_state_machine(load_output_t *load, bool source_enabled)
}
break;
case LOAD_STATE_OFF_OVERCURRENT:
if (time(NULL) > load->overcurrent_timestamp + 30*60) { // wait 5 min (TODO: make configurable)
if (time(NULL) > load->overcurrent_timestamp + LOAD_OVERCURRENT_RECOVERY_TIMEOUT) { // wait some time
load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
@@ -183,6 +282,7 @@ void load_state_machine(load_output_t *load, bool source_enabled)
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) {
short_circuit = false;
load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
@@ -199,6 +299,7 @@ void load_control(load_output_t *load)
{
if (short_circuit) {
load->switch_state = LOAD_STATE_OFF_SHORT_CIRCUIT;
return;
}
// junction temperature calculation model for overcurrent detection
@@ -208,8 +309,8 @@ void load_control(load_output_t *load)
) / (MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY);
if (load->junction_temperature > MOSFET_MAX_JUNCTION_TEMP) {
hw_load_switch(false);
hw_usb_out(false);
load_switch_set(false);
load_usb_set(false);
load->switch_state = LOAD_STATE_OFF_OVERCURRENT;
load->overcurrent_timestamp = time(NULL);
}
@@ -218,8 +319,8 @@ void load_control(load_output_t *load)
if (load->voltage > LOW_SIDE_VOLTAGE_MAX) {
debounce_counter++;
if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
hw_load_switch(false);
hw_usb_out(false);
load_switch_set(false);
load_usb_set(false);
load->switch_state = LOAD_STATE_OFF_OVERVOLTAGE;
load->overcurrent_timestamp = time(NULL);
log_data.error_flags |= (1 << ERR_BAT_OVERVOLTAGE);
@@ -227,3 +328,5 @@ void load_control(load_output_t *load)
}
debounce_counter = 0;
}
#endif /* UNIT_TEST */
+8
View File
@@ -63,6 +63,14 @@ typedef struct {
*/
void load_init(load_output_t *load);
/** Enable/disable load switch
*/
void load_switch_set(bool enabled);
/** Enable/disable USB output
*/
void load_usb_set(bool enabled);
/** State machine, called every second.
*/
void load_state_machine(load_output_t *load, bool source_enabled);