Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /* * Copyright (c) 2018 Nordic Semiconductor. * * SPDX-License-Identifier: Apache-2.0 */ /* * @addtogroup t_wdt_basic * @{ * @defgroup t_wdt_timeout test_wdt_timeout_capabilities * @brief TestPurpose: verify Watchdog Timer install/setup/feed can work, * and reset can be triggered when timeout * @details * There are three tests. Each test provide watchdog installation, setup and * wait for reset. Three variables are placed in noinit section to prevent * clearing them during board reset.These variables save number of the current * test case, current test state and value to check if test passed or not. * * - Test Steps - test_wdt_no_callback * -# Get device. * -# Check if the state was changed and test should be finished. * -# Set callback to NULL value. * -# Install watchdog with current configuration. * -# Setup watchdog with no additions options. * -# Wait for reset. * - Expected Results * -# If reset comes, the same testcase should be executed but state should * be set to finish value and test should return with success. * * - Test Steps - test_wdt_callback_1 * -# Get device. * -# Check if the state was changed. If so check testvalue if interrupt * occurred. * -# Set callback as pointer to wdt_int_cb0. * -# Install watchdog with current configuration. * -# Setup watchdog with no additions options. * -# Wait for reset. * - Expected Results * -# If reset comes, the same testcase should be executed but state should be * set to finish value and test checks if m_testvalue was set in interrupt * right before reset. * * - Test Steps - test_wdt_callback_2 * -# Get device. * -# Check if the state was changed. If so check testvalue if interrupt * occurred. * -# Install two watchdogs: set pointer to wdt_int_cb0 as callback for first * watchdog and wdt_int_cb1 for second one. * -# Install watchdog with current configuration. * -# Setup watchdog with no additions options. * -# Wait for reset and feed first watchdog. * - Expected Results * -# If reset comes, the same testcase should be executed but state should be * set to finish value and test checks if m_testvalue was set in callback * of second watchdog right before reset. * * @} */ #include <watchdog.h> #include <zephyr.h> #include <ztest.h> #include "test_wdt.h" #define WDT_DEV_NAME CONFIG_WDT_0_NAME #define WDT_TEST_STATE_IDLE 0 #define WDT_TEST_STATE_CHECK_RESET 1 #define WDT_TEST_CB0_TEST_VALUE 0x0CB0 #define WDT_TEST_CB1_TEST_VALUE 0x0CB1 #ifdef CONFIG_WDT_NRFX #define TIMEOUTS 2 #else #define TIMEOUTS 1 #endif #define TEST_WDT_CALLBACK_2 (TIMEOUTS > 1) static struct wdt_timeout_cfg m_cfg_wdt0; #if TEST_WDT_CALLBACK_2 static struct wdt_timeout_cfg m_cfg_wdt1; #endif /* m_state indicates state of particular test. Used to check whether testcase * should go to reset state or check other values after reset. */ volatile uint32_t m_state __attribute__((section("app_noinit"))); /* m_testcase_index is incremented after each test to make test possible * switch to next testcase. */ volatile uint32_t m_testcase_index __attribute__((section("app_noinit"))); /* m_testvalue contains value set in interrupt callback to point whether * first or second interrupt was fired. */ volatile uint32_t m_testvalue __attribute__((section("app_noinit"))); static void wdt_int_cb0(struct device *wdt_dev, int channel_id) { ARG_UNUSED(wdt_dev); ARG_UNUSED(channel_id); m_testvalue += WDT_TEST_CB0_TEST_VALUE; } #if TEST_WDT_CALLBACK_2 static void wdt_int_cb1(struct device *wdt_dev, int channel_id) { ARG_UNUSED(wdt_dev); ARG_UNUSED(channel_id); m_testvalue += WDT_TEST_CB1_TEST_VALUE; } #endif static int test_wdt_no_callback(void) { int err; struct device *wdt = device_get_binding(WDT_DEV_NAME); if (!wdt) { TC_PRINT("Cannot get WDT device\n"); return TC_FAIL; } TC_PRINT("Testcase: %s\n", __func__); if (m_state == WDT_TEST_STATE_CHECK_RESET) { m_state = WDT_TEST_STATE_IDLE; m_testcase_index++; TC_PRINT("Testcase passed\n"); return TC_PASS; } m_cfg_wdt0.callback = NULL; m_cfg_wdt0.flags = WDT_FLAG_RESET_SOC; m_cfg_wdt0.window.max = 2000; err = wdt_install_timeout(wdt, &m_cfg_wdt0); if (err < 0) { TC_PRINT("Watchdog install error\n"); } err = wdt_setup(wdt, 0); if (err < 0) { TC_PRINT("Watchdog setup error\n"); } TC_PRINT("Waiting to restart MCU\n"); m_testvalue = 0; m_state = WDT_TEST_STATE_CHECK_RESET; while (1) { }; } static int test_wdt_callback_1(void) { int err; struct device *wdt = device_get_binding(WDT_DEV_NAME); if (!wdt) { TC_PRINT("Cannot get WDT device\n"); return TC_FAIL; } TC_PRINT("Testcase: %s\n", __func__); if (m_state == WDT_TEST_STATE_CHECK_RESET) { m_state = WDT_TEST_STATE_IDLE; m_testcase_index++; if (m_testvalue == WDT_TEST_CB0_TEST_VALUE) { TC_PRINT("Testcase passed\n"); return TC_PASS; } else { return TC_FAIL; } } m_testvalue = 0; m_cfg_wdt0.flags = WDT_FLAG_RESET_SOC; m_cfg_wdt0.callback = wdt_int_cb0; m_cfg_wdt0.window.max = 2000; err = wdt_install_timeout(wdt, &m_cfg_wdt0); if (err < 0) { TC_PRINT("Watchdog install error\n"); return TC_FAIL; } err = wdt_setup(wdt, 0); if (err < 0) { TC_PRINT("Watchdog setup error\n"); return TC_FAIL; } TC_PRINT("Waiting to restart MCU\n"); m_testvalue = 0; m_state = WDT_TEST_STATE_CHECK_RESET; while (1) { }; } #if TEST_WDT_CALLBACK_2 static int test_wdt_callback_2(void) { int err; struct device *wdt = device_get_binding(WDT_DEV_NAME); if (!wdt) { TC_PRINT("Cannot get WDT device\n"); return TC_FAIL; } TC_PRINT("Testcase: %s\n", __func__); if (m_state == WDT_TEST_STATE_CHECK_RESET) { m_state = WDT_TEST_STATE_IDLE; m_testcase_index++; if (m_testvalue == WDT_TEST_CB1_TEST_VALUE) { TC_PRINT("Testcase passed\n"); return TC_PASS; } else { return TC_FAIL; } } m_testvalue = 0; m_cfg_wdt0.callback = wdt_int_cb0; m_cfg_wdt0.flags = WDT_FLAG_RESET_SOC; m_cfg_wdt0.window.max = 2000; err = wdt_install_timeout(wdt, &m_cfg_wdt0); if (err < 0) { TC_PRINT("Watchdog install error\n"); return TC_FAIL; } m_cfg_wdt1.callback = wdt_int_cb1; m_cfg_wdt1.flags = WDT_FLAG_RESET_SOC; m_cfg_wdt1.window.max = 2000; err = wdt_install_timeout(wdt, &m_cfg_wdt1); if (err < 0) { TC_PRINT("Watchdog install error\n"); return TC_FAIL; } err = wdt_setup(wdt, 0); if (err < 0) { TC_PRINT("Watchdog setup error\n"); return TC_FAIL; } TC_PRINT("Waiting to restart MCU\n"); m_testvalue = 0; m_state = WDT_TEST_STATE_CHECK_RESET; while (1) { wdt_feed(wdt, 0); }; } #endif void test_wdt(void) { if (m_testcase_index == 0) { zassert_true(test_wdt_no_callback() == TC_PASS, NULL); } if (m_testcase_index == 1) { zassert_true(test_wdt_callback_1() == TC_PASS, NULL); } if (m_testcase_index == 2) { #if TEST_WDT_CALLBACK_2 zassert_true(test_wdt_callback_2() == TC_PASS, NULL); #else m_testcase_index++; #endif } if (m_testcase_index > 2) { m_testcase_index = 0; m_state = WDT_TEST_STATE_IDLE; } } |