-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRV8564C2.cpp
executable file
·278 lines (210 loc) · 7.83 KB
/
RV8564C2.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***************************************************************************
* *
* 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 program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: RV8564C2.cpp *
* *
***************************************************************************/
#include "armlib.h"
/// Reserve memory for the singleton object.
static RV8564C2 rv8564c2SingletonObject;
/**
* Pointer to the RV8564C2 Real Time Clock object.
*/
RV8564C2 *RV8564C2::GetInstance()
{
return &rv8564c2SingletonObject;
}
/**
* Read the current real time clock value.
*
* @return pointer to the RTCTime object that contains the time
*/
const RTCTime *RV8564C2::GetTime()
{
uint8_t data[10];
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 02 - Seconds.
data[0] = 0x02;
I2C0::GetInstance()->Write(data, 1);
// Restart and read 7 bytes from the device.
I2C0::GetInstance()->Start(0xa3);
I2C0::GetInstance()->Read(data, 7);
I2C0::GetInstance()->Stop();
// Populate the time structure.
this->time.seconds = ConvertBCDToDecimal(data[0] & 0x7f);
this->time.minutes = ConvertBCDToDecimal(data[1] & 0x7f);
this->time.hours = ConvertBCDToDecimal(data[2] & 0x3f);
this->time.day = ConvertBCDToDecimal(data[3] & 0x3f);
this->time.month = ConvertBCDToDecimal(data[5] & 0x1f);
this->time.year = 2000 + ConvertBCDToDecimal(data[6]);
return &this->time;
}
/**
* Set the RTC time to UTC using the GPS fix data.
*
* @param gps pointer to GPSData object
*/
void RV8564C2::SetTime(const GPSData *gps)
{
RTCTime rtc;
// Ignore the request if we don't have a valid fix.
if (gps->fixType == GPSData::NoFix)
return;
rtc.seconds = gps->seconds;
rtc.minutes = gps->minutes;
rtc.hours = gps->hours;
rtc.month = gps->month;
rtc.day = gps->day;
rtc.year = gps->year;
SetTime (&rtc);
}
/**
* Set the RTC hardware. Setting the time will also clear the low power indicator bit.
* Check the low power bit BEFORE setting the time.
*
* @param rtc pointer to RTCTime object
*/
void RV8564C2::SetTime(const RTCTime *rtc)
{
uint8_t data[10];
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 00 - Control / Status 1.
data[0] = 0x00;
// Control / Status 1 byte. Set the STOP bit so we clear the pre-scalar and stop the clock during the update.
data[1] = CONTROL1_STOP;
// Control / Status 2 byte.
data[2] = 0x00;
// Seconds.
data[3] = ConvertDecimalToBCD(rtc->seconds);
// Minutes.
data[4] = ConvertDecimalToBCD(rtc->minutes);
// Hours.
data[5] = ConvertDecimalToBCD(rtc->hours);
// Days.
data[6] = ConvertDecimalToBCD(rtc->day);
// Weekdays.
data[7] = 0x00;
// Month / Century.
data[8] = ConvertDecimalToBCD(rtc->month);
// Years.
data[9] = ConvertDecimalToBCD(rtc->year - 2000);
I2C0::GetInstance()->Write(data, 10);
I2C0::GetInstance()->Stop();
// Clear the STOP bit so the clock will now run.
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 00 - Control / Status 1.
data[0] = 0x00;
data[1] = 0x00;
I2C0::GetInstance()->Write(data, 2);
I2C0::GetInstance()->Stop();
}
/**
* Set the time of the alarm in Hours/Minutes. The alarm time is cleared
* if the system time is updated.
*
* @param alarm pointer to RTCTime object that contains the alarm time
*/
void RV8564C2::EnableAlarm(const RTCTime *alarm)
{
uint8_t data[5];
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 09 - Minute Alarm.
data[0] = 0x09;
// Minute Alarm.
data[1] = ConvertDecimalToBCD(alarm->minutes);
// Hour Alarm.
data[2] = ConvertDecimalToBCD(alarm->hours);
// Day Alarm set to ignore state.
data[3] = ALARM_AD;
// Weekday Alarm set to ignore state.
data[4] = ALARM_AD;
I2C0::GetInstance()->Write(data, 5);
I2C0::GetInstance()->Stop();
// Enable the alarm clock function.
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 01 - Control / Status 2.
data[0] = 0x01;
data[1] = CONTROL2_AIE;
I2C0::GetInstance()->Write(data, 2);
I2C0::GetInstance()->Stop();
}
/**
* One-shot that determines if an alarm event has occurred.
*
* @return true if alarm as occurred; otherwise false
*/
bool_t RV8564C2::IsAlarm()
{
uint8_t data[2];
uint32_t status;
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 01 - Control / Status 2.
data[0] = 0x01;
I2C0::GetInstance()->Write(data, 1);
// Restart and read 1 byte from the device.
I2C0::GetInstance()->Start(0xa3);
I2C0::GetInstance()->Read(data, 1);
I2C0::GetInstance()->Stop();
status = data[0];
if ((status & CONTROL2_AF) == 0x00)
return false;
// Clear the alarm flag if it has occurred.
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 01 - Control / Status 2.
data[0] = 0x01;
// Clear the AF flag.
data[1] = status & ~CONTROL2_AF;
I2C0::GetInstance()->Write(data, 2);
I2C0::GetInstance()->Stop();
return true;
}
/**
* Determine if low power has been detected and the time needs to be set/reset.
*
* @return true if low power; otherwise false
*/
bool_t RV8564C2::IsLowPower()
{
RTCTime time;
uint8_t data[2];
uint32_t seconds;
I2C0::GetInstance()->Start(0xa2);
// Set the address pointer to start at 02 - Seconds.
data[0] = 0x02;
I2C0::GetInstance()->Write(data, 1);
// Restart and read 1 byte from the device.
I2C0::GetInstance()->Start(0xa3);
I2C0::GetInstance()->Read(data, 1);
I2C0::GetInstance()->Stop();
seconds = data[0];
if ((seconds & 0x80) == 0x00)
return false;
// Set the clock to 0000 UTC on 1 Jan 2008.
time.hours = 0;
time.minutes = 0;
time.seconds = 0;
time.month = 1;
time.day = 1;
time.year = 2008;
SetTime(&time);
return true;
}