mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-21 02:06:14 +08:00
[libc] move clock_time to time.
This commit is contained in:
@@ -1,155 +0,0 @@
|
||||
/*
|
||||
* File : clock_time.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-12-08 Bernard fix the issue of _timevalue.tv_usec initialization,
|
||||
* which found by Rob <rdent@iinet.net.au>
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <pthread.h>
|
||||
|
||||
struct timeval _timevalue;
|
||||
void clock_time_system_init()
|
||||
{
|
||||
time_t time;
|
||||
rt_tick_t tick;
|
||||
rt_device_t device;
|
||||
|
||||
time = 0;
|
||||
device = rt_device_find("rtc");
|
||||
if (device != RT_NULL)
|
||||
{
|
||||
/* get realtime seconds */
|
||||
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
|
||||
}
|
||||
|
||||
/* get tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
|
||||
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
|
||||
}
|
||||
|
||||
int clock_time_to_tick(const struct timespec *time)
|
||||
{
|
||||
int tick;
|
||||
int nsecond, second;
|
||||
struct timespec tp;
|
||||
|
||||
RT_ASSERT(time != RT_NULL);
|
||||
|
||||
tick = RT_WAITING_FOREVER;
|
||||
if (time != NULL)
|
||||
{
|
||||
/* get current tp */
|
||||
clock_gettime(CLOCK_REALTIME, &tp);
|
||||
|
||||
if ((time->tv_nsec - tp.tv_nsec) < 0)
|
||||
{
|
||||
nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
|
||||
second = time->tv_sec - tp.tv_sec - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
nsecond = time->tv_nsec - tp.tv_nsec;
|
||||
second = time->tv_sec - tp.tv_sec;
|
||||
}
|
||||
|
||||
tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
|
||||
if (tick < 0) tick = 0;
|
||||
}
|
||||
|
||||
return tick;
|
||||
}
|
||||
RTM_EXPORT(clock_time_to_tick);
|
||||
|
||||
int clock_getres(clockid_t clockid, struct timespec *res)
|
||||
{
|
||||
if ((clockid != CLOCK_REALTIME) || (res == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
res->tv_sec = 0;
|
||||
res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(clock_getres);
|
||||
|
||||
int clock_gettime(clockid_t clockid, struct timespec *tp)
|
||||
{
|
||||
rt_tick_t tick;
|
||||
|
||||
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* get tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
|
||||
tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(clock_gettime);
|
||||
|
||||
int clock_settime(clockid_t clockid, const struct timespec *tp)
|
||||
{
|
||||
int second;
|
||||
rt_tick_t tick;
|
||||
rt_device_t device;
|
||||
|
||||
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
|
||||
{
|
||||
rt_set_errno(EINVAL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* get second */
|
||||
second = tp->tv_sec;
|
||||
/* get tick */
|
||||
tick = rt_tick_get();
|
||||
|
||||
/* update timevalue */
|
||||
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
|
||||
_timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
|
||||
|
||||
/* update for RTC device */
|
||||
device = rt_device_find("rtc");
|
||||
if (device != RT_NULL)
|
||||
{
|
||||
/* set realtime seconds */
|
||||
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
RTM_EXPORT(clock_settime);
|
||||
@@ -27,9 +27,6 @@
|
||||
|
||||
int pthread_system_init(void)
|
||||
{
|
||||
/* initialize clock and time */
|
||||
clock_time_system_init();
|
||||
|
||||
/* initialize key area */
|
||||
pthread_key_system_init();
|
||||
/* initialize posix mqueue */
|
||||
|
||||
@@ -276,35 +276,4 @@ int pthread_barrier_init(pthread_barrier_t *barrier,
|
||||
|
||||
int pthread_barrier_wait(pthread_barrier_t *barrier);
|
||||
|
||||
/* posix clock and timer */
|
||||
#define MILLISECOND_PER_SECOND 1000UL
|
||||
#define MICROSECOND_PER_SECOND 1000000UL
|
||||
#define NANOSECOND_PER_SECOND 1000000000UL
|
||||
|
||||
#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
|
||||
#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
|
||||
#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
|
||||
|
||||
#ifndef CLOCK_REALTIME
|
||||
#define CLOCK_REALTIME 1
|
||||
#endif
|
||||
|
||||
#define CLOCK_CPUTIME_ID 2
|
||||
|
||||
#ifndef CLOCK_PROCESS_CPUTIME_ID
|
||||
#define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID
|
||||
#endif
|
||||
#ifndef CLOCK_THREAD_CPUTIME_ID
|
||||
#define CLOCK_THREAD_CPUTIME_ID CLOCK_CPUTIME_ID
|
||||
#endif
|
||||
|
||||
#ifndef CLOCK_MONOTONIC
|
||||
#define CLOCK_MONOTONIC 4
|
||||
#endif
|
||||
|
||||
int clock_getres (clockid_t clockid, struct timespec *res);
|
||||
int clock_gettime (clockid_t clockid, struct timespec *tp);
|
||||
int clock_settime (clockid_t clockid, const struct timespec *tp);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ rt_inline _pthread_data_t *_pthread_get_data(pthread_t thread)
|
||||
}
|
||||
|
||||
int clock_time_to_tick(const struct timespec *time);
|
||||
void clock_time_system_init(void);
|
||||
|
||||
void posix_mq_system_init(void);
|
||||
void posix_sem_system_init(void);
|
||||
void pthread_key_system_init(void);
|
||||
|
||||
Reference in New Issue
Block a user