Page 1 of 1

I2C EEPROM - Random Read

Posted: Mon Oct 13, 2014 4:59 pm
by Tabulous
I have a 24AA02E48 E2Prom on my board, what i want to do is read from a specific address 6 bytes.

I.e. read from 0xFA and the next 6 bytes.

But i'm struggling to see how this can be done, this is what i've got but it does not work.

Code: Select all

static msg_t readMacAddress(void)
{
msg_t status = RDY_OK;
systime_t tmo = MS2ST(100);


    i2cAcquireBus(&I2CD3);

    i2c_state.txbuf[0] = 0xFA;
    i2c_state.address = E2PROM_ADDR;

    status = i2cMasterTransmitTimeout(&I2CD3,
                                      i2c_state.address,
                                      i2c_state.txbuf, 1,
                                      i2c_state.rxbuf, 0,
                                      tmo);


    status = i2cMasterReceiveTimeout(&I2CD3,
                                     i2c_state.address,
                                     i2c_state.rxbuf, 6,
                                     tmo);

    i2cReleaseBus(&I2CD3);

    return(status);
}

Re: I2C EEPROM - Random Read

Posted: Mon Oct 13, 2014 5:19 pm
by Giovanni
I don't know the exact protocol, are you sure you need a separate receive after the transmit?

Giovanni

Re: I2C EEPROM - Random Read

Posted: Mon Oct 13, 2014 5:25 pm
by Tabulous
I'm not sure really

The Datasheet states the following

START
ADDRESS THE CHIP(WRITE)
ACK
ADDRESS OF MEM LOCATION
ACK
START
ADDRESS THE CHIP(READ)
ACK
DATA FROM THE CHIP
NO ACK
STOP

Re: I2C EEPROM - Random Read

Posted: Mon Oct 13, 2014 5:52 pm
by Giovanni
Then apparently you don't need the receive because there is no STOP and then again START.

Giovanni

Re: I2C EEPROM - Random Read

Posted: Mon Oct 13, 2014 6:12 pm
by steved
You can usually read back as a single Chibi operation; send one, two or three address bytes (dependent on device) to set the memory start address, then read the required number of bytes.

Example using a 2-byte address:

Code: Select all


// Function retrieves a block of memory from NV Memory
msg_t retrieveConfigMem(unsigned int memStart, uint8_t *data, unsigned int maxRead)
{
    msg_t   result;

    if (maxRead == 0) return(0);            // Implied success if nothing to do
    if (maxRead > sizeof(tempBuf.storedData)) return(-2);
    memset(&tempBuf,0, sizeof(tempBuf));

    tempBuf.memAddress[0] = (memStart >> 8) & 0xff;       // Address MSB
    tempBuf.memAddress[1] = memStart & 0xff;              // Address LSB

    i2cAcquireBus(&NVR_I2C_PORT);
    result = i2cMasterTransmitTimeout(&NVR_I2C_PORT, NVR_I2C_ADDRESS/2, (uint8_t *)&tempBuf, 2, (uint8_t *)&tempBuf.crc, maxRead + NVR_BUFFER_OVERHEAD - 2, NVR_I2C_TIMEOUT);
    i2cReleaseBus(&NVR_I2C_PORT);
    if (result != 0)
    {
        return(result);
    }

    if (tempBuf.dataSize > (int)sizeof(tempBuf.storedData))
    {
        tempBuf.dataSize = sizeof(tempBuf.storedData);  // In case of garbage
    }
    memcpy(data, tempBuf.storedData, tempBuf.dataSize);
    return(0);
}

This example goes via an intermediate buffer, because I actually add a header and CRC onto the data, but you can certainly read direct into your destination RAM if you want.

Re: I2C EEPROM - Random Read

Posted: Tue Oct 14, 2014 6:43 am
by Tabulous
I've changed my routine to this

Code: Select all

    i2c_state.txbuf[0] = 0xFA;
    i2c_state.address = E2PROM_ADDR;

    i2cAcquireBus(&I2CD3);
    status = i2cMasterTransmitTimeout(&I2CD3,
                                      i2c_state.address,
                                      i2c_state.txbuf, 1,
                                      i2c_state.rxbuf, 1,
                                      tmo);

    i2cReleaseBus(&I2CD3);


The issue i have is the address been written out on the scope is not what it should be....... E2PROM_ADDR is 0xA0

But what is on the scope is 0XF0 see scope picture attached

Re: I2C EEPROM - Random Read

Posted: Tue Oct 14, 2014 7:28 am
by Tabulous
Right guys i think there is an issue here in the driver.

My last post showed a scope trace, showing what comes out of the I2C bus.

For me to get this to work i have to have E2PROM_ADDR set to 0x50, if i want 0xA0 to come out of the bus.

Why should i need to offset the address by one shift to the right i.e 0xA0 >> 1

Re: I2C EEPROM - Random Read

Posted: Tue Oct 14, 2014 7:31 am
by Giovanni
That parameter is just the address field without the RW bit.

Giovanni

Re: I2C EEPROM - Random Read

Posted: Tue Oct 14, 2014 7:51 am
by Tabulous
Giovanni wrote:That parameter is just the address field without the RW bit.

Giovanni


yeah its just clicked :oops: