cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030 Read protection

markjohnson9
Associate II
Posted on November 21, 2016 at 18:10

Looking to see if someone can shed some additional light on programming the memory read protection level 1 or 2 for a STM32F030 device. There are a few examples but they perform other options that are not required for my application. I have unlocked the flash but experiencing questions on whether or not to set the user data 0 & 1 option bytes before setting the read protection in the flashprotect function and what other code is required. 

void unlockflash(void)

{

    while ((FLASH->SR & FLASH_SR_BSY) != 0)

    if ((FLASH->CR & FLASH_CR_LOCK) != 0)

  {    

    FLASH->KEYR = FLASH_FKEY1;

    FLASH->KEYR = FLASH_FKEY2;

  }

    if ((FLASH->CR & FLASH_CR_OPTWRE) == 0)

  {

    FLASH->OPTKEYR = FLASH_OPTKEY1;

    FLASH->OPTKEYR = FLASH_OPTKEY2;

  }

}

    

void flashprotect(void)

{

    OB->RDP |= 0xAA; // program read protect, 0xAA level 0, 0xAB level 1, 0xCC level 2.

}

/* main: initialize and start the system */

void asmfunc(void);

int __attribute__((naked)) main(void)

{

    osKernelInitialize( ); // initialize CMSIS-RTOS

    SystemCoreClockConfigure( ); // configure System Clock

    configureGPIO( ); // configure GPIO's

    unlockflash( ); // unlock flash

    flashprotect( ); //read/write protect flash

    asmfunc( ); // jump to assembly code

    osKernelStart( ); // start thread execution

}

#stm32-read-protection
1 REPLY 1
matic
Associate III
Posted on November 21, 2016 at 19:25

I am not sure how OB programming is done with F0 series, but it has to be quite similar to other series:

1. Unlock flash (LOCK bit is reset)

2. Unlock option bytes (OPTWRE is set)

3. Erase option bytes by setting OPTER bit and after that STRT bit

4. Set the OPTPG bit to allow programming option bytes

5. Write the data (half word) to the desired address