From 61291bd87b739413225b80833880462a5a944b06 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Wed, 1 Jun 2016 13:38:43 +1000 Subject: [PATCH] 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. --- app/modules/tmr.c | 14 +++++++++++++- app/user/user_main.c | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/modules/tmr.c b/app/modules/tmr.c index e32e7bb1..3ee63cb6 100755 --- a/app/modules/tmr.c +++ b/app/modules/tmr.c @@ -95,7 +95,11 @@ static sint32_t soft_watchdog = -1; static timer_struct_t alarm_timers[NUM_TMR]; 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]; lua_State* L = lua_getstate(); if(tmr->lua_ref == LUA_NOREF) @@ -112,6 +116,12 @@ static void alarm_timer_common(void* arg){ 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 ) static int tmr_delay( lua_State* L ){ sint32_t us = luaL_checkinteger(L, 1); @@ -342,6 +352,8 @@ int luaopen_tmr( lua_State *L ){ ets_timer_disarm(&rtc_timer); ets_timer_setfn(&rtc_timer, rtc_callback, NULL); ets_timer_arm_new(&rtc_timer, 1000, 1, 1); + + callback_task = task_get_id (run_callback); return 0; } diff --git a/app/user/user_main.c b/app/user/user_main.c index f98be49b..73df65a7 100644 --- a/app/user/user_main.c +++ b/app/user/user_main.c @@ -156,5 +156,5 @@ void user_init(void) // NodeMCU currently uses more than that. The game is on to find these // culprits, but this gcc doesn't do the -fstack-usage option :( xTaskCreate ( - nodemcu_main, "nodemcu", 600, 0, configTIMER_TASK_PRIORITY +1, NULL); + nodemcu_main, "nodemcu", 800, 0, configTIMER_TASK_PRIORITY +1, NULL); }