2003-10-24 14:21:10 +04:00
|
|
|
/*
|
|
|
|
** Copyright 2003, Jeff Ward, jeff@r2d2.stcloudstate.edu. All rights reserved.
|
|
|
|
** Distributed under the terms of the OpenBeOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <OS.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <arch/real_time_clock.h>
|
|
|
|
#include <real_time_clock.h>
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
|
|
|
|
static bigtime_t sBootTime;
|
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
|
|
|
|
void
|
2003-10-24 16:01:25 +04:00
|
|
|
rtc_set_system_time(uint32 current_time)
|
|
|
|
{
|
2003-10-24 14:21:10 +04:00
|
|
|
uint64 useconds;
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
useconds = (uint64)current_time * 1000000;
|
2003-10-24 16:01:25 +04:00
|
|
|
sBootTime = useconds - system_time();
|
2003-10-24 14:21:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Write the system time to CMOS. */
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
static void
|
|
|
|
rtc_system_to_hw(void)
|
|
|
|
{
|
2003-10-24 14:21:10 +04:00
|
|
|
uint32 seconds;
|
2003-10-24 16:01:25 +04:00
|
|
|
|
|
|
|
seconds = (sBootTime + system_time()) / 1000000;
|
2003-10-24 14:21:10 +04:00
|
|
|
arch_rtc_set_hw_time(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Read the CMOS clock and update the system time accordingly. */
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
static void
|
|
|
|
rtc_hw_to_system(void)
|
|
|
|
{
|
2003-10-24 14:21:10 +04:00
|
|
|
uint32 current_time;
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
current_time = arch_rtc_get_hw_time();
|
|
|
|
rtc_set_system_time(current_time);
|
|
|
|
}
|
|
|
|
|
2003-10-24 16:01:25 +04:00
|
|
|
|
2003-10-24 14:21:10 +04:00
|
|
|
bigtime_t
|
2003-10-24 16:01:25 +04:00
|
|
|
rtc_boot_time(void)
|
|
|
|
{
|
|
|
|
return sBootTime;
|
2003-10-24 14:21:10 +04:00
|
|
|
}
|
2003-10-24 16:09:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
rtc_print(void)
|
|
|
|
{
|
|
|
|
uint32 currentTime;
|
|
|
|
|
|
|
|
currentTime = (sBootTime + system_time()) / 1000000;
|
|
|
|
dprintf("system_time: %u\n", (unsigned)system_time());
|
|
|
|
dprintf("boot_time: %u\n", (unsigned)sBootTime);
|
|
|
|
dprintf("current_time: %u\n", (unsigned)currentTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
rtc_debug(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc < 2) {
|
|
|
|
// If no arguments were given, output all usefull data.
|
|
|
|
rtc_print();
|
|
|
|
} else {
|
|
|
|
// If there was an argument, reset the system and hw time.
|
|
|
|
rtc_set_system_time(strtoul(argv[1], NULL, 10));
|
|
|
|
rtc_system_to_hw();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
rtc_init(kernel_args *ka)
|
|
|
|
{
|
|
|
|
//dprintf("rtc_init: entry\n");
|
|
|
|
add_debugger_command("rtc", &rtc_debug, "Set and test the real-time clock");
|
|
|
|
|
|
|
|
rtc_hw_to_system();
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|