@@ -465,38 +465,42 @@ RTM_EXPORT(difftime);
465465RTM_EXPORT (strftime );
466466
467467#ifdef RT_USING_POSIX
468+
469+ #ifdef RT_USING_RTC
468470static volatile struct timeval _timevalue ;
469471static int _rt_clock_time_system_init ()
470472{
471- time_t time ;
473+ time_t time = 0 ;
472474 rt_tick_t tick ;
473475 rt_device_t device ;
474476
475- time = 0 ;
476-
477- #ifdef RT_USING_RTC
478477 device = rt_device_find ("rtc" );
479478 if (device != RT_NULL )
480479 {
481480 /* get realtime seconds */
482- rt_device_control (device , RT_DEVICE_CTRL_RTC_GET_TIME , & time );
481+ if (rt_device_control (device , RT_DEVICE_CTRL_RTC_GET_TIME , & time ) == RT_EOK )
482+ {
483+ /* get tick */
484+ tick = rt_tick_get ();
485+ _timevalue .tv_usec = (tick %RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ;
486+ _timevalue .tv_sec = time - tick /RT_TICK_PER_SECOND - 1 ;
487+ return 0 ;
488+ }
483489 }
484- #else
485- LOG_W ("Cannot find a RTC device to provide time!" );
486- #endif
487-
488- /* get tick */
489- tick = rt_tick_get ();
490-
491- _timevalue .tv_usec = (tick %RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ;
492- _timevalue .tv_sec = time - tick /RT_TICK_PER_SECOND - 1 ;
493490
494- return 0 ;
491+ _timevalue .tv_usec = 0 ;
492+ _timevalue .tv_sec = 0 ;
493+ return -1 ;
495494}
496495INIT_COMPONENT_EXPORT (_rt_clock_time_system_init );
496+ #endif /* RT_USING_RTC */
497497
498498int clock_getres (clockid_t clockid , struct timespec * res )
499499{
500+ #ifndef RT_USING_RTC
501+ LOG_W ("Cannot find a RTC device to save time!" );
502+ return -1 ;
503+ #else
500504 int ret = 0 ;
501505
502506 if (res == RT_NULL )
@@ -526,11 +530,16 @@ int clock_getres(clockid_t clockid, struct timespec *res)
526530 }
527531
528532 return ret ;
533+ #endif /* RT_USING_RTC */
529534}
530535RTM_EXPORT (clock_getres );
531536
532537int clock_gettime (clockid_t clockid , struct timespec * tp )
533538{
539+ #ifndef RT_USING_RTC
540+ LOG_W ("Cannot find a RTC device to save time!" );
541+ return -1 ;
542+ #else
534543 int ret = 0 ;
535544
536545 if (tp == RT_NULL )
@@ -571,19 +580,23 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
571580 }
572581
573582 return ret ;
583+ #endif /* RT_USING_RTC */
574584}
575585RTM_EXPORT (clock_gettime );
576586
577587int clock_settime (clockid_t clockid , const struct timespec * tp )
578588{
589+ #ifndef RT_USING_RTC
590+ LOG_W ("Cannot find a RTC device to save time!" );
591+ return -1 ;
592+ #else
579593 int second ;
580594 rt_tick_t tick ;
581595 rt_device_t device ;
582596
583597 if ((clockid != CLOCK_REALTIME ) || (tp == RT_NULL ))
584598 {
585599 rt_set_errno (EINVAL );
586-
587600 return -1 ;
588601 }
589602
@@ -596,21 +609,19 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
596609 _timevalue .tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ;
597610 _timevalue .tv_sec = second - tick /RT_TICK_PER_SECOND - 1 ;
598611
599- #ifdef RT_USING_RTC
600612 /* update for RTC device */
601613 device = rt_device_find ("rtc" );
602614 if (device != RT_NULL )
603615 {
604616 /* set realtime seconds */
605- rt_device_control (device , RT_DEVICE_CTRL_RTC_SET_TIME , & second );
617+ if (rt_device_control (device , RT_DEVICE_CTRL_RTC_SET_TIME , & second ) == RT_EOK )
618+ {
619+ return 0 ;
620+ }
606621 }
607- else
608- #else
609- LOG_W ("Cannot find a RTC device to save time!" );
610- #endif
611- return -1 ;
612622
613- return 0 ;
623+ return -1 ;
624+ #endif /* RT_USING_RTC */
614625}
615626RTM_EXPORT (clock_settime );
616627
0 commit comments