From 4eaf89154af084ec8e86738284cb7e9a98ac2ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Wed, 27 Jan 2021 06:34:48 +0100 Subject: [PATCH] Use BOARD_HAS_* macro to identify key hardware features These macros replace the direct use of DT_ macros in many locations and make the code more readable. --- src/bat_charger.cpp | 7 ++++--- src/board.h | 23 +++++++++++++++++++++++ src/daq.cpp | 36 ++++++++++++++++++------------------ src/data_nodes.cpp | 32 ++++++++++++++++---------------- src/dcdc.cpp | 5 +++-- src/device_status.cpp | 10 +++++----- src/ext/oled.cpp | 6 +++--- src/half_bridge.cpp | 5 +++-- src/load.cpp | 5 +++-- src/load_driver.c | 15 ++++++++------- src/main.cpp | 24 ++++++++++++------------ src/pwm_switch.cpp | 4 ++-- src/pwm_switch_driver.c | 5 +++-- src/setup.cpp | 9 +++++---- src/setup.h | 9 +++++---- 15 files changed, 113 insertions(+), 82 deletions(-) create mode 100644 src/board.h diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index d90551a..a2e89f3 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -16,6 +16,7 @@ LOG_MODULE_REGISTER(bat_charger, CONFIG_BAT_LOG_LEVEL); #include // for fabs function #include +#include "board.h" #include "helper.h" #include "device_status.h" @@ -23,7 +24,7 @@ extern DeviceStatus dev_stat; extern LoadOutput load; // DISCHARGE_CURRENT_MAX used to estimate current-compensation of load disconnect voltage -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT #define DISCHARGE_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max) #else #define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(dcdc), current_max) @@ -355,8 +356,8 @@ void Charger::enter_state(int next_state) void Charger::discharge_control(BatConf *bat_conf) { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) || \ - DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_LOAD_OUTPUT || \ + BOARD_HAS_USB_OUTPUT if (!empty) { // as we don't have a proper SOC estimation, we determine an empty battery by the main diff --git a/src/board.h b/src/board.h new file mode 100644 index 0000000..d4a880f --- /dev/null +++ b/src/board.h @@ -0,0 +1,23 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2021 Martin Jäger / Libre Solar + */ + +#ifndef BOARD_H_ +#define BOARD_H_ + +/** + * @file + * + * @brief Defines for conditional compiling based on existing board features + */ + +#include + +#define BOARD_HAS_DCDC DT_NODE_EXISTS(DT_PATH(dcdc)) +#define BOARD_HAS_PWM_PORT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#define BOARD_HAS_LOAD_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#define BOARD_HAS_USB_OUTPUT DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) + +#endif /* BOARD_H_ */ diff --git a/src/daq.cpp b/src/daq.cpp index 1f6890a..8004efd 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -22,13 +22,13 @@ LOG_MODULE_REGISTER(daq, CONFIG_DAQ_LOG_LEVEL); // typical value for Semitec 103AT-5 thermistor: 3435 #define NTC_BETA_VALUE 3435 -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC static uint16_t dcdc_current_offset_raw; #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT static uint16_t pwm_current_offset_raw; #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT static uint16_t load_current_offset_raw; #endif @@ -93,20 +93,20 @@ static inline float ntc_temp(uint32_t channel, int32_t vref, float ntc_series_re void calibrate_current_sensors() { -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC dcdc_current_offset_raw = adc_raw_filtered(ADC_POS(i_dcdc)); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT pwm_current_offset_raw = adc_raw_filtered(ADC_POS(i_pwm)); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT load_current_offset_raw = adc_raw_filtered(ADC_POS(i_load)); #endif } void adc_update_value(unsigned int pos) { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT // only read input voltage and current when switch is on or permanently off if ((pos != ADC_POS(v_pwm) && pos != ADC_POS(i_pwm)) || pwm_switch.signal_high() || pwm_switch.active() == false) @@ -168,16 +168,16 @@ void daq_update() // calculate lower voltage first, as it is needed for PWM terminal voltage calculation lv_bus.voltage = adc_scaled(ADC_POS(v_low), vref, ADC_GAIN(v_low)); -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC hv_bus.voltage = adc_scaled(ADC_POS(v_high), vref, ADC_GAIN(v_high)); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT pwm_switch.ext_voltage = lv_bus.voltage - adc_scaled(ADC_POS(v_pwm), vref, ADC_GAIN(v_pwm), ADC_OFFSET(v_pwm)); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT load.current = adc_scaled(ADC_POS(i_load), vref, ADC_GAIN(i_load), load_current_offset_raw); float load_current = load.current; #else @@ -186,7 +186,7 @@ void daq_update() float lv_terminal_current = -load_current; -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT // current multiplied with PWM duty cycle for PWM charger to get avg current for correct power // calculation pwm_switch.current = -pwm_switch.get_duty_cycle() * @@ -197,7 +197,7 @@ void daq_update() pwm_switch.power = pwm_switch.bus->voltage * pwm_switch.current; #endif -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC dcdc.inductor_current = adc_scaled(ADC_POS(i_dcdc), vref, ADC_GAIN(i_dcdc), dcdc_current_offset_raw); @@ -212,7 +212,7 @@ void daq_update() lv_terminal.current = lv_terminal_current; lv_terminal.power = lv_terminal.bus->voltage * lv_terminal.current; -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT load.power = load.bus->voltage * load.current; #endif @@ -255,10 +255,10 @@ void daq_update() void lv_overvoltage_alert() { // disable any sort of input -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC dcdc.stop(); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT pwm_switch.stop(); #endif // do not use enter_state function, as we don't want to wait entire recharge delay @@ -272,7 +272,7 @@ void lv_overvoltage_alert() void lv_undervoltage_alert() { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT // the battery undervoltage must have been caused by a load current peak load.stop(ERR_LOAD_VOLTAGE_DIP); #endif @@ -281,7 +281,7 @@ void lv_undervoltage_alert() adc_readings[ADC_POS(v_low)], adc_alerts_lower[ADC_POS(v_low)].limit); } -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC void hv_overvoltage_alert() { dcdc.stop(); @@ -324,7 +324,7 @@ void daq_set_lv_limits(float lv_overvoltage, float lv_undervoltage) adc_alerts_lower[ADC_POS(v_low)].callback = lv_undervoltage_alert; } -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC void daq_set_hv_limit(float hv_overvoltage) { float scale = (((4096 << 4) * 1000) / ADC_GAIN(v_high)) / (float)VREF; diff --git a/src/data_nodes.cpp b/src/data_nodes.cpp index 050a9fb..f80794e 100644 --- a/src/data_nodes.cpp +++ b/src/data_nodes.cpp @@ -169,7 +169,7 @@ static DataNode data_nodes[] = { ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), // load settings -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_BOOL(0x50, "LoadEnDefault", &load.enable, ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), @@ -186,7 +186,7 @@ static DataNode data_nodes[] = { ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT TS_NODE_BOOL(0x55, "UsbEnDefault", &usb_pwr.enable, ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), @@ -210,22 +210,22 @@ static DataNode data_nodes[] = { TS_NODE_PATH(ID_INPUT, "input", 0, NULL), -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_BOOL(0x61, "LoadEn", &load.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT TS_NODE_BOOL(0x62, "UsbEn", &usb_pwr.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC TS_NODE_BOOL(0x63, "DcdcEn", &dcdc.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT TS_NODE_BOOL(0x64, "PwmEn", &pwm_switch.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif @@ -282,7 +282,7 @@ static DataNode data_nodes[] = { TS_NODE_UINT32(0x7C, "ChgState", &charger.state, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC TS_NODE_UINT16(0x7D, "DCDCState", &dcdc.state, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), #endif @@ -305,7 +305,7 @@ static DataNode data_nodes[] = { ID_OUTPUT, TS_ANY_R, 0), #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_FLOAT(0x89, "Load_A", &load.current, 2, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), @@ -316,7 +316,7 @@ static DataNode data_nodes[] = { ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT TS_NODE_INT32(0x8C, "UsbInfo", &usb_pwr.info, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), #endif @@ -344,7 +344,7 @@ static DataNode data_nodes[] = { TS_NODE_UINT32(0x08, "SolarInTotal_Wh", &dev_stat.solar_in_total_Wh, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_UINT32(0x09, "LoadOutTotal_Wh", &dev_stat.load_out_total_Wh, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), #endif @@ -375,7 +375,7 @@ static DataNode data_nodes[] = { TS_NODE_UINT16(0x0F, "SolarMaxDay_W", &dev_stat.solar_power_max_day, ID_REC, TS_ANY_R | TS_MKR_W, 0), -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_UINT16(0x10, "LoadMaxDay_W", &dev_stat.load_power_max_day, ID_REC, TS_ANY_R | TS_MKR_W, 0), #endif @@ -385,7 +385,7 @@ static DataNode data_nodes[] = { ID_REC, TS_ANY_R | TS_MKR_W, PUB_SER | PUB_CAN), #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_FLOAT(0xA2, "LoadOutDay_Wh", &load.pos_energy_Wh, 2, ID_REC, TS_ANY_R | TS_MKR_W, PUB_SER | PUB_CAN), #endif @@ -409,7 +409,7 @@ static DataNode data_nodes[] = { TS_NODE_UINT16(0xB1, "SolarMaxTotal_W", &dev_stat.solar_power_max_total, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_UINT16(0xB2, "LoadMaxTotal_W", &dev_stat.load_power_max_total, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), #endif @@ -423,7 +423,7 @@ static DataNode data_nodes[] = { TS_NODE_FLOAT(0xB5, "DcdcMaxTotal_A", &dev_stat.dcdc_current_max, 2, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT TS_NODE_FLOAT(0xB6, "LoadMaxTotal_A", &dev_stat.load_current_max, 2, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), #endif @@ -442,7 +442,7 @@ static DataNode data_nodes[] = { TS_NODE_PATH(ID_CAL, "cal", 0, NULL), -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC TS_NODE_FLOAT(0xD1, "DcdcMin_W", &dcdc.output_power_min, 1, ID_CAL, TS_ANY_R | TS_MKR_W, PUB_NVM), @@ -489,7 +489,7 @@ void data_nodes_update_conf() if (battery_conf_check(&bat_conf_user)) { LOG_INF("New config valid and activated."); battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger); -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max); #endif diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 0c09e71..c3d7124 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -23,10 +23,11 @@ LOG_MODULE_REGISTER(dcdc, CONFIG_DCDC_LOG_LEVEL); #include "helper.h" #include "half_bridge.h" #include "eeprom.h" +#include "setup.h" extern DeviceStatus dev_stat; -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_ext_sense)) #define HV_EXT_SENSE_GPIO DT_CHILD(DT_PATH(outputs), hv_ext_sense) @@ -354,4 +355,4 @@ void Dcdc::output_hvs_disable() Dcdc::Dcdc(DcBus *high, DcBus *low, DcdcOperationMode op_mode) {} -#endif /* DT_NODE_EXISTS(DT_PATH(dcdc)) */ +#endif /* BOARD_HAS_DCDC */ diff --git a/src/device_status.cpp b/src/device_status.cpp index f6904f9..052cab3 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -68,7 +68,7 @@ void DeviceStatus::update_energy() grid_export_total_Wh_prev = grid_export_total_Wh; #endif solar_terminal.neg_energy_Wh = 0.0; - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + #if BOARD_HAS_LOAD_OUTPUT load.pos_energy_Wh = 0.0; #endif bat_terminal.pos_energy_Wh = 0.0; @@ -92,7 +92,7 @@ void DeviceStatus::update_energy() (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT load_out_total_Wh = load_out_total_Wh_prev + (load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0); #endif @@ -121,7 +121,7 @@ void DeviceStatus::update_min_max_values() } #endif -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC if (dcdc.inductor_current > dcdc_current_max) { dcdc_current_max = dcdc.inductor_current; } @@ -131,7 +131,7 @@ void DeviceStatus::update_min_max_values() } #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT if (load.current > load_current_max) { load_current_max = load.current; } @@ -146,7 +146,7 @@ void DeviceStatus::update_min_max_values() } #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT if (load.power > load_power_max_day) { load_power_max_day = load.power; if (load_power_max_day > load_power_max_total) { diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 20cc03e..d237005 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -93,7 +93,7 @@ void oled_update() } // solar panel data -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT if (pwm_switch.active()) { #else if (half_bridge_enabled()) { @@ -108,7 +108,7 @@ void oled_update() len = snprintf(buf, sizeof(buf), "n/a"); oled.writeString(buf, len); } -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT if (in_terminal.bus->voltage > bat_terminal.bus->voltage) #endif { @@ -146,7 +146,7 @@ void oled_update() oled.setTextCursor(0, 56); -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT bool pwm_enabled = pwm_switch.active(); float duty_cycle = pwm_switch.get_duty_cycle(); #else diff --git a/src/half_bridge.cpp b/src/half_bridge.cpp index 4be742f..1f9162f 100644 --- a/src/half_bridge.cpp +++ b/src/half_bridge.cpp @@ -11,6 +11,7 @@ #include #include "mcu.h" +#include "setup.h" #ifndef UNIT_TEST #include @@ -19,7 +20,7 @@ #define DT_DRV_COMPAT st_stm32_timers #endif -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC static uint16_t tim_ccr_min; // capture/compare register min/max static uint16_t tim_ccr_max; @@ -301,4 +302,4 @@ float half_bridge_get_duty_cycle() return (float)(half_bridge_get_ccr()) / half_bridge_get_arr(); } -#endif // DT_NODE_EXISTS(DT_PATH(dcdc)) +#endif // BOARD_HAS_DCDC diff --git a/src/load.cpp b/src/load.cpp index 6459249..8d6f8c6 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -7,8 +7,9 @@ #include "load.h" #include +#include "board.h" -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) || UNIT_TEST +#if BOARD_HAS_LOAD_OUTPUT || UNIT_TEST #include "leds.h" #include "device_status.h" @@ -171,4 +172,4 @@ void load_short_circuit_stop() load.stop(ERR_LOAD_SHORT_CIRCUIT); } -#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) */ +#endif /* BOARD_HAS_LOAD_OUTPUT */ diff --git a/src/load_driver.c b/src/load_driver.c index a46debd..b161bda 100644 --- a/src/load_driver.c +++ b/src/load_driver.c @@ -13,18 +13,19 @@ #include #include +#include "board.h" #include "hardware.h" #include "leds.h" #include "mcu.h" #include -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT #define LOAD_GPIO DT_CHILD(DT_PATH(outputs), load) static const struct device *dev_load; #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT #define USB_GPIO DT_CHILD(DT_PATH(outputs), usb_pwr) static const struct device *dev_usb; #endif @@ -144,7 +145,7 @@ void load_out_set(bool status) leds_set(LED_POS(load), status, LED_TIMEOUT_INFINITE); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT gpio_pin_configure(dev_load, DT_GPIO_PIN(LOAD_GPIO, gpios), DT_GPIO_FLAGS(LOAD_GPIO, gpios) | GPIO_OUTPUT_INACTIVE); if (status == true) { @@ -162,7 +163,7 @@ void load_out_set(bool status) void usb_out_set(bool status) { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT gpio_pin_configure(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), DT_GPIO_FLAGS(USB_GPIO, gpios) | GPIO_OUTPUT_INACTIVE); if (status == true) { @@ -197,7 +198,7 @@ void load_cp_enable() void load_out_init() { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT dev_load = device_get_binding(DT_GPIO_LABEL(LOAD_GPIO, gpios)); #endif @@ -214,14 +215,14 @@ void load_out_init() void usb_out_init() { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT dev_usb = device_get_binding(DT_GPIO_LABEL(USB_GPIO, gpios)); #endif } bool pgood_check() { -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT return gpio_pin_get(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios)); #else return false; diff --git a/src/main.cpp b/src/main.cpp index 71521c1..09daeba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,7 +39,7 @@ void main(void) battery_conf_init(&bat_conf, CONFIG_BAT_TYPE, CONFIG_BAT_NUM_CELLS, CONFIG_BAT_CAPACITY_AH); battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values - #if DT_NODE_EXISTS(DT_PATH(dcdc)) + #if BOARD_HAS_DCDC daq_set_hv_limit(DT_PROP(DT_PATH(pcb), hs_voltage_max)); #endif @@ -60,12 +60,12 @@ void main(void) charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system charger.init_terminal(&bat_conf); - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + #if BOARD_HAS_LOAD_OUTPUT load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max); #endif - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) + #if BOARD_HAS_USB_OUTPUT usb_pwr.set_voltage_limits(bat_conf.voltage_load_disconnect - 0.1, // keep on longer than load bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max); #endif @@ -82,13 +82,13 @@ void main(void) charger.charge_control(&bat_conf); // energy + soc calculation must be called exactly once per second - #if DT_NODE_EXISTS(DT_PATH(dcdc)) + #if BOARD_HAS_DCDC if (dcdc.state != DCDC_CONTROL_OFF) { hv_terminal.energy_balance(); } #endif - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) + #if BOARD_HAS_PWM_PORT if (pwm_switch.active() == 1) { pwm_switch.energy_balance(); } @@ -96,7 +96,7 @@ void main(void) lv_terminal.energy_balance(); - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + #if BOARD_HAS_LOAD_OUTPUT if (load.state == 1) { load.energy_balance(); } @@ -106,7 +106,7 @@ void main(void) dev_stat.update_min_max_values(); charger.update_soc(&bat_conf); - #if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && DT_NODE_EXISTS(DT_PATH(dcdc)) + #if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && BOARD_HAS_DCDC if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) { dcdc.fuse_destruction(); } @@ -114,7 +114,7 @@ void main(void) leds_update_1s(); - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + #if BOARD_HAS_LOAD_OUTPUT leds_update_soc(charger.soc, flags_check(&load.error_flags, ERR_LOAD_SHEDDING)); #else leds_update_soc(charger.soc, false); @@ -146,12 +146,12 @@ void control_thread() lv_terminal.update_bus_current_margins(); - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) + #if BOARD_HAS_PWM_PORT pwm_switch.control(); charging |= pwm_switch.active(); #endif - #if DT_NODE_EXISTS(DT_PATH(dcdc)) + #if BOARD_HAS_DCDC hv_terminal.update_bus_current_margins(); dcdc.control(); // control of DC/DC including MPPT algorithm charging |= half_bridge_enabled(); @@ -159,11 +159,11 @@ void control_thread() leds_set_charging(charging); - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + #if BOARD_HAS_LOAD_OUTPUT load.control(); #endif - #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) + #if BOARD_HAS_USB_OUTPUT usb_pwr.control(); #endif diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index a162d06..25eea44 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -21,7 +21,7 @@ LOG_MODULE_REGISTER(pwm_switch, CONFIG_PWM_LOG_LEVEL); #include "helper.h" #include "setup.h" -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT #define PWM_CURRENT_MAX (DT_PROP(DT_CHILD(DT_PATH(outputs), pwm_switch), current_max)) #define PWM_PERIOD (DT_PHA(DT_CHILD(DT_PATH(outputs), pwm_switch), pwms, period)) @@ -160,4 +160,4 @@ float PwmSwitch::get_duty_cycle() return pwm_signal_get_duty_cycle(); } -#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) */ +#endif /* BOARD_HAS_PWM_PORT */ diff --git a/src/pwm_switch_driver.c b/src/pwm_switch_driver.c index 196025d..3d77fc6 100644 --- a/src/pwm_switch_driver.c +++ b/src/pwm_switch_driver.c @@ -11,11 +11,12 @@ #include #include +#include "board.h" #include "daq.h" #include "helper.h" #include "mcu.h" -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT #ifndef UNIT_TEST @@ -161,4 +162,4 @@ bool pwm_active() { return false; } #endif -#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) */ +#endif /* BOARD_HAS_PWM_PORT */ diff --git a/src/setup.cpp b/src/setup.cpp index b616437..aa3f80f 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -13,6 +13,7 @@ #include "thingset.h" // handles access to internal data via communication interfaces +#include "board.h" #include "half_bridge.h" // PWM generation for DC/DC converter #include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc. #include "dcdc.h" // DC/DC converter control (hardware independent) @@ -28,7 +29,7 @@ DcBus lv_bus; PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT) -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC DcBus hv_bus; PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT) #if CONFIG_HV_TERMINAL_NANOGRID @@ -40,15 +41,15 @@ Dcdc dcdc(&hv_bus, &lv_bus, DCDC_MODE_BUCK); #endif // CONFIG_HV_TERMINAL #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT PwmSwitch pwm_switch(&lv_bus); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT LoadOutput load(&lv_bus, &load_out_set, &load_out_init, NULL); #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT LoadOutput usb_pwr(&lv_bus, &usb_out_set, &usb_out_init, &pgood_check); #endif diff --git a/src/setup.h b/src/setup.h index 02a4a5f..8978944 100644 --- a/src/setup.h +++ b/src/setup.h @@ -14,17 +14,18 @@ #include "power_port.h" #include "pwm_switch.h" #include "thingset.h" +#include "board.h" extern DcBus lv_bus; extern PowerPort lv_terminal; -#if DT_NODE_EXISTS(DT_PATH(dcdc)) +#if BOARD_HAS_DCDC extern DcBus hv_bus; extern PowerPort hv_terminal; extern Dcdc dcdc; #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) +#if BOARD_HAS_PWM_PORT extern PwmSwitch pwm_switch; #endif @@ -37,11 +38,11 @@ extern Charger charger; extern BatConf bat_conf; extern BatConf bat_conf_user; -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#if BOARD_HAS_LOAD_OUTPUT extern LoadOutput load; #endif -#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#if BOARD_HAS_USB_OUTPUT extern LoadOutput usb_pwr; #endif