cancel
Showing results for 
Search instead for 
Did you mean: 

Hello Everyone. I am working on STM32F103VG controller and trying to code for I2C based EEPROM IC (AT24CM02). I am facing some issues with writing and reading EEPROM.

sne_123
Associate III

I want to save 2 strings of 127 bytes of data in 1 page. 1 page is 256 bytes each. Whern I try to read and write 127 bytes of 1st string, it works successfully. But when I try to append 127bytes of 2nd string,it does not read and write data properly. I have attached the code below. Can anyone please help me with the mistake?

Please help me why i am unable to append the 127bytes of 2nd string??

to write data into EEPROM, i am using:-

DI2Buff is 127 bytes of string.

EpCntDI2 is number of page.

Code:-

WritrToEEprom(DI2Buff, sizeof(DI2Buff),127 * EpCntDI2, 0xA0);

char WritrToEEprom(char* WriteData,int Lenght,unsigned long EpLocation,int EpAddr)

{

unsigned short int EEPROMadress ;

HAL_Delay(50); 

EEPROMadress=EpLocation>>16;

EEPROMadress=0xA0|(EEPROMadress<<1);

StatusCode = HAL_I2C_Mem_Write(&hi2c1, EEPROMadress ,EpLocation, I2C_MEMADD_SIZE_16BIT,(unsigned char *) WriteData, Lenght,2000);

HAL_Delay(350); //350

if(StatusCode == HAL_OK)

{

printf("write success at location = %d\r\n",EpLocation);

}

else

{

printf("write fail at location reinitialize= %d\r\n",EpLocation);

HAL_I2C_MspDeInit(&hi2c1);

HAL_Delay(250);

MX_I2C1_Init();

}

}

For reading I am using:-

ReadBuff is buffer to read data.

ReadEEprom((char *)ReadBuff,sizeof(ReadBuff),127 * EpcntDI2,0xA1);

char ReadEEprom(char* ReadData1,int Lenght,unsigned long EpLocation,int EpAddr)

{

 unsigned char EEPROMadress1 ;

HAL_Delay(50); 

EEPROMadress1=EpLocation>>16;

EEPROMadress1=(0xA1)|(EEPROMadress1<<1);

StatusCode = HAL_I2C_Mem_Read(&hi2c1, EEPROMadress1, EpLocation, I2C_MEMADD_SIZE_16BIT,(unsigned char *) ReadData1, Lenght, 2000);

HAL_Delay(500);

if(StatusCode == HAL_OK) 

{

printf("Read success at location = %d\r\n",EpLocation);

}

else{

printf("Read fail at location reinitialize = %d\r\n",EpLocation);

HAL_I2C_MspDeInit(&hi2c1);

HAL_Delay(250);

MX_I2C1_Init();

}

memset(Buff11,sizeof(Buff11),'F');

HAL_Delay(250);

for(int z=0;z<256;z++)

{

Buff11[z] = 0x00;

}

WritrToEEprom(Buff11,Lenght,EpLocation,0xA0);

}

Please help me why i am unable to append the 127bytes of 2nd string??

1 REPLY 1
Peter BENSCH
ST Employee

Please check the requirements of HAL_I2C_Mem_Write and compare it with your settings of EpLocation and EpAddr. Some questions come up while looking at your code, e.g.:

Why do you define EpLocation as unsigned long?

Why do you divide it by 65536 (>>16)?

Why do you define EpAddr within WritrToEEprom but not use it?

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.