diff --git a/components/drivers/rtc/rtc.c b/components/drivers/rtc/rtc.c index df5e635453a048bf23f4d6e105c4e3a3dee62c10..72756e4c5050c6f395704506309dce7de0c29f37 100644 --- a/components/drivers/rtc/rtc.c +++ b/components/drivers/rtc/rtc.c @@ -44,7 +44,11 @@ time_t time(time_t* t) /* read timestamp from RTC device. */ if (device != RT_NULL) { - rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now); + if (rt_device_open(device, 0) == RT_EOK) + { + rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now); + rt_device_close(device); + } } /* if t is not NULL, write timestamp to *t */ @@ -70,6 +74,7 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) struct tm * p_tm; struct tm tm_new; rt_device_t device; + rt_err_t ret = RT_ERROR; /* get current time */ now = time(RT_NULL); @@ -94,11 +99,13 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) device = rt_device_find("rtc"); if (device == RT_NULL) { - return -RT_ERROR; + return RT_ERROR; } + + ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); /* update to RTC device. */ - return rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); + return ret; } /** \brief set system time(date not modify). @@ -115,6 +122,7 @@ rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) struct tm * p_tm; struct tm tm_new; rt_device_t device; + rt_err_t ret = RT_ERROR; /* get current time */ now = time(RT_NULL); @@ -142,13 +150,15 @@ rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) return RT_ERROR; } + ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); + /* update to RTC device. */ - return rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now); + return ret; } #ifdef RT_USING_FINSH #include - +#include void list_date(void) { time_t now;