-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathds3231_for_stm32_hal.c
460 lines (410 loc) · 14.3 KB
/
ds3231_for_stm32_hal.c
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* An STM32 HAL library written for the DS3231 real-time clock IC. */
/* Library by @eepj www.github.com/eepj */
#include "ds3231_for_stm32_hal.h"
#include "main.h"
#ifdef __cplusplus
extern "C"{
#endif
I2C_HandleTypeDef *_ds3231_ui2c;
/**
* @brief Initializes the DS3231 module. Set clock halt bit to 0 to start timing.
* @param hi2c User I2C handle pointer.
*/
void DS3231_Init(I2C_HandleTypeDef *hi2c) {
_ds3231_ui2c = hi2c;
DS3231_EnableAlarm1(DS3231_DISABLED);
DS3231_EnableAlarm2(DS3231_DISABLED);
DS3231_ClearAlarm1Flag();
DS3231_ClearAlarm2Flag();
DS3231_SetInterruptMode(DS3231_ALARM_INTERRUPT);
}
/**
* @brief Set the byte in the designated DS3231 register to value.
* @param regAddr Register address to write.
* @param val Value to set, 0 to 255.
*/
void DS3231_SetRegByte(uint8_t regAddr, uint8_t val) {
uint8_t bytes[2] = { regAddr, val };
HAL_I2C_Master_Transmit(_ds3231_ui2c, DS3231_I2C_ADDR << 1, bytes, 2, DS3231_TIMEOUT);
}
/**
* @brief Gets the byte in the designated DS3231 register.
* @param regAddr Register address to read.
* @return Value stored in the register, 0 to 255.
*/
uint8_t DS3231_GetRegByte(uint8_t regAddr) {
uint8_t val;
HAL_I2C_Master_Transmit(_ds3231_ui2c, DS3231_I2C_ADDR << 1, ®Addr, 1, DS3231_TIMEOUT);
HAL_I2C_Master_Receive(_ds3231_ui2c, DS3231_I2C_ADDR << 1, &val, 1, DS3231_TIMEOUT);
return val;
}
/**
* @brief Enables battery-backed square wave output at the INT#/SQW pin.
* @param enable Enable, DS3231_ENABLED or DS3231_DISABLED.
*/
void DS3231_EnableBatterySquareWave(DS3231_State enable){
uint8_t control = DS3231_GetRegByte(DS3231_REG_CONTROL);
DS3231_SetRegByte(DS3231_REG_CONTROL, (control & 0xbf) | ((enable & 0x01) << DS3231_BBSQW));
}
/**
* @brief Set the interrupt mode to either alarm interrupt or square wave interrupt.
* @param mode Interrupt mode to set, DS3231_ALARM_INTERRUPT or DS3231_SQUARE_WAVE_INTERRUPT.
*/
void DS3231_SetInterruptMode(DS3231_InterruptMode mode){
uint8_t control = DS3231_GetRegByte(DS3231_REG_CONTROL);
DS3231_SetRegByte(DS3231_REG_CONTROL, (control & 0xfb) | ((mode & 0x01) << DS3231_INTCN));
}
/**
* @brief Set frequency of the square wave output
* @param rate Frequency to set, DS3231_1HZ, DS3231_1024HZ, DS3231_4096HZ or DS3231_8192HZ.
*/
void DS3231_SetRateSelect(DS3231_Rate rate){
uint8_t control = DS3231_GetRegByte(DS3231_REG_CONTROL);
DS3231_SetRegByte(DS3231_REG_CONTROL, (control & 0xe7) | ((rate & 0x03) << DS3231_RS1));
}
/**
* @brief Enables clock oscillator.
* @param enable Enable, DS3231_ENABLED or DS3231_DISABLED.
*/
void DS3231_EnableOscillator(DS3231_State enable){
uint8_t control = DS3231_GetRegByte(DS3231_REG_CONTROL);
DS3231_SetRegByte(DS3231_REG_CONTROL, (control & 0x7f) | ((!enable & 0x01) << DS3231_EOSC));
}
/**
* @brief Enables alarm 2.
* @param enable Enable, DS3231_ENABLED or DS3231_DISABLED.
*/
void DS3231_EnableAlarm2(DS3231_State enable){
uint8_t control = DS3231_GetRegByte(DS3231_REG_CONTROL);
DS3231_SetRegByte(DS3231_REG_CONTROL, (control & 0xfd) | ((enable & 0x01) << DS3231_A2IE));
DS3231_SetInterruptMode(DS3231_ALARM_INTERRUPT);
}
/**
* @brief Clears alarm 2 matched flag. Matched flags must be cleared before the next match or the next interrupt will be masked.
*/
void DS3231_ClearAlarm2Flag(){
uint8_t status = DS3231_GetRegByte(DS3231_REG_STATUS) & 0xfd;
DS3231_SetRegByte(DS3231_REG_STATUS, status & ~(0x01 << DS3231_A2F));
}
/**
* @brief Set alarm 2 minute to match. Does not change alarm 2 matching mode.
* @param minute Minute, 0 to 59.
*/
void DS3231_SetAlarm2Minute(uint8_t minute){
uint8_t temp = DS3231_GetRegByte(DS3231_A2_MINUTE) & 0x80;
uint8_t a2m2 = temp | (DS3231_EncodeBCD(minute) & 0x3f);
DS3231_SetRegByte(DS3231_A2_MINUTE, a2m2);
}
/**
* @brief Set alarm 2 hour to match. Does not change alarm 2 matching mode.
* @param hour Hour to match in 24h format, 0 to 23.
*/
void DS3231_SetAlarm2Hour(uint8_t hour_24mode){
uint8_t temp = DS3231_GetRegByte(DS3231_A2_HOUR) & 0x80;
uint8_t a2m3 = temp | (DS3231_EncodeBCD(hour_24mode) & 0x3f);
DS3231_SetRegByte(DS3231_A2_HOUR, a2m3);
}
/**
* @brief Set alarm 2 date. Alarm 2 can only be set to match either date or day. Does not change alarm 2 matching mode.
* @param date Date, 0 to 31.
*/
void DS3231_SetAlarm2Date(uint8_t date){
uint8_t temp = DS3231_GetRegByte(DS3231_A2_DATE) & 0x80;
uint8_t a2m4 = temp | (DS3231_EncodeBCD(date) & 0x3f);
DS3231_SetRegByte(DS3231_A2_DATE, a2m4);
}
/**
* @brief Set alarm 2 day. Alarm 2 can only be set to match either date or day. Does not change alarm 2 matching mode.
* @param day Days since last Sunday, 1 to 7.
*/
void DS3231_SetAlarm2Day(uint8_t day){
uint8_t temp = DS3231_GetRegByte(DS3231_A2_DATE) & 0x80;
uint8_t a2m4 = temp | (0x01 << DS3231_DYDT) | (DS3231_EncodeBCD(day) & 0x3f);
DS3231_SetRegByte(DS3231_A2_DATE, a2m4);
}
/**
* @brief Set alarm 2 mode.
* @param alarmMode Alarm 2 mode, DS3231_A2_EVERY_M, DS3231_A2_MATCH_M, DS3231_A2_MATCH_M_H, DS3231_A2_MATCH_M_H_DATE or DS3231_A2_MATCH_M_H_DAY.
*/
void DS3231_SetAlarm2Mode(DS3231_Alarm2Mode alarmMode){
uint8_t temp;
temp = DS3231_GetRegByte(DS3231_A1_MINUTE) & 0x7f;
DS3231_SetRegByte(DS3231_A2_MINUTE, temp | (((alarmMode >> 0) & 0x01) << DS3231_AXMY));
temp = DS3231_GetRegByte(DS3231_A1_HOUR) & 0x7f;
DS3231_SetRegByte(DS3231_A2_HOUR, temp | (((alarmMode >> 1) & 0x01) << DS3231_AXMY));
temp = DS3231_GetRegByte(DS3231_A1_DATE) & 0x7f;
DS3231_SetRegByte(DS3231_A2_DATE, temp | (((alarmMode >> 2) & 0x01) << DS3231_AXMY) | (alarmMode & 0x80));
}
/**
* @brief Enables alarm 1.
* @param enable Enable, DS3231_ENABLED or DS3231_DISABLED.
*/
void DS3231_EnableAlarm1(DS3231_State enable){
uint8_t control = DS3231_GetRegByte(DS3231_REG_CONTROL);
DS3231_SetRegByte(DS3231_REG_CONTROL, (control & 0xfe) | ((enable & 0x01) << DS3231_A1IE));
DS3231_SetInterruptMode(DS3231_ALARM_INTERRUPT);
}
/**
* @brief Clears alarm 1 matched flag. Matched flags must be cleared before the next match or the next interrupt will be masked.
*/
void DS3231_ClearAlarm1Flag(){
uint8_t status = DS3231_GetRegByte(DS3231_REG_STATUS) & 0xfe;
DS3231_SetRegByte(DS3231_REG_STATUS, status & ~(0x01 << DS3231_A1F));
}
/**
* @brief Set alarm 1 second to match. Does not change alarm 1 matching mode.
* @param second Second, 0 to 59.
*/
void DS3231_SetAlarm1Second(uint8_t second){
uint8_t temp = DS3231_GetRegByte(DS3231_A1_SECOND) & 0x80;
uint8_t a1m1 = temp | (DS3231_EncodeBCD(second) & 0x3f);
DS3231_SetRegByte(DS3231_A1_SECOND, a1m1);
}
/**
* @brief Set alarm 1 minute to match. Does not change alarm 1 matching mode.
* @param minute Minute, 0 to 59.
*/
void DS3231_SetAlarm1Minute(uint8_t minute){
uint8_t temp = DS3231_GetRegByte(DS3231_A1_MINUTE) & 0x80;
uint8_t a1m2 = temp | (DS3231_EncodeBCD(minute) & 0x3f);
DS3231_SetRegByte(DS3231_A1_MINUTE, a1m2);
}
/**
* @brief Set alarm 1 hour to match. Does not change alarm 1 matching mode.
* @param hour Hour, 0 to 59.
*/
void DS3231_SetAlarm1Hour(uint8_t hour_24mode){
uint8_t temp = DS3231_GetRegByte(DS3231_A1_HOUR) & 0x80;
uint8_t a1m3 = temp | (DS3231_EncodeBCD(hour_24mode) & 0x3f);
DS3231_SetRegByte(DS3231_A1_HOUR, a1m3);
}
/**
* @brief Set alarm 1 date. Alarm 1 can only be set to match either date or day. Does not change alarm 1 matching mode.
* @param date Date, 0 to 31.
*/
void DS3231_SetAlarm1Date(uint8_t date){
uint8_t temp = DS3231_GetRegByte(DS3231_A1_DATE) & 0x80;
uint8_t a1m4 = temp | (DS3231_EncodeBCD(date) & 0x3f);
DS3231_SetRegByte(DS3231_A1_DATE, a1m4);
}
/**
* @brief Set alarm 1 day. Alarm 1 can only be set to match either date or day. Does not change alarm 1 matching mode.
* @param day Days since last Sunday, 1 to 7.
*/
void DS3231_SetAlarm1Day(uint8_t day){
uint8_t temp = DS3231_GetRegByte(DS3231_A1_DATE) & 0x80;
uint8_t a1m4 = temp | (0x01 << DS3231_DYDT) | (DS3231_EncodeBCD(day) & 0x3f);
DS3231_SetRegByte(DS3231_A1_DATE, a1m4);
}
/**
* @brief Set alarm 1 mode.
* @param alarmMode Alarm 1 mode, DS3231_A1_EVERY_S, DS3231_A1_MATCH_S, DS3231_A1_MATCH_S_M, DS3231_A1_MATCH_S_M_H, DS3231_A1_MATCH_S_M_H_DATE or DS3231_A1_MATCH_S_M_H_DAY.
*/
void DS3231_SetAlarm1Mode(DS3231_Alarm1Mode alarmMode){
uint8_t temp;
temp = DS3231_GetRegByte(DS3231_A1_SECOND) & 0x7f;
DS3231_SetRegByte(DS3231_A1_SECOND, temp | (((alarmMode >> 0) & 0x01) << DS3231_AXMY));
temp = DS3231_GetRegByte(DS3231_A1_MINUTE) & 0x7f;
DS3231_SetRegByte(DS3231_A1_MINUTE, temp | (((alarmMode >> 1) & 0x01) << DS3231_AXMY));
temp = DS3231_GetRegByte(DS3231_A1_HOUR) & 0x7f;
DS3231_SetRegByte(DS3231_A1_HOUR, temp | (((alarmMode >> 2) & 0x01) << DS3231_AXMY));
temp = DS3231_GetRegByte(DS3231_A1_DATE) & 0x7f;
DS3231_SetRegByte(DS3231_A1_DATE, temp | (((alarmMode >> 3) & 0x01) << DS3231_AXMY) | (alarmMode & 0x80));
}
/**
* @brief Check whether the clock oscillator is stopped.
* @return Oscillator stopped flag (OSF) bit, 0 or 1.
*/
uint8_t DS3231_IsOscillatorStopped(){
return (DS3231_GetRegByte(DS3231_REG_STATUS) >> DS3231_OSF) & 0x01;
}
/**
* @brief Check whether the 32kHz output is enabled.
* @return EN32kHz flag bit, 0 or 1.
*/
uint8_t DS3231_Is32kHzEnabled(){
return (DS3231_GetRegByte(DS3231_REG_STATUS) >> DS3231_EN32KHZ) & 0x01;
}
/**
* @brief Check if alarm 1 is triggered.
* @return A1F flag bit, 0 or 1.
*/
uint8_t DS3231_IsAlarm1Triggered(){
return (DS3231_GetRegByte(DS3231_REG_STATUS) >> DS3231_A1F) & 0x01;
}
/**
* @brief Check if alarm 2 is triggered.
* @return A2F flag bit, 0 or 1.
*/
uint8_t DS3231_IsAlarm2Triggered(){
return (DS3231_GetRegByte(DS3231_REG_STATUS) >> DS3231_A2F) & 0x01;
}
/**
* @brief Gets the current day of week.
* @return Days from last Sunday, 0 to 6.
*/
uint8_t DS3231_GetDayOfWeek(void) {
return DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_DOW));
}
/**
* @brief Gets the current day of month.
* @return Day of month, 1 to 31.
*/
uint8_t DS3231_GetDate(void) {
return DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_DATE));
}
/**
* @brief Gets the current month.
* @return Month, 1 to 12.
*/
uint8_t DS3231_GetMonth(void) {
return DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_MONTH) & 0x7f);
}
/**
* @brief Gets the current year.
* @return Year, 2000 to 2199.
*/
uint16_t DS3231_GetYear(void) {
uint8_t decYear = DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_YEAR));
uint16_t century = (DS3231_GetRegByte(DS3231_REG_MONTH) >> DS3231_CENTURY) * 100 + 2000;
return century + decYear;
}
/**
* @brief Gets the current hour in 24h format.
* @return Hour in 24h format, 0 to 23.
*/
uint8_t DS3231_GetHour(void) {
return DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_HOUR));
}
/**
* @brief Gets the current minute.
* @return Minute, 0 to 59.
*/
uint8_t DS3231_GetMinute(void) {
return DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_MINUTE));
}
/**
* @brief Gets the current second. Clock halt bit not included.
* @return Second, 0 to 59.
*/
uint8_t DS3231_GetSecond(void) {
return DS3231_DecodeBCD(DS3231_GetRegByte(DS3231_REG_SECOND));
}
/**
* @brief Set the current day of week.
* @param dayOfWeek Days since last Sunday, 1 to 7.
*/
void DS3231_SetDayOfWeek(uint8_t dayOfWeek) {
DS3231_SetRegByte(DS3231_REG_DOW, DS3231_EncodeBCD(dayOfWeek));
}
/**
* @brief Set the current day of month.
* @param date Day of month, 1 to 31.
*/
void DS3231_SetDate(uint8_t date) {
DS3231_SetRegByte(DS3231_REG_DATE, DS3231_EncodeBCD(date));
}
/**
* @brief Set the current month.
* @param month Month, 1 to 12.
*/
void DS3231_SetMonth(uint8_t month) {
uint8_t century = DS3231_GetRegByte(DS3231_REG_MONTH) & 0x80;
DS3231_SetRegByte(DS3231_REG_MONTH, DS3231_EncodeBCD(month) | century);
}
/**
* @brief Set the current year.
* @param year Year, 2000 to 2199.
*/
void DS3231_SetYear(uint16_t year) {
uint8_t century = (year / 100) % 20;
uint8_t monthReg = (DS3231_GetRegByte(DS3231_REG_MONTH) & 0x7f) | (century << DS3231_CENTURY);
DS3231_SetRegByte(DS3231_REG_MONTH, monthReg);
DS3231_SetRegByte(DS3231_REG_YEAR, DS3231_EncodeBCD(year % 100));
}
/**
* @brief Set the current hour, in 24h format.
* @param hour_24mode Hour in 24h format, 0 to 23.
*/
void DS3231_SetHour(uint8_t hour_24mode) {
DS3231_SetRegByte(DS3231_REG_HOUR, DS3231_EncodeBCD(hour_24mode & 0x3f));
}
/**
* @brief Set the current minute.
* @param minute Minute, 0 to 59.
*/
void DS3231_SetMinute(uint8_t minute) {
DS3231_SetRegByte(DS3231_REG_MINUTE, DS3231_EncodeBCD(minute));
}
/**
* @brief Set the current second.
* @param second Second, 0 to 59.
*/
void DS3231_SetSecond(uint8_t second) {
DS3231_SetRegByte(DS3231_REG_SECOND, DS3231_EncodeBCD(second));
}
/**
* @brief Set the current time.
* @param hour_24mode Hour in 24h format, 0 to 23.
* @param minute Minute, 0 to 59.
* @param second Second, 0 to 59.
*/
void DS3231_SetFullTime(uint8_t hour_24mode, uint8_t minute, uint8_t second){
DS3231_SetHour(hour_24mode);
DS3231_SetMinute(minute);
DS3231_SetSecond(second);
}
/**
* @brief Set the current date, month, day of week and year.
* @param date Date, 0 to 31.
* @param month Month, 1 to 12.
* @param dow Days since last Sunday, 1 to 7.
* @param year Year, 2000 to 2199.
*/
void DS3231_SetFullDate(uint8_t date, uint8_t month, uint8_t dow, uint16_t year){
DS3231_SetDate(date);
DS3231_SetMonth(month);
DS3231_SetDayOfWeek(dow);
DS3231_SetYear(year);
}
/**
* @brief Decodes the raw binary value stored in registers to decimal format.
* @param bin Binary-coded decimal value retrieved from register, 0 to 255.
* @return Decoded decimal value.
*/
uint8_t DS3231_DecodeBCD(uint8_t bin) {
return (((bin & 0xf0) >> 4) * 10) + (bin & 0x0f);
}
/**
* @brief Encodes a decimal number to binaty-coded decimal for storage in registers.
* @param dec Decimal number to encode.
* @return Encoded binary-coded decimal value.
*/
uint8_t DS3231_EncodeBCD(uint8_t dec) {
return (dec % 10 + ((dec / 10) << 4));
}
/**
* @brief Enable the 32kHz output.
* @param enable Enable, DS3231_ENABLE or DS3231_DISABLE.
*/
void DS3231_Enable32kHzOutput(DS3231_State enable){
uint8_t status = DS3231_GetRegByte(DS3231_REG_STATUS) & 0xfb;
DS3231_SetRegByte(DS3231_REG_STATUS, status | (enable << DS3231_EN32KHZ));
}
/**
* @brief Get the integer part of the temperature.
* @return Integer part of the temperature, -127 to 127.
*/
int8_t DS3231_GetTemperatureInteger(){
return DS3231_GetRegByte(DS3231_TEMP_MSB);
}
/**
* @brief Get the fractional part of the temperature to 2 decimal places.
* @return Fractional part of the temperature, 0, 25, 50 or 75.
*/
uint8_t DS3231_GetTemperatureFraction(){
return (DS3231_GetRegByte(DS3231_TEMP_LSB) >> 6) * 25;
}
#ifdef __cplusplus
}
#endif