I2C EEPROM - Random Read

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

I2C EEPROM - Random Read

Post 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);
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C EEPROM - Random Read

Post by Giovanni »

I don't know the exact protocol, are you sure you need a separate receive after the transmit?

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: I2C EEPROM - Random Read

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C EEPROM - Random Read

Post by Giovanni »

Then apparently you don't need the receive because there is no STOP and then again START.

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: I2C EEPROM - Random Read

Post 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.
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: I2C EEPROM - Random Read

Post 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
Attachments
Scope Trace
Scope Trace
DS0004.jpg (35.83 KiB) Viewed 4800 times
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: I2C EEPROM - Random Read

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C EEPROM - Random Read

Post by Giovanni »

That parameter is just the address field without the RW bit.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: I2C EEPROM - Random Read

Post by Tabulous »

Giovanni wrote:That parameter is just the address field without the RW bit.

Giovanni


yeah its just clicked :oops:
Post Reply