LibDriver DS1302
Loading...
Searching...
No Matches
driver_ds1302.c
Go to the documentation of this file.
1
36
37#include "driver_ds1302.h"
38
42#define CHIP_NAME "Maxim Integrated DS1302"
43#define MANUFACTURER_NAME "Maxim Integrated"
44#define SUPPLY_VOLTAGE_MIN 2.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 1.28f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define DS1302_REG_SECOND (0 << 1)
55#define DS1302_REG_MINUTE (1 << 1)
56#define DS1302_REG_HOUR (2 << 1)
57#define DS1302_REG_DATE (3 << 1)
58#define DS1302_REG_MONTH (4 << 1)
59#define DS1302_REG_WEEK (5 << 1)
60#define DS1302_REG_YEAR (6 << 1)
61#define DS1302_REG_CONTROL (7 << 1)
62#define DS1302_REG_CHARGE (8 << 1)
63
67#define DS1302_COMMAND_RTC (0 << 6)
68#define DS1302_COMMAND_RAM (1 << 6)
69#define DS1302_COMMAND_BURST (0x1F << 1)
70
81static uint8_t a_ds1302_write(ds1302_handle_t *handle, uint8_t reg, uint8_t data)
82{
83 uint8_t res;
84 uint8_t i;
85 uint8_t temp;
86 uint8_t prev;
87
88 prev = (1 << 7) | reg; /* set reg */
89 res = handle->ce_gpio_write(1); /* set ce high */
90 if (res != 0) /* check the result */
91 {
92 return 1; /* return error */
93 }
94 temp = prev; /* set reg */
95 for (i = 0; i < 8; i++) /* loop */
96 {
97 if ((temp & 0x01) != 0) /* check lsb bit */
98 {
99 res = handle->io_gpio_write(1); /* set io high */
100 if (res != 0) /* check the result */
101 {
102 return 1; /* return error */
103 }
104 }
105 else
106 {
107 res = handle->io_gpio_write(0); /* set io low */
108 if (res != 0) /* check the result */
109 {
110 return 1; /* return error */
111 }
112 }
113 temp = temp >> 1; /* right shift 1 */
114 handle->delay_us(1); /* delay 1us */
115 res = handle->sclk_gpio_write(1); /* set sclk high */
116 if (res != 0) /* check the result */
117 {
118 return 1; /* return error */
119 }
120 handle->delay_us(1); /* delay 1us */
121 res = handle->sclk_gpio_write(0); /* set sclk low */
122 if (res != 0) /* check the result */
123 {
124 return 1; /* return error */
125 }
126 }
127 temp = data; /* set data */
128 for (i = 0; i < 8; i++) /* loop */
129 {
130 if ((temp & 0x01) != 0) /* check the lsb bit */
131 {
132 res = handle->io_gpio_write(1); /* set io high */
133 if (res != 0) /* check the result */
134 {
135 return 1; /* return error */
136 }
137 }
138 else
139 {
140 res = handle->io_gpio_write(0); /* set io low */
141 if (res != 0) /* check the result */
142 {
143 return 1; /* return error */
144 }
145 }
146 temp = temp >> 1; /* right shift 1 */
147 handle->delay_us(1); /* delay 1us */
148 res = handle->sclk_gpio_write(1); /* set sclk high */
149 if (res != 0) /* check the result */
150 {
151 return 1; /* return error */
152 }
153 handle->delay_us(1); /* delay 1us */
154 res = handle->sclk_gpio_write(0); /* set sclk low */
155 if (res != 0) /* check the result */
156 {
157 return 1; /* return error */
158 }
159 }
160 res = handle->io_gpio_write(0); /* set io low */
161 if (res != 0) /* check the result */
162 {
163 return 1; /* return error */
164 }
165 res = handle->ce_gpio_write(0); /* set ce low */
166 if (res != 0) /* check the result */
167 {
168 return 1; /* return error */
169 }
170
171 return 0; /* success return 0 */
172}
173
184static uint8_t a_ds1302_read(ds1302_handle_t *handle, uint8_t reg, uint8_t *data)
185{
186 uint8_t res;
187 uint8_t i;
188 uint8_t temp;
189 uint8_t prev;
190
191 prev = (1 << 7) | reg; /* set reg */
192 res = handle->ce_gpio_write(1); /* set ce high */
193 if (res != 0) /* check the result */
194 {
195 return 1; /* return error */
196 }
197 temp = prev; /* set reg */
198 for (i = 0; i < 8; i++) /* loop */
199 {
200 if ((temp & 0x01) != 0) /* check the lsb */
201 {
202 res = handle->io_gpio_write(1); /* set io high */
203 if (res != 0) /* check the result */
204 {
205 return 1; /* return error */
206 }
207 }
208 else
209 {
210 res = handle->io_gpio_write(0); /* set io low */
211 if (res != 0) /* check the result */
212 {
213 return 1; /* return error */
214 }
215 }
216 temp = temp >> 1; /* right shift 1 */
217 handle->delay_us(1); /* delay 1us */
218 res = handle->sclk_gpio_write(1); /* set sclk high */
219 if (res != 0) /* check the result */
220 {
221 return 1; /* return error */
222 }
223 handle->delay_us(1); /* delay 1us */
224 res = handle->sclk_gpio_write(0); /* set sclk low */
225 if (res != 0) /* check the result */
226 {
227 return 1; /* return error */
228 }
229 }
230 temp = 0; /* init temp 0 */
231 for (i = 0; i < 8; i++) /* loop */
232 {
233 uint8_t level;
234
235 temp = temp >> 1; /* right shift 1 */
236 res = handle->io_gpio_read(&level); /* read the level */
237 if (res != 0) /* check the result */
238 {
239 return 1; /* return error */
240 }
241 if (level != 0) /* check the level */
242 {
243 temp |= 1 << 7; /* set msb bit high */
244 }
245 else
246 {
247 temp &= ~(1 << 7); /* set msb bit low */
248 }
249 res = handle->sclk_gpio_write(1); /* set sclk high */
250 if (res != 0) /* check the result */
251 {
252 return 1; /* return error */
253 }
254 handle->delay_us(1); /* delay 1us */
255 res = handle->sclk_gpio_write(0); /* set sclk low */
256 if (res != 0) /* check the result */
257 {
258 return 1; /* return error */
259 }
260 }
261 *data = temp; /* save data */
262 res = handle->ce_gpio_write(0); /* set ce low */
263 if (res != 0) /* check the result */
264 {
265 return 1; /* return error */
266 }
267
268 return 0; /* success return 0 */
269}
270
282static uint8_t a_ds1302_multiple_write(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
283{
284 uint8_t res;
285 uint8_t i;
286
287 for (i = 0; i < len; i++) /* loop */
288 {
289 res = a_ds1302_write(handle, (uint8_t)(reg + (i << 1)), buf[i]); /* write one byte */
290 if (res != 0) /* check the result */
291 {
292 return 1; /* return error */
293 }
294 }
295
296 return 0; /* success return 0 */
297}
298
310static uint8_t a_ds1302_multiple_read(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
311{
312 uint8_t res;
313 uint8_t i;
314
315 for (i = 0; i < len; i++) /* loop */
316 {
317 res = a_ds1302_read(handle, (uint8_t)(reg + (i << 1) | 0x01), &buf[i]); /* read one byte */
318 if (res != 0) /* check the result */
319 {
320 return 1; /* read error */
321 }
322 }
323
324 return 0; /* success return 0 */
325}
326
338static uint8_t a_ds1302_burst_write(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
339{
340 uint8_t res;
341 uint8_t i;
342 uint8_t j;
343 uint8_t temp;
344 uint8_t prev;
345
346 prev = (1 << 7) | reg; /* set reg */
347 res = handle->ce_gpio_write(1); /* set ce high */
348 if (res != 0) /* check the result */
349 {
350 return 1; /* return error */
351 }
352 temp = prev; /* set reg */
353 for (i = 0; i < 8; i++) /* loop */
354 {
355 if ((temp & 0x01) != 0) /* check lsb bit */
356 {
357 res = handle->io_gpio_write(1); /* set io high */
358 if (res != 0) /* check the result */
359 {
360 return 1; /* return error */
361 }
362 }
363 else
364 {
365 res = handle->io_gpio_write(0); /* set io low */
366 if (res != 0) /* check the result */
367 {
368 return 1; /* return error */
369 }
370 }
371 temp = temp >> 1; /* right shift 1 */
372 handle->delay_us(1); /* delay 1us */
373 res = handle->sclk_gpio_write(1); /* set sclk high */
374 if (res != 0) /* check the result */
375 {
376 return 1; /* return error */
377 }
378 handle->delay_us(1); /* delay 1us */
379 res = handle->sclk_gpio_write(0); /* set sclk low */
380 if (res != 0) /* check the result */
381 {
382 return 1; /* return error */
383 }
384 }
385 for (j = 0; j < len; j++) /* write all */
386 {
387 temp = buf[j]; /* set data */
388 for (i = 0; i < 8; i++) /* loop */
389 {
390 if ((temp & 0x01) != 0) /* check the lsb bit */
391 {
392 res = handle->io_gpio_write(1); /* set io high */
393 if (res != 0) /* check the result */
394 {
395 return 1; /* return error */
396 }
397 }
398 else
399 {
400 res = handle->io_gpio_write(0); /* set io low */
401 if (res != 0) /* check the result */
402 {
403 return 1; /* return error */
404 }
405 }
406 temp = temp >> 1; /* right shift 1 */
407 handle->delay_us(1); /* delay 1us */
408 res = handle->sclk_gpio_write(1); /* set sclk high */
409 if (res != 0) /* check the result */
410 {
411 return 1; /* return error */
412 }
413 handle->delay_us(1); /* delay 1us */
414 res = handle->sclk_gpio_write(0); /* set sclk low */
415 if (res != 0) /* check the result */
416 {
417 return 1; /* return error */
418 }
419 }
420 }
421 res = handle->io_gpio_write(0); /* set io low */
422 if (res != 0) /* check the result */
423 {
424 return 1; /* return error */
425 }
426 res = handle->ce_gpio_write(0); /* set ce low */
427 if (res != 0) /* check the result */
428 {
429 return 1; /* return error */
430 }
431
432 return 0; /* success return 0 */
433}
434
446static uint8_t a_ds1302_burst_read(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
447{
448 uint8_t res;
449 uint8_t i;
450 uint8_t j;
451 uint8_t temp;
452 uint8_t prev;
453
454 prev = (1 << 7) | reg | 0x01; /* set reg */
455 res = handle->ce_gpio_write(1); /* set ce high */
456 if (res != 0) /* check the result */
457 {
458 return 1; /* return error */
459 }
460 temp = prev; /* set reg */
461 for (i = 0; i < 8; i++) /* loop */
462 {
463 if ((temp & 0x01) != 0) /* check the lsb */
464 {
465 res = handle->io_gpio_write(1); /* set io high */
466 if (res != 0) /* check the result */
467 {
468 return 1; /* return error */
469 }
470 }
471 else
472 {
473 res = handle->io_gpio_write(0); /* set io low */
474 if (res != 0) /* check the result */
475 {
476 return 1; /* return error */
477 }
478 }
479 temp = temp >> 1; /* right shift 1 */
480 handle->delay_us(1); /* delay 1us */
481 res = handle->sclk_gpio_write(1); /* set sclk high */
482 if (res != 0) /* check the result */
483 {
484 return 1; /* return error */
485 }
486 handle->delay_us(1); /* delay 1us */
487 res = handle->sclk_gpio_write(0); /* set sclk low */
488 if (res != 0) /* check the result */
489 {
490 return 1; /* return error */
491 }
492 }
493 for (j = 0; j < len; j++) /* read all */
494 {
495 temp = 0; /* init temp 0 */
496 for (i = 0; i < 8; i++) /* loop */
497 {
498 uint8_t level;
499
500 temp = temp >> 1; /* right shift 1 */
501 res = handle->io_gpio_read(&level); /* read the level */
502 if (res != 0) /* check the result */
503 {
504 return 1; /* return error */
505 }
506 if (level != 0) /* check the level */
507 {
508 temp |= 1 << 7; /* set msb bit high */
509 }
510 else
511 {
512 temp &= ~(1 << 7); /* set msb bit low */
513 }
514 res = handle->sclk_gpio_write(1); /* set sclk high */
515 if (res != 0) /* check the result */
516 {
517 return 1; /* return error */
518 }
519 handle->delay_us(1); /* delay 1us */
520 res = handle->sclk_gpio_write(0); /* set sclk low */
521 if (res != 0) /* check the result */
522 {
523 return 1; /* return error */
524 }
525 }
526 buf[j] = temp; /* save to buffer */
527 }
528 res = handle->ce_gpio_write(0); /* set ce low */
529 if (res != 0) /* check the result */
530 {
531 return 1; /* return error */
532 }
533
534 return 0; /* success return 0 */
535}
536
543static uint8_t a_ds1302_hex2bcd(uint8_t val)
544{
545 uint8_t i, j, k;
546
547 i = val / 10; /* get tens place */
548 j = val % 10; /* get ones place */
549 k = j + (i << 4); /* set bcd */
550
551 return k; /* return bcd */
552}
553
560static uint8_t a_ds1302_bcd2hex(uint8_t val)
561{
562 uint8_t temp;
563
564 temp = val & 0x0F; /* get ones place */
565 val = (val >> 4) & 0x0F; /* get tens place */
566 val = val * 10; /* set tens place */
567 temp = temp + val; /* get hex */
568
569 return temp; /* return hex */
570}
571
585{
586 uint8_t res;
587 uint8_t reg;
588 uint16_t year;
589
590 if (handle == NULL) /* check handle */
591 {
592 return 2; /* return error */
593 }
594 if (handle->inited != 1) /* check handle initialization */
595 {
596 return 3; /* return error */
597 }
598 if (t == NULL) /* check time */
599 {
600 handle->debug_print("ds1302: time is null.\n"); /* time is null */
601
602 return 2; /* return error */
603 }
604 if (t->format == DS1302_FORMAT_12H) /* if 12H */
605 {
606 if ((t->year < 2000) || (t->year > 2100)) /* check year */
607 {
608 handle->debug_print("ds1302: year can't be over 2100 or less than 2000.\n"); /* year can't be over 2100 or less than 2000 */
609
610 return 4; /* return error */
611 }
612 if ((t->month == 0) || (t->month > 12)) /* check month */
613 {
614 handle->debug_print("ds1302: month can't be zero or over than 12.\n"); /* month can't be zero or over than 12 */
615
616 return 4; /* return error */
617 }
618 if ((t->week == 0) || (t->week > 7)) /* check week */
619 {
620 handle->debug_print("ds1302: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
621
622 return 4; /* return error */
623 }
624 if ((t->date == 0) || (t->date > 31)) /* check data */
625 {
626 handle->debug_print("ds1302: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
627
628 return 4; /* return error */
629 }
630 if ((t->hour < 1) || (t->hour > 12)) /* check hour */
631 {
632 handle->debug_print("ds1302: hour can't be over than 12 or less 1.\n"); /* hour can't be over than 12 or less 1 */
633
634 return 4; /* return error */
635 }
636 if (t->minute > 59) /* check minute */
637 {
638 handle->debug_print("ds1302: minute can't be over than 59.\n"); /* minute can't be over than 59 */
639
640 return 4; /* return error */
641 }
642 if (t->second > 59) /* check second */
643 {
644 handle->debug_print("ds1302: second can't be over than 59.\n"); /* second can't be over than 59 */
645
646 return 4; /* return error */
647 }
648 }
649 else if (t->format == DS1302_FORMAT_24H) /* if 24H */
650 {
651 if ((t->year < 2000) || (t->year > 2100)) /* check year */
652 {
653 handle->debug_print("ds1302: year can't be over 2100 or less than 2000.\n"); /* year can't be over 2100 or less than 2000 */
654
655 return 4; /* return error */
656 }
657 if ((t->month == 0) || (t->month > 12)) /* check month */
658 {
659 handle->debug_print("ds1302: month can't be zero or over than 12.\n"); /* month can't be zero or over than 12 */
660
661 return 4; /* return error */
662 }
663 if ((t->week == 0) || (t->week > 7)) /* check week */
664 {
665 handle->debug_print("ds1302: week can't be zero or over than 7.\n"); /* week can't be zero or over than 7 */
666
667 return 4; /* return error */
668 }
669 if ((t->date == 0) || (t->date > 31)) /* check data */
670 {
671 handle->debug_print("ds1302: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
672
673 return 4; /* return error */
674 }
675 if (t->hour > 23) /* check hour */
676 {
677 handle->debug_print("ds1302: hour can't be over than 23.\n"); /* hour can't be over than 23 */
678
679 return 4; /* return error */
680 }
681 if (t->minute > 59) /* check minute */
682 {
683 handle->debug_print("ds1302: minute can't be over than 59.\n"); /* minute can't be over than 59 */
684
685 return 4; /* return error */
686 }
687 if (t->second > 59) /* check second */
688 {
689 handle->debug_print("ds1302: second can't be over than 59.\n"); /* second can't be over than 59 */
690
691 return 4; /* return error */
692 }
693 }
694 else
695 {
696 handle->debug_print("ds1302: format is invalid.\n"); /* format is invalid */
697
698 return 4; /* return error */
699 }
700
701 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_SECOND, &reg, 1); /* read second */
702 if (res != 0) /* check result */
703 {
704 handle->debug_print("ds1302: read second failed.\n"); /* read second failed */
705
706 return 1; /* return error */
707 }
708 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_SECOND,
709 a_ds1302_hex2bcd(t->second) | reg & (1 << 7)); /* write second */
710 if (res != 0) /* check result */
711 {
712 handle->debug_print("ds1302: write second failed.\n"); /* write second failed */
713
714 return 1; /* return error */
715 }
716 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_MINUTE, a_ds1302_hex2bcd(t->minute)); /* write minute */
717 if (res != 0) /* check result */
718 {
719 handle->debug_print("ds1302: write minute failed.\n"); /* write minute failed */
720
721 return 1; /* return error */
722 }
723 if (t->format == DS1302_FORMAT_12H) /* if 12H */
724 {
725 reg = (uint8_t)((1 << 7) | (t->am_pm << 5) | a_ds1302_hex2bcd(t->hour)); /* set hour in 12H */
726 }
727 else /* if 24H */
728 {
729 reg = (0 << 7) | a_ds1302_hex2bcd(t->hour); /* set hour in 24H */
730 }
731 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_HOUR, reg); /* write hour */
732 if (res != 0) /* check result */
733 {
734 handle->debug_print("ds1302: write hour failed.\n"); /* write hour failed */
735
736 return 1; /* return error */
737 }
738 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_WEEK, a_ds1302_hex2bcd(t->week)); /* write week */
739 if (res != 0) /* check result */
740 {
741 handle->debug_print("ds1302: write week failed.\n"); /* write week failed */
742
743 return 1; /* return error */
744 }
745 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_DATE, a_ds1302_hex2bcd(t->date)); /* write data */
746 if (res != 0) /* check result */
747 {
748 handle->debug_print("ds1302: write date failed.\n"); /* write date failed */
749
750 return 1; /* return error */
751 }
752 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_MONTH, a_ds1302_hex2bcd(t->month)); /* write month and century */
753 if (res != 0) /* check result */
754 {
755 handle->debug_print("ds1302: write century and month failed.\n"); /* write century and month failed */
756
757 return 1; /* return error */
758 }
759 year = t->year - 2000; /* year - 2000 */
760 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_YEAR, a_ds1302_hex2bcd((uint8_t)year)); /* write year */
761 if (res != 0) /* check result */
762 {
763 handle->debug_print("ds1302: write year failed.\n"); /* write year failed */
764
765 return 1; /* return error */
766 }
767
768 return 0; /* success return 0 */
769}
770
783{
784 uint8_t res;
785 uint8_t buf[7];
786
787 if (handle == NULL) /* check handle */
788 {
789 return 2; /* return error */
790 }
791 if (handle->inited != 1) /* check handle initialization */
792 {
793 return 3; /* return error */
794 }
795 if (t == NULL) /* check time */
796 {
797 handle->debug_print("ds1302: time is null.\n"); /* time is null */
798
799 return 2; /* return error */
800 }
801
802 memset(buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
803 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_SECOND,
804 (uint8_t *)buf, 7); /* multiple_read */
805 if (res != 0) /* check result */
806 {
807 handle->debug_print("ds1302: multiple read failed.\n"); /* multiple read failed */
808
809 return 1; /* return error */
810 }
811 t->year = a_ds1302_bcd2hex(buf[6]) + 2000; /* get year */
812 t->month = a_ds1302_bcd2hex(buf[4] & 0x1F); /* get month */
813 t->week = a_ds1302_bcd2hex(buf[5] & 0x7); /* get week */
814 t->date = a_ds1302_bcd2hex(buf[3] & 0x3F); /* get date */
815 t->am_pm = (ds1302_am_pm_t)((buf[2] >> 5) & 0x01); /* get am pm */
816 t->format = (ds1302_format_t)((buf[2] >> 7) & 0x01); /* get format */
817 if (t->format == DS1302_FORMAT_12H) /* if 12H */
818 {
819 t->hour = a_ds1302_bcd2hex(buf[2] & 0x1F); /* get hour */
820 }
821 else
822 {
823 t->hour = a_ds1302_bcd2hex(buf[2] & 0x3F); /* get hour */
824 }
825 t->minute = a_ds1302_bcd2hex(buf[1]); /* get minute */
826 t->second = a_ds1302_bcd2hex(buf[0] & (~(1 << 7))); /* get second */
827
828 return 0; /* success return 0 */
829}
830
843{
844 uint8_t res;
845 uint8_t prev;
846
847 if (handle == NULL) /* check handle */
848 {
849 return 2; /* return error */
850 }
851 if (handle->inited != 1) /* check handle initialization */
852 {
853 return 3; /* return error */
854 }
855
856 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_SECOND,
857 &prev, 1); /* read second */
858 if (res != 0) /* check result */
859 {
860 handle->debug_print("ds1302: read second failed.\n"); /* read second failed */
861
862 return 1; /* return error */
863 }
864 prev &= ~(1 << 7); /* clear config */
865 prev |= (!enable) << 7; /* set enable */
866 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_SECOND,
867 prev); /* write second */
868 if (res != 0) /* check result */
869 {
870 handle->debug_print("ds1302: write second failed.\n"); /* write second failed */
871
872 return 1; /* return error */
873 }
874
875 return 0; /* success return 0 */
876}
877
890{
891 uint8_t res;
892 uint8_t prev;
893
894 if (handle == NULL) /* check handle */
895 {
896 return 2; /* return error */
897 }
898 if (handle->inited != 1) /* check handle initialization */
899 {
900 return 3; /* return error */
901 }
902
903 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_SECOND,
904 (uint8_t *)&prev, 1); /* multiple read */
905 if (res != 0) /* check result */
906 {
907 handle->debug_print("ds1302: read second failed.\n"); /* read second failed */
908
909 return 1; /* return error */
910 }
911 *enable = (ds1302_bool_t)(!((prev >> 7) & 0x01)); /* get enable */
912
913 return 0; /* success return 0 */
914}
915
928{
929 uint8_t res;
930 uint8_t prev;
931
932 if (handle == NULL) /* check handle */
933 {
934 return 2; /* return error */
935 }
936 if (handle->inited != 1) /* check handle initialization */
937 {
938 return 3; /* return error */
939 }
940
941 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_CONTROL,
942 &prev, 1); /* read control */
943 if (res != 0) /* check result */
944 {
945 handle->debug_print("ds1302: read control failed.\n"); /* read control failed */
946
947 return 1; /* return error */
948 }
949 prev &= ~(1 << 7); /* clear config */
950 prev |= enable << 7; /* set bool */
951 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_CONTROL, prev); /* write control */
952 if (res != 0) /* check result */
953 {
954 handle->debug_print("ds1302: write control failed.\n"); /* write control failed */
955
956 return 1; /* return error */
957 }
958
959 return 0; /* success return 0 */
960}
961
974{
975 uint8_t res;
976 uint8_t prev;
977
978 if (handle == NULL) /* check handle */
979 {
980 return 2; /* return error */
981 }
982 if (handle->inited != 1) /* check handle initialization */
983 {
984 return 3; /* return error */
985 }
986
987 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_CONTROL,
988 &prev, 1); /* read control */
989 if (res != 0) /* check result */
990 {
991 handle->debug_print("ds1302: read control failed.\n"); /* read control failed */
992
993 return 1; /* return error */
994 }
995 *enable = (ds1302_bool_t)((prev >> 7) & 0x01); /* set bool */
996
997 return 0; /* success return 0 */
998}
999
1011uint8_t ds1302_set_charge(ds1302_handle_t *handle, uint8_t charge)
1012{
1013 uint8_t res;
1014
1015 if (handle == NULL) /* check handle */
1016 {
1017 return 2; /* return error */
1018 }
1019 if (handle->inited != 1) /* check handle initialization */
1020 {
1021 return 3; /* return error */
1022 }
1023
1024 res = a_ds1302_write(handle, DS1302_COMMAND_RTC | DS1302_REG_CHARGE, charge); /* write charge */
1025 if (res != 0) /* check result */
1026 {
1027 handle->debug_print("ds1302: write charge failed.\n"); /* write charge failed */
1028
1029 return 1; /* return error */
1030 }
1031
1032 return 0; /* success return 0 */
1033}
1034
1046uint8_t ds1302_get_charge(ds1302_handle_t *handle, uint8_t *charge)
1047{
1048 uint8_t res;
1049
1050 if (handle == NULL) /* check handle */
1051 {
1052 return 2; /* return error */
1053 }
1054 if (handle->inited != 1) /* check handle initialization */
1055 {
1056 return 3; /* return error */
1057 }
1058
1059 res = a_ds1302_multiple_read(handle, DS1302_COMMAND_RTC | DS1302_REG_CHARGE, charge, 1); /* read charge */
1060 if (res != 0) /* check result */
1061 {
1062 handle->debug_print("ds1302: read charge failed.\n"); /* read charge failed */
1063
1064 return 1; /* return error */
1065 }
1066
1067 return 0; /* success return 0 */
1068}
1069
1085uint8_t ds1302_read_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
1086{
1087 uint8_t res;
1088
1089 if (handle == NULL) /* check handle */
1090 {
1091 return 2; /* return error */
1092 }
1093 if (handle->inited != 1) /* check handle initialization */
1094 {
1095 return 3; /* return error */
1096 }
1097 if (addr > 30) /* check addr */
1098 {
1099 handle->debug_print("ds1302: addr > 30.\n"); /* addr > 30 */
1100
1101 return 4; /* return error */
1102 }
1103 if (addr + len - 1 > 30) /* check len */
1104 {
1105 handle->debug_print("ds1302: len is invalid.\n"); /* len is invalid */
1106
1107 return 5; /* return error */
1108 }
1109
1110 res = a_ds1302_multiple_read(handle, (uint8_t)(DS1302_COMMAND_RAM + addr),
1111 buf, len); /* read ram */
1112 if (res != 0) /* check result */
1113 {
1114 handle->debug_print("ds1302: read ram failed.\n"); /* read ram failed */
1115
1116 return 1; /* return error */
1117 }
1118
1119 return 0; /* success return 0 */
1120}
1121
1137uint8_t ds1302_write_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
1138{
1139 uint8_t res;
1140
1141 if (handle == NULL) /* check handle */
1142 {
1143 return 2; /* return error */
1144 }
1145 if (handle->inited != 1) /* check handle initialization */
1146 {
1147 return 3; /* return error */
1148 }
1149 if (addr > 30) /* check addr */
1150 {
1151 handle->debug_print("ds1302: addr > 30.\n"); /* addr > 30 */
1152
1153 return 4; /* return error */
1154 }
1155 if (addr + len - 1 > 30) /* check len */
1156 {
1157 handle->debug_print("ds1302: len is invalid.\n"); /* len is invalid */
1158
1159 return 5; /* return error */
1160 }
1161
1162 res = a_ds1302_multiple_write(handle, (uint8_t)(DS1302_COMMAND_RAM + addr),
1163 buf, len); /* write ram */
1164 if (res != 0) /* check result */
1165 {
1166 handle->debug_print("ds1302: write ram failed.\n"); /* write ram failed */
1167
1168 return 1; /* return error */
1169 }
1170
1171 return 0; /* success return 0 */
1172}
1173
1185{
1186 if (handle == NULL) /* check handle */
1187 {
1188 return 2; /* return error */
1189 }
1190 if (handle->debug_print == NULL) /* check debug_print */
1191 {
1192 return 3; /* return error */
1193 }
1194 if (handle->ce_gpio_init == NULL) /* check ce_gpio_init */
1195 {
1196 handle->debug_print("ds1302: ce_gpio_init is null.\n"); /* ce_gpio_init is null */
1197
1198 return 3; /* return error */
1199 }
1200 if (handle->ce_gpio_deinit == NULL) /* check ce_gpio_deinit */
1201 {
1202 handle->debug_print("ds1302: ce_gpio_deinit is null.\n"); /* ce_gpio_deinit is null */
1203
1204 return 3; /* return error */
1205 }
1206 if (handle->ce_gpio_write == NULL) /* check ce_gpio_write */
1207 {
1208 handle->debug_print("ds1302: ce_gpio_write is null.\n"); /* ce_gpio_write is null */
1209
1210 return 3; /* return error */
1211 }
1212 if (handle->sclk_gpio_init == NULL) /* check sclk_gpio_init */
1213 {
1214 handle->debug_print("ds1302: sclk_gpio_init is null.\n"); /* sclk_gpio_init is null */
1215
1216 return 3; /* return error */
1217 }
1218 if (handle->sclk_gpio_deinit == NULL) /* check sclk_gpio_deinit */
1219 {
1220 handle->debug_print("ds1302: sclk_gpio_deinit is null.\n"); /* sclk_gpio_deinit is null */
1221
1222 return 3; /* return error */
1223 }
1224 if (handle->sclk_gpio_write == NULL) /* check sclk_gpio_write */
1225 {
1226 handle->debug_print("ds1302: sclk_gpio_write is null.\n"); /* sclk_gpio_write is null */
1227
1228 return 3; /* return error */
1229 }
1230 if (handle->io_gpio_init == NULL) /* check io_gpio_init */
1231 {
1232 handle->debug_print("ds1302: io_gpio_init is null.\n"); /* io_gpio_init is null */
1233
1234 return 3; /* return error */
1235 }
1236 if (handle->io_gpio_deinit == NULL) /* check io_gpio_deinit */
1237 {
1238 handle->debug_print("ds1302: io_gpio_deinit is null.\n"); /* io_gpio_deinit is null */
1239
1240 return 3; /* return error */
1241 }
1242 if (handle->io_gpio_write == NULL) /* check io_gpio_write */
1243 {
1244 handle->debug_print("ds1302: io_gpio_write is null.\n"); /* io_gpio_write is null */
1245
1246 return 3; /* return error */
1247 }
1248 if (handle->io_gpio_read == NULL) /* check io_gpio_read */
1249 {
1250 handle->debug_print("ds1302: io_gpio_read is null.\n"); /* io_gpio_read is null */
1251
1252 return 3; /* return error */
1253 }
1254 if (handle->delay_ms == NULL) /* check delay_ms */
1255 {
1256 handle->debug_print("ds1302: delay_ms is null.\n"); /* delay_ms is null */
1257
1258 return 3; /* return error */
1259 }
1260
1261 if (handle->ce_gpio_init() != 0) /* ce gpio init */
1262 {
1263 handle->debug_print("ds1302: ce gpio init failed.\n"); /* ce gpio init failed */
1264
1265 return 1; /* return error */
1266 }
1267 if (handle->sclk_gpio_init() != 0) /* sclk gpio init */
1268 {
1269 handle->debug_print("ds1302: sclk gpio init failed.\n"); /* sclk gpio init failed */
1270 (void)handle->ce_gpio_deinit(); /* ce gpio deinit */
1271
1272 return 1; /* return error */
1273 }
1274 if (handle->io_gpio_init() != 0) /* io gpio init */
1275 {
1276 handle->debug_print("ds1302: io gpio init failed.\n"); /* io gpio init failed */
1277 (void)handle->ce_gpio_deinit(); /* ce gpio deinit */
1278 (void)handle->sclk_gpio_deinit(); /* sclk gpio deinit */
1279
1280 return 1; /* return error */
1281 }
1282 handle->inited = 1; /* flag finish initialization */
1283
1284 return 0; /* success return 0 */
1285}
1286
1298{
1299 if (handle == NULL) /* check handle */
1300 {
1301 return 2; /* return error */
1302 }
1303 if (handle->inited != 1) /* check handle initialization */
1304 {
1305 return 3; /* return error */
1306 }
1307
1308 if (handle->ce_gpio_deinit() != 0) /* ce gpio deinit */
1309 {
1310 handle->debug_print("ds1302: ce gpio deinit failed.\n"); /* ce gpio deinit failed */
1311
1312 return 1; /* return error */
1313 }
1314 if (handle->sclk_gpio_deinit() != 0) /* sclk gpio deinit */
1315 {
1316 handle->debug_print("ds1302: sclk gpio deinit failed.\n"); /* sclk gpio deinit failed */
1317
1318 return 1; /* return error */
1319 }
1320 if (handle->io_gpio_deinit() != 0) /* io gpio deinit */
1321 {
1322 handle->debug_print("ds1302: io gpio deinit failed.\n"); /* io gpio deinit failed */
1323
1324 return 1; /* return error */
1325 }
1326 handle->inited = 0; /* flag close */
1327
1328 return 0; /* success return 0 */
1329}
1330
1344uint8_t ds1302_clock_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
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 if (len > 8) /* check len */
1355 {
1356 handle->debug_print("ds1302: len > 8.\n"); /* len > 8 */
1357
1358 return 4; /* return error */
1359 }
1360
1361 if (a_ds1302_burst_write(handle, DS1302_COMMAND_RTC | DS1302_COMMAND_BURST, buf, len) != 0) /* write data */
1362 {
1363 return 1; /* return error */
1364 }
1365
1366 return 0; /* success return 0 */
1367}
1368
1382uint8_t ds1302_clock_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
1383{
1384 if (handle == NULL) /* check handle */
1385 {
1386 return 2; /* return error */
1387 }
1388 if (handle->inited != 1) /* check handle initialization */
1389 {
1390 return 3; /* return error */
1391 }
1392 if (len > 8) /* check len */
1393 {
1394 handle->debug_print("ds1302: len > 8.\n"); /* len > 8 */
1395
1396 return 4; /* return error */
1397 }
1398
1399 if (a_ds1302_burst_read(handle, DS1302_COMMAND_RTC | DS1302_COMMAND_BURST, buf, len) != 0) /* read data */
1400 {
1401 return 1; /* return error */
1402 }
1403
1404 return 0; /* success return 0 */
1405}
1406
1420uint8_t ds1302_ram_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
1421{
1422 if (handle == NULL) /* check handle */
1423 {
1424 return 2; /* return error */
1425 }
1426 if (handle->inited != 1) /* check handle initialization */
1427 {
1428 return 3; /* return error */
1429 }
1430 if (len > 31) /* check len */
1431 {
1432 handle->debug_print("ds1302: len > 31.\n"); /* len > 31 */
1433
1434 return 4; /* return error */
1435 }
1436
1437 if (a_ds1302_burst_write(handle, DS1302_COMMAND_RAM | DS1302_COMMAND_BURST, buf, len) != 0) /* write data */
1438 {
1439 return 1; /* return error */
1440 }
1441
1442 return 0; /* success return 0 */
1443}
1444
1458uint8_t ds1302_ram_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
1459{
1460 if (handle == NULL) /* check handle */
1461 {
1462 return 2; /* return error */
1463 }
1464 if (handle->inited != 1) /* check handle initialization */
1465 {
1466 return 3; /* return error */
1467 }
1468 if (len > 31) /* check len */
1469 {
1470 handle->debug_print("ds1302: len > 31.\n"); /* len > 31 */
1471
1472 return 4; /* return error */
1473 }
1474
1475 if (a_ds1302_burst_read(handle, DS1302_COMMAND_RAM | DS1302_COMMAND_BURST, buf, len) != 0) /* read data */
1476 {
1477 return 1; /* return error */
1478 }
1479
1480 return 0; /* success return 0 */
1481}
1482
1496uint8_t ds1302_set_reg(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
1497{
1498 if (handle == NULL) /* check handle */
1499 {
1500 return 2; /* return error */
1501 }
1502 if (handle->inited != 1) /* check handle initialization */
1503 {
1504 return 3; /* return error */
1505 }
1506
1507 if (a_ds1302_multiple_write(handle, reg, buf, len) != 0) /* write data */
1508 {
1509 return 1; /* return error */
1510 }
1511
1512 return 0; /* success return 0 */
1513}
1514
1528uint8_t ds1302_get_reg(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
1529{
1530 if (handle == NULL) /* check handle */
1531 {
1532 return 2; /* return error */
1533 }
1534 if (handle->inited != 1) /* check handle initialization */
1535 {
1536 return 3; /* return error */
1537 }
1538
1539 if (a_ds1302_multiple_read(handle, reg, buf, len) != 0) /* read data */
1540 {
1541 return 1; /* return error */
1542 }
1543
1544 return 0; /* success return 0 */
1545}
1546
1556{
1557 if (info == NULL) /* check handle */
1558 {
1559 return 2; /* return error */
1560 }
1561
1562 memset(info, 0, sizeof(ds1302_info_t)); /* initialize ds1302 info structure */
1563 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1564 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1565 strncpy(info->interface, "GPIO", 8); /* copy interface name */
1566 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1567 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1568 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1569 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1570 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1571 info->driver_version = DRIVER_VERSION; /* set driver version */
1572
1573 return 0; /* success return 0 */
1574}
#define DS1302_REG_WEEK
#define MAX_CURRENT
#define DS1302_REG_YEAR
#define DS1302_COMMAND_BURST
#define DS1302_REG_SECOND
chip register definition
#define DS1302_REG_CONTROL
#define DS1302_COMMAND_RAM
#define DS1302_COMMAND_RTC
chip command definition
#define SUPPLY_VOLTAGE_MAX
#define DS1302_REG_HOUR
#define TEMPERATURE_MAX
#define DS1302_REG_CHARGE
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DS1302_REG_MONTH
#define DRIVER_VERSION
#define DS1302_REG_MINUTE
#define DS1302_REG_DATE
driver ds1302 header file
uint8_t ds1302_clock_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
clock burst read
uint8_t ds1302_ram_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
ram burst write
uint8_t ds1302_clock_burst_write(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
clock burst write
uint8_t ds1302_ram_burst_read(ds1302_handle_t *handle, uint8_t *buf, uint8_t len)
ram burst read
uint8_t ds1302_init(ds1302_handle_t *handle)
initialize the chip
uint8_t ds1302_set_write_protect(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable write protect
struct ds1302_time_s ds1302_time_t
ds1302 time structure definition
ds1302_bool_t
ds1302 bool enumeration definition
uint8_t ds1302_deinit(ds1302_handle_t *handle)
close the chip
ds1302_format_t
ds1302 format enumeration definition
struct ds1302_info_s ds1302_info_t
ds1302 information structure definition
uint8_t ds1302_set_oscillator(ds1302_handle_t *handle, ds1302_bool_t enable)
enable or disable the oscillator
uint8_t ds1302_get_write_protect(ds1302_handle_t *handle, ds1302_bool_t *enable)
get write protect status
ds1302_am_pm_t
ds1302 am pm enumeration definition
uint8_t ds1302_get_time(ds1302_handle_t *handle, ds1302_time_t *t)
get the current time
uint8_t ds1302_info(ds1302_info_t *info)
get chip's information
uint8_t ds1302_read_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
read ram
uint8_t ds1302_get_oscillator(ds1302_handle_t *handle, ds1302_bool_t *enable)
get the chip oscillator status
struct ds1302_handle_s ds1302_handle_t
ds1302 handle structure definition
uint8_t ds1302_write_ram(ds1302_handle_t *handle, uint8_t addr, uint8_t *buf, uint8_t len)
write ram
uint8_t ds1302_set_charge(ds1302_handle_t *handle, uint8_t charge)
set charge
uint8_t ds1302_set_time(ds1302_handle_t *handle, ds1302_time_t *t)
set the current time
uint8_t ds1302_get_charge(ds1302_handle_t *handle, uint8_t *charge)
get charge
@ DS1302_FORMAT_24H
@ DS1302_FORMAT_12H
uint8_t ds1302_set_reg(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
set the chip register
uint8_t ds1302_get_reg(ds1302_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
get the chip register
uint8_t(* ce_gpio_deinit)(void)
uint8_t(* io_gpio_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* sclk_gpio_write)(uint8_t value)
uint8_t(* sclk_gpio_deinit)(void)
uint8_t(* io_gpio_write)(uint8_t value)
void(* debug_print)(const char *const fmt,...)
void(* delay_us)(uint32_t us)
uint8_t(* sclk_gpio_init)(void)
uint8_t(* ce_gpio_init)(void)
uint8_t(* ce_gpio_write)(uint8_t value)
uint8_t(* io_gpio_deinit)(void)
uint8_t(* io_gpio_read)(uint8_t *value)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]
ds1302_am_pm_t am_pm
ds1302_format_t format