130 lines
3.3 KiB
C
130 lines
3.3 KiB
C
#include "flash_config.h"
|
|
#include <string.h>
|
|
|
|
// 计算校验和
|
|
static uint32_t Calculate_Checksum(const uint8_t *data, uint32_t len)
|
|
{
|
|
uint32_t sum = 0;
|
|
for(uint32_t i = 0; i < len; i++) {
|
|
sum += data[i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
// 擦除配置页
|
|
static HAL_StatusTypeDef Flash_Config_Erase(void)
|
|
{
|
|
FLASH_EraseInitTypeDef EraseInitStruct;
|
|
uint32_t PAGEError;
|
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
|
|
EraseInitStruct.PageAddress = FLASH_CONFIG_PAGE_ADDR;
|
|
EraseInitStruct.NbPages = 1;
|
|
|
|
if(HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
|
|
{
|
|
HAL_FLASH_Lock();
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
HAL_FLASH_Lock();
|
|
return HAL_OK;
|
|
}
|
|
|
|
// 保存配置到Flash
|
|
HAL_StatusTypeDef Flash_Config_Save(FlashConfig_t *config)
|
|
{
|
|
HAL_StatusTypeDef status;
|
|
uint32_t *source = (uint32_t*)config;
|
|
uint32_t address = FLASH_CONFIG_PAGE_ADDR;
|
|
uint32_t words = (sizeof(FlashConfig_t) + 3) / 4;
|
|
|
|
// 设置魔数和计算校验和
|
|
config->magic = FLASH_CONFIG_MAGIC;
|
|
config->checksum = Calculate_Checksum((uint8_t*)config,
|
|
sizeof(FlashConfig_t) - sizeof(uint32_t));
|
|
|
|
// 擦除页
|
|
if((status = Flash_Config_Erase()) != HAL_OK)
|
|
return status;
|
|
|
|
// 写入数据
|
|
HAL_FLASH_Unlock();
|
|
|
|
for(uint32_t i = 0; i < words; i++)
|
|
{
|
|
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,
|
|
address + (i * 4),
|
|
source[i]) != HAL_OK)
|
|
{
|
|
HAL_FLASH_Lock();
|
|
return HAL_ERROR;
|
|
}
|
|
}
|
|
|
|
HAL_FLASH_Lock();
|
|
return HAL_OK;
|
|
}
|
|
|
|
// 从Flash加载配置
|
|
HAL_StatusTypeDef Flash_Config_Load(FlashConfig_t *config)
|
|
{
|
|
FlashConfig_t *flash_config = (FlashConfig_t*)FLASH_CONFIG_PAGE_ADDR;
|
|
|
|
// 检查魔数
|
|
if(flash_config->magic != FLASH_CONFIG_MAGIC)
|
|
{
|
|
Flash_Config_SetDefault(config);
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
// 复制数据
|
|
memcpy(config, flash_config, sizeof(FlashConfig_t));
|
|
|
|
// 验证校验和
|
|
uint32_t checksum = Calculate_Checksum((uint8_t*)config,
|
|
sizeof(FlashConfig_t) - sizeof(uint32_t));
|
|
if(checksum != config->checksum)
|
|
{
|
|
Flash_Config_SetDefault(config);
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
// 设置默认配置
|
|
void Flash_Config_SetDefault(FlashConfig_t *config)
|
|
{
|
|
// 设置默认网络配置
|
|
config->net_config.mac[0] = 0x00;
|
|
config->net_config.mac[1] = 0x08;
|
|
config->net_config.mac[2] = 0xDC;
|
|
config->net_config.mac[3] = 0x12;
|
|
config->net_config.mac[4] = 0x34;
|
|
config->net_config.mac[5] = 0x56;
|
|
|
|
config->net_config.ip[0] = 169;
|
|
config->net_config.ip[1] = 254;
|
|
config->net_config.ip[2] = 1;
|
|
config->net_config.ip[3] = 11;
|
|
|
|
config->net_config.sn[0] = 255;
|
|
config->net_config.sn[1] = 255;
|
|
config->net_config.sn[2] = 255;
|
|
config->net_config.sn[3] = 0;
|
|
|
|
config->net_config.gw[0] = 169;
|
|
config->net_config.gw[1] = 254;
|
|
config->net_config.gw[2] = 1;
|
|
config->net_config.gw[3] = 1;
|
|
|
|
config->net_config.dns[0] = 8;
|
|
config->net_config.dns[1] = 8;
|
|
config->net_config.dns[2] = 8;
|
|
config->net_config.dns[3] = 8;
|
|
|
|
config->net_config.dhcp = NETINFO_STATIC;
|
|
} |