2021-01-12 10:42 PM
hey there
I am using an EEPROM which is AT24C08. here is my code
//write
ee24_write((16*(3)),(unsigned char *)("+989163306941"),13,500);
ee24_write((16*(4)),(unsigned char *)("aaa"),strlen("aaa"),100);
ee24_write((16*(7)),(unsigned char *)("+989163306942"),13,500);
ee24_write((16*(8)),(unsigned char *)("bbb"),strlen("bbb"),100);
ee24_write((16*(11)),(unsigned char *)("+989163306943"),13,500);
ee24_write((16*(12)),(unsigned char *)("ccc"),strlen("ccc"),100);
ee24_write((16*(15)),(unsigned char *)("+989163306944"),13,500);
ee24_write((16*(16)),(unsigned char *)("ddd"),strlen("ddd"),100);
ee24_write((16*(19)),(unsigned char *)("+989163306945"),13,500);
ee24_write((16*(20)),(unsigned char *)("eee"),strlen("eee"),100);
//read
ee24_read((16*(3)),(unsigned char *)SYS.User[0].PhoneNumber,13,500);
ee24_read((16*(4)),(unsigned char *)SYS.User[0].Name,3,500);
ee24_read((16*(7)),(unsigned char *)SYS.User[1].PhoneNumber,13,500);
ee24_read((16*(8)),(unsigned char *)SYS.User[1].Name,3,500);
ee24_read((16*(11)),(unsigned char *)SYS.User[2].PhoneNumber,13,500);
ee24_read((16*(12)),(unsigned char *)SYS.User[2].Name,3,500);
my problem is that the last write("+989163306945"-"eee") is being read in the first arrays(SYS.User[0].PhoneNumber-SYS.User[0].Name)
where they should be ("+989163306941"-"aaa") instead.
why is that?
here is the codes for ee24_write() and ee24_read()
#define _EEPROM_SIZE_KBIT 8
#define _EEPROM_I2C hi2c2
#define _EEPROM_USE_FREERTOS 0
#define _EEPROM_ADDRESS 0xA0
#define _EEPROM_USE_WP_PIN 1
#define _EEPROM_PSIZE 16
#if (_EEPROM_USE_WP_PIN==1)
#define _EEPROM_WP_GPIO GPIOB
#define _EEPROM_WP_PIN GPIO_PIN_2
bool ee24_write(uint16_t address, uint8_t *data, size_t len, uint32_t timeout)
{
if (ee24_lock == 1)
return false;
ee24_lock = 1;
uint16_t w;
uint32_t startTime = HAL_GetTick();
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN,GPIO_PIN_RESET);
#endif
while (1)
{
w = _EEPROM_PSIZE - (address % _EEPROM_PSIZE);
if (w > len)
w = len;
#if ((_EEPROM_SIZE_KBIT==1) || (_EEPROM_SIZE_KBIT==2))
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==4)
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0100 >> 7)), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==8)
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0300 >> 7)), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==16)
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0700 >> 7)), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, w, 100) == HAL_OK)
#else
if (HAL_I2C_Mem_Write(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_16BIT, data, w, 100) == HAL_OK)
#endif
{
HAL_Delay(10);
len -= w;
data += w;
address += w;
if (len == 0)
{
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN, GPIO_PIN_SET);
#endif
ee24_lock = 0;
return true;
}
if (HAL_GetTick() - startTime >= timeout)
{
ee24_lock = 0;
return false;
}
}
else
{
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN, GPIO_PIN_SET);
#endif
ee24_lock = 0;
return false;
}
}
}
//################################################################################################################
bool ee24_read(uint16_t address, uint8_t *data, size_t len, uint32_t timeout)
{
if (ee24_lock == 1)
return false;
ee24_lock = 1;
#if (_EEPROM_USE_WP_PIN==1)
HAL_GPIO_WritePin(_EEPROM_WP_GPIO, _EEPROM_WP_PIN, GPIO_PIN_SET);
#endif
#if ((_EEPROM_SIZE_KBIT==1) || (_EEPROM_SIZE_KBIT==2))
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT == 4)
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0100 >> 7)), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT == 8)
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0300 >> 7)), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#elif (_EEPROM_SIZE_KBIT==16)
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS | ((address & 0x0700>> 7)), (address & 0xff), I2C_MEMADD_SIZE_8BIT, data, len, 100) == HAL_OK)
#else
if (HAL_I2C_Mem_Read(&_EEPROM_I2C, _EEPROM_ADDRESS, address, I2C_MEMADD_SIZE_16BIT, data, len, timeout) == HAL_OK)
#endif
{
ee24_lock = 0;
return true;
}
else
{
ee24_lock = 0;
return false;
}
}
why is that?
2021-01-12 11:14 PM
Maybe try change
((address & 0x0300 >> 7))
to
((address & 0x0300) >> 7)
2021-01-13 01:08 AM
what's the difference? seems just like my own code..
actually I tried a 64bit EEPROM and it worked!!!!
#define _EEPROM_SIZE_KBIT 64
#define _EEPROM_PSIZE 32
are the only changes that I made.
2021-01-13 04:29 AM
Sorry your new defines changes use code where you dont have & >> operators, and when priority operator >> is high as & , then your match result
(address & 0x06) 8)