[bug] 修正rtc对时间参数范围的处理 | fix a bug of rtc
This commit is contained in:
parent
5e9f525c41
commit
991b6e78b3
|
@ -38,7 +38,7 @@ RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegiste
|
|||
return;
|
||||
}
|
||||
|
||||
static void get_rtc_timeval(struct timeval *tv)
|
||||
static rt_err_t stm32_rtc_get_timeval(struct timeval *tv)
|
||||
{
|
||||
RTC_TimeTypeDef RTC_TimeStruct = {0};
|
||||
RTC_DateTypeDef RTC_DateStruct = {0};
|
||||
|
@ -59,6 +59,8 @@ static void get_rtc_timeval(struct timeval *tv)
|
|||
#if defined(SOC_SERIES_STM32H7)
|
||||
tv->tv_usec = (255.0 - RTC_TimeStruct.SubSeconds * 1.0) / 256.0 * 1000.0 * 1000.0;
|
||||
#endif
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t set_rtc_time_stamp(time_t time_stamp)
|
||||
|
@ -244,36 +246,30 @@ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
|
|||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t stm32_rtc_get_secs(void *args)
|
||||
static rt_err_t stm32_rtc_get_secs(time_t *sec)
|
||||
{
|
||||
struct timeval tv;
|
||||
get_rtc_timeval(&tv);
|
||||
*(rt_uint32_t *) args = tv.tv_sec;
|
||||
LOG_D("RTC: get rtc_time %x", *(rt_uint32_t *)args);
|
||||
|
||||
stm32_rtc_get_timeval(&tv);
|
||||
*(time_t *) sec = tv.tv_sec;
|
||||
LOG_D("RTC: get rtc_time %d", *sec);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t stm32_rtc_set_secs(void *args)
|
||||
static rt_err_t stm32_rtc_set_secs(time_t *sec)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
|
||||
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
|
||||
if (set_rtc_time_stamp(*sec))
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
LOG_D("RTC: set rtc_time %x", *(rt_uint32_t *)args);
|
||||
LOG_D("RTC: set rtc_time %d", *sec);
|
||||
|
||||
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,
|
||||
|
|
|
@ -289,9 +289,9 @@ static void date(int argc, char **argv)
|
|||
gettimeofday(&tv, &tz);
|
||||
now = tv.tv_sec;
|
||||
/* output current time */
|
||||
rt_kprintf("local: %.*s", 25, ctime(&now));
|
||||
rt_kprintf("stamp: %ld\n", (long)tv.tv_sec);
|
||||
rt_kprintf("tz: %c%d\n", -tz.tz_minuteswest > 0 ? '+' : '-', -tz.tz_minuteswest / 60);
|
||||
rt_kprintf("local time: %.*s", 25, ctime(&now));
|
||||
rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec);
|
||||
rt_kprintf("timezone: UTC%c%d\n", -tz.tz_minuteswest > 0 ? '+' : '-', -tz.tz_minuteswest / 60);
|
||||
}
|
||||
else if (argc >= 7)
|
||||
{
|
||||
|
@ -301,17 +301,17 @@ static void date(int argc, char **argv)
|
|||
rt_err_t err;
|
||||
|
||||
tm_new.tm_year = atoi(argv[1]) - 1900;
|
||||
tm_new.tm_mon = atoi(argv[2]);
|
||||
tm_new.tm_mon = atoi(argv[2]) - 1; /* .tm_min's range is [0-11] */
|
||||
tm_new.tm_mday = atoi(argv[3]);
|
||||
tm_new.tm_hour = atoi(argv[4]);
|
||||
tm_new.tm_min = atoi(argv[5]);
|
||||
tm_new.tm_sec = atoi(argv[6]);
|
||||
if (tm_new.tm_year > 199 || tm_new.tm_year < 100)
|
||||
if (tm_new.tm_year <= 0)
|
||||
{
|
||||
rt_kprintf("year is out of range [2000-2099]\n");
|
||||
rt_kprintf("year is out of range [1900-]\n");
|
||||
return;
|
||||
}
|
||||
if (tm_new.tm_mon == 0 || tm_new.tm_mon > 12)
|
||||
if (tm_new.tm_mon > 11) /* .tm_min's range is [0-11] */
|
||||
{
|
||||
rt_kprintf("month is out of range [1-12]\n");
|
||||
return;
|
||||
|
@ -331,9 +331,9 @@ static void date(int argc, char **argv)
|
|||
rt_kprintf("minute is out of range [0-59]\n");
|
||||
return;
|
||||
}
|
||||
if (tm_new.tm_sec > 59)
|
||||
if (tm_new.tm_sec > 60)
|
||||
{
|
||||
rt_kprintf("second is out of range [0-59]\n");
|
||||
rt_kprintf("second is out of range [0-60]\n");
|
||||
return;
|
||||
}
|
||||
/* save old timestamp */
|
||||
|
@ -351,8 +351,7 @@ static void date(int argc, char **argv)
|
|||
rt_kprintf("set date failed. %d\n", err);
|
||||
return;
|
||||
}
|
||||
/* get new timestamp */
|
||||
get_timestamp(&now);
|
||||
get_timestamp(&now); /* get new timestamp */
|
||||
rt_kprintf("old: %.*s", 25, ctime(&old));
|
||||
rt_kprintf("now: %.*s", 25, ctime(&now));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue