Skip to content

Commit 3f5cab4

Browse files
committed
feat(bod): Add a simple callback function for brownout detector,
Closes #15661
1 parent 8a9d659 commit 3f5cab4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

components/esp_hw_support/power_supply/brownout.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include "esp_rom_uart.h"
2727
#include "hal/uart_ll.h"
2828
#include "soc/power_supply_periph.h"
29+
#include "esp_brownout.h"
30+
#include "esp_check.h"
31+
#include "esp_memory_utils.h"
2932

3033
#if defined(CONFIG_ESP_BROWNOUT_DET_LVL)
3134
#define BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL
@@ -35,6 +38,8 @@
3538

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

41+
static brownout_callback_t s_brownout_callback = NULL;
42+
3843
#if CONFIG_ESP_BROWNOUT_USE_INTR
3944
IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
4045
{
@@ -44,6 +49,10 @@ IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
4449
*/
4550
brownout_ll_intr_clear();
4651

52+
if (s_brownout_callback) {
53+
s_brownout_callback();
54+
}
55+
4756
// Stop the other core.
4857
#if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
4958
const uint32_t core_id = esp_cpu_get_core_id();
@@ -123,3 +132,13 @@ void esp_brownout_disable(void)
123132
rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
124133
#endif // CONFIG_ESP_BROWNOUT_USE_INTR
125134
}
135+
136+
esp_err_t esp_brownout_register_callback(brownout_callback_t callback)
137+
{
138+
if (callback != NULL) {
139+
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(callback), ESP_ERR_INVALID_ARG, TAG, "brownout callback is not in IRAM");
140+
}
141+
s_brownout_callback = callback;
142+
143+
return ESP_OK;
144+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include "esp_err.h"
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/**
16+
* @brief Type definition for a brownout callback function.
17+
*
18+
* This type defines a callback function that will be invoked when a brownout
19+
* condition is detected.
20+
*
21+
* @note The callback function is executed in an brownout interrupt context, so it
22+
* must be designed to execute quickly and must not call blocking functions.
23+
*/
24+
typedef void (*brownout_callback_t)(void);
25+
26+
/**
27+
* @brief Register a callback to be called during brownout interrupt.
28+
*
29+
* @note The callback in brownout must be put in IRAM.
30+
*
31+
* @param callback The callback function to register. Set NULL to unregister the callback.
32+
*/
33+
esp_err_t esp_brownout_register_callback(brownout_callback_t callback);
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif

0 commit comments

Comments
 (0)