LibDriver PCF8563
Loading...
Searching...
No Matches
driver_pcf8563.c
Go to the documentation of this file.
1
36
37#include "driver_pcf8563.h"
38
42#define CHIP_NAME "NXP PCF8563"
43#define MANUFACTURER_NAME "NXP"
44#define SUPPLY_VOLTAGE_MIN 1.9f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.80f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define PCF8563_REG_CONTROL_STATUS1 0x00
55#define PCF8563_REG_CONTROL_STATUS2 0x01
56#define PCF8563_REG_SECOND 0x02
57#define PCF8563_REG_MINUTE 0x03
58#define PCF8563_REG_HOUR 0x04
59#define PCF8563_REG_DAY 0x05
60#define PCF8563_REG_WEEK 0x06
61#define PCF8563_REG_MONTH 0x07
62#define PCF8563_REG_YEAR 0x08
63#define PCF8563_REG_MINUTE_ALARM 0x09
64#define PCF8563_REG_HOUR_ALARM 0x0A
65#define PCF8563_REG_DAY_ALARM 0x0B
66#define PCF8563_REG_WEEK_ALARM 0x0C
67#define PCF8563_REG_CLKOUT_CONTROL 0x0D
68#define PCF8563_REG_TIMER_CONTROL 0x0E
69#define PCF8563_REG_TIMER 0x0F
70
74#define PCF8563_ADDRESS 0xA2
75
86static uint8_t a_pcf8563_iic_write(pcf8563_handle_t *handle, uint8_t reg, uint8_t data)
87{
88 if (handle->iic_write(PCF8563_ADDRESS, reg, &data, 1) != 0) /* write data */
89 {
90 return 1; /* return error */
91 }
92
93 return 0; /* success return 0 */
94}
95
107static uint8_t a_pcf8563_iic_multiple_read(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint8_t len)
108{
109 if (handle->iic_read(PCF8563_ADDRESS, reg, buf, len) != 0) /* read data */
110 {
111 return 1; /* return error */
112 }
113
114 return 0; /* success return 0 */
115}
116
123static uint8_t a_pcf8563_hex2bcd(uint8_t val)
124{
125 uint8_t i, j, k;
126
127 i = val / 10; /* get tens place */
128 j = val % 10; /* get ones place */
129 k = j + (i << 4); /* set bcd */
130
131 return k; /* return bcd */
132}
133
140static uint8_t a_pcf8563_bcd2hex(uint8_t val)
141{
142 uint8_t temp;
143
144 temp = val & 0x0F; /* get ones place */
145 val = (val >> 4) & 0x0F; /* get tens place */
146 val = val * 10; /* set tens place */
147 temp = temp + val; /* get hex */
148
149 return temp; /* return hex */
150}
151
165{
166 uint8_t res;
167 uint16_t year;
168
169 if (handle == NULL) /* check handle */
170 {
171 return 2; /* return error */
172 }
173 if (handle->inited != 1) /* check handle initialization */
174 {
175 return 3; /* return error */
176 }
177 if (t == NULL) /* check time */
178 {
179 handle->debug_print("pcf8563: time is null.\n"); /* time is null */
180
181 return 2; /* return error */
182 }
183
184 if ((t->year < 2000) || (t->year > 2199)) /* check year */
185 {
186 handle->debug_print("pcf8563: year can't be over 2199 or less than 2000.\n"); /* year can't be over 2199 or less than 2000 */
187
188 return 4; /* return error */
189 }
190 if ((t->month == 0) || (t->month > 12)) /* check month */
191 {
192 handle->debug_print("pcf8563: month can't be zero or over than 12.\n"); /* month can't be zero or over than 12 */
193
194 return 4; /* return error */
195 }
196 if (t->week > 6) /* check week */
197 {
198 handle->debug_print("pcf8563: week can't be over than 6.\n"); /* week can't be over than 6 */
199
200 return 4; /* return error */
201 }
202 if ((t->date == 0) || (t->date > 31)) /* check data */
203 {
204 handle->debug_print("pcf8563: date can't be zero or over than 31.\n"); /* date can't be zero or over than 31 */
205
206 return 4; /* return error */
207 }
208 if (t->hour > 23) /* check hour */
209 {
210 handle->debug_print("pcf8563: hour can't be over than 23.\n"); /* hour can't be over than 23 */
211
212 return 4; /* return error */
213 }
214 if (t->minute > 59) /* check minute */
215 {
216 handle->debug_print("pcf8563: minute can't be over than 59.\n"); /* minute can't be over than 59 */
217
218 return 4; /* return error */
219 }
220 if (t->second > 59) /* check second */
221 {
222 handle->debug_print("pcf8563: second can't be over than 59.\n"); /* second can't be over than 59 */
223
224 return 4; /* return error */
225 }
226 res = a_pcf8563_iic_write(handle, PCF8563_REG_SECOND, a_pcf8563_hex2bcd(t->second)); /* write second */
227 if (res != 0) /* check result */
228 {
229 handle->debug_print("pcf8563: write second failed.\n"); /* write second failed */
230
231 return 1; /* return error */
232 }
233 res = a_pcf8563_iic_write(handle, PCF8563_REG_MINUTE, a_pcf8563_hex2bcd(t->minute)); /* write minute */
234 if (res != 0) /* check result */
235 {
236 handle->debug_print("pcf8563: write minute failed.\n"); /* write minute failed */
237
238 return 1; /* return error */
239 }
240 res = a_pcf8563_iic_write(handle, PCF8563_REG_HOUR, a_pcf8563_hex2bcd(t->hour)); /* write hour */
241 if (res != 0) /* check result */
242 {
243 handle->debug_print("pcf8563: write hour failed.\n"); /* write hour failed */
244
245 return 1; /* return error */
246 }
247 res = a_pcf8563_iic_write(handle, PCF8563_REG_WEEK, a_pcf8563_hex2bcd(t->week)); /* write week */
248 if (res != 0) /* check result */
249 {
250 handle->debug_print("pcf8563: write week failed.\n"); /* write week failed */
251
252 return 1; /* return error */
253 }
254 res = a_pcf8563_iic_write(handle, PCF8563_REG_DAY, a_pcf8563_hex2bcd(t->date)); /* write day */
255 if (res != 0) /* check result */
256 {
257 handle->debug_print("pcf8563: write day failed.\n"); /* write day failed */
258
259 return 1; /* return error */
260 }
261 year = t->year - 2000; /* convert year */
262 if (year >= 100) /* check year */
263 {
264 res = a_pcf8563_iic_write(handle, PCF8563_REG_MONTH, a_pcf8563_hex2bcd(t->month) | (1 << 7)); /* write month and century */
265 if (res != 0) /* check result */
266 {
267 handle->debug_print("pcf8563: write century and month failed.\n"); /* write century and month failed */
268
269 return 1; /* return error */
270 }
271 year = year - 100; /* year - 100 */
272 res = a_pcf8563_iic_write(handle, PCF8563_REG_YEAR, a_pcf8563_hex2bcd((uint8_t)year)); /* write year */
273 if (res != 0) /* check result */
274 {
275 handle->debug_print("pcf8563: write year failed.\n"); /* write year failed */
276
277 return 1; /* return error */
278 }
279 }
280 else
281 {
282 res = a_pcf8563_iic_write(handle, PCF8563_REG_MONTH, a_pcf8563_hex2bcd(t->month)); /* write month and century */
283 if (res != 0) /* check result */
284 {
285 handle->debug_print("pcf8563: write century and month failed.\n"); /* write century and month failed */
286
287 return 1; /* return error */
288 }
289 res = a_pcf8563_iic_write(handle, PCF8563_REG_YEAR, a_pcf8563_hex2bcd((uint8_t)year)); /* write year */
290 if (res != 0) /* check result */
291 {
292 handle->debug_print("pcf8563: write year failed.\n"); /* write year failed */
293
294 return 1; /* return error */
295 }
296 }
297
298 return 0; /* success return 0 */
299}
300
314{
315 uint8_t res;
316 uint8_t buf[7];
317
318 if (handle == NULL) /* check handle */
319 {
320 return 2; /* return error */
321 }
322 if (handle->inited != 1) /* check handle initialization */
323 {
324 return 3; /* return error */
325 }
326 if (t == NULL) /* check time */
327 {
328 handle->debug_print("pcf8563: time is null.\n"); /* time is null */
329
330 return 2; /* return error */
331 }
332
333 memset(buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
334 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_SECOND, (uint8_t *)buf, 7); /* multiple_read */
335 if (res != 0) /* check result */
336 {
337 handle->debug_print("pcf8563: multiple read failed.\n"); /* multiple read failed */
338
339 return 1; /* return error */
340 }
341 if (((buf[0] >> 7) & 0x01) != 0) /* check bit */
342 {
343 handle->debug_print("pcf8563: clock integrity is not guaranteed.\n"); /* clock integrity is not guaranteed */
344
345 return 4; /* return error */
346 }
347 t->year = a_pcf8563_bcd2hex(buf[6]) + 2000; /* get year */
348 if (((buf[5] >> 7) & 0x01) != 0) /* check century */
349 {
350 t->year += 100; /* add 100 */
351 buf[5] &= ~(1 << 7); /* clear bit */
352 }
353 t->month = a_pcf8563_bcd2hex(buf[5] & 0x1F); /* get month */
354 t->week = a_pcf8563_bcd2hex(buf[4] & 0x7); /* get week */
355 t->date = a_pcf8563_bcd2hex(buf[3] & 0x3F); /* get date */
356 t->hour = a_pcf8563_bcd2hex(buf[2] & 0x3F); /* get hour */
357 t->minute = a_pcf8563_bcd2hex(buf[1] & 0x7F); /* get minute */
358 t->second = a_pcf8563_bcd2hex(buf[0] & 0x7F); /* get second */
359
360 return 0; /* success return 0 */
361}
362
375{
376 uint8_t res;
377 uint8_t prev;
378
379 if (handle == NULL) /* check handle */
380 {
381 return 2; /* return error */
382 }
383 if (handle->inited != 1) /* check handle initialization */
384 {
385 return 3; /* return error */
386 }
387
388 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
389 if (res != 0) /* check result */
390 {
391 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
392
393 return 1; /* return error */
394 }
395 prev &= ~(1 << 6); /* clear unused */
396 prev &= ~(1 << 4); /* clear unused */
397 prev &= ~(7 << 0); /* clear unused */
398 prev &= ~(1 << 7); /* clear config */
399 prev |= enable << 7; /* set bool */
400 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS1, prev); /* write control status1 */
401 if (res != 0) /* check result */
402 {
403 handle->debug_print("pcf8563: write control status1 failed.\n"); /* write control status1 failed */
404
405 return 1; /* return error */
406 }
407
408 return 0; /* success return 0 */
409}
410
423{
424 uint8_t res;
425 uint8_t prev;
426
427 if (handle == NULL) /* check handle */
428 {
429 return 2; /* return error */
430 }
431 if (handle->inited != 1) /* check handle initialization */
432 {
433 return 3; /* return error */
434 }
435
436 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
437 if (res != 0) /* check result */
438 {
439 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
440
441 return 1; /* return error */
442 }
443 *enable = (pcf8563_bool_t)(((prev >> 7) & 0x01)); /* get enable */
444
445 return 0; /* success return 0 */
446}
447
460{
461 uint8_t res;
462 uint8_t prev;
463
464 if (handle == NULL) /* check handle */
465 {
466 return 2; /* return error */
467 }
468 if (handle->inited != 1) /* check handle initialization */
469 {
470 return 3; /* return error */
471 }
472
473 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
474 if (res != 0) /* check result */
475 {
476 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
477
478 return 1; /* return error */
479 }
480 prev &= ~(1 << 6); /* clear unused */
481 prev &= ~(1 << 4); /* clear unused */
482 prev &= ~(7 << 0); /* clear unused */
483 prev &= ~(1 << 5); /* clear config */
484 prev |= enable << 5; /* set bool */
485 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS1, prev); /* write control status1 */
486 if (res != 0) /* check result */
487 {
488 handle->debug_print("pcf8563: write control status1 failed.\n"); /* write control status1 failed */
489
490 return 1; /* return error */
491 }
492
493 return 0; /* success return 0 */
494}
495
508{
509 uint8_t res;
510 uint8_t prev;
511
512 if (handle == NULL) /* check handle */
513 {
514 return 2; /* return error */
515 }
516 if (handle->inited != 1) /* check handle initialization */
517 {
518 return 3; /* return error */
519 }
520
521 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
522 if (res != 0) /* check result */
523 {
524 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
525
526 return 1; /* return error */
527 }
528 *enable = (pcf8563_bool_t)(((prev >> 5) & 0x01)); /* get enable */
529
530 return 0; /* success return 0 */
531}
532
545{
546 uint8_t res;
547 uint8_t prev;
548
549 if (handle == NULL) /* check handle */
550 {
551 return 2; /* return error */
552 }
553 if (handle->inited != 1) /* check handle initialization */
554 {
555 return 3; /* return error */
556 }
557
558 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
559 if (res != 0) /* check result */
560 {
561 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
562
563 return 1; /* return error */
564 }
565 prev &= ~(1 << 6); /* clear unused */
566 prev &= ~(1 << 4); /* clear unused */
567 prev &= ~(7 << 0); /* clear unused */
568 prev &= ~(1 << 3); /* clear config */
569 prev |= enable << 3; /* set bool */
570 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS1, prev); /* write control status1 */
571 if (res != 0) /* check result */
572 {
573 handle->debug_print("pcf8563: write control status1 failed.\n"); /* write control status1 failed */
574
575 return 1; /* return error */
576 }
577
578 return 0; /* success return 0 */
579}
580
593{
594 uint8_t res;
595 uint8_t prev;
596
597 if (handle == NULL) /* check handle */
598 {
599 return 2; /* return error */
600 }
601 if (handle->inited != 1) /* check handle initialization */
602 {
603 return 3; /* return error */
604 }
605
606 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
607 if (res != 0) /* check result */
608 {
609 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
610
611 return 1; /* return error */
612 }
613 *enable = (pcf8563_bool_t)(((prev >> 3) & 0x01)); /* get enable */
614
615 return 0; /* success return 0 */
616}
617
630{
631 uint8_t res;
632 uint8_t prev;
633
634 if (handle == NULL) /* check handle */
635 {
636 return 2; /* return error */
637 }
638 if (handle->inited != 1) /* check handle initialization */
639 {
640 return 3; /* return error */
641 }
642
643 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
644 if (res != 0) /* check result */
645 {
646 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
647
648 return 1; /* return error */
649 }
650 prev &= ~(1 << 7); /* clear config */
651 prev |= (!enable) << 7; /* set bool */
652 res = a_pcf8563_iic_write(handle, PCF8563_REG_MINUTE_ALARM, prev); /* write minute alarm */
653 if (res != 0) /* check result */
654 {
655 handle->debug_print("pcf8563: write minute alarm failed.\n"); /* write minute alarm failed */
656
657 return 1; /* return error */
658 }
659
660 return 0; /* success return 0 */
661}
662
675{
676 uint8_t res;
677 uint8_t prev;
678
679 if (handle == NULL) /* check handle */
680 {
681 return 2; /* return error */
682 }
683 if (handle->inited != 1) /* check handle initialization */
684 {
685 return 3; /* return error */
686 }
687
688 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
689 if (res != 0) /* check result */
690 {
691 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
692
693 return 1; /* return error */
694 }
695 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
696
697 return 0; /* success return 0 */
698}
699
712uint8_t pcf8563_set_minute_alarm(pcf8563_handle_t *handle, uint8_t minute)
713{
714 uint8_t res;
715 uint8_t prev;
716
717 if (handle == NULL) /* check handle */
718 {
719 return 2; /* return error */
720 }
721 if (handle->inited != 1) /* check handle initialization */
722 {
723 return 3; /* return error */
724 }
725 if (minute > 59) /* check minute */
726 {
727 handle->debug_print("pcf8563: minute can't be over than 59.\n"); /* minute can't be over than 59 */
728
729 return 4; /* return error */
730 }
731
732 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
733 if (res != 0) /* check result */
734 {
735 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
736
737 return 1; /* return error */
738 }
739 prev &= ~(0x7F << 0); /* clear config */
740 prev |= a_pcf8563_hex2bcd(minute); /* set config */
741 res = a_pcf8563_iic_write(handle, PCF8563_REG_MINUTE_ALARM, prev); /* write minute alarm */
742 if (res != 0) /* check result */
743 {
744 handle->debug_print("pcf8563: write minute alarm failed.\n"); /* write minute alarm failed */
745
746 return 1; /* return error */
747 }
748
749 return 0; /* success return 0 */
750}
751
763uint8_t pcf8563_get_minute_alarm(pcf8563_handle_t *handle, uint8_t *minute)
764{
765 uint8_t res;
766 uint8_t prev;
767
768 if (handle == NULL) /* check handle */
769 {
770 return 2; /* return error */
771 }
772 if (handle->inited != 1) /* check handle initialization */
773 {
774 return 3; /* return error */
775 }
776
777 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
778 if (res != 0) /* check result */
779 {
780 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
781
782 return 1; /* return error */
783 }
784 *minute = a_pcf8563_bcd2hex(prev & 0x7F); /* convert */
785
786 return 0; /* success return 0 */
787}
788
801{
802 uint8_t res;
803 uint8_t prev;
804
805 if (handle == NULL) /* check handle */
806 {
807 return 2; /* return error */
808 }
809 if (handle->inited != 1) /* check handle initialization */
810 {
811 return 3; /* return error */
812 }
813
814 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
815 if (res != 0) /* check result */
816 {
817 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
818
819 return 1; /* return error */
820 }
821 prev &= ~(1 << 7); /* clear config */
822 prev |= (!enable) << 7; /* set bool */
823 res = a_pcf8563_iic_write(handle, PCF8563_REG_HOUR_ALARM, prev); /* write hour alarm */
824 if (res != 0) /* check result */
825 {
826 handle->debug_print("pcf8563: write hour alarm failed.\n"); /* write hour alarm failed */
827
828 return 1; /* return error */
829 }
830
831 return 0; /* success return 0 */
832}
833
846{
847 uint8_t res;
848 uint8_t prev;
849
850 if (handle == NULL) /* check handle */
851 {
852 return 2; /* return error */
853 }
854 if (handle->inited != 1) /* check handle initialization */
855 {
856 return 3; /* return error */
857 }
858
859 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
860 if (res != 0) /* check result */
861 {
862 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
863
864 return 1; /* return error */
865 }
866 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
867
868 return 0; /* success return 0 */
869}
870
883uint8_t pcf8563_set_hour_alarm(pcf8563_handle_t *handle, uint8_t hour)
884{
885 uint8_t res;
886 uint8_t prev;
887
888 if (handle == NULL) /* check handle */
889 {
890 return 2; /* return error */
891 }
892 if (handle->inited != 1) /* check handle initialization */
893 {
894 return 3; /* return error */
895 }
896 if (hour > 23) /* check hour */
897 {
898 handle->debug_print("pcf8563: hour can't be over than 23.\n"); /* hour can't be over than 23 */
899
900 return 4; /* return error */
901 }
902
903 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
904 if (res != 0) /* check result */
905 {
906 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
907
908 return 1; /* return error */
909 }
910 prev &= ~(0x3F << 0); /* clear config */
911 prev |= a_pcf8563_hex2bcd(hour); /* set config */
912 res = a_pcf8563_iic_write(handle, PCF8563_REG_HOUR_ALARM, prev); /* write hour alarm */
913 if (res != 0) /* check result */
914 {
915 handle->debug_print("pcf8563: write hour alarm failed.\n"); /* write hour alarm failed */
916
917 return 1; /* return error */
918 }
919
920 return 0; /* success return 0 */
921}
922
934uint8_t pcf8563_get_hour_alarm(pcf8563_handle_t *handle, uint8_t *hour)
935{
936 uint8_t res;
937 uint8_t prev;
938
939 if (handle == NULL) /* check handle */
940 {
941 return 2; /* return error */
942 }
943 if (handle->inited != 1) /* check handle initialization */
944 {
945 return 3; /* return error */
946 }
947
948 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
949 if (res != 0) /* check result */
950 {
951 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
952
953 return 1; /* return error */
954 }
955 *hour = a_pcf8563_bcd2hex(prev & 0x3F); /* convert */
956
957 return 0; /* success return 0 */
958}
959
972{
973 uint8_t res;
974 uint8_t prev;
975
976 if (handle == NULL) /* check handle */
977 {
978 return 2; /* return error */
979 }
980 if (handle->inited != 1) /* check handle initialization */
981 {
982 return 3; /* return error */
983 }
984
985 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
986 if (res != 0) /* check result */
987 {
988 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
989
990 return 1; /* return error */
991 }
992 prev &= ~(1 << 7); /* clear config */
993 prev |= (!enable) << 7; /* set bool */
994 res = a_pcf8563_iic_write(handle, PCF8563_REG_DAY_ALARM, prev); /* write day alarm */
995 if (res != 0) /* check result */
996 {
997 handle->debug_print("pcf8563: write day alarm failed.\n"); /* write day alarm failed */
998
999 return 1; /* return error */
1000 }
1001
1002 return 0; /* success return 0 */
1003}
1004
1017{
1018 uint8_t res;
1019 uint8_t prev;
1020
1021 if (handle == NULL) /* check handle */
1022 {
1023 return 2; /* return error */
1024 }
1025 if (handle->inited != 1) /* check handle initialization */
1026 {
1027 return 3; /* return error */
1028 }
1029
1030 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
1031 if (res != 0) /* check result */
1032 {
1033 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
1034
1035 return 1; /* return error */
1036 }
1037 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
1038
1039 return 0; /* success return 0 */
1040}
1041
1054uint8_t pcf8563_set_day_alarm(pcf8563_handle_t *handle, uint8_t day)
1055{
1056 uint8_t res;
1057 uint8_t prev;
1058
1059 if (handle == NULL) /* check handle */
1060 {
1061 return 2; /* return error */
1062 }
1063 if (handle->inited != 1) /* check handle initialization */
1064 {
1065 return 3; /* return error */
1066 }
1067 if ((day == 0) || (day > 31)) /* check day */
1068 {
1069 handle->debug_print("pcf8563: day can't be zero or over than 31.\n"); /* day can't be zero or over than 31 */
1070
1071 return 4; /* return error */
1072 }
1073
1074 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
1075 if (res != 0) /* check result */
1076 {
1077 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
1078
1079 return 1; /* return error */
1080 }
1081 prev &= ~(0x3F << 0); /* clear config */
1082 prev |= a_pcf8563_hex2bcd(day); /* set config */
1083 res = a_pcf8563_iic_write(handle, PCF8563_REG_DAY_ALARM, prev); /* write day alarm */
1084 if (res != 0) /* check result */
1085 {
1086 handle->debug_print("pcf8563: write day alarm failed.\n"); /* write day alarm failed */
1087
1088 return 1; /* return error */
1089 }
1090
1091 return 0; /* success return 0 */
1092}
1093
1105uint8_t pcf8563_get_day_alarm(pcf8563_handle_t *handle, uint8_t *day)
1106{
1107 uint8_t res;
1108 uint8_t prev;
1109
1110 if (handle == NULL) /* check handle */
1111 {
1112 return 2; /* return error */
1113 }
1114 if (handle->inited != 1) /* check handle initialization */
1115 {
1116 return 3; /* return error */
1117 }
1118
1119 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
1120 if (res != 0) /* check result */
1121 {
1122 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
1123
1124 return 1; /* return error */
1125 }
1126 *day = a_pcf8563_bcd2hex(prev & 0x3F); /* convert */
1127
1128 return 0; /* success return 0 */
1129}
1130
1143{
1144 uint8_t res;
1145 uint8_t prev;
1146
1147 if (handle == NULL) /* check handle */
1148 {
1149 return 2; /* return error */
1150 }
1151 if (handle->inited != 1) /* check handle initialization */
1152 {
1153 return 3; /* return error */
1154 }
1155
1156 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1157 if (res != 0) /* check result */
1158 {
1159 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1160
1161 return 1; /* return error */
1162 }
1163 prev &= ~(1 << 7); /* clear config */
1164 prev |= (!enable) << 7; /* set bool */
1165 res = a_pcf8563_iic_write(handle, PCF8563_REG_WEEK_ALARM, prev); /* write week alarm */
1166 if (res != 0) /* check result */
1167 {
1168 handle->debug_print("pcf8563: write week alarm failed.\n"); /* write week alarm failed */
1169
1170 return 1; /* return error */
1171 }
1172
1173 return 0; /* success return 0 */
1174}
1175
1188{
1189 uint8_t res;
1190 uint8_t prev;
1191
1192 if (handle == NULL) /* check handle */
1193 {
1194 return 2; /* return error */
1195 }
1196 if (handle->inited != 1) /* check handle initialization */
1197 {
1198 return 3; /* return error */
1199 }
1200
1201 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1202 if (res != 0) /* check result */
1203 {
1204 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1205
1206 return 1; /* return error */
1207 }
1208 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
1209
1210 return 0; /* success return 0 */
1211}
1212
1225uint8_t pcf8563_set_week_alarm(pcf8563_handle_t *handle, uint8_t week)
1226{
1227 uint8_t res;
1228 uint8_t prev;
1229
1230 if (handle == NULL) /* check handle */
1231 {
1232 return 2; /* return error */
1233 }
1234 if (handle->inited != 1) /* check handle initialization */
1235 {
1236 return 3; /* return error */
1237 }
1238 if (week > 6) /* check week */
1239 {
1240 handle->debug_print("pcf8563: week can't be over than 6.\n"); /* week can't be over than 6 */
1241
1242 return 4; /* return error */
1243 }
1244
1245 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1246 if (res != 0) /* check result */
1247 {
1248 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1249
1250 return 1; /* return error */
1251 }
1252 prev &= ~(0x7 << 0); /* clear config */
1253 prev |= a_pcf8563_hex2bcd(week); /* set config */
1254 res = a_pcf8563_iic_write(handle, PCF8563_REG_WEEK_ALARM, prev); /* write week alarm */
1255 if (res != 0) /* check result */
1256 {
1257 handle->debug_print("pcf8563: write week alarm failed.\n"); /* write week alarm failed */
1258
1259 return 1; /* return error */
1260 }
1261
1262 return 0; /* success return 0 */
1263}
1264
1276uint8_t pcf8563_get_week_alarm(pcf8563_handle_t *handle, uint8_t *week)
1277{
1278 uint8_t res;
1279 uint8_t prev;
1280
1281 if (handle == NULL) /* check handle */
1282 {
1283 return 2; /* return error */
1284 }
1285 if (handle->inited != 1) /* check handle initialization */
1286 {
1287 return 3; /* return error */
1288 }
1289
1290 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1291 if (res != 0) /* check result */
1292 {
1293 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1294
1295 return 1; /* return error */
1296 }
1297 *week = a_pcf8563_bcd2hex(prev & 0x07); /* convert */
1298
1299 return 0; /* success return 0 */
1300}
1301
1314{
1315 uint8_t res;
1316 uint8_t prev;
1317
1318 if (handle == NULL) /* check handle */
1319 {
1320 return 2; /* return error */
1321 }
1322 if (handle->inited != 1) /* check handle initialization */
1323 {
1324 return 3; /* return error */
1325 }
1326
1327 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1328 if (res != 0) /* check result */
1329 {
1330 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1331
1332 return 1; /* return error */
1333 }
1334 prev &= ~(1 << 7); /* clear config */
1335 prev |= enable << 7; /* set bool */
1336 res = a_pcf8563_iic_write(handle, PCF8563_REG_CLKOUT_CONTROL, prev); /* write clkout control */
1337 if (res != 0) /* check result */
1338 {
1339 handle->debug_print("pcf8563: write clkout control failed.\n"); /* write clkout control failed */
1340
1341 return 1; /* return error */
1342 }
1343
1344 return 0; /* success return 0 */
1345}
1346
1359{
1360 uint8_t res;
1361 uint8_t prev;
1362
1363 if (handle == NULL) /* check handle */
1364 {
1365 return 2; /* return error */
1366 }
1367 if (handle->inited != 1) /* check handle initialization */
1368 {
1369 return 3; /* return error */
1370 }
1371
1372 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1373 if (res != 0) /* check result */
1374 {
1375 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1376
1377 return 1; /* return error */
1378 }
1379 *enable = (pcf8563_bool_t)((prev >> 7) & 0x01); /* get bool */
1380
1381 return 0; /* success return 0 */
1382}
1383
1396{
1397 uint8_t res;
1398 uint8_t prev;
1399
1400 if (handle == NULL) /* check handle */
1401 {
1402 return 2; /* return error */
1403 }
1404 if (handle->inited != 1) /* check handle initialization */
1405 {
1406 return 3; /* return error */
1407 }
1408
1409 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1410 if (res != 0) /* check result */
1411 {
1412 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1413
1414 return 1; /* return error */
1415 }
1416 prev &= ~(3 << 0); /* clear config */
1417 prev |= clk << 0; /* set config */
1418 res = a_pcf8563_iic_write(handle, PCF8563_REG_CLKOUT_CONTROL, prev); /* write clkout control */
1419 if (res != 0) /* check result */
1420 {
1421 handle->debug_print("pcf8563: write clkout control failed.\n"); /* write clkout control failed */
1422
1423 return 1; /* return error */
1424 }
1425
1426 return 0; /* success return 0 */
1427}
1428
1441{
1442 uint8_t res;
1443 uint8_t prev;
1444
1445 if (handle == NULL) /* check handle */
1446 {
1447 return 2; /* return error */
1448 }
1449 if (handle->inited != 1) /* check handle initialization */
1450 {
1451 return 3; /* return error */
1452 }
1453
1454 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1455 if (res != 0) /* check result */
1456 {
1457 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1458
1459 return 1; /* return error */
1460 }
1461 *clk = (pcf8563_clock_out_t)(prev & 0x03); /* get clock */
1462
1463 return 0; /* success return 0 */
1464}
1465
1478{
1479 uint8_t res;
1480 uint8_t prev;
1481
1482 if (handle == NULL) /* check handle */
1483 {
1484 return 2; /* return error */
1485 }
1486 if (handle->inited != 1) /* check handle initialization */
1487 {
1488 return 3; /* return error */
1489 }
1490
1491 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1492 if (res != 0) /* check result */
1493 {
1494 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1495
1496 return 1; /* return error */
1497 }
1498 prev &= ~(1 << 7); /* clear config */
1499 prev |= enable << 7; /* set bool */
1500 res = a_pcf8563_iic_write(handle, PCF8563_REG_TIMER_CONTROL, prev); /* write timer control */
1501 if (res != 0) /* check result */
1502 {
1503 handle->debug_print("pcf8563: write timer control failed.\n"); /* write timer control failed */
1504
1505 return 1; /* return error */
1506 }
1507
1508 return 0; /* success return 0 */
1509}
1510
1523{
1524 uint8_t res;
1525 uint8_t prev;
1526
1527 if (handle == NULL) /* check handle */
1528 {
1529 return 2; /* return error */
1530 }
1531 if (handle->inited != 1) /* check handle initialization */
1532 {
1533 return 3; /* return error */
1534 }
1535
1536 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1537 if (res != 0) /* check result */
1538 {
1539 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1540
1541 return 1; /* return error */
1542 }
1543 *enable = (pcf8563_bool_t)((prev >> 7) & 0x01); /* get bool */
1544
1545 return 0; /* success return 0 */
1546}
1547
1560{
1561 uint8_t res;
1562 uint8_t prev;
1563
1564 if (handle == NULL) /* check handle */
1565 {
1566 return 2; /* return error */
1567 }
1568 if (handle->inited != 1) /* check handle initialization */
1569 {
1570 return 3; /* return error */
1571 }
1572
1573 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1574 if (res != 0) /* check result */
1575 {
1576 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1577
1578 return 1; /* return error */
1579 }
1580 prev &= ~(3 << 0); /* clear config */
1581 prev |= freq << 0; /* set freq */
1582 res = a_pcf8563_iic_write(handle, PCF8563_REG_TIMER_CONTROL, prev); /* write timer control */
1583 if (res != 0) /* check result */
1584 {
1585 handle->debug_print("pcf8563: write timer control failed.\n"); /* write timer control failed */
1586
1587 return 1; /* return error */
1588 }
1589
1590 return 0; /* success return 0 */
1591}
1592
1605{
1606 uint8_t res;
1607 uint8_t prev;
1608
1609 if (handle == NULL) /* check handle */
1610 {
1611 return 2; /* return error */
1612 }
1613 if (handle->inited != 1) /* check handle initialization */
1614 {
1615 return 3; /* return error */
1616 }
1617
1618 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1619 if (res != 0) /* check result */
1620 {
1621 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1622
1623 return 1; /* return error */
1624 }
1625 *freq = (pcf8563_timer_freq_t)(prev & 0x03); /* get freq */
1626
1627 return 0; /* success return 0 */
1628}
1629
1641uint8_t pcf8563_set_timer_value(pcf8563_handle_t *handle, uint8_t value)
1642{
1643 uint8_t res;
1644 uint8_t prev;
1645
1646 if (handle == NULL) /* check handle */
1647 {
1648 return 2; /* return error */
1649 }
1650 if (handle->inited != 1) /* check handle initialization */
1651 {
1652 return 3; /* return error */
1653 }
1654
1655 prev = value; /* set value */
1656 res = a_pcf8563_iic_write(handle, PCF8563_REG_TIMER, prev); /* write timer */
1657 if (res != 0) /* check result */
1658 {
1659 handle->debug_print("pcf8563: write timer failed.\n"); /* write timer failed */
1660
1661 return 1; /* return error */
1662 }
1663
1664 return 0; /* success return 0 */
1665}
1666
1678uint8_t pcf8563_get_timer_value(pcf8563_handle_t *handle, uint8_t *value)
1679{
1680 uint8_t res;
1681
1682 if (handle == NULL) /* check handle */
1683 {
1684 return 2; /* return error */
1685 }
1686 if (handle->inited != 1) /* check handle initialization */
1687 {
1688 return 3; /* return error */
1689 }
1690
1691 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER, value, 1); /* read timer */
1692 if (res != 0) /* check result */
1693 {
1694 handle->debug_print("pcf8563: read timer failed.\n"); /* read timer failed */
1695
1696 return 1; /* return error */
1697 }
1698
1699 return 0; /* success return 0 */
1700}
1701
1714{
1715 uint8_t res;
1716 uint8_t prev;
1717
1718 if (handle == NULL) /* check handle */
1719 {
1720 return 2; /* return error */
1721 }
1722 if (handle->inited != 1) /* check handle initialization */
1723 {
1724 return 3; /* return error */
1725 }
1726
1727 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1728 if (res != 0) /* check result */
1729 {
1730 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1731
1732 return 1; /* return error */
1733 }
1734 prev &= ~(7 << 5); /* clear unused */
1735 prev &= ~(1 << 0); /* clear config */
1736 prev |= enable << 0; /* set bool */
1737 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
1738 if (res != 0) /* check result */
1739 {
1740 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1741
1742 return 1; /* return error */
1743 }
1744
1745 return 0; /* success return 0 */
1746}
1747
1760{
1761 uint8_t res;
1762 uint8_t prev;
1763
1764 if (handle == NULL) /* check handle */
1765 {
1766 return 2; /* return error */
1767 }
1768 if (handle->inited != 1) /* check handle initialization */
1769 {
1770 return 3; /* return error */
1771 }
1772
1773 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1774 if (res != 0) /* check result */
1775 {
1776 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1777
1778 return 1; /* return error */
1779 }
1780 *enable = (pcf8563_bool_t)((prev >> 0) & 0x01); /* get bool */
1781
1782 return 0; /* success return 0 */
1783}
1784
1797{
1798 uint8_t res;
1799 uint8_t prev;
1800
1801 if (handle == NULL) /* check handle */
1802 {
1803 return 2; /* return error */
1804 }
1805 if (handle->inited != 1) /* check handle initialization */
1806 {
1807 return 3; /* return error */
1808 }
1809
1810 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1811 if (res != 0) /* check result */
1812 {
1813 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1814
1815 return 1; /* return error */
1816 }
1817 prev &= ~(7 << 5); /* clear unused */
1818 prev &= ~(1 << 1); /* clear config */
1819 prev |= enable << 1; /* set bool */
1820 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
1821 if (res != 0) /* check result */
1822 {
1823 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1824
1825 return 1; /* return error */
1826 }
1827
1828 return 0; /* success return 0 */
1829}
1830
1843{
1844 uint8_t res;
1845 uint8_t prev;
1846
1847 if (handle == NULL) /* check handle */
1848 {
1849 return 2; /* return error */
1850 }
1851 if (handle->inited != 1) /* check handle initialization */
1852 {
1853 return 3; /* return error */
1854 }
1855
1856 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1857 if (res != 0) /* check result */
1858 {
1859 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1860
1861 return 1; /* return error */
1862 }
1863 *enable = (pcf8563_bool_t)((prev >> 1) & 0x01); /* get bool */
1864
1865 return 0; /* success return 0 */
1866}
1867
1880{
1881 uint8_t res;
1882 uint8_t prev;
1883
1884 if (handle == NULL) /* check handle */
1885 {
1886 return 2; /* return error */
1887 }
1888 if (handle->inited != 1) /* check handle initialization */
1889 {
1890 return 3; /* return error */
1891 }
1892
1893 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1894 if (res != 0) /* check result */
1895 {
1896 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1897
1898 return 1; /* return error */
1899 }
1900 prev &= ~(7 << 5); /* clear unused */
1901 prev &= ~(1 << 4); /* clear config */
1902 prev |= mode << 4; /* set mode */
1903 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
1904 if (res != 0) /* check result */
1905 {
1906 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1907
1908 return 1; /* return error */
1909 }
1910
1911 return 0; /* success return 0 */
1912}
1913
1926{
1927 uint8_t res;
1928 uint8_t prev;
1929
1930 if (handle == NULL) /* check handle */
1931 {
1932 return 2; /* return error */
1933 }
1934 if (handle->inited != 1) /* check handle initialization */
1935 {
1936 return 3; /* return error */
1937 }
1938
1939 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1940 if (res != 0) /* check result */
1941 {
1942 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1943
1944 return 1; /* return error */
1945 }
1946 *mode = (pcf8563_interrupt_mode_t)((prev >> 4) & 0x1); /* get mode */
1947
1948 return 0; /* success return 0 */
1949}
1950
1962{
1963 uint8_t res;
1964 uint8_t prev;
1965 uint8_t prev2;
1966
1967 if (handle == NULL) /* check handle */
1968 {
1969 return 2; /* return error */
1970 }
1971 if (handle->inited != 1) /* check handle initialization */
1972 {
1973 return 3; /* return error */
1974 }
1975
1976 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1977 if (res != 0) /* check result */
1978 {
1979 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1980
1981 return 1; /* return error */
1982 }
1983 prev2 = prev; /* save config */
1984 prev2 &= ~(3 << 2); /* clear settings */
1985 if (((prev >> 2) & 0x1) != 0) /* check timer */
1986 {
1987 if (handle->receive_callback != NULL) /* if not null */
1988 {
1989 handle->receive_callback(PCF8563_INTERRUPT_EVENT_TIMER); /* run the callback */
1990 }
1991 }
1992 if (((prev >> 3) & 0x01) != 0) /* check alarm */
1993 {
1994 if (handle->receive_callback != NULL) /* if not null */
1995 {
1996 handle->receive_callback(PCF8563_INTERRUPT_EVENT_ALARM); /* run the callback */
1997 }
1998 }
1999 prev &= ~(7 << 5); /* clear unused */
2000 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev2); /* write control status2 */
2001 if (res != 0) /* check result */
2002 {
2003 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
2004
2005 return 1; /* return error */
2006 }
2007
2008 return 0; /* success return 0 */
2009}
2010
2023{
2024 uint8_t res;
2025 uint8_t prev;
2026
2027 if (handle == NULL) /* check handle */
2028 {
2029 return 2; /* return error */
2030 }
2031 if (handle->inited != 1) /* check handle initialization */
2032 {
2033 return 3; /* return error */
2034 }
2035
2036 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
2037 if (res != 0) /* check result */
2038 {
2039 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
2040
2041 return 1; /* return error */
2042 }
2043 if (event == PCF8563_INTERRUPT_EVENT_TIMER) /* timer */
2044 {
2045 prev &= ~(1 << 2); /* clear bit */
2046 }
2047 else /* alarm */
2048 {
2049 prev &= ~(1 << 3); /* clear bit */
2050 }
2051 prev &= ~(7 << 5); /* clear unused */
2052 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
2053 if (res != 0) /* check result */
2054 {
2055 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
2056
2057 return 1; /* return error */
2058 }
2059
2060 return 0; /* success return 0 */
2061}
2062
2074{
2075 if (handle == NULL) /* check handle */
2076 {
2077 return 2; /* return error */
2078 }
2079 if (handle->debug_print == NULL) /* check debug_print */
2080 {
2081 return 3; /* return error */
2082 }
2083 if (handle->iic_init == NULL) /* check iic_init */
2084 {
2085 handle->debug_print("pcf8563: iic_init is null.\n"); /* iic_init is null */
2086
2087 return 3; /* return error */
2088 }
2089 if (handle->iic_deinit == NULL) /* check iic_deinit */
2090 {
2091 handle->debug_print("pcf8563: iic_deinit is null.\n"); /* iic_deinit is null */
2092
2093 return 3; /* return error */
2094 }
2095 if (handle->iic_write == NULL) /* check iic_write */
2096 {
2097 handle->debug_print("pcf8563: iic_write is null.\n"); /* iic_write is null */
2098
2099 return 3; /* return error */
2100 }
2101 if (handle->iic_read == NULL) /* check iic_read */
2102 {
2103 handle->debug_print("pcf8563: iic_read is null.\n"); /* iic_read is null */
2104
2105 return 3; /* return error */
2106 }
2107 if (handle->delay_ms == NULL) /* check delay_ms */
2108 {
2109 handle->debug_print("pcf8563: delay_ms is null.\n"); /* delay_ms is null */
2110
2111 return 3; /* return error */
2112 }
2113 if (handle->receive_callback == NULL) /* check receive_callback */
2114 {
2115 handle->debug_print("pcf8563: receive_callback is null.\n"); /* receive_callback is null */
2116
2117 return 3; /* return error */
2118 }
2119
2120 if (handle->iic_init() != 0) /* iic init */
2121 {
2122 handle->debug_print("pcf8563: iic init failed.\n"); /* iic init failed */
2123
2124 return 1; /* return error */
2125 }
2126 handle->inited = 1; /* flag finish initialization */
2127
2128 return 0; /* success return 0 */
2129}
2130
2142{
2143 if (handle == NULL) /* check handle */
2144 {
2145 return 2; /* return error */
2146 }
2147 if (handle->inited != 1) /* check handle initialization */
2148 {
2149 return 3; /* return error */
2150 }
2151
2152 if (handle->iic_deinit() != 0) /* iic deinit */
2153 {
2154 handle->debug_print("pcf8563: iic deinit failed.\n"); /* iic deinit failed */
2155
2156 return 1; /* return error */
2157 }
2158 handle->inited = 0; /* flag close */
2159
2160 return 0; /* success return 0 */
2161}
2162
2176uint8_t pcf8563_set_reg(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2177{
2178 if (handle == NULL) /* check handle */
2179 {
2180 return 2; /* return error */
2181 }
2182 if (handle->inited != 1) /* check handle initialization */
2183 {
2184 return 3; /* return error */
2185 }
2186
2187 if (handle->iic_write(PCF8563_ADDRESS, reg, buf, len) != 0) /* write data */
2188 {
2189 return 1; /* return error */
2190 }
2191
2192 return 0; /* success return 0 */
2193}
2194
2208uint8_t pcf8563_get_reg(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2209{
2210 if (handle == NULL) /* check handle */
2211 {
2212 return 2; /* return error */
2213 }
2214 if (handle->inited != 1) /* check handle initialization */
2215 {
2216 return 3; /* return error */
2217 }
2218
2219 if (handle->iic_read(PCF8563_ADDRESS, reg, buf, len) != 0) /* read data */
2220 {
2221 return 1; /* return error */
2222 }
2223
2224 return 0; /* success return 0 */
2225}
2226
2236{
2237 if (info == NULL) /* check handle */
2238 {
2239 return 2; /* return error */
2240 }
2241
2242 memset(info, 0, sizeof(pcf8563_info_t)); /* initialize pcf8563 info structure */
2243 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2244 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2245 strncpy(info->interface, "IIC", 8); /* copy interface name */
2246 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2247 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2248 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2249 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2250 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2251 info->driver_version = DRIVER_VERSION; /* set driver version */
2252
2253 return 0; /* success return 0 */
2254}
#define PCF8563_REG_MINUTE_ALARM
#define PCF8563_REG_TIMER
#define PCF8563_REG_DAY_ALARM
#define MAX_CURRENT
#define PCF8563_ADDRESS
chip address definition
#define PCF8563_REG_CONTROL_STATUS2
#define SUPPLY_VOLTAGE_MAX
#define PCF8563_REG_MONTH
#define PCF8563_REG_YEAR
#define TEMPERATURE_MAX
#define PCF8563_REG_MINUTE
#define PCF8563_REG_CONTROL_STATUS1
chip register definition
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define PCF8563_REG_SECOND
#define PCF8563_REG_HOUR_ALARM
#define PCF8563_REG_HOUR
#define PCF8563_REG_DAY
#define PCF8563_REG_TIMER_CONTROL
#define PCF8563_REG_WEEK
#define CHIP_NAME
chip information definition
#define PCF8563_REG_CLKOUT_CONTROL
#define DRIVER_VERSION
#define PCF8563_REG_WEEK_ALARM
driver pcf8563 header file
uint8_t pcf8563_info(pcf8563_info_t *info)
get chip's information
uint8_t pcf8563_set_week_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable week alarm
uint8_t pcf8563_set_hour_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable hour alarm
uint8_t pcf8563_set_timer_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable timer enable
pcf8563_timer_freq_t
pcf8563 timer freq enumeration definition
uint8_t pcf8563_set_day_alarm(pcf8563_handle_t *handle, uint8_t day)
set day alarm
uint8_t pcf8563_get_week_alarm(pcf8563_handle_t *handle, uint8_t *week)
get week alarm
uint8_t pcf8563_deinit(pcf8563_handle_t *handle)
close the chip
uint8_t pcf8563_get_minute_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the minute alarm status
pcf8563_bool_t
pcf8563 bool enumeration definition
uint8_t pcf8563_get_rtc_stop(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the rtc stop status
uint8_t pcf8563_set_clock_out(pcf8563_handle_t *handle, pcf8563_clock_out_t clk)
set clock out
uint8_t pcf8563_set_hour_alarm(pcf8563_handle_t *handle, uint8_t hour)
set hour alarm
uint8_t pcf8563_get_interrupt_mode(pcf8563_handle_t *handle, pcf8563_interrupt_mode_t *mode)
get the interrupt mode
uint8_t pcf8563_get_hour_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the hour alarm status
uint8_t pcf8563_get_alarm_interrupt(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get alarm interrupt status
uint8_t pcf8563_get_timer_freq(pcf8563_handle_t *handle, pcf8563_timer_freq_t *freq)
get timer freq
uint8_t pcf8563_set_timer_interrupt(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable timer interrupt
uint8_t pcf8563_set_test_mode(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable test mode
uint8_t pcf8563_set_minute_alarm(pcf8563_handle_t *handle, uint8_t minute)
set minute alarm
uint8_t pcf8563_set_timer_freq(pcf8563_handle_t *handle, pcf8563_timer_freq_t freq)
set timer freq
uint8_t pcf8563_set_clock_out_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable clock out enable
pcf8563_interrupt_mode_t
pcf8563 interrupt mode enumeration definition
uint8_t pcf8563_init(pcf8563_handle_t *handle)
initialize the chip
struct pcf8563_info_s pcf8563_info_t
pcf8563 information structure definition
uint8_t pcf8563_get_hour_alarm(pcf8563_handle_t *handle, uint8_t *hour)
get hour alarm
uint8_t pcf8563_get_clock_out_enable(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get clock out enable status
pcf8563_clock_out_t
pcf8563 clock out enumeration definition
uint8_t pcf8563_get_timer_interrupt(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get timer interrupt status
uint8_t pcf8563_set_time(pcf8563_handle_t *handle, pcf8563_time_t *t)
set the current time
uint8_t pcf8563_get_day_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the day alarm status
uint8_t pcf8563_get_minute_alarm(pcf8563_handle_t *handle, uint8_t *minute)
get minute alarm
struct pcf8563_time_s pcf8563_time_t
pcf8563 time structure definition
uint8_t pcf8563_get_day_alarm(pcf8563_handle_t *handle, uint8_t *day)
get day alarm
uint8_t pcf8563_get_clock_out(pcf8563_handle_t *handle, pcf8563_clock_out_t *clk)
get clock out
uint8_t pcf8563_get_timer_value(pcf8563_handle_t *handle, uint8_t *value)
get timer value
uint8_t pcf8563_irq_handler(pcf8563_handle_t *handle)
irq handler
uint8_t pcf8563_set_alarm_interrupt(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable alarm interrupt
uint8_t pcf8563_set_week_alarm(pcf8563_handle_t *handle, uint8_t week)
set week alarm
uint8_t pcf8563_set_day_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable day alarm
uint8_t pcf8563_set_minute_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable minute alarm
uint8_t pcf8563_set_rtc_stop(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable rtc stop
struct pcf8563_handle_s pcf8563_handle_t
pcf8563 handle structure definition
uint8_t pcf8563_set_interrupt_mode(pcf8563_handle_t *handle, pcf8563_interrupt_mode_t mode)
set the interrupt mode
uint8_t pcf8563_get_power_on_reset(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the power on reset status
pcf8563_interrupt_event_t
pcf8563 interrupt event enumeration definition
uint8_t pcf8563_get_timer_enable(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get timer enable status
uint8_t pcf8563_set_timer_value(pcf8563_handle_t *handle, uint8_t value)
set timer value
uint8_t pcf8563_get_week_alarm_enable(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the week alarm status
uint8_t pcf8563_get_time(pcf8563_handle_t *handle, pcf8563_time_t *t)
get the current time
uint8_t pcf8563_get_test_mode(pcf8563_handle_t *handle, pcf8563_bool_t *enable)
get the test mode status
uint8_t pcf8563_clear_status(pcf8563_handle_t *handle, pcf8563_interrupt_event_t event)
clear status
uint8_t pcf8563_set_power_on_reset(pcf8563_handle_t *handle, pcf8563_bool_t enable)
enable or disable power on reset
@ PCF8563_INTERRUPT_EVENT_TIMER
@ PCF8563_INTERRUPT_EVENT_ALARM
uint8_t pcf8563_get_reg(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t pcf8563_set_reg(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set 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)
uint32_t driver_version
char manufacturer_name[32]