first, the statements after your first system reset will never be excuted.
2nd, if the read-protection is enabled(might be the result of erase the option bytes), you will not be able to debug, and come across hard fault. hope it helps.
I am currently exploring the read/write capacities of a STM32F103. Basically, I am trying to write a value at a desired memory address and then write protect it but there seems to be a probleme with the FLASH_GetWriteProtectionOptionByte() function as it always return 0xFFFFFFFF. This is the code I am using : /******************************************************/ FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); WRPR_Value = FLASH_GetWriteProtectionOptionByte(); ProtectedPages = WRPR_Value & 0x000000C0; if (ProtectedPages == 0x00) {/* Pages are write protected */ /* Disable the write protection */ FLASHStatus = FLASH_EraseOptionBytes(); /* Generate System Reset to load the new option byte values */ NVIC_GenerateSystemReset(); } if (ProtectedPages != 0x00) {/* Pages not write protected */ /* Enable the pages write protection */ FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages24to27 |FLASH_WRProt_Pages28to31); /* Generate System Reset to load the new option byte values */ NVIC_GenerateSystemReset(); } /******************************************************/ The thing is that, on the first time, the logic seems correct, the WRPR_Value returns no write protection (0xFFFFFFF) and enter the condition that enables it. However, after the reset, it does exactly the same thing resulting in an infinite loop. I saw that there was a similar problem here :
Isn't it recommended to reset the device if changing the RO/RW protections to apply the changes to the FLASH controller? Pls check in Flash programming manual.