here i am used this code for the read the data in particular address,
but i have one problem when my received address is 0x00 to 0x0F location i will get the data properly ,but when my address is greater then 0x0F then i can not get the properly data ,
so i need to code for write and read operation using i2c protocol.
Code: Select all
uint16_t device address =0xA0,starting_address=0x10;
uint8_t Read_data(uint8_t deviceaddress, uint16_t eeaddress, uint8_t* data, size_t size)
{
uint8_t loop1,loop;
uint16_t templen=size,dptr=0,Dadd=eeaddress;
if(size+eeaddress > 512)
return 1;
if(size >16)
{
if(eeaddress % 16)
{
loop1=16-(eeaddress%16);
if(Dadd>255)
{
deviceaddress |= 0x02;
}
printf("print dptr+data ==>%p \n", (data+dptr));
if(eeprom_read(deviceaddress,Dadd,data+dptr,loop1) != ESP_OK)
{
printf("error is occur \n");
return 1;
}
Dadd += loop1;
dptr +=loop1;
templen -= loop1;
}
loop1=(templen/16);
if(templen%16)
{
loop1++;
vTaskDelay(10/portTICK_PERIOD_MS);
}
for(loop=0;loop<loop1;loop++)
{
if(Dadd >255)
deviceaddress |= 0x02;
if(eeprom_read(deviceaddress,Dadd,data+dptr,(templen>16)?16:templen) != ESP_OK)
{
return 1;
}
Dadd += (templen>16)?16:templen;
dptr += (templen>16)?16:templen;
templen -= (templen>16)?16:templen;
vTaskDelay(10/portTICK_PERIOD_MS);
}
return 0;
}
return 0;
}
esp_err_t eeprom_read(uint8_t deviceaddress, uint16_t eeaddress, uint8_t* data, size_t size) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (deviceaddress)|EEPROM_WRITE_ADDR, 1);
i2c_master_write_byte(cmd, eeaddress, 1);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (deviceaddress)|EEPROM_READ_ADDR, 1);
if (size > 1)
{
i2c_master_read(cmd, data, size-1, 0);
}
i2c_master_read_byte(cmd, data+size-1, 1);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_1, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return ret;
}