Skip to content

Commit c25d250

Browse files
thedjnKde-nordic
authored andcommitted
boot: zephyr: Move watchdog code to separate file
Moves the code for this functionality to a separate file so it can be fixed from a central location and replaced by modules Signed-off-by: Jamie McCrae <[email protected]>
1 parent 66a890a commit c25d250

File tree

4 files changed

+88
-79
lines changed

4 files changed

+88
-79
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ zephyr_library_sources(
7171
flash_map_extended.c
7272
os.c
7373
keys.c
74+
watchdog.c
7475
)
7576

7677
if(DEFINED CONFIG_ENABLE_MGMT_PERUSER)

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define __MCUBOOT_CONFIG_H__
1212

1313
#include <zephyr/devicetree.h>
14+
#include <watchdog.h>
1415

1516
#ifdef CONFIG_BOOT_SIGNATURE_TYPE_RSA
1617
#define MCUBOOT_SIGN_RSA
@@ -442,85 +443,6 @@
442443
#define MCUBOOT_BOOTUTIL_LIB_FOR_DIRECT_XIP 1
443444
#endif
444445

445-
#if CONFIG_BOOT_WATCHDOG_FEED
446-
#if CONFIG_BOOT_WATCHDOG_FEED_NRFX_WDT
447-
#include <nrfx_wdt.h>
448-
449-
#define FEED_WDT_INST(inst) \
450-
do { \
451-
for (uint8_t i = 0; i < NRF_WDT_CHANNEL_NUMBER; i++) \
452-
{ \
453-
nrf_wdt_reload_request_set(inst, \
454-
(nrf_wdt_rr_register_t)(NRF_WDT_RR0 + i)); \
455-
} \
456-
} while (0)
457-
#if defined(NRF_WDT0) && defined(NRF_WDT1)
458-
#define MCUBOOT_WATCHDOG_FEED() \
459-
do { \
460-
FEED_WDT_INST(NRF_WDT0); \
461-
FEED_WDT_INST(NRF_WDT1); \
462-
} while (0)
463-
#elif defined(NRF_WDT0)
464-
#define MCUBOOT_WATCHDOG_FEED() \
465-
FEED_WDT_INST(NRF_WDT0);
466-
#elif defined(NRF_WDT30) && defined(NRF_WDT31)
467-
#define MCUBOOT_WATCHDOG_FEED() \
468-
do { \
469-
FEED_WDT_INST(NRF_WDT30); \
470-
FEED_WDT_INST(NRF_WDT31); \
471-
} while (0)
472-
#elif defined(NRF_WDT30)
473-
#define MCUBOOT_WATCHDOG_FEED() \
474-
FEED_WDT_INST(NRF_WDT30);
475-
#elif defined(NRF_WDT31)
476-
#define MCUBOOT_WATCHDOG_FEED() \
477-
FEED_WDT_INST(NRF_WDT31);
478-
#elif defined(NRF_WDT010)
479-
#define MCUBOOT_WATCHDOG_FEED() \
480-
FEED_WDT_INST(NRF_WDT010);
481-
#else
482-
#error "No NRFX WDT instances enabled"
483-
#endif
484-
485-
#elif DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay) /* CONFIG_BOOT_WATCHDOG_FEED_NRFX_WDT */
486-
#include <zephyr/device.h>
487-
#include <zephyr/drivers/watchdog.h>
488-
489-
#define MCUBOOT_WATCHDOG_SETUP() \
490-
do { \
491-
const struct device* wdt = \
492-
DEVICE_DT_GET(DT_ALIAS(watchdog0)); \
493-
if (device_is_ready(wdt)) { \
494-
wdt_setup(wdt, 0); \
495-
} \
496-
} while (0)
497-
498-
#define MCUBOOT_WATCHDOG_FEED() \
499-
do { \
500-
const struct device* wdt = \
501-
DEVICE_DT_GET(DT_ALIAS(watchdog0)); \
502-
if (device_is_ready(wdt)) { \
503-
wdt_feed(wdt, 0); \
504-
} \
505-
} while (0)
506-
#else /* DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay) */
507-
/* No vendor implementation, no-op for historical reasons */
508-
#define MCUBOOT_WATCHDOG_FEED() \
509-
do { \
510-
} while (0)
511-
#endif
512-
#else /* CONFIG_BOOT_WATCHDOG_FEED */
513-
/* Not enabled, no feed activity */
514-
#define MCUBOOT_WATCHDOG_FEED() \
515-
do { \
516-
} while (0)
517-
518-
#endif /* CONFIG_BOOT_WATCHDOG_FEED */
519-
520-
#ifndef MCUBOOT_WATCHDOG_SETUP
521-
#define MCUBOOT_WATCHDOG_SETUP()
522-
#endif
523-
524446
#define MCUBOOT_CPU_IDLE() \
525447
if (!IS_ENABLED(CONFIG_MULTITHREADING)) { \
526448
k_cpu_idle(); \

boot/zephyr/include/watchdog.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2025, Jamie McCrae
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/** Set watchdog timer up and active it ready for feeding. */
8+
void mcuboot_watchdog_setup(void);
9+
10+
/** Feed active watchdog to prevent timeout. */
11+
void mcuboot_watchdog_feed(void);
12+
13+
#define MCUBOOT_WATCHDOG_SETUP() mcuboot_watchdog_setup()
14+
#define MCUBOOT_WATCHDOG_FEED() mcuboot_watchdog_feed()

boot/zephyr/watchdog.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2019-2023 MCUboot authors
3+
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
4+
* Copyright (c) 2025 Jamie McCrae
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <watchdog.h>
10+
#include <bootutil/bootutil_log.h>
11+
#include <zephyr/device.h>
12+
#include <zephyr/drivers/watchdog.h>
13+
14+
#ifdef CONFIG_BOOT_WATCHDOG_FEED_NRFX_WDT
15+
#include <nrfx_wdt.h>
16+
#endif
17+
18+
BOOT_LOG_MODULE_DECLARE(mcuboot);
19+
20+
__weak void mcuboot_watchdog_setup(void)
21+
{
22+
#if defined(CONFIG_WATCHDOG) && DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay)
23+
const struct device *watchdog_device = DEVICE_DT_GET(DT_ALIAS(watchdog0));
24+
25+
if (device_is_ready(watchdog_device)) {
26+
int rc;
27+
28+
rc = wdt_setup(watchdog_device, 0);
29+
30+
if (rc != 0) {
31+
BOOT_LOG_ERR("Watchdog setup failed: %d", rc);
32+
}
33+
}
34+
#endif
35+
}
36+
37+
__weak void mcuboot_watchdog_feed(void)
38+
{
39+
#if CONFIG_BOOT_WATCHDOG_FEED_NRFX_WDT
40+
#define FEED_NRFX_WDT_INST(inst) \
41+
do { \
42+
for (uint8_t i = 0; i < NRF_WDT_CHANNEL_NUMBER; i++) \
43+
{ \
44+
nrf_wdt_reload_request_set(inst, \
45+
(nrf_wdt_rr_register_t)(NRF_WDT_RR0 + i)); \
46+
} \
47+
} while (0)
48+
#if defined(NRF_WDT0) && defined(NRF_WDT1)
49+
FEED_NRFX_WDT_INST(NRF_WDT0);
50+
FEED_NRFX_WDT_INST(NRF_WDT1);
51+
#elif defined(NRF_WDT0)
52+
FEED_NRFX_WDT_INST(NRF_WDT0);
53+
#elif defined(NRF_WDT30) && defined(NRF_WDT31)
54+
FEED_NRFX_WDT_INST(NRF_WDT30);
55+
FEED_NRFX_WDT_INST(NRF_WDT31);
56+
#elif defined(NRF_WDT30)
57+
FEED_NRFX_WDT_INST(NRF_WDT30);
58+
#elif defined(NRF_WDT31)
59+
FEED_NRFX_WDT_INST(NRF_WDT31);
60+
#elif defined(NRF_WDT010)
61+
FEED_NRFX_WDT_INST(NRF_WDT010);
62+
#else
63+
#error "No NRFX WDT instances enabled"
64+
#endif
65+
#elif defined(CONFIG_WATCHDOG) && DT_NODE_HAS_STATUS(DT_ALIAS(watchdog0), okay)
66+
const struct device *watchdog_device = DEVICE_DT_GET(DT_ALIAS(watchdog0));
67+
68+
if (device_is_ready(watchdog_device)) {
69+
wdt_feed(watchdog_device, 0);
70+
}
71+
#endif
72+
}

0 commit comments

Comments
 (0)