From cb3a6783aaceb8ca5c3c3da2ff10e2a366374591 Mon Sep 17 00:00:00 2001 From: Chun-Chieh Li Date: Tue, 6 Apr 2021 15:25:01 +0800 Subject: [PATCH] M487: Get around h/w limit with WDT reset from PD Use WKT to get around this h/w limit --- .../TARGET_M480/device/startup_M480.c | 5 +--- .../TARGET_M480/mbed_overrides.c | 27 ++++++++++++++++--- .../TARGET_NUVOTON/TARGET_M480/reset_reason.c | 24 ++++++++++++++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/targets/TARGET_NUVOTON/TARGET_M480/device/startup_M480.c b/targets/TARGET_NUVOTON/TARGET_M480/device/startup_M480.c index 792d0eeb31a..1742e04d94b 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/device/startup_M480.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/device/startup_M480.c @@ -392,10 +392,7 @@ void Reset_Handler_1(void) { /* Disable register write-protection function */ SYS_UnlockReg(); - - /* Disable Power-on Reset function */ - SYS_DISABLE_POR(); - + /** * NOTE 1: Some register accesses require unlock. * NOTE 2: Because EBI (external SRAM) init is done in SystemInit(), SystemInit() must be called at the very start. diff --git a/targets/TARGET_NUVOTON/TARGET_M480/mbed_overrides.c b/targets/TARGET_NUVOTON/TARGET_M480/mbed_overrides.c index 23838a50038..a6db2db5398 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/mbed_overrides.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/mbed_overrides.c @@ -1,5 +1,7 @@ -/* mbed Microcontroller Library - * Copyright (c) 2015-2016 Nuvoton +/* + * Copyright (c) 2015-2016, Nuvoton Technology Corporation + * + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,7 +59,7 @@ void mbed_sdk_init(void) /* Set PCLK0/PCLK1 to HCLK/2 */ CLK->PCLKDIV = (CLK_PCLKDIV_PCLK0DIV2 | CLK_PCLKDIV_PCLK1DIV2); // PCLK divider set 2 - + #if DEVICE_ANALOGIN /* Vref connect to internal */ SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_VREFCTL_Msk) | SYS_VREFCTL_VREF_3_0V; @@ -69,4 +71,23 @@ void mbed_sdk_init(void) /* Lock protected registers */ SYS_LockReg(); + + /* Get around h/w limit with WDT reset from PD */ + if (SYS_IS_WDT_RST()) { + /* Re-unlock protected clock setting */ + SYS_UnlockReg(); + + /* Set up DPD power down mode */ + CLK->PMUSTS |= CLK_PMUSTS_CLRWK_Msk; + CLK->PMUSTS |= CLK_PMUSTS_TMRWK_Msk; + CLK_SetPowerDownMode(CLK_PMUCTL_PDMSEL_DPD); + + CLK_SET_WKTMR_INTERVAL(CLK_PMUCTL_WKTMRIS_256); + CLK_ENABLE_WKTMR(); + + CLK_PowerDown(); + + /* Lock protected registers */ + SYS_LockReg(); + } } diff --git a/targets/TARGET_NUVOTON/TARGET_M480/reset_reason.c b/targets/TARGET_NUVOTON/TARGET_M480/reset_reason.c index 91c29b6512a..332a93ae264 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/reset_reason.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/reset_reason.c @@ -1,5 +1,7 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017-2018 Nuvoton +/* + * Copyright (c) 2017-2018, Nuvoton Technology Corporation + * + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +39,12 @@ reset_reason_t hal_reset_reason_get(void) reset_reason_t reset_reason_cast; uint32_t reset_reason_count = 0; + /* Get around h/w limit with WDT reset from PD */ + if (CLK->PMUSTS & CLK_PMUSTS_TMRWK_Msk) { + /* Per test, these reset reason flags will set with WKT reset. Clear them for this resolution. */ + SYS_CLEAR_RST_SOURCE(SYS_RSTSTS_PINRF_Msk | SYS_RSTSTS_PORF_Msk); + } + if (SYS_IS_POR_RST()) { reset_reason_cast = RESET_REASON_POWER_ON; reset_reason_count ++; @@ -47,7 +55,8 @@ reset_reason_t hal_reset_reason_get(void) reset_reason_count ++; } - if (SYS_IS_WDT_RST()) { + /* Get around h/w limit with WDT reset from PD */ + if (SYS_IS_WDT_RST() || (CLK->PMUSTS & CLK_PMUSTS_TMRWK_Msk)) { reset_reason_cast = RESET_REASON_WATCHDOG; reset_reason_count ++; } @@ -101,6 +110,15 @@ uint32_t hal_reset_reason_get_raw(void) void hal_reset_reason_clear(void) { SYS_CLEAR_RST_SOURCE(SYS->RSTSTS); + + /* Re-unlock protected clock setting */ + SYS_UnlockReg(); + + /* Get around h/w limit with WDT reset from PD */ + CLK->PMUSTS |= (CLK_PMUSTS_CLRWK_Msk | CLK_PMUSTS_TMRWK_Msk); + + /* Lock protected registers */ + SYS_LockReg(); } #endif