使用代码清除 PCROP
在熟悉 ST 网站上的 PCROP 参考代码基础之上, 我们将讨论如何使用代码清除 PCROP。
1. 原理
根据用户手册,要想清除 PCROP 保护, 读保护 RDP 级别必须从 1 设置成 0。也就是说,即使当前 RDP 级别为 0,我们也要
使用代码将其设置成 1。然后,同时关掉 PCROP 和将 RDP 设置成 0。 这也说明,尽管是清除 PCROP 保护,我们的代码必
须加入 RDP 的设置函数, 而不能仅仅修改参考代码中的 PCROP_Enable 的状态字段使其变成 PCROP_Disable。
Figure 1 PCROP 解除保护的条件
2. 材料准备
开发板: STM32F429I-Discovery
开发工具: STM32Cube_FW_F4_V1.15.0
STM32CubeExpansion_AN4701_F4_V1.0.0(从 ST 网站下载的参考代码)
STM32 STLink
IAR/Keil
注:也可以选择其他支持 PCROP 的 STM32 系列并选择相应的开发板与固件库。
3. 代码
设置 RDP 到级别 1
该函数在 RDP 级别为 0 时,若需要清除 PCROP, 必须被使用。
- void RDP_Enable()
- {
- HAL_FLASH_OB_Unlock();
- /* Allow Access to Flash control registers and user Falsh */
- HAL_FLASH_Unlock();
- HAL_FLASHEx_OBGetConfig(&OBInit);
- //After programming please power off and power on in order to use this board
- if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
- {
- OBInit.OptionType = OPTIONBYTE_RDP;
- OBInit.RDPLevel = OB_RDP_LEVEL_1;
- HAL_FLASHEx_OBProgram(&OBInit);
- /* Start the Option Bytes programming process */
- if (HAL_FLASH_OB_Launch() != HAL_OK)
- {
- /* User can add here some code to deal with this error */
- while (1)
- {
- }
- }
- }
- /* Prevent Access to option bytes sector */
- HAL_FLASH_OB_Lock();
- HAL_FLASH_Lock();
- }
设置 RDP 到级别 0
在清除 PCROP 保护的代码里不会直接调用这个函数。参考手册提到, PCROP 的清除必须与 RDP 从 1 到 0 同时发生,而下
列 RDP_Disable 函数则是完整独立的,无法与 PCROP 的 Option bytes 同时操作。不过,这个代码的中间部分,也就是实际
功能部分, 将在清除 PCROP 时被重用。
- void RDP_Disable()
- {
- HAL_FLASH_OB_Unlock();
- /* Allow Access to Flash control registers and user Falsh */
- HAL_FLASH_Unlock();
- HAL_FLASHEx_OBGetConfig(&OBInit);
- //After programming please power off and power on in order to use this board
- if(OBInit.RDPLevel == OB_RDP_LEVEL_1)
- {
- OBInit.OptionType = OPTIONBYTE_RDP;
- OBInit.RDPLevel = OB_RDP_LEVEL_0;
- HAL_FLASHEx_OBProgram(&OBInit);
- /* Start the Option Bytes programming process */
- if (HAL_FLASH_OB_Launch() != HAL_OK)
- {
- /* User can add here some code to deal with this error */
- while (1)
- {
- }
- }
- }
- /* Prevent Access to option bytes sector */
- HAL_FLASH_OB_Lock();
- HAL_FLASH_Lock();
- }
清除 PCROP
下述代码清除 PCROP, 它基于参考代码中的 PCROP_Enable 函数改写而成。首先,它通过 RDP_Enable 将 RDP 设置成 1。
注意实验中不要将 RDP 设置成为 2, 否则所有的 Option bytes 将不再被允许修改。 然后将 RDP 和 PCROP 都设置完毕,调
用一次 HAL_FLASH_OB_Launch 达到同时将 RDP 设置成 1 并清除 PCROP 保护。
- uint32_t PCROP_Disable(void)
- {
- RDP_Enable();
- /* Get FLASH_PCROP_SECTORS protection status */
- HAL_FLASHEx_AdvOBGetConfig(&pAdvOBInit);
- SectorsPCROPStatus = pAdvOBInit.SectorsBank1 & FLASH_PCROP_SECTORS;
- PCROPStatus = pAdvOBInit.SectorsBank1 & PCROP_ENABLED;
- /* Check if sector 2 has been already PCROP-ed */
- if ((PCROPStatus == PCROP_ENABLED) && (SectorsPCROPStatus == FLASH_PCROP_SECTORS))
- {
- /* Allow Access to option bytes sector */
- HAL_FLASH_OB_Unlock();
- /* Allow Access to Flash control registers and user Flash */
- HAL_FLASH_Unlock();
- /* Clear all nWRP bits */
- pAdvOBInit.OptionType = OPTIONBYTE_PCROP;
- pAdvOBInit.PCROPState = OB_PCROP_STATE_DISABLE;
- pAdvOBInit.Banks = FLASH_BANK_BOTH;
- pAdvOBInit.SectorsBank1 = OB_PCROP_SECTOR_All;
- pAdvOBInit.SectorsBank2 = OB_PCROP_SECTOR_All;
- HAL_FLASHEx_AdvOBProgram(&pAdvOBInit);
- /* Clear PCROP protection on sector 2 by setting nWRP 2 bit */
- pAdvOBInit.OptionType = OPTIONBYTE_PCROP;
- pAdvOBInit.PCROPState = OB_PCROP_STATE_DISABLE;
- pAdvOBInit.Banks = FLASH_BANK_1;
- pAdvOBInit.SectorsBank1 = OB_PCROP_SECTOR_2;
- HAL_FLASHEx_AdvOBProgram(&pAdvOBInit);
- /* Disable PCROP Protection by setting SPRMOD bit */
- HAL_FLASHEx_OB_SelectPCROP();
- /* Set RDP from 1 to 0*/
- OBInit.OptionType = OPTIONBYTE_RDP;
- OBInit.RDPLevel = OB_RDP_LEVEL_0;
- HAL_FLASHEx_OBProgram(&OBInit);
- /* Start the Option Bytes programming process */
- if (HAL_FLASH_OB_Launch() != HAL_OK)
- {
- /* Error occurred while options bytes programming */
- return(0);
- }
- /* Prevent Access to option bytes sector */
- HAL_FLASH_OB_Lock();
- /* Disable the Flash option control register access (recommended to protect
- the option Bytes against possible unwanted operations) */
- HAL_FLASH_Lock();
- /* PCROP Protection successfully Enabled */
- return(1);
- }
- }
4.运行
在主函数中,调用 PCROP_Disable 可解除 PCROP 保护。 RDP_Enable 后需要关闭电源,重新启动,然后系统正常运行解
除 PCROP 保护。 解除保护后,可通过 STLink 确认 PCROP Option bytes 已恢复,同时也可以看到整个 Flash 内容已被擦除。
结论
本文讨论了完全使用代码控制 PCROP 的设置与清除。它可以使用在 PCROP 代码保护的开发与部署阶段。
|