2021-11-06 05:24:45 -04:00
|
|
|
/*
|
2022-01-17 21:32:56 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-06 05:24:45 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-05-09 23:34:06 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2020-09-03 06:17:24 -04:00
|
|
|
|
|
|
|
#include "esp_private/system_internal.h"
|
|
|
|
#include "driver/rtc_cntl.h"
|
|
|
|
|
|
|
|
#include "esp_rom_sys.h"
|
|
|
|
|
2019-05-09 23:34:06 -04:00
|
|
|
#include "soc/soc.h"
|
2021-12-13 23:38:15 -05:00
|
|
|
#include "esp_cpu.h"
|
2020-01-21 10:32:28 -05:00
|
|
|
#include "soc/rtc_periph.h"
|
2020-09-03 06:17:24 -04:00
|
|
|
#include "hal/cpu_hal.h"
|
|
|
|
|
2020-01-21 10:32:28 -05:00
|
|
|
#include "hal/brownout_hal.h"
|
2020-09-03 06:17:24 -04:00
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2020-01-21 10:41:39 -05:00
|
|
|
#if defined(CONFIG_ESP32_BROWNOUT_DET_LVL)
|
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP32_BROWNOUT_DET_LVL
|
|
|
|
#elif defined(CONFIG_ESP32S2_BROWNOUT_DET_LVL)
|
2019-06-04 03:02:01 -04:00
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP32S2_BROWNOUT_DET_LVL
|
2021-01-07 09:44:33 -05:00
|
|
|
#elif defined(CONFIG_ESP32S3_BROWNOUT_DET_LVL)
|
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP32S3_BROWNOUT_DET_LVL
|
|
|
|
#elif defined(CONFIG_ESP32C3_BROWNOUT_DET_LVL)
|
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP32C3_BROWNOUT_DET_LVL
|
2021-06-10 03:22:43 -04:00
|
|
|
#elif defined(CONFIG_ESP32H2_BROWNOUT_DET_LVL)
|
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP32H2_BROWNOUT_DET_LVL
|
2022-01-17 21:32:56 -05:00
|
|
|
#elif defined(CONFIG_ESP32C2_BROWNOUT_DET_LVL)
|
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP32C2_BROWNOUT_DET_LVL
|
2019-05-09 23:34:06 -04:00
|
|
|
#else
|
|
|
|
#define BROWNOUT_DET_LVL 0
|
2020-01-21 10:41:39 -05:00
|
|
|
#endif
|
|
|
|
|
2020-09-12 05:58:30 -04:00
|
|
|
#if SOC_BROWNOUT_RESET_SUPPORTED
|
2020-01-21 10:41:39 -05:00
|
|
|
#define BROWNOUT_RESET_EN true
|
|
|
|
#else
|
|
|
|
#define BROWNOUT_RESET_EN false
|
|
|
|
#endif // SOC_BROWNOUT_RESET_SUPPORTED
|
|
|
|
|
2021-01-07 09:44:33 -05:00
|
|
|
#ifndef SOC_BROWNOUT_RESET_SUPPORTED
|
2019-08-11 22:06:07 -04:00
|
|
|
static void rtc_brownout_isr_handler(void *arg)
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
|
|
|
/* Normally RTC ISR clears the interrupt flag after the application-supplied
|
|
|
|
* handler returns. Since restart is called here, the flag needs to be
|
|
|
|
* cleared manually.
|
|
|
|
*/
|
2020-01-21 10:41:39 -05:00
|
|
|
brownout_hal_intr_clear();
|
2019-05-09 23:34:06 -04:00
|
|
|
/* Stall the other CPU to make sure the code running there doesn't use UART
|
2020-07-21 01:07:34 -04:00
|
|
|
* at the same time as the following esp_rom_printf.
|
2019-05-09 23:34:06 -04:00
|
|
|
*/
|
2020-09-03 06:17:24 -04:00
|
|
|
esp_cpu_stall(!cpu_hal_get_core_id());
|
2020-01-21 10:32:28 -05:00
|
|
|
esp_reset_reason_set_hint(ESP_RST_BROWNOUT);
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_printf("\r\nBrownout detector was triggered\r\n\r\n");
|
2019-05-09 23:34:06 -04:00
|
|
|
esp_restart_noos();
|
|
|
|
}
|
2021-01-07 09:44:33 -05:00
|
|
|
#endif // not SOC_BROWNOUT_RESET_SUPPORTED
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2019-08-11 22:06:07 -04:00
|
|
|
void esp_brownout_init(void)
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2020-01-21 10:32:28 -05:00
|
|
|
brownout_hal_config_t cfg = {
|
|
|
|
.threshold = BROWNOUT_DET_LVL,
|
|
|
|
.enabled = true,
|
2020-01-21 10:41:39 -05:00
|
|
|
.reset_enabled = BROWNOUT_RESET_EN,
|
2020-01-21 10:32:28 -05:00
|
|
|
.flash_power_down = true,
|
|
|
|
.rf_power_down = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
brownout_hal_config(&cfg);
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2021-03-24 07:58:12 -04:00
|
|
|
|
2021-01-07 09:44:33 -05:00
|
|
|
#ifndef SOC_BROWNOUT_RESET_SUPPORTED
|
2020-03-02 23:22:41 -05:00
|
|
|
rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M);
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2020-01-21 10:32:28 -05:00
|
|
|
brownout_hal_intr_enable(true);
|
2021-01-07 09:44:33 -05:00
|
|
|
#endif // not SOC_BROWNOUT_RESET_SUPPORTED
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
2021-02-09 06:30:43 -05:00
|
|
|
|
|
|
|
void esp_brownout_disable(void)
|
|
|
|
{
|
|
|
|
brownout_hal_config_t cfg = {
|
|
|
|
.enabled = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
brownout_hal_config(&cfg);
|
|
|
|
|
|
|
|
#ifndef SOC_BROWNOUT_RESET_SUPPORTED
|
|
|
|
brownout_hal_intr_enable(false);
|
|
|
|
|
|
|
|
rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
|
|
|
|
#endif // not SOC_BROWNOUT_RESET_SUPPORTED
|
|
|
|
}
|