|
| 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/ztest.h> |
| 9 | +#include <zephyr/drivers/counter.h> |
| 10 | +#include <zephyr/drivers/flash.h> |
| 11 | +#include <zephyr/devicetree.h> |
| 12 | + |
| 13 | +#define TEST_AREA_DEV_NODE DT_INST(0, jedec_mspi_nor) |
| 14 | +#define TEST_AREA_OFFSET 0x0 |
| 15 | +#define EXPECTED_SIZE 64 |
| 16 | +#define TIMEOUT 15000000 |
| 17 | + |
| 18 | +static const struct device *const flash_dev = DEVICE_DT_GET(TEST_AREA_DEV_NODE); |
| 19 | +static const struct device *const flpr_fault_timer = DEVICE_DT_GET(DT_NODELABEL(fault_timer)); |
| 20 | +static volatile bool timer_irq; |
| 21 | + |
| 22 | +static void timer_irq_handler(const struct device *dev, void *user_data) |
| 23 | +{ |
| 24 | + ARG_UNUSED(dev); |
| 25 | + ARG_UNUSED(user_data); |
| 26 | + |
| 27 | + timer_irq = true; |
| 28 | +} |
| 29 | + |
| 30 | +static void fault_timer_before(void *arg) |
| 31 | +{ |
| 32 | + ARG_UNUSED(arg); |
| 33 | + |
| 34 | + int rc; |
| 35 | + const struct counter_top_cfg top_cfg = { |
| 36 | + .callback = timer_irq_handler, |
| 37 | + .user_data = NULL, |
| 38 | + .flags = 0, |
| 39 | + .ticks = counter_us_to_ticks(flpr_fault_timer, CONFIG_MSPI_NRFE_FAULT_TIMEOUT) |
| 40 | + }; |
| 41 | + |
| 42 | + zassert_true(device_is_ready(flash_dev)); |
| 43 | + zassert_true(device_is_ready(flpr_fault_timer)); |
| 44 | + rc = counter_set_top_value(flpr_fault_timer, &top_cfg); |
| 45 | + zassert_equal(rc, 0, "Cannot set top value"); |
| 46 | + |
| 47 | + timer_irq = false; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Check if the timer is not triggered when the flash is being read. |
| 52 | + * When we send commands to the flash the timer should be reset and stopped by FLPR. |
| 53 | + * If it is not then we will get an IRQ after CONFIG_MSPI_NRFE_FAULT_TIMEOUT. |
| 54 | + */ |
| 55 | +ZTEST(hpf_fault_timer, test_timer_timeout) |
| 56 | +{ |
| 57 | + int rc; |
| 58 | + uint8_t buf[EXPECTED_SIZE]; |
| 59 | + volatile uint32_t count; |
| 60 | + |
| 61 | + /* 1. The timer is started and the flash is read. */ |
| 62 | + rc = counter_start(flpr_fault_timer); |
| 63 | + zassert_equal(rc, 0, "Cannot start timer"); |
| 64 | + |
| 65 | + rc = flash_read(flash_dev, TEST_AREA_OFFSET, buf, EXPECTED_SIZE); |
| 66 | + zassert_equal(rc, 0, "Cannot read flash"); |
| 67 | + |
| 68 | + while (timer_irq == false && count < TIMEOUT) { |
| 69 | + count++; |
| 70 | + } |
| 71 | + count = 0; |
| 72 | + |
| 73 | + /* 2. The timer should not trigger an IRQ. */ |
| 74 | + zassert_false(timer_irq, "Timer IRQ triggered"); |
| 75 | + |
| 76 | + /* 3. The timer is started again and the flash is not read. */ |
| 77 | + rc = counter_start(flpr_fault_timer); |
| 78 | + zassert_equal(rc, 0, "Cannot start timer"); |
| 79 | + |
| 80 | + zassert_false(timer_irq, "Timer IRQ triggered"); |
| 81 | + |
| 82 | + while (timer_irq == false && count < TIMEOUT) { |
| 83 | + count++; |
| 84 | + } |
| 85 | + |
| 86 | + /* 4. FLPR is not supposed to stop the counter here, |
| 87 | + * because it does not receive any message. |
| 88 | + * The timer should trigger an IRQ. |
| 89 | + */ |
| 90 | + zassert_true(timer_irq, "Timer IRQ not triggered"); |
| 91 | + |
| 92 | + rc = counter_stop(flpr_fault_timer); |
| 93 | + zassert_equal(rc, 0, "Cannot stop timer"); |
| 94 | +} |
| 95 | + |
| 96 | +ZTEST_SUITE(hpf_fault_timer, NULL, NULL, fault_timer_before, NULL, NULL); |
0 commit comments