2021-11-06 17:24:45 +08:00
|
|
|
/*
|
2022-01-18 10:32:56 +08:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-06 17:24:45 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-05-10 11:34:06 +08:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2020-09-03 18:17:24 +08:00
|
|
|
|
|
|
|
#include "esp_private/system_internal.h"
|
2022-04-29 13:40:23 +08:00
|
|
|
#include "esp_private/rtc_ctrl.h"
|
2022-05-30 16:49:19 +08:00
|
|
|
#include "esp_private/spi_flash_os.h"
|
2020-09-03 18:17:24 +08:00
|
|
|
|
|
|
|
#include "esp_rom_sys.h"
|
2022-07-21 19:24:42 +08:00
|
|
|
#include "esp_cpu.h"
|
2020-09-03 18:17:24 +08:00
|
|
|
|
2019-05-10 11:34:06 +08:00
|
|
|
#include "soc/soc.h"
|
2022-07-21 19:24:42 +08:00
|
|
|
|
2020-01-21 16:32:28 +01:00
|
|
|
#include "soc/rtc_periph.h"
|
2022-05-30 16:49:19 +08:00
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "bootloader_flash.h"
|
|
|
|
#include "esp_intr_alloc.h"
|
2020-09-03 18:17:24 +08:00
|
|
|
|
2020-01-21 16:32:28 +01:00
|
|
|
#include "hal/brownout_hal.h"
|
2020-09-03 18:17:24 +08:00
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
2019-05-10 11:34:06 +08:00
|
|
|
|
2022-01-07 12:22:56 +08:00
|
|
|
#if defined(CONFIG_ESP_BROWNOUT_DET_LVL)
|
|
|
|
#define BROWNOUT_DET_LVL CONFIG_ESP_BROWNOUT_DET_LVL
|
2019-05-10 11:34:06 +08:00
|
|
|
#else
|
|
|
|
#define BROWNOUT_DET_LVL 0
|
2020-01-21 16:41:39 +01:00
|
|
|
#endif
|
|
|
|
|
2022-05-30 16:49:19 +08:00
|
|
|
#if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
|
|
|
|
IRAM_ATTR static void rtc_brownout_isr_handler(void *arg)
|
2019-05-10 11:34:06 +08: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 16:41:39 +01:00
|
|
|
brownout_hal_intr_clear();
|
2022-08-01 10:36:19 +08:00
|
|
|
|
2022-05-30 16:49:19 +08:00
|
|
|
// Stop the other core.
|
2022-08-01 10:36:19 +08:00
|
|
|
#if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
|
|
|
|
const uint32_t core_id = esp_cpu_get_core_id();
|
|
|
|
const uint32_t other_core_id = (core_id == 0) ? 1 : 0;
|
|
|
|
esp_cpu_stall(other_core_id);
|
|
|
|
#endif
|
|
|
|
|
2020-01-21 16:32:28 +01:00
|
|
|
esp_reset_reason_set_hint(ESP_RST_BROWNOUT);
|
2022-05-30 16:49:19 +08:00
|
|
|
#if CONFIG_SPI_FLASH_BROWNOUT_RESET
|
|
|
|
if (spi_flash_brownout_need_reset()) {
|
|
|
|
bootloader_flash_reset_chip();
|
|
|
|
} else
|
|
|
|
#endif // CONFIG_SPI_FLASH_BROWNOUT_RESET
|
|
|
|
{
|
|
|
|
esp_rom_printf("\r\nBrownout detector was triggered\r\n\r\n");
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:34:06 +08:00
|
|
|
esp_restart_noos();
|
|
|
|
}
|
2022-05-30 16:49:19 +08:00
|
|
|
#endif // CONFIG_ESP_SYSTEM_BROWNOUT_INTR
|
2019-05-10 11:34:06 +08:00
|
|
|
|
2019-08-12 12:06:07 +10:00
|
|
|
void esp_brownout_init(void)
|
2019-05-10 11:34:06 +08:00
|
|
|
{
|
2022-05-30 16:49:19 +08:00
|
|
|
#if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
|
2020-01-21 16:32:28 +01:00
|
|
|
brownout_hal_config_t cfg = {
|
|
|
|
.threshold = BROWNOUT_DET_LVL,
|
|
|
|
.enabled = true,
|
2022-05-30 16:49:19 +08:00
|
|
|
.reset_enabled = false,
|
2020-01-21 16:32:28 +01:00
|
|
|
.flash_power_down = true,
|
|
|
|
.rf_power_down = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
brownout_hal_config(&cfg);
|
2022-05-30 16:49:19 +08:00
|
|
|
brownout_hal_intr_clear();
|
|
|
|
rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M, RTC_INTR_FLAG_IRAM);
|
|
|
|
brownout_hal_intr_enable(true);
|
2019-05-10 11:34:06 +08:00
|
|
|
|
2022-05-30 16:49:19 +08:00
|
|
|
#else // brownout without interrupt
|
2021-03-24 19:58:12 +08:00
|
|
|
|
2022-05-30 16:49:19 +08:00
|
|
|
brownout_hal_config_t cfg = {
|
|
|
|
.threshold = BROWNOUT_DET_LVL,
|
|
|
|
.enabled = true,
|
|
|
|
.reset_enabled = true,
|
|
|
|
.flash_power_down = true,
|
|
|
|
.rf_power_down = true,
|
|
|
|
};
|
2019-05-10 11:34:06 +08:00
|
|
|
|
2022-05-30 16:49:19 +08:00
|
|
|
brownout_hal_config(&cfg);
|
|
|
|
#endif
|
2019-05-10 11:34:06 +08:00
|
|
|
}
|
2021-02-09 19:30:43 +08:00
|
|
|
|
|
|
|
void esp_brownout_disable(void)
|
|
|
|
{
|
|
|
|
brownout_hal_config_t cfg = {
|
|
|
|
.enabled = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
brownout_hal_config(&cfg);
|
2022-05-30 16:49:19 +08:00
|
|
|
#if CONFIG_ESP_SYSTEM_BROWNOUT_INTR
|
2021-02-09 19:30:43 +08:00
|
|
|
brownout_hal_intr_enable(false);
|
|
|
|
rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
|
2022-05-30 16:49:19 +08:00
|
|
|
#endif // CONFIG_ESP_SYSTEM_BROWNOUT_INTR
|
2021-02-09 19:30:43 +08:00
|
|
|
}
|