|
| 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/hwinfo.h> |
| 9 | +#include <zephyr/drivers/watchdog.h> |
| 10 | +#include "hw_info_helper.h" |
| 11 | + |
| 12 | +#include <zephyr/logging/log.h> |
| 13 | +LOG_MODULE_REGISTER(uicr_wdtstart, LOG_LEVEL_INF); |
| 14 | + |
| 15 | +static const struct device *const wdt_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(wdt011)); |
| 16 | +static int wdt_channel_id = -1; |
| 17 | + |
| 18 | + |
| 19 | +/* First call of configure_watchdog will: |
| 20 | + * - report that wdt_disable() returned -EFAULT (-14) |
| 21 | + * This is because driver is not aware that WDT was configured and started. |
| 22 | + * - NOT change WDT configuration |
| 23 | + * This is because WDT can be configured only when it is stopped. |
| 24 | + */ |
| 25 | +static void configure_watchdog(void) |
| 26 | +{ |
| 27 | + int32_t ret; |
| 28 | + struct wdt_timeout_cfg m_cfg_wdt0; |
| 29 | + /* After reconfiguration, watchdog window will be |
| 30 | + * larger than the one set by the Ironside. |
| 31 | + */ |
| 32 | + uint32_t watchdog_window = 5000U; |
| 33 | + |
| 34 | + if (!device_is_ready(wdt_dev)) { |
| 35 | + LOG_ERR("WDT device %s is not ready", wdt_dev->name); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + ret = wdt_disable(wdt_dev); |
| 40 | + LOG_INF("wdt_disable() returned %d", ret); |
| 41 | + |
| 42 | + m_cfg_wdt0.callback = NULL; |
| 43 | + m_cfg_wdt0.flags = WDT_FLAG_RESET_SOC; |
| 44 | + m_cfg_wdt0.window.max = watchdog_window; |
| 45 | + m_cfg_wdt0.window.min = 0U; |
| 46 | + wdt_channel_id = wdt_install_timeout(wdt_dev, &m_cfg_wdt0); |
| 47 | + if (wdt_channel_id < 0) { |
| 48 | + LOG_ERR("wdt_install_timeout() returned %d", wdt_channel_id); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + ret = wdt_setup(wdt_dev, WDT_OPT_PAUSE_HALTED_BY_DBG); |
| 53 | + if (ret < 0) { |
| 54 | + LOG_ERR("wdt_setup() returned %d", ret); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + LOG_INF("Watchdog configured with window of %u milliseconds", watchdog_window); |
| 59 | + print_bar(); |
| 60 | +} |
| 61 | + |
| 62 | +int main(void) |
| 63 | +{ |
| 64 | + int ret; |
| 65 | + uint32_t reset_cause = 0; |
| 66 | + |
| 67 | + LOG_INF("UICR.WDTSTART test on %s", CONFIG_BOARD_TARGET); |
| 68 | + print_bar(); |
| 69 | + |
| 70 | + /* Test relies on RESET_PIN to correctly start. */ |
| 71 | + get_current_reset_cause(&reset_cause); |
| 72 | + |
| 73 | + /* Report unexpected reset type. */ |
| 74 | + if (reset_cause & ~(RESET_PIN | RESET_WATCHDOG)) { |
| 75 | + LOG_INF("Unexpected reset cause was found!"); |
| 76 | + print_bar(); |
| 77 | + } |
| 78 | + |
| 79 | + /* Check if reset was due to pin reset. */ |
| 80 | + if (reset_cause & RESET_PIN) { |
| 81 | + LOG_INF("RESET_PIN detected"); |
| 82 | + print_bar(); |
| 83 | + clear_reset_cause(); |
| 84 | + |
| 85 | + LOG_INF("Watchdog shall fire in ~2 seconds"); |
| 86 | + k_sleep(K_FOREVER); |
| 87 | + } |
| 88 | + |
| 89 | + /* Check if reset was due to watchdog reset. */ |
| 90 | + if (reset_cause & RESET_WATCHDOG) { |
| 91 | + LOG_INF("RESET_WATCHDOG detected"); |
| 92 | + print_bar(); |
| 93 | + clear_reset_cause(); |
| 94 | + |
| 95 | + /* configure_watchdog() is intentionally called twice. |
| 96 | + * - First call will "align" WDT driver state to HW configuration. |
| 97 | + * This will enable WDT feeding but will NOT reconfigure WDT. |
| 98 | + * - Second call will actually apply new WDT configuration. |
| 99 | + */ |
| 100 | + configure_watchdog(); |
| 101 | + configure_watchdog(); |
| 102 | + LOG_INF("WATCHDOG was reconfigured"); |
| 103 | + |
| 104 | + for (int count = 1; count < 4; count++) { |
| 105 | + /* Sleep longer than watchdog window set by the Ironside. |
| 106 | + * But less than the window set during watchdog reconfiguration. |
| 107 | + */ |
| 108 | + k_msleep(3000); |
| 109 | + |
| 110 | + ret = wdt_feed(wdt_dev, wdt_channel_id); |
| 111 | + if (ret != 0) { |
| 112 | + LOG_ERR("wdt_feed() returned %d", ret); |
| 113 | + } else { |
| 114 | + LOG_INF("%u: Watchdog was feed", count); |
| 115 | + } |
| 116 | + } |
| 117 | + print_bar(); |
| 118 | + } |
| 119 | + |
| 120 | + ret = wdt_disable(wdt_dev); |
| 121 | + if (ret == 0) { |
| 122 | + LOG_INF("WATCHDOG was disabled"); |
| 123 | + } else { |
| 124 | + LOG_INF("FAIL: wdt_disable() returned %d", ret); |
| 125 | + } |
| 126 | + |
| 127 | + print_bar(); |
| 128 | + LOG_INF("Test is completed"); |
| 129 | +} |
0 commit comments