Skip to content

Commit 80fe4e5

Browse files
committed
sys/auto_init: add auto_init_wdt_{event, thread} modules
1 parent b8a6988 commit 80fe4e5

File tree

9 files changed

+184
-1
lines changed

9 files changed

+184
-1
lines changed

drivers/include/periph/wdt.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,42 @@
157157
* Makefile add:
158158
*
159159
* ~~~~~~~~~~~~~~~~~~~~~~~~ {.mk}
160-
* MODULE += periph_wdt_cb
160+
* USEMODULE += periph_wdt_cb
161161
* ~~~~~~~~~~~~~~~~~~~~~~~~
162162
*
163+
* WDT Auto-Start
164+
* ==============
165+
*
166+
* It is possible to enable the Watchdog in early boot, before application startup:
167+
*
168+
* ~~~~~~~~~~~~~~~~~~~~~~~~ {.mk}
169+
* USEMODULE += periph_wdt_auto_start
170+
* ~~~~~~~~~~~~~~~~~~~~~~~~
171+
*
172+
* The watchdog will automatically be initialized with the parameters
173+
* @ref CONFIG_PERIPH_WDT_WIN_MIN_MS and @ref CONFIG_PERIPH_WDT_WIN_MAX_MS
174+
*
175+
* It is also possible to automatically kick the watchdog.
176+
* This is a very non-invasive way of using the watchdog, but it is also very
177+
* weak as it can only detect situations where low-priority threads are
178+
* starved from execution and may even trigger wrongly in situations where the
179+
* system just experiences high load, but would otherwise have recovered on it's own.
180+
*
181+
* If you want to enable it anyway, select this module:
182+
*
183+
* ~~~~~~~~~~~~~~~~~~~~~~~~ {.mk}
184+
* USEMODULE += auto_init_wdt_thread
185+
* ~~~~~~~~~~~~~~~~~~~~~~~~
186+
*
187+
* If you are using an event thread, you can also use the watchdog to ensure that events
188+
* are processed in time.
189+
* To do so, add
190+
*
191+
* ~~~~~~~~~~~~~~~~~~~~~~~~ {.mk}
192+
* USEMODULE += auto_init_wdt_event
193+
* ~~~~~~~~~~~~~~~~~~~~~~~~
194+
*
195+
*
163196
* @{
164197
*
165198
* @file wdt.h

makefiles/pseudomodules.inc.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ NO_PSEUDOMODULES += auto_init_multimedia
589589
NO_PSEUDOMODULES += auto_init_security
590590
NO_PSEUDOMODULES += auto_init_usbus
591591
NO_PSEUDOMODULES += auto_init_screen
592+
NO_PSEUDOMODULES += auto_init_wdt_event
593+
NO_PSEUDOMODULES += auto_init_wdt_thread
592594

593595
# Packages and drivers may also add modules to PSEUDOMODULES in their `Makefile.include`.
594596

sys/Makefile.dep

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ ifneq (,$(filter auto_init_gnrc_netif,$(USEMODULE)))
2424
USEMODULE += gnrc_netif_init_devs
2525
endif
2626

27+
ifneq (,$(filter auto_init_wdt_thread,$(USEMODULE)))
28+
USEMODULE += periph_wdt_auto_start
29+
USEMODULE += ztimer_msec
30+
31+
# we can only have either auto_init_wdt_event or auto_init_wdt_thread
32+
ifneq (,$(filter auto_init_wdt_event,$(USEMODULE)))
33+
$(error auto_init_wdt_event and auto_init_wdt_thread are mutually exclusive)
34+
endif
35+
endif
36+
37+
ifneq (,$(filter auto_init_wdt_event,$(USEMODULE)))
38+
USEMODULE += event_periodic_callback
39+
USEMODULE += event_thread
40+
USEMODULE += periph_wdt_auto_start
41+
USEMODULE += ztimer_msec
42+
endif
43+
2744
ifneq (,$(filter auto_init_sock_dns,$(USEMODULE)))
2845
ifneq (,$(filter ipv4,$(USEMODULE)))
2946
USEMODULE += ipv4_addr

sys/auto_init/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ ifneq (,$(filter auto_init_screen,$(USEMODULE)))
3030
DIRS += screen
3131
endif
3232

33+
ifneq (,$(filter auto_init_wdt_event,$(USEMODULE)))
34+
DIRS += wdt_event
35+
endif
36+
37+
ifneq (,$(filter auto_init_wdt_thread,$(USEMODULE)))
38+
DIRS += wdt_thread
39+
endif
40+
3341
include $(RIOTBASE)/Makefile.base

sys/auto_init/include/auto_init_priorities.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ extern "C" {
4141
*/
4242
#define AUTO_INIT_PRIO_MOD_XTIMER 1030
4343
#endif
44+
#ifndef AUTO_INIT_PRIO_WDT_THREAD
45+
/**
46+
* @brief WDT priority
47+
*/
48+
#define AUTO_INIT_PRIO_WDT_THREAD 1035
49+
#endif
4450
#ifndef AUTO_INIT_PRIO_MOD_RANDOM
4551
/**
4652
* @brief RNG priority
@@ -71,6 +77,12 @@ extern "C" {
7177
*/
7278
#define AUTO_INIT_PRIO_MOD_EVENT_THREAD 1080
7379
#endif
80+
#ifndef AUTO_INIT_PRIO_WDT_EVENT
81+
/**
82+
* @brief WDT event priority
83+
*/
84+
#define AUTO_INIT_PRIO_WDT_EVENT 1085
85+
#endif
7486
#ifndef AUTO_INIT_PRIO_MOD_SYS_BUS
7587
/**
7688
* @brief sys bus priority

sys/auto_init/wdt_event/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MODULE = auto_init_wdt_event
2+
3+
include $(RIOTBASE)/Makefile.base

sys/auto_init/wdt_event/wdt.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2022 ML!PA Consulting GmbH
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup sys_auto_init
11+
* @{
12+
*
13+
* @file wdt.c
14+
* @brief Watchdog Event
15+
*
16+
* @author Benjamin Valentin <[email protected]>
17+
*
18+
* @}
19+
*/
20+
21+
#include "auto_init.h"
22+
#include "auto_init_utils.h"
23+
#include "auto_init_priorities.h"
24+
25+
#include "architecture.h"
26+
#include "event/periodic_callback.h"
27+
#include "event/thread.h"
28+
#include "periph/wdt.h"
29+
#include "ztimer.h"
30+
31+
static void _wdt_event_cb(void *ctx)
32+
{
33+
(void)ctx;
34+
wdt_kick();
35+
}
36+
37+
static void auto_init_wdt_event(void)
38+
{
39+
static event_periodic_callback_t wdt_event;
40+
unsigned sleep_ms = (CONFIG_PERIPH_WDT_WIN_MIN_MS + CONFIG_PERIPH_WDT_WIN_MAX_MS)
41+
/ 2;
42+
43+
event_periodic_callback_init(&wdt_event, ZTIMER_MSEC, EVENT_PRIO_LOWEST, _wdt_event_cb, NULL);
44+
event_periodic_callback_start(&wdt_event, sleep_ms);
45+
}
46+
47+
AUTO_INIT(auto_init_wdt_event, AUTO_INIT_PRIO_WDT_EVENT);

sys/auto_init/wdt_thread/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MODULE = auto_init_wdt_thread
2+
3+
include $(RIOTBASE)/Makefile.base

sys/auto_init/wdt_thread/wdt.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2022 ML!PA Consulting GmbH
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup sys_auto_init
11+
* @{
12+
*
13+
* @file wdt.c
14+
* @brief Watchdog Thread
15+
*
16+
* @author Benjamin Valentin <[email protected]>
17+
*
18+
* @}
19+
*/
20+
21+
#include "auto_init.h"
22+
#include "auto_init_utils.h"
23+
#include "auto_init_priorities.h"
24+
25+
#include "architecture.h"
26+
#include "periph/wdt.h"
27+
#include "ztimer.h"
28+
29+
#ifndef WDT_THREAD_STACKSIZE
30+
#if DEVELHELP
31+
#define WDT_THREAD_STACKSIZE THREAD_STACKSIZE_SMALL
32+
#else
33+
#define WDT_THREAD_STACKSIZE THREAD_STACKSIZE_TINY
34+
#endif
35+
#endif
36+
37+
static char WORD_ALIGNED wdt_stack[WDT_THREAD_STACKSIZE];
38+
39+
static void *_wdt_thread(void *ctx)
40+
{
41+
(void)ctx;
42+
unsigned sleep_ms = (CONFIG_PERIPH_WDT_WIN_MIN_MS + CONFIG_PERIPH_WDT_WIN_MAX_MS)
43+
/ 2;
44+
while (1) {
45+
ztimer_sleep(ZTIMER_MSEC, sleep_ms);
46+
wdt_kick();
47+
}
48+
49+
return NULL;
50+
}
51+
52+
static void auto_init_wdt_thread(void)
53+
{
54+
thread_create(wdt_stack, sizeof(wdt_stack), THREAD_PRIORITY_MIN,
55+
THREAD_CREATE_STACKTEST, _wdt_thread, NULL, "watchdog");
56+
}
57+
58+
AUTO_INIT(auto_init_wdt_thread, AUTO_INIT_PRIO_WDT_THREAD);

0 commit comments

Comments
 (0)