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

mryldz
  • 95
  • 2
  • 9

2 Answers2

0

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);
    }
}
denis krasutski
  • 618
  • 4
  • 9
  • I tried these code but it is same result. Setting code protection but code not runnig. – mryldz Sep 18 '17 at 07:25
  • What about interrupt? Do you disable interrupt before calling this function? use rim() to enable interrupts and sim() to disable interrupts: sim(); Read_Protect_Flash(); rim(); – denis krasutski Sep 18 '17 at 16:31
  • I tried and it doesn't work. But, when I first compile the code, debugger work for once. When I call the function as `sim(); Read_Protect_Flash(); rim();` code waiting at this line `flagstatus = (u8)(FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS));` in _stm8_flash.c_ – mryldz Sep 19 '17 at 06:45
0
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.

mryldz
  • 95
  • 2
  • 9