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.
Previous article:stm32 memory space allocation learning
Next article:Differences in IO structure between STM32F030 and STM32F103
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Active video filter (anti-aliasing)
- Sensorless BLDC motor control system circuit diagram
- TMS320C6678 Development Routine User Manual Study Part 3
- QuartusII for Altera FPGA development example 1
- Basic knowledge of smart home technology
- A DIY DC to DC negative pressure module
- Safety distance and requirements for electronic products
- What is the purpose of the PG pin?
- [Mil MYD-YA15XC-T Review] + epoll serial communication
- TI 2018 Annual Educators Conference, friends in Wuhan, let’s get started!