소스 검색

add: 添加hwtimer库

Wendal Chen 4 년 전
부모
커밋
3ccc971e83
3개의 변경된 파일81개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      luat/include/luat_base.h
  2. 14 0
      luat/include/luat_hwtimer.h
  3. 66 0
      luat/modules/luat_lib_hwtimer.c

+ 1 - 0
luat/include/luat_base.h

@@ -117,6 +117,7 @@ LUAMOD_API int luaopen_lwip( lua_State *L );
 
 LUAMOD_API int luaopen_wdt( lua_State *L );
 LUAMOD_API int luaopen_mcu( lua_State *L );
+LUAMOD_API int luaopen_hwtimer( lua_State *L );
 
 /** sprintf需要支持longlong值的打印, 提供平台无关的实现*/
 int l_sprintf(char *buf, size_t size, const char *fmt, ...);

+ 14 - 0
luat/include/luat_hwtimer.h

@@ -0,0 +1,14 @@
+#include "luat_base.h"
+
+typedef struct luat_hwtimer_conf {
+    uint8_t   unit;           /**< timer accuracy */
+    uint32_t  timeout;        /**< timeout period */
+    uint8_t   is_repeat;      /**< cycle timer */
+}luat_hwtimer_conf_t;
+
+int luat_hwtimer_create(luat_hwtimer_conf_t *conf);
+int luat_hwtimer_start(int id);
+int luat_hwtimer_stop(int id);
+int luat_hwtimer_read(int id);
+int luat_hwtimer_change(int id, uint32_t newtimeout);
+int luat_hwtimer_destroy(int id);

+ 66 - 0
luat/modules/luat_lib_hwtimer.c

@@ -0,0 +1,66 @@
+#include "luat_base.h"
+#include "luat_hwtimer.h"
+
+static int l_hwtimer_create(lua_State* L) {
+    luat_hwtimer_conf_t conf = {0};
+    conf.unit = luaL_checkinteger(L, 1);
+    conf.timeout = luaL_checkinteger(L, 2);
+    if (!lua_isnoneornil(L, 3))
+        conf.is_repeat = lua_toboolean(L, 3);
+    int id = luat_hwtimer_create(&conf);
+    lua_pushinteger(L, id);
+    return 1;
+}
+
+static int l_hwtimer_start(lua_State* L) {
+    int id = luaL_checkinteger(L, 1);
+    int ret = luat_hwtimer_start(id);
+    lua_pushboolean(L, ret == 0 ? 1 : 0);
+    return 1;
+}
+
+static int l_hwtimer_stop(lua_State* L) {
+    int id = luaL_checkinteger(L, 1);
+    int ret = luat_hwtimer_stop(id);
+    lua_pushboolean(L, ret == 0 ? 1 : 0);
+    return 1;
+}
+
+static int l_hwtimer_read(lua_State* L) {
+    int id = luaL_checkinteger(L, 1);
+    uint32_t ret = luat_hwtimer_read(id);
+    lua_pushinteger(L, ret);
+    return 1;
+}
+
+static int l_hwtimer_change(lua_State* L) {
+    int id = luaL_checkinteger(L, 1);
+    uint32_t newtimeout = luaL_checkinteger(L, 2);
+    int ret = luat_hwtimer_change(id, newtimeout);
+    lua_pushboolean(L, ret == 0 ? 1 : 0);
+    return 1;
+}
+
+static int l_hwtimer_destroy(lua_State* L) {
+    int id = luaL_checkinteger(L, 1);
+    uint32_t ret = luat_hwtimer_destroy(id);
+    lua_pushinteger(L, ret);
+    return 1;
+}
+
+#include "rotable.h"
+static const rotable_Reg reg_hwtimer[] =
+{
+    { "create" ,        l_hwtimer_create, 0},
+    { "start",          l_hwtimer_start,  0},
+    { "stop",           l_hwtimer_stop,   0},
+    { "read",           l_hwtimer_read,   0},
+    { "change",         l_hwtimer_change, 0},
+    { "destroy",        l_hwtimer_destroy,0},
+	{ NULL,          NULL ,       0}
+};
+
+LUAMOD_API int luaopen_hwtimer( lua_State *L ) {
+    luat_newlib(L, reg_hwtimer);
+    return 1;
+}