brownout.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdbool.h>
  18. #include "esp_private/system_internal.h"
  19. #include "driver/rtc_cntl.h"
  20. #include "esp_rom_sys.h"
  21. #include "soc/soc.h"
  22. #include "soc/cpu.h"
  23. #include "soc/rtc_periph.h"
  24. #include "hal/cpu_hal.h"
  25. #include "hal/brownout_hal.h"
  26. #include "sdkconfig.h"
  27. #if defined(CONFIG_ESP32_BROWNOUT_DET_LVL)
  28. #define BROWNOUT_DET_LVL CONFIG_ESP32_BROWNOUT_DET_LVL
  29. #elif defined(CONFIG_ESP32S2_BROWNOUT_DET_LVL)
  30. #define BROWNOUT_DET_LVL CONFIG_ESP32S2_BROWNOUT_DET_LVL
  31. #elif defined(CONFIG_ESP32S3_BROWNOUT_DET_LVL)
  32. #define BROWNOUT_DET_LVL CONFIG_ESP32S3_BROWNOUT_DET_LVL
  33. #elif defined(CONFIG_ESP32C3_BROWNOUT_DET_LVL)
  34. #define BROWNOUT_DET_LVL CONFIG_ESP32C3_BROWNOUT_DET_LVL
  35. #elif defined(CONFIG_ESP32H2_BROWNOUT_DET_LVL)
  36. #define BROWNOUT_DET_LVL CONFIG_ESP32H2_BROWNOUT_DET_LVL
  37. #else
  38. #define BROWNOUT_DET_LVL 0
  39. #endif
  40. #if SOC_BROWNOUT_RESET_SUPPORTED
  41. #define BROWNOUT_RESET_EN true
  42. #else
  43. #define BROWNOUT_RESET_EN false
  44. #endif // SOC_BROWNOUT_RESET_SUPPORTED
  45. #ifndef SOC_BROWNOUT_RESET_SUPPORTED
  46. static void rtc_brownout_isr_handler(void *arg)
  47. {
  48. /* Normally RTC ISR clears the interrupt flag after the application-supplied
  49. * handler returns. Since restart is called here, the flag needs to be
  50. * cleared manually.
  51. */
  52. brownout_hal_intr_clear();
  53. /* Stall the other CPU to make sure the code running there doesn't use UART
  54. * at the same time as the following esp_rom_printf.
  55. */
  56. esp_cpu_stall(!cpu_hal_get_core_id());
  57. esp_reset_reason_set_hint(ESP_RST_BROWNOUT);
  58. esp_rom_printf("\r\nBrownout detector was triggered\r\n\r\n");
  59. esp_restart_noos();
  60. }
  61. #endif // not SOC_BROWNOUT_RESET_SUPPORTED
  62. void esp_brownout_init(void)
  63. {
  64. brownout_hal_config_t cfg = {
  65. .threshold = BROWNOUT_DET_LVL,
  66. .enabled = true,
  67. .reset_enabled = BROWNOUT_RESET_EN,
  68. .flash_power_down = true,
  69. .rf_power_down = true,
  70. };
  71. brownout_hal_config(&cfg);
  72. #ifndef SOC_BROWNOUT_RESET_SUPPORTED
  73. rtc_isr_register(rtc_brownout_isr_handler, NULL, RTC_CNTL_BROWN_OUT_INT_ENA_M);
  74. brownout_hal_intr_enable(true);
  75. #endif // not SOC_BROWNOUT_RESET_SUPPORTED
  76. }
  77. void esp_brownout_disable(void)
  78. {
  79. brownout_hal_config_t cfg = {
  80. .enabled = false,
  81. };
  82. brownout_hal_config(&cfg);
  83. #ifndef SOC_BROWNOUT_RESET_SUPPORTED
  84. brownout_hal_intr_enable(false);
  85. rtc_isr_deregister(rtc_brownout_isr_handler, NULL);
  86. #endif // not SOC_BROWNOUT_RESET_SUPPORTED
  87. }