mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-01-18 12:53:31 +08:00
[mini2440]rtc clean up
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@518 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
3693e2cd90
commit
dd7cbec8d9
@ -9,32 +9,38 @@
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2009-04-26 yi.qiu first version
|
||||
* 2010-03-18 Gary Lee add functions such as GregorianDay
|
||||
* and rt_rtc_time_to_tm
|
||||
* 2009-04-26 yi.qiu first version
|
||||
* 2010-03-18 Gary Lee add functions such as GregorianDay
|
||||
* and rt_rtc_time_to_tm
|
||||
* 2009-03-20 yi.qiu clean up
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <time.h>
|
||||
#include <s3c24x0.h>
|
||||
#include "rtc.h"
|
||||
|
||||
#define RTC_DEBUG
|
||||
|
||||
#define RTC_ENABLE (RTCCON |= 0x01); /*RTC read and write enable */
|
||||
#define RTC_DISABLE (RTCCON &= ~0x01); /* RTC read and write disable */
|
||||
#define BCD2BIN(n) (((((n) >> 4) & 0x0F) * 10) + ((n) & 0x0F))
|
||||
#define BIN2BCD(n) ((((n) / 10) << 4) | ((n) % 10))
|
||||
|
||||
/**
|
||||
* This function get rtc time
|
||||
*/
|
||||
void rt_hw_rtc_get(struct rtc_time *ti)
|
||||
void rt_hw_rtc_get(struct tm *ti)
|
||||
{
|
||||
rt_uint8_t sec, min, hour, mday, wday, mon, year;
|
||||
|
||||
/* enable access to RTC registers */
|
||||
RTC_ENABLE();
|
||||
RTC_ENABLE
|
||||
|
||||
/* read RTC registers */
|
||||
do
|
||||
{
|
||||
sec = BCDSEC;
|
||||
min = BCDMIN;
|
||||
sec = BCDSEC;
|
||||
min = BCDMIN;
|
||||
hour = BCDHOUR;
|
||||
mday = BCDDATE;
|
||||
wday = BCDDAY;
|
||||
@ -43,7 +49,7 @@ void rt_hw_rtc_get(struct rtc_time *ti)
|
||||
} while (sec != BCDSEC);
|
||||
|
||||
/* disable access to RTC registers */
|
||||
RTC_DISABLE();
|
||||
RTC_DISABLE
|
||||
|
||||
ti->tm_sec = BCD2BIN(sec & 0x7F);
|
||||
ti->tm_min = BCD2BIN(min & 0x7F);
|
||||
@ -59,20 +65,20 @@ void rt_hw_rtc_get(struct rtc_time *ti)
|
||||
/**
|
||||
* This function set rtc time
|
||||
*/
|
||||
void rt_hw_rtc_set(struct rtc_time *ti)
|
||||
void rt_hw_rtc_set(struct tm *ti)
|
||||
{
|
||||
rt_uint8_t sec, min, hour, mday, wday, mon, year;
|
||||
|
||||
year = BIN2BCD(ti->tm_year);
|
||||
year = BIN2BCD(ti->tm_year);
|
||||
mon = BIN2BCD(ti->tm_mon);
|
||||
wday = BIN2BCD(ti->tm_wday);
|
||||
mday = BIN2BCD(ti->tm_mday);
|
||||
hour = BIN2BCD(ti->tm_hour);
|
||||
min = BIN2BCD(ti->tm_min);
|
||||
sec = BIN2BCD(ti->tm_sec);
|
||||
min = BIN2BCD(ti->tm_min);
|
||||
sec = BIN2BCD(ti->tm_sec);
|
||||
|
||||
/* enable access to RTC registers */
|
||||
RTC_ENABLE();
|
||||
RTC_ENABLE
|
||||
|
||||
/* write RTC registers */
|
||||
BCDSEC = sec;
|
||||
@ -80,11 +86,11 @@ void rt_hw_rtc_set(struct rtc_time *ti)
|
||||
BCDHOUR = hour;
|
||||
BCDDATE = mday;
|
||||
BCDDAY = wday;
|
||||
BCDMON = mon;
|
||||
BCDMON = mon;
|
||||
BCDYEAR = year;
|
||||
|
||||
/* disable access to RTC registers */
|
||||
RTC_DISABLE();
|
||||
RTC_DISABLE
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,95 +102,16 @@ void rt_hw_rtc_reset (void)
|
||||
RTCCON &= ~(0x08|0x01);
|
||||
}
|
||||
|
||||
/*
|
||||
* This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
|
||||
*/
|
||||
void GregorianDay(struct rtc_time * tm)
|
||||
{
|
||||
int leapsToDate;
|
||||
int lastYear;
|
||||
int day;
|
||||
int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
|
||||
|
||||
lastYear=tm->tm_year-1;
|
||||
|
||||
/*
|
||||
* Number of leap corrections to apply up to end of last year
|
||||
*/
|
||||
leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
|
||||
|
||||
/*
|
||||
* This year is a leap year if it is divisible by 4 except when it is
|
||||
* divisible by 100 unless it is divisible by 400
|
||||
*
|
||||
* e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
|
||||
*/
|
||||
if((tm->tm_year%4==0) &&
|
||||
((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
|
||||
(tm->tm_mon>2)) {
|
||||
/*
|
||||
* We are past Feb. 29 in a leap year
|
||||
*/
|
||||
day=1;
|
||||
} else {
|
||||
day=0;
|
||||
}
|
||||
|
||||
day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday;
|
||||
|
||||
tm->tm_wday=day%7;
|
||||
}
|
||||
|
||||
void rt_rtc_time_to_tm(rt_uint32_t tim, struct rtc_time *tm)
|
||||
{
|
||||
register int i;
|
||||
register long hms, day;
|
||||
|
||||
day = tim / SECDAY;
|
||||
hms = tim % SECDAY;
|
||||
|
||||
/* Hours, minutes, seconds are easy */
|
||||
tm->tm_hour = hms / 3600;
|
||||
tm->tm_min = (hms % 3600) / 60;
|
||||
tm->tm_sec = (hms % 3600) % 60;
|
||||
|
||||
/* Number of years in days */
|
||||
for (i = STARTOFTIME; day >= days_in_year(i); i++) {
|
||||
day -= days_in_year(i);
|
||||
}
|
||||
tm->tm_year = i;
|
||||
|
||||
/* Number of months in days left */
|
||||
if (LEAP_YEAR(tm->tm_year)) {
|
||||
days_in_month(FEBRUARY) = 29;
|
||||
}
|
||||
for (i = 1; day >= days_in_month(i); i++) {
|
||||
day -= days_in_month(i);
|
||||
}
|
||||
days_in_month(FEBRUARY) = 28;
|
||||
tm->tm_mon = i;
|
||||
|
||||
/* Days are what is left over (+1) from all that. */
|
||||
tm->tm_mday = day + 1;
|
||||
|
||||
/*
|
||||
* Determine the day of week
|
||||
*/
|
||||
GregorianDay(tm);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct rt_device rtc;
|
||||
static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
|
||||
{
|
||||
RTC_ENABLE();
|
||||
RTC_ENABLE
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_rtc_close(rt_device_t dev)
|
||||
{
|
||||
RTC_DISABLE();
|
||||
RTC_DISABLE
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
@ -195,10 +122,10 @@ static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_siz
|
||||
|
||||
static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||
{
|
||||
struct rtc_time* time;
|
||||
struct tm* time;
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
time = (struct rtc_time*)args;
|
||||
time = (struct tm*)args;
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_DEVICE_CTRL_RTC_GET_TIME:
|
||||
@ -258,7 +185,7 @@ time_t time(time_t* t)
|
||||
#include <finsh.h>
|
||||
void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
|
||||
{
|
||||
struct rtc_time ti;
|
||||
struct tm ti;
|
||||
rt_device_t device;
|
||||
|
||||
device = rt_device_find("rtc");
|
||||
@ -275,7 +202,7 @@ 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)
|
||||
{
|
||||
struct rtc_time ti;
|
||||
struct tm ti;
|
||||
rt_device_t device;
|
||||
|
||||
device = rt_device_find("rtc");
|
||||
@ -283,7 +210,7 @@ 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);
|
||||
ti.tm_hour = hour;
|
||||
ti.tm_min = minute;
|
||||
ti.tm_min = minute;
|
||||
ti.tm_sec = second;
|
||||
rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &ti);
|
||||
}
|
||||
@ -299,4 +226,3 @@ void list_date(void)
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(list_date, list date)
|
||||
#endif
|
||||
|
||||
|
@ -1,51 +1,21 @@
|
||||
#ifndef __RT_HW_SERIAL_H__
|
||||
#define __RT_HW_SERIAL_H__
|
||||
/*
|
||||
* File : rtc.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2009, 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://www.rt-thread.org/license/LICENSE
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-03-20 yi.qiu the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#ifndef __RTC_H__
|
||||
#define __RTC_H__
|
||||
|
||||
#include "s3c24x0.h"
|
||||
|
||||
#define RTC_DEBUG
|
||||
|
||||
#define RTC_ENABLE() (RTCCON |= 0x01) //RTC read and write enable
|
||||
#define RTC_DISABLE() (RTCCON &= ~0x01) //RTC read and write disable
|
||||
|
||||
#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))
|
||||
|
||||
#define FEBRUARY 2
|
||||
#define STARTOFTIME 1970
|
||||
#define SECDAY 86400L
|
||||
#define SECYR (SECDAY * 365)
|
||||
#define days_in_year(a) (LEAP_YEAR(a) ? 366 : 365)
|
||||
#define days_in_month(a) (month_days[(a) - 1])
|
||||
|
||||
static unsigned char month_days[] =
|
||||
{
|
||||
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
struct rtc_time
|
||||
{
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
};
|
||||
|
||||
void rt_hw_rtc_get (struct rtc_time *tmp);
|
||||
void rt_hw_rtc_set (struct rtc_time *tmp);
|
||||
void rt_hw_rtc_reset (void);
|
||||
void GregorianDay (struct rtc_time *);
|
||||
void rt_rtc_time_to_tm(rt_uint32_t tim, struct rtc_time *tm);
|
||||
void rt_hw_rtc_init(void);
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user