-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
pmSleep.c
374 lines (319 loc) · 12 KB
/
pmSleep.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "pmSleep.h"
#ifdef PMSLEEP_ENABLE
#define STRINGIFY_VAL(x) #x
#define STRINGIFY(x) STRINGIFY_VAL(x)
//holds duration error string
//uint32 PMSLEEP_SLEEP_MAX_TIME=FPM_SLEEP_MAX_TIME-1;
const char *PMSLEEP_DURATION_ERR_STR="duration: 0 or "STRINGIFY(PMSLEEP_SLEEP_MIN_TIME)"-"STRINGIFY(PMSLEEP_SLEEP_MAX_TIME)" us";
/* INTERNAL VARIABLES */
static void (*user_suspend_cb)(void) = NULL;
static void (*user_resume_cb)(void) = NULL;
static uint8 resume_opmode = 0;
static os_timer_t wifi_suspended_test_timer;
static uint8 autosleep_setting_temp = 0;
static os_timer_t null_mode_check_timer;
static pmSleep_param_t current_config;
/* INTERNAL FUNCTION DECLARATIONS */
static void disarm_all_timers(void);
static void null_mode_check_timer_cb(void* arg);
static void rearm_all_sw_timers(void);
static inline void register_lua_cb(lua_State* L,int* cb_ref);
static void resume_cb(void);
static void wifi_suspended_timer_cb(int arg);
/* INTERNAL FUNCTIONS */
#include "swTimer/swTimer.h"
static void disarm_all_timers(void){
#ifdef ENABLE_TIMER_SUSPEND
swtmr_suspend(NULL);
#endif
return;
}
static void rearm_all_sw_timers(void){
#ifdef ENABLE_TIMER_SUSPEND
swtmr_resume(NULL);
#endif
return;
}
static void null_mode_check_timer_cb(void* arg){
// if current opmode is NULL and uart 0 tx buffer is empty and uart 1 tx buffer is empty
if (wifi_get_opmode() == NULL_MODE && (READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0 &&
(READ_PERI_REG(UART_STATUS(1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0){
os_timer_disarm(&null_mode_check_timer);
disarm_all_timers();
if(current_config.sleep_mode == LIGHT_SLEEP_T){
wifi_fpm_do_sleep(current_config.sleep_duration);
return;
}
else{
sint8 retval_wifi_fpm_do_sleep = wifi_fpm_do_sleep(current_config.sleep_duration); // Request WiFi suspension and store return value
// If wifi_fpm_do_sleep success
if (retval_wifi_fpm_do_sleep == 0){
PMSLEEP_DBG("wifi_fpm_do_sleep success, starting wifi_suspend_test timer");
os_timer_disarm(&wifi_suspended_test_timer);
os_timer_setfn(&wifi_suspended_test_timer, (os_timer_func_t*)wifi_suspended_timer_cb, NULL);
os_timer_arm(&wifi_suspended_test_timer, 1, 1);
}
else{ // This should never happen. if it does, return the value for error reporting
wifi_fpm_close();
luaL_error(lua_getstate(), "wifi_fpm_do_sleep returned %d", retval_wifi_fpm_do_sleep);
return;
}
}
}
else{
// PMSLEEP_DBG("wifi_get_opmode()!=NULL_MODE");
}
}
//function to register a lua callback function in the LUA_REGISTRYINDEX for later execution
static inline void register_lua_cb(lua_State* L, int* cb_ref){
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
if( *cb_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, *cb_ref);
*cb_ref = ref;
}
// C callback for bringing WiFi back from forced sleep
static void resume_cb(void){
PMSLEEP_DBG("START");
//TODO: Add support for extended light sleep duration
wifi_fpm_close(); // Disable force sleep API
PMSLEEP_DBG("WiFi resume");
rearm_all_sw_timers();
//this section restores null mode auto sleep setting
if(autosleep_setting_temp == 1) {
wifi_fpm_auto_sleep_set_in_null_mode(autosleep_setting_temp);
autosleep_setting_temp = 0;
}
//this section restores previous wifi mode
if (resume_opmode == STATION_MODE || resume_opmode == SOFTAP_MODE || resume_opmode == STATIONAP_MODE){
if (wifi_set_opmode_current(resume_opmode)){
if (resume_opmode == STATION_MODE || resume_opmode == STATIONAP_MODE){
wifi_station_connect(); // Connect to currently configured Access Point
}
PMSLEEP_DBG("WiFi mode restored");
resume_opmode = 0; // reset variable to default value
}
}
else{
wifi_set_opmode_current(NULL_MODE);
}
//execute the external resume callback
if (user_resume_cb != NULL){
PMSLEEP_DBG("calling user resume cb (%p)", user_resume_cb);
user_resume_cb();
user_resume_cb = NULL;
}
PMSLEEP_DBG("END");
return;
}
// This callback executes the suspended callback when Wifi suspension is in effect
static void wifi_suspended_timer_cb(int arg){
// check if wifi is suspended.
if (pmSleep_get_state() == PMSLEEP_SUSPENDED){
os_timer_disarm(&wifi_suspended_test_timer); // Stop rf_closed_timer
PMSLEEP_DBG("WiFi is suspended");
//execute the external suspended callback
if (user_suspend_cb != NULL){
PMSLEEP_DBG("calling user suspend cb (%p)", user_suspend_cb);
user_suspend_cb();
user_suspend_cb = NULL;
}
PMSLEEP_DBG("END");
}
}
/* EXTERNAL FUNCTIONS */
//this function executes the application developer's Lua callback
void pmSleep_execute_lua_cb(int* cb_ref){
if (*cb_ref != LUA_NOREF){
lua_State* L = lua_getstate(); // Get Lua main thread pointer
lua_rawgeti(L, LUA_REGISTRYINDEX, *cb_ref); // Push resume callback onto the stack
lua_unref(L, *cb_ref); // Remove resume callback from registry
*cb_ref = LUA_NOREF; // Update variable since reference is no longer valid
lua_call(L, 0, 0); // Execute resume callback
}
}
//this function checks current wifi suspension state and returns the result
uint8 pmSleep_get_state(void){
if (fpm_rf_is_closed()) return PMSLEEP_SUSPENDED;
else if (fpm_is_open()) return PMSLEEP_SUSPENSION_PENDING;
else return PMSLEEP_AWAKE;
}
//this function parses the lua configuration table provided by the application developer
int pmSleep_parse_table_lua( lua_State* L, int table_idx, pmSleep_param_t *cfg, int *suspend_lua_cb_ref, int *resume_lua_cb_ref){
lua_Integer Linteger_tmp = 0;
lua_getfield(L, table_idx, "duration");
if( !lua_isnil(L, -1) ){ /* found? */
if( lua_isnumber(L, -1) ){
lua_Integer Linteger=luaL_checkinteger(L, -1);
luaL_argcheck(L,(((Linteger >= PMSLEEP_SLEEP_MIN_TIME) && (Linteger <= PMSLEEP_SLEEP_MAX_TIME)) ||
(Linteger == 0)), table_idx, PMSLEEP_DURATION_ERR_STR);
cfg->sleep_duration = (uint32)Linteger; // Get suspend duration
}
else{
return luaL_argerror( L, table_idx, "duration: must be number" );
}
}
else{
return luaL_argerror( L, table_idx, PMSLEEP_DURATION_ERR_STR );
}
lua_pop(L, 1);
if( cfg->sleep_mode == MODEM_SLEEP_T ){ //WiFi suspend
lua_getfield(L, table_idx, "suspend_cb");
if( !lua_isnil(L, -1) ){ /* found? */
if( lua_isfunction(L, -1) ){
lua_pushvalue(L, -1); // Push resume callback to the top of the stack
register_lua_cb(L, suspend_lua_cb_ref);
}
else{
return luaL_argerror( L, table_idx, "suspend_cb: must be function" );
}
}
lua_pop(L, 1);
}
else if (cfg->sleep_mode == LIGHT_SLEEP_T){ //CPU suspend
#ifdef ENABLE_TIMER_SUSPEND
lua_getfield(L, table_idx, "wake_pin");
if( !lua_isnil(L, -1) ){ /* found? */
if( lua_isnumber(L, -1) ){
Linteger_tmp=lua_tointeger(L, -1);
luaL_argcheck(L, (platform_gpio_exists(Linteger_tmp) && Linteger_tmp > 0), table_idx, "wake_pin: Invalid interrupt pin");
cfg->wake_pin = Linteger_tmp;
}
else{
return luaL_argerror( L, table_idx, "wake_pin: must be number" );
}
}
else if(cfg->sleep_duration == 0){
return luaL_argerror( L, table_idx, "wake_pin: must specify pin if sleep duration is indefinite" );
}
lua_pop(L, 1);
lua_getfield(L, table_idx, "int_type");
if( !lua_isnil(L, -1) ){ /* found? */
if( lua_isnumber(L, -1) ){
Linteger_tmp=lua_tointeger(L, -1);
luaL_argcheck(L, (Linteger_tmp == GPIO_PIN_INTR_ANYEDGE || Linteger_tmp == GPIO_PIN_INTR_HILEVEL
|| Linteger_tmp == GPIO_PIN_INTR_LOLEVEL || Linteger_tmp == GPIO_PIN_INTR_NEGEDGE
|| Linteger_tmp == GPIO_PIN_INTR_POSEDGE ), 1, "int_type: invalid interrupt type");
cfg->int_type = Linteger_tmp;
}
else{
return luaL_argerror( L, table_idx, "int_type: must be number" );
}
}
else{
cfg->int_type = GPIO_PIN_INTR_LOLEVEL;
}
lua_pop(L, 1);
#endif
}
else{
return luaL_error(L, "FPM Sleep mode not available");
}
lua_getfield(L, table_idx, "resume_cb");
if( !lua_isnil(L, -1) ){ /* found? */
if( lua_isfunction(L, -1) ){
lua_pushvalue(L, -1); // Push resume callback to the top of the stack
register_lua_cb(L, resume_lua_cb_ref);
}
else{
return luaL_argerror( L, table_idx, "resume_cb: must be function" );
}
}
lua_pop(L, 1);
lua_getfield(L, table_idx, "preserve_mode");
if( !lua_isnil(L, -1) ){ /* found? */
if( lua_isboolean(L, -1) ){
cfg->preserve_opmode=lua_toboolean(L, -1);
}
else{
return luaL_argerror( L, table_idx, "preseve_mode: must be boolean" );
}
}
else{
cfg->preserve_opmode = true;
}
lua_pop(L, 1);
//if sleep duration is zero, set indefinite sleep duration
if( cfg->sleep_duration == 0 ){
cfg->sleep_duration = FPM_SLEEP_MAX_TIME;
}
return 0;
}
//This function resumes ESP from MODEM_SLEEP
void pmSleep_resume(void (*resume_cb_ptr)(void)){
PMSLEEP_DBG("START");
uint8 fpm_state = pmSleep_get_state();
if(fpm_state>0){
if(resume_cb_ptr != NULL){
user_resume_cb = resume_cb_ptr;
}
wifi_fpm_do_wakeup(); // Wake up from sleep
resume_cb(); // Finish WiFi wakeup
}
PMSLEEP_DBG("END");
return;
}
//this function puts the ESP8266 into MODEM_SLEEP or LIGHT_SLEEP
void pmSleep_suspend(pmSleep_param_t *cfg){
PMSLEEP_DBG("START");
lua_State* L = lua_getstate();
#ifndef ENABLE_TIMER_SUSPEND
if(cfg->sleep_mode == LIGHT_SLEEP_T){
luaL_error(L, "timer suspend API is disabled, light sleep unavailable");
return;
}
#endif
uint8 current_wifi_mode = wifi_get_opmode(); // Get Current WiFi mode
user_resume_cb = cfg->resume_cb_ptr; //pointer to hold address of user_cb
user_suspend_cb = cfg->suspend_cb_ptr; //pointer to hold address of user_cb
// If Preserve_wifi_mode parameter is TRUE and current WiFi mode is not NULL_MODE
if (cfg->preserve_opmode && current_wifi_mode != 0){
resume_opmode = current_wifi_mode;
}
if (current_wifi_mode == STATION_MODE || current_wifi_mode == STATIONAP_MODE){
wifi_station_disconnect(); // Disconnect from Access Point
}
//the null mode sleep functionality interferes with the forced sleep API and must be disabled
if(get_fpm_auto_sleep_flag() == 1){
autosleep_setting_temp = 1;
wifi_fpm_auto_sleep_set_in_null_mode(0);
}
// If wifi_set_opmode_current is successful
if (wifi_set_opmode_current(NULL_MODE)){
PMSLEEP_DBG("sleep_mode is %s", cfg->sleep_mode == MODEM_SLEEP_T ? "MODEM_SLEEP_T" : "LIGHT_SLEEP_T");
wifi_fpm_set_sleep_type(cfg->sleep_mode);
wifi_fpm_open(); // Enable force sleep API
if (cfg->sleep_mode == LIGHT_SLEEP_T){
#ifdef ENABLE_TIMER_SUSPEND
if(platform_gpio_exists(cfg->wake_pin) && cfg->wake_pin > 0){
PMSLEEP_DBG("Wake-up pin is %d\t interrupt type is %d", cfg->wake_pin, cfg->int_type);
if((cfg->int_type != GPIO_PIN_INTR_ANYEDGE && cfg->int_type != GPIO_PIN_INTR_HILEVEL
&& cfg->int_type != GPIO_PIN_INTR_LOLEVEL && cfg->int_type != GPIO_PIN_INTR_NEGEDGE
&& cfg->int_type != GPIO_PIN_INTR_POSEDGE )){
wifi_fpm_close();
PMSLEEP_DBG("Invalid interrupt type");
return;
}
GPIO_DIS_OUTPUT(pin_num[cfg->wake_pin]);
PIN_FUNC_SELECT(pin_mux[cfg->wake_pin], pin_func[cfg->wake_pin]);
wifi_enable_gpio_wakeup(pin_num[cfg->wake_pin], cfg->int_type);
}
else if(cfg->sleep_duration == FPM_SLEEP_MAX_TIME && cfg->wake_pin == 255){
wifi_fpm_close();
PMSLEEP_DBG("No wake-up pin defined");
return;
}
#endif
}
wifi_fpm_set_wakeup_cb(resume_cb); // Set resume C callback
c_memcpy(¤t_config, cfg, sizeof(pmSleep_param_t));
PMSLEEP_DBG("sleep duration is %d", current_config.sleep_duration);
os_timer_disarm(&null_mode_check_timer);
os_timer_setfn(&null_mode_check_timer, null_mode_check_timer_cb, false);
os_timer_arm(&null_mode_check_timer, 1, 1);
}
else{
luaL_error(L, "ERR, mode change fail");
}
PMSLEEP_DBG("END");
return;
}
#endif