From 4f4ef32d1b7c5ecfd1ad2a4e3032c0460d04f713 Mon Sep 17 00:00:00 2001 From: "CSSC-WORK\\murmur" Date: Thu, 7 Sep 2023 15:24:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RTC=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=92=8CACK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/RS_485.c | 10 +++--- applications/core.c | 2 +- applications/func/func.c | 75 ++++++++++++++++++++++++++++++++++++++++ applications/func/func.h | 2 ++ 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/applications/RS_485.c b/applications/RS_485.c index bb0e2e0..3d1b2a9 100644 --- a/applications/RS_485.c +++ b/applications/RS_485.c @@ -30,7 +30,7 @@ static rt_timer_t timer485=RT_NULL; -static rt_sem_t sendcmd=RT_NULL; +static rt_mutex_t sendcmd=RT_NULL; #ifndef MIN_FRAME_LEN #define MIN_FRAME_LEN 10 #endif @@ -97,7 +97,7 @@ static BATT_INFO batt; static void timer485_cb() { - rt_sem_release(sendcmd); + rt_mutex_release(sendcmd); } static int b2v(uint8_t *din) { @@ -200,7 +200,7 @@ static void serial485_thread_entry(void *parameter) parseBattInfo(rx_buffer, rx_length); } - rt_thread_mdelay(9*60*1000); + rt_thread_mdelay(1*60*1000); } @@ -269,7 +269,7 @@ static int uart485_dma_sample(int argc, char *argv[]) } sendcmd = rt_mutex_create("reqBatt", RT_IPC_FLAG_FIFO); - timer485 = rt_timer_create("batt", timer485_cb, RT_NULL, rt_tick_from_millisecond(10*1000), RT_TIMER_FLAG_PERIODIC); + timer485 = rt_timer_create("batt", timer485_cb, RT_NULL, rt_tick_from_millisecond(6*10*1000), RT_TIMER_FLAG_PERIODIC); /* 启动定时器1 */ if (timer485 != RT_NULL) @@ -284,6 +284,6 @@ static int uart485_dma_sample(int argc, char *argv[]) /* 导出到 msh 命令列表中 */ //MSH_CMD_EXPORT(uart485_dma_sample,uart485_dma_sample); -INIT_APP_EXPORT(uart485_dma_sample); +INIT_COMPONENT_EXPORT(uart485_dma_sample); MSH_CMD_EXPORT(dumpBattInfo,解析电池信息); diff --git a/applications/core.c b/applications/core.c index e32f4a6..ca2544e 100644 --- a/applications/core.c +++ b/applications/core.c @@ -707,7 +707,7 @@ void deInitTT_thread_entry() } pwTT_thread_entry("0"); - startAlarm();//alarm 引起崩溃 + startAlarm(); clearWindowMode(); setManualWindow(-1); LOG_W("shunt down TT DONE"); diff --git a/applications/func/func.c b/applications/func/func.c index fd418f4..8597283 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -21,6 +21,7 @@ extern int pointInPolygon(int polyCorners,float polyX[], float polyY[],float x,f extern rt_sem_t okToreport; extern void resetTM(void); unsigned long getFileSize(char *file); +void updateSysRTC(uint8_t *din, size_t len); #define CFG_ACK /** * 配置文件后的ack信号 @@ -1020,6 +1021,9 @@ void ttRunCMD_thread_entry(uint8_t *din, size_t len) case _INFO_BATT: parseBattInfo(din+8, din[7]); break; + case _CMD_SET_RTC: + updateSysRTC(din+8, din[7]); + break; default: LOG_W("0x%04X=未支持的指令。",cmd); break; @@ -1686,6 +1690,77 @@ void reportINFO(void) } //INIT_APP_EXPORT(reportINFO); +/** + * 设置3S的RTC时间 + */ +void set3SRTC(void) +{ + uint8_t cmd[16]={0x5A, 0xA5, ADDR_3S, ADDR_ANJI, _CMD_SET_RTC >> 8, _CMD_SET_RTC & 0xFF, 0x00, 0x06 }; + uint8_t tm[10]; + time2Byte(tm); + memcpy(cmd+8,tm,6); + cmd[14]=bccCRC(cmd+2, 14); + cmd[15]=0xED; + LOG_HEX("sRTC",16,cmd,sizeof(cmd)); + formatAndSendTo3S(cmd, sizeof(cmd)); +} + +/** + * 更新系统RTC时间 + * @param din + * @param len + */ +void updateSysRTC(uint8_t *din, size_t len) +{ + LOG_I("FUNC = sest RTC"); + + struct tm tm_new = { 0 }; + time_t old = (time_t)0; + time_t now = (time_t)0; + + int err = get_timestamp(&old); + if (err != RT_EOK) + { + LOG_E("Get current timestamp failed. %d", err); + return; + } + + din[0] += 2000 - 1900; + din[1] -= 1; +// memcpy(tm_new, din, sizeof(tm_new)); + tm_new.tm_year = din[0]; + tm_new.tm_mon = din[1]; + tm_new.tm_mday = din[2]; + tm_new.tm_hour = din[3]; + tm_new.tm_min = din[4]; + tm_new.tm_sec = din[5]; + + /* converts the local time into the calendar time. */ + now = mktime(&tm_new); + err = set_timestamp(now); + if (err != RT_EOK) + { + LOG_E("set date failed. %d", err); + return; + } + + get_timestamp(&now); /* get new timestamp */ + rt_kprintf("old: %.*s", 25, ctime(&old)); + rt_kprintf("now: %.*s", 25, ctime(&now)); + + //ACK + uint8_t rst[16]={0x5A, 0xA5, ADDR_ANJI, ADDR_TT, _INFO_RTC_ANSWER >> 8, _INFO_RTC_ANSWER & 0xFF, 0x00, 0x06 }; + uint8_t tm[10]; + time2Byte(tm); + memcpy(rst+8,tm,6); + rst[14]=bccCRC(rst+2, 14); + rst[15]=0xED; + LOG_HEX("sRTC",16,rst,sizeof(rst)); + if (isTTjh()) { + upSend(rst, sizeof(rst)); + } + +} #define FUNC_DEMO #ifdef FUNC_DEMO //测试时导出命令到控制台 diff --git a/applications/func/func.h b/applications/func/func.h index 9ba0015..0f45aff 100644 --- a/applications/func/func.h +++ b/applications/func/func.h @@ -74,6 +74,8 @@ enum //3S #define _CMD_DEPTH_REQUEST 0x0601 #define _CMD_RTC_REQUEST 0x410F +#define _CMD_SET_RTC 0x4006 +#define _INFO_RTC_ANSWER 0x4016 //需补充信息 #define _INFO_DEPTH_ANSWER 0x0608 //深度回复