pmSleep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "pmSleep.h"
  2. #ifdef PMSLEEP_ENABLE
  3. #define STRINGIFY_VAL(x) #x
  4. #define STRINGIFY(x) STRINGIFY_VAL(x)
  5. //holds duration error string
  6. //uint32 PMSLEEP_SLEEP_MAX_TIME=FPM_SLEEP_MAX_TIME-1;
  7. const char *PMSLEEP_DURATION_ERR_STR="duration: 0 or "STRINGIFY(PMSLEEP_SLEEP_MIN_TIME)"-"STRINGIFY(PMSLEEP_SLEEP_MAX_TIME)" us";
  8. /* INTERNAL VARIABLES */
  9. static void (*user_suspend_cb)(void) = NULL;
  10. static void (*user_resume_cb)(void) = NULL;
  11. static uint8 resume_opmode = 0;
  12. static os_timer_t wifi_suspended_test_timer;
  13. static uint8 autosleep_setting_temp = 0;
  14. static os_timer_t null_mode_check_timer;
  15. static pmSleep_param_t current_config;
  16. /* INTERNAL FUNCTION DECLARATIONS */
  17. static void suspend_all_timers(void);
  18. static void null_mode_check_timer_cb(void* arg);
  19. static void resume_all_timers(void);
  20. static inline void register_lua_cb(lua_State* L,int* cb_ref);
  21. static void resume_cb(void);
  22. static void wifi_suspended_timer_cb(int arg);
  23. /* INTERNAL FUNCTIONS */
  24. #include "swTimer/swTimer.h"
  25. static void suspend_all_timers(void){
  26. #ifdef ENABLE_TIMER_SUSPEND
  27. swtmr_suspend(NULL);
  28. #endif
  29. return;
  30. }
  31. static void resume_all_timers(void){
  32. #ifdef ENABLE_TIMER_SUSPEND
  33. swtmr_resume(NULL);
  34. #endif
  35. return;
  36. }
  37. static void null_mode_check_timer_cb(void* arg){
  38. if (wifi_get_opmode() == NULL_MODE){
  39. //check if uart 0 tx buffer is empty and uart 1 tx buffer is empty
  40. if(current_config.sleep_mode == LIGHT_SLEEP_T){
  41. if((READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0 &&
  42. (READ_PERI_REG(UART_STATUS(1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S)) == 0){
  43. ets_timer_disarm(&null_mode_check_timer);
  44. suspend_all_timers();
  45. //Ensure UART 0/1 TX FIFO is clear
  46. SET_PERI_REG_MASK(UART_CONF0(0), UART_TXFIFO_RST);//RESET FIFO
  47. CLEAR_PERI_REG_MASK(UART_CONF0(0), UART_TXFIFO_RST);
  48. SET_PERI_REG_MASK(UART_CONF0(1), UART_TXFIFO_RST);//RESET FIFO
  49. CLEAR_PERI_REG_MASK(UART_CONF0(1), UART_TXFIFO_RST);
  50. wifi_fpm_do_sleep(current_config.sleep_duration);
  51. return;
  52. }
  53. else{
  54. return;
  55. }
  56. }
  57. else{ //MODEM_SLEEP_T
  58. sint8 retval_wifi_fpm_do_sleep = wifi_fpm_do_sleep(current_config.sleep_duration); // Request WiFi suspension and store return value
  59. // If wifi_fpm_do_sleep success
  60. if (retval_wifi_fpm_do_sleep == 0){
  61. PMSLEEP_DBG("wifi_fpm_do_sleep success, starting wifi_suspend_test timer");
  62. os_timer_disarm(&wifi_suspended_test_timer);
  63. os_timer_setfn(&wifi_suspended_test_timer, (os_timer_func_t*)wifi_suspended_timer_cb, NULL);
  64. os_timer_arm(&wifi_suspended_test_timer, 1, 1);
  65. }
  66. else{ // This should never happen. if it does, return the value for error reporting
  67. wifi_fpm_close();
  68. PMSLEEP_ERR("wifi_fpm_do_sleep returned %d", retval_wifi_fpm_do_sleep);
  69. }
  70. }
  71. ets_timer_disarm(&null_mode_check_timer);
  72. return;
  73. }
  74. }
  75. //function to register a lua callback function in the LUA_REGISTRYINDEX for later execution
  76. static inline void register_lua_cb(lua_State* L, int* cb_ref){
  77. int ref = luaL_ref(L, LUA_REGISTRYINDEX);
  78. if( *cb_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, *cb_ref);
  79. *cb_ref = ref;
  80. }
  81. // C callback for bringing WiFi back from forced sleep
  82. static void resume_cb(void){
  83. PMSLEEP_DBG("START");
  84. //TODO: Add support for extended light sleep duration
  85. wifi_fpm_close(); // Disable force sleep API
  86. PMSLEEP_DBG("WiFi resume");
  87. resume_all_timers();
  88. //this section restores null mode auto sleep setting
  89. if(autosleep_setting_temp == 1) {
  90. wifi_fpm_auto_sleep_set_in_null_mode(autosleep_setting_temp);
  91. autosleep_setting_temp = 0;
  92. }
  93. //this section restores previous wifi mode
  94. if (resume_opmode == STATION_MODE || resume_opmode == SOFTAP_MODE || resume_opmode == STATIONAP_MODE){
  95. if (wifi_set_opmode_current(resume_opmode)){
  96. if (resume_opmode == STATION_MODE || resume_opmode == STATIONAP_MODE){
  97. wifi_station_connect(); // Connect to currently configured Access Point
  98. }
  99. PMSLEEP_DBG("WiFi mode restored");
  100. resume_opmode = 0; // reset variable to default value
  101. }
  102. }
  103. else{
  104. wifi_set_opmode_current(NULL_MODE);
  105. }
  106. //execute the external resume callback
  107. if (user_resume_cb != NULL){
  108. PMSLEEP_DBG("calling user resume cb (%p)", user_resume_cb);
  109. user_resume_cb();
  110. user_resume_cb = NULL;
  111. }
  112. PMSLEEP_DBG("END");
  113. return;
  114. }
  115. // This callback executes the suspended callback when Wifi suspension is in effect
  116. static void wifi_suspended_timer_cb(int arg){
  117. // check if wifi is suspended.
  118. if (pmSleep_get_state() == PMSLEEP_SUSPENDED){
  119. os_timer_disarm(&wifi_suspended_test_timer); // Stop rf_closed_timer
  120. PMSLEEP_DBG("WiFi is suspended");
  121. //execute the external suspended callback
  122. if (user_suspend_cb != NULL){
  123. PMSLEEP_DBG("calling user suspend cb (%p)", user_suspend_cb);
  124. user_suspend_cb();
  125. user_suspend_cb = NULL;
  126. }
  127. PMSLEEP_DBG("END");
  128. }
  129. }
  130. /* EXTERNAL FUNCTIONS */
  131. //this function executes the application developer's Lua callback
  132. void pmSleep_execute_lua_cb(int* cb_ref){
  133. if (*cb_ref != LUA_NOREF){
  134. lua_State* L = lua_getstate(); // Get Lua main thread pointer
  135. lua_rawgeti(L, LUA_REGISTRYINDEX, *cb_ref); // Push resume callback onto the stack
  136. lua_unref(L, *cb_ref); // Remove resume callback from registry
  137. *cb_ref = LUA_NOREF; // Update variable since reference is no longer valid
  138. lua_call(L, 0, 0); // Execute resume callback
  139. }
  140. }
  141. //this function checks current wifi suspension state and returns the result
  142. uint8 pmSleep_get_state(void){
  143. if (fpm_rf_is_closed()) return PMSLEEP_SUSPENDED;
  144. else if (fpm_is_open()) return PMSLEEP_SUSPENSION_PENDING;
  145. else return PMSLEEP_AWAKE;
  146. }
  147. //this function parses the lua configuration table provided by the application developer
  148. 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){
  149. lua_Integer Linteger_tmp = 0;
  150. lua_getfield(L, table_idx, "duration");
  151. if( !lua_isnil(L, -1) ){ /* found? */
  152. if( lua_isnumber(L, -1) ){
  153. lua_Integer Linteger=luaL_checkinteger(L, -1);
  154. luaL_argcheck(L,(((Linteger >= PMSLEEP_SLEEP_MIN_TIME) && (Linteger <= PMSLEEP_SLEEP_MAX_TIME)) ||
  155. (Linteger == 0)), table_idx, PMSLEEP_DURATION_ERR_STR);
  156. cfg->sleep_duration = (uint32)Linteger; // Get suspend duration
  157. }
  158. else{
  159. return luaL_argerror( L, table_idx, "duration: must be number" );
  160. }
  161. }
  162. else{
  163. return luaL_argerror( L, table_idx, PMSLEEP_DURATION_ERR_STR );
  164. }
  165. lua_pop(L, 1);
  166. if( cfg->sleep_mode == MODEM_SLEEP_T ){ //WiFi suspend
  167. lua_getfield(L, table_idx, "suspend_cb");
  168. if( !lua_isnil(L, -1) ){ /* found? */
  169. if( lua_isfunction(L, -1) ){
  170. lua_pushvalue(L, -1); // Push resume callback to the top of the stack
  171. register_lua_cb(L, suspend_lua_cb_ref);
  172. }
  173. else{
  174. return luaL_argerror( L, table_idx, "suspend_cb: must be function" );
  175. }
  176. }
  177. lua_pop(L, 1);
  178. }
  179. else if (cfg->sleep_mode == LIGHT_SLEEP_T){ //CPU suspend
  180. #ifdef ENABLE_TIMER_SUSPEND
  181. lua_getfield(L, table_idx, "wake_pin");
  182. if( !lua_isnil(L, -1) ){ /* found? */
  183. if( lua_isnumber(L, -1) ){
  184. Linteger_tmp=lua_tointeger(L, -1);
  185. luaL_argcheck(L, (platform_gpio_exists(Linteger_tmp) && Linteger_tmp > 0), table_idx, "wake_pin: Invalid interrupt pin");
  186. cfg->wake_pin = Linteger_tmp;
  187. }
  188. else{
  189. return luaL_argerror( L, table_idx, "wake_pin: must be number" );
  190. }
  191. }
  192. else if(cfg->sleep_duration == 0){
  193. return luaL_argerror( L, table_idx, "wake_pin: must specify pin if sleep duration is indefinite" );
  194. }
  195. lua_pop(L, 1);
  196. lua_getfield(L, table_idx, "int_type");
  197. if( !lua_isnil(L, -1) ){ /* found? */
  198. if( lua_isnumber(L, -1) ){
  199. Linteger_tmp=lua_tointeger(L, -1);
  200. luaL_argcheck(L, (Linteger_tmp == GPIO_PIN_INTR_ANYEDGE || Linteger_tmp == GPIO_PIN_INTR_HILEVEL
  201. || Linteger_tmp == GPIO_PIN_INTR_LOLEVEL || Linteger_tmp == GPIO_PIN_INTR_NEGEDGE
  202. || Linteger_tmp == GPIO_PIN_INTR_POSEDGE ), 1, "int_type: invalid interrupt type");
  203. cfg->int_type = Linteger_tmp;
  204. }
  205. else{
  206. return luaL_argerror( L, table_idx, "int_type: must be number" );
  207. }
  208. }
  209. else{
  210. cfg->int_type = GPIO_PIN_INTR_LOLEVEL;
  211. }
  212. lua_pop(L, 1);
  213. #endif
  214. }
  215. else{
  216. return luaL_error(L, "FPM Sleep mode not available");
  217. }
  218. lua_getfield(L, table_idx, "resume_cb");
  219. if( !lua_isnil(L, -1) ){ /* found? */
  220. if( lua_isfunction(L, -1) ){
  221. lua_pushvalue(L, -1); // Push resume callback to the top of the stack
  222. register_lua_cb(L, resume_lua_cb_ref);
  223. }
  224. else{
  225. return luaL_argerror( L, table_idx, "resume_cb: must be function" );
  226. }
  227. }
  228. lua_pop(L, 1);
  229. lua_getfield(L, table_idx, "preserve_mode");
  230. if( !lua_isnil(L, -1) ){ /* found? */
  231. if( lua_isboolean(L, -1) ){
  232. cfg->preserve_opmode=lua_toboolean(L, -1);
  233. }
  234. else{
  235. return luaL_argerror( L, table_idx, "preseve_mode: must be boolean" );
  236. }
  237. }
  238. else{
  239. cfg->preserve_opmode = true;
  240. }
  241. lua_pop(L, 1);
  242. //if sleep duration is zero, set indefinite sleep duration
  243. if( cfg->sleep_duration == 0 ){
  244. cfg->sleep_duration = FPM_SLEEP_MAX_TIME;
  245. }
  246. return 0;
  247. }
  248. //This function resumes ESP from MODEM_SLEEP
  249. void pmSleep_resume(void (*resume_cb_ptr)(void)){
  250. PMSLEEP_DBG("START");
  251. uint8 fpm_state = pmSleep_get_state();
  252. if(fpm_state>0){
  253. if(resume_cb_ptr != NULL){
  254. user_resume_cb = resume_cb_ptr;
  255. }
  256. wifi_fpm_do_wakeup(); // Wake up from sleep
  257. resume_cb(); // Finish WiFi wakeup
  258. }
  259. PMSLEEP_DBG("END");
  260. return;
  261. }
  262. //this function puts the ESP8266 into MODEM_SLEEP or LIGHT_SLEEP
  263. void pmSleep_suspend(pmSleep_param_t *cfg){
  264. PMSLEEP_DBG("START");
  265. lua_State* L = lua_getstate();
  266. #ifndef ENABLE_TIMER_SUSPEND
  267. if(cfg->sleep_mode == LIGHT_SLEEP_T){
  268. luaL_error(L, "timer suspend API is disabled, light sleep unavailable");
  269. return;
  270. }
  271. #endif
  272. uint8 current_wifi_mode = wifi_get_opmode(); // Get Current WiFi mode
  273. user_resume_cb = cfg->resume_cb_ptr; //pointer to hold address of user_cb
  274. user_suspend_cb = cfg->suspend_cb_ptr; //pointer to hold address of user_cb
  275. // If Preserve_wifi_mode parameter is TRUE and current WiFi mode is not NULL_MODE
  276. if (cfg->preserve_opmode && current_wifi_mode != 0){
  277. resume_opmode = current_wifi_mode;
  278. }
  279. if (current_wifi_mode == STATION_MODE || current_wifi_mode == STATIONAP_MODE){
  280. wifi_station_disconnect(); // Disconnect from Access Point
  281. }
  282. //the null mode sleep functionality interferes with the forced sleep API and must be disabled
  283. if(get_fpm_auto_sleep_flag() == 1){
  284. autosleep_setting_temp = 1;
  285. wifi_fpm_auto_sleep_set_in_null_mode(0);
  286. }
  287. // If wifi_set_opmode_current is successful
  288. if (wifi_set_opmode_current(NULL_MODE)){
  289. PMSLEEP_DBG("sleep_mode is %s", cfg->sleep_mode == MODEM_SLEEP_T ? "MODEM_SLEEP_T" : "LIGHT_SLEEP_T");
  290. wifi_fpm_set_sleep_type(cfg->sleep_mode);
  291. wifi_fpm_open(); // Enable force sleep API
  292. if (cfg->sleep_mode == LIGHT_SLEEP_T){
  293. #ifdef ENABLE_TIMER_SUSPEND
  294. if(platform_gpio_exists(cfg->wake_pin) && cfg->wake_pin > 0){
  295. PMSLEEP_DBG("Wake-up pin is %d\t interrupt type is %d", cfg->wake_pin, cfg->int_type);
  296. if((cfg->int_type != GPIO_PIN_INTR_ANYEDGE && cfg->int_type != GPIO_PIN_INTR_HILEVEL
  297. && cfg->int_type != GPIO_PIN_INTR_LOLEVEL && cfg->int_type != GPIO_PIN_INTR_NEGEDGE
  298. && cfg->int_type != GPIO_PIN_INTR_POSEDGE )){
  299. wifi_fpm_close();
  300. PMSLEEP_DBG("Invalid interrupt type");
  301. return;
  302. }
  303. GPIO_DIS_OUTPUT(pin_num[cfg->wake_pin]);
  304. PIN_FUNC_SELECT(pin_mux[cfg->wake_pin], pin_func[cfg->wake_pin]);
  305. wifi_enable_gpio_wakeup(pin_num[cfg->wake_pin], cfg->int_type);
  306. }
  307. else if(cfg->sleep_duration == FPM_SLEEP_MAX_TIME && cfg->wake_pin == 255){
  308. wifi_fpm_close();
  309. PMSLEEP_DBG("No wake-up pin defined");
  310. return;
  311. }
  312. #endif
  313. }
  314. wifi_fpm_set_wakeup_cb(resume_cb); // Set resume C callback
  315. c_memcpy(&current_config, cfg, sizeof(pmSleep_param_t));
  316. PMSLEEP_DBG("sleep duration is %d", current_config.sleep_duration);
  317. //this timer intentionally bypasses the swtimer timer registration process
  318. ets_timer_disarm(&null_mode_check_timer);
  319. ets_timer_setfn(&null_mode_check_timer, null_mode_check_timer_cb, false);
  320. ets_timer_arm_new(&null_mode_check_timer, 1, 1, 1);
  321. }
  322. else{
  323. PMSLEEP_ERR("opmode change fail");
  324. }
  325. PMSLEEP_DBG("END");
  326. return;
  327. }
  328. #endif