Files
charge-controller-firmware/src/load.h
T
Martin Jaeger 0e91c834ac 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.
2019-06-12 09:45:19 +02:00

85 lines
2.8 KiB
C

/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LOAD_H
#define LOAD_H
/** @file
*
* @brief
* Load/USB output functions and data types
*/
#include <stdbool.h>
#include <stdint.h>
/** Load/USB output states
*
* Used for USB or normal load outputs connecting to battery via switch.
*/
enum load_state_t {
LOAD_STATE_DISABLED = 0, ///< Actively disabled
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_SHORT_CIRCUIT ///< Off to protect charge controller (overrules target setting)
};
/** Load output type
*
* Stores status of load output incl. 5V USB output (if existing on PCB)
*/
typedef struct {
uint16_t switch_state; ///< Current state of load output switch
uint16_t usb_state; ///< Current state of USB output
float voltage; ///< actual voltage measurement
float current; ///< actual current measurement
float current_max; ///< maximum allowed current
int overcurrent_timestamp; ///< time at which an overcurrent event occured
float junction_temperature; ///< calculated using thermal model based on current and ambient temperature measurement (unit: °C)
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 and overcurrent / short circuit protection comparator (if existing)
*/
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);
/** Main load control function, should be called by control timer
*
* Performs time-critical checks like overcurrent and overvoltage
*/
void load_control(load_output_t *load);
#endif /* LOAD_H */