DS1302 real time clock-calendar platform agnostic driver
DS1302 is a real time clock/calendar (RTCC) chip, which communicates with SPI interface.
The device provides seconds, minutes, hours, day, date, month, and year information.
Driver is based on embedded-hal
traits.
Datasheet: DS1302
- Very importand: SPI frame format with LSB transmitted first!
- SPI speed less than 2 MHz
- CPOL: CK to 0 when idle, CPHA: the first clock transition is the first data capture edge
- Default 8-bit data frame format is selected for transmission/reception
More in -> https://github.com/Nekspire/ds1302-rs/tree/master/examples
- Read and set clock and calendar data in 12-hour or 24-hour format .
- Changing hour format without reseting it.
set_clock_mode()
- Currently using RAM is not supported.
// External crates for IO and strings manipulation
use core::fmt::Write;
use heapless::String;
// DS1302 driver crate
use ds1302::{DS1302, Hours, Clock, Calendar, Mode as ds1302_mode};
// Create with DS1302::new(), specify hour format mode: ds1302_mode::Hour12, in this case
let mut ds1302 = DS1302::new(spi, cs, ds1302_mode::Hour12).unwrap();
let mut data = String::<U32>::from(" ");
let mut text = " ";
let cl = ds1302.get_clock_calendar().unwrap();
// Check the current mode. If it is Hour12, check AM/PM value. Please refer to table description below.
match ds1302.mode {
ds1302_mode::Hour12 => {
if cl.0.hours.am_pm == 1 {text = " PM"}
else {text = " AM"}
}
ds1302_mode::Hour24 => text = ""
}
// Glue cl reads in string called "data", and use it later ...
let _=write!(data,"{} {}.{}.{}\n{:02}:{:02}:{:02} {}",
cl.1.day, cl.1.date, cl.1.month, cl.1.year,
cl.0.hours.hours, cl.0.minutes, cl.0.seconds, text);
From lib.rs:
pub struct Hours {
pub hours: u8,
pub am_pm: u8,
}
pub enum Mode {
Hour24,
Hour12,
}
Mode | am_pm | time format |
---|---|---|
Hour12 | 0 | AM |
Hour12 | 1 | PM |
Hour24 | 0 | - |
Hour24 | 1 | - |
let h = Hours {hours: 4, am_pm: 1};
let clk = Clock {
hours: h,
minutes: 29,
seconds: 0
};
let cal = Calendar {
day: 2,
date: 27,
month: 10,
year: 2020
};
ds1302.set_clock_calendar(clk, cal).unwrap();
Copyright © 2020 Nekspire
Dual licensed under your choice of either of:
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)