|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/devicetree.h> |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/pm/pm.h> |
| 11 | + |
| 12 | +#define PS_NODE DT_PATH(cpus, power_states) |
| 13 | + |
| 14 | +#if DT_NODE_EXISTS(PS_NODE) |
| 15 | +#define PS_VAL(node) DT_PROP_OR(node, min_residency_us, 0), |
| 16 | + |
| 17 | +static const uint32_t min_residencies[] = {DT_FOREACH_CHILD(PS_NODE, PS_VAL)}; |
| 18 | + |
| 19 | +#define PS_COUNT ARRAY_SIZE(min_residencies) |
| 20 | +#else |
| 21 | +static const uint32_t min_residencies[] = {}; |
| 22 | +#define PS_COUNT 0 |
| 23 | +#endif |
| 24 | + |
| 25 | +static const struct gpio_dt_spec leds[] = { |
| 26 | + GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios), |
| 27 | + GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios), |
| 28 | + GPIO_DT_SPEC_GET(DT_ALIAS(led2), gpios), |
| 29 | + GPIO_DT_SPEC_GET(DT_ALIAS(led3), gpios), |
| 30 | +}; |
| 31 | +#define LED_COUNT ARRAY_SIZE(leds) |
| 32 | + |
| 33 | +static void notify_pm_state_entry(enum pm_state state) |
| 34 | +{ |
| 35 | + switch (state) { |
| 36 | + case PM_STATE_SUSPEND_TO_IDLE: |
| 37 | + gpio_pin_set_dt(&leds[0], 1); |
| 38 | + break; |
| 39 | + case PM_STATE_SUSPEND_TO_RAM: |
| 40 | + gpio_pin_set_dt(&leds[2], 1); |
| 41 | + break; |
| 42 | + default: |
| 43 | + __ASSERT(true, "Unexpected PM state: %d", state); |
| 44 | + break; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +static void notify_pm_state_exit(enum pm_state state) |
| 49 | +{ |
| 50 | + for (size_t i = 0; i < LED_COUNT; i++) { |
| 51 | + gpio_pin_set_dt(&leds[i], 0); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +static struct pm_notifier notifier = { |
| 56 | + .state_entry = notify_pm_state_entry, |
| 57 | + .state_exit = notify_pm_state_exit, |
| 58 | +}; |
| 59 | + |
| 60 | +int main(void) |
| 61 | +{ |
| 62 | + int ret; |
| 63 | + |
| 64 | + for (size_t i = 0; i < LED_COUNT; i++) { |
| 65 | + ret = gpio_is_ready_dt(&leds[i]); |
| 66 | + __ASSERT(ret, "Error: GPIO Device not ready"); |
| 67 | + ret = gpio_pin_configure_dt(&leds[i], GPIO_OUTPUT_INACTIVE); |
| 68 | + __ASSERT(ret == 0, "Could not configure led GPIO"); |
| 69 | + } |
| 70 | + |
| 71 | + pm_notifier_register(¬ifier); |
| 72 | + |
| 73 | + k_msleep(1500); |
| 74 | + |
| 75 | + if (PS_COUNT != 0) { |
| 76 | + while (1) { |
| 77 | + k_usleep(min_residencies[0] - 100); |
| 78 | + for (size_t i = 0; i < PS_COUNT; i++) { |
| 79 | + k_usleep(min_residencies[i] + 100); |
| 80 | + } |
| 81 | + k_busy_wait(400); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments