/
ISL12026.cpp
executable file
·357 lines (274 loc) · 9.19 KB
/
ISL12026.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
* *
* 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: ISL12026.cpp *
* *
***************************************************************************/
#include "armlib.h"
/// Reserve memory for the singleton object.
static ISL12026 isl12026SingletonObject;
/**
* Pointer to the ISL12026 Real Time Clock object.
*/
ISL12026 *ISL12026::GetInstance()
{
return &isl12026SingletonObject;
}
/**
* Read the current real time clock value.
*
* @return pointer to the RTCTime object that contains the time
*/
const RTCTime *ISL12026::GetTime()
{
uint8_t data[8];
I2C0::GetInstance()->Start(WriteCCR);
// Set the address pointer to 0x30 to start the RTC (SRAM) locations.
data[0] = 0x00;
data[1] = 0x30;
I2C0::GetInstance()->Write(data, 2);
// Restart and read 8 byte from the device.
I2C0::GetInstance()->Start(ReadCCR);
I2C0::GetInstance()->Read(data, 8);
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[4] & 0x1f);
this->time.year = 100 * ConvertBCDToDecimal(data[7]) + ConvertBCDToDecimal(data[5]);
return &this->time;
}
/**
* Set the RTC time to UTC using the GPS fix data.
*
* @param gps pointer to GPSData object
*/
void ISL12026::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 time to the RTCTime object value.
*
* @param time pointer to desired time.
*/
void ISL12026::SetTime(const RTCTime *time)
{
uint8_t data[10];
WriteEnable();
// Set the time register values.
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = 0x30;
// Seconds.
data[2] = ConvertDecimalToBCD(time->seconds);
// Minutes.
data[3] = ConvertDecimalToBCD(time->minutes);
// Hours.
data[4] = 0x80 | ConvertDecimalToBCD(time->hours);
// Days.
data[5] = ConvertDecimalToBCD(time->day);
// Month.
data[6] = ConvertDecimalToBCD(time->month);
// Tens/ones of years.
data[7] = ConvertDecimalToBCD(time->year % 100);
// Day of week.
data[8] = 0x00;
// Thousands/hundreds of years.
data[9] = ConvertDecimalToBCD(time->year / 100);
I2C0::GetInstance()->Write(data, 10);
I2C0::GetInstance()->Stop();
WriteDisable();
}
/**
* Set the time of the alarm in HH:MM:SS.
*
* @param alarm pointer to RTCTime object that contains the alarm time
*/
void ISL12026::EnableAlarm(const RTCTime *alarm)
{
uint8_t data[20];
// Set the time register values.
WriteEnable();
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = 0x00;
// Seconds.
data[2] = 0x80 | ConvertDecimalToBCD(alarm->seconds);
// Minutes.
data[3] = 0x80 | ConvertDecimalToBCD(alarm->minutes);
// Hours.
data[4] = 0x80 | ConvertDecimalToBCD(alarm->hours);
data[5] = 0x00;
data[6] = 0x00;
data[7] = 0x00;
data[8] = 0x00;
data[9] = 0x00;
data[10] = 0x00;
I2C0::GetInstance()->Write(data, 5);
I2C0::GetInstance()->Stop();
WriteDisable();
// Set the alarm and interrupt enable bits.
WriteEnable();
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = ControlRegister;
data[2] = ControlRegisterAlarm1 | ControlRegisterInterruptMode;
I2C0::GetInstance()->Write(data, 3);
I2C0::GetInstance()->Stop();
WriteDisable();
}
/**
* Disable the alarm.
*/
void ISL12026::DisableAlarm()
{
uint8_t data[4];
// Set the alarm enable bit.
WriteEnable();
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = ControlRegister;
data[2] = 0x00;
I2C0::GetInstance()->Write(data, 3);
I2C0::GetInstance()->Stop();
WriteDisable();
}
/**
* One-shot that determines if an alarm event has occurred.
*
* @return true if alarm as occurred; otherwise false
*/
bool_t ISL12026::IsAlarm()
{
if ((GetStatus() & StatusRegisterAlarm1) == StatusRegisterAlarm1)
return true;
return false;
}
/**
* Determine if low power has been detected and the time needs to be set/reset.
*
* @return true if low power; otherwise false
*/
bool_t ISL12026::IsLowPower()
{
RTCTime time;
uint32_t status;
// Read the status twice since we sometimes get junk on the first read after power on.
status = GetStatus();
status = GetStatus();
if ((status & StatusRegisterRTCF) != StatusRegisterRTCF)
return false;
// Set the clock to GPS epoch time.
time.hours = 0;
time.minutes = 0;
time.seconds = 0;
time.month = 1;
time.day = 6;
time.year = 1980;
SetTime(&time);
return true;
}
/**
* Get the status register (0x3f) value.
*
* @return status register contents
*/
uint32_t ISL12026::GetStatus()
{
uint8_t data[4];
// Set the address pointer to 0x3f.
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = StatusRegister;
I2C0::GetInstance()->Write(data, 2);
// Restart and read 1 byte from the device.
I2C0::GetInstance()->Start(ReadCCR);
I2C0::GetInstance()->Read(data, 1);
I2C0::GetInstance()->Stop();
return data[0];
}
/**
* Clear the RWEL and WEL status register bits.
*/
void ISL12026::WriteDisable()
{
uint8_t data[4];
// Clear the status register.
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = StatusRegister;
data[2] = 0x00;
I2C0::GetInstance()->Write(data, 3);
I2C0::GetInstance()->Stop();
}
/**
* Set the RWEL and WEL status register bits to allow for CCR write operations.
*/
void ISL12026::WriteEnable()
{
uint8_t data[4];
// Set the WEL bit in the status register.
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = StatusRegister;
data[2] = StatusRegisterWEL;
I2C0::GetInstance()->Write(data, 3);
I2C0::GetInstance()->Stop();
// Set the WEL and RWEL bits in the status register.
I2C0::GetInstance()->Start(WriteCCR);
data[0] = 0x00;
data[1] = StatusRegister;
data[2] = StatusRegisterWEL | StatusRegisterRWEL;
I2C0::GetInstance()->Write(data, 3);
I2C0::GetInstance()->Stop();
}
/**
* Integration and test method used to display control register contents through UART0.
*/
void ISL12026::DumpRegisters()
{
uint8_t data[8];
I2C0::GetInstance()->Start(WriteCCR);
// Set the address pointer to 0x10.
data[0] = 0x00;
data[1] = 0x10;
I2C0::GetInstance()->Write(data, 2);
// Restart and read 1 byte from the device.
I2C0::GetInstance()->Start(ReadCCR);
I2C0::GetInstance()->Read(data, 5);
I2C0::GetInstance()->Stop();
for (int i = 0; i < 5; ++i)
UART0::GetInstance()->WriteLine("0x%lx = 0x%x", i + 0x10, data[i]);
}