Skip to content

Commit c989809

Browse files
kl-cruzMaureenHelm
authored andcommitted
tests: watchdog: Add new test implementation
New test checks watchdog API if the simpliest test cases work correctly. Each test contains description at the begining of the source file. This implementation replace old SoC-centric implementation. Signed-off-by: Karol Lasończyk <[email protected]>
1 parent 30a24e8 commit c989809

File tree

5 files changed

+289
-162
lines changed

5 files changed

+289
-162
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
CONFIG_WATCHDOG=y
2+
CONFIG_WDT_NRFX=y
23
CONFIG_ZTEST=y
4+
CONFIG_APPLICATION_MEMORY=y
5+
CONFIG_BOOT_BANNER=n

tests/drivers/watchdog/wdt_basic_api/src/main.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2016 Intel Corporation
3+
* Copyright (c) 2018 Nordic Semiconductor
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -13,14 +14,12 @@
1314

1415
#include <zephyr.h>
1516
#include <ztest.h>
17+
#include "test_wdt.h"
1618

17-
extern void test_wdt_int_reset_26(void);
18-
extern void test_wdt_reset_26(void);
1919

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

0 commit comments

Comments
 (0)