1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2016 by Roman Stolyarov
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
/*
* Real Time Clock interface for Jz4760.
*
*/
#include "config.h"
#include "cpu.h"
#include "rtc.h"
#include "timefuncs.h"
#include "logf.h"
#define RTC_FREQ_DIVIDER (32768 - 1)
/* Stolen from dietlibc-0.29/libugly/gmtime_r.c (GPLv2) */
#define SPD (24*60*60)
#define ISLEAP(year) (!(year%4) && ((year%100) || !(year%400)))
static void _localtime(const time_t t, struct tm *r)
{
time_t i;
register time_t work = t % SPD;
static int m_to_d[12] = /* This could be shared with mktime() */
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
r->tm_sec = work % 60;
work /= 60;
r->tm_min = work % 60;
r->tm_hour = work / 60;
work = t / SPD;
r->tm_wday = (4 + work) % 7;
for (i=1970; ; ++i)
{
register time_t k = ISLEAP(i) ? 366 : 365;
if (work >= k)
work -= k;
else
break;
}
r->tm_year = i - 1900;
r->tm_yday = work;
r->tm_mday = 1;
if (ISLEAP(i) && (work>58))
{
if (work==59)
r->tm_mday=2; /* 29.2. */
work-=1;
}
for (i=11; i && (m_to_d[i] > work); --i);
r->tm_mon = i;
r->tm_mday += work - m_to_d[i];
}
int rtc_read_datetime(struct tm *tm)
{
_localtime(rtc_read_reg(RTC_RTCSR), tm);
return 1;
}
int rtc_write_datetime(const struct tm *tm)
{
rtc_write_reg(RTC_RTCSR, mktime((struct tm*)tm));
return 0;
}
void rtc_init(void)
{
unsigned int cfc,hspr,rgr_1hz;
__cpm_select_rtcclk_rtc();
cfc = HSPR_RTCV;
hspr = rtc_read_reg(RTC_HSPR);
rgr_1hz = rtc_read_reg(RTC_RTCGR) & RTCGR_NC1HZ_MASK;
if((hspr != cfc) || (rgr_1hz != RTC_FREQ_DIVIDER))
{
/* We are powered on for the first time !!! */
/* Set 32768 rtc clocks per seconds */
rtc_write_reg(RTC_RTCGR, RTC_FREQ_DIVIDER);
/* Set minimum wakeup_n pin low-level assertion time for wakeup: 100ms */
rtc_write_reg(RTC_HWFCR, HWFCR_WAIT_TIME(100));
rtc_write_reg(RTC_HRCR, HRCR_WAIT_TIME(60));
/* Reset to the default time */
rtc_write_reg(RTC_RTCSR, 946681200); /* 01/01/2000 */
/* start rtc */
rtc_write_reg(RTC_RTCCR, RTCCR_RTCE);
rtc_write_reg(RTC_HSPR, cfc);
}
/* clear all rtc flags */
rtc_write_reg(RTC_HWRSR, 0);
}
|