eeprom24x - Rust (original) (raw)

Expand description

This is a platform agnostic Rust driver for the 24x series serial EEPROM, based on the embedded-hal traits.

This driver allows you to:

If an embedded_hal::timer::CountDown is available, the embedded-storage traits can additionally be used which allow to read the device capacity and write over page boundaries. To achieve the latter, the Eeprom24x has to be wrapped with Storage::new.

Can be used at least with the devices listed below.

§The devices

These devices provides a number of bits of serial electrically erasable and programmable read only memory (EEPROM) organized as a number of words of 8 bits each. The devices’ cascadable feature allows up to 8 devices to share a common 2-wire bus. The devices are optimized for use in many industrial and commercial applications where low power and low voltage operation are essential.

Device Memory bits 8-bit words Page size Datasheet
24x00 128 bits 16 N/A 24C00
24x01 1 Kbit 128 8 bytes AT24C01
M24x01 1 Kbit 128 16 bytes M24C01
24x02 2 Kbit 256 8 bytes AT24C02
M24x02 2 Kbit 256 16 bytes M24C02
24x04 4 Kbit 512 16 bytes AT24C04
24x08 8 Kbit 1,024 16 bytes AT24C08
24x16 16 Kbit 2,048 16 bytes AT24C16
24x32 32 Kbit 4,096 32 bytes AT24C32
24x64 64 Kbit 8,192 32 bytes AT24C64
24x128 128 Kbit 16,384 64 bytes AT24C128
24x256 256 Kbit 32,768 64 bytes AT24C256
24x512 512 Kbit 65,536 128 bytes AT24C512
24xM01 1 Mbit 131,072 256 bytes AT24CM01
24xM02 2 Mbit 262,144 256 bytes AT24CM02

§Features

§defmt-03

To enable defmt (version 0.3.x) support, when specifying the dependency on eeprom24x, add the feature “defmt-03”.

[dependencies]
eeprom24x = { version = "0.7.2", features = ["defmt-03"] }

§Usage examples (see also examples folder)

To create a new instance you can use the new_<device> methods. There are many compatible vendors so the method has a somewhat generic name. For example, if you are using an AT24C32, you can create a device by callingEeprom24x::new_24x32(...). Please refer to the device table above for further examples of device names.

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

§Instantiating with the default address

Import this crate and an embedded_hal implementation, then instantiate the device:

use linux_embedded_hal::I2cdev;
use eeprom24x::{ Eeprom24x, SlaveAddr };

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
// using the AT24C256
let mut eeprom = Eeprom24x::new_24x256(dev, address);

§Providing an alternative address

use linux_embedded_hal::I2cdev;
use eeprom24x::{ Eeprom24x, SlaveAddr };

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let (a2, a1, a0) = (false, false, true);
let address = SlaveAddr::Alternative(a2, a1, a0);
let mut eeprom = Eeprom24x::new_24x256(dev, address);

§Writing and reading a byte

use linux_embedded_hal::I2cdev;
use eeprom24x::{ Eeprom24x, SlaveAddr };

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut eeprom = Eeprom24x::new_24x256(dev, SlaveAddr::default());
let address = 0x1234;
let data = 0xAB;
eeprom.write_byte(address, data);
// EEPROM enters internally-timed write cycle. Will not respond for some time.
let retrieved_data = eeprom.read_byte(address);

§Writing a page

use linux_embedded_hal::I2cdev;
use eeprom24x::{ Eeprom24x, SlaveAddr };

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut eeprom = Eeprom24x::new_24x256(dev, SlaveAddr::default());
let address = 0x1234;
let data = [0xAB; 64];
eeprom.write_page(address, &data);
// EEPROM enters internally-timed write cycle. Will not respond for some time.

§Using embedded-storage traits

use linux_embedded_hal::{I2cdev, Delay};
use eeprom24x::{ Eeprom24x, SlaveAddr, Storage };
use embedded_storage::{ReadStorage, Storage as _};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let eeprom = Eeprom24x::new_24x256(dev, SlaveAddr::default());
let mut storage = Storage::new(eeprom, Delay {});
let _capacity = storage.capacity();
let address = 0x1234;
let data = [0xAB; 256];
storage.write(address, &data);
// EEPROM writes four pages. This introduces a delay of at least 20 ms, 5 ms per page.

addr_size

Memory address size markers

page_size

Page size markers

unique_serial

Factory-supplied unique serial number markers

Eeprom24x

EEPROM24X driver

Storage

EEPROM24X extension which supports the embedded-storage traits but requires anembedded_hal::delay::DelayNs to handle the timeouts when writing over page boundaries

Error

All possible errors in this crate

SlaveAddr

Possible slave addresses

Eeprom24xTrait

Eeprom24x type trait for use in generic code