When we develop applications, we often need to save some program running parameters, such as some correction coefficients. The characteristics of these data are: small in number and do not need to be modified frequently, but they cannot be defined as constants, because each device may be different and may be modified in the future. It is a convenient way to store this type of data in a specified location, directly modify the value of the storage location when it needs to be modified, and directly read it when it needs to be used. Considering that the amount of data is relatively small, it is neither economical nor necessary to use a dedicated storage unit. The internal Flash capacity of STM32F103 is large, and ST's library function also provides basic Flash operation functions, which are also convenient to implement.
Take the large-capacity product STM32F103VE as an example. Its Flash capacity reaches 512K, and part of it can be used for data storage. The following is the organization mode of large-capacity Flash:
According to the above Flash organization model, we can make corresponding definitions according to our own convenience. Because each sector of large capacity is defined as 2K, and each of small capacity and medium capacity is defined as 1K, we make the following macro definition:
#define FLASH_SIZE 512 //FLASH capacity of the selected MCU (in K)
#if FLASH_SIZE<256
#define SECTOR_SIZE 1024 // bytes
#else
#define SECTOR_SIZE 2048 // bytes
#endif
Although ST's library functions are relatively comprehensive, they are all basic operations. For ease of use, we encapsulate them again according to our own needs.
Read operations are relatively simple, and the built-in flash memory module can be directly addressed in the general address space, just like reading a variable.
//Read multiple data from the specified address
void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)
{
uint16_t dataIndex;
for(dataIndex=0;dataIndex { readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2); } } //Read the half word (16-bit data) of the specified address uint16_t FLASH_ReadHalfWord(uint32_t address) { return *(__IO uint16_t*)address; } //Read the full word (32-bit data) of the specified address uint32_t FLASH_ReadWord(uint32_t address) { uint32_t temp1,temp2; temp1=*(__IO uint16_t*)address; temp2=*(__IO uint16_t*)(address+2); return (temp2<<16)+temp1; } The write operation is relatively more complicated, including writing and erasing user data. There is also a write protection lock to prevent misoperation. However, these basic operations have been written for us in the ST library functions, and we only need to call them. After STM32 is reset, the FPEC module is protected. Only after the write protection is released can we operate the related registers. The programming of STM32 flash memory must be written 16 bits at a time. Any operation that is not a half word will cause an error. The following figure shows the Flash writing process: When programming the STM32 FLASH, the FLASH at the write address must be erased (that is, its value must be 0XFFFF), otherwise it cannot be written. Flash erasure requires that the entire page must be erased, so the entire page must also be written, otherwise data may be lost. The following figure shows the Flash page erase process: The following is the Flash full erase process According to the above diagram, we write the data writing function as follows: //Write multiple data starting from the specified address void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite) { if(startAddress { return; //Illegal address } FLASH_Unlock(); //Unlock write protection uint32_t offsetAddress=startAddress-FLASH_BASE; //Calculate the actual offset address after removing 0X08000000 uint32_t sectorPosition=offsetAddress/SECTOR_SIZE; //Calculate the sector address, which is 0~255 for STM32F103VET6 uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE; //The first address of the corresponding sector FLASH_ErasePage(sectorStartAddress); //Erase this sector uint16_t dataIndex; for(dataIndex=0;dataIndex { FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]); } FLASH_Lock(); //Lock write protection } Before erasing, the data on the page should be read out and merged with the data to be written, and then written after erasing. However, the amount of data is large (the large capacity is 2K per sector), so considering that a small amount of data is stored, all the data is written at the same time each time to simplify the operation and reduce the amount of data processing. After testing, the above program writes and reads data correctly, and the internal Flash read and write operations can be realized. For more in-depth understanding, please refer to the "STM32F10xxx Flash Programming Reference Manual".
Previous article:STM32F103 series PB4 pin problem
Next article:STM32 Flash read and write; storage content and method corresponding to Flash address
Recommended ReadingLatest update time:2024-11-16 11:37
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- DSP generates bin file method
- Leez AI LAN Object Detection
- Why does Ka-band need more bandwidth?
- Mid-Autumn Festival has passed, come back to move bricks
- How to compile DSP assembly or linear assembly program in Linux environment?
- [Rewarded Live Broadcast] Recommend "TI mmWave Packaged Antenna Sensor IWR6843" to achieve building industrial sensing simply and efficiently
- FAQ: Microchip Security Seminar | Trust Your Firmware: Secure Boot Application Processors
- What do you think about private 5G networks?
- Theoretical speed calculation of WiFi protocols
- MicroPython Hands-on (32) - MQTT for the Internet of Things