Crate ds1302[][src]

Expand description

DS1302 platform agnostic driver crate

About

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

The driver allows to:

  • Read and set clock and calendar data in 12-hour or 24-hour format .
  • Changing hour format without reseting it. set_clock_mode().

The driver does not allow to:

  • Currently using RAM is not supported.

Initialization

// 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();

Read time and date

let mut data = String::<U32>::from(" ");

let cl = ds1302.get_clock_calendar().unwrap();
let (text, h) = match cl.0.hours {
    Hours::Hour24(h) => ("", h),
    Hours::Hour12am(h) => ("am", h),
    Hours::Hour12pm(h) => ("pm", h),
};
// 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,
            h, cl.0.minutes, cl.0.seconds, text);

From lib.rs:

pub enum Hours {
    Hour24(u8),
    Hour12am(u8),
    Hour12pm(u8),
}
pub enum Mode {
    Hour24,
    Hour12,
}

Set time and date

let clk = Clock {
    hours: Hours::Hour12pm(4),
    minutes: 29,
    seconds: 0
};
let cal = Calendar {
    day: 2,
    date: 27,
    month: 10,
    year: 2020
};
ds1302.set_clock_calendar(clk, cal).unwrap();

Structs

Calendar information

Clock information

DS1302 RTCC driver

Enums

Hour information: 12-hour (AM/PM) or 24-hour

Hour format: 12-hour (AM/PM) or 24-hour

Traits

For timing ds1302 uses fugit crate which only provides Duration and Instant types. It does not provide any clock or timer traits. Therefore ds1302 has its own Delay trait that provides all timing capabilities that are needed for the library. User must implement this trait for the timer by itself.