Skip to content

Commit ca5d53f

Browse files
authored
Merge pull request #40 from armink/fix_rtc
拉取
2 parents a4a01ed + 76be91c commit ca5d53f

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

bsp/simulator/drivers/drv_rtc.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm)
4848
static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
4949
{
5050
__time32_t *t;
51-
struct tm *newtime;
51+
struct tm newtime;
5252

5353
RT_ASSERT(dev != RT_NULL);
5454

@@ -57,10 +57,16 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
5757
case RT_DEVICE_CTRL_RTC_GET_TIME:
5858
{
5959
t = (__time32_t *)args;
60-
_time32(t);
61-
/* TODO The libc time module not support timezone now. So get the time from locatime. */
62-
newtime = _localtime32(t);
63-
*t = _mkgmtime32(newtime);
60+
SYSTEMTIME sys_time;
61+
62+
GetSystemTime(&sys_time);
63+
newtime.tm_year = sys_time.wYear - 1900;
64+
newtime.tm_mon = sys_time.wMonth - 1;
65+
newtime.tm_mday = sys_time.wDay;
66+
newtime.tm_hour = sys_time.wHour;
67+
newtime.tm_min = sys_time.wMinute;
68+
newtime.tm_sec = sys_time.wSecond;
69+
*t = timegm(&newtime);
6470
break;
6571
}
6672
case RT_DEVICE_CTRL_RTC_SET_TIME:
@@ -79,6 +85,16 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
7985
soft_rtc_alarm_update(&wkalarm);
8086
break;
8187
#endif
88+
case RT_DEVICE_CTRL_RTC_GET_TIME_US:
89+
{
90+
long *tv_usec = (long *)args;
91+
SYSTEMTIME sys_time;
92+
GetSystemTime(&sys_time);
93+
*tv_usec = sys_time.wMilliseconds * 1000UL;
94+
break;
95+
}
96+
default:
97+
return -RT_ERROR;
8298
}
8399

84100
return RT_EOK;

components/libc/compilers/common/time.c

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct tm* localtime_r(const time_t* t, struct tm* r)
120120
time_t local_tz;
121121
int utc_plus;
122122

123-
utc_plus = 0; /* GTM: UTC+0 */
123+
utc_plus = 8; /* GMT: UTC+8 */
124124
local_tz = *t + utc_plus * 3600;
125125
return gmtime_r(&local_tz, r);
126126
}
@@ -183,18 +183,14 @@ char* ctime(const time_t *tim_p)
183183
}
184184
RTM_EXPORT(ctime);
185185

186-
/**
187-
* Returns the current time.
188-
*
189-
* @param time_t * t the timestamp pointer, if not used, keep NULL.
190-
*
191-
* @return The value ((time_t)-1) is returned if the calendar time is not available.
192-
* If timer is not a NULL pointer, the return value is also stored in timer.
193-
*
194-
*/
195-
RT_WEAK time_t time(time_t *t)
186+
static void get_timeval(struct timeval *tv)
196187
{
197-
time_t time_now = ((time_t)-1); /* default is not available */
188+
if (tv == RT_NULL)
189+
return;
190+
/* default is not available */
191+
tv->tv_sec = -1;
192+
/* default is 0 */
193+
tv->tv_usec = 0;
198194

199195
#ifdef RT_USING_RTC
200196
static rt_device_t device = RT_NULL;
@@ -210,26 +206,41 @@ RT_WEAK time_t time(time_t *t)
210206
{
211207
if (rt_device_open(device, 0) == RT_EOK)
212208
{
213-
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
209+
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &tv->tv_sec);
210+
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME_US, &tv->tv_usec);
214211
rt_device_close(device);
215212
}
216213
}
217214
#endif /* RT_USING_RTC */
218215

219-
/* if t is not NULL, write timestamp to *t */
220-
if (t != RT_NULL)
221-
{
222-
*t = time_now;
223-
}
224-
225-
if(time_now == (time_t)-1)
216+
if (tv->tv_sec == (time_t) -1)
226217
{
227218
/* LOG_W will cause a recursive printing if ulog timestamp function is turned on */
228219
rt_kprintf("Cannot find a RTC device to provide time!\r\n");
229220
errno = ENOSYS;
230221
}
222+
}
231223

232-
return time_now;
224+
/**
225+
* Returns the current time.
226+
*
227+
* @param time_t * t the timestamp pointer, if not used, keep NULL.
228+
*
229+
* @return The value ((time_t)-1) is returned if the calendar time is not available.
230+
* If timer is not a NULL pointer, the return value is also stored in timer.
231+
*
232+
*/
233+
RT_WEAK time_t time(time_t *t)
234+
{
235+
struct timeval now;
236+
237+
get_timeval(&now);
238+
239+
if (t)
240+
{
241+
*t = now.tv_sec;
242+
}
243+
return now.tv_sec;
233244
}
234245
RTM_EXPORT(time);
235246

@@ -344,12 +355,10 @@ RTM_EXPORT(timegm);
344355
/* TODO: timezone */
345356
int gettimeofday(struct timeval *tv, struct timezone *tz)
346357
{
347-
time_t t = time(RT_NULL);
358+
get_timeval(tv);
348359

349-
if (tv != RT_NULL && t != (time_t)-1)
360+
if (tv != RT_NULL && tv->tv_sec != (time_t) -1)
350361
{
351-
tv->tv_sec = t;
352-
tv->tv_usec = 0;
353362
return 0;
354363
}
355364
else

include/rtdef.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,10 +962,12 @@ enum rt_device_class_type
962962
#define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x13 /**< block device : enter/exit auto refresh mode */
963963
#define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /**< get mac address */
964964
#define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */
965-
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get time */
966-
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set time */
965+
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */
966+
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */
967967
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x12 /**< get alarm */
968968
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x13 /**< set alarm */
969+
#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x14 /**< get microsecond time */
970+
#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x15 /**< set microsecond time */
969971

970972
typedef struct rt_device *rt_device_t;
971973

0 commit comments

Comments
 (0)