diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 47b7ad7..5159bbb 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -19,10 +19,10 @@ extern DeviceStatus dev_stat; extern LoadOutput load; // DISCHARGE_CURRENT_MAX used to estimate current-compensation of load disconnect voltage -#ifdef DT_OUTPUTS_LOAD_CURRENT_MAX -#define DISCHARGE_CURRENT_MAX DT_OUTPUTS_LOAD_CURRENT_MAX +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#define DISCHARGE_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max) #else -#define DISCHARGE_CURRENT_MAX DT_INST_0_DCDC_CURRENT_MAX +#define DISCHARGE_CURRENT_MAX DT_PROP(DT_PATH(dcdc), current_max) #endif void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capacity) @@ -304,7 +304,9 @@ void Charger::enter_state(int next_state) void Charger::discharge_control(BatConf *bat_conf) { -#if DT_OUTPUTS_LOAD_PRESENT || DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) || \ + DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) + if (!empty) { // as we don't have a proper SOC estimation, we determine an empty battery by the main // load output being switched off @@ -377,7 +379,7 @@ void Charger::discharge_control(BatConf *bat_conf) } } -#endif // DT_OUTPUTS_LOAD_PRESENT || DT_OUTPUTS_USB_PWR_PRESENT +#endif } void Charger::charge_control(BatConf *bat_conf) diff --git a/src/daq.cpp b/src/daq.cpp index f599b00..ad0cd31 100644 --- a/src/daq.cpp +++ b/src/daq.cpp @@ -16,13 +16,13 @@ #include "debug.h" #include "board.h" // contains defines for pins -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) static float dcdc_current_offset; #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) static float pwm_current_offset; #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) static float load_current_offset; #endif @@ -72,13 +72,13 @@ static inline float ntc_temp(uint32_t channel, int32_t vref) void calibrate_current_sensors() { int vref = VREF; -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) dcdc_current_offset = -adc_scaled(ADC_POS(i_dcdc), vref, ADC_GAIN(i_dcdc)); #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) pwm_current_offset = -adc_scaled(ADC_POS(i_pwm), vref, ADC_GAIN(i_pwm)); #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load_current_offset = -adc_scaled(ADC_POS(i_load), vref, ADC_GAIN(i_load)); #endif } @@ -89,7 +89,7 @@ void adc_update_value(unsigned int pos) // y(n) = c * x(n) + (c - 1) * y(n-1) // see also here: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) if (pos == ADC_POS(v_pwm) || pos == ADC_POS(i_pwm)) { // only read input voltage and current when switch is on or permanently off if (pwm_switch.signal_high() || pwm_switch.active() == false) { @@ -140,23 +140,23 @@ 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_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) hv_bus.voltage = adc_scaled(ADC_POS(v_high), vref, ADC_GAIN(v_high)); #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) 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_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load.current = adc_scaled(ADC_POS(i_load), vref, ADC_GAIN(i_load)) + load_current_offset; float load_current = load.current; #else float load_current = 0; // value used below, so we still need to define the variable #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) // 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() * ( @@ -167,7 +167,7 @@ void daq_update() pwm_switch.power = pwm_switch.bus->voltage * pwm_switch.current; #endif -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) dcdc_lv_port.current = adc_scaled(ADC_POS(i_dcdc), vref, ADC_GAIN(i_dcdc)) + dcdc_current_offset; @@ -180,7 +180,7 @@ void daq_update() #endif lv_terminal.power = lv_terminal.bus->voltage * lv_terminal.current; -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load.power = load.bus->voltage * load.current; #endif @@ -223,10 +223,10 @@ void daq_update() void high_voltage_alert() { // disable any sort of input -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) dcdc.emergency_stop(); #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) pwm_switch.emergency_stop(); #endif // do not use enter_state function, as we don't want to wait entire recharge delay @@ -240,7 +240,7 @@ void high_voltage_alert() void low_voltage_alert() { -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) // the battery undervoltage must have been caused by a load current peak load.stop(ERR_LOAD_VOLTAGE_DIP); #endif diff --git a/src/daq_driver.c b/src/daq_driver.c index 991a611..2b967e2 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -244,12 +244,14 @@ static void adc_init(ADC_TypeDef *adc) LL_ADC_Enable(adc); } +#define V_HIGH_ENABLE_GPIO DT_CHILD(DT_PATH(adc_inputs), v_high) + static void adc_setup() { -#ifdef DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_PIN - struct device *dev = device_get_binding(DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_PIN, - DT_ADC_INPUTS_V_HIGH_ENABLE_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); +#if DT_NODE_EXISTS(DT_PROP(V_HIGH_ENABLE_GPIO, enable_gpios)) + struct device *dev = device_get_binding(DT_GPIO_LABEL(V_HIGH_ENABLE_GPIO, enable_gpios)); + gpio_pin_configure(dev, DT_GPIO_PIN(V_HIGH_ENABLE_GPIO, enable_gpios), + DT_GPIO_FLAGS(V_HIGH_ENABLE_GPIO, enable_gpios) | GPIO_OUTPUT_ACTIVE); #endif adc_init(ADC1); diff --git a/src/data_nodes.cpp b/src/data_nodes.cpp index 4c03963..148bc95 100644 --- a/src/data_nodes.cpp +++ b/src/data_nodes.cpp @@ -30,8 +30,8 @@ #include "eeprom.h" const char* const manufacturer = "Libre Solar"; -const char* const device_type = DT_CHARGE_CONTROLLER_PCB_TYPE; -const char* const hardware_version = DT_CHARGE_CONTROLLER_PCB_VERSION_STR; +const char* const device_type = DT_PROP(DT_PATH(pcb), type); +const char* const hardware_version = DT_PROP(DT_PATH(pcb), version_str); const char* const firmware_version = "0.1"; const char* const firmware_commit = COMMIT_HASH; char device_id[9]; @@ -166,7 +166,7 @@ static DataNode data_nodes[] = { ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), // load settings -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_BOOL(0x50, "LoadEnDefault", &load.enable, ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), @@ -183,7 +183,7 @@ static DataNode data_nodes[] = { ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), #endif -#if DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) TS_NODE_BOOL(0x55, "UsbEnDefault", &usb_pwr.enable, ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), @@ -202,22 +202,22 @@ static DataNode data_nodes[] = { TS_NODE_PATH(ID_INPUT, "input", 0, NULL), -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_BOOL(0x61, "LoadEn", &load.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif -#if DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) TS_NODE_BOOL(0x62, "UsbEn", &usb_pwr.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) TS_NODE_BOOL(0x63, "DcdcEn", &dcdc.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) TS_NODE_BOOL(0x64, "PwmEn", &pwm_switch.enable, ID_INPUT, TS_ANY_R | TS_ANY_W, 0), #endif @@ -274,7 +274,7 @@ static DataNode data_nodes[] = { TS_NODE_UINT32(0x7C, "ChgState", &charger.state, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) TS_NODE_UINT16(0x7D, "DCDCState", &dcdc.state, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), #endif @@ -297,7 +297,7 @@ static DataNode data_nodes[] = { ID_OUTPUT, TS_ANY_R, 0), #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_FLOAT(0x89, "Load_A", &load.current, 2, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), @@ -308,7 +308,7 @@ static DataNode data_nodes[] = { ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), #endif -#if DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) TS_NODE_INT32(0x8C, "UsbInfo", &usb_pwr.info, ID_OUTPUT, TS_ANY_R, PUB_SER | PUB_CAN), #endif @@ -330,7 +330,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_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_UINT32(0x09, "LoadOutTotal_Wh", &dev_stat.load_out_total_Wh, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), #endif @@ -353,7 +353,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_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_UINT16(0x10, "LoadMaxDay_W", &dev_stat.load_power_max_day, ID_REC, TS_ANY_R | TS_MKR_W, 0), #endif @@ -363,7 +363,7 @@ static DataNode data_nodes[] = { ID_REC, TS_ANY_R | TS_MKR_W, PUB_SER | PUB_CAN), #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_FLOAT(0xA2, "LoadOutDay_Wh", &load.pos_energy_Wh, 2, ID_REC, TS_ANY_R | TS_MKR_W, PUB_SER | PUB_CAN), #endif @@ -387,7 +387,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_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_UINT16(0xB2, "LoadMaxTotal_W", &dev_stat.load_power_max_total, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), #endif @@ -401,7 +401,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_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) TS_NODE_FLOAT(0xB6, "LoadMaxTotal_A", &dev_stat.load_current_max, 2, ID_REC, TS_ANY_R | TS_MKR_W, PUB_NVM), #endif @@ -420,7 +420,7 @@ static DataNode data_nodes[] = { TS_NODE_PATH(ID_CAL, "cal", 0, NULL), -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) TS_NODE_FLOAT(0xD1, "DcdcMin_W", &dcdc.output_power_min, 1, ID_CAL, TS_ANY_R | TS_MKR_W, PUB_NVM), @@ -465,7 +465,7 @@ void data_nodes_update_conf() if (battery_conf_check(&bat_conf_user)) { printf("New config valid and activated.\n"); battery_conf_overwrite(&bat_conf_user, &bat_conf, &charger); -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) 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 af85fc8..f832884 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -25,11 +25,11 @@ extern DeviceStatus dev_stat; -#if !DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) -Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) {} - -#else +#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) +#endif Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) { @@ -38,11 +38,11 @@ Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) mode = op_mode; enable = true; state = DCDC_STATE_OFF; - lvs->neg_current_limit = -DT_INST_0_DCDC_CURRENT_MAX; - lvs->pos_current_limit = DT_INST_0_DCDC_CURRENT_MAX; - ls_current_max = DT_INST_0_DCDC_CURRENT_MAX; - hs_voltage_max = DT_CHARGE_CONTROLLER_PCB_HS_VOLTAGE_MAX; - ls_voltage_max = DT_CHARGE_CONTROLLER_PCB_LS_VOLTAGE_MAX; + lvs->neg_current_limit = -DT_PROP(DT_PATH(dcdc), current_max); + lvs->pos_current_limit = DT_PROP(DT_PATH(dcdc), current_max); + ls_current_max = DT_PROP(DT_PATH(dcdc), current_max); + hs_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max); + ls_voltage_max = DT_PROP(DT_PATH(pcb), hs_voltage_max); ls_voltage_min = 9.0; output_power_min = 1; // switch off if power < 1 W restart_interval = 60; @@ -50,8 +50,8 @@ Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) 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_INST_0_DCDC_PWM_FREQUENCY / 1000, DT_INST_0_DCDC_PWM_DEADTIME, - 12 / hs_voltage_max, 0.97); + half_bridge_init(DT_PROP(DT_PATH(dcdc), pwm_frequency) / 1000, + DT_PROP(DT_PATH(dcdc), pwm_deadtime), 12 / hs_voltage_max, 0.97); } int Dcdc::perturb_observe_controller() @@ -198,7 +198,8 @@ __weak void Dcdc::control() // high-side MOSFET must be broken --> set flag and let main() decide // what to do... (e.g. call dcdc_self_destruction) current_debounce_counter++; - if (current_debounce_counter > CONFIG_CONTROL_FREQUENCY) { // waited 1s before setting the flag + // waited 1s before setting the flag + if (current_debounce_counter > CONFIG_CONTROL_FREQUENCY) { dev_stat.set_error(ERR_DCDC_HS_MOSFET_SHORT); } } @@ -296,20 +297,24 @@ void Dcdc::fuse_destruction() void Dcdc::output_hvs_enable() { -#ifdef DT_OUTPUTS_HV_EXT_SENSE_GPIOS_CONTROLLER - struct device *dev_hv_ext = device_get_binding(DT_OUTPUTS_HV_EXT_SENSE_GPIOS_CONTROLLER); - gpio_pin_configure(dev_hv_ext, DT_OUTPUTS_HV_EXT_SENSE_GPIOS_PIN, - DT_OUTPUTS_HV_EXT_SENSE_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_ext_sense)) + struct device *dev_hv_ext = device_get_binding(DT_GPIO_LABEL(HV_EXT_SENSE_GPIO, gpios)); + gpio_pin_configure(dev_hv_ext, DT_GPIO_PIN(HV_EXT_SENSE_GPIO, gpios), + DT_GPIO_FLAGS(HV_EXT_SENSE_GPIO, gpios) | GPIO_OUTPUT_ACTIVE); #endif } void Dcdc::output_hvs_disable() { -#ifdef DT_OUTPUTS_HV_EXT_SENSE_GPIOS_CONTROLLER - struct device *dev_hv_ext = device_get_binding(DT_OUTPUTS_HV_EXT_SENSE_GPIOS_CONTROLLER); - gpio_pin_configure(dev_hv_ext, DT_OUTPUTS_HV_EXT_SENSE_GPIOS_PIN, - DT_OUTPUTS_HV_EXT_SENSE_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), hv_ext_sense)) + struct device *dev_hv_ext = device_get_binding(DT_GPIO_LABEL(HV_EXT_SENSE_GPIO, gpios)); + gpio_pin_configure(dev_hv_ext, DT_GPIO_PIN(HV_EXT_SENSE_GPIO, gpios), + DT_GPIO_FLAGS(HV_EXT_SENSE_GPIO, gpios) | GPIO_OUTPUT_INACTIVE); #endif } -#endif /* DT_COMPAT_DCDC */ +#else + +Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) {} + +#endif /* DT_NODE_EXISTS(DT_PATH(dcdc)) */ diff --git a/src/device_status.cpp b/src/device_status.cpp index b352597..362b2aa 100644 --- a/src/device_status.cpp +++ b/src/device_status.cpp @@ -58,7 +58,7 @@ void DeviceStatus::update_energy() bat_chg_total_Wh_prev = bat_chg_total_Wh; bat_dis_total_Wh_prev = bat_dis_total_Wh; solar_terminal.neg_energy_Wh = 0.0; - #if DT_OUTPUTS_LOAD_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load.pos_energy_Wh = 0.0; #endif bat_terminal.pos_energy_Wh = 0.0; @@ -78,7 +78,7 @@ void DeviceStatus::update_energy() (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0); #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load_out_total_Wh = load_out_total_Wh_prev + (load.pos_energy_Wh > 0 ? load.pos_energy_Wh : 0); #endif @@ -100,7 +100,7 @@ void DeviceStatus::update_min_max_values() } #endif -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) if (dcdc.lvs->current > dcdc_current_max) { dcdc_current_max = dcdc.lvs->current; } @@ -110,7 +110,7 @@ void DeviceStatus::update_min_max_values() } #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) if (load.current > load_current_max) { load_current_max = load.current; } @@ -125,7 +125,7 @@ void DeviceStatus::update_min_max_values() } #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) 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/can.cpp b/src/ext/can.cpp index 6d9ec61..e7b1bfb 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -24,6 +24,10 @@ #define CAN_NODE_ID 20 #endif +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), can_en)) +#define CAN_EN_GPIO DT_CHILD(DT_PATH(outputs), can_en) +#endif + extern ThingSet ts; struct device *can_dev; @@ -160,16 +164,16 @@ ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c) { - can_en_dev = device_get_binding(DT_OUTPUTS_CAN_EN_GPIOS_CONTROLLER); - gpio_pin_configure(can_en_dev, DT_OUTPUTS_CAN_EN_GPIOS_PIN, - DT_OUTPUTS_CAN_EN_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); + can_en_dev = device_get_binding(DT_GPIO_LABEL(CAN_EN_GPIO, gpios)); + gpio_pin_configure(can_en_dev, DT_GPIO_PIN(CAN_EN_GPIO, gpios), + DT_GPIO_FLAGS(CAN_EN_GPIO, gpios) | GPIO_OUTPUT_INACTIVE); can_dev = device_get_binding("CAN_1"); } void ThingSetCAN::enable() { - gpio_pin_set(can_en_dev, DT_OUTPUTS_CAN_EN_GPIOS_PIN, 1); + gpio_pin_set(can_en_dev, DT_GPIO_PIN(CAN_EN_GPIO, gpios), 1); #ifdef CONFIG_ISOTP k_tid_t tid = k_thread_create(&rx_thread_data, rx_thread_stack, diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 22cf771..c32dfe2 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -52,15 +52,18 @@ const unsigned char bmp_disconnected [] = { 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const char i2c_dev[] = DT_ALIAS_I2C_UEXT_LABEL; -OledSSD1306 oled(i2c_dev); +OledSSD1306 oled(DT_LABEL(DT_ALIAS(i2c_uext))); + +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en)) +#define UEXT_EN_GPIO DT_CHILD(DT_PATH(outputs), uext_en) +#endif void ExtOled::enable() { -#ifdef DT_OUTPUTS_UEXT_EN_PRESENT - struct device *dev_uext_en = device_get_binding(DT_OUTPUTS_UEXT_EN_GPIOS_CONTROLLER); - gpio_pin_configure(dev_uext_en, DT_OUTPUTS_UEXT_EN_GPIOS_PIN, - DT_OUTPUTS_UEXT_EN_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), uext_en)) + struct device *dev_uext_en = device_get_binding(DT_GPIO_LABEL(UEXT_EN_GPIO, gpios)); + gpio_pin_configure(dev_uext_en, DT_GPIO_PIN(UEXT_EN_GPIO, gpios), + DT_GPIO_FLAGS(UEXT_EN_GPIO, gpios) | GPIO_OUTPUT_ACTIVE); #endif oled.init(CONFIG_EXT_OLED_BRIGHTNESS); @@ -116,8 +119,8 @@ void ExtOled::process_1s() } // solar panel data -#ifdef CHARGER_TYPE_PWM - if (pwm_switch_enabled()) { +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) + if (pwm_switch.active()) { #else if (half_bridge_enabled()) { #endif @@ -131,7 +134,7 @@ void ExtOled::process_1s() len = snprintf(buf, sizeof(buf), "n/a"); oled.writeString(buf, len); } -#ifndef CHARGER_TYPE_PWM +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) if (in_terminal.bus->voltage > bat_terminal.bus->voltage) #endif { @@ -169,8 +172,8 @@ void ExtOled::process_1s() oled.setTextCursor(0, 56); -#ifdef CHARGER_TYPE_PWM - bool pwm_enabled = pwm_switch.active()(); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) + bool pwm_enabled = pwm_switch.active(); float duty_cycle = pwm_switch.get_duty_cycle(); #else bool pwm_enabled = half_bridge_enabled(); diff --git a/src/ext/serial.cpp b/src/ext/serial.cpp index 0cee98b..e12d1b9 100644 --- a/src/ext/serial.cpp +++ b/src/ext/serial.cpp @@ -22,7 +22,7 @@ #define TX_BUF_SIZE CONFIG_EXT_THINGSET_SERIAL_TX_BUF_SIZE #define RX_BUF_SIZE CONFIG_EXT_THINGSET_SERIAL_RX_BUF_SIZE -#define UART_DEVICE_NAME DT_ALIAS_UART_DBG_LABEL +#define UART_DEVICE_NAME DT_LABEL(DT_ALIAS(uart_dbg)) struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); class ThingSetStream: public ExtInterface diff --git a/src/hardware.cpp b/src/hardware.cpp index 3cf617b..5925ce1 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -26,6 +26,10 @@ #ifndef UNIT_TEST +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0)) +#define BOOT0_GPIO DT_CHILD(DT_PATH(outputs), boot0) +#endif + #include #include #include @@ -41,7 +45,7 @@ int hw_wdt_channel; void watchdog_init() { - wdt = device_get_binding(DT_INST_0_ST_STM32_WATCHDOG_LABEL); + wdt = device_get_binding(DT_LABEL(DT_NODELABEL(iwdg))); if (!wdt) { printk("Cannot get WDT device\n"); return; @@ -99,15 +103,16 @@ void sw_watchdog_start() void start_stm32_bootloader() { -#ifdef DT_OUTPUTS_BOOT0_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), boot0)) + // pin is connected to BOOT0 via resistor and capacitor - struct device *dev = device_get_binding(DT_OUTPUTS_BOOT0_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_OUTPUTS_BOOT0_GPIOS_PIN, - DT_OUTPUTS_BOOT0_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); + struct device *dev = device_get_binding(DT_GPIO_LABEL(BOOT0_GPIO, gpios)); + gpio_pin_configure(dev, DT_GPIO_PIN(BOOT0_GPIO, gpios), + DT_GPIO_FLAGS(BOOT0_GPIO, gpios) | GPIO_OUTPUT_ACTIVE); k_sleep(K_MSEC(100)); // wait for capacitor at BOOT0 pin to charge up reset_device(); -#elif defined (CONFIG_SOC_SERIES_STM32F0X) +#elif defined(CONFIG_SOC_SERIES_STM32F0X) // place magic code at end of RAM and initiate reset *((uint32_t *)(MAGIC_CODE_ADDR)) = 0xDEADBEEF; reset_device(); diff --git a/src/load.cpp b/src/load.cpp index bea82a5..3308618 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -8,7 +8,7 @@ #include -#if DT_OUTPUTS_LOAD_PRESENT || UNIT_TEST +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) || UNIT_TEST #include "board.h" #include "leds.h" @@ -16,6 +16,13 @@ #include "debug.h" #include "helper.h" +#define LOAD_CURRENT_MAX DT_PROP(DT_CHILD(DT_PATH(outputs), load), 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 DeviceStatus dev_stat; LoadOutput::LoadOutput(DcBus *dc_bus, void (*drv_switch_fn)(bool), void (*drv_init_fn)()) : @@ -42,14 +49,13 @@ 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 / - (DT_OUTPUTS_LOAD_CURRENT_MAX * DT_OUTPUTS_LOAD_CURRENT_MAX) * - (DT_CHARGE_CONTROLLER_PCB_MOSFETS_TJ_MAX - DT_CHARGE_CONTROLLER_PCB_INTERNAL_TREF_MAX) - ) / (DT_CHARGE_CONTROLLER_PCB_MOSFETS_TAU_JA * CONFIG_CONTROL_FREQUENCY); + 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 > DT_CHARGE_CONTROLLER_PCB_MOSFETS_TJ_MAX || - current > DT_OUTPUTS_LOAD_CURRENT_MAX * 2) + if (junction_temperature > PCB_MOSFETS_TJ_MAX || + current > LOAD_CURRENT_MAX * 2) { flags_set(&error_flags, ERR_LOAD_OVERCURRENT); oc_timestamp = uptime(); @@ -68,7 +74,7 @@ void LoadOutput::control() // 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 > bus->series_voltage(overvoltage) || - bus->voltage > DT_CHARGE_CONTROLLER_PCB_LS_VOLTAGE_MAX) + bus->voltage > PCB_LS_VOLTAGE_MAX) { ov_debounce_counter++; if (ov_debounce_counter > CONFIG_CONTROL_FREQUENCY) { @@ -107,7 +113,7 @@ void LoadOutput::control() if (flags_check(&error_flags, ERR_LOAD_OVERVOLTAGE) && bus->voltage < (bus->series_voltage(overvoltage) - ov_hysteresis) && - bus->voltage < (DT_CHARGE_CONTROLLER_PCB_LS_VOLTAGE_MAX - ov_hysteresis)) + bus->voltage < (PCB_LS_VOLTAGE_MAX - ov_hysteresis)) { flags_clear(&error_flags, ERR_LOAD_OVERVOLTAGE); } @@ -159,4 +165,4 @@ void load_short_circuit_stop() load.stop(ERR_LOAD_SHORT_CIRCUIT); } -#endif /* DT_OUTPUTS_LOAD_PRESENT */ +#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) */ diff --git a/src/load_driver.c b/src/load_driver.c index c251c84..7fe9e80 100644 --- a/src/load_driver.c +++ b/src/load_driver.c @@ -21,11 +21,13 @@ #include #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) +#define LOAD_GPIO DT_CHILD(DT_PATH(outputs), load) static struct device *dev_load; #endif -#if DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) +#define USB_GPIO DT_CHILD(DT_PATH(outputs), usb_pwr) static struct device *dev_usb; #endif @@ -155,37 +157,37 @@ void load_out_set(bool status) leds_set(LED_LOAD, status, -1); #endif -#ifdef DT_OUTPUTS_LOAD_PRESENT - gpio_pin_configure(dev_load, DT_OUTPUTS_LOAD_GPIOS_PIN, - DT_OUTPUTS_LOAD_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + gpio_pin_configure(dev_load, DT_GPIO_PIN(LOAD_GPIO, gpios), + DT_GPIO_FLAGS(LOAD_GPIO, gpios) | GPIO_OUTPUT_INACTIVE); if (status == true) { #ifdef PIN_I_LOAD_COMP lptim_init(); #else - gpio_pin_set(dev_load, DT_OUTPUTS_LOAD_GPIOS_PIN, 1); + gpio_pin_set(dev_load, DT_GPIO_PIN(LOAD_GPIO, gpios), 1); #endif } else { - gpio_pin_set(dev_load, DT_OUTPUTS_LOAD_GPIOS_PIN, 0); + gpio_pin_set(dev_load, DT_GPIO_PIN(LOAD_GPIO, gpios), 0); } #endif } void usb_out_set(bool status) { -#ifdef DT_OUTPUTS_USB_PWR_PRESENT - gpio_pin_configure(dev_usb, DT_OUTPUTS_USB_PWR_GPIOS_PIN, - DT_OUTPUTS_USB_PWR_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) + gpio_pin_configure(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), + DT_GPIO_FLAGS(USB_GPIO, gpios) | GPIO_OUTPUT_INACTIVE); if (status == true) { - gpio_pin_set(dev_usb, DT_OUTPUTS_USB_PWR_GPIOS_PIN, 1); + gpio_pin_set(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), 1); } else { - gpio_pin_set(dev_usb, DT_OUTPUTS_USB_PWR_GPIOS_PIN, 0); + gpio_pin_set(dev_usb, DT_GPIO_PIN(USB_GPIO, gpios), 0); } #endif } -#ifdef DT_OUTPUTS_CHARGE_PUMP_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump)) #if defined(CONFIG_SOC_STM32G431XX) #include @@ -194,10 +196,13 @@ void usb_out_set(bool status) #include "stm32g431xx.h" #endif +#define CP_PWM_PERIOD (DT_PHA(DT_CHILD(DT_PATH(outputs), charge_pump), pwms, period)) +#define CP_PWM_CHANNEL (DT_PHA(DT_CHILD(DT_PATH(outputs), charge_pump), pwms, channel)) + /* Currently hard-coded for TIM8 as Zephyr driver doesn't work with this timer at the moment */ void load_cp_enable() { - int freq_Hz = 1000*1000*1000 / DT_OUTPUTS_CHARGE_PUMP_PWMS_PERIOD; + int freq_Hz = 1000*1000*1000 / CP_PWM_PERIOD; LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM8); @@ -239,22 +244,21 @@ void load_cp_enable() return; } // set to 50% duty cycle - pwm_pin_set_nsec(pwm_dev, DT_OUTPUTS_CHARGE_PUMP_PWMS_CHANNEL, - DT_OUTPUTS_CHARGE_PUMP_PWMS_PERIOD, DT_OUTPUTS_CHARGE_PUMP_PWMS_PERIOD / 2, 0); + pwm_pin_set_nsec(pwm_dev, CP_PWM_CHANNEL, CP_PWM_PERIOD, CP_PWM_PERIOD / 2, 0); */ } #endif void load_out_init() { -#ifdef DT_OUTPUTS_LOAD_PRESENT - dev_load = device_get_binding(DT_OUTPUTS_LOAD_GPIOS_CONTROLLER); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) + dev_load = device_get_binding(DT_GPIO_LABEL(LOAD_GPIO, gpios)); #endif // analog comparator to detect short circuits and trigger immediate load switch-off short_circuit_comp_init(); -#ifdef DT_OUTPUTS_CHARGE_PUMP_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), charge_pump)) // enable charge pump for high-side switches (if existing) load_cp_enable(); #endif @@ -262,8 +266,8 @@ void load_out_init() void usb_out_init() { -#ifdef DT_OUTPUTS_USB_PWR_PRESENT - dev_usb = device_get_binding(DT_OUTPUTS_USB_PWR_GPIOS_CONTROLLER); +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) + dev_usb = device_get_binding(DT_GPIO_LABEL(USB_GPIO, gpios)); #endif } diff --git a/src/main.cpp b/src/main.cpp index 0549bf5..d3ad83b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,12 +61,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_OUTPUTS_LOAD_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max); #endif - #if DT_OUTPUTS_USB_PWR_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) 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,7 +82,7 @@ void main(void) leds_update_1s(); - #if DT_OUTPUTS_LOAD_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) leds_update_soc(charger.soc, flags_check(&load.error_flags, ERR_LOAD_SHEDDING)); #else leds_update_soc(charger.soc, false); @@ -113,12 +113,12 @@ void control_thread() lv_terminal.update_bus_current_margins(); - #if DT_OUTPUTS_PWM_SWITCH_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) pwm_switch.control(); charging |= pwm_switch.active(); #endif - #if DT_COMPAT_DCDC + #if DT_NODE_EXISTS(DT_PATH(dcdc)) hv_terminal.update_bus_current_margins(); dcdc.control(); // control of DC/DC including MPPT algorithm charging |= half_bridge_enabled(); @@ -126,11 +126,11 @@ void control_thread() leds_set_charging(charging); - #if DT_OUTPUTS_LOAD_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) load.control(); #endif - #if DT_OUTPUTS_USB_PWR_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) usb_pwr.control(); #endif @@ -139,13 +139,13 @@ void control_thread() last_call = now; // energy + soc calculation must be called exactly once per second - #if DT_COMPAT_DCDC + #if DT_NODE_EXISTS(DT_PATH(dcdc)) if (dcdc.state != DCDC_STATE_OFF) { hv_terminal.energy_balance(); } #endif - #if DT_OUTPUTS_PWM_SWITCH_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) if (pwm_switch.active() == 1) { pwm_switch.energy_balance(); } @@ -153,7 +153,7 @@ void control_thread() lv_terminal.energy_balance(); - #if DT_OUTPUTS_LOAD_PRESENT + #if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) if (load.state == 1) { load.energy_balance(); } @@ -163,7 +163,7 @@ void control_thread() dev_stat.update_min_max_values(); charger.update_soc(&bat_conf); - #if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && DT_COMPAT_DCDC + #if CONFIG_HS_MOSFET_FAIL_SAFE_PROTECTION && DT_NODE_EXISTS(DT_PATH(dcdc)) if (dev_stat.has_error(ERR_DCDC_HS_MOSFET_SHORT)) { dcdc.fuse_destruction(); } diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index cfd959d..b5fa5e7 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -17,7 +17,10 @@ #include "debug.h" #include "helper.h" -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) + +#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)) bool PwmSwitch::active() { @@ -39,7 +42,7 @@ PwmSwitch::PwmSwitch(DcBus *dc_bus) : restart_interval = 60; // s When should we retry to start charging after low solar power cut-off? // period stored in nanoseconds - pwm_signal_init_registers(1000*1000*1000/DT_OUTPUTS_PWM_SWITCH_PWMS_PERIOD); + pwm_signal_init_registers(1000 * 1000 * 1000 / PWM_PERIOD); enable = true; // enable charging in general } @@ -78,7 +81,7 @@ void PwmSwitch::control() } else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target || current < neg_current_limit // port current limit exceeded - || current < -DT_OUTPUTS_LOAD_CURRENT_MAX) // PCB current limit exceeded + || current < -PWM_CURRENT_MAX) // PCB current limit exceeded { // decrease power, as limits were reached @@ -137,4 +140,4 @@ float PwmSwitch::get_duty_cycle() return pwm_signal_get_duty_cycle(); } -#endif /* DT_OUTPUTS_PWM_SWITCH_PRESENT */ +#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) */ diff --git a/src/pwm_switch_driver.c b/src/pwm_switch_driver.c index 471ee0b..47b979f 100644 --- a/src/pwm_switch_driver.c +++ b/src/pwm_switch_driver.c @@ -28,26 +28,26 @@ #include "stm32g431xx.h" #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) #ifndef UNIT_TEST // all PWM charge controllers use TIM3 at the moment static TIM_TypeDef *tim = TIM3; -#if DT_OUTPUTS_PWM_SWITCH_PWMS_CHANNEL == 1 +#if DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 1 #define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH1 #define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH1 #define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH1 -#elif DT_OUTPUTS_PWM_SWITCH_PWMS_CHANNEL == 2 +#elif DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 2 #define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH2 #define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH2 #define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH2 -#elif DT_OUTPUTS_PWM_SWITCH_PWMS_CHANNEL == 3 +#elif DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 3 #define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH3 #define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH3 #define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH3 -#elif DT_OUTPUTS_PWM_SWITCH_PWMS_CHANNEL == 4 +#elif DT_PWMS_CHANNEL(DT_CHILD(DT_PATH(outputs), pwm_switch)) == 4 #define LL_TIM_CHANNEL LL_TIM_CHANNEL_CH4 #define LL_TIM_OC_SetCompare LL_TIM_OC_SetCompareCH4 #define LL_TIM_OC_GetCompare LL_TIM_OC_GetCompareCH4 @@ -161,4 +161,4 @@ bool pwm_active() { return false; } #endif -#endif /* DT_OUTPUTS_PWM_SWITCH_PRESENT */ +#endif /* DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) */ diff --git a/src/setup.cpp b/src/setup.cpp index 1c1cee5..0a83941 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -30,7 +30,7 @@ DcBus lv_bus; PowerPort lv_terminal(&lv_bus, true); // low voltage terminal (battery for typical MPPT) -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) DcBus hv_bus; PowerPort hv_terminal(&hv_bus, true); // high voltage terminal (solar for typical MPPT) PowerPort dcdc_lv_port(&lv_bus); // internal low voltage side of DC/DC converter @@ -43,15 +43,15 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); #endif // CONFIG_HV_TERMINAL #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) PwmSwitch pwm_switch(&lv_bus); #endif -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) LoadOutput load(&lv_bus, &load_out_set, &load_out_init); #endif -#if DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) LoadOutput usb_pwr(&lv_bus, &usb_out_set, &usb_out_init); #endif diff --git a/src/setup.h b/src/setup.h index 9bf9d81..3c8520a 100644 --- a/src/setup.h +++ b/src/setup.h @@ -19,14 +19,14 @@ extern DcBus lv_bus; extern PowerPort lv_terminal; -#if DT_COMPAT_DCDC +#if DT_NODE_EXISTS(DT_PATH(dcdc)) extern DcBus hv_bus; extern PowerPort hv_terminal; extern PowerPort dcdc_lv_port; extern Dcdc dcdc; #endif -#if DT_OUTPUTS_PWM_SWITCH_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) extern PwmSwitch pwm_switch; #endif @@ -39,11 +39,11 @@ extern Charger charger; extern BatConf bat_conf; extern BatConf bat_conf_user; -#if DT_OUTPUTS_LOAD_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), load)) extern LoadOutput load; #endif -#if DT_OUTPUTS_USB_PWR_PRESENT +#if DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), usb_pwr)) extern LoadOutput usb_pwr; #endif diff --git a/test/board.h b/test/board.h index a45165f..c92fb94 100644 --- a/test/board.h +++ b/test/board.h @@ -8,10 +8,10 @@ #define PCB_STUB_H // PWM charge controller -#define CONFIG_PWM_TERMINAL_SOLAR DT_OUTPUTS_PWM_SWITCH_PRESENT +#define CONFIG_PWM_TERMINAL_SOLAR DT_NODE_EXISTS(DT_CHILD(DT_PATH(outputs), pwm_switch)) // MPPT buck/boost or nanogrid mode -#define CONFIG_HV_TERMINAL_SOLAR DT_COMPAT_DCDC +#define CONFIG_HV_TERMINAL_SOLAR DT_NODE_EXISTS(DT_PATH(dcdc)) #define CONFIG_HV_TERMINAL_NANOGRID 0 // battery always assumed to be at low-voltage terminal (might need to be changed for boost mode) @@ -24,50 +24,11 @@ #define CONFIG_DEVICE_ID 12345678 -#define DT_CHARGE_CONTROLLER_PCB_TYPE "PCB-STUB" -#define DT_CHARGE_CONTROLLER_PCB_VERSION_STR "v0.1" - #define CONFIG_THINGSET_EXPERT_PASSWORD "expert123" #define CONFIG_THINGSET_MAKER_PASSWORD "maker456" -// specify features of charge controller -#define DT_COMPAT_DCDC 1 -#define DT_INST_0_DCDC_PWM_FREQUENCY 70000 // Hz -#define DT_INST_0_DCDC_PWM_DEADTIME 300 // ns -#define DT_INST_0_DCDC_CURRENT_MAX 20 // A - -#define DT_OUTPUTS_PWM_SWITCH_PRESENT 1 -#define DT_OUTPUTS_PWM_SWITCH_PWMS_PERIOD (20*1000*1000) -#define DT_OUTPUTS_PWM_SWITCH_CURRENT_MAX 20 // A - -#define DT_OUTPUTS_LOAD_PRESENT 1 -#define DT_OUTPUTS_LOAD_CURRENT_MAX 20 // PCB maximum load switch current - -#define DT_OUTPUTS_USB_PWR_PRESENT 1 - // Values that are otherwise defined by Kconfig #define CONFIG_CONTROL_FREQUENCY 10 // Hz -#define DT_CHARGE_CONTROLLER_PCB_MOSFETS_TJ_MAX 120 -#define DT_CHARGE_CONTROLLER_PCB_INTERNAL_TREF_MAX 50 -#define DT_CHARGE_CONTROLLER_PCB_MOSFETS_TAU_JA 5 - -#define DT_CHARGE_CONTROLLER_PCB_LS_VOLTAGE_MAX 32 // Maximum voltage at battery port (V) -#define DT_CHARGE_CONTROLLER_PCB_HS_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V) - -#define PIN_UEXT_TX 0 -#define PIN_UEXT_RX 0 -#define PIN_UEXT_SCL 0 -#define PIN_UEXT_SDA 0 -#define PIN_UEXT_MISO 0 -#define PIN_UEXT_MOSI 0 -#define PIN_UEXT_SCK 0 -#define PIN_UEXT_SSEL 0 - -#define PIN_SWD_TX 0 -#define PIN_SWD_RX 0 - -//#define PIN_LOAD_DIS 0 -//#define PIN_USB_PWR_DIS 0 enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT }; diff --git a/test/devicetree.h b/test/devicetree.h new file mode 100644 index 0000000..6936e25 --- /dev/null +++ b/test/devicetree.h @@ -0,0 +1,46 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +/* + * Macros copied from devicetree.h and util.h + */ + +#define DT_NODE_EXISTS(node_id) DT_CAT(node_id, _EXISTS) + +#define DT_PROP(node_id, prop) DT_CAT(node_id, _P_##prop) +#define DT_PROP_LEN(node_id, prop) DT_PROP(node_id, prop##_LEN) + +#define DT_CHILD(node_id, child) UTIL_CAT(node_id, DT_S_PREFIX(child)) + +#define DT_CAT(node_id, prop_suffix) node_id##prop_suffix + +#define DT_S_PREFIX(name) _S_##name + +#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__) +#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__ + +#define DT_FOREACH_CHILD(node_id, fn) \ + DT_CAT(node_id, _FOREACH_CHILD)(fn) + +#define DT_PHA_BY_IDX(node_id, pha, idx, cell) \ + DT_PROP(node_id, pha##_IDX_##idx##_VAL_##cell) + +#define DT_PHA(node_id, pha, cell) DT_PHA_BY_IDX(node_id, pha, 0, cell) + +#define DT_PHANDLE_BY_IDX(node_id, prop, idx) \ + DT_PROP(node_id, prop##_IDX_##idx##_PH) + +#define DT_PROP_BY_PHANDLE_IDX(node_id, phs, idx, prop) \ + DT_PROP(DT_PHANDLE_BY_IDX(node_id, phs, idx), prop) + +#define DT_PROP_BY_PHANDLE(node_id, ph, prop) \ + DT_PROP_BY_PHANDLE_IDX(node_id, ph, 0, prop) + +/* + * Simplified mocks for certain macros from devicetree.h + */ + +#define DT_PATH(node) UTIL_CAT(DT_N_S_, node) diff --git a/test/devicetree_mock.h b/test/devicetree_mock.h deleted file mode 100644 index b0434fc..0000000 --- a/test/devicetree_mock.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * Copyright (c) 2020 Martin Jäger / Libre Solar - */ - -/* - * Generated defines (from devicetree_unfixed.h) - */ - -#define DT_N_S_adc_inputs_S_temp_bat_EXISTS 1 - -#define DT_N_S_adc_inputs_S_v_high_P_multiplier 105600 -#define DT_N_S_adc_inputs_S_v_high_P_divider 5600 -#define DT_N_S_adc_inputs_S_v_low_P_multiplier 105600 -#define DT_N_S_adc_inputs_S_v_low_P_divider 5600 -#define DT_N_S_adc_inputs_S_v_pwm_P_multiplier 25224 // see pwm_2420_lus.dts -#define DT_N_S_adc_inputs_S_v_pwm_P_divider 984 -#define DT_N_S_adc_inputs_S_v_pwm_P_offset 2338 - -// amp gain: 50, resistor: 4 mOhm -#define DT_N_S_adc_inputs_S_i_load_P_multiplier 1000 -#define DT_N_S_adc_inputs_S_i_load_P_divider (4 * 50) -#define DT_N_S_adc_inputs_S_i_dcdc_P_multiplier 1000 -#define DT_N_S_adc_inputs_S_i_dcdc_P_divider (4 * 50) -#define DT_N_S_adc_inputs_S_i_pwm_P_multiplier 1000 -#define DT_N_S_adc_inputs_S_i_pwm_P_divider (4 * 50) - -/* - * Macros copied from devicetree.h - */ - -#define DT_CAT(node_id, prop_suffix) node_id##prop_suffix - -#define DT_PROP(node_id, prop) DT_CAT(node_id, _P_##prop) - -#define DT_NODE_EXISTS(name) DT_CAT(name, _EXISTS) - -#define DT_CHILD(node_id, child) UTIL_CAT(node_id, DT_S_PREFIX(child)) - -#define DT_CAT(node_id, prop_suffix) node_id##prop_suffix - -#define DT_S_PREFIX(name) _S_##name - -#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__) -#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__ - -/* - * Simplified mocks for certain macros from devicetree.h - */ - -#define DT_PATH(node) DT_N_S_##node - -#define DT_FOREACH_CHILD(dummy, fn) \ - fn(DT_N_S_adc_inputs_S_v_low) \ - fn(DT_N_S_adc_inputs_S_v_high) \ - fn(DT_N_S_adc_inputs_S_v_pwm) \ - fn(DT_N_S_adc_inputs_S_i_dcdc) \ - fn(DT_N_S_adc_inputs_S_i_load) \ - fn(DT_N_S_adc_inputs_S_i_pwm) \ - fn(DT_N_S_adc_inputs_S_temp_bat) \ - fn(DT_N_S_adc_inputs_S_vref_mcu) \ - fn(DT_N_S_adc_inputs_S_temp_mcu) diff --git a/test/devicetree_unfixed.h b/test/devicetree_unfixed.h new file mode 100644 index 0000000..a94ede3 --- /dev/null +++ b/test/devicetree_unfixed.h @@ -0,0 +1,62 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright (c) 2020 Martin Jäger / Libre Solar + */ + +/* + * Needed defines that are normally generated by Zephyr build system + */ + +#define DT_N_S_pcb_P_type "TEST-PCB" +#define DT_N_S_pcb_P_version_str "v0.1" + +#define DT_N_S_pcb_P_hs_voltage_max 80 +#define DT_N_S_pcb_P_ls_voltage_max 32 + +#define DT_N_S_pcb_P_internal_tref_max 50 +#define DT_N_S_pcb_P_mosfets_tj_max 120 +#define DT_N_S_pcb_P_mosfets_tau_ja 5 + +#define DT_N_S_dcdc_EXISTS 1 +#define DT_N_S_dcdc_P_pwm_frequency 70000 +#define DT_N_S_dcdc_P_pwm_deadtime 300 +#define DT_N_S_dcdc_P_current_max 20 + +#define DT_N_S_outputs_S_pwm_switch_EXISTS 1 +#define DT_N_S_outputs_S_pwm_switch_P_current_max 20 +#define DT_N_S_outputs_S_pwm_switch_P_pwms_IDX_0_VAL_period 20000000 + +#define DT_N_S_outputs_S_load_EXISTS 1 +#define DT_N_S_outputs_S_load_P_current_max 20 + +#define DT_N_S_outputs_S_usb_pwr_EXISTS 1 + +#define DT_N_S_adc_inputs_S_temp_bat_EXISTS 1 + +#define DT_N_S_adc_inputs_S_v_high_P_multiplier 105600 +#define DT_N_S_adc_inputs_S_v_high_P_divider 5600 +#define DT_N_S_adc_inputs_S_v_low_P_multiplier 105600 +#define DT_N_S_adc_inputs_S_v_low_P_divider 5600 +#define DT_N_S_adc_inputs_S_v_pwm_P_multiplier 25224 // see pwm_2420_lus.dts +#define DT_N_S_adc_inputs_S_v_pwm_P_divider 984 +#define DT_N_S_adc_inputs_S_v_pwm_P_offset 2338 + +// amp gain: 50, resistor: 4 mOhm +#define DT_N_S_adc_inputs_S_i_load_P_multiplier 1000 +#define DT_N_S_adc_inputs_S_i_load_P_divider (4 * 50) +#define DT_N_S_adc_inputs_S_i_dcdc_P_multiplier 1000 +#define DT_N_S_adc_inputs_S_i_dcdc_P_divider (4 * 50) +#define DT_N_S_adc_inputs_S_i_pwm_P_multiplier 1000 +#define DT_N_S_adc_inputs_S_i_pwm_P_divider (4 * 50) + +#define DT_N_S_adc_inputs_FOREACH_CHILD(fn) \ + fn(DT_N_S_adc_inputs_S_v_low) \ + fn(DT_N_S_adc_inputs_S_v_high) \ + fn(DT_N_S_adc_inputs_S_v_pwm) \ + fn(DT_N_S_adc_inputs_S_i_dcdc) \ + fn(DT_N_S_adc_inputs_S_i_load) \ + fn(DT_N_S_adc_inputs_S_i_pwm) \ + fn(DT_N_S_adc_inputs_S_temp_bat) \ + fn(DT_N_S_adc_inputs_S_vref_mcu) \ + fn(DT_N_S_adc_inputs_S_temp_mcu) diff --git a/test/tests_load.cpp b/test/tests_load.cpp index d36be96..a13d2db 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -135,12 +135,13 @@ void control_on_to_off_overcurrent() LoadOutput load_out(&bus, &load_drv_set, &load_drv_init); load_init(&load_out, true); - load_out.current = DT_OUTPUTS_LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately + // current slightly below factor 2 so that it is not switched off immediately + load_out.current = DT_PROP(DT_CHILD(DT_PATH(outputs), load), current_max) * 1.9; load_out.control(); TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); // almost 2x current = 4x heat generation: Should definitely trigger after waiting one time constant - int trigger_steps = DT_CHARGE_CONTROLLER_PCB_MOSFETS_TAU_JA * CONFIG_CONTROL_FREQUENCY; + int trigger_steps = DT_PROP(DT_PATH(pcb), mosfets_tau_ja) * CONFIG_CONTROL_FREQUENCY; for (int i = 0; i <= trigger_steps; i++) { load_out.control(); } diff --git a/test/zephyr.h b/test/zephyr.h index 8b42bea..b0bb3b5 100644 --- a/test/zephyr.h +++ b/test/zephyr.h @@ -11,7 +11,8 @@ * for unit tests */ -#include "devicetree_mock.h" +#include "devicetree.h" +#include "devicetree_unfixed.h" #ifndef __deprecated #define __deprecated __attribute__((deprecated)) diff --git a/zephyr/dts/bindings/outputs.yaml b/zephyr/dts/bindings/outputs.yaml index 928d257..4b0c0b4 100644 --- a/zephyr/dts/bindings/outputs.yaml +++ b/zephyr/dts/bindings/outputs.yaml @@ -21,9 +21,6 @@ child-binding: type: string required: true description: Human readable string describing the device (used by Zephyr for API name) - present: - type: int - default: 1 kconfig-flag: type: boolean description: This value can be checked by Kconfig parser using dt_node_has_bool_prop function