cat24c32-rs 0.1.0

Platform-agnostic Rust driver for CAT24C32 EEPROM Serial 32Kb I2C devices
Documentation

cat24c32-rs

Crates.io Documentation License: MIT OR Apache-2.0

Platform-agnostic Rust driver for the CAT24C32 EEPROM Serial 32-Kb I2C device using the embedded-hal traits.

The Device

The CAT24C32 is an EEPROM Serial 32-Kb I2C device, internally organized as 4096 words of 8 bits each. It features a 32-byte page write buffer and supports the Standard (100 kHz), Fast (400 kHz) and Fast-Plus (1 MHz) I2C protocol. External address pins make it possible to address up to eight CAT24C32 devices on the same bus.

Features

  • Supports Standard, Fast and Fast-Plus I2C Protocol
  • 1.7 V to 5.5 V Supply Voltage Range
  • 32-Byte Page Write Buffer
  • Hardware Write Protection for Entire Memory
  • Schmitt Triggers and Noise Suppression Filters on I2C Bus Inputs (SCL and SDA)
  • Low Power CMOS Technology
  • 1,000,000 Program/Erase Cycles
  • 100 Year Data Retention
  • Industrial and Extended Temperature Range
  • PDIP, SOIC, TSSOP, UDFN, US 8-lead, WLCSP 4-ball and 5-ball Packages
  • This Device is Pb-Free, Halogen Free/BFR Free, and RoHS Compliant

Datasheet: CAT24C32

Usage

Add this to your Cargo.toml:

[dependencies]
cat24c32-rs = "0.1.0"
embedded-hal = "1.0.0"

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

use cat24c32_rs::{Cat24c32, SlaveAddr};
use linux_embedded_hal::I2cdev;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let dev = I2cdev::new("/dev/i2c-1")?;
    let mut eeprom = Cat24c32::new(dev, SlaveAddr::Default);

    // Write a byte
    eeprom.write_byte(0x00, 0x42)?;

    // Read a byte
    let value = eeprom.read_byte(0x00)?;
    println!("Read value: 0x{:02X}", value);

    // Write a page (up to 32 bytes)
    let data = [0xAA; 32];
    eeprom.write_page(0x00, &data)?;

    // Read multiple bytes
    let mut buffer = [0u8; 32];
    eeprom.read_data(0x00, &mut buffer)?;
    println!("Read data: {:?}", buffer);

    Ok(())
}

Using with embedded-storage

The driver also implements the embedded-storage traits for ecosystem compatibility:

use cat24c32_rs::Cat24c32;
use embedded_storage::{ReadStorage, Storage};
use embedded_storage::nor_flash::NorFlash;

fn storage_example() -> Result<(), Box<dyn std::error::Error>> {
    let dev = I2cdev::new("/dev/i2c-1")?;
    let mut eeprom = Cat24c32::new(dev, SlaveAddr::Default);

    // Using ReadStorage trait
    let mut buffer = [0u8; 64];
    ReadStorage::read(&mut eeprom, 0x0000, &mut buffer)?;
    
    // Using NorFlash trait (automatic page write optimization)
    let data = b"Hello, embedded-storage!";
    NorFlash::write(&mut eeprom, 0x0100, data)?;
    
    // Check capacity
    assert_eq!(ReadStorage::capacity(&eeprom), 4096); // 4KB

    Ok(())
}

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.