From 400d927011202b013d3d412f3ffb976e70e0dffa Mon Sep 17 00:00:00 2001 From: Sara Souza Date: Fri, 16 Jul 2021 17:42:00 -0300 Subject: [PATCH] xtensa/esp32s2: Disable wdt and wrap it. --- arch/xtensa/src/esp32s2/Make.defs | 2 +- arch/xtensa/src/esp32s2/esp32s2_start.c | 7 ++- arch/xtensa/src/esp32s2/esp32s2_wdt.c | 50 +++++++++++++++++++ arch/xtensa/src/esp32s2/esp32s2_wdt.h | 34 +++++++++++++ .../src/esp32s2/hardware/esp32s2_rtccntl.h | 6 +++ 5 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 arch/xtensa/src/esp32s2/esp32s2_wdt.c create mode 100644 arch/xtensa/src/esp32s2/esp32s2_wdt.h diff --git a/arch/xtensa/src/esp32s2/Make.defs b/arch/xtensa/src/esp32s2/Make.defs index a5b1140659..824e10fac1 100644 --- a/arch/xtensa/src/esp32s2/Make.defs +++ b/arch/xtensa/src/esp32s2/Make.defs @@ -22,7 +22,7 @@ HEAD_ASRC = xtensa_vectors.S xtensa_window_vector.S xtensa_windowspill.S HEAD_ASRC += xtensa_int_handlers.S xtensa_user_handler.S -HEAD_CSRC = esp32s2_start.c +HEAD_CSRC = esp32s2_start.c esp32s2_wdt.c # Common XTENSA files (arch/xtensa/src/common) diff --git a/arch/xtensa/src/esp32s2/esp32s2_start.c b/arch/xtensa/src/esp32s2/esp32s2_start.c index 76f73d8cd8..4eb239df47 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_start.c +++ b/arch/xtensa/src/esp32s2/esp32s2_start.c @@ -38,6 +38,7 @@ #include "esp32s2_region.h" #include "esp32s2_start.h" #include "esp32s2_lowputc.h" +#include "esp32s2_wdt.h" /**************************************************************************** * Pre-processor Definitions @@ -78,11 +79,9 @@ void IRAM_ATTR __start(void) uint32_t regval; uint32_t sp; - /* Kill the watchdog timer */ + /* Disable any wdt enabled by bootloader */ - regval = getreg32(RTC_CNTL_WDTCONFIG0_REG); - regval &= ~RTC_CNTL_WDT_FLASHBOOT_MOD_EN; - putreg32(regval, RTC_CNTL_WDTCONFIG0_REG); + esp32s2_wdt_early_deinit(); regval = getreg32(DR_REG_BB_BASE + 0x48); /* DR_REG_BB_BASE+48 */ regval &= ~(1 << 14); diff --git a/arch/xtensa/src/esp32s2/esp32s2_wdt.c b/arch/xtensa/src/esp32s2/esp32s2_wdt.c new file mode 100644 index 0000000000..60addbd988 --- /dev/null +++ b/arch/xtensa/src/esp32s2/esp32s2_wdt.c @@ -0,0 +1,50 @@ +/**************************************************************************** + * arch/xtensa/src/esp32s2/esp32s2_wdt.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "xtensa.h" +#include "hardware/esp32s2_rtccntl.h" + +#include "esp32s2_wdt.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32s2_wdt_early_deinit + * + * Description: + * Disable the WDT(s) that was/were enabled by the bootloader. + * + ****************************************************************************/ + +void esp32s2_wdt_early_deinit(void) +{ + uint32_t regval; + putreg32(RTC_CNTL_WDT_WKEY_VALUE, RTC_CNTL_WDTWPROTECT_REG); + regval = getreg32(RTC_CNTL_WDTCONFIG0_REG); + regval &= ~RTC_CNTL_WDT_EN; + putreg32(regval, RTC_CNTL_WDTCONFIG0_REG); + putreg32(0, RTC_CNTL_WDTWPROTECT_REG); +} diff --git a/arch/xtensa/src/esp32s2/esp32s2_wdt.h b/arch/xtensa/src/esp32s2/esp32s2_wdt.h new file mode 100644 index 0000000000..bd33075b53 --- /dev/null +++ b/arch/xtensa/src/esp32s2/esp32s2_wdt.h @@ -0,0 +1,34 @@ +/**************************************************************************** + * arch/xtensa/src/esp32s2/esp32s2_wdt.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_WDT_H +#define __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_WDT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +void esp32s2_wdt_early_deinit(void); + +#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_WDT_H */ diff --git a/arch/xtensa/src/esp32s2/hardware/esp32s2_rtccntl.h b/arch/xtensa/src/esp32s2/hardware/esp32s2_rtccntl.h index 7b7f7b37c5..482beadbf5 100644 --- a/arch/xtensa/src/esp32s2/hardware/esp32s2_rtccntl.h +++ b/arch/xtensa/src/esp32s2/hardware/esp32s2_rtccntl.h @@ -31,6 +31,12 @@ * Pre-processor Definitions ****************************************************************************/ +/* The value that needs to be written to RTC_CNTL_WDT_WKEY to + * write-enable the wdt registers + */ + +#define RTC_CNTL_WDT_WKEY_VALUE 0x50d83aa1 + #define DPORT_CPUPERIOD_SEL_80 0 #define DPORT_CPUPERIOD_SEL_160 1 #define DPORT_CPUPERIOD_SEL_240 2