1: To program the STM32 internal FLASH, you need to follow the following process:
1. Flash unlock
2. Clear related flags
3. Erase FLASH (the reason for erasing first and then writing is for the convenience of industrial production, that is, the convenience of physical implementation)
4. Write to FLASH
5. Lock FLASH
(1) Get status: FLASH_Status FLASH_GetStatus(void);
The return value is defined by the enumeration type.
typedef enum
{
FLASH_BUSY = 1, //忙
FLASH_ERROR_PG, //Programming error
FLASH_ERROR_WRP, //Write protection error
FLASH_COMPLETE, // Operation completed
FLASH_TIMEOUT // Operation timeout
}FLASH_Status;
(2) FLASH_Unlock(); //Unlock function (must be unlocked before writing to the flash)
(3) FLASH_ClearFlag(); // Clear all existing flags
(4) There are three erase functions:
FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
FLASH_Status FLASH_EraseAllPages(void);
FLASH_Status FLASH_EraseOptionBytes(void);
(5) Write function:
FLASHStatus FLASH_ProgramWord(uint32_t Address, uint32_t Data); //32-bit word write function
FLASHStatus FLASH_ProgramHalfWord(uint32_t Address,uint32_t Data); //16-bit half-word write
FLASHStatus FLASH_ProgramOptionByteData(uint32_t Address,uint32_t Data); //User byte write
int write_flash(u32 StartAddr,u16 *buf,u16 len)
{
volatile FLASH_Status FLASHStatus;
u32 FlashAddr;
len=(len+1)/2;
FLASH_Unlock(); //1. Unlock function (must be unlocked before writing to the flash)
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); //2. Clear all existing flags
FlashAddr=StartAddr;
FLASH_ErasePage(StartAddr); //3. Erase a flash page
while(len--)
{
//FLASH_ProgramHalfWord is a write function, the return value is the status of the flash (5 types)
FLASHStatus = FLASH_ProgramHalfWord(FlashAddr,*buf++);
if (FLASHStatus != FLASH_COMPLETE) //FLASH_COMPLETE indicates flash completion
{
//printf("FLSH :Error %08X\n\r",FLASHStatus);
return -1;
}
FlashAddr += 2;
}
FLASH_Lock();
return 0;
}
2. FLASH reading method
*(uint32_t *)0x8000000; //read a word
*(uint8_t *)0x8000000; //read a byte;
*(uint16_t *)0x8000000; //read half word;
Example:
uint8_t data;
data = *(uint8_t *)0x8000000; // read the data at address 0x8000000 in FLASH
int read_flash(u32 StartAddr,u32 *buf,u16 len) //Read one word 4 bytes at a time
{
len=(len+3)/4;
while(len--)
{
*buf=*(__IO uint32_t *)StartAddr;
StartAddr=StartAddr+4;
buf++;
}
return 0;
}
Three: Several useful sub-functions
/*
Function: Write data to the specified address
Parameter description: addr: address of the FLASH page to be written
p: the address of the variable to be written (the array must be of type uint8_t and the number of elements must be an even number)
Byte_Num: The number of bytes written to the variable (must be an even number)
*/
void FLASH_WriteByte(uint32_t addr , uint8_t *p , uint16_t Byte_Num)
{
uint32_t HalfWord;
Byte_Num = Byte_Num/2;
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
FLASH_ErasePage(addr);
while(Byte_Num --)
{
HalfWord=*(p++);
HalfWord|=*(p++)<<8;
FLASH_ProgramHalfWord(addr, HalfWord);
addr += 2;
}
FLASH_Lock();
}
example:
uint8_t data[100];
FLASH_WriteByte(0x8000000, data, 100);/*The data in the array data is written into FLASH*/
/*
Function: Read data from the specified address
Parameter description: addr address read from FLASH
p is the address of the variable to be stored after reading (the array must be of type uint8_t)
Byte_Num The number of bytes to be read
*/
void FLASH_ReadByte(uint32_t addr , uint8_t *p , uint16_t Byte_Num)
{
while(Byte_Num--)
{
*(p++)=*((uint8_t*)addr++);
}
}
example:
uint8_t data[101];
FLASH_ReadByte(0x8000001, data, 101);/*The data in FLASH is read into the array data*/
hardware_conf.h中:
//64 flash
#define ADDRESS_START_ADDR ((u32)0x0800EC00) //59: store domain name and port number
#define TIME_START_ADDR ((u32)0x0800F000) //60: storage time
#define KEYS_START_ADDR ((u32)0x0800F400) //61: store various keys
#define MACHINEID_START_ADDR ((u32)0x0800F800) //62: store machine code
#define VERSION_START_ADDR ((u32)0x0800FC00) //63: store version number
In main():
//Get the flash content
void getFlash()
{
/*Read machine code*/
u8 machineId[19]="";
read_flash(MACHINEID_START_ADDR, (u32 *) machineId, 19);
strncpy(MachineID_Default, machineId, 18);
/*Read time*/
u8 times[20]="";
read_flash(TIME_START_ADDR, (u32 *) times, 20);
char timeKey[2]="";
strncpy(timeKey, times, 1);
/*Read keys*/
u8 out_data[220];
read_flash(KEYS_START_ADDR, (u32 *) out_data, 220);
}
//Write various keys to flash
int getAllMessage()
{
char in_data[220];
//The following are the variables that falsh needs to store
sprintf(in_data, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",isFisrt,community_code,address,swipe,code_overtime,myKey1,Private_Key,check_pw);
if(strlen(in_data)==96&&in_data[0]=='N' &&in_data[1]=='\n'&&in_data[63]=='\n'&&in_data[65]=='\n'&&in_data[69]=='\n'&&in_data[75]=='\n'&&in_data[82]=='\n'&&in_data[89]=='\n')
{
write_flash(KEYS_START_ADDR, (u16 *) in_data, 220);
ACCLOG("write keys success\n");
return 1;
}else{
ACCLOG("write keys fail...\n");
return 0;
}
}
Previous article:Detailed explanation of the meaning of the five states of FLASH_Status in STM32
Next article:STM32L0 Development Notes 13: 485 bus send and receive switching time
Recommended ReadingLatest update time:2024-11-16 12:44
- 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
- What do these two options mean?
- Multisim square wave phase shift circuit diagram
- [Digi-Key Contest] 100 electronic design kits worth up to 600 yuan will be given away for free, and DIY can also win a cash prize of 10,000 yuan!
- Introduction to the theoretical knowledge of four-axis
- An essential handbook for engineers: a practical design guide for switching power supplies
- What happened to Microsoft's underwater data center that sank to the bottom of the sea for 2 years?
- Questions about op amp TIA circuit
- Deping Technology Shenzhen Co., Ltd.
- I have never understood several parameters of MOS tubes. Please help me explain them.
- Luminous flux of LED after aging