rv8803 4.0.0

RTC clock driver for the rv8803 chip via I2C
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
use crate::{formatter::ByteMutWriter, log::LoggableClockData};
use core::fmt::{Debug, Write};

/// Holds the clock data.
#[derive(Debug, Copy, Clone, Default)]
pub struct ClockData {
    /// Hundredths.
    pub hundredths: u8,
    /// Seconds.
    pub seconds: u8,
    /// Minutes.
    pub minutes: u8,
    /// Hours.
    pub hours: u8,
    /// Weekday.
    pub weekday: u8,
    /// Date.
    pub date: u8,
    /// Month.
    pub month: u8,
    /// Year.
    pub year: u8,
}

impl ClockData {
    /// Creates a [`ClockData`].
    #[must_use]
    pub fn new() -> ClockData {
        ClockData {
            ..Default::default()
        }
    }

    /// Hundredths.
    #[must_use]
    pub fn hundredths(&self) -> u8 {
        self.hundredths
    }

    /// Seconds.
    #[must_use]
    pub fn seconds(&self) -> u8 {
        self.seconds
    }

    /// Minutes.
    #[must_use]
    pub fn minutes(&self) -> u8 {
        self.minutes
    }

    /// Hours.
    #[must_use]
    pub fn hours(&self) -> u8 {
        self.hours
    }

    /// Weekday.
    #[must_use]
    pub fn weekday(&self) -> u8 {
        self.weekday
    }

    /// Day. The rtc module refers to this as "date".
    #[must_use]
    pub fn date(&self) -> u8 {
        self.date
    }

    /// Month.
    #[must_use]
    pub fn month(&self) -> u8 {
        self.month
    }

    /// Year.
    #[must_use]
    pub fn year(&self) -> u8 {
        self.year
    }

    /// Set the date and time.  Hundredths is set to 0.
    pub fn set(&mut self, value: &ClockData) {
        *self = *value;
    }

    fn _set(&mut self, value: (u8, u8, u8, Weekday, u8, Month, CurrentYear)) {
        let (hours, minutes, seconds, weekday, day, month, year) = value;

        self.hundredths = 0;
        self.hours = hours;
        self.minutes = minutes;
        self.seconds = seconds;
        self.weekday = weekday as u8;
        self.date = day;
        self.month = month as u8;
        self.year = year.0;
    }
}

/// Creates a tuple to hold the current year.
#[derive(Debug, Default)]
pub struct CurrentYear(u8);

impl CurrentYear {
    /// Provides the current year as a [`u8`].
    pub fn new(value: u16) -> Self {
        if value < 1970 {
            panic!("Year must be greater than 1970");
        }

        if value < 2000 {
            Self((value - 1900) as u8)
        } else {
            Self((value - 2000) as u8)
        }
    }
}

impl defmt::Format for LoggableClockData {
    fn format(&self, fmt: defmt::Formatter) {
        let data = self.data();

        let mut buf = [0u8; 2];
        let mut buf = ByteMutWriter::new(&mut buf[..]);
        let hours = left_pad(&mut buf, data.hours);

        let mut buf = [0u8; 2];
        let mut buf = ByteMutWriter::new(&mut buf[..]);
        let minutes = left_pad(&mut buf, data.minutes);

        let mut buf = [0u8; 2];
        let mut buf = ByteMutWriter::new(&mut buf[..]);
        let seconds = left_pad(&mut buf, data.seconds);

        let mut buf = [0u8; 2];
        let mut buf = ByteMutWriter::new(&mut buf[..]);
        let day = left_pad(&mut buf, data.date);

        let month = Month::from(data.month);
        let weekday = Weekday::from(data.weekday);

        let mut buf = [0u8; 4];
        let mut buf = ByteMutWriter::new(&mut buf[..]);
        let year = pad_year(&mut buf, data.year, self.century());

        defmt::write!(
            fmt,
            "{}:{}:{}, {}, {} {} {}",
            hours,
            minutes,
            seconds,
            weekday,
            day,
            month,
            year,
        );
    }
}

/// Enumerated type values for the weekday register.
#[derive(Debug, Default)]
#[allow(dead_code)]
pub enum Weekday {
    #[default]
    /// Sunday
    Sunday = 1,
    /// Monday
    Monday = 2,
    /// Tuesday
    Tuesday = 4,
    /// Wednesday
    Wednesday = 8,
    /// Thursday
    Thursday = 16,
    /// Friday
    Friday = 32,
    /// Saturday
    Saturday = 64,
}

// FIXME this causes [defmt] to get stuck. Why?
// impl From<u8> for Weekday {
//     fn from(value: u8) -> Self {
//         Self::into(value.into())
//     }
// }
#[allow(dead_code)]
#[allow(clippy::match_same_arms)]
impl Weekday {
    /// Get variant from a provided value.
    #[must_use]
    pub fn from(val: u8) -> Self {
        match val {
            1 => Self::Sunday,
            2 => Self::Monday,
            4 => Self::Tuesday,
            8 => Self::Wednesday,
            16 => Self::Thursday,
            32 => Self::Friday,
            64 => Self::Saturday,
            _ => Self::Sunday,
        }
    }
}

impl defmt::Format for Weekday {
    fn format(&self, fmt: defmt::Formatter) {
        defmt::write!(
            fmt,
            "{}",
            match self {
                Self::Sunday => "Sunday",
                Self::Monday => "Monday",
                Self::Tuesday => "Tuesday",
                Self::Wednesday => "Wednesday",
                Self::Thursday => "Thursday",
                Self::Friday => "Friday",
                Self::Saturday => "Saturday",
            }
        );
    }
}

/// Enumerated type values for the month register.
#[allow(dead_code)]
#[derive(Debug, Default)]
pub enum Month {
    #[default]
    /// January
    January = 1,
    /// February
    February = 2,
    /// March
    March = 3,
    /// April
    April = 4,
    /// May
    May = 5,
    /// June
    June = 6,
    /// July
    July = 7,
    /// August
    August = 8,
    /// September
    September = 9,
    /// October
    October = 10,
    /// November
    November = 11,
    /// December
    December = 12,
}

#[allow(dead_code)]
#[allow(clippy::match_same_arms)]
impl Month {
    /// Get variant from a provided value.
    #[must_use]
    pub fn from(val: u8) -> Self {
        match val {
            1 => Self::January,
            2 => Self::February,
            3 => Self::March,
            4 => Self::April,
            5 => Self::May,
            6 => Self::June,
            7 => Self::July,
            8 => Self::August,
            9 => Self::September,
            10 => Self::October,
            11 => Self::November,
            12 => Self::December,
            _ => Self::January,
        }
    }
}

impl defmt::Format for Month {
    fn format(&self, fmt: defmt::Formatter) {
        defmt::write!(
            fmt,
            "{}",
            match self {
                Self::January => "January",
                Self::February => "February",
                Self::March => "March",
                Self::April => "April",
                Self::May => "May",
                Self::June => "June",
                Self::July => "July",
                Self::August => "August",
                Self::September => "September",
                Self::October => "October",
                Self::November => "November",
                Self::December => "December",
            }
        );
    }
}

/// Enumerated type values for the year.
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
pub enum Year {
    /// 20th Century Fox. Definitely, better times.
    TwentiethCentury(u8),
    /// Reality.
    TwentyFirstCentury(u8),
}

impl Default for Year {
    fn default() -> Self {
        Self::TwentyFirstCentury(20)
    }
}

/// Creates a [`DateTimeBuilder`] to set the time.
#[derive(Debug, Default)]
pub struct DateTimeBuilder {
    hours: u8,
    minutes: u8,
    seconds: u8,
    date: u8,
    weekday: Weekday,
    month: Month,
    year: CurrentYear,
}

impl DateTimeBuilder {
    /// Creates a new [`DateTimeBuilder`], defaulting to the UNIX epoch.
    #[must_use]
    pub fn new() -> Self {
        Self {
            hours: 0,
            minutes: 0,
            seconds: 0,
            date: 1,
            weekday: Weekday::Thursday,
            month: Month::January,
            year: CurrentYear::new(1970),
        }
    }

    /// Set the hours.
    #[must_use]
    pub fn hours(mut self, value: u8) -> Self {
        self.hours = value;
        self
    }

    /// Set the minutes.
    #[must_use]
    pub fn minutes(mut self, value: u8) -> Self {
        self.minutes = value;
        self
    }

    /// Set the seconds.
    #[must_use]
    pub fn seconds(mut self, value: u8) -> Self {
        self.seconds = value;
        self
    }

    /// Set the date.
    #[must_use]
    pub fn date(mut self, value: u8) -> Self {
        self.date = value;
        self
    }

    /// Set the weekday.
    #[must_use]
    pub fn weekday(mut self, value: Weekday) -> Self {
        self.weekday = value;
        self
    }

    /// Set the month.
    #[must_use]
    pub fn month(mut self, value: Month) -> Self {
        self.month = value;
        self
    }

    /// Set the year.
    #[must_use]
    pub fn year(mut self, value: CurrentYear) -> Self {
        self.year = value;
        self
    }

    /// Build the time.
    #[must_use]
    pub fn build(self) -> ClockData {
        let mut data = ClockData::new();
        data._set((
            self.hours,
            self.minutes,
            self.seconds,
            self.weekday,
            self.date,
            self.month,
            self.year,
        ));

        data
    }
}

fn left_pad<'a>(buf: &'a mut ByteMutWriter<'_>, value: u8) -> &'a str {
    buf.clear();
    write!(buf, "{}{}", common_padding(value), value).unwrap();

    buf.as_str()
}

fn pad_year<'a>(buf: &'a mut ByteMutWriter<'_>, value: u8, century: Year) -> &'a str {
    buf.clear();

    match century {
        Year::TwentiethCentury(_) => write!(buf, "19{}{}", common_padding(value), value).unwrap(),
        Year::TwentyFirstCentury(_) => write!(buf, "20{}{}", common_padding(value), value).unwrap(),
    }

    buf.as_str()
}

fn common_padding<'a>(value: u8) -> &'a str {
    if value < 10 {
        "0"
    } else {
        ""
    }
}

#[allow(dead_code)]
pub mod misc {
    pub fn bcd_to_dec(value: u8) -> u8 {
        ((value / 0x10) * 10) + (value % 0x10)
    }

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