STM32 Flash Learning (2)

The official firmware library of STM32F1 operates several common functions of FLASH. These functions and definitions are distributed in the source file stm32f1xx_hal_flash.c/stm32f1xx_hal_flash_ex.c and the header file stm32f1xx_hal_flash.h/stm32f1xx_hal_flash_ex.h.

lock solution function

Before writing to FLASH, it must be unlocked first. Unlock operation: write a specific sequence (KEY1 and KEY2) in FLASH_KEYR. The implementation of the HAL library is very simple:

HAL_StatusTypeDef HAL_FLASH_Unlock(void);

After the write operation is completed, to lock the FLASH:

HAL_StatusTypeDef HAL_FLASH_lock(void);

write function

The HAL library provides a general FLASH write operation function HAL_FLASH_Program, which is declared as follows:

HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram,uint32_t Address,uint64_t Data);

This function has three entry parameters.

  • TypeProgram: distinguish the type of data to be written, the values ​​are FLASH_TYPEPROGRAM_BYTE (byte: 8 bits), FLASH_TYPEPROGRAM_HALFWORD (half word: 16 bits), FLASH_TYPEPROGRAM_WORD (word: 32 bits) and FLASH_TYPEPROGRAM_DOUBLEWORD (double word: 64 bits).
  • Address sets the FLASH address to write data.
  • Data, the data type to be written, the parameter is 64-bit by default, if it is less than 64-bit, type conversion will be performed.

erase function

The erase function provided by the HAL library is defined in stm32f1xx_hal_flash_ex.c. Like the programming function, HAL provides a general cell erase-based function HAL_FLASHEx_Erase, which is declared as follows:

HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError);

typedef struct
{
    
    
 uint32_t TypeErase; //擦除类型
 uint32_t Banks; //擦除的 Bank 编号
 uint32_t PageAddress; //擦除页面地址
 uint32_t NbPages; //擦除的页面数
} FLASH_EraseInitTypeDef;
  • TypeErase sets the erasure type, whether it is Page erasure or BANK-level batch erasure, and the value is FLASH_TYPEERASE_PAGES or FLASH_TYPEERASE_MASSERASE. If you want to erase all Pages under a Bank at a time, you need to select FLASH_TYPEERASE_MASSERASE.
  • Banks sets the Bank number to be erased, and it is only valid when it is set to batch erase.
  • PageAddress The address of the page to erase.
  • NbPages Number of pages to erase.

Wait for operation to complete function

When performing a flash write operation, any read operation to the flash memory will lock the bus, and the read operation can only be performed correctly after the write operation is completed; that is, the
code or data cannot be read during the write or erase operation.
Before each operation, it must wait for the previous operation to complete before it can start.

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);

This function is used in many places in the HAL library. For example, the erase function HAL_FLASHEx_Erase will call this function after erasing the FLASH and wait for the erase operation to complete.

Read FLASH specific address data function

The function firmware library is not given, and it provides to read half a byte from the specified address:

u16 STMFLASH_ReadHalfWord(u32 faddr)
{
    
    
	return *(vu16 *)faddr;
}

Guess you like

Origin blog.csdn.net/Caramel_biscuit/article/details/131932687