reset_reason.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2018 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 "esp_system.h"
  15. #include "esp_rom_sys.h"
  16. #include "esp_private/system_internal.h"
  17. #include "soc/rtc_periph.h"
  18. #include "esp32/rom/rtc.h"
  19. static void esp_reset_reason_clear_hint(void);
  20. static esp_reset_reason_t s_reset_reason;
  21. static esp_reset_reason_t get_reset_reason(uint32_t rtc_reset_reason, esp_reset_reason_t reset_reason_hint)
  22. {
  23. switch (rtc_reset_reason) {
  24. case RESET_REASON_CHIP_POWER_ON:
  25. return ESP_RST_POWERON;
  26. case RESET_REASON_CPU0_SW:
  27. case RESET_REASON_CORE_SW:
  28. if (reset_reason_hint == ESP_RST_PANIC ||
  29. reset_reason_hint == ESP_RST_BROWNOUT ||
  30. reset_reason_hint == ESP_RST_TASK_WDT ||
  31. reset_reason_hint == ESP_RST_INT_WDT) {
  32. return reset_reason_hint;
  33. }
  34. return ESP_RST_SW;
  35. case RESET_REASON_CORE_DEEP_SLEEP:
  36. return ESP_RST_DEEPSLEEP;
  37. case RESET_REASON_CORE_MWDT0:
  38. return ESP_RST_TASK_WDT;
  39. case RESET_REASON_CORE_MWDT1:
  40. return ESP_RST_INT_WDT;
  41. case RESET_REASON_CORE_RTC_WDT:
  42. case RESET_REASON_SYS_RTC_WDT:
  43. case RESET_REASON_CPU0_RTC_WDT:
  44. case RESET_REASON_CPU0_MWDT0:
  45. return ESP_RST_WDT;
  46. case RESET_REASON_SYS_BROWN_OUT:
  47. return ESP_RST_BROWNOUT;
  48. case RESET_REASON_CORE_SDIO:
  49. return ESP_RST_SDIO;
  50. default:
  51. return ESP_RST_UNKNOWN;
  52. }
  53. }
  54. static void __attribute__((constructor)) esp_reset_reason_init(void)
  55. {
  56. esp_reset_reason_t hint = esp_reset_reason_get_hint();
  57. s_reset_reason = get_reset_reason(esp_rom_get_reset_reason(PRO_CPU_NUM), hint);
  58. if (hint != ESP_RST_UNKNOWN) {
  59. esp_reset_reason_clear_hint();
  60. }
  61. }
  62. esp_reset_reason_t esp_reset_reason(void)
  63. {
  64. return s_reset_reason;
  65. }
  66. /* Reset reason hint is stored in RTC_RESET_CAUSE_REG, a.k.a. RTC_CNTL_STORE6_REG,
  67. * a.k.a. RTC_ENTRY_ADDR_REG. It is safe to use this register both for the
  68. * deep sleep wake stub entry address and for reset reason hint, since wake stub
  69. * is only used for deep sleep reset, and in this case the reason provided by
  70. * esp_rom_get_reset_reason is unambiguous.
  71. *
  72. * Same layout is used as for RTC_APB_FREQ_REG (a.k.a. RTC_CNTL_STORE5_REG):
  73. * the value is replicated in low and high half-words. In addition to that,
  74. * MSB is set to 1, which doesn't happen when RTC_CNTL_STORE6_REG contains
  75. * deep sleep wake stub address.
  76. */
  77. #define RST_REASON_BIT 0x80000000
  78. #define RST_REASON_MASK 0x7FFF
  79. #define RST_REASON_SHIFT 16
  80. /* in IRAM, can be called from panic handler */
  81. void IRAM_ATTR esp_reset_reason_set_hint(esp_reset_reason_t hint)
  82. {
  83. assert((hint & (~RST_REASON_MASK)) == 0);
  84. uint32_t val = hint | (hint << RST_REASON_SHIFT) | RST_REASON_BIT;
  85. REG_WRITE(RTC_RESET_CAUSE_REG, val);
  86. }
  87. /* in IRAM, can be called from panic handler */
  88. esp_reset_reason_t IRAM_ATTR esp_reset_reason_get_hint(void)
  89. {
  90. uint32_t reset_reason_hint = REG_READ(RTC_RESET_CAUSE_REG);
  91. uint32_t high = (reset_reason_hint >> RST_REASON_SHIFT) & RST_REASON_MASK;
  92. uint32_t low = reset_reason_hint & RST_REASON_MASK;
  93. if ((reset_reason_hint & RST_REASON_BIT) == 0 || high != low) {
  94. return ESP_RST_UNKNOWN;
  95. }
  96. return (esp_reset_reason_t) low;
  97. }
  98. static void esp_reset_reason_clear_hint(void)
  99. {
  100. REG_WRITE(RTC_RESET_CAUSE_REG, 0);
  101. }