7 years, 1 month ago.

i'mm using STM32f103, i can't able to read data from an i2c eeprom,can anyone help me out?

#include "mbed.h"
#define eep_add ((uint8_t) 0x50<<1)
//#define eep_add (0xA0)
I2C i2c(PB_9, PB_8);
Serial pc(PA_2, PA_3);
char *data;
char buf[40]="hello ,welcome to asdfghjkloifnet";
int main() {
    i2c.frequency(100000);
    while(1){
        char cmd[sizeof(buf)+2];
        cmd[0]=0x00>>8;
        cmd[1]=0x00&0xFF;
        for (int i=0;i<sizeof(buf);i++){
            cmd[2+i]=buf[i];
        }
        i2c.write(eep_add,cmd,2+sizeof(buf));//write
        wait(0.1);
        cmd[0]=0x00>>8;
        cmd[1]=0x00&0xFF;
        i2c.write(eep_add,cmd,2,true);
        i2c.read(eep_add,data,sizeof(buf));//read
        pc.printf("%s\n",data);
        wait(0.01);
    }
}

2 Answers

7 years, 1 month ago.

There are some problems with the code:

You declared

char *data;

and later on you read some data from the eeprom to store at that data pointer. However, there is no free memory allocated to the pointer and that may lead to undefined results. Either use malloc or replace the pointer by a static array same as for cmd.

Also note that when using printf for a string the string data must have a '0' as last entry.

Accepted Answer

Thanks..! i will check it out and let you know wim.

posted by Mounish periasamy 20 Mar 2017

Thanks wim it actually worked.!

posted by Mounish periasamy 21 Mar 2017
7 years, 1 month ago.

Hmmm...did you add the pullup resistors to CLK and DATA?

yea i did, i used 4.7K.

posted by Mounish periasamy 20 Mar 2017