OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3730|回复: 1

STM32F4(Flash读保护)

[复制链接]

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
发表于 2018-2-2 21:26:17 | 显示全部楼层 |阅读模式
STM32F4(Flash读保护)


转载来源:STM32F4(Flash读保护)



1,目的
        在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。

2,开发环境
        1,适用芯片:STM32F4全部芯片
        2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
        3,IDE:MDK517

3,程序源码
[mw_shl_code=c,true]/****************************************************************
* Function:    Flash_EnableReadProtection
* Description: Enable the read protection of user flash area.
* Input:
* Output:
* Return:      1: Read Protection successfully enable
*              2: Error: Flash read unprotection failed
*****************************************************************/
uint32_t Flash_EnableReadProtection(void)
{
  /* Returns the FLASH Read Protection level. */
  if( FLASH_OB_GetRDP() == RESET )
  {
    /* Unlock the Option Bytes */
    FLASH_OB_Unlock();
   
    /* Sets the read protection level. */
    FLASH_OB_RDPConfig(OB_RDP_Level_1);
   
    /* Start the Option Bytes programming process. */  
    if (FLASH_OB_Launch() != FLASH_COMPLETE)
    {
      /* Disable the Flash option control register access (recommended to protect
         the option Bytes against possible unwanted operations) */
      FLASH_OB_Lock();
      
      /* Error: Flash read unprotection failed */
      return (2);
    }
  
    /* Disable the Flash option control register access (recommended to protect
       the option Bytes against possible unwanted operations) */
    FLASH_OB_Lock();

    /* Read Protection successfully enable */
    return (1);
  }
  
  /* Read Protection successfully enable */
  return (1);
}

/****************************************************************
* Function:    Flash_DisableReadProtection
* Description: Disable the read protection of user flash area.
* Input:
* Output:
* Return:      1: Read Protection successfully disable
*              2: Error: Flash read unprotection failed
*****************************************************************/
uint32_t Flash_DisableReadProtection(void)
{
  /* Returns the FLASH Read Protection level. */
  if( FLASH_OB_GetRDP() != RESET )
  {
    /* Unlock the Option Bytes */
    FLASH_OB_Unlock();
   
    /* Sets the read protection level. */
    FLASH_OB_RDPConfig(OB_RDP_Level_0);
   
    /* Start the Option Bytes programming process. */  
    if (FLASH_OB_Launch() != FLASH_COMPLETE)
    {
      /* Disable the Flash option control register access (recommended to protect
         the option Bytes against possible unwanted operations) */
      FLASH_OB_Lock();
      
      /* Error: Flash read unprotection failed */
      return (2);
    }
  
    /* Disable the Flash option control register access (recommended to protect
       the option Bytes against possible unwanted operations) */
    FLASH_OB_Lock();

    /* Read Protection successfully disable */
    return (1);
  }
  
  /* Read Protection successfully disable */
  return (1);
}[/mw_shl_code]

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

6

主题

462

帖子

0

精华

高级会员

Rank: 4

积分
906
金钱
906
注册时间
2017-12-15
在线时间
111 小时
发表于 2018-2-3 12:33:03 | 显示全部楼层
当然出厂之前要设置0xCC的调试保护值了。难道这还需要说出来?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-20 03:49

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表