2015-03-16 02:58 AM
Hi,
I'm wrtiting a bootloader for updating an embedded product. Bootloader sequence seems correct now and I want to protect from writing the bootloader flash addresses. Using the snippets of code from St this is my code:void Check_Write_Protection( void )
{
// unlock the Flash Program Erase Controller (FPEC)
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY);
ProtectedPages = FLASH_OB_GetWRP();
// pages are not write protected
if(ProtectedPages == 0xFFFFFFFF)
{
FLASH_OB_Unlock();
res = FLASH_OB_EnableWRP(OB_WRP_Pages0to3|OB_WRP_Pages4to7);
FLASH_OB_Lock();
NVIC_SystemReset();
}
}
the problem is when I call the
FLASH_OB_EnableWRP,
the flag for write protectione seems unlocked but i have a FLASH_PROGRAM_ERROR when I call :
if(WRP0_Data != 0xFF)
{
OB->WRP0 = WRP0_Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
OB->WRP0 = WRP0_Data
set PGERR = 1 in SR register and so memory is non protected.
Any suggestion? What am I missing?? Thanks a lot2015-03-16 04:23 AM
What am I missing??
Probably a FLASH_Unlock()? or to erase the options.2015-03-16 04:28 AM
Hi Clive,
first of all thanks for your response.Well I've tried the flash_unlock() and flash_lock(), but it seems the same. What do you mean with the Erase Option? I have to copy the value of option bytes then erase them ?Thanks...2015-03-16 04:37 AM
Flash memory needs to be erased before it is re-written.
FLASH_OB_Erase() would be needed if you change the option(s). And yes you'd probably need to evaluate the current state and save it prior to the erase so you could restore the other option settings.2015-03-16 04:49 AM
void Check_Write_Protection( void )
{
// unlock the Flash Program Erase Controller (FPEC)
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY);
ProtectedPages = FLASH_OB_GetWRP();
// pages are not write protected
if(ProtectedPages == 0xFFFFFFFF)
{
FLASH_Unlock();
OPBByteCpy = FLASH_OB_GetUser();
FLASH_OB_Unlock();
FLASH_OB_Erase();
res = FLASH_OB_EnableWRP(OB_WRP_Pages0to3);
res = FLASH_OB_EnableWRP(OB_WRP_Pages4to7);
FLASH_OB_WriteUser(OPBByteCpy);
//FLASH_OB_Launch();
FLASH_OB_Lock();
NVIC_SystemReset();
}
2015-03-16 07:18 AM
St Link utility works fine...:(
I don't really know where's the problem....2015-03-16 09:00 AM
Don't rely on the debugger, it's invasive, instrument your code, and understand the error returns, and status of the flash controller, in the cases where it is failing.
Have the code stop earlier if it gets an error.2019-03-04 07:20 AM
I'm also trying to implement something similar.
I don't understand what you are trying to do - by saving the state to OPBByteCopy and then writing it back again - you are not effecting any change...unless somehow the function FLASH_OB_EnableWRP changes the value of OPBByteCopy
Were you able to get this to work?
Thanks,
Mechi