1 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19
20 #include "esp_private/system_internal.h"
21 #include "driver/rtc_cntl.h"
22
23 #include "esp_rom_sys.h"
24
25 #include "soc/soc.h"
26 #include "soc/cpu.h"
27 #include "soc/rtc_periph.h"
28 #include "hal/cpu_hal.h"
29
30 #include "hal/brownout_hal.h"
31 #if !defined(__ZEPHYR__)
32 #include "sdkconfig.h"
33 #endif
34
35 #if defined(CONFIG_ESP32_BROWNOUT_DET_LVL)
36 #define BROWNOUT_DET_LVL CONFIG_ESP32_BROWNOUT_DET_LVL
37 #elif defined(CONFIG_ESP32S2_BROWNOUT_DET_LVL)
38 #define BROWNOUT_DET_LVL CONFIG_ESP32S2_BROWNOUT_DET_LVL
39 #elif defined(CONFIG_ESP32S3_BROWNOUT_DET_LVL)
40 #define BROWNOUT_DET_LVL CONFIG_ESP32S3_BROWNOUT_DET_LVL
41 #elif defined(CONFIG_ESP32C3_BROWNOUT_DET_LVL)
42 #define BROWNOUT_DET_LVL CONFIG_ESP32C3_BROWNOUT_DET_LVL
43 #elif defined(CONFIG_ESP32H2_BROWNOUT_DET_LVL)
44 #define BROWNOUT_DET_LVL CONFIG_ESP32H2_BROWNOUT_DET_LVL
45 #else
46 #define BROWNOUT_DET_LVL 0
47 #endif
48
49 #if SOC_BROWNOUT_RESET_SUPPORTED
50 #define BROWNOUT_RESET_EN true
51 #else
52 #define BROWNOUT_RESET_EN false
53 #endif // SOC_BROWNOUT_RESET_SUPPORTED
54
55 #ifndef SOC_BROWNOUT_RESET_SUPPORTED
rtc_brownout_isr_handler(void * arg)56 static void rtc_brownout_isr_handler(void *arg)
57 {
58 /* Normally RTC ISR clears the interrupt flag after the application-supplied
59 * handler returns. Since restart is called here, the flag needs to be
60 * cleared manually.
61 */
62 brownout_hal_intr_clear();
63 /* Stall the other CPU to make sure the code running there doesn't use UART
64 * at the same time as the following esp_rom_printf.
65 */
66 esp_cpu_stall(!cpu_hal_get_core_id());
67 esp_reset_reason_set_hint(ESP_RST_BROWNOUT);
68 esp_rom_printf("\r\nBrownout detector was triggered\r\n\r\n");
69 esp_restart_noos();
70 }
71 #endif // not SOC_BROWNOUT_RESET_SUPPORTED
72
esp_brownout_init(void)73 void esp_brownout_init(void)
74 {
75 brownout_hal_config_t cfg = {
76 .threshold = BROWNOUT_DET_LVL,
77 .enabled = true,
78 .reset_enabled = BROWNOUT_RESET_EN,
79 .flash_power_down = true,
80 .rf_power_down = true,
81 };
82
83 brownout_hal_config(&cfg);
84
85
86 #ifndef SOC_BROWNOUT_RESET_SUPPORTED
87 rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M);
88
89 brownout_hal_intr_enable(true);
90 #endif // not SOC_BROWNOUT_RESET_SUPPORTED
91 }
92
esp_brownout_disable(void)93 void esp_brownout_disable(void)
94 {
95 brownout_hal_config_t cfg = {
96 .enabled = false,
97 };
98
99 brownout_hal_config(&cfg);
100
101 #ifndef SOC_BROWNOUT_RESET_SUPPORTED
102 brownout_hal_intr_enable(false);
103
104 rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
105 #endif // not SOC_BROWNOUT_RESET_SUPPORTED
106 }
107