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.

80 lines
2.5 KiB

#include <stdio.h>
#include <string.h>
#include "flash_map.hpp"
#include "main.h"
#include "project_configs.h"
static uint32_t m_sector_num = 0;
namespace iflytop {
uint32_t* zsimple_flash_get_add(int sector_num) {
switch (sector_num) {
case 0:
return (uint32_t*)Z_FLASH_SECTOR_00_ADDR;
case 1:
return (uint32_t*)Z_FLASH_SECTOR_01_ADDR;
case 2:
return (uint32_t*)Z_FLASH_SECTOR_02_ADDR;
case 3:
return (uint32_t*)Z_FLASH_SECTOR_03_ADDR;
case 4:
return (uint32_t*)Z_FLASH_SECTOR_04_ADDR;
case 5:
return (uint32_t*)Z_FLASH_SECTOR_05_ADDR;
case 6:
return (uint32_t*)Z_FLASH_SECTOR_06_ADDR;
case 7:
return (uint32_t*)Z_FLASH_SECTOR_07_ADDR;
case 8:
return (uint32_t*)Z_FLASH_SECTOR_08_ADDR;
case 9:
return (uint32_t*)Z_FLASH_SECTOR_09_ADDR;
case 10:
return (uint32_t*)Z_FLASH_SECTOR_10_ADDR;
case 11:
return (uint32_t*)Z_FLASH_SECTOR_11_ADDR;
default:
assert_param(0);
}
return 0;
}
static HAL_StatusTypeDef pri_earse_sector() {
HAL_StatusTypeDef status;
uint32_t sector_error_point;
FLASH_EraseInitTypeDef flash_erase_structer = {
//
.TypeErase = FLASH_TYPEERASE_SECTORS, //
.Sector = m_sector_num, //
.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, &sector_error_point);
HAL_FLASH_Lock(); // 上锁
return status;
}
void zsimple_flash_init(uint32_t sector_num) { m_sector_num = sector_num; }
void zsimple_flash_read(uint8_t* data, size_t len) { memcpy(data, zsimple_flash_get_add(m_sector_num), len); }
void zsimple_flash_write(const uint8_t* data, size_t len) {
pri_earse_sector();
uint32_t* from = (uint32_t*)data;
uint32_t* to = (uint32_t*)zsimple_flash_get_add(m_sector_num);
uint32_t writesize = len / 4;
//HAL_StatusTypeDef status;
HAL_FLASH_Unlock(); // 解锁
for (size_t i = 0; i < writesize; i++) {
//status =
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)to, *from);
from++;
to++;
}
HAL_FLASH_Lock(); // 上锁
}
} // namespace iflytop