Skip to main content
l239955_st
Associate II
March 16, 2015
Question

Stm32f030c8 Flash write protection error

  • March 16, 2015
  • 6 replies
  • 1904 views
Posted on March 16, 2015 at 10:58

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 lot
    This topic has been closed for replies.

    6 replies

    Tesla DeLorean
    Guru
    March 16, 2015
    Posted on March 16, 2015 at 12:23

    What am I missing??

    Probably a FLASH_Unlock()? or to erase the options.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    l239955_st
    Associate II
    March 16, 2015
    Posted on March 16, 2015 at 12:28

    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...

    Tesla DeLorean
    Guru
    March 16, 2015
    Posted on March 16, 2015 at 12:37

    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.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    l239955_st
    Associate II
    March 16, 2015
    Posted on March 16, 2015 at 12:49 Tried this...not working...sigh HSI is enabled and I'm using st link 2 with SWD... Do you have any other idea Clive? A strange thing...when I read option bytes I get 0xFF but in the debugger register I don't see 0xFF

    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();
    }

    MFend
    Associate III
    March 4, 2019

    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

    l239955_st
    Associate II
    March 16, 2015
    Posted on March 16, 2015 at 15:18

    St Link utility works fine...:(

    I don't really know where's the problem....

    Tesla DeLorean
    Guru
    March 16, 2015
    Posted on March 16, 2015 at 17:00

    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.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..