2007-02-03 13:10:17 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2009-07-12 09:43:44 +00:00
|
|
|
* $Id$
|
2007-02-03 13:10:17 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Barry Wardell
|
|
|
|
*
|
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.
|
2007-02-03 13:10:17 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-10-25 09:03:47 +00:00
|
|
|
#include <stdbool.h>
|
2009-04-12 18:00:53 +00:00
|
|
|
#include "config.h"
|
2007-02-03 13:10:17 +00:00
|
|
|
#include "rtc.h"
|
2007-10-25 09:03:47 +00:00
|
|
|
#include "as3514.h"
|
2008-10-31 00:16:42 +00:00
|
|
|
#include "ascodec.h"
|
2007-02-03 13:10:17 +00:00
|
|
|
|
2010-02-22 10:25:02 +00:00
|
|
|
/* AMS Sansas start counting from Jan 1st 1970 instead of 1980 (not as3525v2) */
|
2009-04-12 18:00:53 +00:00
|
|
|
#if (CONFIG_CPU==AS3525)
|
|
|
|
#define SECS_ADJUST 315532800 /* seconds between 1970-1-1 and 1980-1-1 */
|
2010-02-22 10:25:02 +00:00
|
|
|
#elif (CONFIG_CPU==AS3525v2)
|
|
|
|
#define SECS_ADJUST 315532800 - (2*365*24*3600) - 26*(24*3600) + 7*3600 + 25*60
|
2009-04-12 18:00:53 +00:00
|
|
|
#else
|
|
|
|
#define SECS_ADJUST 0
|
|
|
|
#endif
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
#define MINUTE_SECONDS 60
|
|
|
|
#define HOUR_SECONDS 3600
|
|
|
|
#define DAY_SECONDS 86400
|
|
|
|
#define WEEK_SECONDS 604800
|
|
|
|
#define YEAR_SECONDS 31536000
|
|
|
|
#define LEAP_YEAR_SECONDS 31622400
|
|
|
|
|
|
|
|
/* Days in each month */
|
|
|
|
static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
|
|
|
|
static inline bool is_leapyear(int year)
|
|
|
|
{
|
|
|
|
if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-22 00:28:03 +00:00
|
|
|
#ifdef HAVE_RTC_ALARM /* as3543 */
|
|
|
|
static int wakeup_h;
|
|
|
|
static int wakeup_m;
|
|
|
|
static bool alarm_enabled = false;
|
|
|
|
|
|
|
|
void rtc_set_alarm(int h, int m)
|
|
|
|
{
|
|
|
|
wakeup_h = h;
|
|
|
|
wakeup_m = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_get_alarm(int *h, int *m)
|
|
|
|
{
|
|
|
|
*h = wakeup_h;
|
|
|
|
*m = wakeup_m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_alarm_poweroff(void)
|
|
|
|
{
|
|
|
|
if(!alarm_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct tm tm;
|
|
|
|
rtc_read_datetime(&tm);
|
|
|
|
int hours = wakeup_h - tm.tm_hour;
|
|
|
|
int mins = wakeup_m - tm.tm_min;
|
|
|
|
if(mins < 0)
|
|
|
|
{
|
|
|
|
mins += 60;
|
|
|
|
hours -= 1;
|
|
|
|
}
|
|
|
|
if(hours < 0)
|
|
|
|
hours += 24;
|
|
|
|
|
|
|
|
uint32_t seconds = hours*3600 + mins*60;
|
|
|
|
if(seconds == 0)
|
|
|
|
seconds = 24*3600;
|
|
|
|
|
|
|
|
seconds -= tm.tm_sec;
|
|
|
|
|
|
|
|
disable_irq();
|
|
|
|
|
|
|
|
ascodec_write(AS3543_WAKEUP, seconds);
|
|
|
|
seconds >>= 8;
|
|
|
|
ascodec_write(AS3543_WAKEUP, seconds);
|
|
|
|
seconds >>= 8;
|
|
|
|
seconds |= 1<<7; /* enable bit */
|
|
|
|
ascodec_write(AS3543_WAKEUP, seconds);
|
|
|
|
|
2010-05-24 10:06:46 +00:00
|
|
|
/* write our desired time of wake up to detect power-up from RTC */
|
2010-05-22 00:28:03 +00:00
|
|
|
ascodec_write(AS3543_WAKEUP, wakeup_h);
|
|
|
|
ascodec_write(AS3543_WAKEUP, wakeup_m);
|
|
|
|
|
2010-05-24 10:06:46 +00:00
|
|
|
/* enable hearbeat watchdog */
|
|
|
|
ascodec_write(AS3514_SYSTEM, (1<<3) | (1<<0));
|
|
|
|
|
|
|
|
/* In_Cntr : disable hearbeat source */
|
|
|
|
ascodec_write_pmu(0x1a, 4, ascodec_read_pmu(0x1a, 4) & ~(3<<2));
|
2010-05-22 00:28:03 +00:00
|
|
|
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
2010-05-22 00:28:26 +00:00
|
|
|
void rtc_enable_alarm(bool enable)
|
2010-05-22 00:28:03 +00:00
|
|
|
{
|
2010-05-22 00:28:26 +00:00
|
|
|
alarm_enabled = enable;
|
2010-05-22 00:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool rtc_check_alarm_started(bool release_alarm)
|
|
|
|
{
|
|
|
|
(void) release_alarm;
|
|
|
|
|
|
|
|
/* 3 first reads give the 23 bits counter and enable bit */
|
|
|
|
ascodec_read(AS3543_WAKEUP); /* bits 7:0 */
|
|
|
|
ascodec_read(AS3543_WAKEUP); /* bits 15:8 */
|
2010-06-17 05:28:38 +00:00
|
|
|
if(!(ascodec_read(AS3543_WAKEUP) & (1<<7))) /* enable bit */
|
|
|
|
return false;
|
2010-05-22 00:28:03 +00:00
|
|
|
|
2010-06-17 05:28:38 +00:00
|
|
|
/* subsequent reads give the 16 bytes static SRAM */
|
|
|
|
wakeup_h = ascodec_read(AS3543_WAKEUP);
|
|
|
|
wakeup_m = ascodec_read(AS3543_WAKEUP);
|
2010-05-22 00:28:03 +00:00
|
|
|
|
2010-06-17 05:28:38 +00:00
|
|
|
struct tm tm;
|
|
|
|
rtc_read_datetime(&tm);
|
2010-05-22 00:28:03 +00:00
|
|
|
|
2010-06-17 05:28:38 +00:00
|
|
|
/* were we powered up at the programmed time ? */
|
|
|
|
return wakeup_h == tm.tm_hour && wakeup_m == tm.tm_min;
|
2010-05-22 00:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool rtc_check_alarm_flag(void)
|
|
|
|
{
|
|
|
|
/* We don't need to do anything special if it has already fired */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_RTC_ALARM */
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
void rtc_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_read_datetime(struct tm *tm)
|
2007-02-03 13:10:17 +00:00
|
|
|
{
|
|
|
|
char tmp[4];
|
2009-09-26 14:58:32 +00:00
|
|
|
int i, year, mday, hour, min;
|
2007-02-03 13:10:17 +00:00
|
|
|
unsigned int seconds;
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* RTC_AS3514's slave address is 0x46*/
|
2009-09-26 14:58:32 +00:00
|
|
|
for (i = 0; i < 4; i++){
|
2008-10-31 00:16:42 +00:00
|
|
|
tmp[i] = ascodec_read(AS3514_RTC_0 + i);
|
2007-02-03 13:10:17 +00:00
|
|
|
}
|
|
|
|
seconds = tmp[0] + (tmp[1]<<8) + (tmp[2]<<16) + (tmp[3]<<24);
|
2009-04-12 18:00:53 +00:00
|
|
|
seconds -= SECS_ADJUST;
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* Convert seconds since Jan-1-1980 to format compatible with
|
|
|
|
* get_time() from firmware/common/timefuncs.c */
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* weekday */
|
2009-09-26 14:58:32 +00:00
|
|
|
tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* Year */
|
|
|
|
year = 1980;
|
2009-09-26 14:58:32 +00:00
|
|
|
while (seconds >= LEAP_YEAR_SECONDS)
|
2007-02-03 13:10:17 +00:00
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
if (is_leapyear(year)){
|
2007-02-03 13:10:17 +00:00
|
|
|
seconds -= LEAP_YEAR_SECONDS;
|
|
|
|
} else {
|
|
|
|
seconds -= YEAR_SECONDS;
|
|
|
|
}
|
2007-05-04 07:37:19 +00:00
|
|
|
|
|
|
|
year++;
|
2007-02-03 13:10:17 +00:00
|
|
|
}
|
2009-09-26 14:58:32 +00:00
|
|
|
|
|
|
|
if (is_leapyear(year)) {
|
2007-02-03 13:10:17 +00:00
|
|
|
days_in_month[1] = 29;
|
|
|
|
} else {
|
|
|
|
days_in_month[1] = 28;
|
|
|
|
if(seconds>YEAR_SECONDS){
|
|
|
|
year++;
|
|
|
|
seconds -= YEAR_SECONDS;
|
|
|
|
}
|
|
|
|
}
|
2009-09-26 14:58:32 +00:00
|
|
|
tm->tm_year = year%100 + 100;
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* Month */
|
2009-09-26 14:58:32 +00:00
|
|
|
for (i = 0; i < 12; i++)
|
2007-02-03 13:10:17 +00:00
|
|
|
{
|
2009-09-26 14:58:32 +00:00
|
|
|
if (seconds < days_in_month[i]*DAY_SECONDS){
|
|
|
|
tm->tm_mon = i;
|
2007-02-03 13:10:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
seconds -= days_in_month[i]*DAY_SECONDS;
|
|
|
|
}
|
2009-09-26 14:58:32 +00:00
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* Month Day */
|
2009-09-26 14:58:32 +00:00
|
|
|
mday = seconds/DAY_SECONDS;
|
|
|
|
seconds -= mday*DAY_SECONDS;
|
|
|
|
tm->tm_mday = mday + 1; /* 1 ... 31 */
|
2007-02-03 13:10:17 +00:00
|
|
|
|
|
|
|
/* Hour */
|
2009-09-26 14:58:32 +00:00
|
|
|
hour = seconds/HOUR_SECONDS;
|
|
|
|
seconds -= hour*HOUR_SECONDS;
|
|
|
|
tm->tm_hour = hour;
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* Minute */
|
2009-09-26 14:58:32 +00:00
|
|
|
min = seconds/MINUTE_SECONDS;
|
|
|
|
seconds -= min*MINUTE_SECONDS;
|
|
|
|
tm->tm_min = min;
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
/* Second */
|
2009-09-26 14:58:32 +00:00
|
|
|
tm->tm_sec = seconds;
|
|
|
|
|
2007-02-03 13:10:17 +00:00
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
int rtc_write_datetime(const struct tm *tm)
|
2007-02-03 13:10:17 +00:00
|
|
|
{
|
|
|
|
int i, year;
|
|
|
|
unsigned int year_days = 0;
|
|
|
|
unsigned int month_days = 0;
|
|
|
|
unsigned int seconds = 0;
|
|
|
|
|
2009-09-26 14:58:32 +00:00
|
|
|
year = 2000 + tm->tm_year - 100;
|
2007-02-03 13:10:17 +00:00
|
|
|
|
|
|
|
if(is_leapyear(year)) {
|
|
|
|
days_in_month[1] = 29;
|
|
|
|
} else {
|
|
|
|
days_in_month[1] = 28;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Number of days in months gone by this year*/
|
2009-09-26 14:58:32 +00:00
|
|
|
for(i = 0; i < tm->tm_mon; i++){
|
2007-02-03 13:10:17 +00:00
|
|
|
month_days += days_in_month[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Number of days in years gone by since 1-Jan-1980 */
|
2009-10-09 09:44:55 +00:00
|
|
|
year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
|
2007-02-03 13:10:17 +00:00
|
|
|
|
|
|
|
/* Convert to seconds since 1-Jan-1980 */
|
2009-09-26 14:58:32 +00:00
|
|
|
seconds = tm->tm_sec
|
|
|
|
+ tm->tm_min*MINUTE_SECONDS
|
|
|
|
+ tm->tm_hour*HOUR_SECONDS
|
|
|
|
+ (tm->tm_mday-1)*DAY_SECONDS
|
2007-02-03 13:10:17 +00:00
|
|
|
+ month_days*DAY_SECONDS
|
|
|
|
+ year_days*DAY_SECONDS;
|
2009-04-12 18:00:53 +00:00
|
|
|
seconds += SECS_ADJUST;
|
2007-02-03 13:10:17 +00:00
|
|
|
|
|
|
|
/* Send data to RTC */
|
2009-09-26 14:58:32 +00:00
|
|
|
for (i=0; i<4; i++){
|
2008-10-31 00:16:42 +00:00
|
|
|
ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
|
2007-02-03 13:10:17 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|