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

update: 优化ulwip/napt传输性能

Wendal Chen 1 год назад
Родитель
Сommit
df6a9022bf

+ 3 - 2
components/network/ulwip/binding/luat_lib_napt.c

@@ -64,7 +64,7 @@ static void toPs(void *ctx)
     UINT8 cid = ipv == 4 ? 1 : 2;
 
     int rc = PsifRawUlOutput(cid, pkt, len);
-    if (rc) {
+    if (rc != 1) {
         LLOGE("PsifRawUlOutput rc %d", rc);
     }
 
@@ -76,6 +76,7 @@ static void sendIp2Ps(uint8_t *pkt, uint32_t len, struct netif* netif)
 {
     void **param = (void **)luat_heap_malloc(sizeof(void *) * 2);
     if (param == NULL) {
+        luat_heap_free(pkt);
         LLOGE("no mem for sendIp2Ps");
         return;
     }
@@ -150,7 +151,7 @@ static int l_napt_rebuild(lua_State *L) {
 @api napt.check()
 @return nil
 @usage
--- 需要周期性调用, 30秒一次
+-- 两次check之间没有数据包的映射记录,会被清理
 */
 static int l_napt_check(lua_State *L) {
     luat_napt_table_check(NULL);

+ 67 - 1
components/network/ulwip/binding/luat_lib_ulwip.c

@@ -28,6 +28,8 @@ lua代码 <- ulwip回调函数 <- lwip(netif->low_level_output) <- lwip处理逻
 #include "luat_base.h"
 #include "luat_ulwip.h"
 #include "luat_crypto.h"
+#include "luat_gpio.h"
+#include "luat_spi.h"
 
 #define LUAT_LOG_TAG "ulwip"
 #include "luat_log.h"
@@ -604,7 +606,69 @@ static int l_ulwip_reg(lua_State *L) {
     return 1;
 }
 
-extern struct netif * net_lwip_get_netif(uint8_t adapter_index);
+/*
+操作XT804进行SPI快速收发
+@api ulwip.xt804_xfer(spi_id, cs_pin, addr, zbuff, len, offset, auto_seek, auto_len)
+@int spi_id SPI的ID号
+@int cs_pin CS脚的GPIO号
+@int addr 寄存器地址
+@zbuff zbuff对象
+@int len 长度
+@int offset 偏移量, 默认buff:used()
+@boolean auto_seek 是否自动移动偏移量, 默认false
+@int auto_len 自动分片长度, 默认按寄存器自动选择
+@return nil 无返回值
+@usage
+-- 本函数属于辅助函数
+*/
+static int l_ulwip_xt804_xfer(lua_State *L) {
+    int spi_id = luaL_checkinteger(L, 1);  // SPI的ID号
+    int cs_pin = luaL_checkinteger(L, 2);  // CS脚的GPIO号
+    int addr = luaL_checkinteger(L, 3);    // 寄存器地址
+    luat_zbuff_t* zbuff = ((luat_zbuff_t *)luaL_checkudata(L, 4, LUAT_ZBUFF_TYPE));
+    size_t len = luaL_checkinteger(L, 5);
+    size_t offset = luaL_optinteger(L, 6, zbuff->used);
+    int auto_seek = lua_toboolean(L, 7);
+    size_t auto_len = luaL_optinteger(L, 8, 0);
+    if (auto_len == 0) {
+        int tmpaddr = addr & 0x7F;
+        if (tmpaddr == 0x00 || tmpaddr == 0x10 || tmpaddr == 0x01 || tmpaddr == 0x11)
+            auto_len = 4;
+        else
+            auto_len = 2;
+    }
+    if (len % auto_len != 0) {
+        len = (len + auto_len - 1) / auto_len * auto_len;
+    }
+
+    char tmp[5] = {0};
+    tmp[0] = addr & 0xFF;
+    int is_write = addr & 0x80;
+    
+    if (is_write) {
+        for (size_t i = 0; i < len / auto_len; i++) {
+            memcpy(tmp+1, zbuff->addr + offset, auto_len);
+            luat_gpio_set(cs_pin, 0);
+            luat_spi_send(spi_id, tmp, auto_len + 1);
+            luat_gpio_set(cs_pin, 1);
+            offset += auto_len;
+        }
+    }
+    else {
+        for (size_t i = 0; i < len / auto_len; i++) {
+            luat_gpio_set(cs_pin, 0);
+            memcpy(tmp+1, zbuff->addr + offset, auto_len);
+            luat_spi_send(spi_id, tmp, 1);
+            luat_spi_recv(spi_id, zbuff->addr + offset, auto_len);
+            luat_gpio_set(cs_pin, 1);
+            offset += auto_len;
+        }
+    }
+    if (auto_seek) {
+        zbuff->used += len;
+    }
+    return 0;
+ }
 
 #include "rotable2.h"
 static const rotable_Reg_t reg_ulwip[] =
@@ -617,6 +681,8 @@ static const rotable_Reg_t reg_ulwip[] =
     { "ip" ,                ROREG_FUNC(l_ulwip_ip)},
     { "reg" ,               ROREG_FUNC(l_ulwip_reg)},
 
+    { "xt804_xfer" ,        ROREG_FUNC(l_ulwip_xt804_xfer)},
+
     // 网卡FLAGS,默认
     // NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6
 

+ 2 - 2
components/network/ulwip/src/napt_ipv4.c

@@ -1010,12 +1010,12 @@ static int alg_tcp_proc(u8 is_inet,
 #endif
                 return -1;
             }
-            LLOGD("分配新的TCP映射 ip %d port %d -> %d", src_ip, tcp_hdr->src, napt->new_port);
+            // LLOGD("分配新的TCP映射 ip %d port %d -> %d", src_ip, tcp_hdr->src, napt->new_port);
         }
         else
         {
             luat_napt_table_update_4tcp(napt);
-            LLOGD("复用老的TCP映射 ip %d port %d -> %d", src_ip, tcp_hdr->src, napt->new_port);
+            // LLOGD("复用老的TCP映射 ip %d port %d -> %d", src_ip, tcp_hdr->src, napt->new_port);
         }
         memcpy(napt->mac, ptr + 6, 6); //保存源mac地址
         // LLOGD("记录内网-->外网的源MAC %02X%02X%02X%02X%02X%02X", napt->mac[0], napt->mac[1], napt->mac[2], napt->mac[3], napt->mac[4], napt->mac[5]);