diff --git a/applications/SCCM_link.c b/applications/SCCM_link.c index 62bf311..8d6cb10 100644 --- a/applications/SCCM_link.c +++ b/applications/SCCM_link.c @@ -63,29 +63,22 @@ void uart_thread_entry(void *parameter) if (!req) continue; rt_strncpy(req->input, (const char *)input, sizeof(req->input)); req->input_len = len; - req->sem = rt_malloc(sizeof(struct rt_semaphore)); - if (!req->sem) { - rt_free(req); - continue; - } - rt_sem_init(req->sem, "uart_resp", 0, RT_IPC_FLAG_FIFO); // 发送到处理线程 - if (rt_mq_send(proc_mq, &req, sizeof(req)) == RT_EOK) + if (rt_mq_send(request_mq, &req, sizeof(req)) == RT_EOK) { // 等待处理完成 - if (rt_sem_take(req->sem, rt_tick_from_millisecond(100)) >= RT_EOK) + if (rt_mq_recv(response_mq, &req, sizeof(req), 500) >= RT_EOK) { if(req->output_len>0) rs485_send(hinst, req->output, req->output_len); } } // 清理 - rt_sem_detach(req->sem); // 或 rt_sem_delete - rt_free(req->sem); rt_free(req); } } } + extern struct rt_memheap sram_DTCMRAM; static struct rt_thread dat_uart_thread; static void *dat_uart_stack __attribute__((aligned(4)))= RT_NULL ; diff --git a/applications/data/DATA_comm.c b/applications/data/DATA_comm.c index 909d091..9dce2b7 100644 --- a/applications/data/DATA_comm.c +++ b/applications/data/DATA_comm.c @@ -24,8 +24,8 @@ #define DBG_LVL DBG_LOG #include -rt_mq_t proc_mq; -//static struct proc_request *req; +struct rt_messagequeue *request_mq; // UART → Proc +struct rt_messagequeue *response_mq; // Proc → UART char cjson_falg=0;//是否解析cjson char *json_buffer=NULL;// @@ -535,7 +535,7 @@ void proc_thread_entry(void *parameter) while (1) { // 接收请求指针 - if (rt_mq_recv(proc_mq, &req, sizeof(req), RT_WAITING_FOREVER) >= RT_EOK) + if (rt_mq_recv(request_mq, &req, sizeof(req), RT_WAITING_FOREVER) >= RT_EOK) { if (!req) continue; memcpy(DATA_api,req->input,5); @@ -565,11 +565,8 @@ void proc_thread_entry(void *parameter) { req->input[i]=0; } - // 通知 UART 线程可以发送了 - if (req->sem) - { - rt_sem_release(req->sem); - } + // 发送到 UART 线程 + rt_mq_send(response_mq, &req, sizeof(req)); } } } @@ -580,8 +577,9 @@ static void *dat_comm_stack __attribute__((aligned(4)))= RT_NULL ; int data_comm_init(void) { rt_err_t dat_err; - proc_mq = rt_mq_create("proc_mq", sizeof(struct proc_request*), 5, RT_IPC_FLAG_FIFO); - if (proc_mq == RT_NULL) + request_mq = rt_mq_create("request_mq", sizeof(struct proc_request*), 5, RT_IPC_FLAG_FIFO); + response_mq = rt_mq_create("response_mq", sizeof(struct proc_request*), 5, RT_IPC_FLAG_FIFO); + if ((request_mq == RT_NULL) || (response_mq ==RT_NULL)) { LOG_E("Failed to create message queue!\n"); return -1; diff --git a/applications/data/DATA_comm.h b/applications/data/DATA_comm.h index 41edfe6..370396b 100644 --- a/applications/data/DATA_comm.h +++ b/applications/data/DATA_comm.h @@ -20,11 +20,11 @@ struct proc_request { int input_len; char output[MAX_OUTPUT_LEN]; int output_len; - struct rt_semaphore *sem; // 指向动态分配的信号量 }; -// 消息队列(供外部使用) -extern rt_mq_t proc_mq; +// 双消息队列声明 +extern struct rt_messagequeue *request_mq; // UART → Proc +extern struct rt_messagequeue *response_mq; // Proc → UART int data_comm_init(void); diff --git a/applications/sql/DB_SQLite.c b/applications/sql/DB_SQLite.c index 4270be1..12b47ec 100644 --- a/applications/sql/DB_SQLite.c +++ b/applications/sql/DB_SQLite.c @@ -276,6 +276,8 @@ static void db_sqlite(void *parameter) { // 默认启用数据库 db_enabled = RT_TRUE; static rt_bool_t first_wait_done = RT_FALSE; + //连续链接次数 + rt_int16_t op_link=0; while (1) { // 接收命令 @@ -284,12 +286,23 @@ static void db_sqlite(void *parameter) { if (first_wait_done) { first_wait_done = RT_FALSE; - //sqlite3_exec(g_db, "PRAGMA shrink_memory;", 0, 0, 0); + sqlite3_exec(g_db, "PRAGMA shrink_memory;", 0, 0, 0); sqlite3_close(g_db); g_db=RT_NULL; + op_link=0; } continue; - }else{first_wait_done = RT_TRUE;} + }else{ + if(op_link>1000) + {//连续操作数据库1000次后强制关闭防止内存占用 + sqlite3_close(g_db); + g_db=RT_NULL; + op_link=0; + } + op_link++; + + first_wait_done = RT_TRUE; + } //检查排队超时 rt_tick_t now = rt_tick_get(); @@ -328,8 +341,8 @@ static void db_sqlite(void *parameter) { return_disabled(&cmd); continue; } else { - //限制缓存大小 (100KB) - //sqlite3_exec(g_db, "PRAGMA cache_size = -50;", 0, 0, 0); + //限制缓存大小 (KB) + sqlite3_exec(g_db, "PRAGMA cache_size = 8;", 0, 0, 0); //设置临时存储为文件模式 (节省内存) //sqlite3_exec(g_db, "PRAGMA temp_store = FILE;", 0, 0, 0); //rt_kprintf("Opened database: %s\n", DB_NAME);