2006-12-05 09:35:16 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum
|
|
|
|
*
|
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-12-05 09:35:16 +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"
|
2017-01-26 00:32:15 +00:00
|
|
|
#include "timefuncs.h"
|
2006-12-05 09:35:16 +00:00
|
|
|
|
|
|
|
void rtc_init(void)
|
|
|
|
{
|
|
|
|
/* RTC Enable */
|
|
|
|
RTCCON |= 1;
|
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_read_datetime(struct tm *tm)
|
2006-12-05 09:35:16 +00:00
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
tm->tm_sec = BCD2DEC(BCDSEC);
|
|
|
|
tm->tm_min = BCD2DEC(BCDMIN);
|
|
|
|
tm->tm_hour = BCD2DEC(BCDHOUR);
|
|
|
|
tm->tm_mday = BCD2DEC(BCDDATE);
|
|
|
|
tm->tm_mon = BCD2DEC(BCDMON) - 1;
|
|
|
|
tm->tm_year = BCD2DEC(BCDYEAR) + 100;
|
2017-01-26 00:32:15 +00:00
|
|
|
tm->tm_yday = 0; /* Not implemented for now */
|
|
|
|
|
|
|
|
set_day_of_week(tm);
|
2006-12-05 09:35:16 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_write_datetime(const struct tm *tm)
|
2006-12-05 09:35:16 +00:00
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
BCDSEC = DEC2BCD(tm->tm_sec);
|
|
|
|
BCDMIN = DEC2BCD(tm->tm_min);
|
|
|
|
BCDHOUR = DEC2BCD(tm->tm_hour);
|
|
|
|
BCDDAY = DEC2BCD(tm->tm_wday) + 1; /* chip wants 1..7 for wday */
|
|
|
|
BCDDATE = DEC2BCD(tm->tm_mday);
|
|
|
|
BCDMON = DEC2BCD(tm->tm_mon + 1);
|
|
|
|
BCDYEAR = DEC2BCD(tm->tm_year - 100);
|
2006-12-05 09:35:16 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-07 19:34:34 +00:00
|
|
|
#ifdef HAVE_RTC_ALARM
|
2008-11-12 03:52:33 +00:00
|
|
|
/* This alarm code works with a flashed bootloader. This will not work with
|
|
|
|
* the OF bootloader.
|
2007-04-09 06:27:35 +00:00
|
|
|
*/
|
2008-11-12 03:52:33 +00:00
|
|
|
|
|
|
|
/* Check whether the unit has been started by the RTC alarm function */
|
2007-04-09 06:27:35 +00:00
|
|
|
bool rtc_check_alarm_started(bool release_alarm)
|
|
|
|
{
|
2008-11-12 03:52:33 +00:00
|
|
|
if (GSTATUS3)
|
|
|
|
{
|
|
|
|
GSTATUS3 &= ~release_alarm;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
2007-04-09 06:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 03:52:33 +00:00
|
|
|
/* Check to see if the alarm has flaged since the last it was checked */
|
2007-04-09 06:27:35 +00:00
|
|
|
bool rtc_check_alarm_flag(void)
|
|
|
|
{
|
2008-11-12 03:52:33 +00:00
|
|
|
bool ret=SRCPND & 0x40000000;
|
|
|
|
|
|
|
|
SRCPND=RTC_MASK;
|
|
|
|
|
|
|
|
return ret;
|
2007-04-09 06:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set alarm time registers to the given time (repeat once per day) */
|
|
|
|
void rtc_set_alarm(int h, int m)
|
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
ALMMIN = DEC2BCD(m); /* minutes */
|
|
|
|
ALMHOUR = DEC2BCD(h); /* hour */
|
2007-04-09 06:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read out the current alarm time */
|
|
|
|
void rtc_get_alarm(int *h, int *m)
|
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
*m = BCD2DEC(ALMMIN);
|
|
|
|
*h = BCD2DEC(ALMHOUR);
|
2007-04-09 06:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* turn alarm on or off by setting the alarm flag enable
|
|
|
|
*/
|
2010-06-06 17:37:13 +00:00
|
|
|
void rtc_enable_alarm(bool enable)
|
2007-04-09 06:27:35 +00:00
|
|
|
{
|
|
|
|
if (enable)
|
2007-05-07 19:34:34 +00:00
|
|
|
{
|
2007-04-09 06:27:35 +00:00
|
|
|
RTCALM=0x46;
|
2007-05-07 19:34:34 +00:00
|
|
|
}
|
2007-04-09 06:27:35 +00:00
|
|
|
else
|
2007-05-07 19:34:34 +00:00
|
|
|
{
|
2007-04-09 06:27:35 +00:00
|
|
|
RTCALM=0x00;
|
2007-05-07 19:34:34 +00:00
|
|
|
}
|
2007-04-09 06:27:35 +00:00
|
|
|
}
|
|
|
|
#endif
|