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.
116 lines
3.2 KiB
116 lines
3.2 KiB
|
|
#include "zflash.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "zbase.h"
|
|
#include "zlog.h"
|
|
|
|
static uint32_t* _rawstartadd;
|
|
static uint32_t* _defaultdata;
|
|
static uint32_t _rawsize;
|
|
static bool _is_first_run = false;
|
|
|
|
static uint32_t* _flashadd;
|
|
static uint32_t _flashSector;
|
|
|
|
static bool _xs_check_raw_data() {
|
|
uint32_t checksum = 0;
|
|
if (_rawstartadd[0] != FLASH_MASK_VAL) {
|
|
return false;
|
|
}
|
|
for (uint32_t i = 0; i < _rawsize - 1; i++) {
|
|
checksum += _rawstartadd[i];
|
|
}
|
|
if (checksum != _rawstartadd[_rawsize - 1]) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static HAL_StatusTypeDef _flash_erase(void) {
|
|
HAL_StatusTypeDef status;
|
|
uint32_t sector_error_point;
|
|
FLASH_EraseInitTypeDef flash_erase_structer = {
|
|
//
|
|
.TypeErase = FLASH_TYPEERASE_SECTORS, //
|
|
.Sector = _flashSector, //
|
|
.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);
|
|
if (status != HAL_OK) {
|
|
ZLOGE("flash", "erase error");
|
|
}
|
|
HAL_FLASH_Lock(); //
|
|
return status;
|
|
}
|
|
/*******************************************************************************
|
|
* EXTERN *
|
|
*******************************************************************************/
|
|
|
|
void zflash_init(uint32_t* flashadd, uint32_t flashSector, uint32_t* rawstartadd, uint32_t rawsize) {
|
|
_flashadd = flashadd;
|
|
_flashSector = flashSector;
|
|
|
|
_rawstartadd = rawstartadd;
|
|
_defaultdata = NULL;
|
|
_rawsize = rawsize;
|
|
|
|
// 读取flash数据
|
|
memcpy(_rawstartadd, (uint32_t*)(_flashadd), _rawsize * 4);
|
|
|
|
#if 0
|
|
// 校验数据
|
|
if (_xs_check_raw_data()) {
|
|
return;
|
|
}
|
|
_is_first_run = true;
|
|
zflash_factory_reset();
|
|
#endif
|
|
}
|
|
|
|
bool zflash_check(void) { return _xs_check_raw_data(); }
|
|
|
|
bool zflash_set_default_data(uint32_t* defaultdata) {
|
|
_defaultdata = defaultdata;
|
|
return true;
|
|
}
|
|
|
|
bool zflash_is_first_run(void) { return _is_first_run; }
|
|
|
|
uint32_t zcompute_checksum(uint32_t* data, uint32_t size) {
|
|
uint32_t checksum = 0;
|
|
for (uint32_t i = 0; i < size; i++) {
|
|
checksum += data[i];
|
|
}
|
|
return checksum;
|
|
}
|
|
bool zflash_factory_reset(void) {
|
|
ZLOGI("flash", "factory reset");
|
|
memcpy(_rawstartadd, _defaultdata, _rawsize * 4);
|
|
zflash_flush();
|
|
return true;
|
|
}
|
|
|
|
bool zflash_flush(void) {
|
|
_rawstartadd[0] = FLASH_MASK_VAL;
|
|
_rawstartadd[_rawsize - 1] = zcompute_checksum(_rawstartadd, _rawsize - 1);
|
|
|
|
_flash_erase();
|
|
HAL_FLASH_Unlock(); //
|
|
HAL_StatusTypeDef status;
|
|
for (uint32_t i = 0; i < _rawsize; i++) {
|
|
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)_flashadd + i * 4, _rawstartadd[i]);
|
|
if (status != HAL_OK) {
|
|
ZLOGE("flash", "write error");
|
|
}
|
|
}
|
|
HAL_FLASH_Lock(); //
|
|
return true;
|
|
}
|