Based on stm32 single-chip microcomputer, FLASH function analysis case of erasing first and then writing

FLASh must be erased before writing

The following function is an analysis case

Based on stm32 single-chip microcomputer, FLASH function analysis case of erasing first and then writing

void FLASH_WriteByte (u32 addr, u16 flashdata1)

{

FLASH_Status FLASHstatus = FLASH_COMPLETE;

FLASH_Unlock();//Unlock FLASH programming and erasing controller

// FLASH_ClearFlag (FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);//Clear the flag bit

FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);

/

FLASHstatus=FLASH_ErasePage(addr);//Erase the specified address page

FLASHstatus=FLASH_ProgramHalfWord(addr, flashdata1);//Start writing from the addr address of the specified page

//FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);

FLASH_Lock();//Lock FLASH programming and erasing control

}

Basic sequence: Unlock-"Clear the flag bit (can be omitted)-"Erase-"Write half-word-"Clear the flag bit (can also be omitted)-"Lock.

If FLASH_START_ADDR is the macro defined 0x8000000+2048*255

1. 0x8000000 is the starting address of Flash

2. 2048 is because I am using a large-capacity chip. According to the Flash address of the previous note, it can be seen that the chip has a capacity of 2K per page, which is 2048 bytes.

3. 255 represents the last page of the chip, this depends on different chips. The reason why writing from the following page can prevent the stored data from destroying the user program.

4. addr*2 is because each data occupies 2 bytes (half word), although 1 byte of data is written, but the programming is in 2 bytes.

In other words, one byte of data will also occupy two byte addresses.

This sub-function is to write the data flashdata1 to the address addr. The length of the data is variable.

When you need to read in data, you can directly access the address,

Such as: rdata=*(u16 *)0x08014000; //Read the default data in flash

0x08014000 is the address of the storage.

BAM Power Transmission Capacitors

BAM power transmission capacitors

RAM/RFM Induction Heating Capacitors,Water Pump Capacitor,Water Cooled Condense,Tank Capacitor

YANGZHOU POSITIONING TECH CO., LTD. , https://www.pst-thyristor.com