Crate ds1307[][src]

This is a platform agnostic Rust driver for the DS1307 real-time clock, based on the embedded-hal traits.

The device

The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus.

The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.

The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

Datasheet: DS1307

Get the year

extern crate linux_embedded_hal as hal;
extern crate ds1307;

use hal::I2cdev;
use ds1307::DS1307;

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut rtc = DS1307::new(dev);
let year = rtc.get_year().unwrap();
println!("Year: {}", year);

Set the year

extern crate linux_embedded_hal as hal;
extern crate ds1307;

use hal::I2cdev;
use ds1307::DS1307;

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut rtc = DS1307::new(dev);
rtc.set_year(2018).unwrap();

Set the current date and time

extern crate linux_embedded_hal as hal;
extern crate ds1307;

use hal::I2cdev;
use ds1307::{DS1307, DateTime, Hours};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut rtc = DS1307::new(dev);
let datetime = DateTime {
                          year: 2018,
                          month: 08,
                          day: 15,
                          weekday: 4,
                          hour: Hours::H24(19),
                          minute: 59,
                          second: 58
               };
rtc.set_datetime(&datetime).unwrap();

Get the current date and time

extern crate linux_embedded_hal as hal;
extern crate ds1307;

use hal::I2cdev;
use ds1307::{DS1307, Hours};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut rtc = DS1307::new(dev);

let datetime = rtc.get_datetime().unwrap();

// The hours depend on the RTC running mode
match datetime.hour {
    Hours::H24(h) => println!("{}-{}-{}, {} {}:{}:{}", datetime.year,
                              datetime.month, datetime.day, datetime.weekday,
                              h, datetime.minute, datetime.second),
    Hours::AM(h) => println!("{}-{}-{}, {} {}:{}:{} AM", datetime.year,
                              datetime.month, datetime.day, datetime.weekday,
                              h, datetime.minute, datetime.second),
    Hours::PM(h) => println!("{}-{}-{}, {} {}:{}:{} PM", datetime.year,
                              datetime.month, datetime.day, datetime.weekday,
                              h, datetime.minute, datetime.second),
}
// This will print something like: 2018-08-15, 4 19:59:58

Structs

DS1307

DS1307 driver

DateTime

Date and time

Enums

Error

All possible errors in this crate

Hours

Hours in either 12-hour (AM/PM) or 24-hour format