Nicolas Felipe

i2c Eeprom Atmel 24c02bn Writing Buffer, Weird Data back

Discussion created by Nicolas Felipe on Mar 20, 2017
Latest reply on Mar 20, 2017 by Nicolas Felipe

Hi, here i post the code i have been using, i can write 5 numbers at the same time ok, but when is more than that,

the info read back becomes crazy and out of ordered, im using ATMEL 24c02bn .

 

the code :

 

#define EEPROM_ADDRESS 0x50<<1
#define EEPROM_MAXPKT 32 //(page size)
#define EEPROM_MEMBASEADDRESS (uint16_t)0x100 //must be multiple of (EEPROM_MAXPKT)
#define EEPROM_WRITE 5 //time to wait in ms
#define EEPROM_TIMEOUT 2*EEPROM_WRITE //timeout while writing

#define BufferSize 30
uint8_t WriteBuffer[BufferSize],ReadBuffer[BufferSize],Test;

 

// FUNCTIONS TO WRITE AND READ BACK

HAL_StatusTypeDef SysHW_readEEPROM(uint8_t* MemTarget, uint16_t Size)
{
uint16_t Counter = 0;
HAL_StatusTypeDef Result = HAL_OK;
while (Counter < Size && Result == HAL_OK)
{
uint16_t Diff = Size - Counter;

if (Diff >= EEPROM_MAXPKT)
{
//Multi-Byte
Result = HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDRESS, EEPROM_MEMBASEADDRESS + Counter, I2C_MEMADD_SIZE_16BIT, &MemTarget[Counter], EEPROM_MAXPKT, EEPROM_TIMEOUT);
Counter += EEPROM_MAXPKT;
}
else
{
//and the remaining ones...low packet size
Result = HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDRESS, EEPROM_MEMBASEADDRESS + Counter, I2C_MEMADD_SIZE_16BIT, &MemTarget[Counter], Diff, EEPROM_TIMEOUT);
Counter += Diff;
}
HAL_Delay(EEPROM_WRITE);
}
return Result;
}

HAL_StatusTypeDef SysHW_writeEEPROM(uint8_t* MemSource, uint16_t Size)
{
uint16_t Counter = 0;
HAL_StatusTypeDef Result = HAL_OK;
while (Counter < Size && Result == HAL_OK)
{
uint16_t Diff = Size - Counter;

if (Diff >= EEPROM_MAXPKT)
{
//Multi-Byte
Result = HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDRESS, EEPROM_MEMBASEADDRESS + Counter, I2C_MEMADD_SIZE_16BIT, &MemSource[Counter], EEPROM_MAXPKT, EEPROM_TIMEOUT);
Counter += EEPROM_MAXPKT;
}
else
{
//and the remaining ones...low packet size
Result = HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDRESS, EEPROM_MEMBASEADDRESS + Counter, I2C_MEMADD_SIZE_16BIT, &MemSource[Counter], Diff, EEPROM_TIMEOUT);
Counter += Diff;
}
HAL_Delay(EEPROM_WRITE);
}
return Result;
}

 

 

//EXECUTION OF THE CODE

 

for(uint8_t i=0; i<30; i++)
{
     WriteBuffer[i]=i;
}

 

HAL_Delay(100);
SysHW_writeEEPROM(WriteBuffer, 30);
HAL_Delay(100);

SysHW_readEEPROM(ReadBuffer, 30);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); //BLUE LED ON

 

 

The Write Buffer is perfect Filled from the loop "0 to 30" perfectly,

when i try to read back the back WriteBuffer  starts at position 1 with "24,25,26,27,28,29" and then "255" 'y' from 6 to 13 

position, then it reads 8,9,10,11,12,13,14,7

 

do you know why it always start with the wrong numbers?

i ve trying to get into HAL but almost everything i find is STDPeriph based,

 

pls help me if you know what im doing wrong. thanks.

Attachments

Outcomes