-
Notifications
You must be signed in to change notification settings - Fork 511
/
sleep.cpp
222 lines (190 loc) · 6.62 KB
/
sleep.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "application.h"
#include "unit-test/unit-test.h"
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
static retained uint32_t magick = 0;
SYSTEM_THREAD(ENABLED);
// Serial1LogHandler logHandler(115200, LOG_LEVEL_ALL);
/*
* Tests for issue #1043
* This test will do a couple of publishes and enter deep sleep for a short time (which will reset the device).
* After the reset, the tests should be restarted again.
*/
test(sleep_0_device_wakes_from_deep_sleep_with_short_sleep_time)
{
waitFor(Particle.connected, 5 * 60 * 1000);
if (magick == 0xdeadbeef) {
magick = 0;
// We should have woken up from deep sleep
assertEqual(System.resetReason(), (int)RESET_REASON_POWER_MANAGEMENT);
/**
* Issue #1447 "WKP/A7 ignores pinMode after waking from deep sleep"
* A7/WKP pin must be jumpered to D7 because
* digitalWrite(A7) will be equal to digitalRead(A7) despite this issue.
*/
Serial.println("SETUP: Make sure to jumper A7/WKP pin to D7 pin.");
pinMode(A7, OUTPUT);
pinMode(D7, INPUT);
digitalWrite(A7, HIGH);
delay(1);
assertEqual((int)digitalRead(D7), (int)HIGH);
digitalWrite(A7, LOW);
delay(1);
assertEqual((int)digitalRead(D7), (int)LOW);
// Issue #1447
} else {
Serial.println("Publishing 10 messages, see them at 'particle subscribe mine'");
// Set magic key in retained memory to indicate that we just went into deep sleep
magick = 0xdeadbeef;
for (int i = 0; i < 10; i++) {
Particle.publish("test", String("teststring ") + String(i+1));
delay(1100);
}
Serial.println("deep sleeping for 5 seconds, please reconnect serial and run tests again!");
// Do a couple of publishes
System.sleep(SLEEP_MODE_DEEP, 5);
}
}
/*
* Tests for issue #1029
* This test will enter STOP mode for 5 seconds, which will cause USB Serial connection to close.
* USB Serial port should be opened again for the test to progress after waking from STOP mode.
* The test will wait for user to open USB Serial session.
*/
test(sleep_1_interrupts_attached_handler_is_not_detached_after_stop_mode)
{
// Just in case
magick = 0;
uint16_t pin = D1;
STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
uint16_t exti = PIN_MAP[pin].gpio_pin;
volatile bool cont = false;
pinMode(D7, OUTPUT);
digitalWrite(D7, LOW);
pinMode(pin, INPUT);
// Configure D1, attach interrupt handler
attachInterrupt(pin, [&] {
cont = true;
}, FALLING);
// Check that interrupt handler fires
cont = false;
EXTI_GenerateSWInterrupt(exti);
uint32_t s = millis();
while (!cont && (millis() - s) < 1000);
assertEqual(static_cast<bool>(cont), true);
Serial.println("sleeping in stop mode for 5 seconds, please reconnect serial!");
delay(100);
// Sleep for 5 seconds
System.sleep(pin, FALLING, 5, SLEEP_NETWORK_STANDBY);
// Ideally there should be a test here checking that the interrupt triggered while sleeping
// was immediately serviced
digitalWrite(D7, HIGH);
// Wait for Host to connect back
while (!Serial.isConnected());
Serial.println("welcome back!");
// Check that interrupt handler fires
cont = false;
EXTI_GenerateSWInterrupt(exti);
s = millis();
while (!cont && (millis() - s) < 1000);
assertEqual(static_cast<bool>(cont), true);
detachInterrupt(pin);
}
static bool NotReady() {
#if Wiring_Cellular
return !Cellular.ready();
#elif Wiring_WiFi
return !WiFi.ready();
#endif
}
#if PLATFORM_ID==PLATFORM_ELECTRON_PRODUCTION
static int testfunc(String s) {
return 1337;
}
static uint32_t s_cloud_disconnected = 0;
static void onCloudStatus(system_event_t ev, int param) {
switch (param) {
case cloud_status_disconnected:
s_cloud_disconnected = millis();
break;
default:
break;
}
}
/*
* Tests for issue #1133
* NOTE: This test will take approximately 5 minutes to finish
*/
test(sleep_2_electron_all_confirmable_messages_are_sent_before_sleep_step_1)
{
//Serial.println("Disconnecting...");
Particle.disconnect();
waitFor(Particle.disconnected, 60000);
assertTrue(Particle.disconnected());
//Serial.println("Disconnected");
Cellular.off();
waitFor(NotReady, 60000);
pinMode(D7, OUTPUT);
digitalWrite(D7, LOW);
// Switch to MANUAL mode
//Serial.println("Switching to MANUAL");
set_system_mode(MANUAL);
//Serial.println("Switched");
// Register a function with a random name just in case
// so that session resume will definitely not happen
randomSeed((uint32_t)Time.now());
int r = random(0, 99999999);
char tmp[13] = {0};
sprintf(tmp, "f%d", r);
//Serial.println("Registering random function");
Particle.function(tmp, testfunc);
//Serial.println("Registered");
Cellular.on();
waitFor(Cellular.ready, 60000);
// Connect
//Serial.println("Connecting...");
Particle.connect();
waitFor(Particle.connected, 5 * 60 * 1000);
assertTrue(Particle.connected());
//Serial.println("Connected");
System.on(cloud_status, onCloudStatus);
Serial.println("Publishing 10 messages over next 5 minutes, see them at 'particle subscribe mine'");
// Repeat 10 times
for (int i = 0; i < 10; i++) {
assertTrue(Particle.connected());
sprintf(tmp, "%d/10", i + 1);
if (i == 9) {
assertTrue(Particle.publish("please reconnect serial in 30 seconds!", tmp, 60, PRIVATE));
} else {
assertTrue(Particle.publish("sleeping", tmp, 60, PRIVATE));
}
uint32_t ts = millis();
System.sleep(65535, RISING, 30, SLEEP_NETWORK_STANDBY);
assertTrue(Particle.connected());
assertLessOrEqual(s_cloud_disconnected, ts);
}
}
test(sleep_2_electron_all_confirmable_messages_are_sent_before_sleep_step_2) {
pinMode(D7, OUTPUT);
digitalWrite(D7, HIGH);
waitUntil(Serial.isConnected);
}
test(sleep_3_restore_system_mode) {
set_system_mode(AUTOMATIC);
}
#endif // PLATFORM_ID==PLATFORM_ELECTRON_PRODUCTION
/*
* Issue #1155, broken by PR #1051/#1076
*/
test(sleep_4_system_sleep_sleep_mode_wlan_works_correctly)
{
#if Wiring_Cellular
Serial.println("sleeping Cellular for 10 seconds, will reconnect to Cloud shortly!");
#elif Wiring_WiFi
Serial.println("sleeping Wi-Fi for 10 seconds, will reconnect to Cloud shortly!");
#endif
System.sleep(10);
waitFor(Particle.disconnected, 60000);
waitFor(NotReady, 60000);
waitFor(Particle.connected, 120000);
assertTrue(Particle.connected());
}