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 << 7); /* clear config */
396 prev |= enable << 7; /* set bool */
397 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS1, prev); /* write control status1 */
398 if (res != 0) /* check result */
399 {
400 handle->debug_print("pcf8563: write control status1 failed.\n"); /* write control status1 failed */
401
402 return 1; /* return error */
403 }
404
405 return 0; /* success return 0 */
406}
407
420{
421 uint8_t res;
422 uint8_t prev;
423
424 if (handle == NULL) /* check handle */
425 {
426 return 2; /* return error */
427 }
428 if (handle->inited != 1) /* check handle initialization */
429 {
430 return 3; /* return error */
431 }
432
433 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
434 if (res != 0) /* check result */
435 {
436 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
437
438 return 1; /* return error */
439 }
440 *enable = (pcf8563_bool_t)(((prev >> 7) & 0x01)); /* get enable */
441
442 return 0; /* success return 0 */
443}
444
457{
458 uint8_t res;
459 uint8_t prev;
460
461 if (handle == NULL) /* check handle */
462 {
463 return 2; /* return error */
464 }
465 if (handle->inited != 1) /* check handle initialization */
466 {
467 return 3; /* return error */
468 }
469
470 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
471 if (res != 0) /* check result */
472 {
473 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
474
475 return 1; /* return error */
476 }
477 prev &= ~(1 << 5); /* clear config */
478 prev |= enable << 5; /* set bool */
479 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS1, prev); /* write control status1 */
480 if (res != 0) /* check result */
481 {
482 handle->debug_print("pcf8563: write control status1 failed.\n"); /* write control status1 failed */
483
484 return 1; /* return error */
485 }
486
487 return 0; /* success return 0 */
488}
489
502{
503 uint8_t res;
504 uint8_t prev;
505
506 if (handle == NULL) /* check handle */
507 {
508 return 2; /* return error */
509 }
510 if (handle->inited != 1) /* check handle initialization */
511 {
512 return 3; /* return error */
513 }
514
515 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
516 if (res != 0) /* check result */
517 {
518 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
519
520 return 1; /* return error */
521 }
522 *enable = (pcf8563_bool_t)(((prev >> 5) & 0x01)); /* get enable */
523
524 return 0; /* success return 0 */
525}
526
539{
540 uint8_t res;
541 uint8_t prev;
542
543 if (handle == NULL) /* check handle */
544 {
545 return 2; /* return error */
546 }
547 if (handle->inited != 1) /* check handle initialization */
548 {
549 return 3; /* return error */
550 }
551
552 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
553 if (res != 0) /* check result */
554 {
555 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
556
557 return 1; /* return error */
558 }
559 prev &= ~(1 << 3); /* clear config */
560 prev |= enable << 3; /* set bool */
561 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS1, prev); /* write control status1 */
562 if (res != 0) /* check result */
563 {
564 handle->debug_print("pcf8563: write control status1 failed.\n"); /* write control status1 failed */
565
566 return 1; /* return error */
567 }
568
569 return 0; /* success return 0 */
570}
571
584{
585 uint8_t res;
586 uint8_t prev;
587
588 if (handle == NULL) /* check handle */
589 {
590 return 2; /* return error */
591 }
592 if (handle->inited != 1) /* check handle initialization */
593 {
594 return 3; /* return error */
595 }
596
597 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS1, &prev, 1); /* read control status1 */
598 if (res != 0) /* check result */
599 {
600 handle->debug_print("pcf8563: read control status1 failed.\n"); /* read control status1 failed */
601
602 return 1; /* return error */
603 }
604 *enable = (pcf8563_bool_t)(((prev >> 3) & 0x01)); /* get enable */
605
606 return 0; /* success return 0 */
607}
608
621{
622 uint8_t res;
623 uint8_t prev;
624
625 if (handle == NULL) /* check handle */
626 {
627 return 2; /* return error */
628 }
629 if (handle->inited != 1) /* check handle initialization */
630 {
631 return 3; /* return error */
632 }
633
634 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
635 if (res != 0) /* check result */
636 {
637 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
638
639 return 1; /* return error */
640 }
641 prev &= ~(1 << 7); /* clear config */
642 prev |= (!enable) << 7; /* set bool */
643 res = a_pcf8563_iic_write(handle, PCF8563_REG_MINUTE_ALARM, prev); /* write minute alarm */
644 if (res != 0) /* check result */
645 {
646 handle->debug_print("pcf8563: write minute alarm failed.\n"); /* write minute alarm failed */
647
648 return 1; /* return error */
649 }
650
651 return 0; /* success return 0 */
652}
653
666{
667 uint8_t res;
668 uint8_t prev;
669
670 if (handle == NULL) /* check handle */
671 {
672 return 2; /* return error */
673 }
674 if (handle->inited != 1) /* check handle initialization */
675 {
676 return 3; /* return error */
677 }
678
679 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
680 if (res != 0) /* check result */
681 {
682 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
683
684 return 1; /* return error */
685 }
686 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
687
688 return 0; /* success return 0 */
689}
690
703uint8_t pcf8563_set_minute_alarm(pcf8563_handle_t *handle, uint8_t minute)
704{
705 uint8_t res;
706 uint8_t prev;
707
708 if (handle == NULL) /* check handle */
709 {
710 return 2; /* return error */
711 }
712 if (handle->inited != 1) /* check handle initialization */
713 {
714 return 3; /* return error */
715 }
716 if (minute > 59) /* check minute */
717 {
718 handle->debug_print("pcf8563: minute can't be over than 59.\n"); /* minute can't be over than 59 */
719
720 return 4; /* return error */
721 }
722
723 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
724 if (res != 0) /* check result */
725 {
726 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
727
728 return 1; /* return error */
729 }
730 prev &= ~(0x7F << 0); /* clear config */
731 prev |= a_pcf8563_hex2bcd(minute); /* set config */
732 res = a_pcf8563_iic_write(handle, PCF8563_REG_MINUTE_ALARM, prev); /* write minute alarm */
733 if (res != 0) /* check result */
734 {
735 handle->debug_print("pcf8563: write minute alarm failed.\n"); /* write minute alarm failed */
736
737 return 1; /* return error */
738 }
739
740 return 0; /* success return 0 */
741}
742
754uint8_t pcf8563_get_minute_alarm(pcf8563_handle_t *handle, uint8_t *minute)
755{
756 uint8_t res;
757 uint8_t prev;
758
759 if (handle == NULL) /* check handle */
760 {
761 return 2; /* return error */
762 }
763 if (handle->inited != 1) /* check handle initialization */
764 {
765 return 3; /* return error */
766 }
767
768 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_MINUTE_ALARM, &prev, 1); /* read minute alarm */
769 if (res != 0) /* check result */
770 {
771 handle->debug_print("pcf8563: read minute alarm failed.\n"); /* read minute alarm failed */
772
773 return 1; /* return error */
774 }
775 *minute = a_pcf8563_bcd2hex(prev & 0x7F); /* convert */
776
777 return 0; /* success return 0 */
778}
779
792{
793 uint8_t res;
794 uint8_t prev;
795
796 if (handle == NULL) /* check handle */
797 {
798 return 2; /* return error */
799 }
800 if (handle->inited != 1) /* check handle initialization */
801 {
802 return 3; /* return error */
803 }
804
805 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
806 if (res != 0) /* check result */
807 {
808 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
809
810 return 1; /* return error */
811 }
812 prev &= ~(1 << 7); /* clear config */
813 prev |= (!enable) << 7; /* set bool */
814 res = a_pcf8563_iic_write(handle, PCF8563_REG_HOUR_ALARM, prev); /* write hour alarm */
815 if (res != 0) /* check result */
816 {
817 handle->debug_print("pcf8563: write hour alarm failed.\n"); /* write hour alarm failed */
818
819 return 1; /* return error */
820 }
821
822 return 0; /* success return 0 */
823}
824
837{
838 uint8_t res;
839 uint8_t prev;
840
841 if (handle == NULL) /* check handle */
842 {
843 return 2; /* return error */
844 }
845 if (handle->inited != 1) /* check handle initialization */
846 {
847 return 3; /* return error */
848 }
849
850 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
851 if (res != 0) /* check result */
852 {
853 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
854
855 return 1; /* return error */
856 }
857 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
858
859 return 0; /* success return 0 */
860}
861
874uint8_t pcf8563_set_hour_alarm(pcf8563_handle_t *handle, uint8_t hour)
875{
876 uint8_t res;
877 uint8_t prev;
878
879 if (handle == NULL) /* check handle */
880 {
881 return 2; /* return error */
882 }
883 if (handle->inited != 1) /* check handle initialization */
884 {
885 return 3; /* return error */
886 }
887 if (hour > 23) /* check hour */
888 {
889 handle->debug_print("pcf8563: hour can't be over than 23.\n"); /* hour can't be over than 23 */
890
891 return 4; /* return error */
892 }
893
894 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
895 if (res != 0) /* check result */
896 {
897 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
898
899 return 1; /* return error */
900 }
901 prev &= ~(0x3F << 0); /* clear config */
902 prev |= a_pcf8563_hex2bcd(hour); /* set config */
903 res = a_pcf8563_iic_write(handle, PCF8563_REG_HOUR_ALARM, prev); /* write hour alarm */
904 if (res != 0) /* check result */
905 {
906 handle->debug_print("pcf8563: write hour alarm failed.\n"); /* write hour alarm failed */
907
908 return 1; /* return error */
909 }
910
911 return 0; /* success return 0 */
912}
913
925uint8_t pcf8563_get_hour_alarm(pcf8563_handle_t *handle, uint8_t *hour)
926{
927 uint8_t res;
928 uint8_t prev;
929
930 if (handle == NULL) /* check handle */
931 {
932 return 2; /* return error */
933 }
934 if (handle->inited != 1) /* check handle initialization */
935 {
936 return 3; /* return error */
937 }
938
939 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_HOUR_ALARM, &prev, 1); /* read hour alarm */
940 if (res != 0) /* check result */
941 {
942 handle->debug_print("pcf8563: read hour alarm failed.\n"); /* read hour alarm failed */
943
944 return 1; /* return error */
945 }
946 *hour = a_pcf8563_bcd2hex(prev & 0x3F); /* convert */
947
948 return 0; /* success return 0 */
949}
950
963{
964 uint8_t res;
965 uint8_t prev;
966
967 if (handle == NULL) /* check handle */
968 {
969 return 2; /* return error */
970 }
971 if (handle->inited != 1) /* check handle initialization */
972 {
973 return 3; /* return error */
974 }
975
976 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
977 if (res != 0) /* check result */
978 {
979 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
980
981 return 1; /* return error */
982 }
983 prev &= ~(1 << 7); /* clear config */
984 prev |= (!enable) << 7; /* set bool */
985 res = a_pcf8563_iic_write(handle, PCF8563_REG_DAY_ALARM, prev); /* write day alarm */
986 if (res != 0) /* check result */
987 {
988 handle->debug_print("pcf8563: write day alarm failed.\n"); /* write day alarm failed */
989
990 return 1; /* return error */
991 }
992
993 return 0; /* success return 0 */
994}
995
1008{
1009 uint8_t res;
1010 uint8_t prev;
1011
1012 if (handle == NULL) /* check handle */
1013 {
1014 return 2; /* return error */
1015 }
1016 if (handle->inited != 1) /* check handle initialization */
1017 {
1018 return 3; /* return error */
1019 }
1020
1021 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
1022 if (res != 0) /* check result */
1023 {
1024 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
1025
1026 return 1; /* return error */
1027 }
1028 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
1029
1030 return 0; /* success return 0 */
1031}
1032
1045uint8_t pcf8563_set_day_alarm(pcf8563_handle_t *handle, uint8_t day)
1046{
1047 uint8_t res;
1048 uint8_t prev;
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 if ((day == 0) || (day > 31)) /* check day */
1059 {
1060 handle->debug_print("pcf8563: day can't be zero or over than 31.\n"); /* day can't be zero or over than 31 */
1061
1062 return 4; /* return error */
1063 }
1064
1065 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
1066 if (res != 0) /* check result */
1067 {
1068 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
1069
1070 return 1; /* return error */
1071 }
1072 prev &= ~(0x3F << 0); /* clear config */
1073 prev |= a_pcf8563_hex2bcd(day); /* set config */
1074 res = a_pcf8563_iic_write(handle, PCF8563_REG_DAY_ALARM, prev); /* write day alarm */
1075 if (res != 0) /* check result */
1076 {
1077 handle->debug_print("pcf8563: write day alarm failed.\n"); /* write day alarm failed */
1078
1079 return 1; /* return error */
1080 }
1081
1082 return 0; /* success return 0 */
1083}
1084
1096uint8_t pcf8563_get_day_alarm(pcf8563_handle_t *handle, uint8_t *day)
1097{
1098 uint8_t res;
1099 uint8_t prev;
1100
1101 if (handle == NULL) /* check handle */
1102 {
1103 return 2; /* return error */
1104 }
1105 if (handle->inited != 1) /* check handle initialization */
1106 {
1107 return 3; /* return error */
1108 }
1109
1110 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_DAY_ALARM, &prev, 1); /* read day alarm */
1111 if (res != 0) /* check result */
1112 {
1113 handle->debug_print("pcf8563: read day alarm failed.\n"); /* read day alarm failed */
1114
1115 return 1; /* return error */
1116 }
1117 *day = a_pcf8563_bcd2hex(prev & 0x3F); /* convert */
1118
1119 return 0; /* success return 0 */
1120}
1121
1134{
1135 uint8_t res;
1136 uint8_t prev;
1137
1138 if (handle == NULL) /* check handle */
1139 {
1140 return 2; /* return error */
1141 }
1142 if (handle->inited != 1) /* check handle initialization */
1143 {
1144 return 3; /* return error */
1145 }
1146
1147 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1148 if (res != 0) /* check result */
1149 {
1150 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1151
1152 return 1; /* return error */
1153 }
1154 prev &= ~(1 << 7); /* clear config */
1155 prev |= (!enable) << 7; /* set bool */
1156 res = a_pcf8563_iic_write(handle, PCF8563_REG_WEEK_ALARM, prev); /* write week alarm */
1157 if (res != 0) /* check result */
1158 {
1159 handle->debug_print("pcf8563: write week alarm failed.\n"); /* write week alarm failed */
1160
1161 return 1; /* return error */
1162 }
1163
1164 return 0; /* success return 0 */
1165}
1166
1179{
1180 uint8_t res;
1181 uint8_t prev;
1182
1183 if (handle == NULL) /* check handle */
1184 {
1185 return 2; /* return error */
1186 }
1187 if (handle->inited != 1) /* check handle initialization */
1188 {
1189 return 3; /* return error */
1190 }
1191
1192 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1193 if (res != 0) /* check result */
1194 {
1195 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1196
1197 return 1; /* return error */
1198 }
1199 *enable = (pcf8563_bool_t)(!((prev >> 7) & 0x01)); /* get bool */
1200
1201 return 0; /* success return 0 */
1202}
1203
1216uint8_t pcf8563_set_week_alarm(pcf8563_handle_t *handle, uint8_t week)
1217{
1218 uint8_t res;
1219 uint8_t prev;
1220
1221 if (handle == NULL) /* check handle */
1222 {
1223 return 2; /* return error */
1224 }
1225 if (handle->inited != 1) /* check handle initialization */
1226 {
1227 return 3; /* return error */
1228 }
1229 if (week > 6) /* check week */
1230 {
1231 handle->debug_print("pcf8563: week can't be over than 6.\n"); /* week can't be over than 6 */
1232
1233 return 4; /* return error */
1234 }
1235
1236 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1237 if (res != 0) /* check result */
1238 {
1239 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1240
1241 return 1; /* return error */
1242 }
1243 prev &= ~(0x7 << 0); /* clear config */
1244 prev |= a_pcf8563_hex2bcd(week); /* set config */
1245 res = a_pcf8563_iic_write(handle, PCF8563_REG_WEEK_ALARM, prev); /* write week alarm */
1246 if (res != 0) /* check result */
1247 {
1248 handle->debug_print("pcf8563: write week alarm failed.\n"); /* write week alarm failed */
1249
1250 return 1; /* return error */
1251 }
1252
1253 return 0; /* success return 0 */
1254}
1255
1267uint8_t pcf8563_get_week_alarm(pcf8563_handle_t *handle, uint8_t *week)
1268{
1269 uint8_t res;
1270 uint8_t prev;
1271
1272 if (handle == NULL) /* check handle */
1273 {
1274 return 2; /* return error */
1275 }
1276 if (handle->inited != 1) /* check handle initialization */
1277 {
1278 return 3; /* return error */
1279 }
1280
1281 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_WEEK_ALARM, &prev, 1); /* read week alarm */
1282 if (res != 0) /* check result */
1283 {
1284 handle->debug_print("pcf8563: read week alarm failed.\n"); /* read week alarm failed */
1285
1286 return 1; /* return error */
1287 }
1288 *week = a_pcf8563_bcd2hex(prev & 0x07); /* convert */
1289
1290 return 0; /* success return 0 */
1291}
1292
1305{
1306 uint8_t res;
1307 uint8_t prev;
1308
1309 if (handle == NULL) /* check handle */
1310 {
1311 return 2; /* return error */
1312 }
1313 if (handle->inited != 1) /* check handle initialization */
1314 {
1315 return 3; /* return error */
1316 }
1317
1318 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1319 if (res != 0) /* check result */
1320 {
1321 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1322
1323 return 1; /* return error */
1324 }
1325 prev &= ~(1 << 7); /* clear config */
1326 prev |= enable << 7; /* set bool */
1327 res = a_pcf8563_iic_write(handle, PCF8563_REG_CLKOUT_CONTROL, prev); /* write clkout control */
1328 if (res != 0) /* check result */
1329 {
1330 handle->debug_print("pcf8563: write clkout control failed.\n"); /* write clkout control failed */
1331
1332 return 1; /* return error */
1333 }
1334
1335 return 0; /* success return 0 */
1336}
1337
1350{
1351 uint8_t res;
1352 uint8_t prev;
1353
1354 if (handle == NULL) /* check handle */
1355 {
1356 return 2; /* return error */
1357 }
1358 if (handle->inited != 1) /* check handle initialization */
1359 {
1360 return 3; /* return error */
1361 }
1362
1363 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1364 if (res != 0) /* check result */
1365 {
1366 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1367
1368 return 1; /* return error */
1369 }
1370 *enable = (pcf8563_bool_t)((prev >> 7) & 0x01); /* get bool */
1371
1372 return 0; /* success return 0 */
1373}
1374
1387{
1388 uint8_t res;
1389 uint8_t prev;
1390
1391 if (handle == NULL) /* check handle */
1392 {
1393 return 2; /* return error */
1394 }
1395 if (handle->inited != 1) /* check handle initialization */
1396 {
1397 return 3; /* return error */
1398 }
1399
1400 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1401 if (res != 0) /* check result */
1402 {
1403 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1404
1405 return 1; /* return error */
1406 }
1407 prev &= ~(3 << 0); /* clear config */
1408 prev |= clk << 0; /* set config */
1409 res = a_pcf8563_iic_write(handle, PCF8563_REG_CLKOUT_CONTROL, prev); /* write clkout control */
1410 if (res != 0) /* check result */
1411 {
1412 handle->debug_print("pcf8563: write clkout control failed.\n"); /* write clkout control failed */
1413
1414 return 1; /* return error */
1415 }
1416
1417 return 0; /* success return 0 */
1418}
1419
1432{
1433 uint8_t res;
1434 uint8_t prev;
1435
1436 if (handle == NULL) /* check handle */
1437 {
1438 return 2; /* return error */
1439 }
1440 if (handle->inited != 1) /* check handle initialization */
1441 {
1442 return 3; /* return error */
1443 }
1444
1445 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CLKOUT_CONTROL, &prev, 1); /* read clkout control */
1446 if (res != 0) /* check result */
1447 {
1448 handle->debug_print("pcf8563: read clkout control failed.\n"); /* read clkout control failed */
1449
1450 return 1; /* return error */
1451 }
1452 *clk = (pcf8563_clock_out_t)(prev & 0x03); /* get clock */
1453
1454 return 0; /* success return 0 */
1455}
1456
1469{
1470 uint8_t res;
1471 uint8_t prev;
1472
1473 if (handle == NULL) /* check handle */
1474 {
1475 return 2; /* return error */
1476 }
1477 if (handle->inited != 1) /* check handle initialization */
1478 {
1479 return 3; /* return error */
1480 }
1481
1482 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1483 if (res != 0) /* check result */
1484 {
1485 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1486
1487 return 1; /* return error */
1488 }
1489 prev &= ~(1 << 7); /* clear config */
1490 prev |= enable << 7; /* set bool */
1491 res = a_pcf8563_iic_write(handle, PCF8563_REG_TIMER_CONTROL, prev); /* write timer control */
1492 if (res != 0) /* check result */
1493 {
1494 handle->debug_print("pcf8563: write timer control failed.\n"); /* write timer control failed */
1495
1496 return 1; /* return error */
1497 }
1498
1499 return 0; /* success return 0 */
1500}
1501
1514{
1515 uint8_t res;
1516 uint8_t prev;
1517
1518 if (handle == NULL) /* check handle */
1519 {
1520 return 2; /* return error */
1521 }
1522 if (handle->inited != 1) /* check handle initialization */
1523 {
1524 return 3; /* return error */
1525 }
1526
1527 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1528 if (res != 0) /* check result */
1529 {
1530 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1531
1532 return 1; /* return error */
1533 }
1534 *enable = (pcf8563_bool_t)((prev >> 7) & 0x01); /* get bool */
1535
1536 return 0; /* success return 0 */
1537}
1538
1551{
1552 uint8_t res;
1553 uint8_t prev;
1554
1555 if (handle == NULL) /* check handle */
1556 {
1557 return 2; /* return error */
1558 }
1559 if (handle->inited != 1) /* check handle initialization */
1560 {
1561 return 3; /* return error */
1562 }
1563
1564 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1565 if (res != 0) /* check result */
1566 {
1567 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1568
1569 return 1; /* return error */
1570 }
1571 prev &= ~(3 << 0); /* clear config */
1572 prev |= freq << 0; /* set freq */
1573 res = a_pcf8563_iic_write(handle, PCF8563_REG_TIMER_CONTROL, prev); /* write timer control */
1574 if (res != 0) /* check result */
1575 {
1576 handle->debug_print("pcf8563: write timer control failed.\n"); /* write timer control failed */
1577
1578 return 1; /* return error */
1579 }
1580
1581 return 0; /* success return 0 */
1582}
1583
1596{
1597 uint8_t res;
1598 uint8_t prev;
1599
1600 if (handle == NULL) /* check handle */
1601 {
1602 return 2; /* return error */
1603 }
1604 if (handle->inited != 1) /* check handle initialization */
1605 {
1606 return 3; /* return error */
1607 }
1608
1609 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER_CONTROL, &prev, 1); /* read timer control */
1610 if (res != 0) /* check result */
1611 {
1612 handle->debug_print("pcf8563: read timer control failed.\n"); /* read timer control failed */
1613
1614 return 1; /* return error */
1615 }
1616 *freq = (pcf8563_timer_freq_t)(prev & 0x03); /* get freq */
1617
1618 return 0; /* success return 0 */
1619}
1620
1632uint8_t pcf8563_set_timer_value(pcf8563_handle_t *handle, uint8_t value)
1633{
1634 uint8_t res;
1635 uint8_t prev;
1636
1637 if (handle == NULL) /* check handle */
1638 {
1639 return 2; /* return error */
1640 }
1641 if (handle->inited != 1) /* check handle initialization */
1642 {
1643 return 3; /* return error */
1644 }
1645
1646 prev = value; /* set value */
1647 res = a_pcf8563_iic_write(handle, PCF8563_REG_TIMER, prev); /* write timer */
1648 if (res != 0) /* check result */
1649 {
1650 handle->debug_print("pcf8563: write timer failed.\n"); /* write timer failed */
1651
1652 return 1; /* return error */
1653 }
1654
1655 return 0; /* success return 0 */
1656}
1657
1669uint8_t pcf8563_get_timer_value(pcf8563_handle_t *handle, uint8_t *value)
1670{
1671 uint8_t res;
1672
1673 if (handle == NULL) /* check handle */
1674 {
1675 return 2; /* return error */
1676 }
1677 if (handle->inited != 1) /* check handle initialization */
1678 {
1679 return 3; /* return error */
1680 }
1681
1682 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_TIMER, value, 1); /* read timer */
1683 if (res != 0) /* check result */
1684 {
1685 handle->debug_print("pcf8563: read timer failed.\n"); /* read timer failed */
1686
1687 return 1; /* return error */
1688 }
1689
1690 return 0; /* success return 0 */
1691}
1692
1705{
1706 uint8_t res;
1707 uint8_t prev;
1708
1709 if (handle == NULL) /* check handle */
1710 {
1711 return 2; /* return error */
1712 }
1713 if (handle->inited != 1) /* check handle initialization */
1714 {
1715 return 3; /* return error */
1716 }
1717
1718 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1719 if (res != 0) /* check result */
1720 {
1721 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1722
1723 return 1; /* return error */
1724 }
1725 prev &= ~(1 << 0); /* clear config */
1726 prev |= enable << 0; /* set bool */
1727 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
1728 if (res != 0) /* check result */
1729 {
1730 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1731
1732 return 1; /* return error */
1733 }
1734
1735 return 0; /* success return 0 */
1736}
1737
1750{
1751 uint8_t res;
1752 uint8_t prev;
1753
1754 if (handle == NULL) /* check handle */
1755 {
1756 return 2; /* return error */
1757 }
1758 if (handle->inited != 1) /* check handle initialization */
1759 {
1760 return 3; /* return error */
1761 }
1762
1763 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1764 if (res != 0) /* check result */
1765 {
1766 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1767
1768 return 1; /* return error */
1769 }
1770 *enable = (pcf8563_bool_t)((prev >> 0) & 0x01); /* get bool */
1771
1772 return 0; /* success return 0 */
1773}
1774
1787{
1788 uint8_t res;
1789 uint8_t prev;
1790
1791 if (handle == NULL) /* check handle */
1792 {
1793 return 2; /* return error */
1794 }
1795 if (handle->inited != 1) /* check handle initialization */
1796 {
1797 return 3; /* return error */
1798 }
1799
1800 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1801 if (res != 0) /* check result */
1802 {
1803 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1804
1805 return 1; /* return error */
1806 }
1807 prev &= ~(1 << 1); /* clear config */
1808 prev |= enable << 1; /* set bool */
1809 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
1810 if (res != 0) /* check result */
1811 {
1812 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1813
1814 return 1; /* return error */
1815 }
1816
1817 return 0; /* success return 0 */
1818}
1819
1832{
1833 uint8_t res;
1834 uint8_t prev;
1835
1836 if (handle == NULL) /* check handle */
1837 {
1838 return 2; /* return error */
1839 }
1840 if (handle->inited != 1) /* check handle initialization */
1841 {
1842 return 3; /* return error */
1843 }
1844
1845 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1846 if (res != 0) /* check result */
1847 {
1848 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1849
1850 return 1; /* return error */
1851 }
1852 *enable = (pcf8563_bool_t)((prev >> 1) & 0x01); /* get bool */
1853
1854 return 0; /* success return 0 */
1855}
1856
1869{
1870 uint8_t res;
1871 uint8_t prev;
1872
1873 if (handle == NULL) /* check handle */
1874 {
1875 return 2; /* return error */
1876 }
1877 if (handle->inited != 1) /* check handle initialization */
1878 {
1879 return 3; /* return error */
1880 }
1881
1882 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1883 if (res != 0) /* check result */
1884 {
1885 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1886
1887 return 1; /* return error */
1888 }
1889 prev &= ~(1 << 4); /* clear config */
1890 prev |= mode << 4; /* set mode */
1891 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
1892 if (res != 0) /* check result */
1893 {
1894 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1895
1896 return 1; /* return error */
1897 }
1898
1899 return 0; /* success return 0 */
1900}
1901
1914{
1915 uint8_t res;
1916 uint8_t prev;
1917
1918 if (handle == NULL) /* check handle */
1919 {
1920 return 2; /* return error */
1921 }
1922 if (handle->inited != 1) /* check handle initialization */
1923 {
1924 return 3; /* return error */
1925 }
1926
1927 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1928 if (res != 0) /* check result */
1929 {
1930 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1931
1932 return 1; /* return error */
1933 }
1934 *mode = (pcf8563_interrupt_mode_t)((prev >> 4) & 0x1); /* get mode */
1935
1936 return 0; /* success return 0 */
1937}
1938
1950{
1951 uint8_t res;
1952 uint8_t prev;
1953 uint8_t prev2;
1954
1955 if (handle == NULL) /* check handle */
1956 {
1957 return 2; /* return error */
1958 }
1959 if (handle->inited != 1) /* check handle initialization */
1960 {
1961 return 3; /* return error */
1962 }
1963
1964 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
1965 if (res != 0) /* check result */
1966 {
1967 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
1968
1969 return 1; /* return error */
1970 }
1971 prev2 = prev; /* save config */
1972 prev2 &= ~(3 << 2); /* clear settings */
1973 if (((prev >> 2) & 0x1) != 0) /* check timer */
1974 {
1975 if (handle->receive_callback != NULL) /* if not null */
1976 {
1977 handle->receive_callback(PCF8563_INTERRUPT_EVENT_TIMER); /* run the callback */
1978 }
1979 }
1980 if (((prev >> 3) & 0x01) != 0) /* check alarm */
1981 {
1982 if (handle->receive_callback != NULL) /* if not null */
1983 {
1984 handle->receive_callback(PCF8563_INTERRUPT_EVENT_ALARM); /* run the callback */
1985 }
1986 }
1987 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev2); /* write control status2 */
1988 if (res != 0) /* check result */
1989 {
1990 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
1991
1992 return 1; /* return error */
1993 }
1994
1995 return 0; /* success return 0 */
1996}
1997
2010{
2011 uint8_t res;
2012 uint8_t prev;
2013
2014 if (handle == NULL) /* check handle */
2015 {
2016 return 2; /* return error */
2017 }
2018 if (handle->inited != 1) /* check handle initialization */
2019 {
2020 return 3; /* return error */
2021 }
2022
2023 res = a_pcf8563_iic_multiple_read(handle, PCF8563_REG_CONTROL_STATUS2, &prev, 1); /* read control status2 */
2024 if (res != 0) /* check result */
2025 {
2026 handle->debug_print("pcf8563: read control status2 failed.\n"); /* read control status2 failed */
2027
2028 return 1; /* return error */
2029 }
2030 if (event == PCF8563_INTERRUPT_EVENT_TIMER) /* timer */
2031 {
2032 prev &= ~(1 << 2); /* clear bit */
2033 }
2034 else /* alarm */
2035 {
2036 prev &= ~(1 << 3); /* clear bit */
2037 }
2038 res = a_pcf8563_iic_write(handle, PCF8563_REG_CONTROL_STATUS2, prev); /* write control status2 */
2039 if (res != 0) /* check result */
2040 {
2041 handle->debug_print("pcf8563: write control status2 failed.\n"); /* write control status2 failed */
2042
2043 return 1; /* return error */
2044 }
2045
2046 return 0; /* success return 0 */
2047}
2048
2060{
2061 if (handle == NULL) /* check handle */
2062 {
2063 return 2; /* return error */
2064 }
2065 if (handle->debug_print == NULL) /* check debug_print */
2066 {
2067 return 3; /* return error */
2068 }
2069 if (handle->iic_init == NULL) /* check iic_init */
2070 {
2071 handle->debug_print("pcf8563: iic_init is null.\n"); /* iic_init is null */
2072
2073 return 3; /* return error */
2074 }
2075 if (handle->iic_deinit == NULL) /* check iic_deinit */
2076 {
2077 handle->debug_print("pcf8563: iic_deinit is null.\n"); /* iic_deinit is null */
2078
2079 return 3; /* return error */
2080 }
2081 if (handle->iic_write == NULL) /* check iic_write */
2082 {
2083 handle->debug_print("pcf8563: iic_write is null.\n"); /* iic_write is null */
2084
2085 return 3; /* return error */
2086 }
2087 if (handle->iic_read == NULL) /* check iic_read */
2088 {
2089 handle->debug_print("pcf8563: iic_read is null.\n"); /* iic_read is null */
2090
2091 return 3; /* return error */
2092 }
2093 if (handle->delay_ms == NULL) /* check delay_ms */
2094 {
2095 handle->debug_print("pcf8563: delay_ms is null.\n"); /* delay_ms is null */
2096
2097 return 3; /* return error */
2098 }
2099 if (handle->receive_callback == NULL) /* check receive_callback */
2100 {
2101 handle->debug_print("pcf8563: receive_callback is null.\n"); /* receive_callback is null */
2102
2103 return 3; /* return error */
2104 }
2105
2106 if (handle->iic_init() != 0) /* iic init */
2107 {
2108 handle->debug_print("pcf8563: iic init failed.\n"); /* iic init failed */
2109
2110 return 1; /* return error */
2111 }
2112 handle->inited = 1; /* flag finish initialization */
2113
2114 return 0; /* success return 0 */
2115}
2116
2128{
2129 if (handle == NULL) /* check handle */
2130 {
2131 return 2; /* return error */
2132 }
2133 if (handle->inited != 1) /* check handle initialization */
2134 {
2135 return 3; /* return error */
2136 }
2137
2138 if (handle->iic_deinit() != 0) /* iic deinit */
2139 {
2140 handle->debug_print("pcf8563: iic deinit failed.\n"); /* iic deinit failed */
2141
2142 return 1; /* return error */
2143 }
2144 handle->inited = 0; /* flag close */
2145
2146 return 0; /* success return 0 */
2147}
2148
2162uint8_t pcf8563_set_reg(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2163{
2164 if (handle == NULL) /* check handle */
2165 {
2166 return 2; /* return error */
2167 }
2168 if (handle->inited != 1) /* check handle initialization */
2169 {
2170 return 3; /* return error */
2171 }
2172
2173 if (handle->iic_write(PCF8563_ADDRESS, reg, buf, len) != 0) /* write data */
2174 {
2175 return 1; /* return error */
2176 }
2177
2178 return 0; /* success return 0 */
2179}
2180
2194uint8_t pcf8563_get_reg(pcf8563_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2195{
2196 if (handle == NULL) /* check handle */
2197 {
2198 return 2; /* return error */
2199 }
2200 if (handle->inited != 1) /* check handle initialization */
2201 {
2202 return 3; /* return error */
2203 }
2204
2205 if (handle->iic_read(PCF8563_ADDRESS, reg, buf, len) != 0) /* read data */
2206 {
2207 return 1; /* return error */
2208 }
2209
2210 return 0; /* success return 0 */
2211}
2212
2222{
2223 if (info == NULL) /* check handle */
2224 {
2225 return 2; /* return error */
2226 }
2227
2228 memset(info, 0, sizeof(pcf8563_info_t)); /* initialize pcf8563 info structure */
2229 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2230 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2231 strncpy(info->interface, "IIC", 8); /* copy interface name */
2232 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2233 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2234 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2235 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2236 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2237 info->driver_version = DRIVER_VERSION; /* set driver version */
2238
2239 return 0; /* success return 0 */
2240}
#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]