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:

  • Read a single byte from a memory address. See read_byte().
  • Read a byte array starting on a memory address. See: read_data().
  • Read the current memory address (please read notes). See: read_current_address().
  • Write a byte to a memory address. See: write_byte().
  • Write a byte array (up to a memory page) to a memory address. See: write_page().

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.

DeviceMemory bits8-bit wordsPage sizeDatasheet
24x00128 bits16N/A24C00
24x011 Kbit1288 bytesAT24C01
M24x011 Kbit12816 bytesM24C01
24x022 Kbit2568 bytesM24C02
M24x022 Kbit25616 bytesAT24C02
24x044 Kbit51216 bytesAT24C04
24x088 Kbit1,02416 bytesAT24C08
24x1616 Kbit2,04816 bytesAT24C16
24x3232 Kbit4,09632 bytesAT24C32
24x6464 Kbit8,19232 bytesAT24C64
24x128128 Kbit16,38464 bytesAT24C128
24x256256 Kbit32,76864 bytesAT24C256
24x512512 Kbit65,536128 bytesAT24C512
24xM011 Mbit131,072256 bytesAT24CM01
24xM022 Mbit262,144256 bytesAT24CM02

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 calling Eeprom24x::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, SysTimer};
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, SysTimer::new());
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.

Modules

Memory address size markers

Page size markers

Structs

EEPROM24X driver

EEPROM24X extension which supports the embedded-storage traits but requires an embedded_hal::timer::CountDown to handle the timeouts when writing over page boundaries

Enums

All possible errors in this crate

Possible slave addresses