STM32 read protection function and clear read protection function settings [transfer]

Publisher:Yuexiang666Latest update time:2018-12-29 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. STM32 protection measures for internal Flash 

   

All STM32 chips provide Flash protection to prevent illegal access to the Flash - write protection and read protection.
  

 1) Read protection is commonly referred to as "encryption", which acts on the entire Flash storage area. Once Flash read protection is set, the built-in Flash storage area can only be read out through normal program execution, and cannot be read out through any of the following methods:

Through a debugger (JTAG or SWD);

Programs that are launched and executed from RAM;
  

 2) Write protection is provided in units of four pages (1KB/page) of Flash storage area. Programming or erasing operations on the protected pages will not be executed, and an operation error flag will be generated.  
   

The effects of read and write settings are shown in the following table:
   

Read protection and write protection for Flash operation functions
    

The effective CPU can only read, and debugging and illegal access are prohibited.
    

Valid and invalid: CPU can read and write, debugging and illegal access are prohibited, pages 0~3 are write-protected.
    

Invalid valid CPU readable, allowing debugging and illegal access.
    

Invalid Invalid CPU can read and write, allowing debugging and illegal access.

2. When Flash read protection is in effect, the CPU can execute programs to read the protected Flash area, but there are two exceptions:
   

1) When debugging the execution program;
   

2) When starting and executing the program from RAM
   

STM32 also provides a special protection, that is, after applying read protection to the Flash storage area, even if write protection is not enabled, pages 0 to 3 of the Flash will be in a write-protected state. This is to prevent the modification of the reset or interrupt vector and jump to the RAM area to execute illegal program code.

3. Flash protection related functions
   FLASH_Unlock(); //Flash unlock
   FLASH_ReadOutProtection(DISABLE); //Flash read protection disabled  
   FLASH_ReadOutProtection(ENABLE); //Flash read protection enabled


4. STM32 read protection and clear read protection operations

  

Function: After the read protection is set, the contents of the flash cannot be read; when the read protection is released, the stm32 will automatically erase the entire flash;
  

Read protection setting: Add "read protection" code at the beginning of the program to implement the read protection function; (Enable protection each time the program is run)
  

Remove read protection: Remove read protection can be set in the button to facilitate unlocking. It is also not possible to execute the program in RAM without setting the button and then remove the read protection;

  (1) Set read protection:

 int main(void)
{
    ....

      
    if(FLASH_GetReadOutProtectionStatus() != SET)
    {
        //FLASH_Unlock(); Can read protection be set without unlocking FALSH? ? ?
        FLASH_ReadOutProtection(ENABLE);   
    }
    ......
    while(1)
    {
       .....
     }
}

    

After this is written into the program and executed, the program cannot be "read out" using j-link, which is "read protection"! The program downloaded to the chip can be read without using this program, but if this program is used, the program cannot be read out. However, it is also impossible to burn a new program into the chip again (please be careful when testing!!!!!!)

  

A button can be set in the main program to clear the "read protection". Once the button is pressed, the chip can be re-programmed when the "read protection" is cleared.

  

(2) Remove read protection: Add the following code to an operation in the program (such as pressing a button, etc.). After execution,
the code kills itself!
if(FLASH_GetReadOutProtectionStatus() != RESET)
{
FLASH_Unlock();
FLASH_ReadOutProtection(DISABLE);
}
   These functions are in stm32f10x_flash. Note: When calling the above library, you need to add #define _FLASH_PROG before #include "stm32f10x_flash.h"; otherwise, an error (not defined) will be reported.

   If you have not done the step of clearing the read protection by pressing the key, there is a second method to remedy this: write a special program to clear the "read protection", use the method of running the program in RAM, run this program to unlock the "read protection", and after execution, the FLASH will be automatically erased. The code is as follows:
int main(void)
{
Chinp_Init();
FLASH_Unlock();
FLASH_ReadOutProtection(DISABLE); 
}

※ For running the program in RAM, the instructions are as follows:
(1) I use IAR + J-Link, and I can debug in RAM without setting the Boot0 and boot1 pins to start from RAM (I set it to start from FLASH).
(2) Set the Link file to lnkarm_ram.xcl in the IAR environment
(3) Uncheck all in the Download tab of the Debuger option.
After completing the above settings, press the Debug button and execute the above program to remove the read protection.

 

*************************Gorgeous dividing line***********************************************

  
if (FLASH_GetReadOutProtectionStatus() == RESET) 
{
FLASH_Unlock();
FLASH_ReadOutProtection(ENABLE); 
}
*************************Gorgeous dividing line***********************************************
 
if (FLASH_GetReadOutProtectionStatus() == SET) 


FLASH_Unlock();
FLASH_ReadOutProtection(DISABLE); 
}
After CPU Flash read protection is enabled, emulator debugging will fail. J-LINK has an unlock menu, which needs to be unlocked before the program can be burned again normally. Of course, unlocking will cause all Flash contents to be erased. 

Start the "J-Flash ARM" tool, and then click Target->unsercure chip to remove the chip's read protection. After clicking Target->unsercure chip, you must power on and reset the system.


Keywords:STM32 Reference address:STM32 read protection function and clear read protection function settings [transfer]

Previous article:stm32 memory space allocation learning
Next article:Differences in IO structure between STM32F030 and STM32F103

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号