Resetting option bytes of STM32U575xx with RAM application
Currently I write locked the first few sectors of my STM32U575xx. However I would also like the unlock the write protection. Since both J-Link and the ST-Link cannot do this I thought about writing a small application and run it from RAM to reset the option bytes. This however does not work.
When I try to write the option bytes the status register returns `Option write error`. Does anyone has an idea on what I'm doing wrong? I previously locked pages 0 to 8, I thought I leave it unchanged. Changing this to the factory default however doesn't work.
The failure occurs on line 82.
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_ICACHE_Init();
/* USER CODE BEGIN 2 */
void disable_wr_protection(void);
disable_wr_protection();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/** Enable write protection on the bootloader region if not yet enabled
*/
void disable_wr_protection(void)
{
// Get FLASH_WRP_SECTORS write protection status
FLASH_OBProgramInitTypeDef option_bytes_init = {0};
option_bytes_init.WRPArea = OB_WRPAREA_BANK1_AREAA;
HAL_FLASHEx_OBGetConfig(&option_bytes_init);
uint32_t wrp_status = ((option_bytes_init.OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
&& (option_bytes_init.WRPLock == DISABLE);
// check if write protection is already enabled
if (wrp_status)
{
printf("wrp: write protection disabled\r\n");
return;
}
printf("wrp: disabling write protection on the bootloader sections\r\n");
// allow access to flash control registers and user flash
if (HAL_FLASH_Unlock() != HAL_OK)
// reset on failure, shouldn't happen
NVIC_SystemReset();
// allow access to option bytes sector
if (HAL_FLASH_OB_Unlock() != HAL_OK)
// reset on failure, shouldn't happen
NVIC_SystemReset();
// enable FLASH_WRP_SECTORS write protection
option_bytes_init.OptionType = OPTIONBYTE_WRP;
option_bytes_init.WRPStartOffset = 0;
option_bytes_init.WRPEndOffset = 8;
option_bytes_init.WRPLock = DISABLE;
if (HAL_FLASHEx_OBProgram(&option_bytes_init) != HAL_OK)
// reset on failure, shouldn't happen
NVIC_SystemReset();
// start the option bytes programming process
if (HAL_FLASH_OB_Launch() != HAL_OK)
// reset on failure, shouldn't happen
NVIC_SystemReset();
// prevent access to option bytes sector
if (HAL_FLASH_OB_Lock() != HAL_OK)
// reset on failure, shouldn't happen
NVIC_SystemReset();
// disable the flash option control register access (recommended to protect the option bytes against possible unwanted operations)
if (HAL_FLASH_Lock() != HAL_OK)
// reset on failure, shouldn't happen
NVIC_SystemReset();
// get FLASH_WRP_SECTORS write protection status
HAL_FLASHEx_OBGetConfig(&option_bytes_init);
wrp_status = ((option_bytes_init.OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
&& (option_bytes_init.WRPLock == DISABLE);
// check if FLASH_WRP_SECTORS write protection is disabled
if (wrp_status)
printf("wrp: write protection disabled\r\n");
else
printf("wrp: failed to disable write protection\r\n");
}Related: https://community.st.com/s/question/0D53W00001JVhsxSAD/howto-reset-option-bytes-on-stm32u575xx
