1 /*
2 * Copyright 2020 Peter Bigot Consulting, LLC
3 * Copyright 2022 Nordic Semiconductor ASA
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/drivers/regulator.h>
10 #include <zephyr/ztest.h>
11
12 static const struct device *const reg = DEVICE_DT_GET(DT_PATH(regulator));
13 static const struct gpio_dt_spec check_gpio =
14 GPIO_DT_SPEC_GET(DT_PATH(resources), check_gpios);
15 static const uint32_t startup_delay_ms = DT_PROP(DT_PATH(regulator),
16 startup_delay_us) / 1000U;
17 static const uint32_t off_on_delay_ms = DT_PROP(DT_PATH(regulator),
18 off_on_delay_us) / 1000U;
19
ZTEST(regulator_fixed,test_enable_disable)20 ZTEST(regulator_fixed, test_enable_disable)
21 {
22 int ret;
23 int64_t init;
24
25 zassert_true(k_uptime_get() >= startup_delay_ms);
26
27 ret = gpio_pin_get_dt(&check_gpio);
28 zassert_equal(ret, 1);
29
30 zassert_true(regulator_is_enabled(reg));
31
32 ret = regulator_disable(reg);
33 zassert_equal(ret, 0);
34
35 ret = gpio_pin_get_dt(&check_gpio);
36 zassert_equal(ret, 0);
37
38 init = k_uptime_get();
39 ret = regulator_enable(reg);
40 zassert_equal(ret, 0);
41 zassert_true(k_uptime_delta(&init) >= off_on_delay_ms);
42
43 ret = gpio_pin_get_dt(&check_gpio);
44 zassert_equal(ret, 1);
45
46 ret = regulator_disable(reg);
47 zassert_equal(ret, 0);
48
49 ret = gpio_pin_get_dt(&check_gpio);
50 zassert_equal(ret, 0);
51 }
52
setup(void)53 void *setup(void)
54 {
55 zassert_true(device_is_ready(reg));
56 zassert_equal(gpio_pin_configure_dt(&check_gpio, GPIO_INPUT), 0);
57
58 return NULL;
59 }
60
61 ZTEST_SUITE(regulator_fixed, NULL, setup, NULL, NULL, NULL);
62