2021-09-30 08:36 AM
Hello,
I could't found example with Low Level driver to read-write from data EEPROM (FLASH).
I wrote some simple functions to read-write from data EEPROM.
If anyone have even simple solution, i would sincerely appreciate it.
Thanks!
Regards,
Swapnil
/**************************************************************** Data Read-Write from data EEPROM (FLASH) start *******************************************************************************/
#define EEPROM_BASE_ADDR 0x08080000 // Starting Address
#define EEPROM_BYTE_SIZE 0x01FF // End address (512 Bytes)
uint8_t data_to_write[] = "LL Hello to EEPROM LL"; // write data buffer
uint8_t data_to_read[100]; // read data buffer
/* Write to EEPROM: */
void EEPROM_WRITE(uint16_t BiasAddress, uint8_t *Data, uint16_t len)
{
uint16_t i; // counter i
UnlockEeprom(); // Unlock the EEPROM
FLASH->PECR = FLASH->PECR & ~(FLASH_PECR_ERASE | FLASH_PECR_DATA); // Reset the ERASE and DATA bits in the FLASH_PECR register to disable any residual erase
for(i=0;i<len;i++)
{
FLASHEx_DATAEEPROM_Program(EEPROM_BASE_ADDR+BiasAddress+i, *Data); // call fun to write data to EEPROM
Data++; // Data increment
}
LockEeprom(); // Lock the EEPROM
}
FLASHEx_DATAEEPROM_Program(uint32_t Address, uint32_t Data)
{
*(__IO uint8_t *)Address = (uint8_t) Data; // Program byte (8-bit) at a specified address.
__WFI(); //Enter in wait for interrupt. The EOP check is done in the Flash ISR
}
/* Lock the EEPROM: */
void LockEeprom(void)
{
while ((FLASH->SR & FLASH_SR_BSY) != 0) // Wait for FLASH to be free
{
// insert timeout test
}
FLASH->PECR = FLASH->PECR & ~(FLASH_PECR_ERRIE | FLASH_PECR_EOPIE); // disable flash interrupts
FLASH->PECR = FLASH->PECR | FLASH_PECR_PELOCK; // Lock memory with PELOCK */
}
/* Unlock the EEPROM: */
void UnlockEeprom(void)
{
while ((FLASH->SR & FLASH_SR_BSY) != 0) // Wait for FLASH to be free
{
// insert timeout test
}
if ((FLASH->PECR & FLASH_PECR_PELOCK) != 0) // If PELOCK is locked
{
FLASH->PEKEYR = 0x89ABCDEF; // Perform unlock sequence this sequence is used to unlock the data EEPROM and the FLASH_PECR register
FLASH->PEKEYR = 0x02030405; // Perform unlock sequence this sequence is used to unlock the data EEPROM and the FLASH_PECR register
}
FLASH->PECR = FLASH->PECR | (FLASH_PECR_ERRIE | FLASH_PECR_EOPIE); // enable flash interrupts
}
/* Read from the EEPROM: */
void EEPROM_READ(uint16_t BiasAddress,uint8_t *Buffer,uint16_t Len)
{
uint8_t *wAddr; // pointer to written address data
wAddr=(uint8_t *)(EEPROM_BASE_ADDR+BiasAddress); // store data in var wAddr
while(Len--)
{
*Buffer++=*wAddr++; // store data from var wAddr to read data buffer
}
}
int main(void)
{
EEPROM_WRITE(0x08080000,data_to_write, strlen((char *)data_to_write)); // Write to EEPROM
EEPROM_READ(0x08080000,data_to_read,50); // Read from EEPROM
while (1)
{
}
}
/**************************************************************** Data Read-Write from data EEPROM (FLASH) end *******************************************************************************/