1.0.0 • Published 7 months ago

i2c-eeproms v1.0.0

Weekly downloads
-
License
GPL-2.0
Repository
github
Last release
7 months ago

Node.js I2C access for various AT24CXXX EEPROMS

This module supports the following I2C EEPROMs:

EEPROMVendorSize (bits)Page CountPage Size (bytes)Total Size (bytes)Write Delay(ms)Devices/Bus
AT24C01Atmel/Microchip1Kb16812858
AT24C02Atmel/Microchip2Kb32825658
AT24C04Atmel/Microchip4Kb321651254
AT24C08Atmel/Microchip8Kb6416102452
AT24C16Atmel/Microchip16Kb12816204851
AT24C32Atmel/Microchip32Kb128324096108
AT24C32CAtmel/Microchip32Kb12832409658
AT24C64Atmel/Microchip64Kb256328192108
AT24C64CAtmel/Microchip64Kb25632819258
AT24C128Atmel/Microchip128Kb2566416384104
AT24C128CAtmel/Microchip128Kb256641638458
AT24C256Atmel/Microchip256Kb5126432768104
AT24C256CAtmel/Microchip256Kb512643276858
K24C256CK-Line256Kb512643276858
AT24C512Atmel/Microchip512Kb51212865536104
AT24C512CAtmel/Microchip512Kb5121286553658
AT24CM01Atmel/Microchip1Mb51225613107254
AT24CM02Atmel/Microchip2Mb102425626214452
  • Note that the'C' revision of chips have a lower post-write delay requirement and may support more concurrent devices per bus.
  • For example, the AT24C32C chip has a 5ms post-write delay compared to 10ms for the AT24C32.
  • For example, the AT24C128/256/512C chip supports a maximum of 8 devices per bus compared to 4 for the AT24C128/256/512.

  • The K24C256C chip is a Chinese Brand delivered on a board purchased from Amazon US stating that it contained AT24C256. It is functionally identical to the AT24C256C.

Web Sites and Data Sheets

Various web sites were and data sheets were referenced during the development of this module. Links are provided below for convenience.

.NET nanoFramework API Reference Device Details Internet

AT24C01A/AT24C02/AT24C04/AT24C08A/AT24C16A datasheet from Microchip AT24C01 datasheet from Mouser Electronics USA AT24C01D/AT24C02D (Low Voltage - max 3.6V) datasheet from Microchip AT24C08D (Low Voltage - max 3.6V) datasheet from Microchip

AT24C32/AT24C64 datasheet from Microchip AT24C32C/AT24C64C datasheet from Microchip

AT24C128/AT24C256 datasheet from Microchip AT24C128C/AT24C256C datasheet from Microchip AT24C128C datasheet from Microchip AT24C256C datasheet from Microchip

AT24C512C datasheet from Microchip AT24CM01 datasheet from Microchip AT24CM02 datasheet from Microchip

K24C256C datasheet from K-Line

Supported Node.js versions

Supported (tested) Node.js versions: 10, 12, 14, 16, 18, 20

Installation

npm install i2c-eeproms

TypeScript typings are included in this package.

You should be able to use this module on any Linux based OS that support i2c-bus.

Example

This module requires an IC2Bus or Promisified bus instance to operate. Construct the i2c-bus object and pass it in the constructor along with the I2C address of the expander chip.

The AT24C256C example below can be found in the examples directory of this package together with a TypeScript example.

// Import the EEPROM class
//const ChipConstructor = require('i2c-eeproms').AT24C256C;
const ChipConstructor = require('../').AT24C256C;

// Import the i2c-bus module
const { openSync: I2CBusOpenSync, openPromisified: I2CBusOpenPromisified } = require('i2c-bus');

const example = async () => {
    // Adjust for your Raspberry Pi or other device
    const busNumber = 1;
    // Adjust for your EEPROM's address - for address 0x50, specify 0x50, for address 0x57, specify 0x57.
    // Some chips don't support 8 distinct addresses on the bus and as such an error will be thrown during construction.
    const i2cAddress = 0x50;

    // This library can accept either an I2CBus or a PromisifiedBus as an input parameter
    const i2cBus = await I2CBusOpenPromisified(busNumber);
    const chip = new ChipConstructor(i2cBus, i2cAddress);

    // Create text that is larger than 2 pages of an EEPROM
    const textIn = '00:0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{|}~' +
        '01!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~' +
        '02!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~' +
        '03!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~' +
        '04!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~' +
        '05!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~' +
        '06!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~' +
        '07!#?$%&()*+,-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^abcdefghijklmnopqrstuvwxyz{|}~';

    console.log('Read/Write Test for chip:%s', chip.name);
    const maxAddress = chip.storageSizeInBytes;
    // Writes to the chip are not limited to the maximum page size the EEPROM supports.
    // Page writes are managed internally by the EEPROM implementation.
    const blockSize = chip.pageSizeInBytes * 2;

    const maxLoops = 2;
    let loopCount = 0;

    while1: while (loopCount++ < maxLoops) {
        for (let address = 0x00; address < maxAddress; address += blockSize) {
            console.log('loopCount:%o address:0x%s', loopCount, address.toString(16).padStart(4, '0'));
            const toWrite = textIn.substring(0, blockSize);
            const bytesWritten = await chip.writeDataBlock(address, toWrite);
            const readResult = await chip.readDataBlock(address, bytesWritten);
            const textOut = readResult.toString('utf-8');
            if (textOut != toWrite) {
                console.log('Read/Write Text Mismatch!.  read:"%s"', textOut);
                break while1;
            }
        }
    }

    console.log('Clearing EEPROM with 0x00s');
    const bufferWithNulls = Buffer.alloc(blockSize);
    for (let address = 0x00; address < maxAddress; address += blockSize) {
        console.log('address:0x%s', address.toString(16).padStart(4, '0'));
        const bytesWritten = await chip.writeDataBlock(address, bufferWithNulls);
        const readResult = await chip.readDataBlock(address, bytesWritten);
        for (let i = 0; i < readResult.byteLength; i++) {
            if (readResult[i] !== 0x00) {
                console.log('Failed to clear EEPROM Memory');
                break;
            }
        }
    }
    await i2cBus.close();
};

// Run the example
example().catch(console.error);

Usage

Users may construct instances of chips by supplying 2 parameters;0 An 'i2cBus' and an 'i2address'. If the value specified for i2cAddress is not allowed based on the chip type, an error will be thrown. The error message provides detailed information as to the allowed I2C addresses for the chip type selected.

Note that all of the chips in this module support a common I2C address of 0x50.

Constructors

new AT24C01(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C01 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C02(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C02 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C04(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C04 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C08(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C08 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C16(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C16 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C32(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C32 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C64(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C64 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C128(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C128 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C256(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C256 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C512(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C512 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C512(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24CM01 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24CM01(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24CM01 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24CM02(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24CM02 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C32C(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C32C instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C64C(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C64C instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C128C(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C128C instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new AT24C256C(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new AT24C256C instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

new K24C256C(i2cBus, address)

constructor (i2cBus: I2CBus | PromisifiedBus, address: number);

Constructor for a new K24C256C instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the EEPROM IC.

Note that you need to construct the i2c-bus object and pass it in to the module.

API

The API uses Promises for all asynchronous read/write actions.

get name()

get name(): string

String representing the name of the chip.

get detail()

get detail(): string

String representing the detail of the chip in the format:

'Name[maxDevices]<storageSizeInBytes>'

Where maxDevices is the number of concurrent chips of this type that are supported on a single I2C Bus.

get maxDevices()

get maxDevices(): number

Number representing the maximum number of concurrent chips of this type that are supported on a single I2C Bus.

get i2cAddressesAllowed()

get i2cAddressesAllowed(): Array<number>

Array of numbers containing the valid I2C addresses that are allowed for this chip.

get storageSizeInBytes()

get storageSizeInBytes(): number

Number of bytes this chip is capable of supporting.

get pageSizeInBytes()

get pageSizeInBytes(): number

Number of bytes for each memory page on the chip. (Determines the maximum number of bytes that may be written in a single I2C operation).

get pageCount()

get pageCount(): number

Number of memory pages on the chip.

readDataBlock(memoryAddress, byteCount)

readDataBlock(memoryAddress: number, byteCount: number) : Promise<Buffer>;
  • memoryAddress - The address to begin reading from.
  • byteCount - The number of bytes to read.

Returns a Promise which will be resolved with a Buffer containing the data read from the chip.
If the sum of memoryAddress and byteCount is greater than the storageSizeInBytes of the chip, a rejected promise will be returned.

writeDataBlock(memoryAddress, data, stringEncoding)

writeDataBlock(memoryAddress: number, data: EEPROMDataTypes, stringEncoding?: string) : Promise<number>
  • memoryAddress - The address to begin reading from.
  • data - The data to write. Allowed types defined by EEPROMDataTypes are are string, Uint8Array, Uint8ClampedArray, and Buffer.
  • stringEncoding - Optional encoding to be used if the type of data is 'string'. Default value is 'utf-8' if not specified.

Returns a Promise which will be resolved with the number of bytes written to the chip.
If the sum of memoryAddress and the actual length in bytes of the encoded data block is greater than the storageSizeInBytes of the chip, a rejected promise will be returned.

License

Licensed under GPL Version 2

Copyright (c) 2024 Lyndel McGee lynniemagoo@yahoo.com