Merge branch 'feat/add_cbs_bod' into 'master'

feat(bod): Add a simple callback function for brownout detector

Closes IDFGH-14954

See merge request espressif/esp-idf!38432
This commit is contained in:
C.S.M
2025-04-18 11:34:48 +08:00
2 changed files with 56 additions and 0 deletions

View File

@@ -26,6 +26,9 @@
#include "esp_rom_uart.h"
#include "hal/uart_ll.h"
#include "soc/power_supply_periph.h"
#include "esp_brownout.h"
#include "esp_check.h"
#include "esp_memory_utils.h"
#if defined(CONFIG_ESP_BROWNOUT_DET_LVL)
#define BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL
@@ -35,6 +38,8 @@
static __attribute__((unused)) DRAM_ATTR const char TAG[] = "BOD";
static brownout_callback_t s_brownout_callback = NULL;
#if CONFIG_ESP_BROWNOUT_USE_INTR
IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
{
@@ -44,6 +49,10 @@ IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
*/
brownout_ll_intr_clear();
if (s_brownout_callback) {
s_brownout_callback();
}
// Stop the other core.
#if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
const uint32_t core_id = esp_cpu_get_core_id();
@@ -123,3 +132,13 @@ void esp_brownout_disable(void)
rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
#endif // CONFIG_ESP_BROWNOUT_USE_INTR
}
esp_err_t esp_brownout_register_callback(brownout_callback_t callback)
{
if (callback != NULL) {
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(callback), ESP_ERR_INVALID_ARG, TAG, "brownout callback is not in IRAM");
}
s_brownout_callback = callback;
return ESP_OK;
}

View File

@@ -0,0 +1,37 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Type definition for a brownout callback function.
*
* This type defines a callback function that will be invoked when a brownout
* condition is detected.
*
* @note The callback function is executed in an brownout interrupt context, so it
* must be designed to execute quickly and must not call blocking functions.
*/
typedef void (*brownout_callback_t)(void);
/**
* @brief Register a callback to be called during brownout interrupt.
*
* @note The callback in brownout must be put in IRAM.
*
* @param callback The callback function to register. Set NULL to unregister the callback.
*/
esp_err_t esp_brownout_register_callback(brownout_callback_t callback);
#ifdef __cplusplus
}
#endif