1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 */ 9 #include <sys/time.h> 10 #include <rtthread.h> 11 12 #ifdef RT_USING_DEVICE 13 int gettimeofday(struct timeval *tp, void *ignore) 14 { 15 time_t time; 16 rt_device_t device; 17 18 device = rt_device_find("rtc"); 19 if (device != RT_NULL) 20 { 21 rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time); 22 if (tp != RT_NULL) 23 { 24 tp->tv_sec = time; 25 tp->tv_usec = 0; 26 } 27 28 return time; 29 } 30 31 return 0; 32 } 33 #endif 34 35 /** 36 * Returns the current time. 37 * 38 * @param time_t * t the timestamp pointer, if not used, keep NULL. 39 * 40 * @return time_t return timestamp current. 41 * 42 */ 43 /* for IAR 6.2 later Compiler */ 44 #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000 45 #pragma module_name = "?time" 46 time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */ 47 #else 48 time_t time(time_t *t) 49 #endif 50 { 51 time_t time_now = 0; 52 53 #ifdef RT_USING_RTC 54 static rt_device_t device = RT_NULL; 55 56 /* optimization: find rtc device only first. */ 57 if (device == RT_NULL) 58 { 59 device = rt_device_find("rtc"); 60 } 61 62 /* read timestamp from RTC device. */ 63 if (device != RT_NULL) 64 { 65 if (rt_device_open(device, 0) == RT_EOK) 66 { 67 rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now); 68 rt_device_close(device); 69 } 70 } 71 #endif /* RT_USING_RTC */ 72 73 /* if t is not NULL, write timestamp to *t */ 74 if (t != RT_NULL) 75 { 76 *t = time_now; 77 } 78 79 return time_now; 80 } 81 82 RT_WEAK clock_t clock(void) 83 { 84 return rt_tick_get(); 85 } 86