2009-12-11 17:42:01 +08:00
|
|
|
/*
|
|
|
|
* File : rtc.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2006, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
|
* http://openlab.rt-thread.com/license/LICENSE
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2010-03-20 01:44:24 +08:00
|
|
|
* 2009-04-26 yi.qiu first version
|
|
|
|
* 2010-03-18 Gary Lee add functions such as GregorianDay
|
2010-11-22 22:50:45 +08:00
|
|
|
* and rtc_time_to_tm
|
2010-03-20 01:44:24 +08:00
|
|
|
* 2009-03-20 yi.qiu clean up
|
2009-12-11 17:42:01 +08:00
|
|
|
*/
|
|
|
|
|
2010-01-10 23:15:27 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <s3c24x0.h>
|
2009-12-11 17:42:01 +08:00
|
|
|
|
2010-11-30 15:02:09 +08:00
|
|
|
// #define RTC_DEBUG
|
2010-03-20 01:44:24 +08:00
|
|
|
|
2010-03-24 21:37:20 +08:00
|
|
|
#define RTC_ENABLE RTCCON |= 0x01; /*RTC read and write enable */
|
|
|
|
#define RTC_DISABLE RTCCON &= ~0x01; /* RTC read and write disable */
|
2010-03-20 01:44:24 +08:00
|
|
|
#define BCD2BIN(n) (((((n) >> 4) & 0x0F) * 10) + ((n) & 0x0F))
|
|
|
|
#define BIN2BCD(n) ((((n) / 10) << 4) | ((n) % 10))
|
2009-12-11 17:42:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function get rtc time
|
|
|
|
*/
|
2010-03-20 01:44:24 +08:00
|
|
|
void rt_hw_rtc_get(struct tm *ti)
|
2009-12-11 17:42:01 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t sec, min, hour, mday, wday, mon, year;
|
|
|
|
|
|
|
|
/* enable access to RTC registers */
|
2010-03-24 21:37:20 +08:00
|
|
|
RTCCON |= RTC_ENABLE;
|
2009-12-11 17:42:01 +08:00
|
|
|
|
|
|
|
/* read RTC registers */
|
2009-12-18 13:25:41 +08:00
|
|
|
do
|
2009-12-11 17:42:01 +08:00
|
|
|
{
|
2010-11-22 22:50:45 +08:00
|
|
|
sec = BCDSEC;
|
|
|
|
min = BCDMIN;
|
2010-01-10 23:15:27 +08:00
|
|
|
hour = BCDHOUR;
|
|
|
|
mday = BCDDATE;
|
|
|
|
wday = BCDDAY;
|
|
|
|
mon = BCDMON;
|
|
|
|
year = BCDYEAR;
|
2010-11-22 22:50:45 +08:00
|
|
|
} while (sec != BCDSEC);
|
2010-03-24 21:37:20 +08:00
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
#ifdef RTC_DEBUG
|
2010-03-24 21:37:20 +08:00
|
|
|
rt_kprintf("sec:%x min:%x hour:%x mday:%x wday:%x mon:%x year:%x\n",
|
|
|
|
sec, min, hour, mday, wday, mon, year);
|
2010-11-22 22:50:45 +08:00
|
|
|
#endif
|
2009-12-11 17:42:01 +08:00
|
|
|
|
|
|
|
/* disable access to RTC registers */
|
2010-03-20 01:44:24 +08:00
|
|
|
RTC_DISABLE
|
2010-01-10 23:15:27 +08:00
|
|
|
|
|
|
|
ti->tm_sec = BCD2BIN(sec & 0x7F);
|
|
|
|
ti->tm_min = BCD2BIN(min & 0x7F);
|
|
|
|
ti->tm_hour = BCD2BIN(hour & 0x3F);
|
|
|
|
ti->tm_mday = BCD2BIN(mday & 0x3F);
|
|
|
|
ti->tm_mon = BCD2BIN(mon & 0x1F);
|
|
|
|
ti->tm_year = BCD2BIN(year);
|
|
|
|
ti->tm_wday = BCD2BIN(wday & 0x07);
|
|
|
|
ti->tm_yday = 0;
|
|
|
|
ti->tm_isdst = 0;
|
2009-12-11 17:42:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function set rtc time
|
|
|
|
*/
|
2010-03-20 01:44:24 +08:00
|
|
|
void rt_hw_rtc_set(struct tm *ti)
|
2009-12-11 17:42:01 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t sec, min, hour, mday, wday, mon, year;
|
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
year = BIN2BCD(ti->tm_year);
|
2010-01-10 23:15:27 +08:00
|
|
|
mon = BIN2BCD(ti->tm_mon);
|
|
|
|
wday = BIN2BCD(ti->tm_wday);
|
|
|
|
mday = BIN2BCD(ti->tm_mday);
|
|
|
|
hour = BIN2BCD(ti->tm_hour);
|
2010-11-22 22:50:45 +08:00
|
|
|
min = BIN2BCD(ti->tm_min);
|
|
|
|
sec = BIN2BCD(ti->tm_sec);
|
2009-12-11 17:42:01 +08:00
|
|
|
|
|
|
|
/* enable access to RTC registers */
|
2010-03-20 01:44:24 +08:00
|
|
|
RTC_ENABLE
|
2009-12-11 17:42:01 +08:00
|
|
|
|
2010-03-24 21:37:20 +08:00
|
|
|
do{
|
|
|
|
/* write RTC registers */
|
|
|
|
BCDSEC = sec;
|
|
|
|
BCDMIN = min;
|
|
|
|
BCDHOUR = hour;
|
|
|
|
BCDDATE = mday;
|
|
|
|
BCDDAY = wday;
|
|
|
|
BCDMON = mon;
|
|
|
|
BCDYEAR = year;
|
|
|
|
}while (sec != BCDSEC);
|
|
|
|
|
2009-12-11 17:42:01 +08:00
|
|
|
/* disable access to RTC registers */
|
2010-03-20 01:44:24 +08:00
|
|
|
RTC_DISABLE
|
2009-12-11 17:42:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function reset rtc
|
|
|
|
*/
|
|
|
|
void rt_hw_rtc_reset (void)
|
|
|
|
{
|
|
|
|
RTCCON = (RTCCON & ~0x06) | 0x08;
|
|
|
|
RTCCON &= ~(0x08|0x01);
|
|
|
|
}
|
|
|
|
|
2010-01-10 23:15:27 +08:00
|
|
|
static struct rt_device rtc;
|
2010-11-22 22:50:45 +08:00
|
|
|
static rt_err_t rtc_open(rt_device_t dev, rt_uint16_t oflag)
|
2010-01-10 23:15:27 +08:00
|
|
|
{
|
2010-03-20 01:44:24 +08:00
|
|
|
RTC_ENABLE
|
2010-02-11 10:53:43 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
static rt_err_t rtc_close(rt_device_t dev)
|
2010-02-11 10:53:43 +08:00
|
|
|
{
|
2010-03-20 01:44:24 +08:00
|
|
|
RTC_DISABLE
|
2010-01-10 23:15:27 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
static rt_size_t rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
|
2010-01-10 23:15:27 +08:00
|
|
|
{
|
2010-02-10 16:25:45 +08:00
|
|
|
return RT_EOK;
|
2010-01-10 23:15:27 +08:00
|
|
|
}
|
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
static rt_err_t rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
2010-01-10 23:15:27 +08:00
|
|
|
{
|
2010-11-22 22:50:45 +08:00
|
|
|
struct tm tm, *tm_ptr;
|
|
|
|
time_t *time;
|
2010-01-10 23:15:27 +08:00
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
time = (time_t *)args;
|
2010-01-10 23:15:27 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_RTC_GET_TIME:
|
|
|
|
/* read device */
|
2010-11-22 22:50:45 +08:00
|
|
|
rt_hw_rtc_get(&tm);
|
|
|
|
*((rt_time_t *)args) = mktime(&tm);
|
2010-01-10 23:15:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_DEVICE_CTRL_RTC_SET_TIME:
|
2010-11-22 22:50:45 +08:00
|
|
|
tm_ptr = localtime(time);
|
2010-01-10 23:15:27 +08:00
|
|
|
/* write device */
|
2010-11-22 22:50:45 +08:00
|
|
|
rt_hw_rtc_set(tm_ptr);
|
2010-01-10 23:15:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_hw_rtc_init(void)
|
|
|
|
{
|
|
|
|
rtc.type = RT_Device_Class_RTC;
|
|
|
|
|
|
|
|
/* register rtc device */
|
|
|
|
rtc.init = RT_NULL;
|
2010-11-22 22:50:45 +08:00
|
|
|
rtc.open = rtc_open;
|
|
|
|
rtc.close = rtc_close;
|
|
|
|
rtc.read = rtc_read;
|
2010-01-10 23:15:27 +08:00
|
|
|
rtc.write = RT_NULL;
|
2010-11-22 22:50:45 +08:00
|
|
|
rtc.control = rtc_control;
|
2010-01-10 23:15:27 +08:00
|
|
|
|
|
|
|
/* no private */
|
2010-11-29 15:47:47 +08:00
|
|
|
rtc.user_data = RT_NULL;
|
2010-01-10 23:15:27 +08:00
|
|
|
|
|
|
|
rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
|
|
|
|
}
|
|
|
|
|
2010-02-01 23:16:05 +08:00
|
|
|
#include <finsh.h>
|
2010-11-22 22:50:45 +08:00
|
|
|
void list_date()
|
2010-01-10 23:15:27 +08:00
|
|
|
{
|
2010-11-22 22:50:45 +08:00
|
|
|
time_t time;
|
2010-01-10 23:15:27 +08:00
|
|
|
rt_device_t device;
|
|
|
|
|
|
|
|
device = rt_device_find("rtc");
|
|
|
|
if (device != RT_NULL)
|
|
|
|
{
|
2010-11-22 22:50:45 +08:00
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
|
2010-03-24 21:37:20 +08:00
|
|
|
|
2010-11-22 22:50:45 +08:00
|
|
|
rt_kprintf("%d, %s\n", time, ctime(&time));
|
2010-03-24 21:37:20 +08:00
|
|
|
}
|
2010-01-10 23:15:27 +08:00
|
|
|
}
|
2010-11-22 22:50:45 +08:00
|
|
|
FINSH_FUNCTION_EXPORT(list_date, list date);
|