Can't write to flash memory after erase

14,792

The problem was that PER bit in FLASH->CR register which is set when FLASH_PageErase() is called isn't cleared at the end of it. Clearing this bit while flash is still unlocked allows other operations on flash to be run after that.

STM documentation has nothing to say about this.

Share:
14,792

Related videos on Youtube

andrey
Author by

andrey

Updated on June 04, 2022

Comments

  • andrey
    andrey almost 2 years

    So I can't write to internal flash memory directly after erasing it. If there's no erase operation before write operation, then I can. Any ideas as to why?

    Programming function returns 'successful write' value, but when looking at memory, no data is written. Here's the code:

    uint32_t pageAddress = 0x08008000;
    uint16_t buffer = 0xAAAA;
    
    HAL_FLASH_Unlock();
    FLASH_PageErase(pageAddress);
    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, pageAddress, buffer);
    HAL_FLASH_Lock();
    

    I've tried locking the memory between erasing and programming it, creating a delay between these operations, that doesn't help.

  • iwasz
    iwasz almost 7 years
    You saved my day. Another thing I noticed is, that when you try to perform few consecutive stores (using HAL_FLASH_Program) you have to clear PG bit after each operation (at least that was my case on STM32F072). So I modified my program so it performs CLEAR_BIT (FLASH->CR, (FLASH_CR_PER)) after page clear and CLEAR_BIT (FLASH->CR, (FLASH_CR_PG)) after programming a word.