Merge pull request #5113 from armink/fix_rtc

Fix rtc
This commit is contained in:
Bernard Xiong 2021-09-30 18:25:54 +08:00 committed by GitHub
commit b60adb2d04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 45 deletions

View File

@ -45,6 +45,23 @@ static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm)
#endif #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) static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
{ {
__time32_t *t; __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: case RT_DEVICE_CTRL_RTC_GET_TIME:
{ {
t = (__time32_t *)args; struct timeval tv;
SYSTEMTIME sys_time; get_rtc_timeval(&tv);
*(rt_uint32_t *) args = tv.tv_sec;
GetSystemTime(&sys_time); break;
newtime.tm_year = sys_time.wYear - 1900; }
newtime.tm_mon = sys_time.wMonth - 1; case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
newtime.tm_mday = sys_time.wDay; {
newtime.tm_hour = sys_time.wHour; get_rtc_timeval((struct timeval *) args);
newtime.tm_min = sys_time.wMinute;
newtime.tm_sec = sys_time.wSecond;
*t = timegm(&newtime);
break; break;
} }
case RT_DEVICE_CTRL_RTC_SET_TIME: 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); soft_rtc_alarm_update(&wkalarm);
break; break;
#endif #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: default:
return -RT_ERROR; return -RT_ERROR;
} }

View File

@ -38,7 +38,7 @@ RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegiste
return; return;
} }
static time_t get_rtc_timestamp(void) static void get_rtc_timeval(struct timeval *tv)
{ {
RTC_TimeTypeDef RTC_TimeStruct = {0}; RTC_TimeTypeDef RTC_TimeStruct = {0};
RTC_DateTypeDef RTC_DateStruct = {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_mon = RTC_DateStruct.Month - 1;
tm_new.tm_year = RTC_DateStruct.Year + 100; tm_new.tm_year = RTC_DateStruct.Year + 100;
LOG_D("get rtc time."); tv->tv_sec = timegm(&tm_new);
return 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) 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) 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); LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
return RT_EOK; return RT_EOK;
@ -262,6 +267,13 @@ static rt_err_t stm32_rtc_set_secs(void *args)
return result; 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 = static const struct rt_rtc_ops stm32_rtc_ops =
{ {
stm32_rtc_init, stm32_rtc_init,
@ -269,7 +281,7 @@ static const struct rt_rtc_ops stm32_rtc_ops =
stm32_rtc_set_secs, stm32_rtc_set_secs,
RT_NULL, RT_NULL,
RT_NULL, RT_NULL,
RT_NULL, stm32_rtc_get_timeval,
RT_NULL, RT_NULL,
}; };

View File

@ -15,12 +15,12 @@
#include <rtdef.h> #include <rtdef.h>
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */ #define RT_DEVICE_CTRL_RTC_GET_TIME 0x20 /**< get second time */
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */ #define RT_DEVICE_CTRL_RTC_SET_TIME 0x21 /**< set second time */
#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */ #define RT_DEVICE_CTRL_RTC_GET_TIMEVAL 0x22 /**< get timeval for gettimeofday */
#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */ #define RT_DEVICE_CTRL_RTC_SET_TIMEVAL 0x23 /**< set timeval for gettimeofday */
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */ #define RT_DEVICE_CTRL_RTC_GET_ALARM 0x24 /**< get alarm */
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */ #define RT_DEVICE_CTRL_RTC_SET_ALARM 0x25 /**< set alarm */
struct rt_rtc_ops struct rt_rtc_ops
{ {
@ -29,8 +29,8 @@ struct rt_rtc_ops
rt_err_t (*set_secs)(void *arg); rt_err_t (*set_secs)(void *arg);
rt_err_t (*get_alarm)(void *arg); rt_err_t (*get_alarm)(void *arg);
rt_err_t (*set_alarm)(void *arg); rt_err_t (*set_alarm)(void *arg);
rt_err_t (*get_usecs)(void *arg); rt_err_t (*get_timeval)(void *arg);
rt_err_t (*set_usecs)(void *arg); rt_err_t (*set_timeval)(void *arg);
}; };
typedef struct rt_rtc_device typedef struct rt_rtc_device

View File

@ -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: case RT_DEVICE_CTRL_RTC_SET_TIME:
ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args); ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args);
break; break;
case RT_DEVICE_CTRL_RTC_GET_TIME_US: case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
ret = TRY_DO_RTC_FUNC(rtc_device, get_usecs, args); ret = TRY_DO_RTC_FUNC(rtc_device, get_timeval, args);
break; break;
case RT_DEVICE_CTRL_RTC_SET_TIME_US: case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
ret = TRY_DO_RTC_FUNC(rtc_device, set_usecs, args); ret = TRY_DO_RTC_FUNC(rtc_device, set_timeval, args);
break; break;
case RT_DEVICE_CTRL_RTC_GET_ALARM: case RT_DEVICE_CTRL_RTC_GET_ALARM:
ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args); ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args);

View File

@ -101,7 +101,7 @@ static rt_err_t get_timeval(struct timeval *tv)
if (rt_device_open(device, 0) == RT_EOK) if (rt_device_open(device, 0) == RT_EOK)
{ {
rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec); 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); rt_device_close(device);
} }
} }
@ -147,7 +147,7 @@ static int set_timeval(struct timeval *tv)
if (rt_device_open(device, 0) == RT_EOK) if (rt_device_open(device, 0) == RT_EOK)
{ {
rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec); 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); rt_device_close(device);
} }
} }

View File

@ -966,13 +966,13 @@ enum rt_device_class_type
/** /**
* special device commands * special device commands
*/ */
#define RT_DEVICE_CTRL_CHAR_STREAM 0x10 /**< stream mode on char device */ #define RT_DEVICE_CTRL_CHAR_STREAM 0x20 /**< stream mode on char device */
#define RT_DEVICE_CTRL_BLK_GETGEOME 0x10 /**< get geometry information */ #define RT_DEVICE_CTRL_BLK_GETGEOME 0x20 /**< get geometry information */
#define RT_DEVICE_CTRL_BLK_SYNC 0x11 /**< flush data to block device */ #define RT_DEVICE_CTRL_BLK_SYNC 0x21 /**< flush data to block device */
#define RT_DEVICE_CTRL_BLK_ERASE 0x12 /**< erase block on block device */ #define RT_DEVICE_CTRL_BLK_ERASE 0x22 /**< erase block on block device */
#define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x13 /**< block device : enter/exit auto refresh mode */ #define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x23 /**< block device : enter/exit auto refresh mode */
#define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /**< get mac address */ #define RT_DEVICE_CTRL_NETIF_GETMAC 0x20 /**< get mac address */
#define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */ #define RT_DEVICE_CTRL_MTD_FORMAT 0x20 /**< format a MTD device */
typedef struct rt_device *rt_device_t; typedef struct rt_device *rt_device_t;