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

add: 添加air001的概念实现(非常初步)

Wendal Chen 5 лет назад
Родитель
Сommit
dd85430550

+ 8 - 1
bsp/air001/CMakeLists.txt

@@ -11,9 +11,11 @@ INCLUDE_DIRECTORIES(
     "../../luat/packages/lua-cjson"
     "../../luat/packages/minmea"
     "../../luat/packages/u8g2"
+    "../../luat/packages/heap"
 )
 
 AUX_SOURCE_DIRECTORY(src DIR_SRCS)
+AUX_SOURCE_DIRECTORY(impl DIR_SRCS)
 AUX_SOURCE_DIRECTORY(../../lua/src LUA_SRCS)
 AUX_SOURCE_DIRECTORY(../../luat/modules LUAT_MODULES_SRCS)
 
@@ -21,15 +23,19 @@ AUX_SOURCE_DIRECTORY(../../luat/packages/vsprintf PRINTF_SRCS)
 AUX_SOURCE_DIRECTORY(../../luat/packages/lua-cjson CJSON_SRCS)
 AUX_SOURCE_DIRECTORY(../../luat/packages/minmea MINMEA_SRCS)
 AUX_SOURCE_DIRECTORY(../../luat/packages/u8g2 U8G2_SRCS)
+AUX_SOURCE_DIRECTORY(../../luat/packages/heap BGET_SRCS)
 
 add_library(lua SHARED ${LUA_SRCS})
 add_library(luatm SHARED ${LUAT_MODULES_SRCS})
 add_library(printf SHARED ${PRINTF_SRCS})
 add_library(cjson SHARED ${CJSON_SRCS})
 add_library(minmea SHARED ${MINMEA_SRCS})
+add_library(bget SHARED ${BGET_SRCS})
 #add_library(u8g2 SHARED ${U8G2_SRCS})
 
-target_link_libraries(luatm m)
+target_link_libraries(lua m)
+
+target_link_libraries(luatm lua)
 
 #add_executable(air001 ${DIR_SRCS} ${LUA_SRCS} ${LUAT_MODULES_SRCS} ${PRINTF_SRCS} ${CJSON_SRCS} ${MINMEA_SRCS} ${U8G2_SRCS})
 add_executable(air001 ${DIR_SRCS})
@@ -39,4 +45,5 @@ target_link_libraries(air001 luatm)
 target_link_libraries(air001 printf)
 target_link_libraries(air001 cjson)
 target_link_libraries(air001 minmea)
+target_link_libraries(air001 bget)
 #set_target_properties(air001 PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")

+ 101 - 0
bsp/air001/impl/luat_base_air001.c

@@ -0,0 +1,101 @@
+
+#include "luat_vdev.h"
+
+#include <unistd.h>
+
+#include "luat_malloc.h"
+
+luat_vdev_t vdev;
+
+int luat_vdev_init() {
+    printf("Go\r\n");
+    memset(&vdev, 0, sizeof(luat_vdev_t));
+    // 分配lua heap内存
+
+    vdev.luatvm_heap_size = 128*1024; // 做成可配置
+
+    // TODO 初始化GPIO
+    luat_vdev_gpio_init();
+
+    // TODO 初始化UART
+    luat_vdev_uart_init();
+
+    // 初始化Lua VM内存
+    luat_heap_init();
+
+    return 0;
+}
+
+
+void luat_os_reboot(int code) {
+    return; // NOP
+}
+/** 设备进入待机模式 */
+void luat_os_standy(int timeout) {
+    return; // nop
+}
+/** 厂商/模块名字, 例如Air302, Air640W*/
+const char* luat_os_bsp(void) {
+    return "Air001"; // 做成配置
+}
+
+void luat_timer_us_delay(size_t us) {
+    if (us)
+        usleep(us);
+    return;
+}
+
+
+// -----------------------
+
+static const luaL_Reg loadedlibs[] = {
+  {"_G", luaopen_base}, // _G
+  {LUA_LOADLIBNAME, luaopen_package}, // require
+  {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
+  {LUA_TABLIBNAME, luaopen_table},    // table库,操作table类型的数据结构
+  {LUA_IOLIBNAME, luaopen_io},        // io库,操作文件
+  {LUA_OSLIBNAME, luaopen_os},        // os库,已精简
+  {LUA_STRLIBNAME, luaopen_string},   // string库,字符串操作
+  {LUA_MATHLIBNAME, luaopen_math},    // math 数值计算
+//  {LUA_UTF8LIBNAME, luaopen_utf8},
+  {LUA_DBLIBNAME, luaopen_debug},     // debug库,已精简
+#if defined(LUA_COMPAT_BITLIB)
+  {LUA_BITLIBNAME, luaopen_bit32},    // 不太可能启用
+#endif
+// 往下是RTT环境下加载的库
+  {"rtos", luaopen_rtos},             // rtos底层库, 核心功能是队列和定时器
+  {"log", luaopen_log},               // 日志库
+  {"timer", luaopen_timer},           // 延时库
+  {"json", luaopen_cjson},            // json的序列化和反序列化
+  {"pack", luaopen_pack},             // pack.pack/pack.unpack
+  {"uart", luaopen_uart},             // 串口操作
+  {"mqttcore",luaopen_mqttcore},      // MQTT 协议封装
+  {"gpio", luaopen_gpio},              // GPIO脚的操作
+  {"sensor", luaopen_sensor},          // 传感器操作
+//   {"wlan", luaopen_wlan},              // wlan/wifi联网操作
+//   {"socket", luaopen_socket},          // 套接字操作
+//   {"http", luaopen_http},              // http库
+//   {"libcoap", luaopen_libcoap},        // 处理COAP数据包
+//   {"i2c", luaopen_i2c},                // I2C操作
+//   {"spi", luaopen_spi},                // SPI操作
+//   {"disp", luaopen_disp},              // 显示屏
+//   {"crypto", luaopen_crypto},          // 加密和hash库
+  {"fs",   luaopen_fs},                // 文件系统库
+  {NULL, NULL}
+};
+
+// 按不同的rtconfig加载不同的库函数
+void luat_openlibs(lua_State *L) {
+    // 初始化队列服务
+    luat_msgbus_init();
+    //print_list_mem("done>luat_msgbus_init");
+    // 加载系统库
+    const luaL_Reg *lib;
+    /* "require" functions from 'loadedlibs' and set results to global table */
+    for (lib = loadedlibs; lib->func; lib++) {
+        luaL_requiref(L, lib->name, lib->func, 1);
+        lua_pop(L, 1);  /* remove lib */
+        //extern void print_list_mem(const char* name);
+        //print_list_mem(lib->name);
+    }
+}

+ 0 - 0
bsp/air001/impl/luat_conf_air001.c


+ 32 - 0
bsp/air001/impl/luat_fs_air001.c

@@ -0,0 +1,32 @@
+
+#include "luat_base.h"
+#include "luat_fs.h"
+
+int luat_fs_init(void) {
+    return 0;
+}
+
+FILE* luat_fs_fopen(const char *filename, const char *mode) {
+    char buff[256] = {0};
+    snprintf(buff, 256, "disk/%s", filename);
+    return fopen(buff, mode);
+}
+
+int luat_fs_remove(const char *filename) {
+    char buff[256] = {0};
+    snprintf(buff, 256, "disk/%s", filename);
+    return remove(buff);
+}
+
+int luat_fs_rename(const char *old_filename, const char *new_filename) {
+    char buff[256] = {0};
+    char buff2[256] = {0};
+    snprintf(buff, 256, "disk/%s", old_filename);
+    snprintf(buff2, 256, "disk/%s", new_filename);
+    return rename(buff, buff2);
+}
+
+int luat_fs_info(const char* path, luat_fs_info_t *conf) {
+    return -1;
+}
+

+ 23 - 0
bsp/air001/impl/luat_gpio_air001.c

@@ -0,0 +1,23 @@
+
+#include "luat_vdev_gpio.h"
+#include "luat_gpio.h"
+
+int luat_vdev_gpio_init(void) {
+    return 0;
+}
+
+void luat_gpio_mode(int pin, int mode, int pull, int initOutput) {
+    
+}
+int luat_gpio_setup(luat_gpio_t* gpio) {
+    return -1;
+}
+int luat_gpio_set(int pin, int level) {
+    return -1;
+}
+int luat_gpio_get(int pin) {
+    return -1;
+}
+void luat_gpio_close(int pin) {
+    // nop
+}

+ 11 - 33
bsp/air001/impl/luat_malloc_air001.c

@@ -10,42 +10,20 @@
 #include "luat_base.h"
 #include "luat_malloc.h"
 
-void  luat_heap_init(void) {
+
+#include "luat_vdev.h"
+#include "bget.h"
+
+extern luat_vdev_t vdev;
+
+void luat_heap_init(void) {
+    if (vdev.luatvm_heap_ptr)
+        return;
+    vdev.luatvm_heap_ptr = malloc(vdev.luatvm_heap_size);
+    bpool(vdev.luatvm_heap_ptr, vdev.luatvm_heap_size);
     return; // nop
 }
-void* luat_heap_malloc(size_t len) {
-    return malloc(len);
-}
-void  luat_heap_free(void* ptr) {
-    return free(ptr);
-}
-void* luat_heap_realloc(void* ptr, size_t len) {
-    return realloc(ptr, len);
-}
-void* luat_heap_calloc(size_t count, size_t _size) {
-    return calloc(count, _size);
-}
-//size_t luat_heap_getfree(void);
-static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
-  (void)ud; (void)osize;  /* not used */
-  if (nsize == 0) {
-    free(ptr);
-    return NULL;
-  }
-  else
-    return realloc(ptr, nsize);
-}
-// 这部分是LuaVM专属内存
-void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
-    return l_alloc(ud, ptr, osize, nsize);
-}
 
-// 两个获取内存信息的方法,单位字节
-void luat_meminfo_luavm(size_t* total, size_t* used, size_t* max_used) {
-    *total = 0;
-    *used = 0;
-    *max_used = 0;
-}
 void luat_meminfo_sys(size_t* total, size_t* used, size_t* max_used) {
     *total = 0;
     *used = 0;

+ 17 - 0
bsp/air001/impl/luat_msgbus_air001.c

@@ -0,0 +1,17 @@
+#include "luat_base.h"
+#include "luat_msgbus.h"
+
+// 定义接口方法
+void luat_msgbus_init(void) {
+
+}
+//void* luat_msgbus_data();
+uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
+    return -1;
+}
+uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
+    return -1;
+}
+uint32_t luat_msgbus_freesize(void) {
+    return 1;
+}

+ 36 - 0
bsp/air001/impl/luat_timer_air001.c

@@ -0,0 +1,36 @@
+
+#include "luat_base.h"
+#include "luat_timer.h"
+
+#include <unistd.h>
+
+int luat_timer_start(luat_timer_t* timer) {
+    return -1;
+}
+
+int luat_timer_stop(luat_timer_t* timer) {
+    return -1;
+}
+
+luat_timer_t* luat_timer_get(size_t timer_id) {
+    return NULL;
+}
+
+
+int luat_timer_mdelay(size_t ms) {
+    while (ms > 0) {
+        if (ms >= 1000) {
+            ms -= 1000;
+            sleep(1);
+        }
+        else if (ms > 100) {
+            ms -= 100;
+            usleep(100*1000);
+        }
+        else {
+            usleep(ms * 1000);
+            break;
+        }
+    }
+    return 0;
+}

+ 32 - 0
bsp/air001/impl/luat_uart_air001.c

@@ -0,0 +1,32 @@
+
+#include "luat_vdev_uart.h"
+
+int luat_vdev_uart_init(void) {
+    return 0;
+}
+
+int luat_uart_setup(luat_uart_t* uart) {
+    return 0;
+}
+int luat_uart_write(int uartid, void* data, size_t length) {
+    for (size_t i = 0; i < length; i++)
+    {
+        char* c = data+i;
+        putchar(*c);
+    }
+    
+    return 0;
+}
+int luat_uart_read(int uartid, void* buffer, size_t length) {
+    return -1;
+}
+int luat_uart_close(int uartid) {
+    return 0;
+}
+int luat_uart_exist(int uartid) {
+    return 0;
+}
+
+int luat_setup_cb(int uartid, int received, int sent) {
+    return 0;
+}

+ 23 - 0
bsp/air001/include/luat_vdev.h

@@ -0,0 +1,23 @@
+
+#include "luat_base.h"
+#include "luat_vdev_gpio.h"
+#include "luat_vdev_uart.h"
+
+typedef struct luat_vdev
+{
+    // 头部信息, 识别码和版本号
+    uint16_t magic; // 0x3A 0x3B
+    uint16_t version;
+
+    // LuaVM内存池
+    size_t luatvm_heap_size;
+    char * luatvm_heap_ptr;
+
+    luat_vdev_gpio_t gpio;
+    luat_vdev_uart_t uart;
+
+}luat_vdev_t;
+
+
+int luat_vdev_init();
+

+ 21 - 0
bsp/air001/include/luat_vdev_gpio.h

@@ -0,0 +1,21 @@
+
+#include "luat_base.h"
+#include "luat_gpio.h"
+
+// GPIO 设备
+typedef struct luat_vdev_gpio_pin
+{
+    uint8_t valid;
+    uint8_t mode;
+    uint8_t value;
+    uint8_t pull;
+    uint32_t ref;
+}luat_vdev_gpio_pin_t;
+
+typedef struct luat_vdev_gpio
+{
+    size_t count;
+    luat_vdev_gpio_pin_t pins[256];
+}luat_vdev_gpio_t;
+
+int luat_vdev_gpio_init(void);

+ 14 - 0
bsp/air001/include/luat_vdev_uart.h

@@ -0,0 +1,14 @@
+
+#include "luat_base.h"
+#include "luat_uart.h"
+
+// UART 设备
+typedef struct luat_vdev_uart
+{
+    size_t count;
+    luat_uart_t devs[8];
+    char recvBuff[1024 * 8];
+    char sendBuff[1024 * 8];
+}luat_vdev_uart_t;
+
+int luat_vdev_uart_init(void);

+ 13 - 1
bsp/air001/src/app.c

@@ -1,6 +1,18 @@
 
 #include "stdio.h"
 
-int main() {
+#include "luat_vdev.h"
+
+int main(int argc, const char* argv[]) {
+
+    // TODO 读取命令行参数
+
+    // TODO 读取环境变量
+
+    // 初始化虚拟设备
+    luat_vdev_init();
+
+    luat_main(0, (char**)NULL, 0);
+
     return 0;
 }