How to modify Option Bytes on STM32l47 MCU
MCU: STM32l47xxx
Hi,
I am attempting to modify Option Bytes in code and am seeing undesirable behavior.
The purpose for modifying the OBs is to be able to reset into Bank 2 afterward. I am implementing to perform a on-the-fly DFU similar to the method found in AN4767. I have looked at both AN 4657 and its example code and followed their method in order to modify the OBs.
After running the code below, the MCU halts and becomes unresponsive. The only way to get it out of this state is to reset the OBs using JLinkSTM32.
I have also looked at the datasheet for this MCU and followed their steps, I am not using HAL libraries so everything is through registers.
Is there a missing step that I am not understanding that is preventing the MCU to reset into Bank 2? I have flashed my application at the start of Bank 2 to test if its working.
Any help would be appreciated!
-Tom
// Function to program the options bytes
void ProgramOptionsBytes(void)
{
SYS_UnlockFlashWrite();
while (FLASH->SR & FLASH_SR_BSY);
FLASH->SR |= FLASH_SR_OPTVERR; // Clear any previous errors
// Enable the Flash option bytes access
FLASH->OPTKEYR = FLASH_OPTKEY1;
FLASH->OPTKEYR = FLASH_OPTKEY2;
while (FLASH->SR & FLASH_SR_BSY);
// Enable the use of the 2nd bank of memory for code execution
FLASH->OPTR = 0xFFFFF8AA;
// Program the new options bytes configuration
FLASH->CR |= FLASH_CR_OPTSTRT;
// Wait for the programming to complete
while (FLASH->SR & FLASH_SR_BSY);
FLASH->CR |= FLASH_CR_OBL_LAUNCH;
}