From 9809c138b73d3cbe1a3ba3a1c801af6601f7d8f4 Mon Sep 17 00:00:00 2001 From: "sc943313837@gmail.com" Date: Thu, 27 Sep 2012 16:31:07 +0000 Subject: [PATCH] add open operation git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2309 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/drivers/rtc/rtc.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/components/drivers/rtc/rtc.c b/components/drivers/rtc/rtc.c index df5e635453..72756e4c50 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;