[cortexm0--stm32f0308] Flash Memory

Source: Internet
Author: User

Problem description

Flash in the IC is used to store code and const data, for which there is no use of the flash part, you can make the application used for data storage. IC general application scenarios are often power outages, and some data need to record some of the previous state and other content, so need to power off the characteristics of the retention, Flash is such a device.

STM32F0308 's Flash

64K Flash with page as a unit to divide, altogether 64 page, that each page is 1K byte, also can use sector to divide, a sector has 4 page, that 64K of Flash can divide into 16 sector.

Flash Read

Flash for the MCU is read only, so for the CPU, read Flash is very simple thing, do not need to read through the Flash interface, M0 core directly through the AHB bus directly access to the Flash address segment, read the instructions in it or read only data. Some memory segments may be execution only, this is by oneself in the design program through the control link to achieve, estimated to execution only memory reading is not successful, as to what will produce any error, you have to try to know. M0 core accesses the read only data in Flash via AHB, which is implemented through instruction fetch and latency, both managed by the FLASH_ACR (Flash access control register). The purpose of the command fetch is to increase the efficiency of the fetch by using a prefetch buffer. Latency is used to maintain the read Flash memory control signal, so that the high-speed reading flash data without error.

Flash Write (program and erase)

Flash for the CPU read only, so only through the Flash peripheral interface to write data, if the same as the read operation of the direct write address, will cause hardfalt. From the macro point of view of the program, stm32f0308 Flash programming in two ways, ICP and IAP,ICP is in the system programming, through SWD and flash download algorithm will be the entire flash programming, IAP is in the application program, by running a small app, Implement the remaining firmware updates.
Flash can not be read when programming/erasing flash.

Flash Registers
    • Key Register (Flash_keyr)
    • Option byte key Regi ster (Flash_optkeyr)
    • Flash Control Register (FLASH_CR)
    • Flash Status Register (FLASH_SR)
    • Flash address Register (FLASH_AR)
    • Option byte Register (FLASH_OBR)
    • Write Protection Register (FLASH_WRPR)
Unlock Flash

This is the first step in programming/erasing, because after the IC reset, Flash is locked and cannot be written and erase, the following unlocking steps are required:

Programming Flash

For flash programming, that is, write data to the corresponding address of flash, can only be written in Half-word (+ bit) , and the Flash programming and erasure must follow the prescribed process, otherwise it will cause flash data modification is unsuccessful, and cause the program to be abnormal. stm32f0308, it seems that before the programming flash need to erase flash for 0x00000000, otherwise it is impossible to program flash success.
The standard steps for programming flash are described below:

    1. First make sure that no flash memory related operations are in progress, so you need to check the BSY bit of the FLASH_SR register;
    2. The PG bit of the FLASH_CR register is set to 1, indicating that it is a flash programming operation;
    3. Writes the Half-word () data to the corresponding address of flash;
    4. Due to the operation written to Flash, it is necessary to wait until the BSY bit of the FLASH_SR register is set;
    5. Finally check the FLASH_SR Register's EOP flag (EOP flag, End of Procedure, a flash operation completed successfully, then the EOP flag is set), EOP flag is set, it means that the Flash programming success, You need to clear the EOP flag in the software, or it will affect the subsequent judgment.
      The detailed flowchart is as follows:
Erase Flash

Flash erase is the flash data to restore the default value of the way, the general Flash erase is the flash will be turned into 0xFFFFFFFF, and here stm32f0308 seems to have become 0x00000000, that is, the clear zero, This is according to the stm32f0308 of the description of the approximate judgment, the specific experiment to see the ~
stm32f0308 Flash Erase is divided into two cases, page Erase and mass Erase, either page erase, or the entire user Flash area is erased.

Page Erase

That is, to erase a page of flash, the standard procedure for Flash page erase is described below:
1. Make sure that no flash memory related operations are in progress, so you need to check the BSY bit of the FLASH_SR register;
2. Set the per bit of FLASH_CR register to 1, indicating that it is the page erase operation for Flash;
3. Set Flash_ar Register, select the page to be erase;
4. Set STRT bit of FLASH_CR Register to 1, start the page erase operation of Flash;
5. Due to the operation written to Flash, it is necessary to wait until the BSY bit of the FLASH_SR register is set;
5. Finally check FLASH_SR Register's EOP flag (EOP flag, End of Procedure, a flash operation completed successfully, then the EOP flag is set), EOP flag is set, this flash erase success , you need to clear the EOP flag in the software, or it will affect the subsequent judgment.
The detailed flowchart shows the following:

Mass Erase

Erase the entire user flash, and of course option byte and the system memory portion are not affected. The procedure is as follows:
1. Make sure that no flash memory related operations are in progress, so you need to check the BSY bit of the FLASH_SR register;
2. Place the MER bit of FLASH_CR Register 1, indicating that the mass erase operation of Flash;
3. Set the STRT bit of FLASH_CR Register to 1, start the mass erase operation of Flash;
4. Due to the operation written to Flash, it is necessary to wait until the BSY bit of the FLASH_SR register is set;
5. Finally check FLASH_SR Register's EOP flag (EOP flag, End of Procedure, a flash operation completed successfully, then the EOP flag is set), EOP flag is set, this flash erase success , you need to clear the EOP flag in the software, or it will affect the subsequent judgment.
Detailed flowchart to keep up with:

Option Byte writes

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[cortexm0--stm32f0308] Flash Memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.