-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparkFun_RV3028.cpp
319 lines (261 loc) · 7.31 KB
/
SparkFun_RV3028.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
/******************************************************************************
SparkFun_RV3028.h
RV3028 Arduino Library
Andy England @ SparkFun Electronics
February 5, 2018
https://github.com/sparkfun/Qwiic_RTC
Development environment specifics:
Arduino IDE 1.6.4
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact [email protected].
Distributed as-is; no warranty is given.
******************************************************************************/
#include "SparkFun_RV3028.h"
#ifndef __MICROBIT_H_
#define __MICROBIT_H_
#include "MicroBit.h"
#endif
#include "pxt.h"
//static MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
enum time_order {
TIME_SECONDS, // 0
TIME_MINUTES, // 1
TIME_HOURS, // 2
TIME_DAY,
TIME_DATE, // 3
TIME_MONTH, // 4
TIME_YEAR, // 5
};
uint8_t _time[TIME_ARRAY_LENGTH];
//uint8_t _timestamp[TIMESTAMP_ARRAY_LENGTH];
//Constructor -- Specifies default configuration
RV3028::RV3028( void )
{
_tsCount = 0;
_previousTsCount = 0;
_timestampInitialized = false;
}
//Configure RTC to output 1-12 hours
//Converts any current hour setting to 12 hour
void RV3028::set12Hour()
{
//Set the 12/24 hour bit
uint8_t setting = readRegister(RV3028_CTRL2);
setting |= (1<<CTRL2_12_24);
writeRegister(RV3028_CTRL2, setting);
}
//Configure RTC to output 0-23 hours
//Converts any current hour setting to 24 hour
void RV3028::set24Hour()
{
//Change to 24 hour mode
uint8_t setting = readRegister(RV3028_CTRL2);
setting &= ~(1<<CTRL2_12_24); //Clear the 12/24 hr bit
writeRegister(RV3028_CTRL2, setting);
}
//Returns true if RTC has been configured for 12 hour mode
bool RV3028::is12Hour()
{
uint8_t controlRegister = readRegister(RV3028_CTRL2);
return(controlRegister & (1<<CTRL2_12_24));
}
//Returns true if RTC has PM bit set and 12Hour bit set
bool RV3028::isPM()
{
return readRegister(RV3028_HOURS) & (1<<HOURS_AM_PM);
}
//Returns the status byte. This likely clears the interrupts as well.
//See .begin() for ARST bit setting
uint8_t RV3028::status(void)
{
return(readRegister(RV3028_STATUS));
}
bool RV3028::setTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t date, uint8_t month, uint8_t year, uint8_t day)
{
_time[TIME_SECONDS] = DECtoBCD(sec);
_time[TIME_MINUTES] = DECtoBCD(min);
_time[TIME_HOURS] = DECtoBCD(hour);
_time[TIME_DATE] = DECtoBCD(date);
_time[TIME_DAY] = DECtoBCD(day);
_time[TIME_MONTH] = DECtoBCD(month);
_time[TIME_YEAR] = DECtoBCD(year);
return setTime(_time, TIME_ARRAY_LENGTH);
}
// setTime -- Set time and date/day registers of RV3028 (using data array)
bool RV3028::setTime(uint8_t * time, uint8_t len)
{
if (len != TIME_ARRAY_LENGTH)
return false;
writeMultipleRegisters(RV3028_SECONDS, time, len);
}
void RV3028::setSeconds(uint8_t value)
{
writeRegister(RV3028_SECONDS, DECtoBCD(value));
}
void RV3028::setMinutes(uint8_t value)
{
writeRegister(RV3028_MINUTES, DECtoBCD(value));
}
void RV3028::setHours(uint8_t value)
{
writeRegister(RV3028_HOURS, DECtoBCD(value));
}
void RV3028::setDate(uint8_t value)
{
writeRegister(RV3028_DATE, DECtoBCD(value));
}
void RV3028::setMonth(uint8_t value)
{
writeRegister(RV3028_MONTHS, DECtoBCD(value));
}
void RV3028::setYear(uint8_t value)
{
writeRegister(RV3028_YEARS, DECtoBCD(value));
}
void RV3028::setWeekday(uint8_t value)
{
writeRegister(RV3028_WEEKDAYS, DECtoBCD(value));
}
void RV3028::initializeTimestamping()
{
if (_timestampInitialized == false)
{
uint8_t setting = readRegister(RV3028_CTRL2);
setting |= (1<<CTRL2_TSE); //Set timestamp to trigger on button
writeRegister(RV3028_CTRL2, setting);
setting = readRegister(RV3028_EVENT_CTRL);
setting |= (0b01<<EVENT_CTRL_ET); //Set read of EVI pin to a 3.9 ms sampling period to debounce the switch
setting |= (1<<EVENT_CTRL_TSOW); //Set TSOW so that we overwrite the TS register with each new button press
writeRegister(RV3028_EVENT_CTRL, setting);
_timestampInitialized = true;
}
}
//Move the hours, mins, sec, etc registers from RV-3028 into the _time array
//Needs to be called before printing time or date
//We do not protect the GPx registers. They will be overwritten. The user has plenty of RAM if they need it.
void RV3028::updateTime()
{
readMultipleRegisters(RV3028_SECONDS, _time, TIME_ARRAY_LENGTH);
if(is12Hour()) _time[TIME_HOURS] &= ~(1<<HOURS_AM_PM); //Remove this bit from value
}
bool RV3028::updateTimestamp()
{
initializeTimestamping();
bool newStamp = false;
_tsCount = readRegister(RV3028_COUNT_TS);
if (_tsCount != _previousTsCount)
{
newStamp = true;
readMultipleRegisters(RV3028_SECONDS_TS, _timestamp, TIMESTAMP_ARRAY_LENGTH);
if(is12Hour()) {
_timestamp[TIME_HOURS] &= ~(1<<HOURS_AM_PM); //Remove this bit from value
}
}
_previousTsCount = _tsCount;
return newStamp;
}
bool RV3028::getTimeStampInitialized()
{
if (_timestampInitialized == false)
return false;
else
return true;
}
uint8_t RV3028::getSeconds()
{
return BCDtoDEC(_time[TIME_SECONDS]);
}
uint8_t RV3028::getMinutes()
{
return BCDtoDEC(_time[TIME_MINUTES]);
}
uint8_t RV3028::getHours()
{
return BCDtoDEC(_time[TIME_HOURS]);
}
uint8_t RV3028::getDate()
{
return BCDtoDEC(_time[TIME_DATE]);
}
uint8_t RV3028::getMonth()
{
return BCDtoDEC(_time[TIME_MONTH]);
}
uint8_t RV3028::getYear()
{
return BCDtoDEC(_time[TIME_YEAR]);
}
uint8_t RV3028::getWeekday()
{
return BCDtoDEC(_time[TIME_DAY]);
}
uint8_t RV3028::getSecondsTimestamp()
{
return BCDtoDEC(_timestamp[TIME_SECONDS]);
}
uint8_t RV3028::getMinutesTimestamp()
{
return BCDtoDEC(_timestamp[TIME_MINUTES]);
}
uint8_t RV3028::getHoursTimestamp()
{
return BCDtoDEC(_timestamp[TIME_HOURS]);
}
uint8_t RV3028::getDateTimestamp()
{
return BCDtoDEC(_timestamp[TIME_DATE - 1]); //Subtract one due to lack of weekday register in timestamp
}
uint8_t RV3028::getMonthTimestamp()
{
return BCDtoDEC(_timestamp[TIME_MONTH - 1]); //Subtract one due to lack of weekday register in timestamp
}
uint8_t RV3028::getYearTimestamp()
{
return BCDtoDEC(_timestamp[TIME_YEAR - 1]); //Subtract one due to lack of weekday register in timestamp
}
uint8_t RV3028::BCDtoDEC(uint8_t val)
{
return ( ( val / 0x10) * 10 ) + ( val % 0x10 );
}
// BCDtoDEC -- convert decimal to binary-coded decimal (BCD)
uint8_t RV3028::DECtoBCD(uint8_t val)
{
return ( ( val / 10 ) * 0x10 ) + ( val % 10 );
}
uint8_t RV3028::readRegister(uint8_t addr)
{
uint8_t data;
uBit.i2c.readRegister(RV3028_ADDR, addr, &data, 1);
return data;
}
void RV3028::readMultipleRegisters(uint8_t addr, uint8_t * dest, uint8_t len)
{
uBit.i2c.readRegister(RV3028_ADDR, addr, dest, len);
}
void RV3028::writeRegister(uint8_t addr, uint8_t val)
{
// uBit.i2c.write(RV3028_ADDR, addr, val);
uint8_t length = 1;
uint8_t realLength = length + 1;
#if MICROBIT_CODAL
uint8_t temp[realLength];
#else
char temp[realLength];
#endif
temp[0] = addr;
memcpy(&temp[1], &val, length); //tempLong is 4 bytes, we only need 3
uBit.i2c.write(RV3028_ADDR, temp, realLength);
}
void RV3028::writeMultipleRegisters(uint8_t addr, uint8_t * values, uint8_t length)
{
uint8_t realLength = length + 1;
#if MICROBIT_CODAL
uint8_t temp[realLength];
#else
char temp[realLength];
#endif
temp[0] = addr;
memcpy(&temp[1], values, length); //tempLong is 4 bytes, we only need 3
uBit.i2c.write(RV3028_ADDR, temp, realLength);
}