3 unstable releases

0.2.1 Jan 30, 2019
0.2.0 Sep 7, 2018
0.1.0 Aug 15, 2018

#41 in Embedded development

Download history 5/week @ 2018-12-16 8/week @ 2018-12-23 3/week @ 2018-12-30 5/week @ 2019-01-06 3/week @ 2019-01-13 9/week @ 2019-01-27 3/week @ 2019-02-03 1/week @ 2019-02-10 44/week @ 2019-02-17 21/week @ 2019-02-24 7/week @ 2019-03-03 20/week @ 2019-03-10 13/week @ 2019-03-17 15/week @ 2019-03-24

51 downloads per month

MIT/Apache

31KB
428 lines

Rust DS1307 Real-Time Clock Driver

crates.io Docs Build Status Coverage Status Maintenance Intention

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

This driver allows you to:

  • Read and set date and time in 12-hour and 24-hour format. See: get_datetime
  • Enable and disable the real-time clock. See: set_running
  • Read and write user RAM. See: read_ram
  • Control square-wave output. See: enable_square_wave_output

Introductory blog post

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

Usage

To use this driver, import this crate and an embedded_hal implementation, then instantiate the device.

extern crate ds1307;
extern crate linux_embedded_hal as hal;

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

fn main() {
    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();

    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
}

License

Licensed under either of

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~95KB