Skip to content

Commit

Permalink
Merge branch 'refactor/move_bod_to_hw_support' into 'master'
Browse files Browse the repository at this point in the history
refactor(bod): Move brownout handling file from esp_system to esp_hw_support

See merge request espressif/esp-idf!36191
  • Loading branch information
mythbuster5 committed Jan 9, 2025
2 parents e73f27f + 5e4fd8e commit 50cd05c
Show file tree
Hide file tree
Showing 48 changed files with 433 additions and 185 deletions.
7 changes: 6 additions & 1 deletion components/esp_hw_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ if(NOT non_os_build)
if(CONFIG_SOC_GPIO_CLOCKOUT_BY_GPIO_MATRIX OR CONFIG_SOC_GPIO_CLOCKOUT_BY_IO_MUX)
list(APPEND srcs "esp_clock_output.c")
endif()

if(CONFIG_SOC_BOD_SUPPORTED)
list(APPEND srcs "power_supply/brownout.c")
endif()

else()
if(ESP_TEE_BUILD)
list(APPEND srcs "esp_clk.c" "hw_random.c")
Expand All @@ -156,7 +161,7 @@ endif()

set(public_include_dirs "include" "include/soc" "include/soc/${target}"
"dma/include" "ldo/include" "debug_probe/include"
"mspi_timing_tuning/include")
"mspi_timing_tuning/include" "power_supply/include")

if(CONFIG_IDF_TARGET_ESP32H21)
list(REMOVE_ITEM srcs
Expand Down
2 changes: 2 additions & 0 deletions components/esp_hw_support/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ menu "Hardware Settings"

orsource "./port/$IDF_TARGET/Kconfig.ldo"

orsource "./power_supply/port/$IDF_TARGET/Kconfig.bod"

# Invisible bringup bypass options for esp_hw_support component
config ESP_BRINGUP_BYPASS_CPU_CLK_SETTING
bool
Expand Down
2 changes: 1 addition & 1 deletion components/esp_hw_support/port/esp32/Kconfig.hw_support
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ choice ESP32_REV_MIN
config ESP32_REV_MIN_0
bool "Rev v0.0 (ECO0)"
# Brownout on Rev 0 is bugged, must use interrupt
select ESP_SYSTEM_BROWNOUT_INTR
select ESP_BROWNOUT_USE_INTR
config ESP32_REV_MIN_1
bool "Rev v1.0 (ECO1)"
config ESP32_REV_MIN_1_1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -25,6 +25,7 @@
#include "sdkconfig.h"
#include "esp_rom_uart.h"
#include "hal/uart_ll.h"
#include "soc/power_supply_periph.h"

#if defined(CONFIG_ESP_BROWNOUT_DET_LVL)
#define BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL
Expand All @@ -34,7 +35,7 @@

static __attribute__((unused)) DRAM_ATTR const char TAG[] = "BOD";

#if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
#if CONFIG_ESP_BROWNOUT_USE_INTR
IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
{
/* Normally RTC ISR clears the interrupt flag after the application-supplied
Expand Down Expand Up @@ -71,11 +72,11 @@ IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)

ESP_INFINITE_LOOP();
}
#endif // CONFIG_ESP_SYSTEM_BROWNOUT_INTR
#endif // CONFIG_ESP_BROWNOUT_USE_INTR

void esp_brownout_init(void)
{
#if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
#if CONFIG_ESP_BROWNOUT_USE_INTR
brownout_hal_config_t cfg = {
.threshold = BROWNOUT_DET_LVL,
.enabled = true,
Expand All @@ -87,15 +88,12 @@ void esp_brownout_init(void)
brownout_hal_config(&cfg);
brownout_ll_intr_clear();

#if SOC_LP_TIMER_BOD_SHARE_INTR_SOURCE
// TODO IDF-6606: LP_RTC_TIMER interrupt source is shared by lp_timer and brownout detector, but lp_timer interrupt
// is not used now. An interrupt allocator is needed when lp_timer intr gets supported.
esp_intr_alloc_intrstatus(ETS_LP_RTC_TIMER_INTR_SOURCE, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_SHARED, (uint32_t)brownout_ll_intr_get_status_reg(), BROWNOUT_DETECTOR_LL_INTERRUPT_MASK, &rtc_brownout_isr_handler, NULL, NULL);
#elif CONFIG_IDF_TARGET_ESP32P4
esp_intr_alloc(ETS_LP_ANAPERI_INTR_SOURCE, ESP_INTR_FLAG_IRAM, &rtc_brownout_isr_handler, NULL, NULL);
#else
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M, RTC_INTR_FLAG_IRAM);
#else
esp_intr_alloc_intrstatus(power_supply_periph_signal.irq, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_SHARED, (uint32_t)brownout_ll_intr_get_status_reg(), BROWNOUT_DETECTOR_LL_INTERRUPT_MASK, &rtc_brownout_isr_handler, NULL, NULL);
#endif

brownout_ll_intr_enable(true);

#else // brownout without interrupt
Expand All @@ -119,8 +117,8 @@ void esp_brownout_disable(void)
};

brownout_hal_config(&cfg);
#if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
#if CONFIG_ESP_BROWNOUT_USE_INTR
brownout_ll_intr_enable(false);
rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
#endif // CONFIG_ESP_SYSTEM_BROWNOUT_INTR
#endif // CONFIG_ESP_BROWNOUT_USE_INTR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Initialize the brownout detection system
*
* This function configures and enables the brownout detection hardware, which monitors
* the power supply voltage and triggers appropriate system actions if the voltage
* drops below a predefined threshold. Brownout detection helps ensure the stability
* and reliability of the system under low-voltage conditions.
*/
void esp_brownout_init(void);

/**
* @brief Disable the brownout detection system
*
* This function disables the brownout detection hardware, stopping voltage monitoring
* and associated system actions. Use this function with caution, as disabling brownout
* detection may increase the risk of system instability or malfunction under low-voltage
* conditions.
*/
void esp_brownout_disable(void);

#ifdef __cplusplus
}
#endif
64 changes: 64 additions & 0 deletions components/esp_hw_support/power_supply/port/esp32/Kconfig.bod
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
menu "Brownout Detector"

config ESP_BROWNOUT_DET
bool "Hardware brownout detect & reset"
depends on !IDF_ENV_FPGA
default y
help
The ESP 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 ESP_BROWNOUT_DET_LVL_SEL
prompt "Brownout voltage level"
depends on ESP_BROWNOUT_DET
default ESP_BROWNOUT_DET_LVL_SEL_0
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 ESP 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 ESP_BROWNOUT_DET_LVL_SEL_0
bool "2.43V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_1
bool "2.48V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_2
bool "2.58V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_3
bool "2.62V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_4
bool "2.67V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_5
bool "2.70V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_6
bool "2.77V +/- 0.05"
config ESP_BROWNOUT_DET_LVL_SEL_7
bool "2.80V +/- 0.05"
endchoice

config ESP_BROWNOUT_DET_LVL
int
default 0 if ESP_BROWNOUT_DET_LVL_SEL_0
default 1 if ESP_BROWNOUT_DET_LVL_SEL_1
default 2 if ESP_BROWNOUT_DET_LVL_SEL_2
default 3 if ESP_BROWNOUT_DET_LVL_SEL_3
default 4 if ESP_BROWNOUT_DET_LVL_SEL_4
default 5 if ESP_BROWNOUT_DET_LVL_SEL_5
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
Two occasions need to restart the chip with interrupt so far.
(1). For ESP32 version 1, brown-out reset function doesn't work (see ESP32 errata 3.4).
So that we must restart from interrupt.
(2). For special workflow, the chip needs do more things instead of restarting directly. This part
needs to be done in callback function of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ menu "Brownout Detector"
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
59 changes: 59 additions & 0 deletions components/esp_hw_support/power_supply/port/esp32s2/Kconfig.bod
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
menu "Brownout Detector"

config ESP_BROWNOUT_DET
bool "Hardware brownout detect & reset"
depends on !IDF_ENV_FPGA
default y
help
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 ESP_BROWNOUT_DET_LVL_SEL
prompt "Brownout voltage level"
depends on ESP_BROWNOUT_DET
default ESP_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 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 ESP_BROWNOUT_DET_LVL_SEL_7
bool "2.44V"
config ESP_BROWNOUT_DET_LVL_SEL_6
bool "2.56V"
config ESP_BROWNOUT_DET_LVL_SEL_5
bool "2.67V"
config ESP_BROWNOUT_DET_LVL_SEL_4
bool "2.84V"
config ESP_BROWNOUT_DET_LVL_SEL_3
bool "2.98V"
config ESP_BROWNOUT_DET_LVL_SEL_2
bool "3.19V"
config ESP_BROWNOUT_DET_LVL_SEL_1
bool "3.30V"
endchoice

config ESP_BROWNOUT_DET_LVL
int
default 1 if ESP_BROWNOUT_DET_LVL_SEL_1
default 2 if ESP_BROWNOUT_DET_LVL_SEL_2
default 3 if ESP_BROWNOUT_DET_LVL_SEL_3
default 4 if ESP_BROWNOUT_DET_LVL_SEL_4
default 5 if ESP_BROWNOUT_DET_LVL_SEL_5
default 6 if ESP_BROWNOUT_DET_LVL_SEL_6
default 7 if ESP_BROWNOUT_DET_LVL_SEL_7

config ESP_BROWNOUT_USE_INTR
bool
default n
help
This config allows to trigger an interrupt when brownout detected. Software restart will be done
at the end of the default callback.
This is because for some special workflow, the chip needs do more things when brownout happens
before restart instead of restarting directly. This part needs to be done in callback function
of interrupt.

endmenu
Loading

0 comments on commit 50cd05c

Please sign in to comment.