esp32wroom 32D with memory ic AT24c04N

kaushal vakani
Posts: 1
Joined: Wed Jan 08, 2020 10:14 am

esp32wroom 32D with memory ic AT24c04N

Postby kaushal vakani » Wed Jan 08, 2020 11:01 am

hello i am working with esp32wroom 32d ,i need to interface AT24c04N memory ic for storing some data on that chip using i2c protocol but i can not read proper data.
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;
}
(mod-edit: added code tags)

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: esp32wroom 32D with memory ic AT24c04N

Postby ESP_Sprite » Thu Jan 09, 2020 3:21 am

I mean this in the nicest way possible, but I'll still be blunt: your code looks like crap. That hurts you for two reasons: first of all, no one is going to dive into your code to figure out what is wrong with it: why would they take that effort when you didn't even take the effort to clean your code up before posting it here? The second reason is that your code looking like crap hurts your own chance of seeing what the issue is: how can you see what conditions apply to a line of code if you need to trace back the opening angles of your if statements manually instead of being able to trust the indentation? Take a moment: get the indentation right, name your variables something that makes more sense than 'loop' and 'loop1' if they're not used in loops, add comments that indicate what you want to achieve with your block of code, and I'm pretty sure you'll see what's wrong with it. If you still do not see any errors, post your cleaned up code here, and I'm sure you'll have enough people willing to take a look at it.

Who is online

Users browsing this forum: AndrewLevel100, Google [Bot] and 104 guests