Skip to main content
Visitor II
November 14, 2025
Question

stm32c071 flash erase issue

  • November 14, 2025
  • 3 replies
  • 332 views

Post edited by ST moderator to be inline with the community rules for the code sharing. In next time please use </> button to paste your code. Please read this post: How to insert source code.

Hi,

I am using stm32c071 MCU to store data into flash. I have erased page 63 wrote data into 0x0801F800 then locked the flash and then again unlocked it erased page 63 wrote data in 0x0801F820, but the contents of 0x0801F800 is still there even after page erase. Is this expected?

My flash write code

eFLASH_ErrorType_t WriteDataToFlash(uint64_t *data, uint32_t address, uint8_t page, uint8_t length)

{

eFLASH_ErrorType_t err_code = e_SUCCESS;

uint32_t *addr = (uint32_t*)address;

// Check if flash is busy

err_code = CheckFlashBusy();

if(err_code != e_SUCCESS)

{

return err_code;

}

// Unlock flash

err_code = flashUnlock();

if(err_code != e_FLASH_UNLOCKED)

{

return err_code;

}

// Erase page before writing

err_code = flashErasePage(page);

if(err_code != e_SUCCESS)

{

return err_code;

}

// Clear any previous errors

ClearFlashErrors();



// Check if flash configuration is busy

if(flashGetStatus(FLASH_SR_CFGBSY) == e_SUCCESS)

{

FLASH->CR |= FLASH_CR_PG; // Flash programming Enable

FLASH->CR |= FLASH_CR_EOPIE; // Enable Interrupt when EOP bit is set



// Program double words

for (uint8_t i = 0; i < length; i++)

{

*(__IO uint32_t*)addr = (uint32_t)data[i];

addr = addr + 1u;

*(__IO uint32_t*)addr = (uint32_t)(data[i] >> 32u);

addr = addr + 1u;



}



// Wait for operation completion

if(flashGetStatus(FLASH_SR_CFGBSY) == e_FLASH_ERROR)

{

err_code = e_FLASH_BUSY;

}



// Wait for and clear EOP flag

if(WaitForFlashEndOfOperation() == e_FLASH_ERROR)

{

err_code = e_FLASH_ERROR;

}



// Disable programming mode

FLASH->CR &= ~FLASH_CR_PG;

}

else

{

err_code = e_FLASH_BUSY;

}



flashLock();



return err_code;

}

Regards,

Shafi

    This topic has been closed for replies.

    3 replies

    Super User
    November 14, 2025

    If you erase a paqe, it will be 0xFF until written to.

    The code provided doesn't give enough information. Perhaps post a full example that exhibits the issue. Explain how you're verifying the flash data is not 0xFF.

    It sounds like the erase failed.

    Your code skips a lot of the required steps when programming flash as given in the reference manual.

    ST Employee
    November 17, 2025

    it seems the flash are not erased successfully.

    you can refer the cube examples here for flash erase:

    STM32CubeC0/Projects/NUCLEO-C071RB/Examples/FLASH/FLASH_EraseProgram at main · STMicroelectronics/STM32CubeC0 · GitHub 

    Super User
    November 17, 2025

    In step 2, the page didn't get erased. Can't see why because variables are not shown. Perhaps the page number is wrong. Probably it erased a page somewhere but not the one you wanted.

    ShafAuthor
    Visitor II
    November 18, 2025

    You are right, page isn't getting erased.

    As per suggestion from @Shirley.Ye I have used  STM32CubeC0/Projects/NUCLEO-C071RB/Examples/FLASH/FLASH_EraseProgram to see the functioning of FLASH on NUCLEO-C071 dev kit and my custom board (STM32C071KBT6). 

    The code works correctly on the NUCLEO-C071 board for erasing and writing to flash.
    However, on my custom board, the first erase and write cycle works without errors(because the page is empty).
    When I re-run the code a second time, the erase operation returns HAL_OK, but the flash page is not actually erased(in memory window).
    Then, when I try to write to flash, PROGERR is set in the FLASH_SR register. 

    What could be causing this issue? two different board behavior for same code.

     Regards,

    Shafi

    Super User
    November 18, 2025

    I recommend posting a full compile-able program which exhibits the issue. You're missing something in the code.

    ShafAuthor
    Visitor II
    November 18, 2025

    As mentioned i am using STM32CubeC0/Projects/NUCLEO-C071RB/Examples/FLASH/FLASH_EraseProgram 

    code flashed on 2 different boards . NUCLEO-C071 and my custom board(containing STM32c071KBT6)