#include "stm32f1xx_hal.h"
// 初始化Flash并擦除指定页
void Flash_Erase(uint32_t Page_Address)
{
FLASH_EraseInitTypeDef EraseInitStruct;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = Page_Address;
EraseInitStruct.NbPages = 1;
if(HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
// Handle error
}
HAL_FLASH_Lock();
}
// 写入数据到Flash
void Flash_Write(uint32_t Address, uint16_t Data)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, Data) != HAL_OK)
{
// Handle error
}
HAL_FLASH_Lock();
}
// 读取数据
uint16_t Flash_Read(uint32_t Address)
{
return *(volatile uint16_t*)Address;
} |