Question
STM32G030K8 eeprom emulation
Hi everybody,
first an apologies for my poor English.
I'm using STM32G030K8 in my project and I want to use eeprom emulation to save some parameters. I used the following code, however It didn't work. I spend a lot of time but I can find the problem.
void flashUnlock(void)
{
/* Unlocking the program memory access */
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
void flashLock(void)
{
FLASH->CR |= FLASH_CR_LOCK;
}
void witeToFlash(uint64_t address, uint64_t data)
{
flashUnlock();
while((FLASH->SR & FLASH_SR_BSY1));
FLASH->SR = FLASH_SR_EOP | FLASH_SR_WRPERR | FLASH_SR_PGSERR;
//erase page
FLASH->CR |= FLASH_CR_PER;
FLASH->CR &= ~FLASH_CR_PNB;
FLASH->CR |= (31 << FLASH_CR_PNB_Pos); //page number 31 erased
FLASH->CR |= FLASH_CR_STRT;
/* Wait for last operation to be completed */
while((FLASH->SR & FLASH_SR_BSY1));
/* Disable the PER Bit */
FLASH->CR &= ~FLASH_CR_PER;
/* Wait for last operation to be completed */
while((FLASH->SR & FLASH_SR_BSY1));
//Enable Fast writing to Flash
FLASH->CR |= FLASH_CR_PG;
//write data
*(__IO uint64_t*)Address = (uint64_t)Data;
/* Wait for last operation to be completed */
while((FLASH->SR & FLASH_SR_BSY1));
/* Disable the PG Bit */
FLASH->CR &= ~FLASH_CR_PG;
flashLock();
}
uint64_t readFromFlash(uint64_t address)
{
return *(uint64_t*)address;
}
void main(void)
{
uint64_t a;
witeToFlash(0x800F800, 0x05); //write 0x05 to page 31. 0x800F800 is the start address of page 31
a = readFromFlash(0x800F800);
}
can anybody help me to figure out the problem?
Thanks
