pcf8563-dd 0.3.0

A driver for the PCF8563/BM8563 real-time clock (uses device-driver crate)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use super::{I2c, RegisterInterface, bisync, only_async, only_sync};
use crate::{
    Alarm, ClkoutFrequency, DateTime, PCF8563_I2C_ADDR, Pcf8563Interface, Pcf8563LowLevel,
    RtcError, Time, TimerFrequency, bcd_to_dec, dec_to_bcd,
};
#[cfg(feature = "rtcc")]
#[only_sync]
use rtcc::{
    Datelike as RtccDatelike, Hours as RtccHours, NaiveDate as RtccNaiveDate,
    NaiveDateTime as RtccNaiveDateTime, NaiveTime as RtccNaiveTime, Timelike as RtccTimelike,
};

#[bisync]
impl<I2CBus, E> RegisterInterface for Pcf8563Interface<I2CBus>
where
    I2CBus: I2c<Error = E>,
    E: core::fmt::Debug,
{
    type AddressType = u8;
    type Error = RtcError<E>;
    async fn read_register(
        &mut self,
        address: u8,
        _size_bits: u32,
        data: &mut [u8],
    ) -> Result<(), Self::Error> {
        self.i2c_bus
            .write_read(PCF8563_I2C_ADDR, &[address], data)
            .await
            .map_err(RtcError::I2c)
    }
    async fn write_register(
        &mut self,
        address: u8,
        _size_bits: u32,
        data: &[u8],
    ) -> Result<(), Self::Error> {
        let mut buffer = [0u8; 9]; // Max: address + 8 bytes for datetime
        if (1 + data.len()) > buffer.len() {
            return Err(RtcError::InvalidInputData);
        }
        buffer[0] = address;
        buffer[1..1 + data.len()].copy_from_slice(data);
        self.i2c_bus
            .write(PCF8563_I2C_ADDR, &buffer[..1 + data.len()])
            .await
            .map_err(RtcError::I2c)
    }
}

pub struct Pcf8563<
    I2CImpl: RegisterInterface<AddressType = u8, Error = RtcError<I2CBusErr>>,
    I2CBusErr: core::fmt::Debug,
> {
    pub ll: Pcf8563LowLevel<I2CImpl>,
    _marker: core::marker::PhantomData<I2CBusErr>,
}

impl<I2CBus, E> Pcf8563<Pcf8563Interface<I2CBus>, E>
where
    I2CBus: I2c<Error = E>,
    E: core::fmt::Debug,
{
    pub fn new(i2c: I2CBus) -> Self {
        Self {
            ll: Pcf8563LowLevel::new(Pcf8563Interface::new(i2c)),
            _marker: core::marker::PhantomData,
        }
    }
}

include!("bisync_helpers.rs");

impl<I2CImpl, I2CBusErr> Pcf8563<I2CImpl, I2CBusErr>
where
    I2CImpl: RegisterInterface<AddressType = u8, Error = RtcError<I2CBusErr>>,
    I2CBusErr: core::fmt::Debug,
{
    // =========================================================================
    // Date and Time
    // =========================================================================

    /// Get the current date and time
    ///
    /// Reads all 7 time/date registers (0x02-0x08) in a single I2C burst read,
    /// as recommended by the datasheet to ensure consistency.
    #[bisync]
    pub async fn get_datetime(&mut self) -> Result<DateTime, RtcError<I2CBusErr>> {
        // Bulk read registers 0x02-0x08 (7 bytes) in one I2C transaction:
        // buf[0]: Seconds (+ VL flag in bit 7)
        // buf[1]: Minutes
        // buf[2]: Hours
        // buf[3]: Days
        // buf[4]: Weekdays
        // buf[5]: Century/Months (century flag in bit 7)
        // buf[6]: Years
        let mut buf = [0u8; 7];
        self.ll.interface().read_register(0x02, 0, &mut buf).await?;

        Ok(DateTime {
            seconds: bcd_to_dec(buf[0] & 0x7F), // mask VL flag
            minutes: bcd_to_dec(buf[1] & 0x7F),
            hours: bcd_to_dec(buf[2] & 0x3F),
            day: bcd_to_dec(buf[3] & 0x3F),
            weekday: buf[4] & 0x07,
            month: bcd_to_dec(buf[5] & 0x1F), // mask century flag
            year: bcd_to_dec(buf[6]),
        })
    }

    /// Set the date and time
    ///
    /// Writes all 7 time/date registers (0x02-0x08) in a single I2C burst write.
    /// Also clears the VL (voltage-low) flag.
    #[bisync]
    pub async fn set_datetime(&mut self, dt: &DateTime) -> Result<(), RtcError<I2CBusErr>> {
        // Validate input
        if dt.year > 99
            || dt.month < 1
            || dt.month > 12
            || dt.weekday > 6
            || dt.day < 1
            || dt.day > 31
            || dt.hours > 23
            || dt.minutes > 59
            || dt.seconds > 59
        {
            return Err(RtcError::InvalidInputData);
        }

        // Bulk write registers 0x02-0x08 (7 bytes) in one I2C transaction
        let buf = [
            dec_to_bcd(dt.seconds) & 0x7F, // seconds with VL flag cleared
            dec_to_bcd(dt.minutes),
            dec_to_bcd(dt.hours),
            dec_to_bcd(dt.day),
            dt.weekday,
            dec_to_bcd(dt.month), // preserves century flag as 0
            dec_to_bcd(dt.year),
        ];
        self.ll.interface().write_register(0x02, 0, &buf).await?;

        Ok(())
    }

    /// Set only the time (hours, minutes, seconds)
    #[bisync]
    pub async fn set_time(&mut self, time: &Time) -> Result<(), RtcError<I2CBusErr>> {
        if time.hours > 23 || time.minutes > 59 || time.seconds > 59 {
            return Err(RtcError::InvalidInputData);
        }

        let seconds_bcd = dec_to_bcd(time.seconds);
        let minutes_bcd = dec_to_bcd(time.minutes);
        let hours_bcd = dec_to_bcd(time.hours);

        let mut op_sec = self.ll.seconds();
        write_internal(&mut op_sec, |r| {
            r.set_vl(false);
            r.set_seconds_ten(seconds_bcd >> 4);
            r.set_seconds_unit(seconds_bcd & 0x0F);
        })
        .await?;

        let mut op_min = self.ll.minutes();
        write_internal(&mut op_min, |r| {
            r.set_minutes_ten(minutes_bcd >> 4);
            r.set_minutes_unit(minutes_bcd & 0x0F);
        })
        .await?;

        let mut op_hr = self.ll.hours();
        write_internal(&mut op_hr, |r| {
            r.set_hours_ten(hours_bcd >> 4);
            r.set_hours_unit(hours_bcd & 0x0F);
        })
        .await?;

        Ok(())
    }

    // =========================================================================
    // Clock Integrity (Voltage Low Detection)
    // =========================================================================

    /// Check if clock integrity is guaranteed
    ///
    /// Returns `false` if the VL (Voltage Low) flag is set, indicating
    /// the clock data may be invalid due to power loss.
    #[bisync]
    pub async fn is_clock_valid(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.seconds();
        let reg = read_internal(&mut op).await?;
        Ok(!reg.vl())
    }

    /// Clear the voltage-low flag
    ///
    /// Should be called after setting the time to indicate clock is valid.
    #[bisync]
    pub async fn clear_voltage_low_flag(&mut self) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.seconds();
        modify_internal(&mut op, |r| r.set_vl(false)).await
    }

    // =========================================================================
    // Century Flag
    // =========================================================================

    /// Get the century flag
    ///
    /// Returns `true` if century is X+1 (e.g., 2100s), `false` if century is X (e.g., 2000s)
    #[bisync]
    pub async fn get_century_flag(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.century_months();
        let reg = read_internal(&mut op).await?;
        Ok(reg.century())
    }

    /// Set the century flag
    #[bisync]
    pub async fn set_century_flag(&mut self, century: bool) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.century_months();
        modify_internal(&mut op, |r| r.set_century(century)).await
    }

    // =========================================================================
    // Clock Control
    // =========================================================================

    /// Start or stop the RTC clock
    #[bisync]
    pub async fn set_clock_running(&mut self, running: bool) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_1();
        modify_internal(&mut op, |r| r.set_stop(!running)).await
    }

    /// Check if the RTC clock is running
    #[bisync]
    pub async fn is_clock_running(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_1();
        let reg = read_internal(&mut op).await?;
        Ok(!reg.stop())
    }

    // =========================================================================
    // Alarm
    // =========================================================================

    /// Get the current alarm configuration
    #[bisync]
    pub async fn get_alarm(&mut self) -> Result<Alarm, RtcError<I2CBusErr>> {
        let mut op_min = self.ll.minute_alarm();
        let min_reg = read_internal(&mut op_min).await?;

        let mut op_hr = self.ll.hour_alarm();
        let hr_reg = read_internal(&mut op_hr).await?;

        let mut op_day = self.ll.day_alarm();
        let day_reg = read_internal(&mut op_day).await?;

        let mut op_wd = self.ll.weekday_alarm();
        let wd_reg = read_internal(&mut op_wd).await?;

        Ok(Alarm {
            minute: if min_reg.ae_m() {
                None
            } else {
                Some(bcd_to_dec(
                    (min_reg.minute_alarm_ten() << 4) as u8 | min_reg.minute_alarm_unit() as u8,
                ))
            },
            hour: if hr_reg.ae_h() {
                None
            } else {
                Some(bcd_to_dec(
                    (hr_reg.hour_alarm_ten() << 4) as u8 | hr_reg.hour_alarm_unit() as u8,
                ))
            },
            day: if day_reg.ae_d() {
                None
            } else {
                Some(bcd_to_dec(
                    (day_reg.day_alarm_ten() << 4) as u8 | day_reg.day_alarm_unit() as u8,
                ))
            },
            weekday: if wd_reg.ae_w() {
                None
            } else {
                Some(wd_reg.weekday_alarm() as u8)
            },
        })
    }

    /// Set the alarm configuration
    ///
    /// Set a field to `Some(value)` to enable that alarm component,
    /// or `None` to disable it.
    #[bisync]
    pub async fn set_alarm(&mut self, alarm: &Alarm) -> Result<(), RtcError<I2CBusErr>> {
        // Minute alarm
        let mut op_min = self.ll.minute_alarm();
        write_internal(&mut op_min, |r| {
            if let Some(min) = alarm.minute {
                let bcd = dec_to_bcd(min);
                r.set_ae_m(false); // Enable
                r.set_minute_alarm_ten(bcd >> 4);
                r.set_minute_alarm_unit(bcd & 0x0F);
            } else {
                r.set_ae_m(true); // Disable
            }
        })
        .await?;

        // Hour alarm
        let mut op_hr = self.ll.hour_alarm();
        write_internal(&mut op_hr, |r| {
            if let Some(hr) = alarm.hour {
                let bcd = dec_to_bcd(hr);
                r.set_ae_h(false); // Enable
                r.set_hour_alarm_ten(bcd >> 4);
                r.set_hour_alarm_unit(bcd & 0x0F);
            } else {
                r.set_ae_h(true); // Disable
            }
        })
        .await?;

        // Day alarm
        let mut op_day = self.ll.day_alarm();
        write_internal(&mut op_day, |r| {
            if let Some(day) = alarm.day {
                let bcd = dec_to_bcd(day);
                r.set_ae_d(false); // Enable
                r.set_day_alarm_ten(bcd >> 4);
                r.set_day_alarm_unit(bcd & 0x0F);
            } else {
                r.set_ae_d(true); // Disable
            }
        })
        .await?;

        // Weekday alarm
        let mut op_wd = self.ll.weekday_alarm();
        write_internal(&mut op_wd, |r| {
            if let Some(wd) = alarm.weekday {
                r.set_ae_w(false); // Enable
                r.set_weekday_alarm(wd);
            } else {
                r.set_ae_w(true); // Disable
            }
        })
        .await?;

        Ok(())
    }

    /// Disable all alarm components
    #[bisync]
    pub async fn disable_alarm(&mut self) -> Result<(), RtcError<I2CBusErr>> {
        self.set_alarm(&Alarm::default()).await
    }

    /// Check if alarm flag is set (alarm has triggered)
    #[bisync]
    pub async fn get_alarm_flag(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        let reg = read_internal(&mut op).await?;
        Ok(reg.af())
    }

    /// Clear the alarm flag
    #[bisync]
    pub async fn clear_alarm_flag(&mut self) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        modify_internal(&mut op, |r| r.set_af(false)).await
    }

    /// Enable or disable alarm interrupt
    #[bisync]
    pub async fn set_alarm_interrupt(&mut self, enable: bool) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        modify_internal(&mut op, |r| r.set_aie(enable)).await
    }

    /// Check if alarm interrupt is enabled
    #[bisync]
    pub async fn is_alarm_interrupt_enabled(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        let reg = read_internal(&mut op).await?;
        Ok(reg.aie())
    }

    // =========================================================================
    // Timer
    // =========================================================================

    /// Set the timer countdown value (0-255)
    #[bisync]
    pub async fn set_timer_value(&mut self, value: u8) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.timer();
        write_internal(&mut op, |r| r.set_timer_value(value)).await
    }

    /// Get the current timer countdown value
    #[bisync]
    pub async fn get_timer_value(&mut self) -> Result<u8, RtcError<I2CBusErr>> {
        let mut op = self.ll.timer();
        let reg = read_internal(&mut op).await?;
        Ok(reg.timer_value() as u8)
    }

    /// Set the timer source clock frequency
    #[bisync]
    pub async fn set_timer_frequency(
        &mut self,
        freq: TimerFrequency,
    ) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.timer_control();
        modify_internal(&mut op, |r| r.set_td(freq)).await
    }

    /// Get the timer source clock frequency
    #[bisync]
    pub async fn get_timer_frequency(&mut self) -> Result<TimerFrequency, RtcError<I2CBusErr>> {
        let mut op = self.ll.timer_control();
        let reg = read_internal(&mut op).await?;
        Ok(reg.td())
    }

    /// Enable or disable the timer
    #[bisync]
    pub async fn set_timer_enabled(&mut self, enable: bool) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.timer_control();
        modify_internal(&mut op, |r| r.set_te(enable)).await
    }

    /// Check if timer is enabled
    #[bisync]
    pub async fn is_timer_enabled(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.timer_control();
        let reg = read_internal(&mut op).await?;
        Ok(reg.te())
    }

    /// Check if timer flag is set (timer has triggered)
    #[bisync]
    pub async fn get_timer_flag(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        let reg = read_internal(&mut op).await?;
        Ok(reg.tf())
    }

    /// Clear the timer flag
    #[bisync]
    pub async fn clear_timer_flag(&mut self) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        modify_internal(&mut op, |r| r.set_tf(false)).await
    }

    /// Enable or disable timer interrupt
    #[bisync]
    pub async fn set_timer_interrupt(&mut self, enable: bool) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        modify_internal(&mut op, |r| r.set_tie(enable)).await
    }

    /// Check if timer interrupt is enabled
    #[bisync]
    pub async fn is_timer_interrupt_enabled(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        let reg = read_internal(&mut op).await?;
        Ok(reg.tie())
    }

    /// Set timer interrupt mode (level or pulse)
    #[bisync]
    pub async fn set_timer_interrupt_pulse_mode(
        &mut self,
        pulse: bool,
    ) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.control_status_2();
        modify_internal(&mut op, |r| r.set_ti_tp(pulse)).await
    }

    // =========================================================================
    // Clock Output
    // =========================================================================

    /// Enable or disable the CLKOUT output
    #[bisync]
    pub async fn set_clkout_enabled(&mut self, enable: bool) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.clkout_control();
        modify_internal(&mut op, |r| r.set_fe(enable)).await
    }

    /// Check if CLKOUT is enabled
    #[bisync]
    pub async fn is_clkout_enabled(&mut self) -> Result<bool, RtcError<I2CBusErr>> {
        let mut op = self.ll.clkout_control();
        let reg = read_internal(&mut op).await?;
        Ok(reg.fe())
    }

    /// Set the CLKOUT frequency
    #[bisync]
    pub async fn set_clkout_frequency(
        &mut self,
        freq: ClkoutFrequency,
    ) -> Result<(), RtcError<I2CBusErr>> {
        let mut op = self.ll.clkout_control();
        modify_internal(&mut op, |r| r.set_fd(freq)).await
    }

    /// Get the CLKOUT frequency setting
    #[bisync]
    pub async fn get_clkout_frequency(&mut self) -> Result<ClkoutFrequency, RtcError<I2CBusErr>> {
        let mut op = self.ll.clkout_control();
        let reg = read_internal(&mut op).await?;
        Ok(reg.fd())
    }

    // =========================================================================
    // Initialization
    // =========================================================================

    /// Initialize the RTC with default settings
    ///
    /// - Clears all control bits
    /// - Clears voltage-low flag
    /// - Disables all alarms
    /// - Sets timer to lowest frequency (1/60 Hz) for power saving
    #[bisync]
    pub async fn init(&mut self) -> Result<(), RtcError<I2CBusErr>> {
        // Clear control status 1
        let mut op1 = self.ll.control_status_1();
        write_internal(&mut op1, |r| {
            r.set_test1(false);
            r.set_stop(false);
            r.set_testc(false);
        })
        .await?;

        // Clear control status 2
        let mut op2 = self.ll.control_status_2();
        write_internal(&mut op2, |r| {
            r.set_ti_tp(false);
            r.set_af(false);
            r.set_tf(false);
            r.set_aie(false);
            r.set_tie(false);
        })
        .await?;

        // Clear voltage-low flag
        self.clear_voltage_low_flag().await?;

        // Disable all alarms
        self.disable_alarm().await?;

        // Set timer to lowest frequency for power saving
        self.set_timer_frequency(TimerFrequency::Freq160Hz).await?;

        Ok(())
    }
}

#[cfg(feature = "rtcc")]
#[only_sync]
impl<I2CImpl, I2CBusErr> rtcc::DateTimeAccess for Pcf8563<I2CImpl, I2CBusErr>
where
    I2CImpl: RegisterInterface<AddressType = u8, Error = RtcError<I2CBusErr>>,
    I2CBusErr: core::fmt::Debug,
{
    type Error = RtcError<I2CBusErr>;

    fn datetime(&mut self) -> Result<RtccNaiveDateTime, Self::Error> {
        let dt = self.get_datetime()?;
        let century_flag = self.get_century_flag()?;
        let base_year = if century_flag { 1900 } else { 2000 };

        let date =
            RtccNaiveDate::from_ymd_opt(base_year + dt.year as i32, dt.month as u32, dt.day as u32)
                .ok_or(RtcError::InvalidInputData)?;
        let time =
            RtccNaiveTime::from_hms_opt(dt.hours as u32, dt.minutes as u32, dt.seconds as u32)
                .ok_or(RtcError::InvalidInputData)?;

        Ok(RtccNaiveDateTime::new(date, time))
    }

    fn set_datetime(&mut self, datetime: &RtccNaiveDateTime) -> Result<(), Self::Error> {
        let date = datetime.date();
        let time = datetime.time();
        let year = date.year();

        if !(1900..=2099).contains(&year) {
            return Err(RtcError::InvalidInputData);
        }

        let dt = DateTime {
            year: (year % 100) as u8,
            month: date.month() as u8,
            day: date.day() as u8,
            weekday: date.weekday().num_days_from_sunday() as u8,
            hours: time.hour() as u8,
            minutes: time.minute() as u8,
            seconds: time.second() as u8,
        };

        self.set_century_flag(year < 2000)?;
        self.set_datetime(&dt)
    }
}

#[cfg(feature = "rtcc")]
#[only_sync]
impl<I2CImpl, I2CBusErr> rtcc::Rtcc for Pcf8563<I2CImpl, I2CBusErr>
where
    I2CImpl: RegisterInterface<AddressType = u8, Error = RtcError<I2CBusErr>>,
    I2CBusErr: core::fmt::Debug,
{
    fn seconds(&mut self) -> Result<u8, Self::Error> {
        Ok(self.get_datetime()?.seconds)
    }

    fn minutes(&mut self) -> Result<u8, Self::Error> {
        Ok(self.get_datetime()?.minutes)
    }

    fn hours(&mut self) -> Result<RtccHours, Self::Error> {
        let hours = self.get_datetime()?.hours;
        if hours > 23 {
            Err(RtcError::InvalidInputData)
        } else {
            Ok(RtccHours::H24(hours))
        }
    }

    fn time(&mut self) -> Result<RtccNaiveTime, Self::Error> {
        let dt = self.get_datetime()?;
        RtccNaiveTime::from_hms_opt(dt.hours as u32, dt.minutes as u32, dt.seconds as u32)
            .ok_or(RtcError::InvalidInputData)
    }

    fn weekday(&mut self) -> Result<u8, Self::Error> {
        let weekday = self.get_datetime()?.weekday;
        if weekday > 6 {
            Err(RtcError::InvalidInputData)
        } else {
            Ok(weekday + 1)
        }
    }

    fn day(&mut self) -> Result<u8, Self::Error> {
        Ok(self.get_datetime()?.day)
    }

    fn month(&mut self) -> Result<u8, Self::Error> {
        Ok(self.get_datetime()?.month)
    }

    fn year(&mut self) -> Result<u16, Self::Error> {
        let datetime = <Self as rtcc::DateTimeAccess>::datetime(self)?;
        Ok(datetime.date().year() as u16)
    }

    fn date(&mut self) -> Result<RtccNaiveDate, Self::Error> {
        Ok(<Self as rtcc::DateTimeAccess>::datetime(self)?.date())
    }

    fn set_seconds(&mut self, seconds: u8) -> Result<(), Self::Error> {
        let mut dt = self.get_datetime()?;
        dt.seconds = seconds;
        self.set_datetime(&dt)
    }

    fn set_minutes(&mut self, minutes: u8) -> Result<(), Self::Error> {
        let mut dt = self.get_datetime()?;
        dt.minutes = minutes;
        self.set_datetime(&dt)
    }

    fn set_hours(&mut self, hours: RtccHours) -> Result<(), Self::Error> {
        let hours_24 = match hours {
            RtccHours::H24(h) if h < 24 => h,
            RtccHours::AM(h) if (1..=12).contains(&h) => {
                if h == 12 {
                    0
                } else {
                    h
                }
            }
            RtccHours::PM(h) if (1..=12).contains(&h) => {
                if h == 12 {
                    12
                } else {
                    h + 12
                }
            }
            _ => return Err(RtcError::InvalidInputData),
        };

        let mut dt = self.get_datetime()?;
        dt.hours = hours_24;
        self.set_datetime(&dt)
    }

    fn set_time(&mut self, time: &RtccNaiveTime) -> Result<(), Self::Error> {
        let time = Time {
            hours: time.hour() as u8,
            minutes: time.minute() as u8,
            seconds: time.second() as u8,
        };
        self.set_time(&time)
    }

    fn set_weekday(&mut self, weekday: u8) -> Result<(), Self::Error> {
        if !(1..=7).contains(&weekday) {
            return Err(RtcError::InvalidInputData);
        }

        let mut dt = self.get_datetime()?;
        dt.weekday = weekday - 1;
        self.set_datetime(&dt)
    }

    fn set_day(&mut self, day: u8) -> Result<(), Self::Error> {
        let mut dt = self.get_datetime()?;
        dt.day = day;
        self.set_datetime(&dt)
    }

    fn set_month(&mut self, month: u8) -> Result<(), Self::Error> {
        let mut dt = self.get_datetime()?;
        dt.month = month;
        self.set_datetime(&dt)
    }

    fn set_year(&mut self, year: u16) -> Result<(), Self::Error> {
        if !(1900..=2099).contains(&year) {
            return Err(RtcError::InvalidInputData);
        }

        let mut dt = self.get_datetime()?;
        dt.year = (year % 100) as u8;
        self.set_century_flag(year < 2000)?;
        self.set_datetime(&dt)
    }

    fn set_date(&mut self, date: &RtccNaiveDate) -> Result<(), Self::Error> {
        let year = date.year();
        if !(1900..=2099).contains(&year) {
            return Err(RtcError::InvalidInputData);
        }

        let mut dt = self.get_datetime()?;
        dt.year = (year % 100) as u8;
        dt.month = date.month() as u8;
        dt.day = date.day() as u8;
        dt.weekday = date.weekday().num_days_from_sunday() as u8;

        self.set_century_flag(year < 2000)?;
        self.set_datetime(&dt)
    }
}