1 /*
2 * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include "esp_private/system_internal.h"
12 #include "esp_private/rtc_ctrl.h"
13 #include "esp_private/spi_flash_os.h"
14 #include "esp_log.h"
15 #include "esp_cpu.h"
16 #include "soc/soc.h"
17 #include "soc/rtc_periph.h"
18 #include "esp_attr.h"
19 #include "esp_rom_sys.h"
20 #include "bootloader_flash.h"
21 #include "esp_intr_alloc.h"
22 #include "hal/brownout_hal.h"
23 #include "hal/brownout_ll.h"
24 #include "sdkconfig.h"
25
26 #if defined(CONFIG_ESP_BROWNOUT_DET_LVL)
27 #define BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL
28 #else
29 #define BROWNOUT_DET_LVL 0
30 #endif
31
32 static __attribute__((unused)) DRAM_ATTR const char TAG[] = "BOD";
33
34 #if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
rtc_brownout_isr_handler(void * arg)35 IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
36 {
37 /* Normally RTC ISR clears the interrupt flag after the application-supplied
38 * handler returns. Since restart is called here, the flag needs to be
39 * cleared manually.
40 */
41 brownout_ll_intr_clear();
42
43 // Stop the other core.
44 #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
45 const uint32_t core_id = esp_cpu_get_core_id();
46 const uint32_t other_core_id = (core_id == 0) ? 1 : 0;
47 esp_cpu_stall(other_core_id);
48 #endif
49
50 esp_reset_reason_set_hint(ESP_RST_BROWNOUT);
51 #if CONFIG_SPI_FLASH_BROWNOUT_RESET
52 if (spi_flash_brownout_need_reset()) {
53 bootloader_flash_reset_chip();
54 } else
55 #endif // CONFIG_SPI_FLASH_BROWNOUT_RESET
56 {
57 ESP_DRAM_LOGI(TAG, "Brownout detector was triggered\r\n\r\n");
58 }
59
60 esp_rom_software_reset_system();
61 while (true) {
62 ;
63 }
64 }
65 #endif // CONFIG_ESP_SYSTEM_BROWNOUT_INTR
66
esp_brownout_init(void)67 void esp_brownout_init(void)
68 {
69 #if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
70 brownout_hal_config_t cfg = {
71 .threshold = BROWNOUT_DET_LVL,
72 .enabled = true,
73 .reset_enabled = false,
74 .flash_power_down = true,
75 .rf_power_down = true,
76 };
77
78 brownout_hal_config(&cfg);
79 brownout_ll_intr_clear();
80 #if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2
81 // TODO IDF-6606: LP_RTC_TIMER interrupt source is shared by lp_timer and brownout detector, but lp_timer interrupt
82 // is not used now. An interrupt allocator is needed when lp_timer intr gets supported.
83 esp_intr_alloc(ETS_LP_RTC_TIMER_INTR_SOURCE, ESP_INTR_FLAG_IRAM, &rtc_brownout_isr_handler, NULL, NULL);
84 #else
85 rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M, RTC_INTR_FLAG_IRAM);
86 #endif
87 brownout_ll_intr_enable(true);
88
89 #else // brownout without interrupt
90
91 brownout_hal_config_t cfg = {
92 .threshold = BROWNOUT_DET_LVL,
93 .enabled = true,
94 .reset_enabled = true,
95 .flash_power_down = true,
96 .rf_power_down = true,
97 };
98
99 brownout_hal_config(&cfg);
100 #endif
101 }
102
esp_brownout_disable(void)103 void esp_brownout_disable(void)
104 {
105 brownout_hal_config_t cfg = {
106 .enabled = false,
107 };
108
109 brownout_hal_config(&cfg);
110 #if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
111 brownout_ll_intr_enable(false);
112 rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
113 #endif // CONFIG_ESP_SYSTEM_BROWNOUT_INTR
114 }
115