diff --git a/usersrc/zflash.c b/usersrc/zflash.c index a0708e0..c3b4cd9 100644 --- a/usersrc/zflash.c +++ b/usersrc/zflash.c @@ -1 +1,37 @@ -#include "zflash.h" \ No newline at end of file +#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; +} + +// 写数据 +void FlashWrite(uint32_t StartAddress, uint32_t data) +{ + // 可以添加一些 StartAddress地址 是否有效的判断 + HAL_FLASH_Unlock(); // 解锁 + HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartAddress, data); + HAL_FLASH_Lock(); // 上锁 +} + +// 读数据 +uint32_t FlashRead(uint32_t StartAddress) +{ + return *(uint32_t *)StartAddress; +} \ No newline at end of file diff --git a/usersrc/zflash.h b/usersrc/zflash.h index 7b9637e..cbdf29c 100644 --- a/usersrc/zflash.h +++ b/usersrc/zflash.h @@ -1 +1,8 @@ -#pragma once \ No newline at end of file +#pragma once +#include "main.h" + +#define FLASH_START_ADDRESS 0x08078000 + +HAL_StatusTypeDef FlashErase(void); +void FlashWrite(uint32_t StartAddress, uint32_t data); +uint32_t FlashRead(uint32_t StartAddress);