2006-11-27 09:44:56 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-11-14 16:53:31 +00:00
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum,
|
|
|
|
* Przemyslaw Holubowski
|
2006-11-27 09:44:56 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-11-27 09:44:56 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "i2c.h"
|
|
|
|
#include "rtc.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "system.h"
|
2007-01-27 20:48:48 +00:00
|
|
|
#include "i2c-pp.h"
|
2017-01-26 00:32:15 +00:00
|
|
|
#include "timefuncs.h"
|
2006-11-27 09:44:56 +00:00
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
/*RTC_E8564's slave address is 0x51*/
|
|
|
|
#define RTC_ADDR 0x51
|
|
|
|
|
2007-11-14 16:13:39 +00:00
|
|
|
/* RTC registers */
|
2007-11-14 16:53:31 +00:00
|
|
|
#define RTC_CTRL1 0x00
|
|
|
|
#define RTC_CTRL2 0x01
|
|
|
|
#define RTC_ALARM_MINUTES 0x09
|
|
|
|
#define RTC_ALARM_HOURS 0x0A
|
|
|
|
#define RTC_ALARM_DAY 0x0B
|
|
|
|
#define RTC_ALARM_WDAY 0x0C
|
|
|
|
#define RTC_TIMER_CTRL 0x0E
|
|
|
|
#define RTC_TIMER 0x0F
|
2007-11-14 16:13:39 +00:00
|
|
|
|
|
|
|
/* Control 2 register flags */
|
2007-11-14 16:53:31 +00:00
|
|
|
#define RTC_TIE 0x01
|
|
|
|
#define RTC_AIE 0x02
|
|
|
|
#define RTC_TF 0x04
|
|
|
|
#define RTC_AF 0x08
|
|
|
|
#define RTC_TI_TP 0x10
|
|
|
|
|
|
|
|
/* Alarm registers flags */
|
|
|
|
#define RTC_AE 0x80
|
|
|
|
|
|
|
|
/* Timer register flags */
|
|
|
|
#define RTC_TE 0x80
|
2007-11-14 16:13:39 +00:00
|
|
|
|
2008-02-14 14:00:59 +00:00
|
|
|
bool rtc_lock_alarm_clear=true;
|
|
|
|
|
2006-11-27 09:44:56 +00:00
|
|
|
void rtc_init(void)
|
|
|
|
{
|
2007-11-14 16:13:39 +00:00
|
|
|
unsigned char tmp;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* initialize Control 1 register */
|
|
|
|
tmp = 0;
|
2009-09-26 14:58:32 +00:00
|
|
|
pp_i2c_send(RTC_ADDR, RTC_CTRL1, tmp);
|
2007-11-14 16:13:39 +00:00
|
|
|
|
|
|
|
/* read value of the Control 2 register - we'll need it to preserve alarm and timer interrupt assertion flags */
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
|
2008-02-14 14:00:59 +00:00
|
|
|
/* preserve alarm and timer interrupt flags */
|
|
|
|
tmp &= (RTC_TF | RTC_AF | RTC_TIE | RTC_AIE);
|
2009-09-26 14:58:32 +00:00
|
|
|
pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
|
2006-11-27 09:44:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_read_datetime(struct tm *tm)
|
2006-11-27 09:44:56 +00:00
|
|
|
{
|
|
|
|
int read;
|
2009-09-26 14:58:32 +00:00
|
|
|
unsigned char buf[7];
|
|
|
|
|
|
|
|
read = i2c_readbytes(RTC_ADDR, 0x02, sizeof(buf), buf);
|
|
|
|
|
|
|
|
/* convert from bcd, avoid getting extra bits */
|
|
|
|
tm->tm_sec = BCD2DEC(buf[0] & 0x7f);
|
|
|
|
tm->tm_min = BCD2DEC(buf[1] & 0x7f);
|
|
|
|
tm->tm_hour = BCD2DEC(buf[2] & 0x3f);
|
|
|
|
tm->tm_mday = BCD2DEC(buf[3] & 0x3f);
|
|
|
|
tm->tm_mon = BCD2DEC(buf[5] & 0x1f) - 1;
|
|
|
|
tm->tm_year = BCD2DEC(buf[6]) + 100;
|
2017-01-26 00:32:15 +00:00
|
|
|
tm->tm_yday = 0; /* Not implemented for now */
|
|
|
|
|
|
|
|
set_day_of_week(tm);
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2006-11-27 09:44:56 +00:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_write_datetime(const struct tm *tm)
|
2006-11-27 09:44:56 +00:00
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned char buf[7];
|
|
|
|
|
|
|
|
buf[0] = tm->tm_sec;
|
|
|
|
buf[1] = tm->tm_min;
|
|
|
|
buf[2] = tm->tm_hour;
|
|
|
|
buf[3] = tm->tm_mday;
|
|
|
|
buf[4] = tm->tm_wday;
|
|
|
|
buf[5] = tm->tm_mon + 1;
|
|
|
|
buf[6] = tm->tm_year - 100;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(buf); i++)
|
|
|
|
pp_i2c_send(RTC_ADDR, 0x02 + i, DEC2BCD(buf[i]));
|
|
|
|
|
2006-11-27 09:44:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-08 09:02:41 +00:00
|
|
|
#ifdef HAVE_RTC_ALARM
|
2007-11-14 16:53:31 +00:00
|
|
|
void rtc_set_alarm(int h, int m)
|
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
unsigned char buf[4] = {0};
|
|
|
|
int i, rv;
|
|
|
|
|
2007-11-14 16:53:31 +00:00
|
|
|
/* clear alarm interrupt */
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, buf);
|
2007-11-14 16:53:31 +00:00
|
|
|
buf[0] &= RTC_AF;
|
2009-09-26 14:58:32 +00:00
|
|
|
pp_i2c_send(RTC_ADDR, RTC_CTRL2, buf[0]);
|
|
|
|
|
2007-11-14 16:53:31 +00:00
|
|
|
/* prepare new alarm */
|
|
|
|
if( m >= 0 )
|
2009-09-26 14:58:32 +00:00
|
|
|
buf[0] = DEC2BCD(m);
|
2007-11-14 16:53:31 +00:00
|
|
|
else
|
|
|
|
/* ignore minutes comparison query */
|
|
|
|
buf[0] = RTC_AE;
|
|
|
|
|
|
|
|
if( h >= 0 )
|
2009-09-26 14:58:32 +00:00
|
|
|
buf[1] = DEC2BCD(h);
|
2007-11-14 16:53:31 +00:00
|
|
|
else
|
|
|
|
/* ignore hours comparison query */
|
|
|
|
buf[1] = RTC_AE;
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-11-14 16:53:31 +00:00
|
|
|
/* ignore day and wday */
|
|
|
|
buf[2] = RTC_AE;
|
|
|
|
buf[3] = RTC_AE;
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-11-14 16:53:31 +00:00
|
|
|
/* write new alarm */
|
2009-09-26 14:58:32 +00:00
|
|
|
for(i = 0; i < 4; i++)
|
|
|
|
pp_i2c_send(RTC_ADDR, RTC_ALARM_MINUTES + i, buf[i]);
|
|
|
|
|
2007-11-14 16:53:31 +00:00
|
|
|
/* note: alarm is not enabled at the point */
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_get_alarm(int *h, int *m)
|
|
|
|
{
|
|
|
|
unsigned char buf[4]={0};
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
/* get alarm preset */
|
|
|
|
i2c_readbytes(RTC_ADDR, RTC_ALARM_MINUTES, 4 ,buf);
|
2007-11-14 16:53:31 +00:00
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
*m = BCD2DEC(buf[0] & 0x7f);
|
|
|
|
*h = BCD2DEC(buf[0] & 0x3f);
|
2007-11-14 16:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-22 00:28:26 +00:00
|
|
|
void rtc_enable_alarm(bool enable)
|
2007-11-14 16:53:31 +00:00
|
|
|
{
|
|
|
|
unsigned char tmp=0;
|
|
|
|
int rv=0;
|
2008-02-14 14:00:59 +00:00
|
|
|
|
2007-11-14 16:53:31 +00:00
|
|
|
if(enable)
|
|
|
|
{
|
|
|
|
/* enable alarm interrupt */
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
tmp |= RTC_AIE;
|
2008-02-14 14:00:59 +00:00
|
|
|
tmp &= ~RTC_AF;
|
2009-09-26 14:58:32 +00:00
|
|
|
pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-14 14:00:59 +00:00
|
|
|
/* disable alarm interrupt */
|
|
|
|
if(rtc_lock_alarm_clear)
|
|
|
|
/* lock disabling alarm before it was checked whether or not the unit was started by RTC alarm */
|
2010-05-22 00:28:26 +00:00
|
|
|
return;
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
|
2008-02-14 14:00:59 +00:00
|
|
|
tmp &= ~(RTC_AIE | RTC_AF);
|
2009-09-26 14:58:32 +00:00
|
|
|
pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rtc_check_alarm_started(bool release_alarm)
|
|
|
|
{
|
|
|
|
static bool alarm_state, run_before = false;
|
|
|
|
unsigned char tmp=0;
|
|
|
|
bool started;
|
|
|
|
int rv=0;
|
|
|
|
|
|
|
|
if (run_before)
|
|
|
|
{
|
|
|
|
started = alarm_state;
|
2009-09-26 14:58:32 +00:00
|
|
|
alarm_state &= ~release_alarm;
|
2007-11-14 16:53:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* read Control 2 register which contains alarm flag */
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
|
2008-02-14 14:00:59 +00:00
|
|
|
alarm_state = started = ( (tmp & RTC_AF) && (tmp & RTC_AIE) );
|
2007-11-14 16:53:31 +00:00
|
|
|
|
2008-02-14 14:00:59 +00:00
|
|
|
if(release_alarm && started)
|
2007-11-14 16:53:31 +00:00
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
/* clear alarm interrupt enable and alarm flag */
|
|
|
|
tmp &= ~(RTC_AF | RTC_AIE);
|
2009-09-26 14:58:32 +00:00
|
|
|
pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
}
|
|
|
|
run_before = true;
|
2008-02-14 14:00:59 +00:00
|
|
|
rtc_lock_alarm_clear = false;
|
2007-11-14 16:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return started;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rtc_check_alarm_flag(void)
|
|
|
|
{
|
|
|
|
unsigned char tmp=0;
|
|
|
|
int rv=0;
|
|
|
|
|
|
|
|
/* read Control 2 register which contains alarm flag */
|
2009-09-26 14:58:32 +00:00
|
|
|
rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
|
2007-11-14 16:53:31 +00:00
|
|
|
|
|
|
|
return (tmp & RTC_AF);
|
|
|
|
}
|
2010-08-08 09:02:41 +00:00
|
|
|
#endif /* HAVE_RTC_ALARM */
|
2009-09-26 14:58:32 +00:00
|
|
|
|