2010-10-22 18:20:38 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
2010-11-22 07:55:44 +08:00
|
|
|
#include <sys/time.h>
|
2010-11-22 17:51:40 +08:00
|
|
|
#include "libc.h"
|
2010-11-22 07:55:44 +08:00
|
|
|
|
|
|
|
struct timeval _timevalue;
|
|
|
|
static void libc_system_time_init()
|
|
|
|
{
|
|
|
|
time_t time;
|
|
|
|
rt_tick_t tick;
|
2010-11-22 17:51:40 +08:00
|
|
|
rt_device_t device;
|
2010-11-22 07:55:44 +08:00
|
|
|
|
2010-11-22 17:51:40 +08:00
|
|
|
time = 0;
|
2010-11-22 07:55:44 +08:00
|
|
|
device = rt_device_find("rtc");
|
|
|
|
if (device != RT_NULL)
|
|
|
|
{
|
2010-11-22 17:51:40 +08:00
|
|
|
/* get realtime seconds */
|
2010-11-22 07:55:44 +08:00
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
|
|
|
|
}
|
|
|
|
|
2010-11-22 17:51:40 +08:00
|
|
|
/* get tick */
|
|
|
|
tick = rt_tick_get();
|
|
|
|
|
|
|
|
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
|
|
|
|
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int libc_get_time(struct timespec *time)
|
|
|
|
{
|
|
|
|
rt_tick_t tick;
|
|
|
|
RT_ASSERT(time != RT_NULL);
|
|
|
|
|
|
|
|
/* get tick */
|
|
|
|
tick = rt_tick_get();
|
|
|
|
|
|
|
|
time->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
|
|
|
|
time->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * NANOSECOND_PER_TICK) * 1000;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int libc_set_time(const struct timespec *time)
|
|
|
|
{
|
|
|
|
int second;
|
|
|
|
rt_tick_t tick;
|
|
|
|
rt_device_t device;
|
|
|
|
|
|
|
|
second = time->tv_sec;
|
|
|
|
device = rt_device_find("rtc");
|
|
|
|
if (device != RT_NULL)
|
|
|
|
{
|
|
|
|
/* get realtime seconds */
|
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
|
|
|
|
}
|
|
|
|
else return -1;
|
|
|
|
|
|
|
|
/* get tick */
|
2010-11-22 07:55:44 +08:00
|
|
|
tick = rt_tick_get();
|
|
|
|
|
2010-11-22 17:51:40 +08:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int libc_time_to_tick(const struct timespec *time)
|
|
|
|
{
|
|
|
|
int tick;
|
|
|
|
int microsecond, second;
|
|
|
|
|
|
|
|
RT_ASSERT(time != RT_NULL);
|
|
|
|
|
|
|
|
tick = RT_WAITING_FOREVER;
|
|
|
|
if (time != NULL)
|
|
|
|
{
|
|
|
|
if ((time->tv_nsec/1000 - _timevalue.tv_usec) < 0)
|
|
|
|
{
|
|
|
|
microsecond = (1000000UL + time->tv_nsec/1000) - _timevalue.tv_usec;
|
|
|
|
second = time->tv_sec - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
microsecond = time->tv_nsec/1000 - _timevalue.tv_usec;
|
|
|
|
second = time->tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
tick = second * RT_TICK_PER_SECOND + microsecond * RT_TICK_PER_SECOND / MICROSECOND_PER_SECOND;
|
|
|
|
if (tick < 0) tick = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tick;
|
2010-11-22 07:55:44 +08:00
|
|
|
}
|
2010-10-22 18:20:38 +08:00
|
|
|
|
|
|
|
void libc_system_init(const char* tty_name)
|
|
|
|
{
|
|
|
|
int fd;
|
2010-11-24 19:04:08 +08:00
|
|
|
extern int pthread_system_init(void);
|
2010-10-22 18:20:38 +08:00
|
|
|
|
2010-11-29 08:04:55 +08:00
|
|
|
#ifndef RT_USING_DFS_DEVFS
|
|
|
|
#error Please enable devfs by defining RT_USING_DFS_DEVFS in rtconfig.h
|
|
|
|
#endif
|
|
|
|
|
2010-10-22 18:20:38 +08:00
|
|
|
/* init console device */
|
|
|
|
rt_console_init(tty_name);
|
|
|
|
|
|
|
|
/* open console as stdin/stdout/stderr */
|
|
|
|
fd = open("/dev/console", O_RDONLY, 0); /* for stdin */
|
|
|
|
fd = open("/dev/console", O_WRONLY, 0); /* for stdout */
|
|
|
|
fd = open("/dev/console", O_WRONLY, 0); /* for stderr */
|
2010-10-29 13:57:19 +08:00
|
|
|
|
|
|
|
/* set PATH and HOME */
|
|
|
|
putenv("PATH=/");
|
|
|
|
putenv("HOME=/");
|
2010-11-22 07:55:44 +08:00
|
|
|
|
2010-11-22 17:51:40 +08:00
|
|
|
/* initialize system time */
|
|
|
|
libc_system_time_init();
|
2010-11-24 19:04:08 +08:00
|
|
|
#ifdef RT_USING_PTHREADS
|
|
|
|
pthread_system_init();
|
|
|
|
#endif
|
2010-11-22 07:55:44 +08:00
|
|
|
}
|