#real-time-clock #driver #rtc #embedded-hal-driver

no-std mcp794xx

Platform-agnostic Rust driver for the MCP794xx real-time clock / calendar (RTC) family. Compatible with MCP7940N, MCP7940M, MCP79400, MCP79401, MCP79402, MCP79410, MCP79411 and MCP79412.

4 releases (2 breaking)

0.3.0 Aug 16, 2022
0.2.1 Sep 8, 2021
0.2.0 Feb 9, 2020
0.1.0 Sep 15, 2019

#1960 in Embedded development

MIT/Apache

74KB
1K SLoC

Rust MCP794xx Real-Time Clock / Calendar Family Driver

crates.io Docs MSRV Build Status Coverage Status

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

This driver allows you to:

  • Read and set date and time. See: datetime().
  • Read and set date. See: date().
  • Read and set time. See: time().
  • Read and set date and time individual elements. For example, see: year().
  • Enable and disable the real-time clock. See: enable().
  • Read whether the oscillator is running. See: is_oscillator_running().
  • Read whether the current year is a leap year. See: is_leap_year().
  • Enable and disable the usage of an external oscillator source. See: `enable_external_oscillator.
  • Set the output pin logic level. See: set_output_pin().
  • Enable and disable coarse trim. See: enable_coarse_trim().
  • Set trimming value. See: set_trimming().
  • Power:
    • Read whether the power has failed. See: has_power_failed().
    • Clear the has-power-failed flag. See: clear_power_failed().
    • Read the date/time when power went down. See: get_power_down_datetime().
    • Read the date/time when power went back up. See: get_power_up_datetime().
    • Enable and disable usage of backup battery power. See: enable_backup_battery_power().
  • SRAM:
    • Read and write byte to SRAM. See: read_sram_byte().
    • Read and write byte array to SRAM. See: read_sram_data().
    • Read current position from SRAM. See: read_sram_current_byte().
  • Alarms:
    • Enable and disable alarms. See: enable_alarm().
    • Set alarms with several matching policies and output pin polarities. See: set_alarm.
    • Read whether alarms have matched. See: has_alarm_matched.
    • Clear flag indicating that alarms have matched. See: clear_alarm_matched_flag.
  • Wave generation:
    • Enable and disable the square-wave generation. See: enable_square_wave.
    • Select the square-wave frequency. See: set_square_wave_frequency.
  • Protected EEPROM:
    • Read and write byte to the protected EEPROM. See: read_protected_eeprom_byte().
    • Read and write byte array to the protected EEPROM. See: read_protected_eeprom_data().
    • Read EUI-48. See: read_eui48().
    • Read EUI-64. See: read_eui64().
  • EEPROM:
    • Read and write byte to the EEPROM. See: read_eeprom_byte().
    • Read and write byte array to the EEPROM. See: read_eeprom_data().
    • Set EEPROM block write protection. See: set_eeprom_write_protection().
    • Read current position from the EEPROM. See: read_eeprom_current_byte().

Introductory blog post

The devices

This driver is compatible with the devices: MCP7940N, MCP7940M, MCP79400, MCP79401, MCP79402, MCP79410, MCP79411 and MCP79412.

The Real-Time Clock/Calendar (RTCC) tracks time using internal counters for hours, minutes, seconds, days, months, years, and day of week. Alarms can be configured on all counters up to and including months. For usage and configuration, the devices support I2C communications up to 400 kHz.

The open-drain, multi-functional output can be configured to assert on an alarm match, to output a selectable frequency square wave, or as a general purpose output.

The devices are designed to operate using a 32.768 kHz tuning fork crystal with external crystal load capacitors. On-chip digital trimming can be used to adjust for frequency variance caused by crystal tolerance and temperature.

SRAM and timekeeping circuitry are powered from the back-up supply when main power is lost, allowing the device to maintain accurate time and the SRAM contents. The times when the device switches over to the back-up supply and when primary power returns are both logged by the power-fail time-stamp.

Some of the devices feature 1 Kbit of internal non-volatile EEPROM with software write-protectable regions. There is an additional 64 bits of protected non-volatile memory which is only writable after an unlock sequence, making it ideal for storing a unique ID or other critical information.

Some of the devices offer a pre-programmed with EUI-48 and EUI-64 addresses. Custom programming is also available.

Datasheets:

Usage

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

In the following example an instance of the device MCP7940N will be created. Other devices can be created with similar methods like: Mcp794xx::new_mcp79400(...).

Please find additional examples using hardware in this repository: driver-examples

use linux_embedded_hal::I2cdev;
use mcp794xx::{DateTimeAccess, Mcp794xx, NaiveDate, Rtcc};

fn main() {
    let dev = I2cdev::new("/dev/i2c-1").unwrap();
    let mut rtc = Mcp794xx::new_mcp7940n(dev);
    rtc.enable().unwrap();
    let datetime = NaiveDate::from_ymd(2018, 8, 20).and_hms(19, 59, 58);
    rtc.set_datetime(&datetime).unwrap();
    rtc.enable().unwrap();
    // do something else...
    let seconds = rtc.seconds().unwrap();
    println!("Seconds: {}", seconds);

    let _dev = rtc.destroy();
}

Support

For questions, issues, feature requests, and other changes, please file an issue in the github project.

License

Licensed under either of

at your option.

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

~1.5MB
~19K SLoC