int set_readProtection_level(uint8_t level)
{
HAL_StatusTypeDef status;
FLASH_OBProgramInitTypeDef obInit;
// 1. Unlock the Flash to enable the flash control register access
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
// 2. Get current Option Bytes
HAL_FLASHEx_OBGetConfig(&obInit);
if (obInit.RDPLevel == OB_RDP_LEVEL_2)
{
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
return 0; // no need to change, already at level 2
}
// 3. Set RDP level
obInit.OptionType = OPTIONBYTE_RDP;
obInit.RDPLevel = level;
status = HAL_FLASHEx_OBProgram(&obInit);
// // 4. Launch Option Bytes loading (this causes a reset)---加这个reload会导致设备进入btloader
// if (status == HAL_OK)
// {
// status = HAL_FLASH_OB_Launch();
// }
// 5. Lock the Flash to disable the flash control register access
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
return (status == HAL_OK) ? 0 : -1;
}
经验分享:
host 读保护实现,测试发现需要1.不要接jlink ,否则无法生效,死机。2.必须重新上电,reload会导致设备进入bt模式,死机(执行地址0x1fffxxxx地址)