From 46035032cfb7558071b1fb2d9215cd3a02a7af45 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 21 Jan 2020 16:33:57 +0100 Subject: [PATCH 1/3] esp32s2: fix inconsistency between reg and struct headers test_mux register doesn't exist in RTCCNTL anymore, remove it from struct header. Also remove adc_ll_vref_output implementation, which depends on that register. --- components/soc/esp32s2/include/hal/adc_ll.h | 23 +------------------ .../soc/esp32s2/include/soc/rtc_cntl_struct.h | 15 ++++-------- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/components/soc/esp32s2/include/hal/adc_ll.h b/components/soc/esp32s2/include/hal/adc_ll.h index c15b52d791..febbbf195f 100644 --- a/components/soc/esp32s2/include/hal/adc_ll.h +++ b/components/soc/esp32s2/include/hal/adc_ll.h @@ -586,26 +586,5 @@ static inline void adc_ll_set_hall_controller(adc_ll_hall_controller_t hall_ctrl */ static inline bool adc_ll_vref_output(int io) { - int channel; - if (io == 25) { - channel = 8; //Channel 8 bit - } else if (io == 26) { - channel = 9; //Channel 9 bit - } else if (io == 27) { - channel = 7; //Channel 7 bit - } else { - return false; - } - RTCCNTL.bias_conf.dbg_atten = 0; //Check DBG effect outside sleep mode - //set dtest (MUX_SEL : 0 -> RTC; 1-> vdd_sar2) - RTCCNTL.test_mux.dtest_rtc = 1; //Config test mux to route v_ref to ADC2 Channels - //set ent - RTCCNTL.test_mux.ent_rtc = 1; - //set sar2_en_test - SENS.sar_meas2_ctrl1.sar2_en_test = 1; - //set sar2 en force - SENS.sar_meas2_ctrl2.sar2_en_pad_force = 1; //Pad bitmap controlled by SW - //set en_pad for channels 7,8,9 (bits 0x380) - SENS.sar_meas2_ctrl2.sar2_en_pad = 1 << channel; - return true; + return false; } diff --git a/components/soc/esp32s2/include/soc/rtc_cntl_struct.h b/components/soc/esp32s2/include/soc/rtc_cntl_struct.h index b9fa2b7cba..3e10ea66a3 100644 --- a/components/soc/esp32s2/include/soc/rtc_cntl_struct.h +++ b/components/soc/esp32s2/include/soc/rtc_cntl_struct.h @@ -13,6 +13,9 @@ // limitations under the License. #ifndef _SOC_RTC_CNTL_STRUCT_H_ #define _SOC_RTC_CNTL_STRUCT_H_ + +#include + #ifdef __cplusplus extern "C" { #endif @@ -554,15 +557,6 @@ typedef volatile struct { uint32_t val; } swd_conf; uint32_t swd_wprotect; /*swd write protect*/ - union { - struct { - uint32_t reserved0: 28; - uint32_t ent_tsens: 1; /*ENT_TSENS*/ - uint32_t ent_rtc: 1; /*ENT_RTC*/ - uint32_t dtest_rtc: 2; - }; - uint32_t val; - } test_mux; union { struct { uint32_t reserved0: 20; @@ -656,7 +650,8 @@ typedef volatile struct { } ext_wakeup1_status; union { struct { - uint32_t reserved0: 4; + uint32_t out2_ena: 1; /*enable brown_out2 to start chip reset*/ + uint32_t reserved1: 3; uint32_t int_wait: 10; /*brown out interrupt wait cycles*/ uint32_t close_flash_ena: 1; /*enable close flash when brown out happens*/ uint32_t pd_rf_ena: 1; /*enable power down RF when brown out happens*/ From 70752baba45a4de1f5ca3c506ab7071abd603c98 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 21 Jan 2020 16:32:28 +0100 Subject: [PATCH 2/3] esp32s2: add brownout detector support 1. add brownout detector HAL for esp32 and esp32s2 2. enable brownout reset for esp32 rev. 1 and above 3. add approximate brownout detector levels for esp32s2 --- components/esp32/brownout.c | 18 ++++--- components/esp32s2/Kconfig | 35 +++++++------- components/esp32s2/brownout.c | 18 +++++-- components/soc/esp32/brownout_hal.c | 41 ++++++++++++++++ .../soc/esp32/include/soc/brownout_caps.h | 27 +++++++++++ components/soc/esp32/sources.cmake | 1 + components/soc/esp32s2/brownout_hal.c | 46 ++++++++++++++++++ components/soc/esp32s2/i2c_brownout.h | 30 ++++++++++++ .../soc/esp32s2/include/soc/brownout_caps.h | 25 ++++++++++ components/soc/esp32s2/sources.cmake | 1 + components/soc/include/hal/brownout_hal.h | 47 +++++++++++++++++++ 11 files changed, 259 insertions(+), 30 deletions(-) create mode 100644 components/soc/esp32/brownout_hal.c create mode 100644 components/soc/esp32/include/soc/brownout_caps.h create mode 100644 components/soc/esp32s2/brownout_hal.c create mode 100644 components/soc/esp32s2/i2c_brownout.h create mode 100644 components/soc/esp32s2/include/soc/brownout_caps.h create mode 100644 components/soc/include/hal/brownout_hal.h diff --git a/components/esp32/brownout.c b/components/esp32/brownout.c index c3db402717..4dbfc83456 100644 --- a/components/esp32/brownout.c +++ b/components/esp32/brownout.c @@ -20,6 +20,7 @@ #include "soc/soc.h" #include "soc/cpu.h" #include "soc/rtc_periph.h" +#include "hal/brownout_hal.h" #include "esp32/rom/ets_sys.h" #include "esp_private/system_internal.h" #include "driver/rtc_cntl.h" @@ -49,14 +50,17 @@ static void rtc_brownout_isr_handler(void *arg) void esp_brownout_init(void) { - REG_WRITE(RTC_CNTL_BROWN_OUT_REG, - RTC_CNTL_BROWN_OUT_ENA /* Enable BOD */ - | RTC_CNTL_BROWN_OUT_PD_RF_ENA /* Automatically power down RF */ - /* Reset timeout must be set to >1 even if BOR feature is not used */ - | (2 << RTC_CNTL_BROWN_OUT_RST_WAIT_S) - | (BROWNOUT_DET_LVL << RTC_CNTL_DBROWN_OUT_THRES_S)); + brownout_hal_config_t cfg = { + .threshold = BROWNOUT_DET_LVL, + .enabled = true, + .reset_enabled = false, + .flash_power_down = true, + .rf_power_down = true, + }; + + brownout_hal_config(&cfg); ESP_ERROR_CHECK( rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M) ); - REG_SET_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_BROWN_OUT_INT_ENA_M); + brownout_hal_intr_enable(true); } diff --git a/components/esp32s2/Kconfig b/components/esp32s2/Kconfig index ea4fb2f0e1..ccbe51872c 100644 --- a/components/esp32s2/Kconfig +++ b/components/esp32s2/Kconfig @@ -364,42 +364,39 @@ menu "ESP32S2-specific" bool "Hardware brownout detect & reset" default y help - The ESP32S2 has a built-in brownout detector which can detect if the voltage is lower than + The ESP32-S2 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. choice ESP32S2_BROWNOUT_DET_LVL_SEL prompt "Brownout voltage level" depends on ESP32S2_BROWNOUT_DET - default ESP32S2_BROWNOUT_DET_LVL_SEL_0 + default ESP32S2_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 ESP32 chip. + between each ESP3-S2 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 ESP32S2_BROWNOUT_DET_LVL_SEL_0 - bool "2.43V +/- 0.05" - config ESP32S2_BROWNOUT_DET_LVL_SEL_1 - bool "2.48V +/- 0.05" - config ESP32S2_BROWNOUT_DET_LVL_SEL_2 - bool "2.58V +/- 0.05" - config ESP32S2_BROWNOUT_DET_LVL_SEL_3 - bool "2.62V +/- 0.05" - config ESP32S2_BROWNOUT_DET_LVL_SEL_4 - bool "2.67V +/- 0.05" - config ESP32S2_BROWNOUT_DET_LVL_SEL_5 - bool "2.70V +/- 0.05" - config ESP32S2_BROWNOUT_DET_LVL_SEL_6 - bool "2.77V +/- 0.05" config ESP32S2_BROWNOUT_DET_LVL_SEL_7 - bool "2.80V +/- 0.05" + bool "2.44V" + config ESP32S2_BROWNOUT_DET_LVL_SEL_6 + bool "2.56V" + config ESP32S2_BROWNOUT_DET_LVL_SEL_5 + bool "2.67V" + config ESP32S2_BROWNOUT_DET_LVL_SEL_4 + bool "2.84V" + config ESP32S2_BROWNOUT_DET_LVL_SEL_3 + bool "2.98V" + config ESP32S2_BROWNOUT_DET_LVL_SEL_2 + bool "3.19V" + config ESP32S2_BROWNOUT_DET_LVL_SEL_1 + bool "3.30V" endchoice config ESP32S2_BROWNOUT_DET_LVL int - default 0 if ESP32S2_BROWNOUT_DET_LVL_SEL_0 default 1 if ESP32S2_BROWNOUT_DET_LVL_SEL_1 default 2 if ESP32S2_BROWNOUT_DET_LVL_SEL_2 default 3 if ESP32S2_BROWNOUT_DET_LVL_SEL_3 diff --git a/components/esp32s2/brownout.c b/components/esp32s2/brownout.c index 986ed73077..e4eef7cb17 100644 --- a/components/esp32s2/brownout.c +++ b/components/esp32s2/brownout.c @@ -17,10 +17,11 @@ #include #include #include "sdkconfig.h" +#include "esp_log.h" #include "soc/soc.h" #include "soc/cpu.h" -#include "soc/rtc_cntl_reg.h" -#include "esp32s2/rom/ets_sys.h" +#include "soc/rtc_periph.h" +#include "hal/brownout_hal.h" #include "esp_private/system_internal.h" #include "driver/rtc_cntl.h" #include "freertos/FreeRTOS.h" @@ -42,15 +43,24 @@ static void rtc_brownout_isr_handler(void *arg) * at the same time as the following ets_printf. */ esp_cpu_stall(!xPortGetCoreID()); + esp_reset_reason_set_hint(ESP_RST_BROWNOUT); ets_printf("\r\nBrownout detector was triggered\r\n\r\n"); esp_restart_noos(); } void esp_brownout_init(void) { -// TODO: implement brownout threshold configuration for esp32s2 - IDF-751 + brownout_hal_config_t cfg = { + .threshold = BROWNOUT_DET_LVL, + .enabled = true, + .reset_enabled = false, + .flash_power_down = true, + .rf_power_down = true, + }; + + brownout_hal_config(&cfg); ESP_ERROR_CHECK( rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M) ); - REG_SET_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_BROWN_OUT_INT_ENA_M); + brownout_hal_intr_enable(true); } diff --git a/components/soc/esp32/brownout_hal.c b/components/soc/esp32/brownout_hal.c new file mode 100644 index 0000000000..5fc448601b --- /dev/null +++ b/components/soc/esp32/brownout_hal.c @@ -0,0 +1,41 @@ + +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed 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. + +#include "hal/brownout_hal.h" +#include "soc/rtc_cntl_struct.h" + +void brownout_hal_config(const brownout_hal_config_t *cfg) +{ + typeof(RTCCNTL.brown_out) brown_out_reg = { + .close_flash_ena = cfg->flash_power_down, + .pd_rf_ena = cfg->rf_power_down, + .rst_wait = 0x3ff, + .rst_ena = cfg->reset_enabled, + .thres = cfg->threshold, + .ena = cfg->enabled, + }; + + RTCCNTL.brown_out = brown_out_reg; +} + +void brownout_hal_intr_enable(bool enable) +{ + RTCCNTL.int_ena.rtc_brown_out = enable; +} + +void brownout_hal_intr_clear(void) +{ + RTCCNTL.int_clr.rtc_brown_out = 1; +} diff --git a/components/soc/esp32/include/soc/brownout_caps.h b/components/soc/esp32/include/soc/brownout_caps.h new file mode 100644 index 0000000000..5e867a8926 --- /dev/null +++ b/components/soc/esp32/include/soc/brownout_caps.h @@ -0,0 +1,27 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed 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. + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#if (CONFIG_ESP32_REV_MIN >= 1) +#define SOC_BROWNOUT_RESET_SUPPORTED 1 +#endif + +#ifdef __cplusplus +} +#endif diff --git a/components/soc/esp32/sources.cmake b/components/soc/esp32/sources.cmake index d32f5d9251..01a0fc69af 100644 --- a/components/soc/esp32/sources.cmake +++ b/components/soc/esp32/sources.cmake @@ -1,4 +1,5 @@ set(SOC_SRCS "adc_periph.c" + "brownout_hal.c" "dac_periph.c" "cpu_util.c" "gpio_periph.c" diff --git a/components/soc/esp32s2/brownout_hal.c b/components/soc/esp32s2/brownout_hal.c new file mode 100644 index 0000000000..74e60e9ede --- /dev/null +++ b/components/soc/esp32s2/brownout_hal.c @@ -0,0 +1,46 @@ + +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed 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. + +#include "hal/brownout_hal.h" +#include "soc/rtc_cntl_struct.h" +#include "soc/rtc_cntl_reg.h" +#include "i2c_rtc_clk.h" +#include "i2c_brownout.h" + + +void brownout_hal_config(const brownout_hal_config_t *cfg) +{ + I2C_WRITEREG_MASK_RTC(I2C_BOD, I2C_BOD_THRESHOLD, cfg->threshold); + typeof(RTCCNTL.brown_out) brown_out_reg = { + .out2_ena = 1, + .int_wait = 0x002, + .close_flash_ena = cfg->flash_power_down, + .pd_rf_ena = cfg->rf_power_down, + .rst_wait = 0x3ff, + .rst_ena = cfg->reset_enabled, + .ena = cfg->enabled, + }; + RTCCNTL.brown_out = brown_out_reg; +} + +void brownout_hal_intr_enable(bool enable) +{ + RTCCNTL.int_ena.rtc_brown_out = enable; +} + +void brownout_hal_intr_clear(void) +{ + RTCCNTL.int_clr.rtc_brown_out = 1; +} diff --git a/components/soc/esp32s2/i2c_brownout.h b/components/soc/esp32s2/i2c_brownout.h new file mode 100644 index 0000000000..5fac2c91b3 --- /dev/null +++ b/components/soc/esp32s2/i2c_brownout.h @@ -0,0 +1,30 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed 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. + +#pragma once + +/** + * @file i2c_brownout.h + * @brief Register definitions for brownout detector + * + * This file lists register fields of the brownout detector, located on an internal configuration + * bus. These definitions are used via macros defined in i2c_rtc_clk.h. + */ + +#define I2C_BOD 0x61 +#define I2C_BOD_HOSTID 1 + +#define I2C_BOD_THRESHOLD 0x5 +#define I2C_BOD_THRESHOLD_MSB 2 +#define I2C_BOD_THRESHOLD_LSB 0 diff --git a/components/soc/esp32s2/include/soc/brownout_caps.h b/components/soc/esp32s2/include/soc/brownout_caps.h new file mode 100644 index 0000000000..797d33ebb9 --- /dev/null +++ b/components/soc/esp32s2/include/soc/brownout_caps.h @@ -0,0 +1,25 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed 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. + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define SOC_BROWNOUT_RESET_SUPPORTED 1 + +#ifdef __cplusplus +} +#endif diff --git a/components/soc/esp32s2/sources.cmake b/components/soc/esp32s2/sources.cmake index 4293a18b0a..5a8711bb8b 100644 --- a/components/soc/esp32s2/sources.cmake +++ b/components/soc/esp32s2/sources.cmake @@ -1,4 +1,5 @@ set(SOC_SRCS "adc_periph.c" + "brownout_hal.c" "dac_periph.c" "cpu_util.c" "gpio_periph.c" diff --git a/components/soc/include/hal/brownout_hal.h b/components/soc/include/hal/brownout_hal.h new file mode 100644 index 0000000000..ac65506ed2 --- /dev/null +++ b/components/soc/include/hal/brownout_hal.h @@ -0,0 +1,47 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed 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. + +/******************************************************************************* + * NOTICE + * The hal is not public api, don't use in application code. + * See readme.md in soc/include/hal/readme.md + ******************************************************************************/ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +typedef struct { + uint8_t threshold; + bool enabled; + bool reset_enabled; + bool flash_power_down; + bool rf_power_down; +} brownout_hal_config_t; + +void brownout_hal_config(const brownout_hal_config_t *cfg); + +void brownout_hal_intr_enable(bool enable); + +void brownout_hal_intr_clear(void); + +#ifdef __cplusplus +} +#endif From caef7ad9f202864836a973b2e942bb4afcb578ed Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 21 Jan 2020 16:41:39 +0100 Subject: [PATCH 3/3] esp32, esp32s2beta: move brownout.c to esp_common --- components/esp32/CMakeLists.txt | 1 - components/esp32/brownout.c | 66 ------------------- components/esp32s2/CMakeLists.txt | 3 +- components/esp_common/CMakeLists.txt | 3 +- .../{esp32s2 => esp_common/src}/brownout.c | 17 +++-- components/soc/include/hal/brownout_hal.h | 1 + 6 files changed, 17 insertions(+), 74 deletions(-) delete mode 100644 components/esp32/brownout.c rename components/{esp32s2 => esp_common/src}/brownout.c (84%) diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index c66f3958c9..3f408b1ae7 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -12,7 +12,6 @@ if(BOOTLOADER_BUILD) else() # Regular app build set(srcs - "brownout.c" "cache_err_int.c" "cache_sram_mmu.c" "clk.c" diff --git a/components/esp32/brownout.c b/components/esp32/brownout.c deleted file mode 100644 index 4dbfc83456..0000000000 --- a/components/esp32/brownout.c +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD -// -// Licensed 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. - -#include -#include -#include -#include -#include "sdkconfig.h" -#include "soc/soc.h" -#include "soc/cpu.h" -#include "soc/rtc_periph.h" -#include "hal/brownout_hal.h" -#include "esp32/rom/ets_sys.h" -#include "esp_private/system_internal.h" -#include "driver/rtc_cntl.h" -#include "freertos/FreeRTOS.h" - -#ifdef CONFIG_ESP32_BROWNOUT_DET_LVL -#define BROWNOUT_DET_LVL CONFIG_ESP32_BROWNOUT_DET_LVL -#else -#define BROWNOUT_DET_LVL 0 -#endif //CONFIG_ESP32_BROWNOUT_DET_LVL - -static void rtc_brownout_isr_handler(void *arg) -{ - /* Normally RTC ISR clears the interrupt flag after the application-supplied - * handler returns. Since restart is called here, the flag needs to be - * cleared manually. - */ - REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_BROWN_OUT_INT_CLR); - /* Stall the other CPU to make sure the code running there doesn't use UART - * at the same time as the following ets_printf. - */ - esp_cpu_stall(!xPortGetCoreID()); - esp_reset_reason_set_hint(ESP_RST_BROWNOUT); - ets_printf("\r\nBrownout detector was triggered\r\n\r\n"); - esp_restart_noos(); -} - -void esp_brownout_init(void) -{ - brownout_hal_config_t cfg = { - .threshold = BROWNOUT_DET_LVL, - .enabled = true, - .reset_enabled = false, - .flash_power_down = true, - .rf_power_down = true, - }; - - brownout_hal_config(&cfg); - - ESP_ERROR_CHECK( rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M) ); - - brownout_hal_intr_enable(true); -} diff --git a/components/esp32s2/CMakeLists.txt b/components/esp32s2/CMakeLists.txt index 1f5a52ed27..45b82a7471 100644 --- a/components/esp32s2/CMakeLists.txt +++ b/components/esp32s2/CMakeLists.txt @@ -11,8 +11,7 @@ if(BOOTLOADER_BUILD) else() # Regular app build - set(srcs "brownout.c" - "cache_err_int.c" + set(srcs "cache_err_int.c" "clk.c" "cpu_start.c" "crosscore_int.c" diff --git a/components/esp_common/CMakeLists.txt b/components/esp_common/CMakeLists.txt index c2b6586437..125400e74d 100644 --- a/components/esp_common/CMakeLists.txt +++ b/components/esp_common/CMakeLists.txt @@ -6,7 +6,8 @@ if(BOOTLOADER_BUILD) set_property(TARGET ${COMPONENT_LIB} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "-Wl,--gc-sections") else() # Regular app build - set(srcs "src/dbg_stubs.c" + set(srcs "src/brownout.c" + "src/dbg_stubs.c" "src/esp_err_to_name.c" "src/esp_timer.c" "src/ets_timer_legacy.c" diff --git a/components/esp32s2/brownout.c b/components/esp_common/src/brownout.c similarity index 84% rename from components/esp32s2/brownout.c rename to components/esp_common/src/brownout.c index e4eef7cb17..57f62cefac 100644 --- a/components/esp32s2/brownout.c +++ b/components/esp_common/src/brownout.c @@ -26,11 +26,20 @@ #include "driver/rtc_cntl.h" #include "freertos/FreeRTOS.h" -#ifdef CONFIG_ESP32S2_BROWNOUT_DET_LVL +#if defined(CONFIG_ESP32_BROWNOUT_DET_LVL) +#define BROWNOUT_DET_LVL CONFIG_ESP32_BROWNOUT_DET_LVL +#elif defined(CONFIG_ESP32S2_BROWNOUT_DET_LVL) #define BROWNOUT_DET_LVL CONFIG_ESP32S2_BROWNOUT_DET_LVL #else #define BROWNOUT_DET_LVL 0 -#endif //CONFIG_ESP32S2_BROWNOUT_DET_LVL +#endif + +#ifdef SOC_BROWNOUT_RESET_SUPPORTED +#define BROWNOUT_RESET_EN true +#else +#define BROWNOUT_RESET_EN false +#endif // SOC_BROWNOUT_RESET_SUPPORTED + static void rtc_brownout_isr_handler(void *arg) { @@ -38,7 +47,7 @@ static void rtc_brownout_isr_handler(void *arg) * handler returns. Since restart is called here, the flag needs to be * cleared manually. */ - REG_WRITE(RTC_CNTL_INT_CLR_REG, RTC_CNTL_BROWN_OUT_INT_CLR); + brownout_hal_intr_clear(); /* Stall the other CPU to make sure the code running there doesn't use UART * at the same time as the following ets_printf. */ @@ -53,7 +62,7 @@ void esp_brownout_init(void) brownout_hal_config_t cfg = { .threshold = BROWNOUT_DET_LVL, .enabled = true, - .reset_enabled = false, + .reset_enabled = BROWNOUT_RESET_EN, .flash_power_down = true, .rf_power_down = true, }; diff --git a/components/soc/include/hal/brownout_hal.h b/components/soc/include/hal/brownout_hal.h index ac65506ed2..97523fcc07 100644 --- a/components/soc/include/hal/brownout_hal.h +++ b/components/soc/include/hal/brownout_hal.h @@ -27,6 +27,7 @@ extern "C" { #include #include #include +#include "soc/brownout_caps.h" typedef struct { uint8_t threshold;