Shunt tmr callbacks into the Lua RTOS task.

The os_timer callback is executed from task rtT, prio 14, so they preempt
the Lua environment whenever they fire. Ideally we should be using the
RTOS timers instead, which run at prio 2 and thus would be more suited
for our uses.
This commit is contained in:
Johny Mattsson 2016-06-01 13:38:43 +10:00
parent 239af986df
commit 61291bd87b
2 changed files with 14 additions and 2 deletions

View File

@ -95,7 +95,11 @@ static sint32_t soft_watchdog = -1;
static timer_struct_t alarm_timers[NUM_TMR]; static timer_struct_t alarm_timers[NUM_TMR];
static os_timer_t rtc_timer; static os_timer_t rtc_timer;
static void alarm_timer_common(void* arg){ static task_handle_t callback_task;
static void run_callback (task_param_t arg, task_prio_t prio)
{
(void)prio;
ptimer_t tmr = &alarm_timers[(uint32_t)arg]; ptimer_t tmr = &alarm_timers[(uint32_t)arg];
lua_State* L = lua_getstate(); lua_State* L = lua_getstate();
if(tmr->lua_ref == LUA_NOREF) if(tmr->lua_ref == LUA_NOREF)
@ -112,6 +116,12 @@ static void alarm_timer_common(void* arg){
lua_call(L, 0, 0); lua_call(L, 0, 0);
} }
static void alarm_timer_common(void* arg)
{
if (!task_post_medium (callback_task, (task_param_t)arg))
NODE_ERROR("ERROR: lost timer callback!");
}
// Lua: tmr.delay( us ) // Lua: tmr.delay( us )
static int tmr_delay( lua_State* L ){ static int tmr_delay( lua_State* L ){
sint32_t us = luaL_checkinteger(L, 1); sint32_t us = luaL_checkinteger(L, 1);
@ -342,6 +352,8 @@ int luaopen_tmr( lua_State *L ){
ets_timer_disarm(&rtc_timer); ets_timer_disarm(&rtc_timer);
ets_timer_setfn(&rtc_timer, rtc_callback, NULL); ets_timer_setfn(&rtc_timer, rtc_callback, NULL);
ets_timer_arm_new(&rtc_timer, 1000, 1, 1); ets_timer_arm_new(&rtc_timer, 1000, 1, 1);
callback_task = task_get_id (run_callback);
return 0; return 0;
} }

View File

@ -156,5 +156,5 @@ void user_init(void)
// NodeMCU currently uses more than that. The game is on to find these // NodeMCU currently uses more than that. The game is on to find these
// culprits, but this gcc doesn't do the -fstack-usage option :( // culprits, but this gcc doesn't do the -fstack-usage option :(
xTaskCreate ( xTaskCreate (
nodemcu_main, "nodemcu", 600, 0, configTIMER_TASK_PRIORITY +1, NULL); nodemcu_main, "nodemcu", 800, 0, configTIMER_TASK_PRIORITY +1, NULL);
} }