From c0e8ec1407296765fb7fe05b73cecde901bbfd91 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Tue, 14 Nov 2023 18:18:07 +0800 Subject: [PATCH] [libc] update ctime.c compatible with old drivers --- components/libc/compilers/common/ctime.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/components/libc/compilers/common/ctime.c b/components/libc/compilers/common/ctime.c index f2cc76df4777..baf5c2495da4 100644 --- a/components/libc/compilers/common/ctime.c +++ b/components/libc/compilers/common/ctime.c @@ -459,6 +459,7 @@ RTM_EXPORT(timegm); int gettimeofday(struct timeval *tv, struct timezone *tz) { + rt_err_t ret; /* The use of the timezone structure is obsolete; * the tz argument should normally be specified as NULL. * The tz_dsttime field has never been used under Linux. @@ -478,9 +479,16 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) { tv->tv_sec = 0; tv->tv_usec = 0; - - if (_control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv) == RT_EOK) + ret = _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv); + if (ret == RT_EOK) + { return 0; + } + else if (ret == -RT_EINVAL) /* RT_DEVICE_CTRL_RTC_GET_TIMEVAL */ + { + ret = _control_rtc(RT_DEVICE_CTRL_RTC_GET_TIME, (void *)&tv->tv_sec); + } + return 0; } rt_set_errno(EINVAL); @@ -490,6 +498,7 @@ RTM_EXPORT(gettimeofday); int settimeofday(const struct timeval *tv, const struct timezone *tz) { + rt_err_t ret; /* The use of the timezone structure is obsolete; * the tz argument should normally be specified as NULL. * The tz_dsttime field has never been used under Linux. @@ -497,8 +506,15 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz) */ if (tv != RT_NULL && (long)tv->tv_usec >= 0 && (long)tv->tv_sec >= 0) { - if (_control_rtc(RT_DEVICE_CTRL_RTC_SET_TIMEVAL, (void *)tv) == RT_EOK) + ret = _control_rtc(RT_DEVICE_CTRL_RTC_SET_TIMEVAL, (void *)tv); + if (ret == RT_EOK) + { return 0; + } + else if (ret == -RT_EINVAL) /* RT_DEVICE_CTRL_RTC_SET_TIME */ + { + ret = _control_rtc(RT_DEVICE_CTRL_RTC_SET_TIME, (void *)&tv->tv_sec); + } } rt_set_errno(EINVAL);