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