#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
\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
#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)
{
{
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);
}
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)
{
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)
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)
/* 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)
{
}
*tm = get_current_timestamp();
- //printf("[timestamp:%u]\n", *tm);
+ if (*tm == 0) {
+ PRINT(TIME_FLOW_PREFIX "读取当前时间戳为0(UTC秒),可能尚未完成时间恢复/校时" NEWLINE);
+ }
return ERRCODE_SUCC;
}
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;
}
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);
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;
}
{
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;
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;
if (ret == 0 )
{
+ PRINT(TIME_FLOW_PREFIX "CAT1时间设置开始:zone=%d, utc_seconds=%u" NEWLINE, zone, sec);
struct timeval tv = { 0 };
struct timezone tz = { 0 };
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);
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
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;