Fixed head file of rtc.c

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@400 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
gary.li.wenchao.4 2010-02-10 08:25:45 +00:00
parent 881d6a82d4
commit 43907c8b79
2 changed files with 28 additions and 27 deletions

View File

@ -15,25 +15,24 @@
#include <rtthread.h> #include <rtthread.h>
#include <time.h> #include <time.h>
#include <s3c24x0.h> #include <s3c24x0.h>
#include "rtc.h"
#define BCD2BIN(n) (((((n) >> 4) & 0x0F) * 10) + ((n) & 0x0F))
#define BIN2BCD(n) ((((n) / 10) << 4) | ((n) % 10))
/** /**
* This function get rtc time * This function get rtc time
*/ */
void rt_hw_rtc_get(struct tm *ti) void rt_hw_rtc_get(struct rtc_time *ti)
{ {
rt_uint8_t sec, min, hour, mday, wday, mon, year; rt_uint8_t sec, min, hour, mday, wday, mon, year;
/* enable access to RTC registers */ /* enable access to RTC registers */
RTCCON |= 0x01; RTCCON |= RTC_ENABLE;
/* read RTC registers */ /* read RTC registers */
do do
{ {
sec = BCDSEC; sec = BCDSEC;
min = BCDMIN; min = BCDMIN;
hour = BCDHOUR; hour = BCDHOUR;
mday = BCDDATE; mday = BCDDATE;
wday = BCDDAY; wday = BCDDAY;
@ -58,7 +57,7 @@ void rt_hw_rtc_get(struct tm *ti)
/** /**
* This function set rtc time * This function set rtc time
*/ */
void rt_hw_rtc_set(struct tm *ti) void rt_hw_rtc_set(struct rtc_time *ti)
{ {
rt_uint8_t sec, min, hour, mday, wday, mon, year; rt_uint8_t sec, min, hour, mday, wday, mon, year;
@ -67,8 +66,8 @@ void rt_hw_rtc_set(struct tm *ti)
wday = BIN2BCD(ti->tm_wday); wday = BIN2BCD(ti->tm_wday);
mday = BIN2BCD(ti->tm_mday); mday = BIN2BCD(ti->tm_mday);
hour = BIN2BCD(ti->tm_hour); hour = BIN2BCD(ti->tm_hour);
min = BIN2BCD(ti->tm_min); min = BIN2BCD(ti->tm_min);
sec = BIN2BCD(ti->tm_sec); sec = BIN2BCD(ti->tm_sec);
/* enable access to RTC registers */ /* enable access to RTC registers */
RTCCON |= 0x01; RTCCON |= 0x01;
@ -79,7 +78,7 @@ void rt_hw_rtc_set(struct tm *ti)
BCDHOUR = hour; BCDHOUR = hour;
BCDDATE = mday; BCDDATE = mday;
BCDDAY = wday; BCDDAY = wday;
BCDMON = mon; BCDMON = mon;
BCDYEAR = year; BCDYEAR = year;
/* disable access to RTC registers */ /* disable access to RTC registers */
@ -103,25 +102,25 @@ static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{ {
return 0; return RT_EOK;
} }
static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args) static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{ {
struct tm* ti; struct rtc_time* time;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
ti = (struct tm*)args; time = (struct rtc_time*)args;
switch (cmd) switch (cmd)
{ {
case RT_DEVICE_CTRL_RTC_GET_TIME: case RT_DEVICE_CTRL_RTC_GET_TIME:
/* read device */ /* read device */
rt_hw_rtc_get(ti); rt_hw_rtc_get(time);
break; break;
case RT_DEVICE_CTRL_RTC_SET_TIME: case RT_DEVICE_CTRL_RTC_SET_TIME:
/* write device */ /* write device */
rt_hw_rtc_set(ti); rt_hw_rtc_set(time);
break; break;
} }
@ -145,7 +144,6 @@ void rt_hw_rtc_init(void)
rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR); rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
return;
} }
time_t time(time_t* t) time_t time(time_t* t)
@ -185,7 +183,7 @@ void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &ti); rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &ti);
} }
} }
FINSH_FUNCTION_EXPORT(set_date, set date) FINSH_FUNCTION_EXPORT(set_date, set date(year, month, day))
void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
{ {
@ -197,20 +195,20 @@ void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
{ {
rt_rtc_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &ti); rt_rtc_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &ti);
ti.tm_hour = hour; ti.tm_hour = hour;
ti.tm_min = minute; ti.tm_min = minute;
ti.tm_sec = second; ti.tm_sec = second;
rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &ti); rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &ti);
} }
} }
FINSH_FUNCTION_EXPORT(set_time, set second) FINSH_FUNCTION_EXPORT(set_time, set time(hour, minute, second))
void list_date() void list_date(void)
{ {
time_t now; time_t now;
time(&now); time(&now);
rt_kprintf("%s\n", ctime(&now)); rt_kprintf("%s\n", ctime(&now));
} }
FINSH_FUNCTION_EXPORT(list_date, set date) FINSH_FUNCTION_EXPORT(list_date, list date)
#endif #endif

View File

@ -8,17 +8,20 @@
#define RTC_DEBUG #define RTC_DEBUG
#define RTC_ENABLE 0x01 #define RTC_ENABLE 0x01
#define RTC_DISABLE 0x02 #define RTC_DISABLE 0x02
#define BCD2BIN(n) (((((n) >> 4) & 0x0F) * 10) + ((n) & 0x0F))
#define BIN2BCD(n) ((((n) / 10) << 4) | ((n) % 10))
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
static const unsigned char days_in_month[] = static const unsigned char days_in_month[] =
{ {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}; };
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
struct rtc_time struct rtc_time
{ {
int tm_sec; int tm_sec;
@ -35,6 +38,6 @@ struct rtc_time
void rt_hw_rtc_get (struct rtc_time *tmp); void rt_hw_rtc_get (struct rtc_time *tmp);
void rt_hw_rtc_set (struct rtc_time *tmp); void rt_hw_rtc_set (struct rtc_time *tmp);
void rt_hw_rtc_reset (void); void rt_hw_rtc_reset (void);
void rt_rtc_time_to_tm(rt_uint32_t time, struct rtc_time *tm); void rt_rtc_time_to_tm(rt_uint32_t time, struct tm *tm);
#endif #endif