问题解决了。
stm32f2xx_flash.c 中有说明
Any operation of erase or program should follow these steps:
1. Call the FLASH_OB_Unlock() function to enable the FLASH option control register access
2. Call one or several functions to program the desired Option Bytes:
- void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState) => to Enable/Disable
the desired sector write protection
- void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
- void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY) => to configure
the user Option Bytes.
- void FLASH_OB_BORConfig(uint8_t OB_BOR) => to set the BOR Level
3. Once all needed Option Bytes to be programmed are correctly written, call the
FLASH_OB_Launch() function to launch the Option Bytes programming process.
@NOTE When changing the IWDG mode from HW to SW or from SW to HW, a system
reset is needed to make the change effective.
4. Call the FLASH_OB_Lock() function to disable the FLASH option control register
access (recommended to protect the Option Bytes against possible unwanted operations)
所以正确的顺序应该是:
#if 1
if(SET != FLASH_OB_GetRDP())
{
printf("Flash read protection not set, protection.....\n");
FLASH_Unlock();
FLASH_OB_Unlock();
//FLASH_OB_WRPConfig();
FLASH_OB_RDPConfig(OB_RDP_Level_1);
//FLASH_OB_UserConfig();
//FLASH_OB_BORConfig();
printf("protection done\n");
FLASH_OB_Launch();
FLASH_OB_Lock();
FLASH_Lock();
}
#endif /* Flash read protection */
然后系统重新上电,保护生效。
|