2020-06-30 12:33 AM
Hello Guys,
I have a AT24CM02 EEPROM which uses I2C to communicate with a STM32H7 MCU.
I need to store some float values into the EEPROM, but I do not know how.
I appreciate any suggestions.
Thank you and best regards,
Vouria
Solved! Go to Solution.
2020-06-30 03:13 AM
If you build a EEPROM write block which deals with EEPROM pages internally (not the case in F/M/RAM), it would be more generic
Then if you just save/retrieve data in EEPROM, use sizeof(my_variable) to get its size in bytes.
This way you can backup float, double, struct, anything without worry.
Please also note that EEPROM needs time after write a page or by polling the ACK bit of the EEPROM at the right time.
With a little extra effort in the early stage, the foundation will be more rugged.
2020-06-30 12:52 AM
Hi,
Please see my functions to write and read data type float to memory FRAM FM24CL16
#define FRAM_HW_ADDRESS 0xA0
uint16_t addr;
float Memory_FL1;
void Read_Float (void)
{
addr = 0;
I2C_RP_F_FRAM(&addr, &Memory_FL1);
}
void Write_Float (void)
{
addr = 0;
I2C_WRP_F_FRAM(&addr, &Memory_FL1);
}
void I2C_WRP_F_FRAM (uint16_t *adress, float *pData_to_WR)
{
uint16_t adr_temp = *adress;
I2C_MEM_WB_FM24CL16(&adr_temp, *((uint8_t*)pData_to_WR));adr_temp++;
I2C_MEM_WB_FM24CL16(&adr_temp, *((uint8_t*)pData_to_WR+1));adr_temp++;
I2C_MEM_WB_FM24CL16(&adr_temp, *((uint8_t*)pData_to_WR+2));adr_temp++;
I2C_MEM_WB_FM24CL16(&adr_temp, *((uint8_t*)pData_to_WR+3));
}
void I2C_RP_F_FRAM (uint16_t *adress, float *pData_to_R)
{
uint16_t adr_temp = *adress;
*((uint8_t*)pData_to_R) = I2C_MEM_RB_FM24CL16(&adr_temp);adr_temp++;
*((uint8_t*)pData_to_R+1) = I2C_MEM_RB_FM24CL16(&adr_temp);adr_temp++;
*((uint8_t*)pData_to_R+2) = I2C_MEM_RB_FM24CL16(&adr_temp);adr_temp++;
*((uint8_t*)pData_to_R+3) = I2C_MEM_RB_FM24CL16(&adr_temp);
}
void I2C_MEM_WB_FM24CL16(uint16_t *WriteAddr, uint8_t WriteData)
{
uint16_t Memadr;
uint16_t Devadr;
Memadr = *WriteAddr;
Devadr = FRAM_HW_ADDRESS;
COPY_BIT(Devadr, 1, Memadr, 8);
COPY_BIT(Devadr, 2, Memadr, 9);
COPY_BIT(Devadr, 3, Memadr, 10);
Memadr = Memadr & 0x00FF;
HAL_I2C_Mem_Write(&hi2c1, Devadr, Memadr, I2C_MEMADD_SIZE_8BIT, &WriteData, 1, 5);
}
uint8_t I2C_MEM_RB_FM24CL16(uint16_t *ReadAddr)
{
uint16_t Memadr;
uint16_t Devadr;
uint8_t Readbyte;
Memadr = *ReadAddr;
Devadr = FRAM_HW_ADDRESS;
COPY_BIT(Devadr, 1, Memadr, 8);
COPY_BIT(Devadr, 2, Memadr, 9);
COPY_BIT(Devadr, 3, Memadr, 10);
Memadr = Memadr & 0x00FF;
HAL_I2C_Mem_Read(&hi2c1, Devadr, Memadr, I2C_MEMADD_SIZE_8BIT, &Readbyte, 1, 5);
/* Read a byte from the EEPROM */
return (Readbyte);
}
2020-06-30 01:21 AM
Thanks a lot for sharing, This is very useful, can you please clarify what "COPY_BIT" is doing?
2020-06-30 01:42 AM
Hi,
#define COPY_BIT(t,tb,s,sb) t = s & (1<<(sb)) ? t | (1<<(tb)) : t & ~(1<<(tb))
2020-06-30 03:13 AM
If you build a EEPROM write block which deals with EEPROM pages internally (not the case in F/M/RAM), it would be more generic
Then if you just save/retrieve data in EEPROM, use sizeof(my_variable) to get its size in bytes.
This way you can backup float, double, struct, anything without worry.
Please also note that EEPROM needs time after write a page or by polling the ACK bit of the EEPROM at the right time.
With a little extra effort in the early stage, the foundation will be more rugged.
2020-06-30 04:22 AM
Thank you very much, I think this is the best way to do it.
2020-07-03 05:03 AM
do you have any example code?