-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathmpsleep.c
176 lines (164 loc) · 5.78 KB
/
mpsleep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* This file is part of the MicroPython ESP32 project, https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo
*
* The MIT License (MIT)
*
* Copyright (c) 2018 LoBo (https://github.com/loboris)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* Author: LoBo ([email protected] / https://github.com/loboris)
* Modification date: 10/2017
* Code based on MicroPython port by Pycom Limited
*/
#include <stdint.h>
#include <string.h>
#include "py/mpstate.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "sdkconfig.h"
#include "rom/rtc.h"
#include "esp_system.h"
#include "esp_sleep.h"
#include "esp_log.h"
#include "mpsleep.h"
STATIC mpsleep_reset_cause_t mpsleep_reset_cause = MPSLEEP_UNKNOWN_RESET;
STATIC mpsleep_wake_reason_t mpsleep_wake_reason = MPSLEEP_NONE_WAKE;
//-------------------------
void mpsleep_init0 (void) {
// check the reset cause
RESET_REASON reason = rtc_get_reset_reason(0);
switch (reason) {
case TG0WDT_SYS_RESET:
mpsleep_reset_cause = MPSLEEP_WDT_RESET;
break;
case DEEPSLEEP_RESET:
mpsleep_reset_cause = MPSLEEP_DEEPSLEEP_RESET;
break;
case RTCWDT_BROWN_OUT_RESET:
mpsleep_reset_cause = MPSLEEP_BROWN_OUT_RESET;
break;
case TG1WDT_SYS_RESET:
mpsleep_reset_cause = MPSLEEP_HARD_RESET;
break;
case SW_CPU_RESET: // machine.reset()
mpsleep_reset_cause = MPSLEEP_SOFT_CPU_RESET;
break;
case POWERON_RESET:
mpsleep_reset_cause = MPSLEEP_PWRON_RESET;
break;
case RTCWDT_RTC_RESET:
mpsleep_reset_cause = MPSLEEP_RTCWDT_RESET;
break;
default:
mpsleep_reset_cause = MPSLEEP_UNKNOWN_RESET;
ESP_LOGD("[RESET]", "Reset reason: %d", reason);
break;
}
// check the wakeup reason
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_EXT0:
mpsleep_wake_reason = MPSLEEP_GPIO0_WAKE;
break;
case ESP_SLEEP_WAKEUP_EXT1:
mpsleep_wake_reason = MPSLEEP_GPIO1_WAKE;
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
mpsleep_wake_reason = MPSLEEP_TOUCH_WAKE;
break;
case ESP_SLEEP_WAKEUP_TIMER:
mpsleep_wake_reason = MPSLEEP_RTC_WAKE;
break;
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
mpsleep_wake_reason = MPSLEEP_NONE_WAKE;
break;
}
}
//-------------------------------------
void mpsleep_signal_soft_reset (void) {
mpsleep_reset_cause = MPSLEEP_SOFT_RESET;
}
//----------------------------------------------------
mpsleep_reset_cause_t mpsleep_get_reset_cause (void) {
return mpsleep_reset_cause;
}
//----------------------------------------------------
mpsleep_wake_reason_t mpsleep_get_wake_reason (void) {
return mpsleep_wake_reason;
}
//------------------------------------------------
void mpsleep_get_reset_desc (char *reset_reason) {
switch (mpsleep_reset_cause) {
case MPSLEEP_PWRON_RESET:
sprintf(reset_reason, "Power on reset");
break;
case MPSLEEP_RTCWDT_RESET:
sprintf(reset_reason, "RTC WDT reset");
break;
case MPSLEEP_HARD_RESET:
sprintf(reset_reason, "Hard reset");
break;
case MPSLEEP_WDT_RESET:
sprintf(reset_reason, "WDT reset");
break;
case MPSLEEP_DEEPSLEEP_RESET:
sprintf(reset_reason, "Deepsleep wake-up");
break;
case MPSLEEP_SOFT_CPU_RESET:
sprintf(reset_reason, "Soft CPU reset");
break;
case MPSLEEP_SOFT_RESET:
sprintf(reset_reason, "Soft reset");
break;
case MPSLEEP_BROWN_OUT_RESET:
sprintf(reset_reason, "Brownout reset");
break;
default:
sprintf(reset_reason, "Unknown reset reason");
break;
}
}
//----------------------------------------------
void mpsleep_get_wake_desc (char *wake_reason) {
switch (mpsleep_wake_reason) {
case MPSLEEP_NONE_WAKE:
sprintf(wake_reason, "No wake-up reason");
break;
case MPSLEEP_GPIO0_WAKE:
sprintf(wake_reason, "EXT_0 wake-up");
break;
case MPSLEEP_GPIO1_WAKE:
sprintf(wake_reason, "EXT_1 wake-up");
break;
case MPSLEEP_TOUCH_WAKE:
sprintf(wake_reason, "Touchpad wake-up");
break;
case MPSLEEP_RTC_WAKE:
sprintf(wake_reason, "RTC wake-up");
break;
case MPSLEEP_ULP_WAKE:
sprintf(wake_reason, "ULP wake-up");
break;
default:
sprintf(wake_reason, "Unknown wake reason");
break;
}
}