Separate load and USB out, improve state handling
Identified issues: - The load output and USB charging port shared the same state machine, which did not allow to set different switch-off thresholds. - Load error codes were global, so it was not possible to have multiple load instances. This commit introduces load-specific error_flags, makes the LoadOutput class inherit from PowerPort (to improve code readability), separates the load driver better from higher level implementation and adds a second LoadOutput object for the USB charging port.
This commit is contained in:
+81
-49
@@ -19,49 +19,85 @@
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
/** Load/USB output states
|
||||
*
|
||||
* Used for USB or normal load outputs connecting to battery via switch.
|
||||
*
|
||||
* DEPRECATED: These states will be removed in the future and the status will be determined only
|
||||
* by the error flags.
|
||||
/**
|
||||
* Load/USB output states
|
||||
*/
|
||||
enum LoadState {
|
||||
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_STATE_OFF_TEMPERATURE ///< Off because of battery or charge controller temperature
|
||||
LOAD_STATE_OFF = 0, ///< Actively disabled
|
||||
LOAD_STATE_ON = 1, ///< Normal state: On
|
||||
};
|
||||
|
||||
|
||||
/** Load error flags
|
||||
*
|
||||
* When adding new flags, please make sure to use only up to 32 errors
|
||||
* Each enum must represent a unique power of 2 number
|
||||
*/
|
||||
enum LoadErrorFlag {
|
||||
/**
|
||||
* Available energy or power too low
|
||||
*
|
||||
* Switching off the load can be triggered either by a low battery voltage or by low state of
|
||||
* charge (SOC) in case of more advanced battery management.
|
||||
*
|
||||
* Set in LoadOutput::control() and cleared after reconnect delay passed and voltage is above
|
||||
* reconnect threshold again.
|
||||
*/
|
||||
ERR_LOAD_SHEDDING = 1U << 0,
|
||||
|
||||
/**
|
||||
* Too high voltage for load
|
||||
*
|
||||
* Set and cleared in LoadOutput::control()
|
||||
*/
|
||||
ERR_LOAD_OVERVOLTAGE = 1U << 1,
|
||||
|
||||
/**
|
||||
* Long-term overcurrent at load port
|
||||
*
|
||||
* Set in LoadOutput::control() and cleared after configurable delay.
|
||||
*/
|
||||
ERR_LOAD_OVERCURRENT = 1U << 2,
|
||||
|
||||
/**
|
||||
* Short circuit detected at load port
|
||||
*
|
||||
* Set by LoadOutput::control() after overcurrent comparator triggered, cleared only if
|
||||
* load output is manually disabled and enabled again.
|
||||
*/
|
||||
ERR_LOAD_SHORT_CIRCUIT = 1U << 3,
|
||||
|
||||
/**
|
||||
* Overcurrent identified via voltage dip (may be caused by too small battery)
|
||||
*
|
||||
* Set and cleared in LoadOutput::control(). Treated same as load overcurrent.
|
||||
*/
|
||||
ERR_LOAD_VOLTAGE_DIP = 1U << 4,
|
||||
|
||||
/**
|
||||
* The bus the load is connected to disabled sourcing current from it
|
||||
*
|
||||
* Reasons can be that battery temperature limits were exceeded. Voltage limits should be
|
||||
* covered by the load directly.
|
||||
*/
|
||||
ERR_LOAD_BUS_SRC_CURRENT = 1U << 5
|
||||
};
|
||||
|
||||
/** Load output type
|
||||
*
|
||||
* Stores status of load output incl. 5V USB output (if existing on PCB)
|
||||
*/
|
||||
class LoadOutput
|
||||
class LoadOutput : public PowerPort
|
||||
{
|
||||
public:
|
||||
/** Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing)
|
||||
/**
|
||||
* Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing)
|
||||
*
|
||||
* @param load Load output struct
|
||||
* @param bus_int Internal DC bus the load switch is connected to (e.g. battery and DC/DC junction)
|
||||
* @param terminal Load terminal DC bus (external interface)
|
||||
* @param dc_bus DC bus the load is connected to
|
||||
* @param switch_set Pointer to function for enabling/disabling load switch
|
||||
* @param init Pointer to function for load driver initialization
|
||||
*/
|
||||
LoadOutput(PowerPort *pwr_port);
|
||||
|
||||
/** Enable/disable load switch
|
||||
*/
|
||||
void switch_set(bool enabled);
|
||||
|
||||
/** Enable/disable USB output
|
||||
*/
|
||||
void usb_set(bool enabled);
|
||||
|
||||
/** USB state machine, called every second.
|
||||
*/
|
||||
void usb_state_machine();
|
||||
LoadOutput(DcBus *dc_bus, void (*switch_fn)(bool), void (*init_fn)());
|
||||
|
||||
/** Main load control function, should be called by control timer
|
||||
*
|
||||
@@ -86,18 +122,13 @@ public:
|
||||
*/
|
||||
void set_voltage_limits(float lvd, float lvr, float ov);
|
||||
|
||||
uint32_t state; ///< Current state of load output switch (DEPRECATED)
|
||||
uint32_t usb_state; ///< Current state of USB output (DEPRECATED)
|
||||
uint32_t state; ///< Current state of load output switch (negative value means
|
||||
///< error state, indicated by LoadErrorFlags)
|
||||
|
||||
PowerPort *port; ///< Pointer to DC bus containting actual voltage and current
|
||||
///< measurement of (external) load output terminal
|
||||
uint32_t error_flags = 0;
|
||||
|
||||
bool enable = false; ///< Target on state set via communication port (overruled if
|
||||
///< battery is empty or any errors occured)
|
||||
bool usb_enable = false; ///< Target on state for USB output
|
||||
|
||||
bool pgood = false; ///< Power good flag that is true if load switch is on
|
||||
bool usb_pgood = false; ///< Power good flag for USB output
|
||||
|
||||
time_t oc_timestamp; ///< Time when last overcurrent event occured
|
||||
int32_t oc_recovery_delay; ///< Seconds before we attempt to re-enable the load
|
||||
@@ -113,21 +144,22 @@ public:
|
||||
float junction_temperature; ///< calculated using thermal model based on current and ambient
|
||||
///< temperature measurement (unit: °C)
|
||||
|
||||
float overvoltage = 0;
|
||||
float overvoltage = 0; ///< Upper voltage limit
|
||||
float ov_hysteresis; ///< Hysteresis to switch back on after an overvoltage event
|
||||
|
||||
private:
|
||||
int determine_load_state();
|
||||
void short_circuit_comp_init();
|
||||
/**
|
||||
* Pointer to the load switch function
|
||||
*/
|
||||
void (*switch_set)(bool);
|
||||
|
||||
int ov_debounce_counter = 0;
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
void get_bindings();
|
||||
|
||||
struct device *dev_load;
|
||||
struct device *dev_usb;
|
||||
#endif
|
||||
};
|
||||
|
||||
void load_out_init();
|
||||
void usb_out_init();
|
||||
|
||||
void load_out_set(bool);
|
||||
void usb_out_set(bool);
|
||||
|
||||
#endif /* LOAD_H */
|
||||
|
||||
Reference in New Issue
Block a user