Browse Source

add: timer库添加udelay函数, 可以低精度进行us级别的阻塞式暂停

Wendal Chen 2 years ago
parent
commit
22bad73df5
1 changed files with 26 additions and 4 deletions
  1. 26 4
      luat/modules/luat_lib_timer.c

+ 26 - 4
luat/modules/luat_lib_timer.c

@@ -11,29 +11,51 @@
 #include "luat_malloc.h"
 
 /*
-硬阻塞指定时长,期间没有任何luat代码会执行,包括底层消息处理机制
+硬阻塞指定时长
 @api    timer.mdelay(timeout)
-@int 阻塞时长
+@int    阻塞时长,单位ms, 最高1024ms, 实际使用强烈建议不要超过200ms
 @return nil 无返回值
+@usage
+-- 期间没有任何luat代码会执行,包括底层消息处理机制
 -- 本方法通常不会使用,除非你很清楚会发生什么
 timer.mdelay(10)
 */
 static int l_timer_mdelay(lua_State *L) {
-    lua_gettop(L);
     if (lua_isinteger(L, 1)) {
         lua_Integer ms = luaL_checkinteger(L, 1);
-        if (ms)
+        if (ms > 0 && ms < 1024)
             luat_timer_mdelay(ms);
     }
     return 0;
 }
 
+/*
+硬阻塞指定时长但us级别,不会很精准
+@api    timer.udelay(timeout)
+@int    阻塞时长,单位us, 最大3000us
+@return nil 无返回值
+@usage
+-- 本方法通常不会使用,除非你很清楚会发生什么
+-- 本API在 2023.05.18 添加
+timer.udelay(10)
+-- 实际阻塞时长是有波动的
+*/
+static int l_timer_udelay(lua_State *L) {
+    if (lua_isinteger(L, 1)) {
+        lua_Integer us = luaL_checkinteger(L, 1);
+        if (us > 0 && us <= 3000)
+            luat_timer_us_delay(us);
+    }
+    return 0;
+}
+
 //TODO 支持hwtimer
 
 #include "rotable2.h"
 static const rotable_Reg_t reg_timer[] =
 {
     { "mdelay", ROREG_FUNC(l_timer_mdelay)},
+    { "udelay", ROREG_FUNC(l_timer_udelay)},
 	{ NULL,     ROREG_INT(0) }
 };