Parcourir la source

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.
Johny Mattsson il y a 8 ans
Parent
commit
61291bd87b
2 fichiers modifiés avec 14 ajouts et 2 suppressions
  1. 13 1
      app/modules/tmr.c
  2. 1 1
      app/user/user_main.c

+ 13 - 1
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;
 }
 

+ 1 - 1
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);
 }