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

  1. #include "zflash.h"
  2. HAL_StatusTypeDef FlashErase(void)
  3. {
  4. HAL_StatusTypeDef status;
  5. uint32_t sector_error_point;
  6. FLASH_EraseInitTypeDef flash_erase_structer = {
  7. //
  8. .TypeErase = FLASH_TYPEERASE_SECTORS, //
  9. .Sector = FLASH_SECTOR_7, //
  10. .NbSectors = 1, //
  11. .VoltageRange = FLASH_VOLTAGE_RANGE_3 //
  12. };
  13. HAL_FLASH_Unlock(); // 解锁
  14. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  15. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); // 清除一些错误标志
  16. status = HAL_FLASHEx_Erase(&flash_erase_structer, &sector_error_point);
  17. HAL_FLASH_Lock(); // 上锁
  18. return status;
  19. }
  20. // 写数据
  21. HAL_StatusTypeDef FlashWrite(uint32_t StartAddress, uint32_t data)
  22. {
  23. HAL_StatusTypeDef status;
  24. // 可以添加一些 StartAddress地址 是否有效的判断
  25. HAL_FLASH_Unlock(); // 解锁
  26. status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, StartAddress, data);
  27. HAL_FLASH_Lock(); // 上锁
  28. return status;
  29. }
  30. // 读数据
  31. uint32_t FlashRead(uint32_t StartAddress)
  32. {
  33. return *(uint32_t *)StartAddress;
  34. }