From 8057f8c0993352835cd82a7d3a2d7d7f54ac3907 Mon Sep 17 00:00:00 2001 From: Raffael Rostagno Date: Wed, 8 Jan 2025 11:20:22 -0300 Subject: [PATCH] [nrf fromtree] tests: drivers: pwm: Update gpio config Update gpio config for more correct handling. Signed-off-by: Raffael Rostagno (cherry picked from commit 931c671cc2e87670b4e49fa44807664721990426) --- .../drivers/pwm/pwm_gpio_loopback/src/main.c | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/tests/drivers/pwm/pwm_gpio_loopback/src/main.c b/tests/drivers/pwm/pwm_gpio_loopback/src/main.c index 16c2357f9b9..12218990634 100644 --- a/tests/drivers/pwm/pwm_gpio_loopback/src/main.c +++ b/tests/drivers/pwm/pwm_gpio_loopback/src/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. + * Copyright (c) 2025 Espressif Systems (Shanghai) Co., Ltd. * * SPDX-License-Identifier: Apache-2.0 */ @@ -97,22 +97,19 @@ static void setup_edge_detect(void) static void config_gpio(const struct gpio_dt_spec *gpio_dt) { - /* Configure GPIO pin for edge detection */ gpio_pin_configure_dt(gpio_dt, GPIO_INPUT); - - gpio_cb.pin_mask = BIT(gpio_dt->pin); - - gpio_init_callback(&gpio_cb, gpio_edge_isr, gpio_cb.pin_mask); + gpio_init_callback(&gpio_cb, gpio_edge_isr, BIT(gpio_dt->pin)); gpio_add_callback(gpio_dt->port, &gpio_cb); - gpio_pin_interrupt_configure(gpio_dt->port, gpio_dt->pin, GPIO_INT_EDGE_BOTH); } -static void unconfig_gpio(const struct gpio_dt_spec *gpio_dt) +static void enable_int_gpio(const struct gpio_dt_spec *gpio_dt, bool enable) { - /* Disable interrupt for already tested channel */ - gpio_pin_interrupt_configure(gpio_dt->port, gpio_dt->pin, GPIO_INT_DISABLE); - - gpio_cb.pin_mask &= ~BIT(gpio_dt->pin); + if (enable) { + gpio_pin_interrupt_configure(gpio_dt->port, gpio_dt->pin, GPIO_INT_EDGE_BOTH); + gpio_cb.pin_mask = BIT(gpio_dt->pin); + } else { + gpio_pin_interrupt_configure(gpio_dt->port, gpio_dt->pin, GPIO_INT_DISABLE); + } } static bool check_range(float refval, float measval) @@ -207,11 +204,11 @@ static void test_run(const struct pwm_dt_spec *pwm_dt, const struct gpio_dt_spec zassert_false(result, "Failed on pwm_set() call"); } - config_gpio(gpio_dt); + enable_int_gpio(gpio_dt, true); result = check_timing(pwm_dt, gpio_dt, duty); - unconfig_gpio(gpio_dt); + enable_int_gpio(gpio_dt, false); zassert_equal(result, TC_PASS, "Test case failed"); } @@ -220,7 +217,6 @@ ZTEST(pwm_gpio_loopback, test_pwm) { for (int i = 0; i < TEST_PWM_COUNT; i++) { zassert_true(device_is_ready(pwms_dt[i].dev), "PWM device is not ready"); - zassert_true(device_is_ready(gpios_dt[i].port), "GPIO device is not ready"); /* Test case: [Duty: 25%] */ test_run(&pwms_dt[i], &gpios_dt[i], 25, true); @@ -251,4 +247,18 @@ ZTEST(pwm_gpio_loopback, test_pwm_cross) } } -ZTEST_SUITE(pwm_gpio_loopback, NULL, NULL, NULL, NULL, NULL); +static void *pwm_gpio_loopback_setup(void) +{ + for (int i = 0; i < TEST_GPIO_COUNT; i++) { + if (device_is_ready(gpios_dt[i].port)) { + /* Configure GPIO pin for edge detection */ + config_gpio(&gpios_dt[i]); + } else { + TC_PRINT("GPIO device %s is not ready", gpios_dt[i].port->name); + } + } + + return NULL; +} + +ZTEST_SUITE(pwm_gpio_loopback, NULL, pwm_gpio_loopback_setup, NULL, NULL, NULL);