stm32芯片解除写保护方法(详细)

 

一. 写保护

1. 目的

  将Flash设置为写保护的目的,是为了防止其他人通过J-Link,ULINK2等仿真器,将Flash中的程序读取出来(设想一下,你辛辛苦苦研发的产品,别人通过仿真器将程序读取出来,再copy一下产品的硬件,就可以生产)。
  可以通过将Flash设置为读保护来保护自己的程序。

2. 开发环境

适用于STM32F1和F4系列(其他系列没有用过);
F1系列的库:STM32F10x_StdPeriph_Lib_V3.5.0
F4系列的库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
开发环境使用的MDK,版本5.25

3. 程序

  通过flash_if.c源程序中的FLASH_If_EnableReadProtection()函数来加密Flash。函数代码如下:

  /**
    * @brief  Enable the read protection of user flash area.
    * @param  None
    * @retval 1: Read Protection successfully enable
    *         2: Error: Flash read unprotection failed
    */

    uint32_t FLASH_If_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);
    }

  一般在BootLoader的主函数里,加入FLASH_If_EnableReadProtection()函数调用,加密Flash。
  加密后,无法再通过仿真器来Debug或烧写程序,只能通过串口等IAP方式来烧写用户程序。
  工程需要添加flash_if.h和flash_if.c文件。
在这里插入图片描述

二. 解除写保护

有两种方法可以解除Flash的写保护。

1. 建立MDK工程,程序设置为SRAM启动,在程序中解除Flash的锁定。

以作者最近使用的STM32F412为例,新建MDK工程,设置Target选项卡:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190327162525708.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podDIzNzAyMDE=,size_16,color_FFFFFF,t_70
配置C/C++选项卡,根据芯片型号,包含对应宏定义;预编译宏VECT_TAB_SRAM为必添加项。
在这里插入图片描述
添加初始化文件路径:G:\ProgrammeFiles\Keil5\ARM\Pack\Keil\STM32F4xx_DFP\2.13.0\MDK\Boards\Keil\MCBSTM32F400\Blinky\Debug_RAM.ini (MDK的安装路径不同这里有所不同)
在这里插入图片描述
在main函数中调用Flash_DisableReadProtection()函数,函数代码如下:

/****************************************************************
 * 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);
}

函数调用完成后,Flash芯片也已经解锁成功。
Flash芯片内的程序会被清除。

2. 通过J-Flash解锁

1中的方法需要自己新建MDK工程,比较繁琐。
可以通过JFlash软件,配合JLink仿真器来解锁。
JFlash软件的下载网址为:
https://www.segger.com/downloads/jlink/JLink_Windows.exe
下载安装后,在安装目录下会看到JFlash软件
在这里插入图片描述
打开JFlash,选择Create a new project
在这里插入图片描述
注意:在弹出的Create New Project对话框中,Speed(kHz)默认是4000,即4MH在,一定要选择200k以下。
因为SWD总线布线太长或者不规范,若使用默认的下载速度(4MHZ),下载或者擦除芯片时会导致出现下述错误。
在这里插入图片描述
  在Target Device中选择对应要解锁的芯片。
  以本人所用的STM32F412CE为例,Flash size为512KB,本应选择STM32F411CE,由于STM32F411CE的Flash size为512KB+16MB,因此,选择STM32F411VE。
  只要Flash size大小与要解锁的芯片的Flash大小一致,Device可以不一致。
在这里插入图片描述通过SWD将芯片与JLink连接后,选择Target-Connect即可连接成功。
在这里插入图片描述
再选择Target-Manual Programming-Erase Chip,即可解除芯片的Flash锁定状态。
不过Flash内的所有数据都会被清除。
在这里插入图片描述

 

 

 3 问题:使用ISP/J-Link/ST-Link等无法下载代码,提示芯片写保护;读芯片信息时提示读保护。

原因:一般是修改了选项字节。 
mcuisp

解决方法:这里使用ST-Link Utility来修改选项字节。

  1. 使用ST-Link连接到STM32芯片,点击Connect。 
    connect

  2. 存在读保护。 
    readerror

  3. 修改选项字节。 
    changeoption

  4. 将读保护等级修改为Level 0。 
    level0

  5. 打钩的扇区会添加写保护,点击Unselect all不选择写保护。 
    writeprotection

  6. 写入选项字节后Flash会被擦除。 
    eraseflash

  7. 能正常写入程序。 
    writeprogram

你可能感兴趣的:(嵌入式)