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.
110 lines
3.1 KiB
110 lines
3.1 KiB
#include "xs_flash.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "xs_log.h"
|
|
|
|
static uint32_t* _rawstartadd;
|
|
static uint32_t* _defaultdata;
|
|
static uint32_t _rawsize;
|
|
static bool _is_first_run = false;
|
|
|
|
static bool _xs_check_raw_data() {
|
|
uint32_t checksum = 0;
|
|
ZLOGI("flash", "_rawstartadd[0] = %x", _rawstartadd[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 = FLASH_EARSE_SECTOR, //
|
|
.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 xs_flash_init(uint32_t* rawstartadd, uint32_t rawsize) {
|
|
_rawstartadd = rawstartadd;
|
|
_defaultdata = NULL;
|
|
_rawsize = rawsize;
|
|
|
|
// 读取flash数据
|
|
memcpy(_rawstartadd, (uint32_t*)(FLASH_START_ADD), _rawsize * 4);
|
|
|
|
#if 0
|
|
// 校验数据
|
|
if (_xs_check_raw_data()) {
|
|
return;
|
|
}
|
|
_is_first_run = true;
|
|
xs_flash_factory_reset();
|
|
#endif
|
|
}
|
|
|
|
bool xs_flash_check(void) { return _xs_check_raw_data(); }
|
|
|
|
bool xs_flash_set_default_data(uint32_t* defaultdata) {
|
|
_defaultdata = defaultdata;
|
|
return true;
|
|
}
|
|
|
|
bool xs_flash_is_first_run(void) { return _is_first_run; }
|
|
|
|
uint32_t xs_compute_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 xs_flash_factory_reset(void) {
|
|
memcpy(_rawstartadd, _defaultdata, _rawsize * 4);
|
|
xs_flash_flush();
|
|
return true;
|
|
}
|
|
|
|
bool xs_flash_flush(void) {
|
|
#if PC_NVS_ENABLE
|
|
_rawstartadd[0] = FLASH_MASK_VAL;
|
|
_rawstartadd[_rawsize - 1] = xs_compute_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, FLASH_START_ADD + i * 4, _rawstartadd[i]);
|
|
if (status != HAL_OK) {
|
|
ZLOGE("flash", "write error");
|
|
}
|
|
}
|
|
HAL_FLASH_Lock(); // 上锁
|
|
#endif
|
|
return true;
|
|
}
|