#rtc #i2c #ds3231

no-std ds3231

A platform-agnostic driver for the DS3231 precision real-time clock

1 unstable release

new 0.1.0 May 4, 2025

#503 in Embedded development

MIT/Apache

71KB
1.5K SLoC

DS3231 Real-Time Clock Driver

Crates.io Documentation License

A platform-agnostic Rust driver for the DS3231 Real-Time Clock, built on the embedded-hal ecosystem. The DS3231 is a low-cost, extremely accurate I²C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal.

  • Both blocking and async I²C operation support
  • Full register access (time/date, alarms, control, status)
  • Optional logging support via log or defmt
  • No unsafe code
  • Comprehensive error handling

Usage

Add this to your Cargo.toml:

[dependencies]
ds3231 = "0.1"  # Replace with actual version

Blocking Example

use ds3231::{DS3231, Config, TimeRepresentation, SquareWaveFrequency, InterruptControl, Ocillator};

// Create configuration
let config = Config {
    time_representation: TimeRepresentation::TwentyFourHour,
    square_wave_frequency: SquareWaveFrequency::Hz1,
    interrupt_control: InterruptControl::SquareWave,
    battery_backed_square_wave: false,
    oscillator_enable: Ocillator::Enabled,
};

// Initialize device with I2C
let mut rtc = DS3231::new(i2c, 0x68);

// Configure the device
rtc.configure(&config)?;

// Get current date/time
let datetime = rtc.datetime()?;

Async Example

Enable the async feature in your Cargo.toml:

[dependencies]
ds3231 = { version = "0.1", features = ["async"] }

Then use with async/await:

use ds3231::asynch::DS3231;

// Initialize device
let mut rtc = DS3231::new(i2c, 0x68);

// Configure asynchronously
rtc.configure(&config).await?;

// Get current date/time asynchronously
let datetime = rtc.datetime().await?;

Features

The crate can be compiled with the following features:

  • async: Enables async I²C support
  • log: Enables logging via the log crate
  • defmt: Enables logging via the defmt crate

License

Licensed under either of:

at your option.

Contribution

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.2–2MB
~36K SLoC