cancel
Showing results for 
Search instead for 
Did you mean: 

stm32c071 flash erase issue

Shaf
Visitor

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

0 REPLIES 0