Unable to setup RDP on STM32H7
I am trying to setup the RDP programmatically on the STM32H7. I am running the code below. All functions return HAL_OK except HAL_FLASH_OB_Launch() which returns HAL_ERROR and its unclear to me why. Any ideas on how to debug this will be helpful as well.
int main(void)
{
/* MCU Configuration */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
MX_RTC_Init();
int err = 0;
if ( HAL_FLASH_Unlock() != HAL_OK ) { err = 1 }
if ( HAL_FLASH_OB_Unlock() != HAL_OK ) { err = 1}
FLASH_OBProgramInitTypeDef ob_struct = {0};
// RDP on Flash bank 2
ob_struct.Banks = FLASH_BANK_2;
ob_struct.OptionType = OPTIONBYTE_RDP;
ob_struct.RDPLevel = OB_RDP_LEVEL_1;
// Program options bytes
HAL_FLASHEx_OBProgram(&ob_struct);
if ( HAL_FLASH_OB_Launch() != HAL_OK) { err = 1 }
// Lock memory
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
/* USER CODE END Init */