diff --git a/bsp/simulator/drivers/drv_rtc.c b/bsp/simulator/drivers/drv_rtc.c index 7e19714da2575046a08e284a8a858fea715d21da..8543b4c9262fb516058f1a3135cf40d7af734c4a 100644 --- a/bsp/simulator/drivers/drv_rtc.c +++ b/bsp/simulator/drivers/drv_rtc.c @@ -45,6 +45,23 @@ static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) #endif +static void get_rtc_timeval(struct timeval *tv) +{ + struct tm newtime = { 0 }; + SYSTEMTIME sys_time; + + GetSystemTime(&sys_time); + newtime.tm_year = sys_time.wYear - 1900; + newtime.tm_mon = sys_time.wMonth - 1; + newtime.tm_mday = sys_time.wDay; + newtime.tm_hour = sys_time.wHour; + newtime.tm_min = sys_time.wMinute; + newtime.tm_sec = sys_time.wSecond; + + tv->tv_sec = timegm(&newtime); + tv->tv_usec = sys_time.wMilliseconds * 1000UL; +} + static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { __time32_t *t; @@ -56,17 +73,14 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { case RT_DEVICE_CTRL_RTC_GET_TIME: { - t = (__time32_t *)args; - SYSTEMTIME sys_time; - - GetSystemTime(&sys_time); - newtime.tm_year = sys_time.wYear - 1900; - newtime.tm_mon = sys_time.wMonth - 1; - newtime.tm_mday = sys_time.wDay; - newtime.tm_hour = sys_time.wHour; - newtime.tm_min = sys_time.wMinute; - newtime.tm_sec = sys_time.wSecond; - *t = timegm(&newtime); + struct timeval tv; + get_rtc_timeval(&tv); + *(rt_uint32_t *) args = tv.tv_sec; + break; + } + case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: + { + get_rtc_timeval((struct timeval *) args); break; } case RT_DEVICE_CTRL_RTC_SET_TIME: @@ -85,14 +99,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) soft_rtc_alarm_update(&wkalarm); break; #endif - case RT_DEVICE_CTRL_RTC_GET_TIME_US: - { - long *tv_usec = (long *)args; - SYSTEMTIME sys_time; - GetSystemTime(&sys_time); - *tv_usec = sys_time.wMilliseconds * 1000UL; - break; - } default: return -RT_ERROR; } diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c b/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c index 278fce140ba0fa0fd1d75d7cd621208f75d3b882..46d09dd16fdc7b53b5de43201be1ba45b2b74497 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c @@ -38,7 +38,7 @@ RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegiste return; } -static time_t get_rtc_timestamp(void) +static void get_rtc_timeval(struct timeval *tv) { RTC_TimeTypeDef RTC_TimeStruct = {0}; RTC_DateTypeDef RTC_DateStruct = {0}; @@ -54,8 +54,11 @@ static time_t get_rtc_timestamp(void) tm_new.tm_mon = RTC_DateStruct.Month - 1; tm_new.tm_year = RTC_DateStruct.Year + 100; - LOG_D("get rtc time."); - return timegm(&tm_new); + tv->tv_sec = timegm(&tm_new); + +#if defined(SOC_SERIES_STM32H7) + tv->tv_usec = (255.0 - RTC_TimeStruct.SubSeconds * 1.0) / 256.0 * 1000.0 * 1000.0; +#endif } static rt_err_t set_rtc_time_stamp(time_t time_stamp) @@ -243,7 +246,9 @@ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1; static rt_err_t stm32_rtc_get_secs(void *args) { - *(rt_uint32_t *)args = get_rtc_timestamp(); + struct timeval tv; + get_rtc_timeval(&tv); + *(rt_uint32_t *) args = tv.tv_sec; LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args); return RT_EOK; @@ -262,6 +267,13 @@ static rt_err_t stm32_rtc_set_secs(void *args) return result; } +static rt_err_t stm32_rtc_get_timeval(void *args) +{ + get_rtc_timeval((struct timeval *) args); + + return RT_EOK; +} + static const struct rt_rtc_ops stm32_rtc_ops = { stm32_rtc_init, @@ -269,7 +281,7 @@ static const struct rt_rtc_ops stm32_rtc_ops = stm32_rtc_set_secs, RT_NULL, RT_NULL, - RT_NULL, + stm32_rtc_get_timeval, RT_NULL, }; diff --git a/components/drivers/include/drivers/rtc.h b/components/drivers/include/drivers/rtc.h index 0bafbad7ad4795f065599fffa016c2d988843983..5b333161f8a78cf814719b5f8c013cbdda4130f1 100644 --- a/components/drivers/include/drivers/rtc.h +++ b/components/drivers/include/drivers/rtc.h @@ -15,12 +15,12 @@ #include -#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */ -#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */ -#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */ -#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */ -#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */ -#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */ +#define RT_DEVICE_CTRL_RTC_GET_TIME 0x20 /**< get second time */ +#define RT_DEVICE_CTRL_RTC_SET_TIME 0x21 /**< set second time */ +#define RT_DEVICE_CTRL_RTC_GET_TIMEVAL 0x22 /**< get timeval for gettimeofday */ +#define RT_DEVICE_CTRL_RTC_SET_TIMEVAL 0x23 /**< set timeval for gettimeofday */ +#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x24 /**< get alarm */ +#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x25 /**< set alarm */ struct rt_rtc_ops { @@ -29,8 +29,8 @@ struct rt_rtc_ops rt_err_t (*set_secs)(void *arg); rt_err_t (*get_alarm)(void *arg); rt_err_t (*set_alarm)(void *arg); - rt_err_t (*get_usecs)(void *arg); - rt_err_t (*set_usecs)(void *arg); + rt_err_t (*get_timeval)(void *arg); + rt_err_t (*set_timeval)(void *arg); }; typedef struct rt_rtc_device diff --git a/components/drivers/rtc/rtc.c b/components/drivers/rtc/rtc.c index bc75c83e7dd410addf8d99b8f8d3af6081028970..0658d2aaf4c2129805980c3b0aa6a114c525f981 100644 --- a/components/drivers/rtc/rtc.c +++ b/components/drivers/rtc/rtc.c @@ -71,11 +71,11 @@ static rt_err_t rt_rtc_control(struct rt_device *dev, int cmd, void *args) case RT_DEVICE_CTRL_RTC_SET_TIME: ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args); break; - case RT_DEVICE_CTRL_RTC_GET_TIME_US: - ret = TRY_DO_RTC_FUNC(rtc_device, get_usecs, args); + case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: + ret = TRY_DO_RTC_FUNC(rtc_device, get_timeval, args); break; - case RT_DEVICE_CTRL_RTC_SET_TIME_US: - ret = TRY_DO_RTC_FUNC(rtc_device, set_usecs, args); + case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: + ret = TRY_DO_RTC_FUNC(rtc_device, set_timeval, args); break; case RT_DEVICE_CTRL_RTC_GET_ALARM: ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args); diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index f81844f8e9aa78498ca35d83f9cd1bdbc23212e5..24289088fb75b40a0c64e54dd4c138e1eb2ecea1 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -101,7 +101,7 @@ static rt_err_t get_timeval(struct timeval *tv) if (rt_device_open(device, 0) == RT_EOK) { rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec); - rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME_US, &tv->tv_usec); + rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIMEVAL, tv); rt_device_close(device); } } @@ -147,7 +147,7 @@ static int set_timeval(struct timeval *tv) if (rt_device_open(device, 0) == RT_EOK) { rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec); - rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME_US, &tv->tv_usec); + rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIMEVAL, tv); rt_device_close(device); } } diff --git a/include/rtdef.h b/include/rtdef.h index 3f5f4c5ef95b15735b962205ee47ab91fbf22bcc..a324eea122b81fbaef515936687f9bf095305ad7 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -966,13 +966,13 @@ enum rt_device_class_type /** * special device commands */ -#define RT_DEVICE_CTRL_CHAR_STREAM 0x10 /**< stream mode on char device */ -#define RT_DEVICE_CTRL_BLK_GETGEOME 0x10 /**< get geometry information */ -#define RT_DEVICE_CTRL_BLK_SYNC 0x11 /**< flush data to block device */ -#define RT_DEVICE_CTRL_BLK_ERASE 0x12 /**< erase block on block device */ -#define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x13 /**< block device : enter/exit auto refresh mode */ -#define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /**< get mac address */ -#define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */ +#define RT_DEVICE_CTRL_CHAR_STREAM 0x20 /**< stream mode on char device */ +#define RT_DEVICE_CTRL_BLK_GETGEOME 0x20 /**< get geometry information */ +#define RT_DEVICE_CTRL_BLK_SYNC 0x21 /**< flush data to block device */ +#define RT_DEVICE_CTRL_BLK_ERASE 0x22 /**< erase block on block device */ +#define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x23 /**< block device : enter/exit auto refresh mode */ +#define RT_DEVICE_CTRL_NETIF_GETMAC 0x20 /**< get mac address */ +#define RT_DEVICE_CTRL_MTD_FORMAT 0x20 /**< format a MTD device */ typedef struct rt_device *rt_device_t;