Skip to content

Commit

Permalink
Exposed forced sleep API and more
Browse files Browse the repository at this point in the history
Added timer suspend functionality

Added functions:
* wifi.suspend
* wifi.resume
* node.sleep 
* tmr.suspend
* tmr.suspend_all
* tmr.resume
* tmr.resume_all
  • Loading branch information
dnc40085 committed Jan 25, 2017
1 parent 4e3560d commit dda240c
Show file tree
Hide file tree
Showing 18 changed files with 1,091 additions and 195 deletions.
2 changes: 2 additions & 0 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SUBDIRS= \
websocket \
swTimer \
misc \
pm \


endif # } PDIR
Expand Down Expand Up @@ -95,6 +96,7 @@ COMPONENTS_eagle.app.v6 = \
dhtlib/libdhtlib.a \
tsl2561/tsl2561lib.a \
http/libhttp.a \
pm/libpm.a \
websocket/libwebsocket.a \
esp-gdbstub/libgdbstub.a \
net/libnodemcu_net.a \
Expand Down
15 changes: 6 additions & 9 deletions app/include/swTimer/swTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#define SWTMR_DEBUG
#endif


#define SWTMR_DBG(fmt, ...) c_printf("\n SWTMR_DBG(%s):"fmt"\n", __FUNCTION__, ##__VA_ARGS__)
#else
#define SWTMR_DBG(...)
#endif

#if defined(SWTMR_ERROR) || defined(NODE_ERROR)
#if (defined(SWTMR_ERROR) || defined(NODE_ERROR))
#define SWTMR_ERR(fmt, ...) c_printf("\n SWTMR:"fmt"\n", ##__VA_ARGS__)
#else
#define SWTMR_DBG(...)
#define SWTMR_ERR(...)
#endif

enum SWTMR_STATUS{
Expand All @@ -40,13 +39,11 @@ enum SWTMR_STATUS{

};



/* Global Function Declarations */
void sw_timer_register(void* timer_ptr);
void sw_timer_unregister(void* timer_ptr);
int sw_timer_suspend(os_timer_t* timer_ptr);
int sw_timer_resume(os_timer_t* timer_ptr);
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);
Expand Down
4 changes: 4 additions & 0 deletions app/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ extern void luaL_assertfail(const char *file, int line, const char *message);
#define WIFI_SDK_EVENT_MONITOR_ENABLE
#define WIFI_EVENT_MONITOR_DISCONNECT_REASON_LIST_ENABLE

#define ENABLE_TIMER_SUSPEND
#define PMSLEEP_ENABLE


#define STRBUF_DEFAULT_INCREMENT 32

#endif /* __USER_CONFIG_H__ */
1 change: 1 addition & 0 deletions app/modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ INCLUDES += -I ../dhtlib
INCLUDES += -I ../fatfs
INCLUDES += -I ../http
INCLUDES += -I ../websocket
INCLUDES += -I ../pm
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile

64 changes: 34 additions & 30 deletions app/modules/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,36 @@ static int node_deepsleep( lua_State* L )
return 0;
}

// Lua: dsleep_set_options
// Combined to dsleep( us, option )
// static int node_deepsleep_setoption( lua_State* L )
// {
// s32 option;
// option = luaL_checkinteger( L, 1 );
// if ( option < 0 || option > 4)
// return luaL_error( L, "wrong arg range" );
// else
// deep_sleep_set_option( option );
// return 0;
// }
// Lua: info()

#ifdef PMSLEEP_ENABLE
#include "pmSleep.h"

int node_sleep_resume_cb_ref= LUA_NOREF;
void node_sleep_resume_cb(void)
{
PMSLEEP_DBG("START");
pmSleep_execute_lua_cb(&node_sleep_resume_cb_ref);
PMSLEEP_DBG("END");
}

// Lua: node.sleep(table)
static int node_sleep( lua_State* L )
{
pmSleep_INIT_CFG(cfg);
cfg.sleep_mode=LIGHT_SLEEP_T;

if(lua_istable(L, 1)){
pmSleep_parse_table_lua(L, 1, &cfg, NULL, &node_sleep_resume_cb_ref);
}
else{
return luaL_argerror(L, 1, "must be table");
}

cfg.resume_cb_ptr = &node_sleep_resume_cb;
pmSleep_suspend(&cfg);
return 0;
}
#endif //PMSLEEP_ENABLE

static int node_info( lua_State* L )
{
Expand Down Expand Up @@ -376,21 +393,6 @@ static int node_restore (lua_State *L)
return 0;
}

#include "swTimer/swTimer.h"

static int node_suspend_timers (lua_State *L)
{
lua_pushnumber(L, sw_timer_suspend(NULL));
return 1;
}

static int node_resume_timers (lua_State *L)
{
lua_pushnumber(L, sw_timer_resume(NULL));
return 1;
}


#ifdef LUA_OPTIMIZE_DEBUG
/* node.stripdebug([level[, function]]). 
* level: 1 don't discard debug
Expand Down Expand Up @@ -577,6 +579,10 @@ static const LUA_REG_TYPE node_map[] =
{
{ LSTRKEY( "restart" ), LFUNCVAL( node_restart ) },
{ LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) },
#ifdef PMSLEEP_ENABLE
{ LSTRKEY( "sleep" ), LFUNCVAL( node_sleep ) },
PMSLEEP_INT_MAP,
#endif
{ LSTRKEY( "info" ), LFUNCVAL( node_info ) },
{ LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) },
{ LSTRKEY( "flashid" ), LFUNCVAL( node_flashid ) },
Expand All @@ -592,8 +598,6 @@ static const LUA_REG_TYPE node_map[] =
{ LSTRKEY( "setcpufreq" ), LFUNCVAL( node_setcpufreq) },
{ LSTRKEY( "bootreason" ), LFUNCVAL( node_bootreason) },
{ LSTRKEY( "restore" ), LFUNCVAL( node_restore) },
{ LSTRKEY( "suspendTimers" ), LFUNCVAL( node_suspend_timers ) },
{ LSTRKEY( "resumeTimers" ), LFUNCVAL( node_resume_timers ) },
{ LSTRKEY( "random" ), LFUNCVAL( node_random) },
#ifdef LUA_OPTIMIZE_DEBUG
{ LSTRKEY( "stripdebug" ), LFUNCVAL( node_stripdebug ) },
Expand Down
82 changes: 75 additions & 7 deletions app/modules/tmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,21 @@ static int tmr_stop(lua_State* L){
return 1;
}

#ifdef ENABLE_TIMER_SUSPEND

static int tmr_suspend(lua_State* L){
timer_t tmr = tmr_get(L, 1);

if((tmr->mode & TIMER_IDLE_FLAG) == 1){
return luaL_error(L, "timer not armed");
}

int retval = sw_timer_suspend(&tmr->os);
int retval = swtmr_suspend(&tmr->os);

if(retval != SWTMR_OK){
return luaL_error(L, swtmr_errorcode2str(retval));
}
else{
// c_printf("tmr->mode & TIMER_SUSPEND_FLAG == %s",(tmr->mode & TIMER_SUSPEND_FLAG) ? "true" : "false");
lua_pushboolean(L, true);
}

Expand All @@ -258,19 +259,45 @@ static int tmr_resume(lua_State* L){
return luaL_error(L, "timer not suspended");
}

int retval = sw_timer_resume(&tmr->os);
int retval = swtmr_resume(&tmr->os);

if(retval != SWTMR_OK){
return luaL_error(L, swtmr_errorcode2str(retval));
}
else{
lua_pushboolean(L, true);
}
return 1;
}

static int tmr_suspend_all (lua_State *L)
{
sint32 retval = swtmr_suspend(NULL);
// lua_pushnumber(L, swtmr_suspend(NULL));
if(retval!=SWTMR_OK){
return luaL_error(L, swtmr_errorcode2str(retval));
}
else{
lua_pushboolean(L, true);
}
return 1;
}

// c_printf("tmr->mode & TIMER_SUSPEND_FLAG == %s",(tmr->mode & TIMER_SUSPEND_FLAG) ? "true" : "false");
static int tmr_resume_all (lua_State *L)
{
sint32 retval = swtmr_resume(NULL);
if(retval!=SWTMR_OK){
return luaL_error(L, swtmr_errorcode2str(retval));
}
else{
lua_pushboolean(L, true);
}
return 1;
}


#endif

// Lua: tmr.unregister( id / ref )
static int tmr_unregister(lua_State* L){
timer_t tmr = tmr_get(L, 1);
Expand Down Expand Up @@ -316,7 +343,11 @@ static int tmr_state(lua_State* L){

lua_pushboolean(L, (tmr->mode & TIMER_IDLE_FLAG) == 0);
lua_pushinteger(L, tmr->mode & (~TIMER_IDLE_FLAG));
#ifdef ENABLE_TIMER_SUSPEND
lua_pushboolean(L, swtmr_suspended_test(&tmr->os));
#else
lua_pushnil(L);
#endif
return 3;
}

Expand Down Expand Up @@ -396,6 +427,23 @@ static int tmr_create( lua_State *L ) {
return 1;
}


#if defined(SWTMR_DEBUG)
static void tmr_printRegistry(lua_State* L){
swtmr_print_registry();
}

static void tmr_printSuspended(lua_State* L){
swtmr_print_suspended();
}

static void tmr_printTimerlist(lua_State* L){
swtmr_print_timer_list();
}


#endif

// Module function map

static const LUA_REG_TYPE tmr_dyn_map[] = {
Expand All @@ -406,13 +454,24 @@ static const LUA_REG_TYPE tmr_dyn_map[] = {
{ LSTRKEY( "unregister" ), LFUNCVAL( tmr_unregister ) },
{ LSTRKEY( "state" ), LFUNCVAL( tmr_state ) },
{ LSTRKEY( "interval" ), LFUNCVAL( tmr_interval) },
{ LSTRKEY( "suspend" ), LFUNCVAL( tmr_suspend ) },
#ifdef ENABLE_TIMER_SUSPEND
{ LSTRKEY( "suspend" ), LFUNCVAL( tmr_suspend ) },
{ LSTRKEY( "resume" ), LFUNCVAL( tmr_resume ) },
#endif
{ LSTRKEY( "__gc" ), LFUNCVAL( tmr_unregister ) },
{ LSTRKEY( "__index" ), LROVAL( tmr_dyn_map ) },
{ LNILKEY, LNILVAL }
};

#if defined(SWTMR_DEBUG)
static const LUA_REG_TYPE tmr_dbg_map[] = {
{ LSTRKEY( "printRegistry" ), LFUNCVAL( tmr_printRegistry ) },
{ LSTRKEY( "printSuspended" ), LFUNCVAL( tmr_printSuspended ) },
{ LSTRKEY( "printTimerlist" ), LFUNCVAL( tmr_printTimerlist ) },
{ LNILKEY, LNILVAL }
};
#endif

static const LUA_REG_TYPE tmr_map[] = {
{ LSTRKEY( "delay" ), LFUNCVAL( tmr_delay ) },
{ LSTRKEY( "now" ), LFUNCVAL( tmr_now ) },
Expand All @@ -423,12 +482,19 @@ static const LUA_REG_TYPE tmr_map[] = {
{ LSTRKEY( "alarm" ), LFUNCVAL( tmr_alarm ) },
{ LSTRKEY( "start" ), LFUNCVAL( tmr_start ) },
{ LSTRKEY( "stop" ), LFUNCVAL( tmr_stop ) },
#ifdef ENABLE_TIMER_SUSPEND
{ LSTRKEY( "suspend" ), LFUNCVAL( tmr_suspend ) },
{ LSTRKEY( "suspend_all" ), LFUNCVAL( tmr_suspend_all ) },
{ LSTRKEY( "resume" ), LFUNCVAL( tmr_resume ) },
{ LSTRKEY( "resume_all" ), LFUNCVAL( tmr_resume_all ) },
#endif
{ LSTRKEY( "unregister" ), LFUNCVAL( tmr_unregister ) },
{ LSTRKEY( "state" ), LFUNCVAL( tmr_state ) },
{ LSTRKEY( "interval" ), LFUNCVAL( tmr_interval ) },
{ LSTRKEY( "create" ), LFUNCVAL( tmr_create ) },
#if defined(SWTMR_DEBUG)
{ LSTRKEY( "debug" ), LROVAL( tmr_dbg_map ) },
#endif
{ LSTRKEY( "ALARM_SINGLE" ), LNUMVAL( TIMER_MODE_SINGLE ) },
{ LSTRKEY( "ALARM_SEMI" ), LNUMVAL( TIMER_MODE_SEMI ) },
{ LSTRKEY( "ALARM_AUTO" ), LNUMVAL( TIMER_MODE_AUTO ) },
Expand All @@ -444,12 +510,14 @@ int luaopen_tmr( lua_State *L ){
alarm_timers[i].lua_ref = LUA_NOREF;
alarm_timers[i].self_ref = LUA_REFNIL;
alarm_timers[i].mode = TIMER_MODE_OFF;
os_timer_disarm(&alarm_timers[i].os);
//improve boot speed by using ets_timer_disarm instead of os_timer_disarm to avoid timer registry maintenance call.
ets_timer_disarm(&alarm_timers[i].os);
}
last_rtc_time=system_get_rtc_time(); // Right now is time 0
last_rtc_time_us=0;

os_timer_disarm(&rtc_timer);
//improve boot speed by using ets_timer_disarm instead of os_timer_disarm to avoid timer registry maintenance call.
ets_timer_disarm(&rtc_timer);
os_timer_setfn(&rtc_timer, rtc_callback, NULL);
os_timer_arm(&rtc_timer, 1000, 1);
return 0;
Expand Down
Loading

0 comments on commit dda240c

Please sign in to comment.