[STM32F1] STM32F103VBT6 HAL库设置FLASH读保护

[复制链接]
7232|19
 楼主| killalljp 发表于 2018-8-21 13:50 | 显示全部楼层 |阅读模式
本帖最后由 killalljp 于 2018-8-23 17:16 编辑

最近用到要在STM32F103VBT6程序中使用HAL库加入FLASH读保护功能,网上找了一下多多少少都有点问题
最后奉献已验证代码
  1. /****************************************************************
  2. * Function:    Flash_EnableReadProtection
  3. * Description: Enable the read protection of user flash area.
  4. * Input:        NONE
  5. * Output:        NONE
  6. * Return:  NONE
  7. *****************************************************************/

  8. void Flash_EnableReadProtection(void)
  9. {

  10.   FLASH_OBProgramInitTypeDef OBInit;
  11.   
  12.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  13.   
  14.   HAL_FLASHEx_OBGetConfig(&OBInit);
  15.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  16.   {
  17.     OBInit.OptionType = OPTIONBYTE_RDP;
  18.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  19.     HAL_FLASH_Unlock();
  20.     HAL_FLASH_OB_Unlock();
  21.     HAL_FLASHEx_OBProgram(&OBInit);
  22.     HAL_FLASH_OB_Lock();
  23.     HAL_FLASH_Lock();
  24.   }
  25.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  26. }

  27. /****************************************************************
  28. * Function:    Flash_DisableReadProtection
  29. * Description: Disable the read protection of user flash area.
  30. * Input:        NONE
  31. * Output:        NONE
  32. * Return:  NONE
  33. *****************************************************************/
  34. void Flash_DisableReadProtection(void)
  35. {

  36.   FLASH_OBProgramInitTypeDef OBInit;
  37.   
  38.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  39.   
  40.   HAL_FLASHEx_OBGetConfig(&OBInit);
  41.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  42.   {
  43.     OBInit.OptionType = OPTIONBYTE_RDP;
  44.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  45.     HAL_FLASH_Unlock();
  46.     HAL_FLASH_OB_Unlock();
  47.     HAL_FLASHEx_OBProgram(&OBInit);
  48.     HAL_FLASH_OB_Lock();
  49.     HAL_FLASH_Lock();
  50.   }
  51.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  52. }
天灵灵地灵灵 发表于 2018-8-21 15:35 | 显示全部楼层
发代码姿势不够帅
看我的
  1. /****************************************************************
  2. * Function:    Flash_EnableReadProtection
  3. * Description: Enable the read protection of user flash area.
  4. * Input:        NONE
  5. * Output:        NONE
  6. * Return:  NONE
  7. *****************************************************************/

  8. void Flash_EnableReadProtection(void)
  9. {

  10.   FLASH_OBProgramInitTypeDef OBInit;
  11.   
  12.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  13.   
  14.   HAL_FLASHEx_OBGetConfig(&OBInit);
  15.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  16.   {
  17.     OBInit.OptionType = OPTIONBYTE_RDP;
  18.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  19.     HAL_FLASH_Unlock();
  20.     HAL_FLASH_OB_Unlock();
  21.     HAL_FLASHEx_OBProgram(&OBInit);
  22.     HAL_FLASH_OB_Lock();
  23.     HAL_FLASH_Lock();
  24.   }
  25.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  26. }

  27. /****************************************************************
  28. * Function:    Flash_DisableReadProtection
  29. * Description: Disable the read protection of user flash area.
  30. * Input:        NONE
  31. * Output:        NONE
  32. * Return:  NONE
  33. *****************************************************************/
  34. void Flash_DisableReadProtection(void)
  35. {

  36.   FLASH_OBProgramInitTypeDef OBInit;
  37.   
  38.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  39.   
  40.   HAL_FLASHEx_OBGetConfig(&OBInit);
  41.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  42.   {
  43.     OBInit.OptionType = OPTIONBYTE_RDP;
  44.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  45.     HAL_FLASH_Unlock();
  46.     HAL_FLASH_OB_Unlock();
  47.     HAL_FLASHEx_OBProgram(&OBInit);
  48.     HAL_FLASH_OB_Lock();
  49.     HAL_FLASH_Lock();
  50.   }
  51.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  52. }

评论

贴的两个函数,内容似乎是一样的呢? Flash_DisableReadProtection()和 Flash_EnableReadProtection(),我怎么看,内容都是一样的呢?  发表于 2018-12-23 12:00
天灵灵地灵灵 发表于 2018-8-21 15:36 | 显示全部楼层
发代码姿势不够帅
看我的
  1. /****************************************************************
  2. * Function:    Flash_EnableReadProtection
  3. * Description: Enable the read protection of user flash area.
  4. * Input:        NONE
  5. * Output:        NONE
  6. * Return:  NONE
  7. *****************************************************************/

  8. void Flash_EnableReadProtection(void)
  9. {

  10.   FLASH_OBProgramInitTypeDef OBInit;
  11.   
  12.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  13.   
  14.   HAL_FLASHEx_OBGetConfig(&OBInit);
  15.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  16.   {
  17.     OBInit.OptionType = OPTIONBYTE_RDP;
  18.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  19.     HAL_FLASH_Unlock();
  20.     HAL_FLASH_OB_Unlock();
  21.     HAL_FLASHEx_OBProgram(&OBInit);
  22.     HAL_FLASH_OB_Lock();
  23.     HAL_FLASH_Lock();
  24.   }
  25.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  26. }

  27. /****************************************************************
  28. * Function:    Flash_DisableReadProtection
  29. * Description: Disable the read protection of user flash area.
  30. * Input:        NONE
  31. * Output:        NONE
  32. * Return:  NONE
  33. *****************************************************************/
  34. void Flash_DisableReadProtection(void)
  35. {

  36.   FLASH_OBProgramInitTypeDef OBInit;
  37.   
  38.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  39.   
  40.   HAL_FLASHEx_OBGetConfig(&OBInit);
  41.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  42.   {
  43.     OBInit.OptionType = OPTIONBYTE_RDP;
  44.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  45.     HAL_FLASH_Unlock();
  46.     HAL_FLASH_OB_Unlock();
  47.     HAL_FLASHEx_OBProgram(&OBInit);
  48.     HAL_FLASH_OB_Lock();
  49.     HAL_FLASH_Lock();
  50.   }
  51.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  52. }
 楼主| killalljp 发表于 2018-8-23 11:15 | 显示全部楼层
天灵灵地灵灵 发表于 2018-8-21 15:36
发代码姿势不够帅
看我的

超级潜水艇 偶尔冒泡。学了一招
leafz 发表于 2018-9-3 16:21 | 显示全部楼层
这个设置貌似不能禁止stlink读出芯片里的程序啊
monty·feng 发表于 2018-10-28 15:42 | 显示全部楼层
leafz 发表于 2018-9-3 16:21
这个设置貌似不能禁止stlink读出芯片里的程序啊

请问关于禁止stlink读程序的这个问题你找到了,该如何解决呢?

评论

需要到option位里设置读写保护。  发表于 2018-12-28 09:38
dabendan001 发表于 2018-12-23 00:53 | 显示全部楼层
一楼、二楼、三楼,贴的两个函数,内容似乎是一样的呢? Flash_DisableReadProtection()和 Flash_EnableReadProtection(),我怎么看,内容都是一样的呢?
dabendan001 发表于 2018-12-23 12:00 | 显示全部楼层
贴的两个函数,内容似乎是一样的呢? Flash_DisableReadProtection()和 Flash_EnableReadProtection(),我怎么看,内容都是一样的呢?
天灵灵地灵灵 发表于 2018-12-27 09:22 | 显示全部楼层
dabendan001 发表于 2018-12-23 00:53
一楼、二楼、三楼,贴的两个函数,内容似乎是一样的呢? Flash_DisableReadProtection()和 Flash_EnableR ...

你眼花了。一个是lock,一个是unlock
其他我看也一样。眼晕。
天灵灵地灵灵 发表于 2018-12-27 09:22 | 显示全部楼层
dabendan001 发表于 2018-12-23 12:00
贴的两个函数,内容似乎是一样的呢? Flash_DisableReadProtection()和 Flash_EnableReadProtection(), ...

仔细看里面调用的函数。不同。
天灵灵地灵灵 发表于 2018-12-27 09:24 | 显示全部楼层
dabendan001 发表于 2018-12-23 12:00
贴的两个函数,内容似乎是一样的呢? Flash_DisableReadProtection()和 Flash_EnableReadProtection(), ...

现在HAL库用的是

/* Peripheral Control functions  **********************************************/
HAL_StatusTypeDef HAL_FLASH_Unlock(void);
HAL_StatusTypeDef HAL_FLASH_Lock(void);
HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
/* Option bytes control */
HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
这几个。我看头文件里是这几个。你仔细看楼主发的函数里面封装的两个不同。
其实直接用就可以的。他为了安全可靠多了一道验证的工序。
因为两个工序一样,所以大部分代码相同。容易 看花眼
dabendan001 发表于 2018-12-27 18:38 | 显示全部楼层
天灵灵地灵灵 发表于 2018-12-27 09:22
你眼花了。一个是lock,一个是unlock
其他我看也一样。眼晕。

我晕!我真的不好意思再发问了……我又瞅了3遍,还是没发现哪里不同……两个函数里写的,都是这一样的代码啊?
    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();
    HAL_FLASHEx_OBProgram(&OBInit);
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();
dabendan001 发表于 2018-12-27 18:38 | 显示全部楼层
天灵灵地灵灵 发表于 2018-12-27 09:22
你眼花了。一个是lock,一个是unlock
其他我看也一样。眼晕。

我晕!我真的不好意思再发问了……我又瞅了3遍,还是没发现哪里不同……两个函数里写的,都是这一样的代码啊?(方便告诉我哪两行内容不同不?代码前有行号)
    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();
    HAL_FLASHEx_OBProgram(&OBInit);
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();
coco01 发表于 2018-12-28 09:16 | 显示全部楼层
liw_ei 发表于 2019-1-11 16:06 | 显示全部楼层
我比较了.代码是一样的
mj1114 发表于 2019-6-28 11:26 | 显示全部楼层
网上很多发布的代码是有很多有意或无意的坑的。
jay8830095 发表于 2019-12-7 17:55 | 显示全部楼层
大坑,不能用;
[..] Any operation of erase or program should follow these steps:
     (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
         register access.
     (#) Call the following functions to program the desired option bytes.
         (++) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);      
     (#) Once all needed option bytes to be programmed are correctly written, call the
         HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
     (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
         to protect the option Bytes against possible unwanted operations).

HAL_FLASH_OB_Launch
 楼主| killalljp 发表于 2020-12-6 19:17 | 显示全部楼层
本帖最后由 killalljp 于 2020-12-6 19:18 编辑

更正一下

  1. /****************************************************************
  2. * Function:    Flash_EnableReadProtection
  3. * Description: Enable the read protection of user flash area.
  4. * Input:        NONE
  5. * Output:        NONE
  6. * Return:  NONE
  7. *****************************************************************/

  8. void Flash_EnableReadProtection(void)
  9. {

  10.   FLASH_OBProgramInitTypeDef OBInit;
  11.   
  12.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  13.   
  14.   HAL_FLASHEx_OBGetConfig(&OBInit);
  15.   if(OBInit.RDPLevel == OB_RDP_LEVEL_0)
  16.   {
  17.     OBInit.OptionType = OPTIONBYTE_RDP;
  18.     OBInit.RDPLevel = OB_RDP_LEVEL_1;
  19.     HAL_FLASH_Unlock();
  20.     HAL_FLASH_OB_Unlock();
  21.     HAL_FLASHEx_OBProgram(&OBInit);
  22.     HAL_FLASH_OB_Lock();
  23.     HAL_FLASH_Lock();
  24.   }
  25.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  26. }

  27. /****************************************************************
  28. * Function:    Flash_DisableReadProtection
  29. * Description: Disable the read protection of user flash area.
  30. * Input:        NONE
  31. * Output:        NONE
  32. * Return:  NONE
  33. *****************************************************************/
  34. void Flash_DisableReadProtection(void)
  35. {

  36.   FLASH_OBProgramInitTypeDef OBInit;
  37.   
  38.   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  39.   
  40.   HAL_FLASHEx_OBGetConfig(&OBInit);
  41.   if(OBInit.RDPLevel == OB_RDP_LEVEL_1)
  42.   {
  43.     OBInit.OptionType = OPTIONBYTE_RDP;
  44.     OBInit.RDPLevel = OB_RDP_LEVEL_0;
  45.     HAL_FLASH_Unlock();
  46.     HAL_FLASH_OB_Unlock();
  47.     HAL_FLASHEx_OBProgram(&OBInit);
  48.     HAL_FLASH_OB_Lock();
  49.     HAL_FLASH_Lock();
  50.   }
  51.   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

  52. }


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

本版积分规则

2

主题

24

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部