Introduction to the internal FLASH of STM32F429

1. Introduction to the internal FLASH of STM32

There is a FLASH memory inside the STM32 chip, which is mainly used to store codes. After we write the application program on the computer, we use the downloader to burn the compiled code files into the internal FLASH. Since the contents of the FLASH memory are lost It will not be lost after power on. After the chip is powered on and reset, the kernel can load the code from the internal FLASH and run it, as shown in the figure below:
  insert image description here
In addition to using external tools (such as downloaders) to read and write the internal FLASH, when the STM32 chip is running , can also read and write its own internal FLASH, therefore, if the internal FLASH has space left after storing the application program, we can use it like the external SPI-FLASH to store some data that needs to be generated when the program is running. stored data.

Because the speed of accessing the internal FLASH is much faster than the external SPI-FLASH, the internal FLASH is often used to store key records in an emergency; in order to prevent the application from being plagiarized, some applications will prohibit reading and writing the contents of the internal FLASH , or calculate the encrypted information and record it in some areas when it runs for the first time, and then delete part of its own encrypted code, these applications all involve the operation of the internal FLASH.

2. Composition of internal FLASH

The internal FLASH of STM32 includes main memory, system memory, OTP area and option byte area. Their address distribution and size are as follows: the
  insert image description here
insert image description here
insert image description here
description of each storage area is as follows:

(1) Main memory
    Generally, when we talk about the internal FLASH of STM32, we refer to this main memory area, which is the space for storing user applications. The 1M FLASH and 2M FLASH in the chip model description refer to the size of this area.
    The main memory is divided into two blocks, 2MB in total, and each block is divided into 12 sectors, including four 16KB sectors, one 64KB sector and seven 128KB sectors. For example, the STM32F429IGT6 chip has a main storage area of ​​1MB, so it only contains sector 0-sector 11 in the table.
    Like other FLASH, before writing data, it must be erased by sector, and sometimes we want to manipulate the storage unit in a small size, so STM32 also provides a double-block storage format for 1MB FLASH products , see the table below:
   insert image description here
insert image description here
    By configuring the DB1M bit of the FLASH option control register FLASH_OPTCR, the two formats can be switched. After switching to the dual-block mode, the space in sectors 8-11 is transferred to sectors divided, the total capacity remains the same.
    
    Note that if you are using the STM32F40x series chip, it does not have a dual-block storage format, and there is no sector 12-23. Only the STM32F42x/43x series products support sectors 12-23.
    
  (2) System storage area
    The system storage area is an area that cannot be accessed by users. It has solidified the startup code when the chip leaves the factory. It is responsible for realizing the ISP programming functions such as serial ports, USB, and CAN.
    
  (3) OTP area
    OTP (One Time Program), refers to a storage area that can only be written once, with a capacity of 512 bytes. After writing, the data cannot be changed. OTP is often used to store the encryption key of the application.
    
  (4) option byte
    The option byte is used to configure FLASH read and write protection, BOR level in power management, software/hardware watchdog and other functions. This part has a total of 32 bytes. It can be modified by modifying the option control register of FLASH.
    
3. The process of writing to the internal FLASH
  3.1. Unlocking
  Since the internal FLASH space mainly stores application programs, which are very critical data, these contents have been modified to prevent misuse. After the chip is reset, the FLASH will be locked by default. At this time It is not allowed to set the control register of FLASH, and the content in FLASH cannot be modified.
  Therefore, before writing data to FLASH, it needs to be unlocked first. The unlocking steps are as follows:
    (1) Write KEY1 = 0x45670123 into the Flash key register FLASH_KEYR
    (2) Then write KEY2 = 0xCDEF89AB into the Flash key register FLASH_KEYR

3.2. Number of data operation bits
  When performing erase and write operations on the internal FLASH, the power supply voltage will affect the maximum number of data operation bits. The power supply voltage can be changed by configuring the PSIZE bit in the FLASH_CR register, as shown in the table below.
  insert image description here
   The maximum number of operating bits will affect the speed of erasing and writing. In addition to the configuration register bits, the 64-bit wide operation needs to add an 8-9V voltage source to the Vpp pin, and the power supply time should not exceed one hour. Otherwise, the FLASH may be damaged, so the operation of 64-bit width is generally used when writing applications to FLASH during mass production, and most applications use 32-bit width.

3.3. To erase a sector,
  before writing new data, the storage area needs to be erased first. STM32 provides a sector erase instruction and an entire FLASH erase (batch erase) instruction. The batch erase instruction is only for main storage district.

The process of sector erasing is as follows:
    (1) Check the “busy register bit BSY” in the FLASH_SR register to confirm that no Flash operation is currently being performed; (
    2) In the FLASH_CR register, set the “activate sector erase register bit SER "Set 1, and set the "sector number register bit SNB" to select the sector to be erased;
    (3) Set the "start erase register bit STRT" in the FLASH_CR register to 1, and start erasing;
    (4) Wait When the BSY bit is cleared, the erase is complete.

3.4. Write data
  You can write data after erasing. The process of writing data is not just using pointers to assign values ​​to addresses. Before assigning values, you need to configure a series of registers. The steps are as follows: (1) Check the FLASH_SR
    in BSY bit to confirm that no other internal Flash operations are currently being performed;
    (2) Set the "Activate Programming Register Bit PG" in the FLASH_CR register to 1;
    (3) Target the desired memory address (in the main memory block or OTP area) Execute data writing operation;
    (4) Waiting for the BSY bit to be cleared, it means the writing is completed.

Guess you like

Origin blog.csdn.net/qizhi321123/article/details/127082926