You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
#include "zflash.h"
|
|
|
|
HAL_StatusTypeDef FlashErase(void)
|
|
{
|
|
HAL_StatusTypeDef status;
|
|
uint32_t sector_error_point;
|
|
FLASH_EraseInitTypeDef flash_erase_structer = {
|
|
//
|
|
.TypeErase = FLASH_TYPEERASE_SECTORS, //
|
|
.Sector = FLASH_SECTOR_7, //
|
|
.NbSectors = 1, //
|
|
.VoltageRange = FLASH_VOLTAGE_RANGE_3 //
|
|
};
|
|
|
|
HAL_FLASH_Unlock(); // 解锁
|
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
|
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); // 清除一些错误标志
|
|
|
|
status = HAL_FLASHEx_Erase(&flash_erase_structer, §or_error_point);
|
|
HAL_FLASH_Lock(); // 上锁
|
|
return status;
|
|
}
|
|
|
|
// 写数据
|
|
HAL_StatusTypeDef FlashWrite(uint32_t StartAddress, uint32_t data)
|
|
{
|
|
HAL_StatusTypeDef status;
|
|
// 可以添加一些 StartAddress地址 是否有效的判断
|
|
HAL_FLASH_Unlock(); // 解锁
|
|
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartAddress, data);
|
|
HAL_FLASH_Lock(); // 上锁
|
|
return status;
|
|
}
|
|
|
|
// 读数据
|
|
uint32_t FlashRead(uint32_t StartAddress)
|
|
{
|
|
return *(uint32_t *)StartAddress;
|
|
}
|