Eeprom addressing

Hi, I need help with understanding this I2C eeprom

The code ive got sort of works, i can write a byte (or more) to “some” address and read “something” back in. But the addressing part does not work, it always returns the last value written.

static void WriteEeprom(Int32 address, Byte[] data, Int32 index, Int32 count)
        {
            byte[] buffer = new Byte[data.Length + 2];
            buffer[0] = (Byte)(address >> 8);
            buffer[1] = (Byte)(address & 0xFF);
            Array.Copy(data, index, buffer, 2, count);
            eeprom.Write(buffer);
            Thread.Sleep(5);
        }

static void ReadEeprom(Int32 address, Byte[] data, Int32 index, Int32 count)
        {
            eeprom.Write(new[] { (Byte)(address >> 8), (Byte)(address & 0xFF) });
            Thread.Sleep(5);
            byte[] buffer = new Byte[count];
            eeprom.Read(buffer);
            Array.Copy(buffer, 0, data, index, count);
        }

This is the 2KB eeprom, does that mean that it has 2000 addresses to store 1 byte per address?
Also how do i point to and read the specific address?

This should write number 78 to address 200

WriteEeprom(200, new byte[1] { 78 }, 0, 1);

Read

byte[] data = new byte[1];
ReadEeprom(200, data, 0, 1);           
Debug.WriteLine(data[0].ToString);

I won’t be a dick™ here, but there’s a thread where I did an eeprom POC for someone. Writing Data to EEPROM Register Address That might give you some hints - even though it’s netmf.

1 Like

For a start, you’re addressing is wrong for this device. The address is a single byte. The device is paged with 16 bytes per page so the address byte will be 0 to 127. This means you have to read and write 1 to 16 bytes at the same time. If you only want to change a single byte, you need to read the number of bytes that it fits into for that page, write the new value to the buffer and then write the buffer back out to the device.

I just don’t get it.
on page 15 of the datasheet there’s a byte write, and page 16 there’s a page write. So I think you can read or write 1 byte at a time, as well as read a page of 16 bytes.

as I understand it, to construct a write buffer I need at least 3 bytes, 1 for the device select + read/write flag, 1 for the memory address and 1 for my data (a number 0-255 stored as byte)

so I removed buffer[1] from the code, but doesn’t buffer[0] when right shifted 8 places always return 0?
where’s the eeprom data address then?

Heres what I got now, ive simplified this, and it works the same as before only the last values is stored, addressing does not work (I think I read from and write to address 0 all the time)

 static void WriteEeprom(byte byteAddress, byte dataIn)
        {
            byte[] buffer = new Byte[3];

            //write byte
            buffer[0] = 0;

            buffer[1] = byteAddress;
            buffer[2] = dataIn;

            eeprom.Write(buffer);
            Thread.Sleep(5);
        }

static byte ReadEeprom(byte address)
    {
        byte[] buffer = new Byte[2];

        //read byte
        buffer[0] = 0;
        buffer[1] = address;

        eeprom.Write(buffer);
        Thread.Sleep(5);

        byte[] outBuffer = new byte[1];
        eeprom.Read(outBuffer);

        return outBuffer[0];
    }

Ive got 3 buttons

First writes this:
WriteEeprom(47, 86);

Second writes this:
WriteEeprom(30, 55);

Third reads this:

            byte data = ReadEeprom(30);
            Thread.Sleep(1000);
            byte data1 = ReadEeprom(47);
            Thread.Sleep(1000);

            Debug.WriteLine(data.ToString() + "-" + data1.ToString());

I always get the last number either 55 or 86 depending on the order I push the buttons, even after restart of device, so the eeprom works just not the addressing.

In your write, you are always setting address 0 and then writing the data as your address.

The first byte is the page address followed by 1 to 16 bytes to fill that page.

Same for your write. The EEPROM address is handled elsewhere and setup when you create eeprom.

Try this but beware that I have not tested this code.

static void WriteEeprom(byte byteAddress, byte dataIn)
        {
            byte[] buffer = new Byte[2];

            buffer[0] = byteAddress;   // Page address we want to write to
            buffer[1] = dataIn;

            eeprom.Write(buffer);
            Thread.Sleep(5);
        }

static byte ReadEeprom(byte address)
    {
        byte[] buffer = new Byte[1];

        buffer[0] = address;

        eeprom.Write(buffer);   // Write the address of the page we want
        Thread.Sleep(5);

        byte[] outBuffer = new byte[1];
        eeprom.Read(outBuffer);

        return outBuffer[0];
    }

Now use your calls to write and read. Note that WriteEeprom(47,86) actually write to memory address 752

Works! Thanks Dave

So the I2C takes care of device addressing.
And based on this I can only point to 0-127 page addresses.

Now I just have to write something to find a specific byte.
Ill post when im finished, maybe someone will find it useful.

2 Likes

The I2C driver takes care of the address. You set this when you create it.

var settings = new I2cConnectionSettings(0x1C);     //The slave's address.
        settings.BusSpeed = I2cBusSpeed.FastMode;

        var controller = I2cController.FromName(FEZ.I2cBus.I2c1);
        var eeprom= controller.GetDevice(settings);