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

add: airlink,增加ready和sdata函数,及对应的demo

Wendal Chen 11 месяцев назад
Родитель
Сommit
f9d2ca86fe

+ 41 - 0
components/airlink/binding/luat_lib_airlink.c

@@ -9,6 +9,7 @@
 #include "luat_mcu.h"
 #include <math.h>
 #include "luat_airlink.h"
+#include "luat_zbuff.h"
 
 #define LUAT_LOG_TAG "airlink"
 #include "luat_log.h"
@@ -108,18 +109,58 @@ static int l_airlink_slave_reboot(lua_State *L) {
     return 0;
 }
 
+static int l_airlink_sdata(lua_State *L) {
+    size_t len = 0;
+    const char* data = NULL;
+    if (lua_type(L, 1) == LUA_TSTRING) {
+        data = luaL_checklstring(L, 1, &len);
+    }
+    else if (lua_isuserdata(L, 1)) {
+        // zbuff
+        luat_zbuff_t* buff = tozbuff(L);
+        data = (const char*)buff->addr;
+        len = buff->used;
+    }
+    else {
+        LLOGE("无效的参数,只能是字符串或者zbuff");
+        return 0;
+    }
+    if (len > 1500) {
+        LLOGE("无效的数据长度,最大1500字节");
+        return 0;
+    }
+    luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + len);
+    cmd->cmd = 0x20;
+    cmd->len = len;
+    memcpy(cmd->data, data, len);
+    luat_airlink_send2slave(cmd);
+    luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
+static int l_airlink_ready(lua_State *L) {
+    lua_pushboolean(L, luat_airlink_ready());
+    return 1;
+}
+
 #include "rotable2.h"
 static const rotable_Reg_t reg_airlink[] =
 {
     { "init" ,         ROREG_FUNC(l_airlink_init )},
     { "start" ,        ROREG_FUNC(l_airlink_start )},
     { "stop" ,         ROREG_FUNC(l_airlink_stop )},
+    { "ready",         ROREG_FUNC(l_airlink_ready)},
     { "test",          ROREG_FUNC(l_airlink_test )},
+    { "sdata",         ROREG_FUNC(l_airlink_sdata)},
     { "statistics",    ROREG_FUNC(l_airlink_statistics )},
     { "slave_reboot",   ROREG_FUNC(l_airlink_slave_reboot )},
 
     // 测试用的fota指令
     // { "fota",          ROREG_FUNC(l_airlink_fota )},
+
+    { "MODE_SPI_SLAVE",    ROREG_INT(0) },
+    { "MODE_SPI_MASTER",   ROREG_INT(1) },
 	{ NULL,            ROREG_INT(0) }
 };
 

+ 4 - 0
components/airlink/include/luat_airlink.h

@@ -7,6 +7,10 @@
 #define AIRLINK_MEM_TYPE LUAT_HEAP_SRAM
 #endif
 
+extern uint64_t g_airlink_last_cmd_timestamp;
+
+int luat_airlink_ready(void);
+
 typedef struct luat_airlink_cmd_ext
 {
     uint64_t pkgid;

+ 1 - 1
components/airlink/src/exec/luat_airlink_cmd_exec_basic.c

@@ -79,7 +79,7 @@ static int sdata_cb(lua_State *L, void *ptr)
 
 int luat_airlink_cmd_exec_sdata(luat_airlink_cmd_t *cmd, void *userdata)
 {
-    LLOGD("收到sdata指令!!!");
+    // LLOGD("收到sdata指令!!! %d", cmd->len);
     rtos_msg_t msg = {0};
     msg.handler = sdata_cb;
     msg.ptr = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, cmd->len);

+ 12 - 0
components/airlink/src/luat_airlink.c

@@ -7,6 +7,8 @@
 #include "luat_crypto.h"
 #include "luat_netdrv.h"
 #include "luat_netdrv_whale.h"
+#include "luat_mcu.h"
+
 #include "lwip/prot/ethernet.h"
 
 #define LUAT_LOG_TAG "airlink"
@@ -29,6 +31,7 @@ luat_airlink_newdata_notify_cb g_airlink_newdata_notify_cb;
 luat_airlink_spi_conf_t g_airlink_spi_conf;
 airlink_statistic_t g_airlink_statistic;
 uint32_t g_airlink_spi_task_mode;
+uint64_t g_airlink_last_cmd_timestamp;
 
 int luat_airlink_init(void)
 {
@@ -330,6 +333,7 @@ void luat_airlink_send2slave(luat_airlink_cmd_t* cmd) {
         LLOGD("luat_airlink_send2slave 内存不足, 丢弃掉");
         return;
     }
+    memcpy(item.cmd->data, cmd->data, cmd->len);
     ret = luat_airlink_queue_send(LUAT_AIRLINK_QUEUE_CMD, &item);
     if (ret != 0) {
         LLOGD("luat_airlink_send2slave 发送消息失败 长度 %d ret %d", cmd->len, ret);
@@ -337,3 +341,11 @@ void luat_airlink_send2slave(luat_airlink_cmd_t* cmd) {
         return;
     }
 }
+
+int luat_airlink_ready(void) {
+    uint64_t tnow = luat_mcu_tick64_ms();
+    if (tnow - g_airlink_last_cmd_timestamp < 100) {
+        return 1;
+    }
+    return 0;
+}

+ 1 - 1
components/airlink/src/task/luat_airlink_spi_master_task.c

@@ -183,7 +183,7 @@ __USER_FUNC_IN_RAM__ static void spi_master_task(void *param)
             LLOGI("从机内存高水位, 停止下发IP数据");
         }
         if (item.len > 0 && item.cmd != NULL) {
-            // LLOGD("发送待传输的数据, 塞入SPI的FIFO %d", item.len);
+            // LLOGD("发送待传输的数据, 塞入SPI的FIFO cmd id %d", item.cmd->cmd);
             luat_airlink_data_pack(item.cmd, item.len, txbuff);
             luat_airlink_cmd_free(item.cmd);
         }

+ 6 - 2
components/airlink/src/task/luat_airlink_task.c

@@ -4,6 +4,7 @@
 #include "luat_mem.h"
 #include "luat_rtos.h"
 #include "luat_crypto.h"
+#include "luat_mcu.h"
 
 #define LUAT_LOG_TAG "airlink"
 #include "luat_log.h"
@@ -46,14 +47,17 @@ __USER_FUNC_IN_RAM__ static int luat_airlink_task(void *param) {
                 LLOGW("空指令!");
                 continue;
             }
-            // TODO 真正的处理逻辑
-            // LLOGD("收到指令/回复 cmd %d len %d", ptr->cmd, len);
+            // 真正的处理逻辑
+            // if (ptr->cmd != 0x10) {
+            //     LLOGD("收到指令/回复 cmd %d len %d", ptr->cmd, ptr->len);
+            // }
             cmd_reg = airlink_cmds;
             while (1) {
                 if (cmd_reg->id == 0) {
                     break;
                 }
                 if (cmd_reg->id == ptr->cmd) {
+                    g_airlink_last_cmd_timestamp = luat_mcu_tick64_ms();
                     // LLOGI("找到CMD执行程序 %p", cmd_reg->exec);
                     cmd_reg->exec(ptr, NULL);
                     break;

+ 38 - 0
demo/airlink/air8101_air780epm_sdata/main.lua

@@ -0,0 +1,38 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "netdrv"
+VERSION = "1.0.4"
+
+
+-- sys库是标配
+_G.sys = require("sys")
+
+sys.taskInit(function()
+    sys.wait(100)
+    -- 初始化airlink
+    airlink.init()
+
+    if rtos.bsp() == "Air8101" then
+        airlink.start(0)
+    else
+        airlink.start(1)
+    end
+    sys.wait(100)
+
+    while 1 do
+        -- 发送给对端设备
+        local data = rtos.bsp() .. " " .. os.date()
+        log.info("发送数据给对端设备", data, "当前airlink状态", airlink.ready())
+        airlink.sdata(data)
+        sys.wait(1000)
+    end
+end)
+
+sys.subscribe("AIRLINK_SDATA", function(data)
+    log.info("收到AIRLINK_SDATA!!", data)
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!