增加时间重置问题的定位 3374/1 3374/head 3385/target/1
authorcaisking <caiyqm@qq.com>
Mon, 27 Apr 2026 08:13:33 +0000 (16:13 +0800)
committercaisking <caiyqm@qq.com>
Mon, 27 Apr 2026 08:13:33 +0000 (16:13 +0800)
code/sdk/bootloader/flashboot/brandy/boot_ulp/boot_ulp.c
code/sdk/middleware/chips/brandy/exception/chip_system_time.c
code/sdk/zhihui/utils/custom_utils.c

index 48e7935..bfe411a 100644 (file)
@@ -11,6 +11,7 @@
 #include "errcode.h"\r
 #include "chip_io.h"\r
 #include "clock_calibration.h"\r
+#include "debug_print.h"\r
 \r
 \r
 bool  g_startup_finish_flag = false;\r
@@ -27,18 +28,56 @@ static uint32_t g_clock_ulp_ssb = 3276800;
 \r
 calendar_regs_t *g_calendar_reg = SSB_CALENDAR_BASE_ADDR;\r
 \r
-static uint64_t uapi_ulp_systick_get_count(void)\r
-{\r
-    uint64_t count = 0;\r
-    writew(ULP_LOCK_REG, 0x1);\r
-    count = (uint64_t)readw(ULP_SYSTICK_VAL3) << 48U;\r
-    count |= (uint64_t)readw(ULP_SYSTICK_VAL2) << 32U;\r
-    count |= (uint64_t)readw(ULP_SYSTICK_VAL1) << 16U;\r
-    count |= (uint64_t)readw(ULP_SYSTICK_VAL0);\r
-    writew(ULP_LOCK_REG, 0x0);\r
-    return count;\r
-}\r
+#if 0\r
+    static uint64_t uapi_ulp_systick_get_count(void)\r
+    {\r
+        uint64_t count = 0;\r
+        writew(ULP_LOCK_REG, 0x1);\r
+        count = (uint64_t)readw(ULP_SYSTICK_VAL3) << 48U;\r
+        count |= (uint64_t)readw(ULP_SYSTICK_VAL2) << 32U;\r
+        count |= (uint64_t)readw(ULP_SYSTICK_VAL1) << 16U;\r
+        count |= (uint64_t)readw(ULP_SYSTICK_VAL0);\r
+        writew(ULP_LOCK_REG, 0x0);\r
+        return count;\r
+    }\r
+#else\r
+    static uint64_t uapi_ulp_systick_get_count(void)\r
+    {\r
+        uint64_t count = 0;\r
+        static uint64_t s_last_count = 0;\r
+        uint16_t lock_before = readw(ULP_LOCK_REG);\r
+        uint16_t lock_after_set = (uint16_t)(lock_before | 0x1);\r
+        writew(ULP_LOCK_REG, lock_after_set);\r
+\r
+        uint16_t v0 = readw(ULP_SYSTICK_VAL0);\r
+        uint16_t v1 = readw(ULP_SYSTICK_VAL1);\r
+        uint16_t v2 = readw(ULP_SYSTICK_VAL2);\r
+        uint16_t v3 = readw(ULP_SYSTICK_VAL3);\r
+\r
+        count = ((uint64_t)v3 << 48U) |\r
+                ((uint64_t)v2 << 32U) |\r
+                ((uint64_t)v1 << 16U) |\r
+                ((uint64_t)v0);\r
+\r
+        uint16_t lock_before_restore = readw(ULP_LOCK_REG);\r
+        writew(ULP_LOCK_REG, lock_before);\r
+        uint16_t lock_after_restore = readw(ULP_LOCK_REG);\r
+\r
+        PRINT("[ULP_CNT][boot] lock_before=0x%04x lock_set=0x%04x lock_before_restore=0x%04x lock_after_restore=0x%04x "\r
+              "v3=0x%04x v2=0x%04x v1=0x%04x v0=0x%04x count=0x%016llx (%llu)\r\n",\r
+              lock_before, lock_after_set, lock_before_restore, lock_after_restore,\r
+              v3, v2, v1, v0,\r
+              (unsigned long long)count, (unsigned long long)count);\r
+\r
+        if (s_last_count != 0 && count < s_last_count) {\r
+            PRINT("[ULP_CNT][WARN][boot] rollback! last=0x%016llx now=0x%016llx\r\n",\r
+                  (unsigned long long)s_last_count, (unsigned long long)count);\r
+        }\r
+        s_last_count = count;\r
 \r
+        return count;\r
+    }\r
+#endif\r
 static uint32_t uapi_ulp_systick_soc_calibration(void)\r
 {\r
     unsigned int irq_sts = osal_irq_lock();\r
index e3ce9be..ad7e093 100644 (file)
@@ -17,6 +17,7 @@
 #define TZ_ADDRESS              0x5702C014
 #define MAX_TIMEZONE_HOURS      24
 #define TIME_MAX                64
+#define TIME_FLOW_PREFIX        "[TIME_FLOW][时间链路] "
 
 void print_time(calendar_t *date)
 {
@@ -59,6 +60,8 @@ void save_time(void)
 {
     uint64_t time_sec = get_local_timestamp();
     time_sec = time_sec * MSEC_PER_SEC;
+    printf(TIME_FLOW_PREFIX "save_time: local_sec=%u, write_calendar_ms=%llu\r\n",
+        (uint32_t)get_local_timestamp(), (unsigned long long)time_sec);
     uapi_calendar_set_timestamp(time_sec);
 }
 
@@ -102,7 +105,11 @@ void restore_time(void)
     uapi_calendar_get_timestamp(&sec);
     printf("callendar timestamp = %lld \r\n", sec);
     sec = sec / MSEC_PER_SEC;
+    printf(TIME_FLOW_PREFIX "restore_time: read_calendar_sec=%llu, timezone_quarters=%u\r\n",
+        (unsigned long long)sec, uapi_get_timezone());
     sync_time(sec, uapi_get_timezone());
+    printf(TIME_FLOW_PREFIX "restore_time: after_sync utc_now=%u, local_now=%u\r\n",
+        get_current_timestamp(), get_local_timestamp());
 }
 uint32_t sync_time_utc(uint32_t timestamp, uint32_t timezone_quarters)
 {
@@ -130,7 +137,10 @@ uint32_t sync_time_utc(uint32_t timestamp, uint32_t timezone_quarters)
     tv.tv_usec = 0;
     tzone.tz_dsttime = 0;
     tzone.tz_minuteswest = timezone_quarters * MINUTES_PER_QUARTER;
-    return settimeofday(&tv, &tzone);
+    uint32_t ret = settimeofday(&tv, &tzone);
+    printf(TIME_FLOW_PREFIX "sync_time_utc: ret=0x%x, input_utc=%u, zone_quarters=%u, utc_now=%u, local_now=%u\r\n",
+        ret, timestamp, timezone_quarters, get_current_timestamp(), get_local_timestamp());
+    return ret;
 }
 
 uint32_t sync_time(uint32_t timestamp, uint32_t timezone_quarters)
@@ -154,7 +164,10 @@ uint32_t sync_time(uint32_t timestamp, uint32_t timezone_quarters)
     tv.tv_usec = 0;
     tzone.tz_dsttime = 0;
     tzone.tz_minuteswest = timezone_quarters * MINUTES_PER_QUARTER;
-    return settimeofday(&tv, &tzone);
+    uint32_t ret = settimeofday(&tv, &tzone);
+    printf(TIME_FLOW_PREFIX "sync_time: ret=0x%x, input_local=%u, zone_quarters=%u, utc_now=%u, local_now=%u\r\n",
+        ret, timestamp, timezone_quarters, get_current_timestamp(), get_local_timestamp());
+    return ret;
 }
 
 // void test_bandy_time(uint32_t time)
index b3e1f9c..6a7728e 100644 (file)
@@ -946,6 +946,7 @@ errcode_t uaip_cmd_update_request(void)
 /* Use lwip time functions instead of musl */
 #include "time.h"
 // #include "chip_system_time.h"
+#define TIME_FLOW_PREFIX "[TIME_FLOW][时间链路] "
 
 errcode_t uapi_get_timestampe_ms(uint32_t *tm)
 {
@@ -967,7 +968,9 @@ errcode_t uapi_get_timestampe_ss(uint32_t *tm)
     }
  
     *tm = get_current_timestamp();
-    //printf("[timestamp:%u]\n", *tm);
+    if (*tm == 0) {
+        PRINT(TIME_FLOW_PREFIX "读取当前时间戳为0(UTC秒),可能尚未完成时间恢复/校时" NEWLINE);
+    }
     return ERRCODE_SUCC;
 }
 
@@ -991,6 +994,7 @@ errcode_t uapi_get_timestampe_ss_extend(uint32_t *tm,int year, int month, int da
 
 errcode_t uaip_set_time_utc_zone(uint32_t seconds,int16_t zone_quarters)
 {    
+    PRINT(TIME_FLOW_PREFIX "设置UTC时间:seconds=%u, zone_quarters=%d" NEWLINE, seconds, zone_quarters);
     return sync_time_utc(seconds,zone_quarters);
     // return 0;
 }
@@ -1003,6 +1007,7 @@ errcode_t uaip_sntp_set_time_utc(uint32_t seconds)
 
     printf(" zone_quarters : %d\n",zone_quarters);
    
+    PRINT(TIME_FLOW_PREFIX "SNTP校时开始:utc_seconds=%u, zone_quarters=%d" NEWLINE, seconds, zone_quarters);
     errcode_t ret = sync_time_utc(seconds,zone_quarters);
 
 
@@ -1019,6 +1024,8 @@ errcode_t uaip_sntp_set_time_utc(uint32_t seconds)
     timeinfo.tm_min,
     timeinfo.tm_sec,zone_quarters,ret);
     printf("utc timestamp %d , local timestamp %d, timezone %ld\n",get_current_timestamp(),get_local_timestamp(),timezone);
+    PRINT(TIME_FLOW_PREFIX "SNTP校时结束:ret=0x%x, utc_now=%u, local_now=%u" NEWLINE,
+        ret, get_current_timestamp(), get_local_timestamp());
     
      return ERRCODE_SUCC;
 }
@@ -1027,6 +1034,7 @@ errcode_t uaip_set_time_local(uint32_t seconds)
 {
      int zone_quarters = uapi_get_timezone();
 
+    PRINT(TIME_FLOW_PREFIX "设置本地时间开始:local_seconds=%u, zone_quarters=%d" NEWLINE, seconds, zone_quarters);
     sync_time(seconds,zone_quarters);
 
     time_t rawtime;
@@ -1043,6 +1051,8 @@ errcode_t uaip_set_time_local(uint32_t seconds)
     timeinfo.tm_sec,zone_quarters);
 
     printf("utc timestamp %d , local timestamp %d\n",get_current_timestamp(),get_local_timestamp());
+    PRINT(TIME_FLOW_PREFIX "设置本地时间结束:utc_now=%u, local_now=%u" NEWLINE,
+        get_current_timestamp(), get_local_timestamp());
     
     return ERRCODE_SUCC;
 
@@ -1281,6 +1291,7 @@ errcode_t uapi_set_time_from_cat1(char* cat1time)
 
      if (ret == 0 )
      {
+            PRINT(TIME_FLOW_PREFIX "CAT1时间设置开始:zone=%d, utc_seconds=%u" NEWLINE, zone, sec);
             struct timeval tv = { 0 };
             struct timezone tz = { 0 };
 
@@ -1289,6 +1300,8 @@ errcode_t uapi_set_time_from_cat1(char* cat1time)
             tz.tz_dsttime = 0;
             tz.tz_minuteswest = zone*60;
             settimeofday(&tv, &tz);
+            PRINT(TIME_FLOW_PREFIX "CAT1时间设置完成:utc_now=%u, local_now=%u" NEWLINE,
+                get_current_timestamp(), get_local_timestamp());
 
      }else{
         printf("convert to time failed ret %d!\n",ret);
@@ -2574,6 +2587,7 @@ void uapi_restore_factory_setting(void)
 uint32_t uapi_calendar_store_date(void)
 {
 #if 1
+     PRINT(TIME_FLOW_PREFIX "关机/重启前保存时间到Calendar寄存器:local_now=%u" NEWLINE, get_local_timestamp());
      save_time();
      return ERRCODE_SUCC;
 #else
@@ -2604,7 +2618,10 @@ uint32_t uapi_calendar_store_date(void)
 uint32_t uapi_calendar_recover_date(void)
 {
 #if 1
+    PRINT(TIME_FLOW_PREFIX "开机恢复时间开始:准备从Calendar寄存器恢复系统时间" NEWLINE);
     restore_time();
+    PRINT(TIME_FLOW_PREFIX "开机恢复时间结束:utc_now=%u, local_now=%u" NEWLINE,
+        get_current_timestamp(), get_local_timestamp());
     return ERRCODE_SUCC;
 #else
     calendar_t date;