cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G030K8 eeprom emulation

Jimi
Visitor

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

5 REPLIES 5
Andrew Neil
Evangelist III

@Jimi wrote:

It didn't work.


Please give more details!

Tell us what tests/investigations/debugging you've done, and what you've found so far

TDK
Guru

How are you determining that it isn't "working"? Put in an infinite loop at the end of main() so you don't return from it.

Step through the program in debug mode and look at FLASH->SR to see any programming errors.

See if you can program the memory through STM32CubeProgrammer and see if the erase step is working.

Program seems fine otherwise.

 

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for your attention.

First, I must say that I clicked the accept icon by mistake. Sorry for that.

I connected to STM32G030K8 using Jlink Programmer and in debug mode through SWD. I can see that all of the steps in writeToFlash() function executed correctly, However when I read the same address I see another value ( I got a = 0x03, but I write 0x05 on that address).

Thank you so much.

As I explained  I connected to STM32G030K8 using Jlink Programmer and in debug mode through SWD. I didn't see any errors in FLASH->SR. I don't understand what is the mistake.