Переглянути джерело

update: airlink,组件内的内存分配方式统一化

Wendal Chen 1 рік тому
батько
коміт
ab0240ac17

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

@@ -1,6 +1,12 @@
 #ifndef LUAT_AIRLINK_H
 #define LUAT_AIRLINK_H
 
+#ifdef LUAT_USE_PSRAM
+#define AIRLINK_MEM_TYPE LUAT_HEAP_PSRAM
+#else
+#define AIRLINK_MEM_TYPE LUAT_HEAP_SRAM
+#endif
+
 typedef struct luat_airlink_cmd_ext
 {
     uint64_t pkgid;

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

@@ -67,7 +67,7 @@ static int sdata_cb(lua_State *L, void *ptr)
     lua_getglobal(L, "sys_pub");
     lua_pushstring(L, "AIRLINK_SDATA");
     lua_pushlstring(L, msg->ptr, msg->arg1);
-    luat_heap_opt_free(LUAT_HEAP_PSRAM, msg->ptr);
+    luat_heap_opt_free(AIRLINK_MEM_TYPE, msg->ptr);
     lua_call(L, 2, 0);
     return 0;
 }
@@ -77,7 +77,7 @@ int luat_airlink_cmd_exec_sdata(luat_airlink_cmd_t *cmd, void *userdata)
     LLOGD("收到sdata指令!!!");
     rtos_msg_t msg = {0};
     msg.handler = sdata_cb;
-    msg.ptr = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, cmd->len);
+    msg.ptr = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, cmd->len);
     if (msg.ptr == NULL)
     {
         LLOGE("sdata_cb malloc fail!!! %d", cmd->len);

+ 24 - 4
components/airlink/src/luat_airlink.c

@@ -12,6 +12,12 @@
 #define LUAT_LOG_TAG "airlink"
 #include "luat_log.h"
 
+#ifdef LUAT_USE_PSRAM
+#define AIRLINK_MEM_TYPE LUAT_HEAP_PSRAM
+#else
+#define AIRLINK_MEM_TYPE LUAT_HEAP_SRAM
+#endif
+
 luat_rtos_queue_t airlink_cmd_queue;
 luat_rtos_queue_t airlink_ippkg_queue;
 
@@ -170,10 +176,24 @@ int luat_airlink_queue_send_ippkg(uint8_t adapter_id, uint8_t *data, size_t len)
             return -3;
         }
     }
+    // 检查内存状态, 如果内存不足, 就直接丢弃掉
+    size_t total = 0;
+    size_t used = 0;
+    size_t max_used = 0;
+    luat_meminfo_opt_sys(AIRLINK_MEM_TYPE, &total, &used, &max_used);
+    if (total - used < 32*1024 && len > 512) {
+        LLOGW("内存相对不足(%d), 丢弃掉大包(%d)", total - used, len);
+        return -3;
+    }
+    else if (total - used < 8*1024) {
+        // 内存严重不足, 抛弃所有的包
+        LLOGW("内存严重不足(%d), 丢弃掉所有包(%d)", total - used, len);
+        return -4;
+    }
 
     airlink_queue_item_t item = {
         .len = len + 5,
-        .cmd = luat_heap_malloc(len + 8),
+        .cmd = luat_heap_opt_zalloc(AIRLINK_MEM_TYPE, len + 8),
     };
     if (item.cmd == NULL)
     {
@@ -242,7 +262,7 @@ void luat_airlink_hexdump(const char* tag, uint8_t* buff, uint16_t len) {
     if (len > 500) {
         len = 500;
     }
-    uint8_t* tmp = luat_heap_opt_zalloc(LUAT_HEAP_PSRAM, len * 2 + 1);
+    uint8_t* tmp = luat_heap_opt_zalloc(AIRLINK_MEM_TYPE, len * 2 + 1);
     if (tmp == NULL) {
         return;
     }
@@ -251,7 +271,7 @@ void luat_airlink_hexdump(const char* tag, uint8_t* buff, uint16_t len) {
         sprintf((char*)(tmp + i * 2), "%02X", buff[i]);
     }
     LLOGD("%s %s", tag, tmp);
-    luat_heap_opt_free(LUAT_HEAP_PSRAM, tmp);
+    luat_heap_opt_free(AIRLINK_MEM_TYPE, tmp);
 }
 
 static uint64_t next_cmd_id;
@@ -272,6 +292,6 @@ luat_airlink_cmd_t* luat_airlink_cmd_new(uint16_t cmd_id, uint16_t data_len) {
 
 void luat_airlink_cmd_free(luat_airlink_cmd_t* cmd) {
     if (cmd) {
-        luat_heap_opt_free(LUAT_HEAP_PSRAM, cmd);
+        luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
     }
 }

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

@@ -110,8 +110,8 @@ static void spi_master_task(void *param)
     int tmpval = 0;
     luat_event_t event = {0};
     airlink_queue_item_t item = {0};
-	uint8_t* txbuff = luat_heap_opt_malloc(LUAT_HEAP_SRAM, TEST_BUFF_SIZE);
-    uint8_t* rxbuff = luat_heap_opt_malloc(LUAT_HEAP_SRAM, TEST_BUFF_SIZE);
+	uint8_t* txbuff = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, TEST_BUFF_SIZE);
+    uint8_t* rxbuff = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, TEST_BUFF_SIZE);
 
     luat_rtos_task_sleep(5); // 等5ms
     spi_gpio_setup();
@@ -137,7 +137,7 @@ static void spi_master_task(void *param)
         if (item.len > 0 && item.cmd != NULL) {
             // LLOGD("发送待传输的数据, 塞入SPI的FIFO %d", item.len);
             luat_airlink_data_pack(item.cmd, item.len, txbuff);
-            luat_heap_free(item.cmd);
+            luat_airlink_cmd_free(item.cmd);
         }
         else {
             // LLOGD("填充PING数据");

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

@@ -21,7 +21,7 @@ void luat_airlink_on_data_recv(uint8_t *data, size_t len) {
         luat_airlink_cmd_exec_ip_pkg(cmd, NULL);
         return;
     }
-    void* ptr = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, len);
+    void* ptr = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, len);
     if (ptr == NULL) {
         LLOGE("airlink分配内存失败!!! %d", len);
         return;
@@ -65,7 +65,7 @@ static int luat_airlink_task(void *param) {
             }
 
             // 处理完成, 释放内存
-            luat_heap_opt_free(LUAT_HEAP_PSRAM, ptr);
+            luat_heap_opt_free(AIRLINK_MEM_TYPE, ptr);
         }
     }
     return 0;