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
//! RV8803 driver for I2C.
#![no_std]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![cfg_attr(feature = "async", allow(incomplete_features))]
#![cfg_attr(docsrs, feature(doc_cfg), feature(doc_auto_cfg))]
#![feature(error_generic_member_access)]
#![feature(trivial_bounds)]

use crate::bus::{Bus, BusTrait};
pub use embedded_hal_0_2;
use models::{Register, Rv8803, TIME_ARRAY_LENGTH};

/// RV8803 I2C bus implementation with embedded-hal version 0.2
pub mod bus;

/// Models
pub(crate) mod models;

pub(crate) mod error;

/// Re-exports
pub mod prelude {
    pub use crate::bus::Bus;
    pub use crate::error::Error;
}

#[allow(dead_code)]
impl<'a, I2C, E> Rv8803<Bus<'a, I2C>>
where
    I2C: embedded_hal_0_2::blocking::i2c::WriteRead<Error = E>
        + embedded_hal_0_2::blocking::i2c::Write<Error = E>,
    Bus<'a, I2C>: bus::BusTrait<Error = E>,
{
    /// Create a new RV8803 from a [`crate::driver::Bus`].
    ///
    /// # Errors
    ///
    /// If this function encounters an error, it will be returned.
    pub fn new(bus: Bus<'a, I2C>) -> Self {
        Self { bus }
    }

    /// Creates a new `Rv8803` driver from a I2C peripheral, and an I2C
    /// device address.
    pub fn from_i2c(i2c: I2C, address: u8) -> Self {
        let bus = crate::bus::Bus::new(i2c, &address);

        Self::new(bus)
    }

    /// Creates a new `Rv8803` driver from a I2C peripheral, and an I2C
    /// device address.
    pub fn from_bus(i2c: I2C, address: u8) -> Self {
        let bus = crate::bus::Bus::new(i2c, &address);

        Self::new(bus)
    }

    /// Set time on the RV8803 module
    ///
    /// # Errors
    ///
    /// If the year specified is always > 2000, hence `u16` casting to `u8`
    /// is OK, as long as the year is < 2100. When the year > 2255 this will
    /// return [`core::num::TryFromIntError`].
    ///
    /// Read/write errors during communication with the `rv8803` chip will also return an error.
    #[allow(clippy::too_many_arguments)]
    pub fn set_time(
        &mut self,
        sec: u8,
        min: u8,
        hour: u8,
        weekday: u8,
        date: u8,
        month: u8,
        year: u16,
    ) -> Result<bool, E>
    where
        E: core::convert::From<core::num::TryFromIntError>,
    {
        self.bus
            .write_register(Register::Seconds, dec_to_bcd(sec))?;
        self.bus
            .write_register(Register::Minutes, dec_to_bcd(min))?;
        self.bus.write_register(Register::Hours, dec_to_bcd(hour))?;
        self.bus.write_register(Register::Date, dec_to_bcd(date))?;
        self.bus
            .write_register(Register::Month, dec_to_bcd(month))?;
        self.bus
            .write_register(Register::Year, dec_to_bcd(u8::try_from(year - 2000)?))?;
        self.bus.write_register(Register::Weekday, weekday)?;

        // Set RESET bit to 0 after setting time to make sure seconds don't get stuck.
        self.write_bit(
            Register::Control.address(),
            Register::ControlReset.address(),
            false,
        )?;

        defmt::debug!("rv8803::set_time: updated RTC clock");

        Ok(true)
    }

    /// Fetch time from the RTC clock and store it in the buffer `dest`.
    pub fn update_time(&mut self, dest: &mut [u8]) -> Result<bool, E> {
        if !(self.bus.read_multiple_registers(
            Register::Hundredths.address(),
            dest,
            TIME_ARRAY_LENGTH,
        )?) {
            defmt::warn!("update_time: attempt read - fail 1");
            return Ok(false); // Something went wrong
        }

        // If hundredths are at 99 or seconds are at 59, read again to make sure we didn't accidentally skip a second/minute
        if bcd_to_dec(dest[0]) == 99 || bcd_to_dec(dest[1]) == 59 {
            let mut temp_time = [0_u8; TIME_ARRAY_LENGTH];

            defmt::debug!("update_time: if hundredths are at 99 or seconds are at 59, read again to make sure we didn't accidentally skip a second/minute / Hundreths: {} / Seconds: {}", bcd_to_dec(dest[0]),bcd_to_dec(dest[1]));

            if !(self.bus.read_multiple_registers(
                Register::Hundredths.address(),
                &mut temp_time,
                TIME_ARRAY_LENGTH,
            )?) {
                defmt::warn!("update_time: attempt read - fail 2");
                return Ok(false); // Something went wrong
            };

            // If the reading for hundredths has rolled over, then our new data is correct, otherwise, we can leave the old data.
            if bcd_to_dec(dest[0]) > bcd_to_dec(temp_time[0]) {
                defmt::debug!("update_time: the reading for hundredths has rolled over, then our new data is correct. / Hundreths: {} / temp_time[0]: {}",
                bcd_to_dec(dest[0]),
                bcd_to_dec(temp_time[0]));

                for (i, el) in temp_time.iter().enumerate() {
                    dest[i] = *el;
                }
            }
        }

        // byte order: https://github.com/sparkfun/SparkFun_RV-8803_Arduino_Library/blob/main/src/SparkFun_RV8803.h#L129-L138
        let mut buf = [0_u8; 8];
        for (i, el) in dest.iter().enumerate() {
            // Note: Weekday does not undergo BCD to Decimal conversion.
            if i == 4 {
                buf[i] = *el;
            } else {
                defmt::info!("Raw: {} / BCD to Dec: {}", *el, bcd_to_dec(*el));
                buf[i] = bcd_to_dec(*el);
            }
        }

        dest.copy_from_slice(&buf[..dest.len()]);

        Ok(true)
    }

    /// Write a single bit to the specified register
    pub fn write_bit(&mut self, reg_addr: u8, bit_addr: u8, bit_to_write: bool) -> Result<bool, E> {
        let mut value = 0;
        if let Ok(reg_value) = self.bus.read_register_by_addr(reg_addr) {
            value = reg_value;
        }

        value &= !(1 << bit_addr);
        value |= u8::from(bit_to_write) << bit_addr;

        self.bus.write_register_by_addr(reg_addr, value)?;

        Ok(true)
    }

    /// Read seconds from the RTC clock
    pub fn read_seconds(&mut self) -> Result<u8, E> {
        let secs = self.bus.read_register(Register::Seconds)?;

        Ok(bcd_to_dec(secs))
    }

    /// Read the year from the RTC clock
    pub fn read_year(&mut self) -> Result<u8, E> {
        let year = self.bus.read_register(Register::Year)?;

        Ok(bcd_to_dec(year))
    }

    /// Set the year and update the RTC clock
    pub fn set_year(&mut self, year: u16) -> Result<u8, E>
    where
        E: core::convert::From<core::num::TryFromIntError>,
    {
        let year = dec_to_bcd(u8::try_from(year - 2000)?);

        self.bus.write_register(Register::Year, year)?;

        self.read_year()
    }
}

fn bcd_to_dec(value: u8) -> u8 {
    ((value / 0x10) * 10) + (value % 0x10)
}

fn dec_to_bcd(value: u8) -> u8 {
    ((value / 10) * 0x10) + (value % 10)
}

/// Module for [`crate::rtc::RTClock`]
#[allow(unused_imports)]
pub mod rtc {
    use crate::Rv8803;
    use chrono::{DateTime, Utc};
    use core::marker::PhantomData;
    use defmt::error;
    use heapless::String;
    use shared_bus::BusManager;

    /// The [`RTClock`] type is the interface for communicating with the `rv8803` rtc clock chip via a shared bus over I2C.
    #[allow(dead_code)]
    pub struct RTClock<'a, I2C, I2cErr, M> {
        datetime: Option<DateTime<Utc>>,
        phantom: PhantomData<&'a I2C>,
        bus_err: PhantomData<&'a I2cErr>,
        bus: BusManager<M>,
        device_address: u8,
    }

    #[allow(dead_code)]
    impl<'a, I2C, I2cErr, SharedBusMutex> RTClock<'a, I2C, I2cErr, SharedBusMutex>
    where
        I2C: embedded_hal_0_2::blocking::i2c::Write<Error = I2cErr>
            + embedded_hal_0_2::blocking::i2c::WriteRead<Error = I2cErr>,
        SharedBusMutex: shared_bus::BusMutex,
        <SharedBusMutex as shared_bus::BusMutex>::Bus: embedded_hal_0_2::blocking::i2c::Write<Error = I2cErr>
            + embedded_hal_0_2::blocking::i2c::WriteRead<Error = I2cErr>,
        I2cErr: defmt::Format,
    {
        /// Creates a new [`RTClock`].
        pub fn new(bus: BusManager<SharedBusMutex>, address: &u8) -> Self {
            Self {
                datetime: None,
                bus,
                phantom: PhantomData,
                bus_err: PhantomData,
                device_address: *address,
            }
        }

        /// Access the driver for the rtc clock.
        pub fn rtc(
            &self,
        ) -> Rv8803<crate::prelude::Bus<'_, shared_bus::I2cProxy<'_, SharedBusMutex>>> {
            let proxy = self.bus.acquire_i2c();

            Rv8803::from_i2c(proxy, self.device_address)
        }
    }

    /// The [`RTClock`] type is the interface for communicating with the `rv8803` rtc clock chip directly over I2C.
    #[allow(dead_code)]
    pub struct RTClockDirect<'a, I2C, I2cErr> {
        datetime: Option<DateTime<Utc>>,
        periph: I2C,
        bus_err: PhantomData<&'a I2cErr>,
        device_address: u8,
    }

    #[allow(dead_code)]
    impl<'a, I2C, I2cErr> RTClockDirect<'a, I2C, I2cErr>
    where
        I2C: embedded_hal_0_2::blocking::i2c::Write<Error = I2cErr>
            + embedded_hal_0_2::blocking::i2c::WriteRead<Error = I2cErr>,
        // I2cErr: defmt::Format,
    {
        /// Creates a new [`RTClock`].
        pub fn new(periph: I2C, address: &u8) -> Self {
            Self {
                datetime: None,
                periph,
                bus_err: PhantomData,
                device_address: *address,
            }
        }

        /// Access the driver for the rtc clock.
        pub fn rtc(self) -> Rv8803<crate::prelude::Bus<'static, I2C>> {
            Rv8803::from_i2c(self.periph, self.device_address)
        }
    }
}