Follow + star official account , don't miss exciting content
Transfer from | STM32
FLASH, referring to Flash Memory, is a non-volatile memory (flash memory), which can save data normally when power is off.
Now, the internal Flash of most single-chip microcomputers can be read, written and programmed, but sometimes the operation fails due to various reasons. Today, I will share with you common problems combined with STM32.
The memory of STM32 usually includes internal SRAM, internal FLASH, and some series also include EEPROM. Among them, FLASH is usually used to store code or data, which can be accessed by reading and writing.
STM32 FLASH basic content
The FLASH organizational structure of STM32 may be slightly different due to different series and models. For example, the familiar STM32F1 small and medium-capacity page size is only 1K, while the F1 large-capacity page has 2K.
For example, some series use sector as the smallest unit, some sectors have a minimum of 16K, and some range from 128K.
This article mainly describes the relevant content about FLASH in conjunction with the F4 series.
1. Flash structure
Usually Flash consists of several blocks, here is an example of F40x:
Main memory: used to store user code or data.
System memory: used to store the factory program, usually the startup program code.
OTP area: A small one-time programmable area for users to store specific data.
Option byte: store configuration information related to chip resources or attributes.
2. Flash routine operation
Flash read, write (program), erase:
128-bit wide data read
Byte, halfword, word, and doubleword data writes
Sector Erase and All Erase
(Hint: There may be differences in different series, such as byte read, page erase, etc.)
Flash read and write protection: realized by configuring option bytes.
3. Flash capacity
The Flash capacity of STM32 has been determined at the factory, and the capacity can be known according to the model.
4. Memory side format
At present, the STM32 memory organization structure defaults to the little-endian format: the low byte of the data is stored in the low address of the memory.
For more information, please refer to the corresponding reference manual of the chip.
FLASH option byte
The internal Flash of the STM32 has a read-write protection function. If you want to read and write the Flash, you must first remove the read-write protection. The read-write protection is completed by configuring the option byte.
There are two common ways to configure option bytes: 1. Software coding; 2. Programming tools;
1. Software coding
For example, the STM32F4 series standard peripheral library library provides functions:
void FLASH_OB_Unlock(void);
void FLASH_OB_Lock(void);
void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
void FLASH_OB_WRP1Config(uint32_t OB_WRP, FunctionalState NewState);
void FLASH_OB_PCROPSelectionConfig(uint8_t OB_PcROP);
void FLASH_OB_PCROPConfig(uint32_t OB_PCROP, FunctionalState NewState);
void FLASH_OB_PCROP1Config(uint32_t OB_PCROP, FunctionalState NewState);
void FLASH_OB_RDPConfig(uint8_t OB_RDP);
void FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
void FLASH_OB_BORConfig(uint8_t OB_BOR);
void FLASH_OB_BootConfig(uint8_t OB_BOOT);
FLASH_Status FLASH_OB_Launch(void);
uint8_t FLASH_OB_GetUser(void);
uint16_t FLASH_OB_GetWRP(void);
uint16_t FLASH_OB_GetWRP1(void);
uint16_t FLASH_OB_GetPCROP(void);
uint16_t FLASH_OB_GetPCROP1(void);
FlagStatus FLASH_OB_GetRDP(void);
uint8_t FLASH_OB_GetBOR(void);
Software coding can configure option bytes by calling these function interfaces.
2. Programming tools
For example, the STM32CubeProg programming tool:
Configuring the STM32 option byte can also be configured through ST-LINK Utility, STVP and other similar tools.
Tip: The STM32 option bytes of different models may be slightly different.
FLASH read and write erase operations
The STM32 internal Flash is similar to other external Flash, and supports routine operations such as reading, writing, and erasing. Operations such as unlocking and deprotection are usually required before operating the internal Flash.
for example:
FLASH_OB_Lock();
FLASH_OB_WRPConfig(OB_WRP_Sector_All, ENABLE);
FLASH_OB_PCROPConfig(OB_PCROP_Sector_All, ENABLE);
1. Read data
There are usually two ways to read internal Flash data:
Read by program (encoding)
Read by external (programming) tool
The program (encoding) reads:
uint32_t uwData32 = 0;
uint32_t uwAddress = 0x08001000;
uwData32 = *(__IO uint32_t*)uwAddress;
The external programming tool reads:
Read prerequisite: no read protection, set the read address, length, data width, etc.
2. Write data
Writing data to the STM32 internal Flash is similar to reading data, but the write data address cannot have data, that is, the data must be erased before writing.
Therefore, relative to reading data, some additional operations are usually required before writing, such as:
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
Writing data through tools is what we call downloading data during mass production. More formally, it is called programming.
3. Erase data
Erasing data is usually divided into erasing pages, sectors, and entire blocks, and the erasing time varies with different models and speeds.
Tip: It is recommended to refer to the official Demo for this part of the content (the standard peripheral library and HAL have basic routines)
FLASH Frequently Asked Questions
The main purpose of STM32 internal Flash is to store program code and data. Be cautious when operating the internal Flash, otherwise the entire program may be destroyed if improperly operated.
Problem 1: Programming (writing data) addresses are not aligned
When writing data, we need to specify the address to be written. If the write address is not aligned, a programming alignment error will occur.
for example:
Following 32-bit (4-byte) address alignment, your address can only be a multiple of 4. 0x08001000 is correct, 0x08001001 is wrong.
Tip: Different models may have different alignment widths, some are 32 bits, some are 128 bits, etc.
Solution: judge the address by "remainder".
Problem 2: The programming address data is not erased
Before writing data, you need to erase the corresponding address data to write normally, otherwise it will fail.
The data we erase is usually a page or a sector. Writing data at a certain address may affect the data at other addresses. If it is directly overwritten, there will be problems.
Solution: The usual method is to read out the entire page (or sector) data and cache it, then erase the entire page, and then write.
Question 3: Reading data when erasing
When the internal Flash of the STM32 is performing a write or erase operation, the bus is blocked, and reading the Flash data will fail at this time. 【Except double bank mode】
Solution: Use flags to determine whether the write/erase operation is complete.
Question 4: Writing fails due to unstable voltage
In an environment with large external interference, the power supply may drop suddenly. When operating the STM32 internal Flash, if it is lower than a specific voltage, programming failure will occur.
The minimum voltage for operating Flash is related to both the operating frequency and the STM32 model (see the data sheet for details).
Solution: Ensure voltage stability by improving the hardware circuit. Insufficient or unstable power supply voltage leads to hidden dangers that are often difficult to detect! !
------------ END ------------
●Column "Embedded Development"
●Selected tutorials in the embedded column
Pay attention to the official account and reply " Jiagroup " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.
Click " Read the original text " to view more sharing.