Просмотр исходного кода

update: 改成Lua 32bit模式, 整理一下代码.use LUA_32BITS and clean codes

Wendal Chen 6 лет назад
Родитель
Сommit
42b1bee1c6
6 измененных файлов с 44 добавлено и 116 удалено
  1. 1 1
      lua/README.md
  2. 1 1
      lua/include/luaconf.h
  3. 1 3
      luat/inculde/luat_msgbus.h
  4. 2 2
      luat/modules/luat_lib_gpio.c
  5. 3 3
      luat/modules/luat_lib_rtos.c
  6. 36 106
      luat/modules/luat_main.c

+ 1 - 1
lua/README.md

@@ -1,6 +1,6 @@
 # Lua 虚拟机的源码
 # Lua 虚拟机的源码
 
 
-来源: 原版Lua 5.4.5, 含2019.12.31前已发布的官方修正
+来源: 原版Lua 5.3.5, 含2019.12.31前已发布的官方修正
 
 
 ## 目录
 ## 目录
 
 

+ 1 - 1
lua/include/luaconf.h

@@ -33,7 +33,7 @@
 ** ensure that all software connected to Lua will be compiled with the
 ** ensure that all software connected to Lua will be compiled with the
 ** same configuration.
 ** same configuration.
 */
 */
-/* #define LUA_32BITS */
+#define LUA_32BITS
 
 
 
 
 /*
 /*

+ 1 - 3
luat/inculde/luat_msgbus.h

@@ -9,9 +9,7 @@
 #define MSG_UART_RX 3
 #define MSG_UART_RX 3
 #define MSG_UART_TXDONE 4
 #define MSG_UART_TXDONE 4
 
 
-
-
-typedef int (*luat_msg_handler) (lua_State *L);
+typedef int (*luat_msg_handler) (lua_State *L, void* ptr);
 
 
 typedef struct rtos_msg{
 typedef struct rtos_msg{
     luat_msg_handler handler;
     luat_msg_handler handler;

+ 2 - 2
luat/modules/luat_lib_gpio.c

@@ -4,9 +4,9 @@
 #include "luat_gpio.h"
 #include "luat_gpio.h"
 #include "luat_malloc.h"
 #include "luat_malloc.h"
 
 
-static int l_gpio_handler(lua_State *L) {
+static int l_gpio_handler(lua_State *L, void* ptr) {
     luat_printf("l_gpio_handler\n");
     luat_printf("l_gpio_handler\n");
-    struct luat_gpio_t *gpio = (struct luat_gpio_t *)luat_msgbus_data();
+    struct luat_gpio_t *gpio = (struct luat_gpio_t *)ptr;
     lua_pushinteger(L, MSG_GPIO);
     lua_pushinteger(L, MSG_GPIO);
     lua_pushinteger(L, gpio->pin);
     lua_pushinteger(L, gpio->pin);
     lua_pushinteger(L, luat_gpio_get(gpio->pin));
     lua_pushinteger(L, luat_gpio_get(gpio->pin));

+ 3 - 3
luat/modules/luat_lib_rtos.c

@@ -13,15 +13,15 @@ static int l_rtos_recv(lua_State *L) {
     re = luat_msgbus_get(&msg, luaL_checkinteger(L, 1));
     re = luat_msgbus_get(&msg, luaL_checkinteger(L, 1));
     if (!re) {
     if (!re) {
         luat_printf("luat_msgbus_get!!!\n");
         luat_printf("luat_msgbus_get!!!\n");
-        return msg.handler(L);
+        return msg.handler(L, &msg);
     }
     }
     return 0;
     return 0;
 }
 }
 
 
 //------------------------------------------------------------------
 //------------------------------------------------------------------
-static int l_timer_handler(lua_State *L) {
+static int l_timer_handler(lua_State *L, void* ptr) {
     luat_printf("l_timer_handler\n");
     luat_printf("l_timer_handler\n");
-    struct luat_timer_t *timer = (struct luat_timer_t *)luat_msgbus_data();
+    struct luat_timer_t *timer = (struct luat_timer_t *)ptr;
     lua_pushinteger(L, MSG_TIMER);
     lua_pushinteger(L, MSG_TIMER);
     lua_pushinteger(L, timer->id);
     lua_pushinteger(L, timer->id);
     lua_pushinteger(L, timer->timeout);
     lua_pushinteger(L, timer->timeout);

+ 36 - 106
luat/modules/luat_main.c

@@ -30,99 +30,23 @@ static void luat_openlibs(lua_State *L) {
     //#endif
     //#endif
 }
 }
 
 
-static int pmain(lua_State *L) {
-    int re = 0;
-    //luat_print("luat_pmain!!!\n");
-    // 加载系统库
-    luaL_openlibs(L);
-
-    // 加载本地库
-    luat_openlibs(L);
-    
-
-    // 打印个提示
-    //luat_print("luat_boot_complete\n");
-
-    // 执行
-    //lfs_file_t file;
-    //if (LFS_FileOpen(&file, "/lua/main.lua", LFS_O_RDONLY) == LFS_ERR_OK) {
-    //    luat_print("reading main.lua--------------------\n");
-    //    char *buf = luat_heap_calloc(file.size+1);
-    //    LFS_FileRead(&file, buf, file.size);
-    //    LFS_FileClose(&file);
-    //    luat_print("run main.lua-----------------------\n");
-    //    re = luaL_dostring(L, buf);
-    //    luat_heap_free(buf);
-    //    luat_print("done main.lua-------------------------\n");
-    //}
-    //else {
-    //  re = luaL_dostring(L, "print(_VERSION)");
-    //  re = luaL_dostring(L, "local a = 1");
-    //    luat_print("/lua/main.lua not found!!! run default lua string\n");
-    //    re = luaL_dostring(L, "print(\"test1=====\") local a = 1\n local b=2\nprint(_G)\nprint(a+b)\nprint(sys)\nprint(_VERSION)");
+static int test_simple_core() {
+  // Test A, 没有外部库的逻辑
+  int re = 0;
+  re = luaL_dostring(L, "print(_VERSION)");
+  re = luaL_dostring(L, "local a = 1");
+  re = luaL_dostring(L, "print(\"test1=====\") local a = 1\n local b=2\nprint(_G)\nprint(a+b)\nprint(sys)\nprint(_VERSION)");
     //    re = luaL_dostring(L, "print(\"test2=====\") local ab=1 \nprint(rtos.get_version()) print(rtos.get_memory_free())");
     //    re = luaL_dostring(L, "print(\"test2=====\") local ab=1 \nprint(rtos.get_version()) print(rtos.get_memory_free())");
     //    re = luaL_dostring(L, "print(\"test3=====\") print(a - 1)");
     //    re = luaL_dostring(L, "print(\"test3=====\") print(a - 1)");
     //    re = luaL_dostring(L, "print(\"test4=====\") print(rtos.get_memory_free()) collectgarbage(\"collect\") print(rtos.get_memory_free())");
     //    re = luaL_dostring(L, "print(\"test4=====\") print(rtos.get_memory_free()) collectgarbage(\"collect\") print(rtos.get_memory_free())");
     //    re = luaL_dostring(L, "print(\"test5=====\") print(rtos.timer_start(1, 3000)) print(rtos.receive(5000)) print(\"timer_get?\")");
     //    re = luaL_dostring(L, "print(\"test5=====\") print(rtos.timer_start(1, 3000)) print(rtos.receive(5000)) print(\"timer_get?\")");
     //    re = luaL_dostring(L, "print(\"test6=====\") local f = io.open(\"abc.log\", \"w\") print(f)");
     //    re = luaL_dostring(L, "print(\"test6=====\") local f = io.open(\"abc.log\", \"w\") print(f)");
     //    re = luaL_dostring(L, "print(_VERSION) print(\"sleep 2s\") timer.mdelay(2000) print(\"hi again\")");
     //    re = luaL_dostring(L, "print(_VERSION) print(\"sleep 2s\") timer.mdelay(2000) print(\"hi again\")");
-    
-
-    // 加载几个帮助方法吧
-    /*
-    luaL_loadstring(L, "-- rtos消息回调\n"
-                       "local handlers = {}\n"
-                       "setmetatable(handlers, {__index = function() return function() end end})\n"
-                       "rtos.on = function(id, handler) handlers[id] = handler end\n"
-                       "timer.ids = {}\n"
-                       "timer.maxid = 1\n"
-                       "timer.start = function(ms, func)\n"
-                       "  local id = timer.maxid\n"
-                       "  timer.maxid = timer.maxid + 1\n"
-                       "  local nt = rtos.timer_start(id, m)\n"
-                       "  timer.ids[id] = {nt=nt,func=func}\n"
-                       "end\n"
-                       "rtos.on(rtos.MSG_TIMER, function(msg)\n"
-                       "   local t = timer.ids[msg]\n"
-                       "   if t ~= nil then t.func() end\n"
-                       "end\n"
-                       "sys.run = functoin()\n"
-                       "   while 1 do\n"
-                       "     local id,msg = rtos.recv(0)\n"
-                       "     if id == rtos.MSG_TIMER then handlers(id)(msg) end\n"
-                       "   end\n"
-                       "end\n");
-        re = luaL_loadstring(L, "-- rtos消息回调\n"
-                      "sys.run = functoin()\n"
-                      "  print(\"sys.run -- GO!GO!GO!\")"
-                      "  while 1 do\n"
-                      "    print(\"sys.run -- GO WHILE!\")"
-                      "    local id,msg = rtos.recv(0)\n"
-                      "    print(id)\n"
-                      "end\n");
-    */
-
-//#ifdef RT_USING_PIN
-        // pin number pls refer pin_map.c
-        /*
-        re = luaL_dostring(L, "print(_VERSION)\n"
-                   " local PA1=14 local PA4=15 \n"
-                   " gpio.setup(PA1,gpio.OUTPUT) gpio.setup(PA4, gpio.OUTPUT)\n"
-                   " print(PA1, PA4)\n"
-                   " gpio.set(PA1, 0) gpio.set(PA4, 0)\n"
-                   " while 1 do\n"
-                   "    gpio.set(PA1, 1)\n"
-                   "    gpio.set(PA4, 1)\n"
-                   "    print(\"sleep 1s\")\n"
-                   "    timer.mdelay(1000)\n"
+    return re;
+}
 
 
-                   "    gpio.set(PA1, 0)\n"
-                   "    gpio.set(PA4, 0)\n"
-                   "    print(\"sleep 1s\")\n"
-                   "    timer.mdelay(1000)\n"
-                   "end\n");
-        */
-       re = luaL_dostring(L, "print(_VERSION)\n"
+static int test_gpio_simple() {
+        int re = luaL_dostring(L, "print(_VERSION)\n"
                    " local PA1=14\n"
                    " local PA1=14\n"
                    " local PB7=27\n"
                    " local PB7=27\n"
                    " gpio.setup(PA1,gpio.OUTPUT)\n"
                    " gpio.setup(PA1,gpio.OUTPUT)\n"
@@ -141,26 +65,32 @@ static int pmain(lua_State *L) {
                    "    timer.mdelay(1000)\n"
                    "    timer.mdelay(1000)\n"
                    "end\n"
                    "end\n"
                    );
                    );
-//#else
-        /*
-        re = luaL_dostring(L, "print(_VERSION .. \" from Luat\")\n timer.mdelay(1000)\n print(_VERSION)"
-                  "local c = 0\n"
-                  "while 1 do\n"
-                  "    print(\"count=\" .. c)\n"
-                  "    timer.mdelay(1000)\n"
-                  "    c = c + 1\n"
-                  "end\n"
-                  );
-        */
-       re = luaL_dostring(L, "print(_VERSION .. \" from Luat\")\n"
-                             "timer.mdelay(1000)\n"
-                             "print(\"END\")\n"
-                             "rtos.timer_start(1, 1000, 3)\n"
-                             "while 1 do print(rtos.recv(-1)) end"
-                             //"sys.run()\n"
-                  );
-//#endif
-    //}
+        return re;
+}
+
+static int test_timer_simple() {
+          int re = luaL_dostring(L, "print(_VERSION)\n"
+                   " while 1 do\n"
+                   "    print(\"sleep 1s\")\n"
+                   "    timer.mdelay(1000)\n"
+                   "end\n"
+                   );
+        return re;
+}
+
+static int pmain(lua_State *L) {
+    int re = 0;
+    //luat_print("luat_pmain!!!\n");
+    // 加载系统库
+    luaL_openlibs(L);
+
+    // 加载本地库
+    luat_openlibs(L);
+    
+    // 测试代码
+    // re = test_core_simple();
+    re = test_gpio_simple();
+    // re = test_timer_simple();
     
     
     if (re) {
     if (re) {
         luat_print("luaL_dostring  return re != 0\n");
         luat_print("luaL_dostring  return re != 0\n");