在线时间179 小时
UID3435185
ST金币572
蝴蝶豆17
注册时间2017-10-29
该用户从未签到
金牌会员
- 最后登录
- 2021-8-4
|
本帖最后由 XinLiYF 于 2019-2-9 17:45 编辑
STM32F1(Flash 读保护)
1,目的
在实际的产品发布中,如果不对储存在单片机 Flash 中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link 等)把 Flash 中的程序读取回来,得到 bin 文件或 hex 文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把 Flash 设置成读保护。
2,开发环境
1,适用芯片:STM32F1 全部芯片
2,固件库:STM32Cube_FW_F1_V1.6.1
3,IDE:MDK525
3,程序源码
- /**
- * @brief Set flash read protection.
- * @param [in] state: Flash read protection state, true: enable protection, false: disable protection.
- * @retval true: Successful operation.
- * @retval false: Operation failed.
- */
- bool SetFlashReadProtection(bool state)
- {
- FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
- HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
-
- if(state == true)
- {
- if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
- {
- OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
- OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
-
- HAL_FLASH_Unlock();
- HAL_FLASH_OB_Unlock();
-
- if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
- {
- HAL_FLASH_OB_Lock();
- HAL_FLASH_Lock();
-
- return false;
- }
-
- HAL_FLASH_OB_Lock();
- HAL_FLASH_Lock();
- }
- }
- else
- {
- if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
- {
- OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
- OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_0;
-
- HAL_FLASH_Unlock();
- HAL_FLASH_OB_Unlock();
-
- if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
- {
- HAL_FLASH_OB_Lock();
- HAL_FLASH_Lock();
-
- return false;
- }
-
- HAL_FLASH_OB_Lock();
- HAL_FLASH_Lock();
- }
- }
-
- return true;
- }
复制代码
|
|