|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/drivers/gpio.h> |
| 9 | +#include <zephyr/drivers/counter.h> |
| 10 | +#include <zephyr/logging/log.h> |
| 11 | +#include <zephyr/devicetree/clocks.h> |
| 12 | +#include <zephyr/drivers/clock_control/nrf_clock_control.h> |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(idle_counter); |
| 15 | + |
| 16 | +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led), gpios); |
| 17 | + |
| 18 | +#define ALARM_CHANNEL_ID (0) |
| 19 | +const struct device *const counter_dev = DEVICE_DT_GET(DT_ALIAS(counter)); |
| 20 | + |
| 21 | +static K_SEM_DEFINE(my_sem, 0, 1); |
| 22 | + |
| 23 | +#if defined(CONFIG_CLOCK_CONTROL) && defined(CONFIG_SOC_NRF54H20_CPUAPP) |
| 24 | +const uint32_t freq[] = {320, 256, 128, 64}; |
| 25 | + |
| 26 | +/* |
| 27 | + * Set Global Domain frequency (HSFLL120) |
| 28 | + */ |
| 29 | +void set_global_domain_frequency(uint32_t freq) |
| 30 | +{ |
| 31 | + int err; |
| 32 | + int res; |
| 33 | + struct onoff_client cli; |
| 34 | + const struct device *hsfll_dev = DEVICE_DT_GET(DT_NODELABEL(hsfll120)); |
| 35 | + const struct nrf_clock_spec clk_spec_global_hsfll = {.frequency = MHZ(freq)}; |
| 36 | + |
| 37 | + LOG_INF("Requested frequency [Hz]: %d", clk_spec_global_hsfll.frequency); |
| 38 | + sys_notify_init_spinwait(&cli.notify); |
| 39 | + err = nrf_clock_control_request(hsfll_dev, &clk_spec_global_hsfll, &cli); |
| 40 | + __ASSERT((err >= 0 && err < 3), "Wrong nrf_clock_control_request return code"); |
| 41 | + do { |
| 42 | + err = sys_notify_fetch_result(&cli.notify, &res); |
| 43 | + k_yield(); |
| 44 | + } while (err == -EAGAIN); |
| 45 | + __ASSERT(err == 0, "Wrong clock control request return code"); |
| 46 | + __ASSERT(res == 0, "Wrong clock control request response"); |
| 47 | +} |
| 48 | +#endif /* CONFIG_CLOCK_CONTROL && CONFIG_SOC_NRF54H20_CPUAPP */ |
| 49 | + |
| 50 | +void counter_handler(const struct device *counter_dev, uint8_t chan_id, uint32_t ticks, |
| 51 | + void *user_data) |
| 52 | +{ |
| 53 | + gpio_pin_set_dt(&led, 1); |
| 54 | + k_sem_give(&my_sem); |
| 55 | + counter_stop(counter_dev); |
| 56 | +} |
| 57 | + |
| 58 | +uint32_t start_timer(const struct device *counter_dev) |
| 59 | +{ |
| 60 | + uint32_t start_time; |
| 61 | + struct counter_alarm_cfg counter_cfg; |
| 62 | + |
| 63 | + counter_cfg.flags = 0; |
| 64 | + counter_cfg.ticks = counter_us_to_ticks(counter_dev, CONFIG_TEST_SLEEP_DURATION_MS * 1000); |
| 65 | + counter_cfg.callback = counter_handler; |
| 66 | + counter_cfg.user_data = &counter_cfg; |
| 67 | + counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID, &counter_cfg); |
| 68 | + |
| 69 | + counter_start(counter_dev); |
| 70 | + start_time = k_cycle_get_32(); |
| 71 | + |
| 72 | + LOG_INF("Counter starts at %u cycle", start_time); |
| 73 | + |
| 74 | + return start_time; |
| 75 | +} |
| 76 | + |
| 77 | +void verify_timer(uint32_t start_time) |
| 78 | +{ |
| 79 | + uint32_t elapsed; |
| 80 | + int ret; |
| 81 | + |
| 82 | + ret = gpio_pin_set_dt(&led, 0); |
| 83 | + __ASSERT(ret == 0, "Unable to turn off LED"); |
| 84 | + ret = k_sem_take(&my_sem, K_MSEC(CONFIG_TEST_SLEEP_DURATION_MS + 100)); |
| 85 | + elapsed = k_cycle_get_32() - start_time; |
| 86 | + __ASSERT(ret == 0, "Timer callback not called"); |
| 87 | + ret = gpio_pin_set_dt(&led, 1); |
| 88 | + __ASSERT(ret == 0, "Unable to turn on LED"); |
| 89 | + LOG_INF("Elapsed %u cycles (%uus)", elapsed, k_cyc_to_us_ceil32(elapsed)); |
| 90 | + __ASSERT(elapsed > k_ms_to_cyc_ceil32(CONFIG_TEST_SLEEP_DURATION_MS - 10) && |
| 91 | + elapsed < k_ms_to_cyc_ceil32(CONFIG_TEST_SLEEP_DURATION_MS + 10), |
| 92 | + "expected time to elapse is 1s"); |
| 93 | +} |
| 94 | + |
| 95 | +int main(void) |
| 96 | +{ |
| 97 | + uint32_t start_time; |
| 98 | + int ret; |
| 99 | + |
| 100 | + ret = gpio_is_ready_dt(&led); |
| 101 | + __ASSERT(ret, "Error: GPIO Device not ready"); |
| 102 | + |
| 103 | + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); |
| 104 | + __ASSERT(ret == 0, "Could not configure led GPIO"); |
| 105 | + |
| 106 | + /* Wait a bit to solve NRFS request timeout issue. */ |
| 107 | + k_msleep(500); |
| 108 | + |
| 109 | + while (1) { |
| 110 | + ret = gpio_pin_set_dt(&led, 0); |
| 111 | + __ASSERT(ret == 0, "Unable to turn off LED"); |
| 112 | + k_msleep(CONFIG_TEST_SLEEP_DURATION_MS); |
| 113 | + ret = gpio_pin_set_dt(&led, 1); |
| 114 | + __ASSERT(ret == 0, "Unable to turn on LED"); |
| 115 | + k_busy_wait(100000); |
| 116 | + |
| 117 | +#if defined(CONFIG_CLOCK_CONTROL) && defined(CONFIG_SOC_NRF54H20_CPUAPP) |
| 118 | + for (int i = 0; i <= ARRAY_SIZE(freq); i++) { |
| 119 | + start_time = start_timer(counter_dev); |
| 120 | + if (i) { |
| 121 | + set_global_domain_frequency(freq[i - 1]); |
| 122 | + } |
| 123 | + verify_timer(start_time); |
| 124 | + k_busy_wait(100000); |
| 125 | + } |
| 126 | +#else |
| 127 | + start_time = start_timer(counter_dev); |
| 128 | + verify_timer(start_time); |
| 129 | + k_busy_wait(100000); |
| 130 | +#endif /* CONFIG_CLOCK_CONTROL && CONFIG_SOC_NRF54H20_CPUAPP */ |
| 131 | + } |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
0 commit comments