From ce7f63b888d9bcdb670d49133cb50bf9c31bbe6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Mon, 6 Apr 2020 11:56:04 +0200 Subject: [PATCH] Introduce generic "outputs" DTS binding Replaces previous "switch" and "pwm-out" bindings and allows to reference either gpio or pwm handle and specify maximum current. Also replacing CONFIG_HAS_LOAD and CONFIG_HAS_USB_PWR with DT_ variants. --- boards/arm/mppt_1210_hus/Kconfig.defconfig | 6 --- boards/arm/mppt_1210_hus/board.h | 2 - boards/arm/mppt_1210_hus/mppt_1210_hus.dts | 13 ++--- boards/arm/mppt_2420_hpx/Kconfig.defconfig | 3 -- boards/arm/mppt_2420_hpx/board.h | 2 - boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts | 31 +++++------- boards/arm/mppt_2420_lc/Kconfig.defconfig | 3 -- boards/arm/mppt_2420_lc/board.h | 2 - boards/arm/mppt_2420_lc/mppt_2420_lc.dts | 15 +++--- boards/arm/pwm_2420_lus/Kconfig.defconfig | 6 --- boards/arm/pwm_2420_lus/board.h | 2 - boards/arm/pwm_2420_lus/pwm_2420_lus.dts | 11 +++-- src/bat_charger.cpp | 18 +++++-- src/daq_driver.c | 8 ++-- src/data_objects.cpp | 8 ++-- src/ext/can.cpp | 8 ++-- src/ext/oled.cpp | 8 ++-- src/hardware.cpp | 8 ++-- src/load.cpp | 4 +- src/load_drv.cpp | 56 +++++++++++++--------- src/main.cpp | 6 +-- src/setup.cpp | 4 +- src/setup.h | 4 +- test/board.h | 8 ++-- test/tests_load.cpp | 2 +- zephyr/dts/bindings/outputs.yaml | 26 ++++++++++ zephyr/dts/bindings/pwm-out.yaml | 17 ------- zephyr/dts/bindings/switch.yaml | 17 ------- zephyr/mppt_1210_hus.overlay_0v4 | 4 +- 29 files changed, 142 insertions(+), 160 deletions(-) create mode 100644 zephyr/dts/bindings/outputs.yaml delete mode 100644 zephyr/dts/bindings/pwm-out.yaml delete mode 100644 zephyr/dts/bindings/switch.yaml diff --git a/boards/arm/mppt_1210_hus/Kconfig.defconfig b/boards/arm/mppt_1210_hus/Kconfig.defconfig index 912ab75..209d0cb 100755 --- a/boards/arm/mppt_1210_hus/Kconfig.defconfig +++ b/boards/arm/mppt_1210_hus/Kconfig.defconfig @@ -6,12 +6,6 @@ if BOARD_MPPT_1210_HUS config BOARD default "mppt_1210_hus" -config HAS_LOAD_OUTPUT - default y - -config HAS_USB_PWR_OUTPUT - default y - if UART_CONSOLE config UART_2 diff --git a/boards/arm/mppt_1210_hus/board.h b/boards/arm/mppt_1210_hus/board.h index 206b6ec..79e46f3 100644 --- a/boards/arm/mppt_1210_hus/board.h +++ b/boards/arm/mppt_1210_hus/board.h @@ -7,8 +7,6 @@ #ifndef MPPT_1210_HUS_H #define MPPT_1210_HUS_H -#define LOAD_CURRENT_MAX 10 // PCB maximum load switch current - #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 #define PIN_UEXT_SCL PB_6 diff --git a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts index ff2c8ee..e43b15e 100755 --- a/boards/arm/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/arm/mppt_1210_hus/mppt_1210_hus.dts @@ -52,23 +52,24 @@ }; }; - switches { - compatible = "switch"; + outputs { + compatible = "outputs"; load { + label = "Load output"; gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; - label = "Load enable"; + current-max = <10>; }; usb-pwr { - gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>; label = "USB charging port enable"; + gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>; }; v-solar { - gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>; label = "Solar voltage measurement enable"; + gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>; }; boot0 { - gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; label = "BOOT0 pin trigger"; + gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; }; }; diff --git a/boards/arm/mppt_2420_hpx/Kconfig.defconfig b/boards/arm/mppt_2420_hpx/Kconfig.defconfig index a86af23..31bcf3b 100644 --- a/boards/arm/mppt_2420_hpx/Kconfig.defconfig +++ b/boards/arm/mppt_2420_hpx/Kconfig.defconfig @@ -6,9 +6,6 @@ if BOARD_MPPT_2420_HPX config BOARD default "mppt_2420_hpx" -config HAS_LOAD_OUTPUT - default y - if UART_CONSOLE config UART_1 diff --git a/boards/arm/mppt_2420_hpx/board.h b/boards/arm/mppt_2420_hpx/board.h index 59a6013..0204fe5 100644 --- a/boards/arm/mppt_2420_hpx/board.h +++ b/boards/arm/mppt_2420_hpx/board.h @@ -9,8 +9,6 @@ #include -#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current - #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 #define PIN_UEXT_SCL PB_6 diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index a1f9d4a..66024a8 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -50,22 +50,16 @@ }; }; - pwmouts { - compatible = "pwm-out"; - - charge_pump: charge_pump { - pwms = <&{/soc/timers@40000400/pwm} 1 4>; /* TIM3 */ - /* originally planned TIM8 not working with current Zephyr driver */ - /* pwms = <&{/soc/timers@40013400/pwm} 1 4>; */ - label = "Charge pump for HS MOSFET drivers"; - }; - }; - - switches { - compatible = "switch"; + outputs { + compatible = "outputs"; load { + label = "Load output"; gpios = <&gpiob 15 GPIO_ACTIVE_LOW>; - label = "Load disable"; + current-max = <20>; + }; + charge_pump { + label = "Charge pump for HS MOSFET drivers"; + pwms = <&pwm3 1 200000>; /* 200 us period = 5 kHz */ }; }; @@ -74,7 +68,6 @@ uext-uart = &usart3; spi-uext = &spi1; i2c-uext = &i2c1; - pwm-cp = &charge_pump; }; }; @@ -126,11 +119,13 @@ status = "okay"; }; -/* for charge pump and PWM solar input */ +/* + * TIM3 used for charge pump (CH1) and PWM power switch (CH2) + * (originally planned TIM8 for charge pump not working with current Zephyr driver) + */ &timers3 { status = "okay"; - - pwm { + pwm3: pwm { status = "okay"; }; }; diff --git a/boards/arm/mppt_2420_lc/Kconfig.defconfig b/boards/arm/mppt_2420_lc/Kconfig.defconfig index 5b08613..423cd15 100755 --- a/boards/arm/mppt_2420_lc/Kconfig.defconfig +++ b/boards/arm/mppt_2420_lc/Kconfig.defconfig @@ -6,9 +6,6 @@ if BOARD_MPPT_2420_LC config BOARD default "mppt_2420_lc" -config HAS_LOAD_OUTPUT - default y - if UART_CONSOLE config UART_2 diff --git a/boards/arm/mppt_2420_lc/board.h b/boards/arm/mppt_2420_lc/board.h index 22cbd5e..1109650 100644 --- a/boards/arm/mppt_2420_lc/board.h +++ b/boards/arm/mppt_2420_lc/board.h @@ -7,8 +7,6 @@ #ifndef MPPT_2420_LC_H #define MPPT_2420_LC_H -#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current - #define PIN_UEXT_DIS PC_14 // starting from rev. 0.10 #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 diff --git a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts index b03d9ee..6ecef82 100755 --- a/boards/arm/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/arm/mppt_2420_lc/mppt_2420_lc.dts @@ -48,19 +48,20 @@ }; }; - switches { - compatible = "switch"; + outputs { + compatible = "outputs"; load { + label = "Load output"; gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; - label = "Load disable"; + current-max = <20>; }; - uext { + uext_en { + label = "UEXT enable"; gpios = <&gpioc 14 GPIO_ACTIVE_LOW>; - label = "UEXT disable"; }; - can { - gpios = <&gpioa 15 GPIO_ACTIVE_LOW>; + can_en { label = "CAN disable / standby"; + gpios = <&gpioa 15 GPIO_ACTIVE_LOW>; }; }; diff --git a/boards/arm/pwm_2420_lus/Kconfig.defconfig b/boards/arm/pwm_2420_lus/Kconfig.defconfig index 1c1c861..f6f9eee 100644 --- a/boards/arm/pwm_2420_lus/Kconfig.defconfig +++ b/boards/arm/pwm_2420_lus/Kconfig.defconfig @@ -6,12 +6,6 @@ if BOARD_PWM_2420_LUS config BOARD default "pwm_2420_lus" -config HAS_LOAD_OUTPUT - default y - -config HAS_USB_PWR_OUTPUT - default y - if UART_CONSOLE config UART_2 diff --git a/boards/arm/pwm_2420_lus/board.h b/boards/arm/pwm_2420_lus/board.h index 84702fd..bc271ac 100644 --- a/boards/arm/pwm_2420_lus/board.h +++ b/boards/arm/pwm_2420_lus/board.h @@ -7,8 +7,6 @@ #ifndef PWM_2420_LUS_H #define PWM_2420_LUS_H -#define LOAD_CURRENT_MAX 15 // PCB maximum load switch current - #define PIN_UEXT_TX PA_2 #define PIN_UEXT_RX PA_3 #define PIN_UEXT_SCL PB_6 diff --git a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts index 61fb716..c05e8af 100644 --- a/boards/arm/pwm_2420_lus/pwm_2420_lus.dts +++ b/boards/arm/pwm_2420_lus/pwm_2420_lus.dts @@ -58,19 +58,20 @@ }; }; - switches { - compatible = "switch"; + outputs { + compatible = "outputs"; load { + label = "Load output"; gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; - label = "Load disable"; + current-max = <20>; }; usb-pwr { - gpios = <&gpiob 5 GPIO_ACTIVE_LOW>; label = "USB charging port disable"; + gpios = <&gpiob 5 GPIO_ACTIVE_LOW>; }; boot0 { - gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; label = "BOOT0 pin trigger"; + gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; }; }; diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 41b51f1..050e2bf 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -6,6 +6,10 @@ #include "bat_charger.h" +#ifndef UNIT_TEST +#include +#endif + #include // for fabs function #include @@ -46,7 +50,7 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa bat->voltage_load_reconnect = num_cells * 2.10; // assumption: Battery selection matching charge controller - bat->internal_resistance = num_cells * (1.95 - 1.80) / LOAD_CURRENT_MAX; + bat->internal_resistance = num_cells * (1.95 - 1.80) / DT_OUTPUTS_LOAD_CURRENT_MAX; bat->voltage_absolute_min = num_cells * 1.6; @@ -84,7 +88,8 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa bat->voltage_load_reconnect = num_cells * 3.15; // 5% voltage drop at max current - bat->internal_resistance = bat->voltage_load_disconnect * 0.05 / LOAD_CURRENT_MAX; + bat->internal_resistance = bat->voltage_load_disconnect * 0.05 / + DT_OUTPUTS_LOAD_CURRENT_MAX; bat->voltage_absolute_min = num_cells * 2.0; bat->ocv_full = num_cells * 3.4; // will give really nonlinear SOC calculation @@ -109,7 +114,8 @@ void battery_conf_init(BatConf *bat, int type, int num_cells, float nominal_capa bat->voltage_load_reconnect = num_cells * 3.6; // 5% voltage drop at max current - bat->internal_resistance = bat->voltage_load_disconnect * 0.05 / LOAD_CURRENT_MAX; + bat->internal_resistance = bat->voltage_load_disconnect * 0.05 / + DT_OUTPUTS_LOAD_CURRENT_MAX; bat->voltage_absolute_min = num_cells * 2.5; @@ -158,9 +164,11 @@ bool battery_conf_check(BatConf *bat_conf) bat_conf->voltage_recharge > (bat_conf->voltage_load_disconnect + 1) && bat_conf->voltage_load_disconnect > (bat_conf->voltage_absolute_min + 0.4) && // max. 10% drop - bat_conf->internal_resistance < bat_conf->voltage_load_disconnect * 0.1 / LOAD_CURRENT_MAX && + bat_conf->internal_resistance < bat_conf->voltage_load_disconnect * 0.1 / + DT_OUTPUTS_LOAD_CURRENT_MAX && // max. 3% loss - bat_conf->wire_resistance < bat_conf->topping_voltage * 0.03 / LOAD_CURRENT_MAX && + bat_conf->wire_resistance < bat_conf->topping_voltage * 0.03 / + DT_OUTPUTS_LOAD_CURRENT_MAX && // C/10 or lower current cutoff allowed bat_conf->topping_current_cutoff < (bat_conf->nominal_capacity / 10.0) && bat_conf->topping_current_cutoff > 0.01 && diff --git a/src/daq_driver.c b/src/daq_driver.c index 0397424..824f2de 100644 --- a/src/daq_driver.c +++ b/src/daq_driver.c @@ -185,10 +185,10 @@ static void adc_init(ADC_TypeDef *adc) static void adc_setup() { -#ifdef DT_SWITCH_V_SOLAR_GPIOS_CONTROLLER - struct device *dev = device_get_binding(DT_SWITCH_V_SOLAR_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_SWITCH_V_SOLAR_GPIOS_PIN, - DT_SWITCH_V_SOLAR_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); +#ifdef DT_OUTPUTS_V_SOLAR_PRESENT + struct device *dev = device_get_binding(DT_OUTPUTS_V_SOLAR_GPIOS_CONTROLLER); + gpio_pin_configure(dev, DT_OUTPUTS_V_SOLAR_GPIOS_PIN, + DT_OUTPUTS_V_SOLAR_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); #endif adc_init(ADC1); diff --git a/src/data_objects.cpp b/src/data_objects.cpp index f309fe6..60fdf2f 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -155,7 +155,7 @@ const data_object_t data_objects[] = { TS_DATA_OBJ_BOOL(0x40, "LoadEnDefault", &load.enable, TS_CONF, TS_READ_ALL | TS_WRITE_ALL), -#if CONFIG_HAS_USB_PWR_OUTPUT +#if DT_OUTPUTS_USB_PWR_PRESENT TS_DATA_OBJ_BOOL(0x41, "UsbEnDefault", &usb_pwr.enable, TS_CONF, TS_READ_ALL | TS_WRITE_ALL), #endif @@ -178,7 +178,7 @@ const data_object_t data_objects[] = { TS_DATA_OBJ_INT32(0x47, "LoadUVRecovery_s", &load.lvd_recovery_delay, TS_CONF, TS_READ_ALL | TS_WRITE_ALL), -#if CONFIG_HAS_USB_PWR_OUTPUT +#if DT_OUTPUTS_USB_PWR_PRESENT TS_DATA_OBJ_INT32(0x48, "UsbUVRecovery_s", &usb_pwr.lvd_recovery_delay, TS_CONF, TS_READ_ALL | TS_WRITE_ALL), #endif @@ -189,7 +189,7 @@ const data_object_t data_objects[] = { TS_DATA_OBJ_BOOL(0x60, "LoadEn", &load.enable, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL), -#if CONFIG_HAS_USB_PWR_OUTPUT +#if DT_OUTPUTS_USB_PWR_PRESENT TS_DATA_OBJ_BOOL(0x61, "UsbEn", &usb_pwr.enable, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL), #endif @@ -219,7 +219,7 @@ const data_object_t data_objects[] = { TS_DATA_OBJ_INT32(0x04, "LoadInfo", &load.info, TS_OUTPUT, TS_READ_ALL), -#if CONFIG_HAS_USB_PWR_OUTPUT +#if DT_OUTPUTS_USB_PWR_PRESENT TS_DATA_OBJ_INT32(0x05, "UsbInfo", &usb_pwr.info, TS_OUTPUT, TS_READ_ALL), #endif diff --git a/src/ext/can.cpp b/src/ext/can.cpp index 4f607bf..0bcb4d0 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -128,16 +128,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_SWITCH_CAN_GPIOS_CONTROLLER); - gpio_pin_configure(can_en_dev, DT_SWITCH_CAN_GPIOS_PIN, - DT_SWITCH_CAN_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); + 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_dev = device_get_binding("CAN_1"); } void ThingSetCAN::enable() { - gpio_pin_set(can_en_dev, DT_SWITCH_CAN_GPIOS_PIN, 1); + gpio_pin_set(can_en_dev, DT_OUTPUTS_CAN_EN_GPIOS_PIN, 1); } #endif diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 39c08c8..22cf771 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -57,10 +57,10 @@ OledSSD1306 oled(i2c_dev); void ExtOled::enable() { -#ifdef DT_SWITCH_UEXT_GPIOS_CONTROLLER - struct device *dev_uext_dis = device_get_binding(DT_SWITCH_UEXT_GPIOS_CONTROLLER); - gpio_pin_configure(dev_uext_dis, DT_SWITCH_UEXT_GPIOS_PIN, - DT_SWITCH_UEXT_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); +#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); #endif oled.init(CONFIG_EXT_OLED_BRIGHTNESS); diff --git a/src/hardware.cpp b/src/hardware.cpp index 72a6ef3..4d860fa 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -65,11 +65,11 @@ void watchdog_feed(int channel_id) void start_stm32_bootloader() { -#ifdef DT_SWITCH_BOOT0_GPIOS_CONTROLLER +#ifdef DT_OUTPUTS_BOOT0_PRESENT // pin is connected to BOOT0 via resistor and capacitor - struct device *dev = device_get_binding(DT_SWITCH_BOOT0_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_SWITCH_BOOT0_GPIOS_PIN, - DT_SWITCH_BOOT0_GPIOS_FLAGS | GPIO_OUTPUT_ACTIVE); + 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); k_sleep(100); // wait for capacitor at BOOT0 pin to charge up reset_device(); diff --git a/src/load.cpp b/src/load.cpp index a57c9b7..65dc7a4 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -44,12 +44,12 @@ void LoadOutput::control() // 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) * + (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); if (junction_temperature > DT_CHARGE_CONTROLLER_PCB_MOSFETS_TJ_MAX || - current > LOAD_CURRENT_MAX * 2) + current > DT_OUTPUTS_LOAD_CURRENT_MAX * 2) { flags_set(&error_flags, ERR_LOAD_OVERCURRENT); oc_timestamp = uptime(); diff --git a/src/load_drv.cpp b/src/load_drv.cpp index 8f0f114..557b62c 100644 --- a/src/load_drv.cpp +++ b/src/load_drv.cpp @@ -6,10 +6,10 @@ #include "load.h" -#include // for PRIu32 in printf statements - #ifndef UNIT_TEST +#include // for PRIu32 in printf statements + #include #include #include @@ -23,12 +23,10 @@ static struct device *dev_load; -#ifdef DT_SWITCH_USB_PWR_GPIOS_CONTROLLER +#ifdef DT_OUTPUTS_USB_PWR_PRESENT static struct device *dev_usb; #endif -#endif // UNIT_TEST - #include "board.h" #include "hardware.h" #include "leds.h" @@ -158,53 +156,55 @@ void load_out_set(bool status) leds_set(LED_LOAD, status); #endif -#ifdef DT_SWITCH_LOAD_GPIOS_CONTROLLER - gpio_pin_configure(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, - DT_SWITCH_LOAD_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); +#ifdef DT_OUTPUTS_LOAD_PRESENT + gpio_pin_configure(dev_load, DT_OUTPUTS_LOAD_GPIOS_PIN, + DT_OUTPUTS_LOAD_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); if (status == true) { #ifdef PIN_I_LOAD_COMP lptim_init(); #else - gpio_pin_set(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, 1); + gpio_pin_set(dev_load, DT_OUTPUTS_LOAD_GPIOS_PIN, 1); #endif } else { - gpio_pin_set(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, 0); + gpio_pin_set(dev_load, DT_OUTPUTS_LOAD_GPIOS_PIN, 0); } #endif } void usb_out_set(bool status) { -#ifdef DT_SWITCH_USB_PWR_GPIOS_CONTROLLER - gpio_pin_configure(dev_usb, DT_SWITCH_USB_PWR_GPIOS_PIN, - DT_SWITCH_USB_PWR_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); +#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 (status == true) { - gpio_pin_set(dev_usb, DT_SWITCH_USB_PWR_GPIOS_PIN, 1); + gpio_pin_set(dev_usb, DT_OUTPUTS_USB_PWR_GPIOS_PIN, 1); } else { - gpio_pin_set(dev_usb, DT_SWITCH_USB_PWR_GPIOS_PIN, 0); + gpio_pin_set(dev_usb, DT_OUTPUTS_USB_PWR_GPIOS_PIN, 0); } #endif } void load_cp_enable() { -#ifdef DT_PWM_OUT_CHARGE_PUMP_PWMS_CONTROLLER +#ifdef DT_OUTPUTS_CHARGE_PUMP_PRESENT struct device *pwm_dev; - pwm_dev = device_get_binding(DT_PWM_OUT_CHARGE_PUMP_PWMS_CONTROLLER); + pwm_dev = device_get_binding(DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER); if (!pwm_dev) { - print_error("Cannot find %s!\n", DT_PWM_OUT_CHARGE_PUMP_PWMS_CONTROLLER); + print_error("Cannot find %s!\n", DT_OUTPUTS_CHARGE_PUMP_PWMS_CONTROLLER); return; } - pwm_pin_set_usec(pwm_dev, DT_PWM_OUT_CHARGE_PUMP_PWMS_CHANNEL, 200, 100, 0); + // 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); #endif } void load_out_init() { -#ifdef DT_SWITCH_LOAD_GPIOS_CONTROLLER - dev_load = device_get_binding(DT_SWITCH_LOAD_GPIOS_CONTROLLER); +#ifdef DT_OUTPUTS_LOAD_PRESENT + dev_load = device_get_binding(DT_OUTPUTS_LOAD_GPIOS_CONTROLLER); #endif // analog comparator to detect short circuits and trigger immediate load switch-off @@ -220,7 +220,17 @@ void load_out_init() void usb_out_init() { -#ifdef DT_SWITCH_USB_PWR_GPIOS_CONTROLLER - dev_usb = device_get_binding(DT_SWITCH_USB_PWR_GPIOS_CONTROLLER); +#ifdef DT_OUTPUTS_USB_PWR_PRESENT + dev_usb = device_get_binding(DT_OUTPUTS_USB_PWR_GPIOS_CONTROLLER); #endif } + +#else // UNIT_TEST + +void load_out_init() {;} +void usb_out_init() {;} + +void load_out_set(bool) {;} +void usb_out_set(bool) {;} + +#endif diff --git a/src/main.cpp b/src/main.cpp index 4ebf691..461dc0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ void main(void) load.set_voltage_limits(bat_conf.voltage_load_disconnect, bat_conf.voltage_load_reconnect, bat_conf.voltage_absolute_max); - #if CONFIG_HAS_USB_PWR_OUTPUT + #if DT_OUTPUTS_USB_PWR_PRESENT 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 @@ -117,11 +117,11 @@ void control_thread() leds_set_charging(half_bridge_enabled()); #endif - #if CONFIG_HAS_LOAD_OUTPUT + #if DT_OUTPUTS_LOAD_PRESENT load.control(); #endif - #if CONFIG_HAS_USB_PWR_OUTPUT + #if DT_OUTPUTS_USB_PWR_PRESENT usb_pwr.control(); #endif diff --git a/src/setup.cpp b/src/setup.cpp index f185832..0258669 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -49,11 +49,11 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK); PwmSwitch pwm_switch(&lv_bus); #endif -#if CONFIG_HAS_LOAD_OUTPUT +#if DT_OUTPUTS_LOAD_PRESENT LoadOutput load(&lv_bus, &load_out_set, &load_out_init); #endif -#if CONFIG_HAS_USB_PWR_OUTPUT +#if DT_OUTPUTS_USB_PWR_PRESENT LoadOutput usb_pwr(&lv_bus, &usb_out_set, &usb_out_init); #endif diff --git a/src/setup.h b/src/setup.h index 1c532ba..d13e23c 100644 --- a/src/setup.h +++ b/src/setup.h @@ -41,11 +41,11 @@ extern Charger charger; extern BatConf bat_conf; extern BatConf bat_conf_user; -#if CONFIG_HAS_LOAD_OUTPUT +#if DT_OUTPUTS_LOAD_PRESENT extern LoadOutput load; #endif -#if CONFIG_HAS_USB_PWR_OUTPUT +#if DT_OUTPUTS_USB_PWR_PRESENT extern LoadOutput usb_pwr; #endif diff --git a/test/board.h b/test/board.h index db721de..289471f 100644 --- a/test/board.h +++ b/test/board.h @@ -37,8 +37,10 @@ #define DT_INST_0_PWM_SWITCH_PWM_FREQUENCY 50 // Hz #define DT_INST_0_PWM_SWITCH_CURRENT_MAX 20 // A -#define CONFIG_HAS_LOAD_OUTPUT 1 -#define CONFIG_HAS_USB_PWR_OUTPUT 1 +#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 @@ -46,8 +48,6 @@ #define DT_CHARGE_CONTROLLER_PCB_INTERNAL_TREF_MAX 50 #define DT_CHARGE_CONTROLLER_PCB_MOSFETS_TAU_JA 5 -#define LOAD_CURRENT_MAX 20 // PCB maximum load switch current - #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) diff --git a/test/tests_load.cpp b/test/tests_load.cpp index ea37822..c2bf72e 100644 --- a/test/tests_load.cpp +++ b/test/tests_load.cpp @@ -101,7 +101,7 @@ void control_on_to_off_overcurrent() LoadOutput load_out(&bus, &load_drv_set, &load_drv_init); load_init(&load_out, true); - load_out.current = LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately + load_out.current = DT_OUTPUTS_LOAD_CURRENT_MAX * 1.9; // with factor 2 it is switched off immediately load_out.control(); TEST_ASSERT_EQUAL(LOAD_STATE_ON, load_out.state); diff --git a/zephyr/dts/bindings/outputs.yaml b/zephyr/dts/bindings/outputs.yaml new file mode 100644 index 0000000..0082776 --- /dev/null +++ b/zephyr/dts/bindings/outputs.yaml @@ -0,0 +1,26 @@ +# Copyright (c) 2020 Martin Jäger / Libre Solar +# SPDX-License-Identifier: Apache-2.0 + +description: GPIOs used for digital output (either on/off type or PWM signal) + +compatible: "outputs" + +child-binding: + description: GPIO digital outputs child node + properties: + gpios: + type: phandle-array + description: GPIO handle for on/off type output + pwms: + type: phandle-array + description: PWM handle for PWM type output + current-max: + type: int + description: Maximum current of driven power switch (A) + label: + type: string + required: true + description: Human readable string describing the device (used by Zephyr for API name) + present: + type: int + default: 1 diff --git a/zephyr/dts/bindings/pwm-out.yaml b/zephyr/dts/bindings/pwm-out.yaml deleted file mode 100644 index 877fd27..0000000 --- a/zephyr/dts/bindings/pwm-out.yaml +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (c) 2020 Martin Jäger / Libre Solar -# SPDX-License-Identifier: Apache-2.0 - -description: GPIO pins to generate a PWM signal - -compatible: "pwm-out" - -child-binding: - description: PWM output child node - properties: - pwms: - type: phandle-array - required: true - label: - type: string - required: true - description: Human readable string describing the device (used by Zephyr for API name) diff --git a/zephyr/dts/bindings/switch.yaml b/zephyr/dts/bindings/switch.yaml deleted file mode 100644 index 36ee135..0000000 --- a/zephyr/dts/bindings/switch.yaml +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (c) 2020 Martin Jäger / Libre Solar -# SPDX-License-Identifier: Apache-2.0 - -description: GPIO pins to switch something on or off - -compatible: "switch" - -child-binding: - description: GPIO switch child node - properties: - gpios: - type: phandle-array - required: true - label: - type: string - required: true - description: Human readable string describing the device (used by Zephyr for API name) diff --git a/zephyr/mppt_1210_hus.overlay_0v4 b/zephyr/mppt_1210_hus.overlay_0v4 index 16c3ecd..26dbb89 100644 --- a/zephyr/mppt_1210_hus.overlay_0v4 +++ b/zephyr/mppt_1210_hus.overlay_0v4 @@ -37,8 +37,8 @@ /delete-node/ c; }; - switches { - compatible = "switch"; + outputs { + compatible = "outputs"; load { gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; label = "Load enable";