From f5c5d777441e72b8c9cfc9f47b5fd0c7243bd458 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Sat, 9 Oct 2021 10:24:14 +0800 Subject: [PATCH] risc-v/esp32c3: Add hardware brownout check and reset --- arch/risc-v/src/esp32c3/Kconfig | 52 +++++++++++++++ arch/risc-v/src/esp32c3/Make.defs | 4 ++ arch/risc-v/src/esp32c3/esp32c3_brownout.c | 66 +++++++++++++++++++ arch/risc-v/src/esp32c3/esp32c3_brownout.h | 65 ++++++++++++++++++ arch/risc-v/src/esp32c3/esp32c3_start.c | 10 +++ .../src/esp32c3/hardware/regi2c_brownout.h | 31 +++++++++ 6 files changed, 228 insertions(+) create mode 100644 arch/risc-v/src/esp32c3/esp32c3_brownout.c create mode 100644 arch/risc-v/src/esp32c3/esp32c3_brownout.h create mode 100644 arch/risc-v/src/esp32c3/hardware/regi2c_brownout.h diff --git a/arch/risc-v/src/esp32c3/Kconfig b/arch/risc-v/src/esp32c3/Kconfig index 6d4bfb26ca..a620ed3d75 100644 --- a/arch/risc-v/src/esp32c3/Kconfig +++ b/arch/risc-v/src/esp32c3/Kconfig @@ -357,6 +357,14 @@ config ESP32C3_BLE ---help--- Enable BLE support +config ESP32C3_BROWNOUT_DET + bool "Brownout Detect" + default y + ---help--- + The ESP32-C3 has a built-in brownout detector which can detect if the voltage is lower than + a specific value. If this happens, it will reset the chip in order to prevent unintended + behaviour. + endmenu # ESP32-C3 Peripheral Support menuconfig ESP32C3_WIFI_BT_COEXIST @@ -1065,4 +1073,48 @@ config ESP32C3_PARTITION_TABLE_OFFSET endmenu # Application Image Configuration +menu "Brownout Detect Configuration" + depends on ESP32C3_BROWNOUT_DET + +choice ESP32C3_BROWNOUT_DET_LVL_SEL + prompt "Brownout voltage level" + default ESP32C3_BROWNOUT_DET_LVL_SEL_7 + ---help--- + The brownout detector will reset the chip when the supply voltage is approximately + below this level. Note that there may be some variation of brownout voltage level + between each chip. + + #The voltage levels here are estimates, more work needs to be done to figure out the exact voltages + #of the brownout threshold levels. + + config ESP32C3_BROWNOUT_DET_LVL_SEL_7 + bool "2.51V" + + config ESP32C3_BROWNOUT_DET_LVL_SEL_6 + bool "2.64V" + + config ESP32C3_BROWNOUT_DET_LVL_SEL_5 + bool "2.76V" + + config ESP32C3_BROWNOUT_DET_LVL_SEL_4 + bool "2.92V" + + config ESP32C3_BROWNOUT_DET_LVL_SEL_3 + bool "3.10V" + + config ESP32C3_BROWNOUT_DET_LVL_SEL_2 + bool "3.27V" +endchoice + +config ESP32C3_BROWNOUT_DET_LVL + int + default 2 if ESP32C3_BROWNOUT_DET_LVL_SEL_2 + default 3 if ESP32C3_BROWNOUT_DET_LVL_SEL_3 + default 4 if ESP32C3_BROWNOUT_DET_LVL_SEL_4 + default 5 if ESP32C3_BROWNOUT_DET_LVL_SEL_5 + default 6 if ESP32C3_BROWNOUT_DET_LVL_SEL_6 + default 7 if ESP32C3_BROWNOUT_DET_LVL_SEL_7 + +endmenu # ESP32C3_BROWNOUT_DET + endif # ARCH_CHIP_ESP32C3 diff --git a/arch/risc-v/src/esp32c3/Make.defs b/arch/risc-v/src/esp32c3/Make.defs index a1db77dcb3..085e48676d 100644 --- a/arch/risc-v/src/esp32c3/Make.defs +++ b/arch/risc-v/src/esp32c3/Make.defs @@ -172,6 +172,10 @@ ifeq ($(CONFIG_ESP32C3_RTC_HEAP),y) CHIP_CSRCS += esp32c3_rtcheap.c endif +ifeq ($(CONFIG_ESP32C3_BROWNOUT_DET),y) +CHIP_CSRCS += esp32c3_brownout.c +endif + ifeq ($(CONFIG_ESP32C3_WIRELESS),y) WIRELESS_DRV_UNPACK = esp-wireless-drivers-3rdparty WIRELESS_DRV_ID = 055f1ef diff --git a/arch/risc-v/src/esp32c3/esp32c3_brownout.c b/arch/risc-v/src/esp32c3/esp32c3_brownout.c new file mode 100644 index 0000000000..b0dcf9d10c --- /dev/null +++ b/arch/risc-v/src/esp32c3/esp32c3_brownout.c @@ -0,0 +1,66 @@ +/**************************************************************************** + * arch/risc-v/src/esp32c3/esp32c3_brownout.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 + +#include "esp32c3.h" +#include "hardware/esp32c3_rtccntl.h" +#include "hardware/regi2c_ctrl.h" +#include "hardware/regi2c_brownout.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ESP32C3_BROWNOUT_VAL CONFIG_ESP32C3_BROWNOUT_DET_LVL +#define ESP32C3_BROWNOUT_RST_SEL 1 +#define ESP32C3_BROWNOUT_RST_WAIT 0x3ff + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32c3_brownout_init + * + * Description: + * Initialize hardware brownout check and reset. + * + ****************************************************************************/ + +void esp32c3_brownout_init(void) +{ + uint32_t regval; + + REGI2C_WRITE_MASK(I2C_BOD, I2C_BOD_THRESHOLD, ESP32C3_BROWNOUT_VAL); + + regval = RTC_CNTL_BROWN_OUT_ENA | + (ESP32C3_BROWNOUT_RST_SEL << RTC_CNTL_BROWN_OUT_RST_SEL_S) | + RTC_CNTL_BROWN_OUT_RST_ENA | + (ESP32C3_BROWNOUT_RST_WAIT << RTC_CNTL_BROWN_OUT_RST_WAIT_S) | + RTC_CNTL_BROWN_OUT_PD_RF_ENA | + RTC_CNTL_BROWN_OUT_CLOSE_FLASH_ENA; + + putreg32(regval, RTC_CNTL_BROWN_OUT_REG); +} diff --git a/arch/risc-v/src/esp32c3/esp32c3_brownout.h b/arch/risc-v/src/esp32c3/esp32c3_brownout.h new file mode 100644 index 0000000000..1afb757247 --- /dev/null +++ b/arch/risc-v/src/esp32c3/esp32c3_brownout.h @@ -0,0 +1,65 @@ +/**************************************************************************** + * arch/risc-v/src/esp32c3/esp32c3_brownout.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_RISCV_SRC_ESP32C3_ESP32C3_BROWNOUT_H +#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_BROWNOUT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32c3_brownout_init + * + * Description: + * Initialize hardware brownout check and reset. + * + ****************************************************************************/ + +void esp32c3_brownout_init(void); + +#if defined(__cplusplus) +} +#endif +#undef EXTERN + +#endif /* __ASSEMBLY__ */ + +#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP32C3_BROWNOUT_H */ diff --git a/arch/risc-v/src/esp32c3/esp32c3_start.c b/arch/risc-v/src/esp32c3/esp32c3_start.c index 07e7016c91..af5f84d252 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_start.c +++ b/arch/risc-v/src/esp32c3/esp32c3_start.c @@ -38,6 +38,10 @@ #include "hardware/esp32c3_cache_memory.h" #include "hardware/extmem_reg.h" +#ifdef CONFIG_ESP32C3_BROWNOUT_DET +# include "esp32c3_brownout.h" +#endif + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -240,6 +244,12 @@ void __esp32c3_start(void) esp32c3_clockconfig(); +#ifdef CONFIG_ESP32C3_BROWNOUT_DET + /* Initialize hardware brownout check and reset */ + + esp32c3_brownout_init(); +#endif + /* Configure the UART so we can get debug output */ esp32c3_lowsetup(); diff --git a/arch/risc-v/src/esp32c3/hardware/regi2c_brownout.h b/arch/risc-v/src/esp32c3/hardware/regi2c_brownout.h new file mode 100644 index 0000000000..fef4feb0b8 --- /dev/null +++ b/arch/risc-v/src/esp32c3/hardware/regi2c_brownout.h @@ -0,0 +1,31 @@ +/**************************************************************************** + * arch/risc-v/src/esp32c3/hardware/regi2c_brownout.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_RISCV_SRC_ESP32C3_HARDWARE_REGI2C_BROWNOUT_H_ +#define __ARCH_RISCV_SRC_ESP32C3_HARDWARE_REGI2C_BROWNOUT_H_ + +#define I2C_BOD 0x61 +#define I2C_BOD_HOSTID 0 + +#define I2C_BOD_THRESHOLD 0x5 +#define I2C_BOD_THRESHOLD_MSB 2 +#define I2C_BOD_THRESHOLD_LSB 0 + +#endif /* __ARCH_RISCV_SRC_ESP32C3_HARDWARE_REGI2C_BROWNOUT_H_ */