-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
main.c
86 lines (71 loc) · 1.81 KB
/
main.c
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
/*
* Copyright (c) 2015 Intel Corporation
* Copyright (c) 2018 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <watchdog.h>
#include <misc/printk.h>
#define WDT_FEED_TRIES 5
#ifdef CONFIG_WDT_0_NAME
#define WDT_DEV_NAME CONFIG_WDT_0_NAME
#else
#define WDT_DEV_NAME DT_WDT_0_NAME
#endif
static void wdt_callback(struct device *wdt_dev, int channel_id)
{
ARG_UNUSED(wdt_dev);
ARG_UNUSED(channel_id);
/* Watchdog timer expired. Handle it here.
* Remember that SoC reset will be done soon.
*/
}
void main(void)
{
int err;
int wdt_channel_id;
struct device *wdt;
struct wdt_timeout_cfg wdt_config;
printk("Watchdog sample application\n");
wdt = device_get_binding(WDT_DEV_NAME);
if (!wdt) {
printk("Cannot get WDT device\n");
return;
}
/* Reset SoC when watchdog timer expires. */
wdt_config.flags = WDT_FLAG_RESET_SOC;
/* Expire watchdog after 5000 milliseconds. */
wdt_config.window.min = 0U;
wdt_config.window.max = 5000U;
/* Set up watchdog callback. Jump into it when watchdog expired. */
wdt_config.callback = wdt_callback;
wdt_channel_id = wdt_install_timeout(wdt, &wdt_config);
if (wdt_channel_id == -ENOTSUP) {
/* IWDG driver for STM32 doesn't support callback */
wdt_config.callback = NULL;
wdt_channel_id = wdt_install_timeout(wdt, &wdt_config);
}
if (wdt_channel_id < 0) {
printk("Watchdog install error\n");
return;
}
err = wdt_setup(wdt, 0);
if (err < 0) {
printk("Watchdog setup error\n");
return;
}
/* Feeding watchdog. */
printk("Feeding watchdog %d times\n", WDT_FEED_TRIES);
for (int i = 0; i < WDT_FEED_TRIES; ++i) {
printk("Feeding watchdog...\n");
wdt_feed(wdt, wdt_channel_id);
k_sleep(1000);
}
/* Waiting for the SoC reset. */
printk("Waiting for reset...\n");
while (1) {
k_yield();
};
}