2424#include "sys/time.h"
2525#include <sys/errno.h>
2626#include <rtthread.h>
27-
27+ #include <rthw.h>
2828#ifdef RT_USING_DEVICE
2929#include <rtdevice.h>
3030#endif
@@ -465,38 +465,47 @@ RTM_EXPORT(difftime);
465465RTM_EXPORT (strftime );
466466
467467#ifdef RT_USING_POSIX
468- static struct timeval _timevalue ;
469- static int clock_time_system_init ()
468+
469+ #ifdef RT_USING_RTC
470+ static volatile struct timeval _timevalue ;
471+ static int _rt_clock_time_system_init ()
470472{
471- time_t time ;
473+ register rt_base_t level ;
474+ time_t time = 0 ;
472475 rt_tick_t tick ;
473476 rt_device_t device ;
474477
475- time = 0 ;
476-
477- #ifdef RT_USING_RTC
478478 device = rt_device_find ("rtc" );
479479 if (device != RT_NULL )
480480 {
481481 /* get realtime seconds */
482- rt_device_control (device , RT_DEVICE_CTRL_RTC_GET_TIME , & time );
482+ if (rt_device_control (device , RT_DEVICE_CTRL_RTC_GET_TIME , & time ) == RT_EOK )
483+ {
484+ level = rt_hw_interrupt_disable ();
485+ tick = rt_tick_get (); /* get tick */
486+ _timevalue .tv_usec = (tick %RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ;
487+ _timevalue .tv_sec = time - tick /RT_TICK_PER_SECOND - 1 ;
488+ rt_hw_interrupt_enable (level );
489+ return 0 ;
490+ }
483491 }
484- #else
485- LOG_W ("Cannot find a RTC device to provide time!" );
486- #endif
487-
488- /* get tick */
489- tick = rt_tick_get ();
490492
491- _timevalue .tv_usec = (tick %RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ;
492- _timevalue .tv_sec = time - tick /RT_TICK_PER_SECOND - 1 ;
493+ level = rt_hw_interrupt_disable ();
494+ _timevalue .tv_usec = 0 ;
495+ _timevalue .tv_sec = 0 ;
496+ rt_hw_interrupt_enable (level );
493497
494- return 0 ;
498+ return -1 ;
495499}
496- INIT_COMPONENT_EXPORT (clock_time_system_init );
500+ INIT_COMPONENT_EXPORT (_rt_clock_time_system_init );
501+ #endif /* RT_USING_RTC */
497502
498503int clock_getres (clockid_t clockid , struct timespec * res )
499504{
505+ #ifndef RT_USING_RTC
506+ LOG_W ("Cannot find a RTC device to save time!" );
507+ return -1 ;
508+ #else
500509 int ret = 0 ;
501510
502511 if (res == RT_NULL )
@@ -526,11 +535,16 @@ int clock_getres(clockid_t clockid, struct timespec *res)
526535 }
527536
528537 return ret ;
538+ #endif /* RT_USING_RTC */
529539}
530540RTM_EXPORT (clock_getres );
531541
532542int clock_gettime (clockid_t clockid , struct timespec * tp )
533543{
544+ #ifndef RT_USING_RTC
545+ LOG_W ("Cannot find a RTC device to save time!" );
546+ return -1 ;
547+ #else
534548 int ret = 0 ;
535549
536550 if (tp == RT_NULL )
@@ -543,11 +557,14 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
543557 {
544558 case CLOCK_REALTIME :
545559 {
546- /* get tick */
547- int tick = rt_tick_get () ;
560+ int tick ;
561+ register rt_base_t level ;
548562
563+ level = rt_hw_interrupt_disable ();
564+ tick = rt_tick_get (); /* get tick */
549565 tp -> tv_sec = _timevalue .tv_sec + tick / RT_TICK_PER_SECOND ;
550566 tp -> tv_nsec = (_timevalue .tv_usec + (tick % RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ) * 1000 ;
567+ rt_hw_interrupt_enable (level );
551568 }
552569 break ;
553570
@@ -571,50 +588,54 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
571588 }
572589
573590 return ret ;
591+ #endif /* RT_USING_RTC */
574592}
575593RTM_EXPORT (clock_gettime );
576594
577595int clock_settime (clockid_t clockid , const struct timespec * tp )
578596{
597+ #ifndef RT_USING_RTC
598+ LOG_W ("Cannot find a RTC device to save time!" );
599+ return -1 ;
600+ #else
601+ register rt_base_t level ;
579602 int second ;
580603 rt_tick_t tick ;
581604 rt_device_t device ;
582605
583606 if ((clockid != CLOCK_REALTIME ) || (tp == RT_NULL ))
584607 {
585608 rt_set_errno (EINVAL );
586-
587609 return -1 ;
588610 }
589611
590612 /* get second */
591613 second = tp -> tv_sec ;
592- /* get tick */
593- tick = rt_tick_get ();
594614
615+ level = rt_hw_interrupt_disable ();
616+ tick = rt_tick_get (); /* get tick */
595617 /* update timevalue */
596618 _timevalue .tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND ) * MICROSECOND_PER_TICK ;
597619 _timevalue .tv_sec = second - tick /RT_TICK_PER_SECOND - 1 ;
620+ rt_hw_interrupt_enable (level );
598621
599- #ifdef RT_USING_RTC
600622 /* update for RTC device */
601623 device = rt_device_find ("rtc" );
602624 if (device != RT_NULL )
603625 {
604626 /* set realtime seconds */
605- rt_device_control (device , RT_DEVICE_CTRL_RTC_SET_TIME , & second );
627+ if (rt_device_control (device , RT_DEVICE_CTRL_RTC_SET_TIME , & second ) == RT_EOK )
628+ {
629+ return 0 ;
630+ }
606631 }
607- else
608- #else
609- LOG_W ("Cannot find a RTC device to save time!" );
610- #endif
611- return -1 ;
612632
613- return 0 ;
633+ return -1 ;
634+ #endif /* RT_USING_RTC */
614635}
615636RTM_EXPORT (clock_settime );
616637
617- int clock_time_to_tick (const struct timespec * time )
638+ int rt_timespec_to_tick (const struct timespec * time )
618639{
619640 int tick ;
620641 int nsecond , second ;
@@ -645,19 +666,17 @@ int clock_time_to_tick(const struct timespec *time)
645666
646667 return tick ;
647668}
648- RTM_EXPORT (clock_time_to_tick );
669+ RTM_EXPORT (rt_timespec_to_tick );
649670
650671#endif /* RT_USING_POSIX */
651672
652673
653- /* timezone APIs (Not standard LIBC APIs) */
674+ /* timezone */
654675#ifndef RT_LIBC_DEFAULT_TIMEZONE
655676#define RT_LIBC_DEFAULT_TIMEZONE 8
656677#endif
657678
658- #include <rthw.h>
659-
660- volatile static rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE ;
679+ static volatile rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE ;
661680
662681void rt_tz_set (rt_int8_t tz )
663682{
0 commit comments