2020-06-16 01:15 AM
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??
2020-06-19 01:31 AM
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