Skip to content

Commit 0779ca0

Browse files
committed
Nuvoton: M487: Fix reset loop with target.wdt-reset-workaround
We may trap in DPD/SPD wake-up reset loop when multi-level bootloaders (e.g. LDROM and MCUBoot) and application all or partial integrate this workaround. To avoid this, the wake-up flag CLK_PMUSTS_TMRWK_Msk is used to guard from duplicate wake-up reset setups and the clear-up of this flag is delayed to hal_watchdog_init, which will be invoked via Mbed Watchdog API. This approach works based on the assumptions: 1. Only application will set up WDT. LDROM and MCUBoot won't. 2. WDT setup is done via Mbed Watchdog API, not direct WDT control.
1 parent c7d1c32 commit 0779ca0

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

targets/TARGET_NUVOTON/TARGET_M480/mbed_overrides.c

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ void mbed_sdk_init(void)
111111
* and WDT wake-up from PD will. So don't rely on SYS_IS_WDT_RST() to
112112
* determine to use or not the workaround.
113113
*
114-
* NOTE: To avoid the workaround duplicate run by bootloader (e.g. MCUBoot)
115-
* and application and then crash when bootloader is enabled, the workaround
116-
* is applied only when any H/W reset flag (SYS.RSTSTS) raised.
114+
* NOTE: We may trap in DPD/SPD wake-up reset loop when multi-level
115+
* bootloaders (e.g. LDROM and MCUBoot) and application all integrate
116+
* this workaround. To avoid this, the wake-up flag CLK_PMUSTS_TMRWK_Msk
117+
* is used to guard from duplicate wake-up reset setups by delaying
118+
* its clean-up to hal_watchdog_init (watchdog_api.c).
117119
*
118120
* NOTE: The workaround will change SYS.RSTSTS and DEVICE_RESET_REASON won't
119121
* work as expected. Don't enable MBED_CONF_TARGET_WDT_RESET_WORKAROUND and
@@ -133,31 +135,10 @@ MBED_CONF_TARGET_WDT_RESET_WORKAROUND and DEVICE_RESET_REASON \
133135
at the same time."
134136
#endif
135137

136-
#define ALL_RESET_FLAGS \
137-
(SYS_RSTSTS_PORF_Msk | \
138-
SYS_RSTSTS_PINRF_Msk | \
139-
SYS_RSTSTS_WDTRF_Msk | \
140-
SYS_RSTSTS_LVRF_Msk | \
141-
SYS_RSTSTS_BODRF_Msk | \
142-
SYS_RSTSTS_SYSRF_Msk | \
143-
SYS_RSTSTS_CPURF_Msk | \
144-
SYS_RSTSTS_CPULKRF_Msk)
145-
146-
/* Apply the workaround only when any H/W reset flag is raised. For
147-
* bootloader enabled application, the workaround must apply either
148-
* by bootloader or by application, but not both.
149-
*/
150-
if (SYS->RSTSTS & ALL_RESET_FLAGS) {
138+
do {
151139
/* Re-unlock protected clock setting */
152140
SYS_UnlockReg();
153141

154-
/* Without this, bootloader enabled application will trap in
155-
* loop of wake-up timer wake-up reset armed here by bootloader
156-
* and application alternately, if both integrate this piece
157-
* of code.
158-
*/
159-
SYS_CLEAR_RST_SOURCE(ALL_RESET_FLAGS);
160-
161142
/* Release I/O hold status */
162143
CLK->IOPDCTL = 1;
163144

@@ -174,12 +155,14 @@ at the same time."
174155
MBED_UNREACHABLE;
175156
}
176157

177-
/* Clean previous wake-up flag */
178-
CLK->PMUSTS |= (CLK_PMUSTS_CLRWK_Msk | CLK_PMUSTS_TMRWK_Msk);
158+
/* Don't clean wake-up flag here (see above) */
159+
#if 0
160+
CLK->PMUSTS |= (CLK_PMUSTS_CLRWK_Msk | CLK_PMUSTS_TMRWK_Msk);
161+
#endif
179162

180163
/* Lock protected registers */
181164
SYS_LockReg();
182-
}
165+
} while (0);
183166
#endif
184167
}
185168

targets/TARGET_NUVOTON/TARGET_M480/watchdog_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ watchdog_status_t hal_watchdog_init(const watchdog_config_t *config)
8888

8989
SYS_UnlockReg();
9090

91+
/* See mbed_sdk_init (mbed_override.c) on workaround to
92+
* H/W limit with WDT reset from PD.
93+
*/
94+
#if MBED_CONF_TARGET_WDT_RESET_WORKAROUND
95+
CLK->PMUSTS |= (CLK_PMUSTS_CLRWK_Msk | CLK_PMUSTS_TMRWK_Msk);
96+
#endif
97+
9198
/* Enable IP module clock */
9299
CLK_EnableModuleClock(WDT_MODULE);
93100

0 commit comments

Comments
 (0)