From 08974169aba12b0621059a61fb1a932dd3ad64a9 Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Thu, 13 Feb 2020 18:00:45 +0100 Subject: [PATCH] Use new Zephyr 2.2 GPIO driver API features Enable and disable pins are now handled the same using _set function instead of _write and using ACTIVE_HIGH/LOW setting in dts. --- .../mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts | 6 +- .../mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts | 8 +-- boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts | 4 +- .../mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts | 12 ++-- .../arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts | 10 ++-- src/daq_zephyr.c | 8 +-- src/ext/can.cpp | 11 ++-- src/ext/oled.cpp | 9 ++- src/hardware.cpp | 8 +-- src/leds.cpp | 12 ++-- src/load_drv.cpp | 56 ++++++------------- 11 files changed, 60 insertions(+), 84 deletions(-) diff --git a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts index 8eb52b5..45c08d9 100755 --- a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts +++ b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts @@ -44,11 +44,11 @@ switches { compatible = "switch"; - load-en { - gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; + load { + gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; label = "Load enable"; }; - usb-en { + usb-pwr { gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; label = "USB charging port enable"; }; diff --git a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts index 1f775ba..95e313b 100755 --- a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts +++ b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts @@ -36,19 +36,19 @@ switches { compatible = "switch"; - load-en { + load { gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; label = "Load enable"; }; - usb-pwr-en { + usb-pwr { gpios = <&gpiob 10 GPIO_ACTIVE_HIGH>; label = "USB charging port enable"; }; - v-solar-en { + v-solar { gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>; label = "Solar voltage measurement enable"; }; - boot0-en { + boot0 { gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; label = "BOOT0 pin trigger"; }; diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index 39967ce..98855dc 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -59,8 +59,8 @@ switches { compatible = "switch"; - load-dis { - gpios = <&gpiob 15 GPIO_ACTIVE_HIGH>; + load { + gpios = <&gpiob 15 GPIO_ACTIVE_LOW>; label = "Load disable"; }; }; diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts index 9a93547..22b6603 100755 --- a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts @@ -32,16 +32,16 @@ switches { compatible = "switch"; - load-dis { - gpios = <&gpiob 2 GPIO_ACTIVE_HIGH>; + load { + gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; label = "Load disable"; }; - uext-dis { - gpios = <&gpioc 14 GPIO_ACTIVE_HIGH>; + uext { + gpios = <&gpioc 14 GPIO_ACTIVE_LOW>; label = "UEXT disable"; }; - can-dis { - gpios = <&gpioa 15 GPIO_ACTIVE_HIGH>; + can { + gpios = <&gpioa 15 GPIO_ACTIVE_LOW>; label = "CAN disable / standby"; }; }; diff --git a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts index 1eceb30..6b387b5 100644 --- a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts @@ -36,15 +36,15 @@ switches { compatible = "switch"; - load-dis { - gpios = <&gpiob 2 GPIO_ACTIVE_HIGH>; + load { + gpios = <&gpiob 2 GPIO_ACTIVE_LOW>; label = "Load disable"; }; - usb-pwr-dis { - gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>; + usb-pwr { + gpios = <&gpiob 5 GPIO_ACTIVE_LOW>; label = "USB charging port disable"; }; - boot0-en { + boot0 { gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>; label = "BOOT0 pin trigger"; }; diff --git a/src/daq_zephyr.c b/src/daq_zephyr.c index f1eeb9a..69970a8 100644 --- a/src/daq_zephyr.c +++ b/src/daq_zephyr.c @@ -98,10 +98,10 @@ static void dac_setup() static void adc_setup() { -#ifdef DT_SWITCH_V_SOLAR_EN_GPIOS_CONTROLLER - struct device *dev = device_get_binding(DT_SWITCH_V_SOLAR_EN_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_SWITCH_V_SOLAR_EN_GPIOS_PIN, GPIO_DIR_OUT); - gpio_pin_write(dev, DT_SWITCH_V_SOLAR_EN_GPIOS_PIN, 1); +#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); #endif struct device *dev_adc = device_get_binding(DT_ADC_1_NAME); diff --git a/src/ext/can.cpp b/src/ext/can.cpp index 8c4c72f..d1bb852 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -79,7 +79,7 @@ private: DigitalOut can_disable; #elif defined(__ZEPHYR__) struct device *can_dev; - struct device *gpio_stb_dev; + struct device *can_en_dev; #endif }; @@ -130,17 +130,16 @@ ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c) { - gpio_stb_dev = device_get_binding(DT_SWITCH_CAN_DIS_GPIOS_CONTROLLER); - gpio_pin_configure(gpio_stb_dev, DT_SWITCH_CAN_DIS_GPIOS_PIN, GPIO_DIR_OUT); - gpio_pin_write(gpio_stb_dev, DT_SWITCH_CAN_DIS_GPIOS_PIN, 1); + 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_dev = device_get_binding("CAN_1"); } void ThingSetCAN::enable() { - // enable the transceiver (set STB pin low) - gpio_pin_write(gpio_stb_dev, DT_SWITCH_CAN_DIS_GPIOS_PIN, 0); + gpio_pin_write(gpio_stb_dev, DT_SWITCH_CAN_GPIOS_PIN, 1); } #endif diff --git a/src/ext/oled.cpp b/src/ext/oled.cpp index 6478542..017edca 100644 --- a/src/ext/oled.cpp +++ b/src/ext/oled.cpp @@ -71,11 +71,10 @@ void ExtOled::enable() uext_dis = 0; #endif -#if defined(DT_SWITCH_UEXT_DIS_GPIOS_CONTROLLER) && defined(__ZEPHYR__) - struct device *dev_uext_dis = - device_get_binding(DT_SWITCH_UEXT_DIS_GPIOS_CONTROLLER); - gpio_pin_configure(dev_uext_dis, DT_SWITCH_UEXT_DIS_GPIOS_PIN, GPIO_DIR_OUT); - gpio_pin_write(dev_uext_dis, DT_SWITCH_UEXT_DIS_GPIOS_PIN, 0); +#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); #endif #ifdef OLED_BRIGHTNESS diff --git a/src/hardware.cpp b/src/hardware.cpp index 792473e..ee720d9 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -224,11 +224,11 @@ void reset_device() void start_stm32_bootloader() { -#ifdef DT_SWITCH_BOOT0_EN_GPIOS_CONTROLLER +#ifdef DT_SWITCH_BOOT0_GPIOS_CONTROLLER // pin is connected to BOOT0 via resistor and capacitor - struct device *dev = device_get_binding(DT_SWITCH_BOOT0_EN_GPIOS_CONTROLLER); - gpio_pin_configure(dev, DT_SWITCH_BOOT0_EN_GPIOS_PIN, GPIO_DIR_OUT); - gpio_pin_write(dev, DT_SWITCH_BOOT0_EN_GPIOS_PIN, 1); + 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); k_sleep(100); // wait for capacitor at BOOT0 pin to charge up reset_device(); diff --git a/src/leds.cpp b/src/leds.cpp index 95ef4a8..6e0eca8 100644 --- a/src/leds.cpp +++ b/src/leds.cpp @@ -172,15 +172,15 @@ void leds_update_thread() for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { switch (led_pin_setup[led_count][pin_number]) { case PIN_HIGH: - gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_OUT); - gpio_pin_write(led_devs[pin_number], led_pins[pin_number], 1); + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_OUTPUT); + gpio_pin_set(led_devs[pin_number], led_pins[pin_number], 1); break; case PIN_LOW: - gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_OUT); - gpio_pin_write(led_devs[pin_number], led_pins[pin_number], 0); + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_OUTPUT); + gpio_pin_set(led_devs[pin_number], led_pins[pin_number], 0); break; case PIN_FLOAT: - gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_IN); + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_INPUT); break; } } @@ -188,7 +188,7 @@ void leds_update_thread() else { // all pins floating for (int pin_number = 0; pin_number < NUM_LED_PINS; pin_number++) { - gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_DIR_IN); + gpio_pin_configure(led_devs[pin_number], led_pins[pin_number], GPIO_INPUT); } } diff --git a/src/load_drv.cpp b/src/load_drv.cpp index b75c353..619428f 100644 --- a/src/load_drv.cpp +++ b/src/load_drv.cpp @@ -185,27 +185,18 @@ void LoadOutput::switch_set(bool status) } #endif -#if defined(__ZEPHYR__) && defined(DT_SWITCH_LOAD_EN_GPIOS_CONTROLLER) - gpio_pin_configure(dev_load, DT_SWITCH_LOAD_EN_GPIOS_PIN, GPIO_DIR_OUT); +#ifdef DT_SWITCH_LOAD_GPIOS_CONTROLLER + gpio_pin_configure(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, + DT_SWITCH_LOAD_GPIOS_FLAGS | GPIO_OUTPUT_INACTIVE); if (status == true) { - gpio_pin_write(dev_load, DT_SWITCH_LOAD_EN_GPIOS_PIN, 1); - } - else { - gpio_pin_write(dev_load, DT_SWITCH_LOAD_EN_GPIOS_PIN, 0); - } +#ifdef PIN_I_LOAD_COMP + lptim_init(); +#else + gpio_pin_set(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, 1); #endif - -#if defined(__ZEPHYR__) && defined(DT_SWITCH_LOAD_DIS_GPIOS_CONTROLLER) - gpio_pin_configure(dev_load, DT_SWITCH_LOAD_DIS_GPIOS_PIN, GPIO_DIR_OUT); - if (status == true) { - #ifdef PIN_I_LOAD_COMP - lptim_init(); - #else - gpio_pin_write(dev_load, DT_SWITCH_LOAD_DIS_GPIOS_PIN, 0); - #endif } else { - gpio_pin_write(dev_load, DT_SWITCH_LOAD_DIS_GPIOS_PIN, 1); + gpio_pin_set(dev_load, DT_SWITCH_LOAD_GPIOS_PIN, 0); } #endif @@ -227,23 +218,14 @@ void LoadOutput::usb_set(bool status) else usb_pwr_dis = 1; #endif -#if defined(__ZEPHYR__) && defined(DT_SWITCH_USB_PWR_EN_GPIOS_CONTROLLER) - gpio_pin_configure(dev_usb, DT_SWITCH_USB_PWR_EN_GPIOS_PIN, GPIO_DIR_OUT); +#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); if (status == true) { - gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_EN_GPIOS_PIN, 1); + gpio_pin_set(dev_usb, DT_SWITCH_USB_PWR_GPIOS_PIN, 1); } else { - gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_EN_GPIOS_PIN, 0); - } -#endif - -#if defined(__ZEPHYR__) && defined(DT_SWITCH_USB_PWR_DIS_GPIOS_CONTROLLER) - gpio_pin_configure(dev_usb, DT_SWITCH_USB_PWR_DIS_GPIOS_PIN, GPIO_DIR_OUT); - if (status == true) { - gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_DIS_GPIOS_PIN, 0); - } - else { - gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_DIS_GPIOS_PIN, 1); + gpio_pin_set(dev_usb, DT_SWITCH_USB_PWR_GPIOS_PIN, 0); } #endif @@ -254,16 +236,12 @@ void LoadOutput::usb_set(bool status) void LoadOutput::get_bindings() { -#if defined(DT_SWITCH_USB_PWR_EN_GPIOS_CONTROLLER) - dev_usb = device_get_binding(DT_SWITCH_USB_PWR_EN_GPIOS_CONTROLLER); -#elif defined(DT_SWITCH_USB_PWR_DIS_GPIOS_CONTROLLER) - dev_usb = device_get_binding(DT_SWITCH_USB_PWR_DIS_GPIOS_CONTROLLER); +#ifdef DT_SWITCH_USB_PWR_GPIOS_CONTROLLER + dev_usb = device_get_binding(DT_SWITCH_USB_PWR_GPIOS_CONTROLLER); #endif -#if defined(DT_SWITCH_LOAD_EN_GPIOS_CONTROLLER) - dev_load = device_get_binding(DT_SWITCH_LOAD_EN_GPIOS_CONTROLLER); -#elif defined(DT_SWITCH_LOAD_DIS_GPIOS_CONTROLLER) - dev_load = device_get_binding(DT_SWITCH_LOAD_DIS_GPIOS_CONTROLLER); +#ifdef DT_SWITCH_LOAD_GPIOS_CONTROLLER + dev_load = device_get_binding(DT_SWITCH_LOAD_GPIOS_CONTROLLER); #endif }