LibDriver DS3231
Loading...
Searching...
No Matches
driver_ds3231.c
Go to the documentation of this file.
1
37
38#include "driver_ds3231.h"
39
43#define CHIP_NAME "Maxim Integrated DS3231"
44#define MANUFACTURER_NAME "Maxim Integrated"
45#define SUPPLY_VOLTAGE_MIN 2.3f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 0.65f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
55#define DS3231_REG_SECOND 0x00
56#define DS3231_REG_MINUTE 0x01
57#define DS3231_REG_HOUR 0x02
58#define DS3231_REG_WEEK 0x03
59#define DS3231_REG_DATE 0x04
60#define DS3231_REG_MONTH 0x05
61#define DS3231_REG_YEAR 0x06
62#define DS3231_REG_ALARM1_SECOND 0x07
63#define DS3231_REG_ALARM1_MINUTE 0x08
64#define DS3231_REG_ALARM1_HOUR 0x09
65#define DS3231_REG_ALARM1_WEEK 0x0A
66#define DS3231_REG_ALARM2_MINUTE 0x0B
67#define DS3231_REG_ALARM2_HOUR 0x0C
68#define DS3231_REG_ALARM2_WEEK 0x0D
69#define DS3231_REG_CONTROL 0x0E
70#define DS3231_REG_STATUS 0x0F
71#define DS3231_REG_XTAL 0x10
72#define DS3231_REG_TEMPERATUREH 0x11
73#define DS3231_REG_TEMPERATUREL 0x12
74
78#define DS3231_ADDRESS 0xD0
79
90static uint8_t a_ds3231_iic_write(ds3231_handle_t *handle, uint8_t reg, uint8_t data)
91{
92 if (handle->iic_write(DS3231_ADDRESS, reg, &data, 1) != 0) /* write data */
93 {
94 return 1; /* return error */
95 }
96 else
97 {
98 return 0; /* success return 0 */
99 }
100}
101
113static uint8_t a_ds3231_iic_multiple_read(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
114{
115 if (handle->iic_read(DS3231_ADDRESS, reg, buf, len) != 0) /* read data */
116 {
117 return 1; /* return error */
118 }
119 else
120 { /* success return 0 */
121 return 0;
122 }
123}
124
131static uint8_t a_ds3231_hex2bcd(uint8_t val)
132{
133 uint8_t i, j, k;
134
135 i = val / 10; /* get tens place */
136 j = val % 10; /* get ones place */
137 k = j + (i << 4); /* set bcd */
138
139 return k; /* return bcd */
140}
141
148static uint8_t a_ds3231_bcd2hex(uint8_t val)
149{
150 uint8_t temp;
151
152 temp = val & 0x0F; /* get ones place */
153 val = (val >> 4) & 0x0F; /* get tens place */
154 val = val * 10; /* set tens place */
155 temp = temp + val; /* get hex */
156
157 return temp; /* return hex */
158}
159
173{
174 uint8_t res;
175 uint8_t reg;
176 uint8_t century;
177 uint16_t year;
178
179 if (handle == NULL) /* check handle */
180 {
181 return 2; /* return error */
182 }
183 if (handle->inited != 1) /* check handle initialization */
184 {
185 return 3; /* return error */
186 }
187 if (t == NULL) /* check time */
188 {
189 handle->debug_print("ds3231: time is null.\n"); /* time is null */
190
191 return 2; /* return error */
192 }
193 if (t->format == DS3231_FORMAT_12H) /* if 12H */
194 {
195 if ((t->year < 2000) || (t->year > 2200)) /* check year */
196 {
197 handle->debug_print("ds3231: year can't be over 2200 or less than 2000.\n"); /* year can't be over 2200 or less than 2000 */
198
199 return 4; /* return error */
200 }
201 if ((t->month == 0) || (t->month > 12)) /* check month */
202 {
203 handle->debug_print("ds3231: month can't be zero or over than 12.\n"); /* month can't be zero or over than 12 */
204
205 return 4; /* return error */
206 }
207 if ((t->week == 0) || (t->week > 7)) /* check week */
208 {
209 handle->debug_print("ds3231: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
210
211 return 4; /* return error */
212 }
213 if ((t->date == 0) || (t->date > 31)) /* check data */
214 {
215 handle->debug_print("ds3231: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
216
217 return 4; /* return error */
218 }
219 if ((t->hour < 1) || (t->hour > 12)) /* check hour */
220 {
221 handle->debug_print("ds3231: hour can't be over than 12 or less 1.\n"); /* hour can't be over than 12 or less 1 */
222
223 return 4; /* return error */
224 }
225 if (t->minute > 59) /* check minute */
226 {
227 handle->debug_print("ds3231: minute can't be over than 59.\n"); /* minute can't be over than 59 */
228
229 return 4; /* return error */
230 }
231 if (t->second > 59) /* check second */
232 {
233 handle->debug_print("ds3231: second can't be over than 59.\n"); /* second can't be over than 59 */
234
235 return 4; /* return error */
236 }
237 }
238 else if (t->format == DS3231_FORMAT_24H) /* if 24H */
239 {
240 if ((t->year < 2000) || (t->year > 2200)) /* check year */
241 {
242 handle->debug_print("ds3231: year can't be over 2200 or less than 2000.\n"); /* year can't be over 2200 or less than 2000 */
243
244 return 4; /* return error */
245 }
246 if ((t->month == 0) || (t->month > 12)) /* check month */
247 {
248 handle->debug_print("ds3231: month can't be zero or over than 12.\n"); /* month can't be zero or over than 12 */
249
250 return 4; /* return error */
251 }
252 if ((t->week == 0) || (t->week > 7)) /* check week */
253 {
254 handle->debug_print("ds3231: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
255
256 return 4; /* return error */
257 }
258 if ((t->date == 0) || (t->date > 31)) /* check data */
259 {
260 handle->debug_print("ds3231: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
261
262 return 4; /* return error */
263 }
264 if (t->hour > 23) /* check hour */
265 {
266 handle->debug_print("ds3231: hour can't be over than 23.\n"); /* hour can't be over than 23 */
267
268 return 4; /* return error */
269 }
270 if (t->minute > 59) /* check minute */
271 {
272 handle->debug_print("ds3231: minute can't be over than 59.\n"); /* minute can't be over than 59 */
273
274 return 4; /* return error */
275 }
276 if (t->second > 59) /* check second */
277 {
278 handle->debug_print("ds3231: second can't be over than 59.\n"); /* second can't be over than 59 */
279
280 return 4; /* return error */
281 }
282 }
283 else
284 {
285 handle->debug_print("ds3231: format is invalid.\n"); /* format is invalid */
286
287 return 4; /* return error */
288 }
289
290 res = a_ds3231_iic_write(handle, DS3231_REG_SECOND, a_ds3231_hex2bcd(t->second)); /* write second */
291 if (res != 0) /* check result */
292 {
293 handle->debug_print("ds3231: write second failed.\n"); /* write second failed */
294
295 return 1; /* return error */
296 }
297 res = a_ds3231_iic_write(handle, DS3231_REG_MINUTE, a_ds3231_hex2bcd(t->minute)); /* write minute */
298 if (res != 0) /* check result */
299 {
300 handle->debug_print("ds3231: write minute failed.\n"); /* write minute failed */
301
302 return 1; /* return error */
303 }
304 if (t->format == DS3231_FORMAT_12H) /* if 12H */
305 {
306 reg = (uint8_t)((1 << 6) | (t->am_pm << 5) | a_ds3231_hex2bcd(t->hour)); /* set hour in 12H */
307 }
308 else /* if 24H */
309 {
310 reg = (0 << 6) | a_ds3231_hex2bcd(t->hour); /* set hour in 24H */
311 }
312 res = a_ds3231_iic_write(handle, DS3231_REG_HOUR, reg); /* write hour */
313 if (res != 0) /* check result */
314 {
315 handle->debug_print("ds3231: write hour failed.\n"); /* write hour failed */
316
317 return 1; /* return error */
318 }
319 res = a_ds3231_iic_write(handle, DS3231_REG_WEEK, a_ds3231_hex2bcd(t->week)); /* write week */
320 if (res != 0) /* check result */
321 {
322 handle->debug_print("ds3231: write week failed.\n"); /* write week failed */
323
324 return 1; /* return error */
325 }
326 res = a_ds3231_iic_write(handle, DS3231_REG_DATE, a_ds3231_hex2bcd(t->date)); /* write data */
327 if (res != 0) /* check result */
328 {
329 handle->debug_print("ds3231: write date failed.\n"); /* write date failed */
330
331 return 1; /* return error */
332 }
333 year = t->year - 2000; /* year - 2000 */
334 if (year >= 100) /* check year */
335 {
336 century = 1; /* set century */
337 year -= 100; /* year -= 100 */
338 }
339 else
340 {
341 century = 0; /* set century 0 */
342 }
343 res = a_ds3231_iic_write(handle, DS3231_REG_MONTH, a_ds3231_hex2bcd(t->month) | (century << 7)); /* write month and century */
344 if (res != 0) /* check result */
345 {
346 handle->debug_print("ds3231: write century and month failed.\n"); /* write century and month failed */
347
348 return 1; /* return error */
349 }
350 res = a_ds3231_iic_write(handle, DS3231_REG_YEAR, a_ds3231_hex2bcd((uint8_t)year)); /* write year */
351 if (res != 0) /* check result */
352 {
353 handle->debug_print("ds3231: write year failed.\n"); /* write year failed */
354
355 return 1; /* return error */
356 }
357
358 return 0; /* success return 0 */
359}
360
373{
374 uint8_t res;
375 uint8_t buf[7];
376
377 if (handle == NULL) /* check handle */
378 {
379 return 2; /* return error */
380 }
381 if (handle->inited != 1) /* check handle initialization */
382 {
383 return 3; /* return error */
384 }
385 if (t == NULL) /* check time */
386 {
387 handle->debug_print("ds3231: time is null.\n"); /* time is null */
388
389 return 2; /* return error */
390 }
391
392 memset(buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
393 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_SECOND, (uint8_t *)buf, 7); /* multiple_read */
394 if (res != 0) /* check result */
395 {
396 handle->debug_print("ds3231: multiple read failed.\n"); /* multiple read failed */
397
398 return 1; /* return error */
399 }
400 t->year = a_ds3231_bcd2hex(buf[6]) + 2000 + ((buf[5] >> 7) & 0x01) * 100; /* get year */
401 t->month = a_ds3231_bcd2hex(buf[5]&0x1F); /* get month */
402 t->week = a_ds3231_bcd2hex(buf[3]); /* get week */
403 t->date = a_ds3231_bcd2hex(buf[4]); /* get date */
404 t->am_pm = (ds3231_am_pm_t)((buf[2] >> 5) & 0x01); /* get am pm */
405 t->format = (ds3231_format_t)((buf[2] >> 6) & 0x01); /* get format */
406 if (t->format == DS3231_FORMAT_12H) /* if 12H */
407 {
408 t->hour = a_ds3231_bcd2hex(buf[2] & 0x1F); /* get hour */
409 }
410 else
411 {
412 t->hour = a_ds3231_bcd2hex(buf[2] & 0x3F); /* get hour */
413 }
414 t->minute = a_ds3231_bcd2hex(buf[1]); /* get minute */
415 t->second = a_ds3231_bcd2hex(buf[0]); /* get second */
416
417 return 0; /* success return 0 */
418}
419
433{
434 uint8_t res;
435 uint8_t reg;
436
437 if (handle == NULL) /* check handle */
438 {
439 return 2; /* return error */
440 }
441 if (handle->inited != 1) /* check handle initialization */
442 {
443 return 3; /* return error */
444 }
445 if (t == NULL) /* check time */
446 {
447 handle->debug_print("ds3231: time is null.\n"); /* time is null */
448
449 return 2; /* return error */
450 }
451 if (t->format == DS3231_FORMAT_12H) /* if 12H */
452 {
453 if ((t->week == 0) || (t->week > 7)) /* check week */
454 {
455 handle->debug_print("ds3231: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
456
457 return 1; /* return error */
458 }
459 if ((t->date == 0) || (t->date > 31)) /* check data */
460 {
461 handle->debug_print("ds3231: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
462
463 return 1; /* return error */
464 }
465 if ((t->hour < 1) || (t->hour > 12)) /* check hour */
466 {
467 handle->debug_print("ds3231: hour can't be over than 12 or less 1.\n"); /* hour can't be over than 12 or less 1 */
468
469 return 1; /* return error */
470 }
471 if (t->minute > 59) /* check minute */
472 {
473 handle->debug_print("ds3231: minute can't be over than 59.\n"); /* minute can't be over than 59 */
474
475 return 1; /* return error */
476 }
477 if (t->second > 59) /* check second */
478 {
479 handle->debug_print("ds3231: second can't be over than 59.\n"); /* second can't be over than 59 */
480
481 return 1; /* return error */
482 }
483 }
484 else if (t->format == DS3231_FORMAT_24H) /* if 24H */
485 {
486 if ((t->week == 0) || (t->week > 7)) /* check week */
487 {
488 handle->debug_print("ds3231: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
489
490 return 1; /* return error */
491 }
492 if ((t->date == 0) || (t->date > 31)) /* check data */
493 {
494 handle->debug_print("ds3231: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
495
496 return 1; /* return error */
497 }
498 if (t->hour > 23) /* check hour */
499 {
500 handle->debug_print("ds3231: hour can't be over than 23.\n"); /* hour can't be over than 23 */
501
502 return 1; /* return error */
503 }
504 if (t->minute > 59) /* check minute */
505 {
506 handle->debug_print("ds3231: minute can't be over than 59.\n"); /* minute can't be over than 59 */
507
508 return 1; /* return error */
509 }
510 if (t->second > 59) /* check second */
511 {
512 handle->debug_print("ds3231: second can't be over than 59.\n"); /* second can't be over than 59 */
513
514 return 1; /* return error */
515 }
516 }
517 else
518 {
519 handle->debug_print("ds3231: format is invalid.\n"); /* format is invalid */
520
521 return 1; /* return error */
522 }
523
524 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM1_SECOND, a_ds3231_hex2bcd(t->second) | ((mode & 0x01) << 7)); /* write second */
525 if (res != 0) /* check result */
526 {
527 handle->debug_print("ds3231: write alarm1 second failed.\n"); /* write alarm1 second failed */
528
529 return 1; /* return error */
530 }
531 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM1_MINUTE, a_ds3231_hex2bcd(t->minute) | (((mode >> 1) & 0x01) << 7)); /* write minute */
532 if (res != 0) /* check result */
533 {
534 handle->debug_print("ds3231: write alarm1 minute failed.\n"); /* write alarm1 minute failed */
535
536 return 1; /* return error */
537 }
538 if (t->format == DS3231_FORMAT_12H) /* if 12H */
539 {
540 reg = (uint8_t)((((mode >> 2) & 0x01) << 7) | (1 << 6) | (t->am_pm << 5) | a_ds3231_hex2bcd(t->hour)); /* set hour in 12H */
541 }
542 else /* if 24H */
543 {
544 reg = (((mode >> 2) & 0x01) << 7) | a_ds3231_hex2bcd(t->hour); /* set hour in 24H */
545 }
546 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM1_HOUR, reg); /* write hour */
547 if (res != 0) /* check result */
548 {
549 handle->debug_print("ds3231: write alarm1 hour failed.\n"); /* write alarm1 hour failed */
550
551 return 1; /* return error */
552 }
553 if (mode >= DS3231_ALARM1_MODE_WEEK_HOUR_MINUTE_SECOND_MATCH) /* if week */
554 {
555 reg = (((mode >> 3) & 0x01) << 7) | (1 << 6) | a_ds3231_hex2bcd(t->week); /* set data in week */
556 }
557 else /* if day */
558 {
559 reg = (((mode >> 3) & 0x01) << 7) | a_ds3231_hex2bcd(t->date); /* set data in date */
560 }
561 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM1_WEEK, reg); /* write week */
562 if (res != 0) /* check result */
563 {
564 handle->debug_print("ds3231: write alarm1 week failed.\n"); /* write alarm1 week failed */
565
566 return 1; /* return error */
567 }
568
569 return 0; /* success return 0 */
570}
571
585{
586 uint8_t res;
587 uint8_t buf[4];
588
589 if (handle == NULL) /* check handle */
590 {
591 return 2; /* return error */
592 }
593 if (handle->inited != 1) /* check handle initialization */
594 {
595 return 3; /* return error */
596 }
597 if (t == NULL) /* check time */
598 {
599 handle->debug_print("ds3231: time is null.\n"); /* time is null */
600
601 return 2; /* return error */
602 }
603
604 memset(buf, 0, sizeof(uint8_t) * 4); /* clear the buffer */
605 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_ALARM1_SECOND, (uint8_t *)buf, 4); /* multiple_read */
606 if (res != 0) /* check result */
607 {
608 handle->debug_print("ds3231: multiple read failed.\n"); /* multiple read failed */
609
610 return 1; /* return error */
611 }
612 t->year = 0; /* get year */
613 t->month = 0; /* get month */
614 if (((buf[3] >> 6) & 0x01) != 0) /* if week */
615 {
616 t->week = a_ds3231_bcd2hex(buf[3] & 0x0F); /* get week */
617 t->date = 0; /* get data */
618 }
619 else /* if data */
620 {
621 t->week = 0; /* get week */
622 t->date = a_ds3231_bcd2hex(buf[3] & 0x3F); /* get data */
623 }
624 t->am_pm = (ds3231_am_pm_t)((buf[2] >> 5) & 0x01); /* get am pm */
625 t->format = (ds3231_format_t)((buf[2] >> 6) & 0x01); /* get format */
626 if (t->format == DS3231_FORMAT_12H) /* if 12H */
627 {
628 t->hour = a_ds3231_bcd2hex(buf[2]&0x1F); /* get hour */
629 }
630 else /* if 24H */
631 {
632 t->hour = a_ds3231_bcd2hex(buf[2]&0x3F); /* get hour */
633 }
634 t->minute = a_ds3231_bcd2hex(buf[1] & 0x7F); /* get minute */
635 t->second = a_ds3231_bcd2hex(buf[0] & 0x7F); /* get second */
636 *mode = (ds3231_alarm1_mode_t)(((buf[0]>>7)&0x01)<<0 | ((buf[1]>>7)&0x01)<<1 | ((buf[2]>>7)&0x01)<<2 | ((buf[3]>>7)&0x01)<<3 |
637 ((buf[3] >> 6)&0x01)<<4
638 ); /* get mode */
639
640 return 0; /* success return 0 */
641}
642
656{
657 uint8_t res;
658 uint8_t reg;
659
660 if (handle == NULL) /* check handle */
661 {
662 return 2; /* return error */
663 }
664 if (handle->inited != 1) /* check handle initialization */
665 {
666 return 3; /* return error */
667 }
668 if (t == NULL) /* check time */
669 {
670 handle->debug_print("ds3231: time is null.\n"); /* time is null */
671
672 return 2; /* return error */
673 }
674 if (t->format == DS3231_FORMAT_12H) /* if 12H */
675 {
676 if ((t->week == 0) || (t->week > 7)) /* check week */
677 {
678 handle->debug_print("ds3231: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
679
680 return 1; /* return error */
681 }
682 if ((t->date == 0) || (t->date > 31)) /* check data */
683 {
684 handle->debug_print("ds3231: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
685
686 return 1; /* return error */
687 }
688 if ((t->hour < 1) || (t->hour > 12)) /* check hour */
689 {
690 handle->debug_print("ds3231: hour can't be over than 12 or less 1.\n"); /* hour can't be over than 12 or less 1 */
691
692 return 1; /* return error */
693 }
694 if (t->minute > 59) /* check minute */
695 {
696 handle->debug_print("ds3231: minute can't be over than 59.\n"); /* minute can't be over than 59 */
697
698 return 1; /* return error */
699 }
700 }
701 else if (t->format == DS3231_FORMAT_24H) /* if 24H */
702 {
703 if ((t->week == 0) || (t->week > 7)) /* check week */
704 {
705 handle->debug_print("ds3231: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
706
707 return 1; /* return error */
708 }
709 if ((t->date == 0) || (t->date > 31)) /* check data */
710 {
711 handle->debug_print("ds3231: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
712
713 return 1; /* return error */
714 }
715 if (t->hour > 23) /* check hour */
716 {
717 handle->debug_print("ds3231: hour can't be over than 23.\n"); /* hour can't be over than 23 */
718
719 return 1; /* return error */
720 }
721 if (t->minute > 59) /* check minute */
722 {
723 handle->debug_print("ds3231: minute can't be over than 59.\n"); /* minute can't be over than 59 */
724
725 return 1; /* return error */
726 }
727 }
728 else
729 {
730 handle->debug_print("ds3231: format is invalid.\n"); /* format is invalid */
731
732 return 1; /* return error */
733 }
734
735 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM2_MINUTE, a_ds3231_hex2bcd(t->minute) | (((mode >> 0) & 0x01) << 7)); /* write minute */
736 if (res != 0) /* check result */
737 {
738 handle->debug_print("ds3231: write alarm2 minute failed.\n"); /* write alarm2 minute failed */
739
740 return 1; /* return error */
741 }
742 if (t->format == DS3231_FORMAT_12H) /* if 12H */
743 {
744 reg = (uint8_t)((((mode >> 1) & 0x01) << 7) | (1 << 6) | (t->am_pm << 5) | a_ds3231_hex2bcd(t->hour)); /* set hour in 12H */
745 }
746 else /* if 24H */
747 {
748 reg = (((mode >> 1) & 0x01) << 7) | a_ds3231_hex2bcd(t->hour); /* set hour in 24H */
749 }
750 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM2_HOUR, reg); /* write hour */
751 if (res != 0) /* check result */
752 {
753 handle->debug_print("ds3231: write alarm2 hour failed.\n"); /* write alarm2 hour failed */
754
755 return 1; /* return error */
756 }
757 if (mode >= (uint8_t)DS3231_ALARM1_MODE_WEEK_HOUR_MINUTE_SECOND_MATCH) /* if week */
758 {
759 reg = (((mode >> 2) & 0x01) << 7) | (1 << 6) | a_ds3231_hex2bcd(t->week); /* set data in week */
760 }
761 else /* if day */
762 {
763 reg = (((mode >> 2) & 0x01) << 7) | a_ds3231_hex2bcd(t->date); /* set data in date */
764 }
765 res = a_ds3231_iic_write(handle, DS3231_REG_ALARM2_WEEK, reg); /* write week */
766 if (res != 0) /* check result */
767 {
768 handle->debug_print("ds3231: write alarm2 week failed.\n"); /* write alarm2 week failed */
769
770 return 1; /* return error */
771 }
772
773 return 0; /* success return 0 */
774}
775
789{
790 uint8_t res;
791 uint8_t buf[3];
792
793 if (handle == NULL) /* check handle */
794 {
795 return 2; /* return error */
796 }
797 if (handle->inited != 1) /* check handle initialization */
798 {
799 return 3; /* return error */
800 }
801 if (t == NULL) /* check time */
802 {
803 handle->debug_print("ds3231: time is null.\n"); /* time is null */
804
805 return 2; /* return error */
806 }
807
808 memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
809 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_ALARM2_MINUTE, (uint8_t *)buf, 3); /* multiple read */
810 if (res != 0) /* check result */
811 {
812 handle->debug_print("ds3231: multiple read failed.\n"); /* multiple read failed */
813
814 return 1; /* return error */
815 }
816 t->year = 0; /* get year */
817 t->month = 0; /* get month */
818 if (((buf[2] >> 6) & 0x01) != 0) /* if week */
819 {
820 t->week = a_ds3231_bcd2hex(buf[2] & 0x0F); /* get week */
821 t->date = 0; /* get data */
822 }
823 else /* if data */
824 {
825 t->week = 0; /* get week */
826 t->date = a_ds3231_bcd2hex(buf[2] & 0x3F); /* get data */
827 }
828 t->am_pm = (ds3231_am_pm_t)((buf[1] >> 5) & 0x01); /* get am pm */
829 t->format = (ds3231_format_t)((buf[1] >> 6) & 0x01); /* get format */
830 if (t->format == DS3231_FORMAT_12H) /* if 12H */
831 {
832 t->hour = a_ds3231_bcd2hex(buf[1]&0x1F); /* get hour */
833 }
834 else /* if 24H */
835 {
836 t->hour = a_ds3231_bcd2hex(buf[1]&0x3F); /* get hour */
837 }
838 t->minute = a_ds3231_bcd2hex(buf[0] & 0x7F); /* get minute */
839 t->second = 0; /* get second */
840 *mode = (ds3231_alarm2_mode_t)(((buf[0]>>7)&0x01)<<0 | ((buf[1]>>7)&0x01)<<1 | ((buf[2]>>7)&0x01)<<2 | ((buf[2]>>6)&0x01)<<4); /* get mode */
841
842 return 0; /* success return 0 */
843}
844
857{
858 uint8_t res;
859 uint8_t prev;
860
861 if (handle == NULL) /* check handle */
862 {
863 return 2; /* return error */
864 }
865 if (handle->inited != 1) /* check handle initialization */
866 {
867 return 3; /* return error */
868 }
869
870 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple read */
871 if (res != 0) /* check result */
872 {
873 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
874
875 return 1; /* return error */
876 }
877 prev &= ~ (1 << 7); /* clear config */
878 prev |= (!enable) << 7; /* set enable */
879 res = a_ds3231_iic_write(handle, DS3231_REG_CONTROL, prev); /* write control */
880 if (res != 0) /* check result */
881 {
882 handle->debug_print("ds3231: write control failed.\n"); /* write control failed */
883
884 return 1; /* return error */
885 }
886
887 return 0; /* success return 0 */
888}
889
902{
903 uint8_t res;
904 uint8_t prev;
905
906 if (handle == NULL) /* check handle */
907 {
908 return 2; /* return error */
909 }
910 if (handle->inited != 1) /* check handle initialization */
911 {
912 return 3; /* return error */
913 }
914
915 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple read */
916 if (res != 0) /* check result */
917 {
918 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
919
920 return 1; /* return error */
921 }
922 *enable = (ds3231_bool_t)(!((prev >> 7) & 0x01)); /* get enable */
923
924 return 0; /* success return 0 */
925}
926
940{
941 uint8_t res;
942 uint8_t prev;
943
944 if (handle == NULL) /* check handle */
945 {
946 return 2; /* return error */
947 }
948 if (handle->inited != 1) /* check handle initialization */
949 {
950 return 3; /* return error */
951 }
952
953 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple_read */
954 if (res != 0) /* check result */
955 {
956 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
957
958 return 1; /* return error */
959 }
960 prev &= ~(1 << alarm); /* clear config */
961 prev |= enable << alarm; /* set enable */
962 res = a_ds3231_iic_write(handle, DS3231_REG_CONTROL, prev); /* write control */
963 if (res != 0) /* check result */
964 {
965 handle->debug_print("ds3231: write control failed.\n"); /* write control failed */
966
967 return 1; /* return error */
968 }
969
970 return 0; /* success return 0 */
971}
972
986{
987 uint8_t res;
988 uint8_t prev;
989
990 if (handle == NULL) /* check handle */
991 {
992 return 2; /* return error */
993 }
994 if (handle->inited != 1) /* check handle initialization */
995 {
996 return 3; /* return error */
997 }
998
999 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple_read */
1000 if (res != 0) /* check result */
1001 {
1002 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
1003
1004 return 1; /* return error */
1005 }
1006 *enable = (ds3231_bool_t)((prev >> alarm) & 0x01); /* get enable */
1007
1008 return 0; /* success return 0 */
1009}
1010
1023{
1024 uint8_t res;
1025 uint8_t prev;
1026
1027 if (handle == NULL) /* check handle */
1028 {
1029 return 2; /* return error */
1030 }
1031 if (handle->inited != 1) /* check handle initialization */
1032 {
1033 return 3; /* return error */
1034 }
1035
1036 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple_read */
1037 if (res != 0) /* check result */
1038 {
1039 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
1040
1041 return 1; /* return error */
1042 }
1043 prev &= ~(1 << 2); /* clear config */
1044 prev |= pin << 2; /* set pin */
1045 res = a_ds3231_iic_write(handle, DS3231_REG_CONTROL, prev); /* write control */
1046 if (res != 0) /* check result */
1047 {
1048 handle->debug_print("ds3231: write control failed.\n"); /* write control failed */
1049
1050 return 1; /* return error */
1051 }
1052
1053 return 0; /* success return 0 */
1054}
1055
1068{
1069 uint8_t res;
1070 uint8_t prev;
1071
1072 if (handle == NULL) /* check handle */
1073 {
1074 return 2; /* return error */
1075 }
1076 if (handle->inited != 1) /* check handle initialization */
1077 {
1078 return 3; /* return error */
1079 }
1080
1081 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple_read */
1082 if (res != 0) /* check result */
1083 {
1084 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
1085
1086 return 1; /* return error */
1087 }
1088 *pin = (ds3231_pin_t)((prev >> 2) & 0x01); /* get pin */
1089
1090 return 0; /* success return 0 */
1091}
1092
1105{
1106 uint8_t res;
1107 uint8_t prev;
1108
1109 if (handle == NULL) /* check handle */
1110 {
1111 return 2; /* return error */
1112 }
1113 if (handle->inited != 1) /* check handle initialization */
1114 {
1115 return 3; /* return error */
1116 }
1117
1118 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple_read */
1119 if (res != 0) /* check result */
1120 {
1121 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
1122
1123 return 1; /* return error */
1124 }
1125 prev &= ~(1 << 6); /* clear config */
1126 prev |= enable << 6; /* set enable */
1127 res = a_ds3231_iic_write(handle, DS3231_REG_CONTROL, prev); /* write control */
1128 if (res != 0) /* check result */
1129 {
1130 handle->debug_print("ds3231: write control failed.\n"); /* write control failed */
1131
1132 return 1; /* return error */
1133 }
1134
1135 return 0; /* success return 0 */
1136}
1137
1150{
1151 uint8_t res;
1152 uint8_t prev;
1153
1154 if (handle == NULL) /* check handle */
1155 {
1156 return 2; /* return error */
1157 }
1158 if (handle->inited != 1) /* check handle initialization */
1159 {
1160 return 3; /* return error */
1161 }
1162
1163 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple_read */
1164 if (res != 0) /* check result */
1165 {
1166 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
1167
1168 return 1; /* return error */
1169 }
1170 *enable = (ds3231_bool_t)((prev >> 6) & 0x01); /* get enable */
1171
1172 return 0; /* success return 0 */
1173}
1174
1187uint8_t ds3231_get_temperature(ds3231_handle_t *handle, int16_t *raw, float *s)
1188{
1189 uint8_t res;
1190 uint8_t prev;
1191 uint32_t times;
1192 uint8_t buf[2];
1193
1194 if (handle == NULL) /* check handle */
1195 {
1196 return 2; /* return error */
1197 }
1198 if (handle->inited != 1) /* check handle initialization */
1199 {
1200 return 3; /* return error */
1201 }
1202
1203 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
1204 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_CONTROL, (uint8_t *)&prev, 1); /* multiple read */
1205 if (res != 0) /* check result */
1206 {
1207 handle->debug_print("ds3231: read control failed.\n"); /* read control failed */
1208
1209 return 1; /* return error */
1210 }
1211 prev &= ~(1 << 5); /* clear config */
1212 prev |= 1 << 5; /* set enable */
1213 res = a_ds3231_iic_write(handle, DS3231_REG_CONTROL, prev); /* write control */
1214 if (res != 0) /* check result */
1215 {
1216 handle->debug_print("ds3231: write control failed.\n"); /* write control failed */
1217
1218 return 1; /* return error */
1219 }
1220 times = 500; /* set 5s */
1221 while (times != 0) /* check times */
1222 {
1223 handle->delay_ms(10); /* delay 10 ms */
1224 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)&prev, 1); /* read status */
1225 if (res != 0) /* check result */
1226 {
1227 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1228
1229 return 1; /* return error */
1230 }
1231 if (((prev >> 2) & 0x01) == 0) /* check result */
1232 {
1233 break; /* break */
1234 }
1235 times--; /* times-- */
1236 }
1237 if (times == 0) /* if zero */
1238 {
1239 handle->debug_print("ds3231: read timeout.\n"); /* read timeout */
1240
1241 return 1; /* return error */
1242 }
1243 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_TEMPERATUREH, (uint8_t *)buf, 2); /* read temperature */
1244 if (res != 0) /* check result */
1245 {
1246 handle->debug_print("ds3231: read temperature failed.\n"); /* read temperature failed */
1247
1248 return 1; /* return error */
1249 }
1250 *raw = (int16_t)(((uint16_t)buf[0]) << 8) | buf[1]; /* set raw temperature */
1251 *raw = (*raw) >> 6; /* right shift */
1252 if (((*raw) & 0x0200) != 0) /* set negative value */
1253 {
1254 *raw = (*raw) | 0xFC00U; /* set negative part */
1255 }
1256 *s = (float)(*raw) * 0.25f; /* set converted temperature */
1257
1258 return 0; /* success return 0 */
1259}
1260
1272uint8_t ds3231_get_status(ds3231_handle_t *handle, uint8_t *status)
1273{
1274 uint8_t res;
1275
1276 if (handle == NULL) /* check handle */
1277 {
1278 return 2; /* return error */
1279 }
1280 if (handle->inited != 1) /* check handle initialization */
1281 {
1282 return 3; /* return error */
1283 }
1284
1285 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)status, 1); /* multiple read */
1286 if (res != 0) /* check result */
1287 {
1288 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1289
1290 return 1; /* return error */
1291 }
1292
1293 return 0; /* success return 0 */
1294}
1295
1307uint8_t ds3231_set_aging_offset(ds3231_handle_t *handle, int8_t offset)
1308{
1309 uint8_t res;
1310
1311 if (handle == NULL) /* check handle */
1312 {
1313 return 2; /* return error */
1314 }
1315 if (handle->inited != 1) /* check handle initialization */
1316 {
1317 return 3; /* return error */
1318 }
1319
1320 res = a_ds3231_iic_write(handle, DS3231_REG_XTAL, offset); /* write offset */
1321 if (res != 0) /* check result */
1322 {
1323 handle->debug_print("ds3231: write offset failed.\n"); /* write offset failed */
1324
1325 return 1; /* return error */
1326 }
1327
1328 return 0; /* success return 0 */
1329}
1330
1342uint8_t ds3231_get_aging_offset(ds3231_handle_t *handle, int8_t *offset)
1343{
1344 uint8_t res;
1345
1346 if (handle == NULL) /* check handle */
1347 {
1348 return 2; /* return error */
1349 }
1350 if (handle->inited != 1) /* check handle initialization */
1351 {
1352 return 3; /* return error */
1353 }
1354
1355 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_XTAL, (uint8_t *)offset, 1); /* read offset */
1356 if (res != 0) /* check result */
1357 {
1358 handle->debug_print("ds3231: read offset failed.\n"); /* read offset failed */
1359
1360 return 1; /* return error */
1361 }
1362
1363 return 0; /* success return 0 */
1364}
1365
1377uint8_t ds3231_aging_offset_convert_to_register(ds3231_handle_t *handle, float offset, int8_t *reg)
1378{
1379 if (handle == NULL) /* check handle */
1380 {
1381 return 2; /* return error */
1382 }
1383 if (handle->inited != 1) /* check handle initialization */
1384 {
1385 return 3; /* return error */
1386 }
1387
1388 *reg = (int8_t)(offset / 0.12f); /* convert real data to register data */
1389
1390 return 0; /* success return 0 */
1391}
1392
1404uint8_t ds3231_aging_offset_convert_to_data(ds3231_handle_t *handle, int8_t reg, float *offset)
1405{
1406 if (handle == NULL) /* check handle */
1407 {
1408 return 2; /* return error */
1409 }
1410 if (handle->inited != 1) /* check handle initialization */
1411 {
1412 return 3; /* return error */
1413 }
1414
1415 *offset = (float)(reg) * 0.12f; /* convert raw data to real data */
1416
1417 return 0; /* success return 0 */
1418}
1419
1431{
1432 uint8_t res, prev;
1433
1434 if (handle == NULL) /* check handle */
1435 {
1436 return 2; /* return error */
1437 }
1438 if (handle->inited != 1) /* check handle initialization */
1439 {
1440 return 3; /* return error */
1441 }
1442
1443 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)&prev, 1); /* multiple read */
1444 if (res != 0) /* check result */
1445 {
1446 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1447
1448 return 1; /* return error */
1449 } /* if oscillator stop */
1450 if ((prev & DS3231_STATUS_ALARM_2) != 0) /* if alarm 2 */
1451 {
1452 if (handle->receive_callback != NULL) /* if receive callback */
1453 {
1454 handle->receive_callback(DS3231_STATUS_ALARM_2); /* run callback */
1455 }
1456 }
1457 if ((prev & DS3231_STATUS_ALARM_1) != 0) /* if alarm 1 */
1458 {
1459 if (handle->receive_callback != NULL) /* if receive callback */
1460 {
1461 handle->receive_callback(DS3231_STATUS_ALARM_1); /* run callback */
1462 }
1463 }
1464
1465 return 0; /* success return 0 */
1466}
1467
1479{
1480 uint8_t res;
1481 uint8_t prev;
1482
1483 if (handle == NULL) /* check handle */
1484 {
1485 return 2; /* return error */
1486 }
1487 if (handle->debug_print == NULL) /* check debug_print */
1488 {
1489 return 3; /* return error */
1490 }
1491 if (handle->iic_init == NULL) /* check iic_init */
1492 {
1493 handle->debug_print("ds3231: iic_init is null.\n"); /* iic_init is null */
1494
1495 return 3; /* return error */
1496 }
1497 if (handle->iic_deinit == NULL) /* check iic_deinit */
1498 {
1499 handle->debug_print("ds3231: iic_deinit is null.\n"); /* iic_deinit is null */
1500
1501 return 3; /* return error */
1502 }
1503 if (handle->iic_write == NULL) /* check iic_write */
1504 {
1505 handle->debug_print("ds3231: iic_write is null.\n"); /* iic_write is null */
1506
1507 return 3; /* return error */
1508 }
1509 if (handle->iic_read == NULL) /* check iic_read */
1510 {
1511 handle->debug_print("ds3231: iic_read is null.\n"); /* iic_read is null */
1512
1513 return 3; /* return error */
1514 }
1515 if (handle->delay_ms == NULL) /* check delay_ms */
1516 {
1517 handle->debug_print("ds3231: delay_ms is null.\n"); /* delay_ms is null */
1518
1519 return 3; /* return error */
1520 }
1521 if (handle->receive_callback == NULL) /* check receive_callback */
1522 {
1523 handle->debug_print("ds3231: receive_callback is null.\n"); /* receive_callback is null */
1524
1525 return 3; /* return error */
1526 }
1527
1528 if (handle->iic_init() != 0) /* iic init */
1529 {
1530 handle->debug_print("ds3231: iic init failed.\n"); /* iic init failed */
1531
1532 return 1; /* return error */
1533 }
1534 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)&prev, 1); /* multiple_read */
1535 if (res != 0) /* check result */
1536 {
1537 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1538 (void)handle->iic_deinit(); /* iic deinit */
1539
1540 return 1; /* return error */
1541 }
1542 prev &= ~(1 << 7); /* clear config */
1543 res = a_ds3231_iic_write(handle, DS3231_REG_STATUS, prev); /* write status */
1544 if (res != 0) /* check result */
1545 {
1546 handle->debug_print("ds3231: write status failed.\n"); /* write status failed */
1547 (void)handle->iic_deinit(); /* iic deinit */
1548
1549 return 1; /* return error */
1550 }
1551 handle->inited = 1; /* flag finish initialization */
1552
1553 return 0; /* success return 0 */
1554}
1555
1567{
1568 if (handle == NULL) /* check handle */
1569 {
1570 return 2; /* return error */
1571 }
1572 if (handle->inited != 1) /* check handle initialization */
1573 {
1574 return 3; /* return error */
1575 }
1576
1577 if (handle->iic_deinit() != 0) /* iic deinit */
1578 {
1579 handle->debug_print("ds3231: iic deinit failed.\n"); /* iic deinit failed */
1580
1581 return 1; /* return error */
1582 }
1583 handle->inited = 0; /* flag close */
1584
1585 return 0; /* success return 0 */
1586}
1587
1600{
1601 uint8_t res;
1602 uint8_t prev;
1603
1604 if (handle == NULL) /* check handle */
1605 {
1606 return 2; /* return error */
1607 }
1608 if (handle->inited != 1) /* check handle initialization */
1609 {
1610 return 3; /* return error */
1611 }
1612
1613 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)&prev, 1); /* multiple_read */
1614 if (res != 0) /* check result */
1615 {
1616 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1617
1618 return 1; /* return error */
1619 }
1620 prev &= ~(1 << alarm); /* clear config */
1621 res = a_ds3231_iic_write(handle, DS3231_REG_STATUS, prev); /* write status */
1622 if (res != 0) /* check result */
1623 {
1624 handle->debug_print("ds3231: write status failed.\n"); /* write status failed */
1625
1626 return 1; /* return error */
1627 }
1628
1629 return 0; /* success return 0 */
1630}
1631
1644{
1645 uint8_t res;
1646 uint8_t prev;
1647
1648 if (handle == NULL) /* check handle */
1649 {
1650 return 2; /* return error */
1651 }
1652 if (handle->inited != 1) /* check handle initialization */
1653 {
1654 return 3; /* return error */
1655 }
1656
1657 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)&prev, 1); /* multiple read */
1658 if (res != 0) /* check result */
1659 {
1660 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1661
1662 return 1; /* return error */
1663 }
1664 prev &= ~(1 << 3); /* clear config */
1665 prev |= enable << 3; /* set enable */
1666 res = a_ds3231_iic_write(handle, DS3231_REG_STATUS, prev); /* write status */
1667 if (res != 0) /* check result */
1668 {
1669 handle->debug_print("ds3231: write status failed.\n"); /* write status failed */
1670
1671 return 1; /* return error */
1672 }
1673
1674 return 0; /* success return 0 */
1675}
1676
1689{
1690 uint8_t res;
1691 uint8_t prev;
1692
1693 if (handle == NULL) /* check handle */
1694 {
1695 return 2; /* return error */
1696 }
1697 if (handle->inited != 1) /* check handle initialization */
1698 {
1699 return 3; /* return error */
1700 }
1701
1702 res = a_ds3231_iic_multiple_read(handle, DS3231_REG_STATUS, (uint8_t *)&prev, 1); /* multiple read */
1703 if (res != 0) /* check result */
1704 {
1705 handle->debug_print("ds3231: read status failed.\n"); /* read status failed */
1706
1707 return 1; /* return error */
1708 }
1709 *enable = (ds3231_bool_t)((prev >> 3) & 0x01); /* get enable */
1710
1711 return 0; /* success return 0 */
1712}
1713
1727uint8_t ds3231_set_reg(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1728{
1729 if (handle == NULL) /* check handle */
1730 {
1731 return 2; /* return error */
1732 }
1733 if (handle->inited != 1) /* check handle initialization */
1734 {
1735 return 3; /* return error */
1736 }
1737
1738 if (handle->iic_write(DS3231_ADDRESS, reg, buf, len) != 0) /* write data */
1739 {
1740 return 1; /* return error */
1741 }
1742 else
1743 {
1744 return 0; /* success return 0 */
1745 }
1746}
1747
1761uint8_t ds3231_get_reg(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1762{
1763 if (handle == NULL) /* check handle */
1764 {
1765 return 2; /* return error */
1766 }
1767 if (handle->inited != 1) /* check handle initialization */
1768 {
1769 return 3; /* return error */
1770 }
1771
1772 if (handle->iic_read(DS3231_ADDRESS, reg, buf, len) != 0) /* read data */
1773 {
1774 return 1; /* return error */
1775 }
1776 else
1777 {
1778 return 0; /* success return 0 */
1779 }
1780}
1781
1791{
1792 if (info == NULL) /* check handle */
1793 {
1794 return 2; /* return error */
1795 }
1796
1797 memset(info, 0, sizeof(ds3231_info_t)); /* initialize ds3231 info structure */
1798 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1799 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1800 strncpy(info->interface, "IIC", 8); /* copy interface name */
1801 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1802 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1803 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1804 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1805 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1806 info->driver_version = DRIVER_VERSION; /* set driver version */
1807
1808 return 0; /* success return 0 */
1809}
#define DS3231_REG_MINUTE
#define DS3231_REG_YEAR
#define DS3231_REG_CONTROL
#define MAX_CURRENT
#define DS3231_REG_DATE
#define DS3231_REG_ALARM2_MINUTE
#define DS3231_REG_XTAL
#define DS3231_REG_ALARM2_WEEK
#define SUPPLY_VOLTAGE_MAX
#define DS3231_REG_HOUR
#define DS3231_ADDRESS
chip address definition
#define DS3231_REG_ALARM1_MINUTE
#define DS3231_REG_ALARM2_HOUR
#define TEMPERATURE_MAX
#define DS3231_REG_SECOND
chip register definition
#define MANUFACTURER_NAME
#define DS3231_REG_MONTH
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define DS3231_REG_ALARM1_HOUR
#define DS3231_REG_ALARM1_SECOND
#define DS3231_REG_TEMPERATUREH
#define DS3231_REG_ALARM1_WEEK
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define DS3231_REG_STATUS
#define DS3231_REG_WEEK
driver ds3231 header file
uint8_t ds3231_get_32khz_output(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the 32KHz output status
uint8_t ds3231_get_pin(ds3231_handle_t *handle, ds3231_pin_t *pin)
get the chip pin function
uint8_t ds3231_set_aging_offset(ds3231_handle_t *handle, int8_t offset)
set the chip aging offset
uint8_t ds3231_get_temperature(ds3231_handle_t *handle, int16_t *raw, float *s)
get the chip temperature
uint8_t ds3231_set_pin(ds3231_handle_t *handle, ds3231_pin_t pin)
set the chip pin function
uint8_t ds3231_aging_offset_convert_to_data(ds3231_handle_t *handle, int8_t reg, float *offset)
convert a register raw data to a converted aging offset data
uint8_t ds3231_get_aging_offset(ds3231_handle_t *handle, int8_t *offset)
get the chip aging offset
uint8_t ds3231_aging_offset_convert_to_register(ds3231_handle_t *handle, float offset, int8_t *reg)
convert a aging offset value to a register raw data
uint8_t ds3231_set_32khz_output(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the 32KHz output
uint8_t ds3231_set_square_wave(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the square wave output
uint8_t ds3231_get_square_wave(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the square wave output status
uint8_t ds3231_get_alarm_interrupt(ds3231_handle_t *handle, ds3231_alarm_t alarm, ds3231_bool_t *enable)
get the alarm interrupt status
uint8_t ds3231_set_alarm_interrupt(ds3231_handle_t *handle, ds3231_alarm_t alarm, ds3231_bool_t enable)
enable or disable the alarm interrupt
ds3231_alarm2_mode_t
ds3231 alarm2 enumeration definition
uint8_t ds3231_get_alarm1(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm1_mode_t *mode)
get the alarm1 time
ds3231_alarm1_mode_t
ds3231 alarm1 enumeration definition
uint8_t ds3231_set_alarm2(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm2_mode_t mode)
set the alarm2 time
uint8_t ds3231_alarm_clear(ds3231_handle_t *handle, ds3231_alarm_t alarm)
clear the alarm flag
uint8_t ds3231_set_alarm1(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm1_mode_t mode)
set the alarm1 time
uint8_t ds3231_get_alarm2(ds3231_handle_t *handle, ds3231_time_t *t, ds3231_alarm2_mode_t *mode)
get the alarm2 time
@ DS3231_ALARM1_MODE_WEEK_HOUR_MINUTE_SECOND_MATCH
ds3231_am_pm_t
ds3231 am pm enumeration definition
uint8_t ds3231_set_oscillator(ds3231_handle_t *handle, ds3231_bool_t enable)
enable or disable the oscillator
uint8_t ds3231_set_time(ds3231_handle_t *handle, ds3231_time_t *t)
set the current time
uint8_t ds3231_get_oscillator(ds3231_handle_t *handle, ds3231_bool_t *enable)
get the chip oscillator status
struct ds3231_time_s ds3231_time_t
ds3231 time structure definition
ds3231_alarm_t
ds3231 alarm enumeration definition
ds3231_format_t
ds3231 format enumeration definition
ds3231_pin_t
ds3231 pin enumeration definition
uint8_t ds3231_info(ds3231_info_t *info)
get chip's information
uint8_t ds3231_irq_handler(ds3231_handle_t *handle)
irq handler
struct ds3231_handle_s ds3231_handle_t
ds3231 handle structure definition
uint8_t ds3231_init(ds3231_handle_t *handle)
initialize the chip
struct ds3231_info_s ds3231_info_t
ds3231 information structure definition
uint8_t ds3231_get_time(ds3231_handle_t *handle, ds3231_time_t *t)
get the current time
uint8_t ds3231_deinit(ds3231_handle_t *handle)
close the chip
ds3231_bool_t
ds3231 bool enumeration definition
uint8_t ds3231_get_status(ds3231_handle_t *handle, uint8_t *status)
get the chip status
@ DS3231_FORMAT_24H
@ DS3231_FORMAT_12H
@ DS3231_STATUS_ALARM_1
@ DS3231_STATUS_ALARM_2
uint8_t ds3231_set_reg(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t ds3231_get_reg(ds3231_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]
ds3231_am_pm_t am_pm
ds3231_format_t format