Commit 96e5c026 authored by dnc40085's avatar dnc40085 Committed by Marcel Stör
Browse files

Refactor timer suspend portion of node.sleep (pmsleep) (#2287)

* pmsleep refactor
* Shortened swtmr disabled message 
* Added swtimer debug module option to user_modules.h.
* Added comments to user_config.h.
* Fixed error in documentation for node.sleep()
* remove blank sntp.c that got added in during rebase onto dev(6218b926)
* Added #ifdefs around SWTIMER_REG_CB to prevent inclusion of disabled
code
parent 4c3d501b
...@@ -46,8 +46,6 @@ SUBDIRS= \ ...@@ -46,8 +46,6 @@ SUBDIRS= \
fatfs \ fatfs \
esp-gdbstub \ esp-gdbstub \
websocket \ websocket \
swTimer \
misc \
pm \ pm \
sjson \ sjson \
sqlite3 \ sqlite3 \
...@@ -102,8 +100,6 @@ COMPONENTS_eagle.app.v6 = \ ...@@ -102,8 +100,6 @@ COMPONENTS_eagle.app.v6 = \
net/libnodemcu_net.a \ net/libnodemcu_net.a \
mbedtls/libmbedtls.a \ mbedtls/libmbedtls.a \
modules/libmodules.a \ modules/libmodules.a \
swTimer/libswtimer.a \
misc/libmisc.a \
sjson/libsjson.a \ sjson/libsjson.a \
sqlite3/libsqlite3.a \ sqlite3/libsqlite3.a \
......
#include "node.h" #include "node.h"
#include "coap_timer.h" #include "coap_timer.h"
#include "os_type.h" #include "os_type.h"
#include "pm/swtimer.h"
static os_timer_t coap_timer; static os_timer_t coap_timer;
static coap_tick_t basetime = 0; static coap_tick_t basetime = 0;
...@@ -48,6 +49,8 @@ void coap_timer_tick(void *arg){ ...@@ -48,6 +49,8 @@ void coap_timer_tick(void *arg){
void coap_timer_setup(coap_queue_t ** queue, coap_tick_t t){ void coap_timer_setup(coap_queue_t ** queue, coap_tick_t t){
os_timer_disarm(&coap_timer); os_timer_disarm(&coap_timer);
os_timer_setfn(&coap_timer, (os_timer_func_t *)coap_timer_tick, queue); os_timer_setfn(&coap_timer, (os_timer_func_t *)coap_timer_tick, queue);
SWTIMER_REG_CB(coap_timer_tick, SWTIMER_RESUME);
//coap_timer_tick processes a queue, my guess is that it is ok to resume the timer from where it left off
os_timer_arm(&coap_timer, t, 0); // no repeat os_timer_arm(&coap_timer, t, 0); // no repeat
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "mem.h" #include "mem.h"
#include "gpio.h" #include "gpio.h"
#include "user_interface.h" #include "user_interface.h"
#include "pm/swtimer.h"
#include "driver/key.h" #include "driver/key.h"
...@@ -148,6 +149,8 @@ key_intr_handler(void *arg) ...@@ -148,6 +149,8 @@ key_intr_handler(void *arg)
// 5s, restart & enter softap mode // 5s, restart & enter softap mode
os_timer_disarm(&keys->single_key[i]->key_5s); os_timer_disarm(&keys->single_key[i]->key_5s);
os_timer_setfn(&keys->single_key[i]->key_5s, (os_timer_func_t *)key_5s_cb, keys->single_key[i]); os_timer_setfn(&keys->single_key[i]->key_5s, (os_timer_func_t *)key_5s_cb, keys->single_key[i]);
SWTIMER_REG_CB(key_5s_cb, SWTIMER_DROP);
// key_5s_cb checks the state of a gpio. After resume, gpio state would be invalid
os_timer_arm(&keys->single_key[i]->key_5s, 5000, 0); os_timer_arm(&keys->single_key[i]->key_5s, 5000, 0);
keys->single_key[i]->key_level = 0; keys->single_key[i]->key_level = 0;
gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_POSEDGE); gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_POSEDGE);
...@@ -155,6 +158,8 @@ key_intr_handler(void *arg) ...@@ -155,6 +158,8 @@ key_intr_handler(void *arg)
// 50ms, check if this is a real key up // 50ms, check if this is a real key up
os_timer_disarm(&keys->single_key[i]->key_50ms); os_timer_disarm(&keys->single_key[i]->key_50ms);
os_timer_setfn(&keys->single_key[i]->key_50ms, (os_timer_func_t *)key_50ms_cb, keys->single_key[i]); os_timer_setfn(&keys->single_key[i]->key_50ms, (os_timer_func_t *)key_50ms_cb, keys->single_key[i]);
SWTIMER_REG_CB(key_50ms_cb, SWTIMER_DROP);
// key_50ms_cb checks the state of a gpio. After resume, gpio state would be invalid
os_timer_arm(&keys->single_key[i]->key_50ms, 50, 0); os_timer_arm(&keys->single_key[i]->key_50ms, 50, 0);
} }
} }
......
...@@ -527,6 +527,7 @@ void spi_slave_init(uint8 spi_no) ...@@ -527,6 +527,7 @@ void spi_slave_init(uint8 spi_no)
#ifdef SPI_SLAVE_DEBUG #ifdef SPI_SLAVE_DEBUG
#include "pm/swtimer.h"
/****************************************************************************** /******************************************************************************
* FunctionName : hspi_master_readwrite_repeat * FunctionName : hspi_master_readwrite_repeat
* Description : SPI master test function for reading and writing esp8266 slave buffer, * Description : SPI master test function for reading and writing esp8266 slave buffer,
...@@ -545,6 +546,8 @@ void hspi_master_readwrite_repeat(void) ...@@ -545,6 +546,8 @@ void hspi_master_readwrite_repeat(void)
temp++; temp++;
spi_byte_write_espslave(SPI_HSPI,temp); spi_byte_write_espslave(SPI_HSPI,temp);
os_timer_setfn(&timer2, (os_timer_func_t *)hspi_master_readwrite_repeat, NULL); os_timer_setfn(&timer2, (os_timer_func_t *)hspi_master_readwrite_repeat, NULL);
SWTIMER_REGISTER_CB_PTR(hspi_master_readwrite_repeat, SWTIMER_RESUME);
//hspi_master_readwrite_repeat timer will be resumed on wake up, maybe data will still be in buffer?
os_timer_arm(&timer2, 500, 0); os_timer_arm(&timer2, 500, 0);
} }
#endif #endif
......
...@@ -323,11 +323,14 @@ uart_autobaud_timeout(void *timer_arg) ...@@ -323,11 +323,14 @@ uart_autobaud_timeout(void *timer_arg)
uart_div_modify(uart_no, divisor); uart_div_modify(uart_no, divisor);
} }
} }
#include "pm/swtimer.h"
static void static void
uart_init_autobaud(uint32_t uart_no) uart_init_autobaud(uint32_t uart_no)
{ {
os_timer_setfn(&autobaud_timer, uart_autobaud_timeout, (void *) uart_no); os_timer_setfn(&autobaud_timer, uart_autobaud_timeout, (void *) uart_no);
SWTIMER_REG_CB(uart_autobaud_timeout, SWTIMER_DROP);
//if autobaud hasn't done it's thing by the time light sleep triggered, it probably isn't going to happen.
os_timer_arm(&autobaud_timer, 100, TRUE); os_timer_arm(&autobaud_timer, 100, TRUE);
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "limits.h" #include "limits.h"
#include "httpclient.h" #include "httpclient.h"
#include "stdlib.h" #include "stdlib.h"
#include "pm/swtimer.h"
#define REDIRECTION_FOLLOW_MAX 20 #define REDIRECTION_FOLLOW_MAX 20
...@@ -525,6 +526,8 @@ static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_ ...@@ -525,6 +526,8 @@ static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_
/* Set connection timeout timer */ /* Set connection timeout timer */
os_timer_disarm( &(req->timeout_timer) ); os_timer_disarm( &(req->timeout_timer) );
os_timer_setfn( &(req->timeout_timer), (os_timer_func_t *) http_timeout_callback, conn ); os_timer_setfn( &(req->timeout_timer), (os_timer_func_t *) http_timeout_callback, conn );
SWTIMER_REG_CB(http_timeout_callback, SWTIMER_IMMEDIATE);
//http_timeout_callback frees memory used by this function and timer cannot be dropped
os_timer_arm( &(req->timeout_timer), req->timeout, false ); os_timer_arm( &(req->timeout_timer), req->timeout, false );
#ifdef CLIENT_SSL_ENABLE #ifdef CLIENT_SSL_ENABLE
......
#ifndef __DYNARR_H__
#define __DYNARR_H__
#include "user_interface.h"
#include "c_stdio.h"
#include "c_stdlib.h"
//#define DYNARR_DEBUG
//#define DYNARR_ERROR
typedef struct _dynarr{
void* data_ptr;
size_t used;
size_t array_size;
size_t data_size;
} dynarr_t;
bool dynarr_init(dynarr_t* array_ptr, size_t array_size, size_t data_size);
bool dynarr_resize(dynarr_t* array_ptr, size_t elements_to_add);
bool dynarr_remove(dynarr_t* array_ptr, void* element_ptr);
bool dynarr_add(dynarr_t* array_ptr, void* data_ptr, size_t data_size);
bool dynarr_boundaryCheck(dynarr_t* array_ptr, void* element_ptr);
bool dynarr_free(dynarr_t* array_ptr);
#if 0 || defined(DYNARR_DEBUG) || defined(NODE_DEBUG)
#define DYNARR_DBG(fmt, ...) c_printf("\n DYNARR_DBG(%s): "fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#else
#define DYNARR_DBG(...)
#endif
#if 0 || defined(DYNARR_ERROR) || defined(NODE_ERROR)
#define DYNARR_ERR(fmt, ...) c_printf("\n DYNARR: "fmt"\n", ##__VA_ARGS__)
#else
#define DYNARR_ERR(...)
#endif
#endif // __DYNARR_H__
/*
* swtimer.h
*
* Created on: Aug 4, 2017
* Author: anonymous
*/
#ifndef APP_INCLUDE_PM_SWTIMER_H_
#define APP_INCLUDE_PM_SWTIMER_H_
void swtmr_cb_register(void* timer_cb_ptr, uint8 suspend_policy);
#define SWTIMER_RESUME 0 //save remaining time
#define SWTIMER_RESTART 1 //use timer_period as remaining time
#define SWTIMER_IMMEDIATE 2 //fire timer immediately after resume
#define SWTIMER_DROP 3 //disarm timer, do not resume
#if defined(TIMER_SUSPEND_ENABLE)
#define SWTIMER_REG_CB(cb_ptr, suspend_policy) do{ \
static bool cb_ptr##_registered_flag;\
if(!cb_ptr##_registered_flag){ \
cb_ptr##_registered_flag = true; \
swtmr_cb_register(cb_ptr, suspend_policy);\
} \
}while(0);
#else
#define SWTIMER_REG_CB(...)
#endif
#endif /* APP_INCLUDE_PM_SWTIMER_H_ */
...@@ -689,11 +689,15 @@ static inline void rtc_time_switch_to_system_clock(void) ...@@ -689,11 +689,15 @@ static inline void rtc_time_switch_to_system_clock(void)
static inline void rtc_time_tmrfn(void* arg); static inline void rtc_time_tmrfn(void* arg);
#include "pm/swtimer.h"
static void rtc_time_install_timer(void) static void rtc_time_install_timer(void)
{ {
static ETSTimer tmr; static ETSTimer tmr;
os_timer_setfn(&tmr,rtc_time_tmrfn,NULL); os_timer_setfn(&tmr,rtc_time_tmrfn,NULL);
SWTIMER_REG_CB(rtc_time_tmrfn, SWTIMER_RESUME);
//I believe the function rtc_time_tmrfn compensates for drift in the clock and updates rtc time accordingly, This timer should probably be resumed
os_timer_arm(&tmr,10000,1); os_timer_arm(&tmr,10000,1);
} }
......
#ifndef __SW_TIMER_H__
#define __SW_TIMER_H__
#include "user_interface.h"
//#define SWTMR_DEBUG
#define USE_SWTMR_ERROR_STRINGS
#if defined(DEVELOP_VERSION)
#define SWTMR_DEBUG
#endif
#if defined(SWTMR_DEBUG)
#define SWTMR_DBG(fmt, ...) dbg_printf("\tSWTIMER(%s):"fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#else
#define SWTMR_DBG(...)
#endif
#if defined(NODE_ERROR)
#define SWTMR_ERR(fmt, ...) NODE_ERR("%s"fmt"\n", "SWTIMER:", ##__VA_ARGS__)
#else
#define SWTMR_ERR(...)
#endif
enum SWTMR_STATUS{
SWTMR_FAIL = 0,
SWTMR_OK = 1,
SWTMR_MALLOC_FAIL = 10,
SWTMR_TIMER_NOT_ARMED,
// SWTMR_NULL_PTR,
SWTMR_REGISTRY_NO_REGISTERED_TIMERS,
// SWTMR_SUSPEND_ARRAY_INITIALIZATION_FAILED,
// SWTMR_SUSPEND_ARRAY_ADD_FAILED,
// SWTMR_SUSPEND_ARRAY_REMOVE_FAILED,
SWTMR_SUSPEND_TIMER_ALREADY_SUSPENDED,
SWTMR_SUSPEND_TIMER_ALREADY_REARMED,
SWTMR_SUSPEND_NO_SUSPENDED_TIMERS,
SWTMR_SUSPEND_TIMER_NOT_SUSPENDED,
};
/* Global Function Declarations */
void swtmr_register(void* timer_ptr);
void swtmr_unregister(void* timer_ptr);
int swtmr_suspend(os_timer_t* timer_ptr);
int swtmr_resume(os_timer_t* timer_ptr);
void swtmr_print_registry(void);
void swtmr_print_suspended(void);
void swtmr_print_timer_list(void);
const char* swtmr_errorcode2str(int error_value);
bool swtmr_suspended_test(os_timer_t* timer_ptr);
#endif // __SW_TIMER_H__
...@@ -116,8 +116,8 @@ extern void luaL_assertfail(const char *file, int line, const char *message); ...@@ -116,8 +116,8 @@ extern void luaL_assertfail(const char *file, int line, const char *message);
#define WIFI_SDK_EVENT_MONITOR_ENABLE #define WIFI_SDK_EVENT_MONITOR_ENABLE
#define WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE #define WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE
////#define ENABLE_TIMER_SUSPEND //#define PMSLEEP_ENABLE // Enable wifi.suspend() and node.sleep() (NOTE: node.sleep() is dependent on TIMER_SUSPEND_ENABLE)
//#define PMSLEEP_ENABLE //#define TIMER_SUSPEND_ENABLE //Required by node.sleep()
#define STRBUF_DEFAULT_INCREMENT 32 #define STRBUF_DEFAULT_INCREMENT 32
......
...@@ -86,5 +86,9 @@ ...@@ -86,5 +86,9 @@
//#define LUA_USE_MODULES_WS2812_EFFECTS //#define LUA_USE_MODULES_WS2812_EFFECTS
//#define LUA_USE_MODULES_XPT2046 //#define LUA_USE_MODULES_XPT2046
//debug modules
//#define LUA_USE_MODULES_SWTMR_DBG //SWTMR timer suspend Debug functions
#endif /* LUA_CROSS_COMPILER */ #endif /* LUA_CROSS_COMPILER */
#endif /* __USER_MODULES_H__ */ #endif /* __USER_MODULES_H__ */
...@@ -1039,6 +1039,7 @@ mdns_reg(struct mdns_info *info) { ...@@ -1039,6 +1039,7 @@ mdns_reg(struct mdns_info *info) {
os_timer_disarm(&mdns_timer); os_timer_disarm(&mdns_timer);
} }
} }
#include "pm/swtimer.h"
/** /**
* Initialize the resolver: set up the UDP pcb and configure the default server * Initialize the resolver: set up the UDP pcb and configure the default server
...@@ -1129,6 +1130,8 @@ mdns_init(struct mdns_info *info) { ...@@ -1129,6 +1130,8 @@ mdns_init(struct mdns_info *info) {
os_timer_disarm(&mdns_timer); os_timer_disarm(&mdns_timer);
os_timer_setfn(&mdns_timer, (os_timer_func_t *)mdns_reg,ms_info); os_timer_setfn(&mdns_timer, (os_timer_func_t *)mdns_reg,ms_info);
SWTIMER_REG_CB(mdns_reg, SWTIMER_RESTART);
//going on the above comment, it's probably a good idea to let mdns_reg run it's course. not sure if the 1 second timing is important, so lets restart it to be safe.
os_timer_arm(&mdns_timer, 1000, 1); os_timer_arm(&mdns_timer, 1000, 1);
} }
} }
......
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
GEN_LIBS = libmisc.a
endif
STD_CFLAGS=-std=gnu11 -Wimplicit
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
INCLUDES += -I ./include
INCLUDES += -I ../include
INCLUDES += -I ../../include
INCLUDES += -I ../lua
INCLUDES += -I ../platform
INCLUDES += -I ../libc
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
#include "misc/dynarr.h"
#define ARRAY_PTR_CHECK if(array_ptr == NULL || array_ptr->data_ptr == NULL){\
/**/DYNARR_DBG("array not initialized");\
return false; \
}
bool dynarr_init(dynarr_t* array_ptr, size_t array_size, size_t data_size){
if(array_ptr == NULL || data_size == 0 || array_size == 0){
/**/DYNARR_DBG("Invalid parameter: array_ptr(%p) data_size(%u) array_size(%u)", array_ptr, data_size, array_size);
return false;
}
if(array_ptr->data_ptr != NULL ){
/**/DYNARR_DBG("Array already initialized: array_ptr->data_ptr=%p", array_ptr->data_ptr);
return false;
}
/**/DYNARR_DBG("Array parameters:\n\t\t\tarray_size(%u)\n\t\t\tdata_size(%u)\n\t\t\ttotal size(bytes):%u", array_size, data_size, (array_size * data_size));
void* temp_array = c_zalloc(array_size * data_size);
if(temp_array == NULL){
/**/DYNARR_ERR("malloc FAIL! req:%u free:%u", (array_size * data_size), system_get_free_heap_size());
return false;
}
array_ptr->data_ptr = temp_array;
array_ptr->array_size = array_size;
array_ptr->data_size = data_size;
array_ptr->used = 0;
return true;
}
bool dynarr_resize(dynarr_t* array_ptr, size_t elements_to_add){
ARRAY_PTR_CHECK;
if(elements_to_add <= 0){
/**/DYNARR_DBG("Invalid qty: elements_to_add=%u", elements_to_add);
return false;
}
size_t new_array_size = array_ptr->array_size + elements_to_add;
/**/DYNARR_DBG("old size=%u\tnew size=%u\tmem used=%u",
array_ptr->array_size, new_array_size, (new_array_size * array_ptr->data_size));
void* temp_array_p = c_realloc(array_ptr->data_ptr, new_array_size * array_ptr->data_size);
if(temp_array_p == NULL){
/**/DYNARR_ERR("malloc FAIL! req:%u free:%u", (new_array_size * array_ptr->data_size), system_get_free_heap_size());
return false;
}
array_ptr->data_ptr = temp_array_p;
size_t prev_size = array_ptr->array_size;
array_ptr->array_size = new_array_size;
//set memory to 0 for newly added array elements
memset((uint8*) array_ptr->data_ptr + (prev_size * array_ptr->data_size), 0, (elements_to_add * array_ptr->data_size));
/**/DYNARR_DBG("Array successfully resized");
return true;
}
bool dynarr_remove(dynarr_t* array_ptr, void* element_to_remove){
ARRAY_PTR_CHECK;
uint8* element_ptr = element_to_remove;
uint8* data_ptr = array_ptr->data_ptr;
if(dynarr_boundaryCheck(array_ptr, element_to_remove) == FALSE){
return false;
}
//overwrite element to be removed by shifting all elements to the left
memmove(element_ptr, element_ptr + array_ptr->data_size, (array_ptr->array_size - 1) * array_ptr->data_size - (element_ptr - data_ptr));
//clear newly freed element
memset(data_ptr + ((array_ptr->array_size-1) * array_ptr->data_size), 0, array_ptr->data_size);
//decrement array used since we removed an element
array_ptr->used--;
/**/DYNARR_DBG("element(%p) removed from array", element_ptr);
return true;
}
bool dynarr_add(dynarr_t* array_ptr, void* data_ptr, size_t data_size){
ARRAY_PTR_CHECK;
if(data_size != array_ptr->data_size){
/**/DYNARR_DBG("Invalid data size: data_size(%u) != arr->data_size(%u)", data_size, array_ptr->data_size);
return false;
}
if(array_ptr->array_size == array_ptr->used){
if(!dynarr_resize(array_ptr, (array_ptr->array_size/2))){
return false;
}
}
memcpy(((uint8*)array_ptr->data_ptr + (array_ptr->used * array_ptr->data_size)), data_ptr, array_ptr->data_size);
array_ptr->used++;
return true;
}
bool dynarr_boundaryCheck(dynarr_t* array_ptr, void* element_to_check){
ARRAY_PTR_CHECK;
uint8* data_ptr = array_ptr->data_ptr;
uint8* element_ptr = element_to_check;
if(element_ptr < data_ptr || ((element_ptr - data_ptr) / array_ptr->data_size) > array_ptr->array_size - 1){
/**/DYNARR_DBG("element_ptr(%p) out of bounds: first element ptr:%p last element ptr:%p",
element_ptr, data_ptr, data_ptr + ((array_ptr->array_size - 1) * array_ptr->data_size));
return false;
}
return true;
}
bool dynarr_free(dynarr_t* array_ptr){
ARRAY_PTR_CHECK;
c_free(array_ptr->data_ptr);
array_ptr->data_ptr=NULL;
array_ptr->array_size = array_ptr->used = 0;
/**/DYNARR_DBG("array freed");
return true;
}
...@@ -190,7 +190,7 @@ static void cron_handle_tmr() { ...@@ -190,7 +190,7 @@ static void cron_handle_tmr() {
struct rtc_timeval tv; struct rtc_timeval tv;
rtctime_gettimeofday(&tv); rtctime_gettimeofday(&tv);
if (tv.tv_sec == 0) { // Wait for RTC time if (tv.tv_sec == 0) { // Wait for RTC time
ets_timer_arm_new(&cron_timer, 1000, 0, 1); os_timer_arm(&cron_timer, 1000, 0);
return; return;
} }
time_t t = tv.tv_sec; time_t t = tv.tv_sec;
...@@ -202,7 +202,7 @@ static void cron_handle_tmr() { ...@@ -202,7 +202,7 @@ static void cron_handle_tmr() {
diff += 60000; diff += 60000;
gmtime_r(&t, &tm); gmtime_r(&t, &tm);
} }
ets_timer_arm_new(&cron_timer, diff, 0, 1); os_timer_arm(&cron_timer, diff, 0);
cron_handle_time(tm.tm_mon + 1, tm.tm_mday, tm.tm_wday, tm.tm_hour, tm.tm_min); cron_handle_time(tm.tm_mon + 1, tm.tm_mday, tm.tm_wday, tm.tm_hour, tm.tm_min);
} }
...@@ -220,11 +220,15 @@ static const LUA_REG_TYPE cron_map[] = { ...@@ -220,11 +220,15 @@ static const LUA_REG_TYPE cron_map[] = {
{ LSTRKEY( "reset" ), LFUNCVAL( lcron_reset ) }, { LSTRKEY( "reset" ), LFUNCVAL( lcron_reset ) },
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
}; };
#include "pm/swtimer.h"
int luaopen_cron( lua_State *L ) { int luaopen_cron( lua_State *L ) {
ets_timer_disarm(&cron_timer); os_timer_disarm(&cron_timer);
ets_timer_setfn(&cron_timer, cron_handle_tmr, 0); os_timer_setfn(&cron_timer, cron_handle_tmr, 0);
ets_timer_arm_new(&cron_timer, 1000, 0, 1); SWTIMER_REG_CB(cron_handle_tmr, SWTIMER_RESTART);
//cron_handle_tmr determines when to execute a scheduled cron job
//My guess: To be sure to give the other modules required by cron enough time to get to a ready state, restart cron_timer.
os_timer_arm(&cron_timer, 1000, 0);
luaL_rometatable(L, "cron.entry", (void *)cronent_map); luaL_rometatable(L, "cron.entry", (void *)cronent_map);
return 0; return 0;
} }
......
...@@ -134,6 +134,8 @@ static int ds18b20_lua_setting(lua_State *L) { ...@@ -134,6 +134,8 @@ static int ds18b20_lua_setting(lua_State *L) {
return 0; return 0;
} }
#include "pm/swtimer.h"
// Reads sensor values from all devices // Reads sensor values from all devices
// Lua: ds18b20.read(function(INDEX, ROM, RES, TEMP, TEMP_DEC, PAR) print(INDEX, ROM, RES, TEMP, TEMP_DEC, PAR) end, ROM[, FAMILY]) // Lua: ds18b20.read(function(INDEX, ROM, RES, TEMP, TEMP_DEC, PAR) print(INDEX, ROM, RES, TEMP, TEMP_DEC, PAR) end, ROM[, FAMILY])
static int ds18b20_lua_read(lua_State *L) { static int ds18b20_lua_read(lua_State *L) {
...@@ -173,6 +175,9 @@ static int ds18b20_lua_read(lua_State *L) { ...@@ -173,6 +175,9 @@ static int ds18b20_lua_read(lua_State *L) {
onewire_write(ds18b20_bus_pin, DS18B20_ROM_SKIP, 0); onewire_write(ds18b20_bus_pin, DS18B20_ROM_SKIP, 0);
onewire_write(ds18b20_bus_pin, DS18B20_FUNC_CONVERT, 1); onewire_write(ds18b20_bus_pin, DS18B20_FUNC_CONVERT, 1);
os_timer_setfn(&ds18b20_timer, (os_timer_func_t *)ds18b20_lua_readoutdone, NULL); os_timer_setfn(&ds18b20_timer, (os_timer_func_t *)ds18b20_lua_readoutdone, NULL);
SWTIMER_REG_CB(ds18b20_lua_readoutdone, SWTIMER_DROP);
//The function ds18b20_lua_readoutdone reads the temperature from the sensor(s) after a set amount of time depending on temperature resolution
//MY guess: If this timer manages to get suspended before it fires and the temperature data is time sensitive then resulting data would be invalid and should be discarded
switch (ds18b20_device_res) { switch (ds18b20_device_res) {
case (9): case (9):
......
...@@ -212,12 +212,15 @@ static void enduser_setup_connected_callback() ...@@ -212,12 +212,15 @@ static void enduser_setup_connected_callback()
} }
} }
#include "pm/swtimer.h"
static void enduser_setup_check_station_start(void) static void enduser_setup_check_station_start(void)
{ {
ENDUSER_SETUP_DEBUG("enduser_setup_check_station_start"); ENDUSER_SETUP_DEBUG("enduser_setup_check_station_start");
os_timer_setfn(&(state->check_station_timer), enduser_setup_check_station, NULL); os_timer_setfn(&(state->check_station_timer), enduser_setup_check_station, NULL);
SWTIMER_REG_CB(enduser_setup_check_station, SWTIMER_RESUME);
//The function enduser_setup_check_station checks for a successful connection to the configured AP
//My guess: I'm not sure about whether or not user feedback is given via the web interface, but I don't see a problem with letting this timer resume.
os_timer_arm(&(state->check_station_timer), 3*1000, TRUE); os_timer_arm(&(state->check_station_timer), 3*1000, TRUE);
} }
...@@ -317,6 +320,9 @@ static void enduser_setup_check_station(void *p) ...@@ -317,6 +320,9 @@ static void enduser_setup_check_station(void *p)
if (!manual) if (!manual)
{ {
os_timer_setfn(&(state->shutdown_timer), enduser_setup_stop_callback, NULL); os_timer_setfn(&(state->shutdown_timer), enduser_setup_stop_callback, NULL);
SWTIMER_REG_CB(enduser_setup_stop_callback, SWTIMER_RESUME);
//The function enduser_setup_stop_callback frees services and resources used by enduser setup.
//My guess: Since it would lead to a memory leak, it's probably best to resume this timer.
os_timer_arm(&(state->shutdown_timer), 10*1000, FALSE); os_timer_arm(&(state->shutdown_timer), 10*1000, FALSE);
} }
} }
......
...@@ -981,6 +981,7 @@ static sint8 socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) ...@@ -981,6 +981,7 @@ static sint8 socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
return espconn_status; return espconn_status;
} }
#include "pm/swtimer.h"
// Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client), function(client, connect_return_code) ) // Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client), function(client, connect_return_code) )
static int mqtt_socket_connect( lua_State* L ) static int mqtt_socket_connect( lua_State* L )
{ {
...@@ -1114,6 +1115,9 @@ static int mqtt_socket_connect( lua_State* L ) ...@@ -1114,6 +1115,9 @@ static int mqtt_socket_connect( lua_State* L )
os_timer_disarm(&mud->mqttTimer); os_timer_disarm(&mud->mqttTimer);
os_timer_setfn(&mud->mqttTimer, (os_timer_func_t *)mqtt_socket_timer, mud); os_timer_setfn(&mud->mqttTimer, (os_timer_func_t *)mqtt_socket_timer, mud);
SWTIMER_REG_CB(mqtt_socket_timer, SWTIMER_RESUME);
//I assume that mqtt_socket_timer connects to the mqtt server, but I'm not really sure what impact light_sleep will have on it.
//My guess: If in doubt, resume the timer
// timer started in socket_connect() // timer started in socket_connect()
if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0)) if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment