stm8 Read Out Protection (ROP) setting option byte in code

3.8k views Asked by At
    FLASH_Unlock(FLASH_MEMTYPE_DATA); 
    if(FLASH_ReadOptionByte(0x4800)!=0xaa) 
    { 
    FLASH_ProgramOptionByte(0x4800, 0xaa);  
    } 
    FLASH_Lock(FLASH_MEMTYPE_DATA); 

Using stm8s003f3.

Adding these code main initialization, code protect (ROP) is setting, but my application code is not working.

If setting option byte via IAR or ST Visual Programmer option byte tab, then both of application code and code protect (ROP) are working correctly.

I need to set ROP in code.

2

There are 2 answers

3
denis krasutski On

I used the following function:

void Read_Protect_Flash(void)
{
    FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
    FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);    
    while(FLASH_ReadOptionByte(0x4800) != 0xAA)
    {
        FLASH_Unlock(FLASH_MEMTYPE_DATA);

        FLASH_EraseOptionByte(0x4800);
        FLASH_ProgramOptionByte(0x4800, 0xAA);

        FLASH_Lock(FLASH_MEMTYPE_DATA);
    }
}
0
mryldz On
FLASH_Unlock(FLASH_MEMTYPE_DATA);

FLASH->CR2 |= FLASH_CR2_OPT;
FLASH->NCR2 &= (u8)(~FLASH_NCR2_NOPT);

 OPT->OPT0=0xAA; 

FLASH->CR2 &= (u8)(~FLASH_CR2_OPT);
FLASH->NCR2 |= FLASH_NCR2_NOPT; 

When I use it problem is solved. ROP is enable and code is running. But this may cause an other problem that unpredictable.

Because normally, while setting OPT0 via current function (FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);) waiting for flags( assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address)); *((NEAR u8*)Address) = FLASH_CLEAR_BYTE; *((NEAR u8*)(Address + 1 )) = FLASH_SET_BYTE;)

Now I remove this function use OPT->OPT0=0xAA;, so another question is what will be happen after this code change.