From 2967e909007b2d312b405419313f19cf60d00255 Mon Sep 17 00:00:00 2001 From: caisking Date: Mon, 27 Apr 2026 16:13:33 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E9=87=8D?= =?utf8?q?=E7=BD=AE=E9=97=AE=E9=A2=98=E7=9A=84=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../flashboot/brandy/boot_ulp/boot_ulp.c | 61 +++++++++++++++---- .../chips/brandy/exception/chip_system_time.c | 17 +++++- code/sdk/zhihui/utils/custom_utils.c | 19 +++++- 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/code/sdk/bootloader/flashboot/brandy/boot_ulp/boot_ulp.c b/code/sdk/bootloader/flashboot/brandy/boot_ulp/boot_ulp.c index 48e79357e..bfe411af9 100644 --- a/code/sdk/bootloader/flashboot/brandy/boot_ulp/boot_ulp.c +++ b/code/sdk/bootloader/flashboot/brandy/boot_ulp/boot_ulp.c @@ -11,6 +11,7 @@ #include "errcode.h" #include "chip_io.h" #include "clock_calibration.h" +#include "debug_print.h" bool g_startup_finish_flag = false; @@ -27,18 +28,56 @@ static uint32_t g_clock_ulp_ssb = 3276800; calendar_regs_t *g_calendar_reg = SSB_CALENDAR_BASE_ADDR; -static uint64_t uapi_ulp_systick_get_count(void) -{ - uint64_t count = 0; - writew(ULP_LOCK_REG, 0x1); - count = (uint64_t)readw(ULP_SYSTICK_VAL3) << 48U; - count |= (uint64_t)readw(ULP_SYSTICK_VAL2) << 32U; - count |= (uint64_t)readw(ULP_SYSTICK_VAL1) << 16U; - count |= (uint64_t)readw(ULP_SYSTICK_VAL0); - writew(ULP_LOCK_REG, 0x0); - return count; -} +#if 0 + static uint64_t uapi_ulp_systick_get_count(void) + { + uint64_t count = 0; + writew(ULP_LOCK_REG, 0x1); + count = (uint64_t)readw(ULP_SYSTICK_VAL3) << 48U; + count |= (uint64_t)readw(ULP_SYSTICK_VAL2) << 32U; + count |= (uint64_t)readw(ULP_SYSTICK_VAL1) << 16U; + count |= (uint64_t)readw(ULP_SYSTICK_VAL0); + writew(ULP_LOCK_REG, 0x0); + return count; + } +#else + static uint64_t uapi_ulp_systick_get_count(void) + { + uint64_t count = 0; + static uint64_t s_last_count = 0; + uint16_t lock_before = readw(ULP_LOCK_REG); + uint16_t lock_after_set = (uint16_t)(lock_before | 0x1); + writew(ULP_LOCK_REG, lock_after_set); + + uint16_t v0 = readw(ULP_SYSTICK_VAL0); + uint16_t v1 = readw(ULP_SYSTICK_VAL1); + uint16_t v2 = readw(ULP_SYSTICK_VAL2); + uint16_t v3 = readw(ULP_SYSTICK_VAL3); + + count = ((uint64_t)v3 << 48U) | + ((uint64_t)v2 << 32U) | + ((uint64_t)v1 << 16U) | + ((uint64_t)v0); + + uint16_t lock_before_restore = readw(ULP_LOCK_REG); + writew(ULP_LOCK_REG, lock_before); + uint16_t lock_after_restore = readw(ULP_LOCK_REG); + + PRINT("[ULP_CNT][boot] lock_before=0x%04x lock_set=0x%04x lock_before_restore=0x%04x lock_after_restore=0x%04x " + "v3=0x%04x v2=0x%04x v1=0x%04x v0=0x%04x count=0x%016llx (%llu)\r\n", + lock_before, lock_after_set, lock_before_restore, lock_after_restore, + v3, v2, v1, v0, + (unsigned long long)count, (unsigned long long)count); + + if (s_last_count != 0 && count < s_last_count) { + PRINT("[ULP_CNT][WARN][boot] rollback! last=0x%016llx now=0x%016llx\r\n", + (unsigned long long)s_last_count, (unsigned long long)count); + } + s_last_count = count; + return count; + } +#endif static uint32_t uapi_ulp_systick_soc_calibration(void) { unsigned int irq_sts = osal_irq_lock(); diff --git a/code/sdk/middleware/chips/brandy/exception/chip_system_time.c b/code/sdk/middleware/chips/brandy/exception/chip_system_time.c index e3ce9be13..ad7e09309 100644 --- a/code/sdk/middleware/chips/brandy/exception/chip_system_time.c +++ b/code/sdk/middleware/chips/brandy/exception/chip_system_time.c @@ -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) diff --git a/code/sdk/zhihui/utils/custom_utils.c b/code/sdk/zhihui/utils/custom_utils.c index b3e1f9c73..6a7728efd 100644 --- a/code/sdk/zhihui/utils/custom_utils.c +++ b/code/sdk/zhihui/utils/custom_utils.c @@ -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; -- 2.17.1