Files
charge-controller-firmware/app/src/load.cpp
T
Martin JägerandClaude Opus 5.5 a428892916 app: convert device status to C struct
DeviceStatus becomes struct device_status with prefixed functions. The
error flag helpers are static inline functions in the header.

The implementation stays in C++ for now, as it reads data from objects
which are not converted yet. The header doesn't include the headers of
these objects anymore.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 15:21:57 +01:00

205 lines
6.4 KiB
C++

/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "load.h"
#include "board.h"
#include <zephyr/kernel.h>
#if BOARD_HAS_LOAD_OUTPUT || UNIT_TEST
#include "device_status.h"
#include "helper.h"
#include "leds.h"
#include <libresolar/drivers/power_switch.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(load, CONFIG_LOAD_LOG_LEVEL);
#define LOAD_CURRENT_MAX DT_PROP(LOAD_SWITCH_NODE, current_max)
#define PCB_LS_VOLTAGE_MAX DT_PROP(DT_PATH(pcb), ls_voltage_max)
#define PCB_MOSFETS_TJ_MAX DT_PROP(DT_PATH(pcb), mosfets_tj_max)
#define PCB_MOSFETS_TAU_JA DT_PROP(DT_PATH(pcb), mosfets_tau_ja)
#define PCB_INTERNAL_TREF_MAX DT_PROP(DT_PATH(pcb), internal_tref_max)
extern struct device_status dev_stat;
LoadOutput::LoadOutput(struct dc_bus *dc_bus, const struct device *sw, int led_pos)
: power_switch(sw), led(led_pos)
{
power_port_init(this, dc_bus, false);
state = LOAD_STATE_OFF;
if (!device_is_ready(power_switch)) {
LOG_ERR("Power switch %s not ready", power_switch->name);
}
else {
// not supported by all switches, so the return value is ignored
power_switch_set_callback(power_switch, POWER_SWITCH_EVENT_FAULT, short_circuit_callback,
this);
}
switch_set(false);
junction_temperature = 25; // starting point: 25°C
oc_recovery_delay = CONFIG_LOAD_OC_RECOVERY_DELAY;
lvd_recovery_delay = CONFIG_LOAD_LVD_RECOVERY_DELAY;
ov_hysteresis = 0.3;
enable = true; // switch on in next control() call if everything is fine
}
// this function is called more often than the state machine
void LoadOutput::control()
{
if (state == LOAD_STATE_ON) {
// junction temperature calculation model for overcurrent detection
junction_temperature = junction_temperature
+ (dev_stat.internal_temp - junction_temperature
+ current * current / (LOAD_CURRENT_MAX * LOAD_CURRENT_MAX)
* (PCB_MOSFETS_TJ_MAX - PCB_INTERNAL_TREF_MAX))
/ (PCB_MOSFETS_TAU_JA * CONFIG_CONTROL_FREQUENCY);
if (junction_temperature > PCB_MOSFETS_TJ_MAX || current > LOAD_CURRENT_MAX * 2) {
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
oc_timestamp = uptime();
}
if (power_switch_power_good(power_switch) == 0) {
flags_set(&error_flags, ERR_LOAD_OVERCURRENT);
oc_timestamp = uptime();
}
// negative margin means sourcing current from bus is allowed
if (bus->src_current_margin > -0.1F) {
flags_set(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
}
if (bus->voltage < dc_bus_src_control_voltage(bus, disconnect_voltage)) {
flags_set(&error_flags, ERR_LOAD_SHEDDING);
lvd_timestamp = uptime();
}
// long-term overvoltage (overvoltage transients are detected as an ADC alert and switch
// off the solar input instead of the load output)
if (bus->voltage > dc_bus_series_voltage(bus, overvoltage)
|| bus->voltage > PCB_LS_VOLTAGE_MAX)
{
ov_debounce_counter++;
if (ov_debounce_counter > CONFIG_CONTROL_FREQUENCY) {
// waited 1s before setting the flag
flags_set(&error_flags, ERR_LOAD_OVERVOLTAGE);
}
}
else {
ov_debounce_counter = 0;
}
if (error_flags) {
stop();
}
if (enable == false) {
switch_set(false);
state = LOAD_STATE_OFF;
}
}
else {
// load is off: check if errors are resolved and if load can be switched on
if (flags_check(&error_flags, ERR_LOAD_SHEDDING)
&& bus->voltage > dc_bus_src_control_voltage(bus, reconnect_voltage)
&& uptime() - lvd_timestamp > lvd_recovery_delay)
{
flags_clear(&error_flags, ERR_LOAD_SHEDDING);
}
if (flags_check(&error_flags, ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP)
&& uptime() - oc_timestamp > oc_recovery_delay)
{
flags_clear(&error_flags, ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP);
}
if (flags_check(&error_flags, ERR_LOAD_OVERVOLTAGE)
&& bus->voltage < (dc_bus_series_voltage(bus, overvoltage) - ov_hysteresis)
&& bus->voltage < (PCB_LS_VOLTAGE_MAX - ov_hysteresis))
{
flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE);
}
if (flags_check(&error_flags, ERR_LOAD_BUS_SRC_CURRENT) && bus->src_current_margin < 0) {
flags_clear(&error_flags, ERR_LOAD_BUS_SRC_CURRENT);
}
if (flags_check(&error_flags, ERR_LOAD_SHORT_CIRCUIT) && enable == false) {
// stay here until the charge controller is reset or load is manually switched off
flags_clear(&error_flags, ERR_LOAD_SHORT_CIRCUIT);
}
// finally switch on if all errors were resolved and at least 1A src current is available
if (enable == true && !error_flags && bus->src_current_margin < -1.0F) {
switch_set(true);
state = LOAD_STATE_ON;
}
}
info = error_flags > 0 ? -error_flags : state;
}
void LoadOutput::stop(uint32_t flag)
{
switch_set(false);
state = LOAD_STATE_OFF;
flags_set(&error_flags, flag);
// flicker the load LED if failure was most probably caused by the user
if (flags_check(&error_flags,
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT))
{
#if LED_EXISTS(load)
leds_flicker(LED_POS(load), LED_TIMEOUT_INFINITE);
#endif
oc_timestamp = uptime();
}
}
void LoadOutput::set_voltage_limits(float lvd, float lvr, float ov)
{
disconnect_voltage = lvd;
reconnect_voltage = lvr;
overvoltage = ov;
}
void LoadOutput::switch_set(bool on)
{
if (led >= 0) {
leds_set(led, on, LED_TIMEOUT_INFINITE);
}
if (device_is_ready(power_switch)) {
if (on) {
power_switch_on(power_switch);
}
else {
power_switch_off(power_switch);
}
}
}
void LoadOutput::short_circuit_callback(const struct device *dev, uint32_t events, void *user_data)
{
static_cast<LoadOutput *>(user_data)->stop(ERR_LOAD_SHORT_CIRCUIT);
}
#endif /* BOARD_HAS_LOAD_OUTPUT */