brownout.c 3.0 KB

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