cancel
Showing results for 
Search instead for 
Did you mean: 

stm32c071 flash erase issue

Shaf
Associate II

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

7 REPLIES 7
TDK
Super User

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.

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

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 

Shaf
Associate II

Hello,
I have attached a video containing execution process of flash
1. erase the page 63, write into page 63 at 0x0801F800 then lock the flash

2.erase page 63, write in page 63 at 0x0801F820 then lock the flash 
You can observe then contents of memory window of FLASH page 63, FLASH status register.

Written the flash write function by following the steps explained in the reference manual.

Regards,

Shafi

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.

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

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

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

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

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)