Skip to content

Commit

Permalink
add: timer库添加udelay函数, 可以低精度进行us级别的阻塞式暂停
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed May 18, 2023
1 parent 3cb04e8 commit 22bad73
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions luat/modules/luat_lib_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
};

Expand Down

0 comments on commit 22bad73

Please sign in to comment.