Add Clang-Format file and apply it

The format tries to maintain the previously used Libre Solar firmware coding
style as much as possible.

Applying the format fixed some issues and re-ordered some parts of the code.
This commit is contained in:
Martin Jäger
2022-01-26 13:56:31 +01:00
committed by Martin Jäger
parent 2359a96bbe
commit 5bdbecf19d
45 changed files with 1036 additions and 986 deletions
+45 -51
View File
@@ -11,14 +11,14 @@
#include <device.h>
#include <drivers/gpio.h>
#include <math.h> // for fabs function
#include <stdlib.h> // for min/max function
#include <math.h> // for fabs function
#include <stdio.h>
#include <stdlib.h> // for min/max function
#include "device_status.h"
#include "helper.h"
#include "half_bridge.h"
#include "data_storage.h"
#include "device_status.h"
#include "half_bridge.h"
#include "helper.h"
#include "setup.h"
LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL);
@@ -35,26 +35,26 @@ Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode)
{
hvb = high;
lvb = low;
mode = op_mode;
enable = true;
state = DCDC_CONTROL_OFF;
mode = op_mode;
enable = true;
state = DCDC_CONTROL_OFF;
inductor_current_max = DT_PROP(DT_PATH(pcb), dcdc_current_max);
hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max);
ls_voltage_max = DT_PROP(DT_PATH(pcb), ls_voltage_max);
ls_voltage_min = 9.0;
output_power_min = 1; // switch off if power < 1 W
output_power_min = 1; // switch off if power < 1 W
restart_interval = 60;
off_timestamp = -10000; // start immediately
pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction
off_timestamp = -10000; // start immediately
pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction
// lower duty limit might have to be adjusted dynamically depending on LS voltage
half_bridge_init(DT_PROP(DT_INST(0, half_bridge), frequency) / 1000,
DT_PROP(DT_INST(0, half_bridge), deadtime), 12 / hs_voltage_max, 0.97);
DT_PROP(DT_INST(0, half_bridge), deadtime), 12 / hs_voltage_max, 0.97);
}
int Dcdc::perturb_observe_controller()
{
int pwr_inc_pwm_direction; // direction of PWM duty cycle change for increasing power
int pwr_inc_pwm_direction; // direction of PWM duty cycle change for increasing power
DcBus *in;
DcBus *out;
float out_power;
@@ -75,10 +75,10 @@ int Dcdc::perturb_observe_controller()
}
if (out_power >= output_power_min) {
power_good_timestamp = uptime(); // reset the time
power_good_timestamp = uptime(); // reset the time
}
int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power
int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power
if ((uptime() - power_good_timestamp > 10 || out_power < -10.0) && mode != DCDC_MODE_AUTO) {
// switch off after 10s low power or negative power (if not in nanogrid mode)
@@ -87,22 +87,22 @@ int Dcdc::perturb_observe_controller()
else if (out->voltage > out->sink_control_voltage()) {
// output voltage target reached
state = (out == lvb) ? DCDC_CONTROL_CV_LS : DCDC_CONTROL_CV_HS;
pwr_inc_goal = -1; // decrease output power
pwr_inc_goal = -1; // decrease output power
}
else if (out->sink_current_margin < 0) {
// output charge current limit reached
state = (out == lvb) ? DCDC_CONTROL_CC_LS : DCDC_CONTROL_CC_HS;
pwr_inc_goal = -1; // decrease output power
pwr_inc_goal = -1; // decrease output power
}
else if (in->src_current_margin > 0) {
// input current (negative signs) limit exceeded
state = (in == hvb) ? DCDC_CONTROL_CC_HS : DCDC_CONTROL_CC_LS;
pwr_inc_goal = -1; // decrease output power
pwr_inc_goal = -1; // decrease output power
}
else if (fabs(inductor_current) > inductor_current_max) {
// current above hardware maximum
state = DCDC_CONTROL_CC_LS;
pwr_inc_goal = -1; // decrease output power
pwr_inc_goal = -1; // decrease output power
}
else if (in->voltage < in->src_control_voltage() && out_power > output_power_min) {
// input voltage below limit
@@ -116,7 +116,7 @@ int Dcdc::perturb_observe_controller()
}
else if (out_power < output_power_min && out->voltage < out->src_control_voltage()) {
// no load condition (e.g. start-up of nanogrid) --> raise voltage
pwr_inc_goal = 1; // increase output power
pwr_inc_goal = 1; // increase output power
}
else {
// start MPPT
@@ -130,11 +130,11 @@ int Dcdc::perturb_observe_controller()
#if CONFIG_DCDC_LOG_LEVEL == LOG_LEVEL_DBG
// workaround as LOG_DBG does not support float printing
printf("P: %.2fW (prev %.2fW), ind. current: %.2f, "
"in: %.2fV, %.2fA margin, out: %.2fV (target %.2fV), %.2fA margin, "
"PWM: %.1f, chg_state: %d, pwr_inc: %d, buck/boost: %d\n",
out_power, power_prev, inductor_current, in->voltage, in->src_current_margin,
out->voltage, out->sink_voltage_intercept, out->sink_current_margin,
half_bridge_get_duty_cycle() * 100.0, state, pwr_inc_goal, pwr_inc_pwm_direction);
"in: %.2fV, %.2fA margin, out: %.2fV (target %.2fV), %.2fA margin, "
"PWM: %.1f, chg_state: %d, pwr_inc: %d, buck/boost: %d\n",
out_power, power_prev, inductor_current, in->voltage, in->src_current_margin,
out->voltage, out->sink_voltage_intercept, out->sink_current_margin,
half_bridge_get_duty_cycle() * 100.0, state, pwr_inc_goal, pwr_inc_pwm_direction);
#endif
power_prev = out_power;
@@ -152,29 +152,24 @@ int Dcdc::perturb_observe_controller()
__weak DcdcOperationMode Dcdc::check_start_conditions()
{
if (enable == false ||
hvb->voltage > hs_voltage_max || // also critical for buck mode because of ringing
lvb->voltage > ls_voltage_max ||
lvb->voltage < ls_voltage_min ||
dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE) ||
(int)uptime() < (off_timestamp + (int)restart_interval))
if (enable == false || hvb->voltage > hs_voltage_max
|| // also critical for buck mode because of ringing
lvb->voltage > ls_voltage_max || lvb->voltage < ls_voltage_min
|| dev_stat.has_error(ERR_BAT_UNDERVOLTAGE | ERR_BAT_OVERVOLTAGE)
|| (int)uptime() < (off_timestamp + (int)restart_interval))
{
return DCDC_MODE_OFF;
}
if (lvb->sink_current_margin > 0 &&
lvb->voltage < lvb->sink_control_voltage() &&
hvb->src_current_margin < 0 &&
hvb->voltage > hvb->src_control_voltage() &&
hvb->voltage * 0.85 > lvb->voltage)
if (lvb->sink_current_margin > 0 && lvb->voltage < lvb->sink_control_voltage()
&& hvb->src_current_margin < 0 && hvb->voltage > hvb->src_control_voltage()
&& hvb->voltage * 0.85 > lvb->voltage)
{
return DCDC_MODE_BUCK;
}
if (hvb->sink_current_margin > 0 &&
hvb->voltage < hvb->sink_control_voltage() &&
lvb->src_current_margin < 0 &&
lvb->voltage > lvb->src_control_voltage())
if (hvb->sink_current_margin > 0 && hvb->voltage < hvb->sink_control_voltage()
&& lvb->src_current_margin < 0 && lvb->voltage > lvb->src_control_voltage())
{
return DCDC_MODE_BOOST;
}
@@ -250,11 +245,10 @@ __weak void Dcdc::control()
half_bridge_start();
power_good_timestamp = uptime();
printf("DC/DC %s mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", mode_name,
hvb->voltage, lvb->voltage, half_bridge_get_duty_cycle() * 100);
hvb->voltage, lvb->voltage, half_bridge_get_duty_cycle() * 100);
}
else {
startup_inhibit(true); // reset inhibit counter
startup_inhibit(true); // reset inhibit counter
}
}
else { // half bridge is on
@@ -269,7 +263,7 @@ __weak void Dcdc::control()
else {
int err = perturb_observe_controller();
if (err != 0) {
stop_reason = "low power";
stop_reason = "low power";
}
}
@@ -312,11 +306,11 @@ void Dcdc::test()
half_bridge_set_duty_cycle(lvb->voltage / hvb->voltage);
half_bridge_start();
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n",
hvb->voltage, lvb->voltage, half_bridge_get_duty_cycle() * 100);
printf("DC/DC test mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hvb->voltage,
lvb->voltage, half_bridge_get_duty_cycle() * 100);
}
else {
startup_inhibit(true); // reset inhibit counter
startup_inhibit(true); // reset inhibit counter
}
}
}
@@ -332,11 +326,11 @@ void Dcdc::fuse_destruction()
{
static int counter = 0;
if (counter > 20) { // wait 20s to be able to send out data
if (counter > 20) { // wait 20s to be able to send out data
LOG_ERR("Charge controller fuse destruction called!\n");
data_storage_write();
half_bridge_stop();
half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle
half_bridge_init(50, 0, 0, 0.98); // reset safety limits to allow 0% duty cycle
half_bridge_set_duty_cycle(0);
half_bridge_start();
// now the fuse should be triggered and we disappear
@@ -350,7 +344,7 @@ void Dcdc::output_hvs_enable()
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
if (device_is_ready(hv_out)) {
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_ACTIVE);
}
#endif
}
@@ -361,7 +355,7 @@ void Dcdc::output_hvs_disable()
const struct device *hv_out = DEVICE_DT_GET(DT_GPIO_CTLR(HV_OUT_NODE, gpios));
if (device_is_ready(hv_out)) {
gpio_pin_configure(hv_out, DT_GPIO_PIN(HV_OUT_NODE, gpios),
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
DT_GPIO_FLAGS(HV_OUT_NODE, gpios) | GPIO_OUTPUT_INACTIVE);
}
#endif
}