Jelajahi Sumber

[update] add rom external libraries support and add cpu lib. | 添加rom外部库支持并且添加cpu库.

liu2guang 7 tahun lalu
induk
melakukan
9c95ba2dcc
6 mengubah file dengan 148 tambahan dan 9 penghapusan
  1. 19 0
      lexlibs/SConscript
  2. 18 0
      lexlibs/lexlibs.h
  3. 107 0
      lexlibs/lua_cpu.c
  4. 2 2
      lua/linit.c
  5. 2 6
      lua/luaconf.h
  6. 0 1
      lua2rtt.c

+ 19 - 0
lexlibs/SConscript

@@ -0,0 +1,19 @@
+from building import * 
+import rtconfig
+
+# get current dir path
+cwd = GetCurrentDir()
+
+# init src and inc vars
+src = []
+inc = []
+
+# add Lua2RTT common include
+inc = inc + [cwd]
+
+# add Lua2RTT basic code
+src = Glob('*.c')
+
+group = DefineGroup('lua2rtt_exlibs', src, depend = ['LUA2RTT_USING_EXLIBS'], CPPPATH = inc)
+
+Return('group')

+ 18 - 0
lexlibs/lexlibs.h

@@ -0,0 +1,18 @@
+/*
+ * @File:   lexlibs.h 
+ * @Author: liu2guang
+ * @Date:   2018-05-08 15:13:56
+ *
+ * @LICENSE: https://github.com/liu2guang/lua2rtt/blob/master/LICENSE.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-05-08     liu2guang    The first version. 
+ */
+#ifndef __LEXLIBS_H__ 
+#define __LEXLIBS_H__ 
+
+#define LUA_EXLIBS_ROM                                    \
+    _ROM("cpu", luaopen_cpu, cpu_map) 
+
+#endif

+ 107 - 0
lexlibs/lua_cpu.c

@@ -0,0 +1,107 @@
+/*
+ * @File:   cpu.c
+ * @Author: liu2guang
+ * @Date:   2018-05-06 09:16:56
+ *
+ * @LICENSE: https://github.com/liu2guang/lua2rtt/blob/master/LICENSE.
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-05-06     liu2guang    The first version.
+ */
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+#define TO_32BIT(L, n) (((signed long long)luaL_checknumber((L), (n))) & 0x0ffffffff)
+
+/* Lua: data = r08(address) */ 
+static int cpu_r08(lua_State *L)
+{
+    uint32_t addr = 0;
+    luaL_checkinteger(L, 1);
+    addr = (uint32_t)TO_32BIT(L, 1);
+    lua_pushnumber(L, (lua_Number)(*(volatile uint8_t *)addr)); 
+    return 1;
+}
+
+/* Lua: w08(address, data) */ 
+static int cpu_w08(lua_State *L)
+{
+    uint32_t addr;
+    uint8_t data = (uint8_t)TO_32BIT(L, 2);
+
+    luaL_checkinteger(L, 1);
+    addr = (uint32_t)TO_32BIT(L, 1);
+    *(volatile uint8_t*)addr = data; 
+    
+    return 0; 
+}
+
+/* Lua: data = r16(address) */
+static int cpu_r16(lua_State *L)
+{
+    uint32_t addr = 0;
+    luaL_checkinteger(L, 1);
+    addr = (uint32_t)TO_32BIT(L, 1);
+    lua_pushnumber(L, (lua_Number)(*(volatile uint16_t *)addr)); 
+    return 1;
+}
+
+/* Lua: w16(address, data) */ 
+static int cpu_w16(lua_State *L)
+{
+    uint32_t addr;
+    uint16_t data = (uint16_t)TO_32BIT(L, 2);
+
+    luaL_checkinteger(L, 1);
+    addr = (uint32_t)TO_32BIT(L, 1);
+    *(volatile uint16_t*)addr = data; 
+    
+    return 0; 
+}
+
+/* Lua: data = r32(address) */
+static int cpu_r32(lua_State *L)
+{
+    uint32_t addr = 0;
+    luaL_checkinteger(L, 1);
+    addr = (uint32_t)TO_32BIT(L, 1);
+    lua_pushnumber(L, (lua_Number)(*(volatile uint32_t *)addr)); 
+    return 1;
+}
+
+/* Lua: w32(address, data) */ 
+static int cpu_w32(lua_State *L)
+{
+    uint32_t addr;
+    uint32_t data = (uint32_t)TO_32BIT(L, 2);
+
+    luaL_checkinteger(L, 1);
+    addr = (uint32_t)TO_32BIT(L, 1);
+    *(volatile uint32_t*)addr = data; 
+    
+    return 0; 
+}
+
+// Module function map 
+#define MIN_OPT_LEVEL 2 
+#include "lrodefs.h" 
+static const LUA_REG_TYPE cpu_map[] =
+{
+    {LSTRKEY("r08"), LFUNCVAL(cpu_r08)}, 
+    {LSTRKEY("w08"), LFUNCVAL(cpu_w08)}, 
+    {LSTRKEY("r16"), LFUNCVAL(cpu_r16)}, 
+    {LSTRKEY("w16"), LFUNCVAL(cpu_w16)}, 
+    {LSTRKEY("r32"), LFUNCVAL(cpu_r32)}, 
+    {LSTRKEY("w32"), LFUNCVAL(cpu_w32)}, 
+    {NULL, NULL} 
+}; 
+
+/* ´ò¿ªCPU¿â */ 
+LUALIB_API int luaopen_cpu(lua_State *L)
+{
+    luaL_register(L, "cpu", cpu_map); 
+    return 1;
+} 

+ 2 - 2
lua/linit.c

@@ -15,14 +15,14 @@
 #include "lrotable.h"
 #include "luaconf.h"
 
-#if defined(RT_LUA_USE_EXLIBS)
+#if defined(LUA2RTT_USING_EXLIBS) 
 #include "lexlibs.h"
 #endif
 
 #if defined(LUA_EXLIBS_ROM)
 #undef _ROM
 #define _ROM( name, openf, table ) extern int openf(lua_State *);
-LUA_EXLIBS_ROM
+LUA_EXLIBS_ROM; 
 #endif
 
 static const luaL_Reg lualibs[] =

+ 2 - 6
lua/luaconf.h

@@ -24,7 +24,6 @@
 #define LUA_OPTIMIZE_MEMORY         0
 #endif
 
-
 /*
 @@ LUA_ANSI controls the use of non-ansi features.
 ** CHANGE it (define it) if you want Lua to avoid the use of any
@@ -34,7 +33,6 @@
 #define LUA_ANSI
 #endif
 
-
 #if !defined(LUA_ANSI) && defined(_WIN32)
 #define LUA_WIN
 #endif
@@ -62,7 +60,6 @@
 #define LUA_USE_ULONGJMP
 #endif
 
-
 /*
 @@ LUA_PATH and LUA_CPATH are the names of the environment variables that
 @* Lua check to set its paths.
@@ -74,7 +71,6 @@
 #define LUA_CPATH       "LUA_CPATH"
 #define LUA_INIT        "LUA_INIT"
 
-
 /*
 @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
 @* Lua libraries.
@@ -102,7 +98,7 @@
         ".\\?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?\\init.lua;" \
                      LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua"
 #define LUA_CPATH_DEFAULT \
-    ".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
+        ".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" 
 #endif // #ifndef LUA_RPC
 
 #else // #if defined(_WIN32)
@@ -818,7 +814,7 @@ union luai_Cast
 
 #define lua_popen(L,c,m)    ((void)((void)c, m),  \
         luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
-#define lua_pclose(L,file)      ((void)((void)L, file), 0)
+#define lua_pclose(L,file)  ((void)((void)L, file), 0)
 
 #endif
 

+ 0 - 1
lua2rtt.c

@@ -265,7 +265,6 @@ start:
         /* 判断输出命令是否过长, 过长后不处理字符, 默认128Byte字符 */
         if(handle.line_position >= LUA2RTT_CMD_SIZE)
         {
-            //handle.line_position = LUA2RTT_CMD_SIZE-1; 
             continue; 
         }