配置文件分区

This commit is contained in:
95384 2024-11-27 18:05:09 +08:00
parent 4c91d4bf82
commit 4bf74fa144
5 changed files with 153 additions and 67 deletions

View File

@ -7,9 +7,16 @@
/* page 511,one page 2k, 0x800F800-0x0800FFFF*/
#define STM32_INFLASH_PAGE_SIZE 2048
#define STM32_INFLASH_BASE 0x8000000 /* STM32 FLASH内部起始地址 */
#define STM32_INFLASH_SAVE_ADDR_BEGIN (STM32_INFLASH_BASE+STM32_INFLASH_PAGE_SIZE*511)
#define STM32_INFLASH_SAVE_ADDR_END (STM32_INFLASH_BASE+STM32_INFLASH_PAGE_SIZE*512-1)
#define STM32_INFLASH_SAVE_PAGE 511
//用户配置存储位置
#define STM32_INFLASH_SAVE_PAGE_1 511
#define STM32_INFLASH_SAVE_ADDR_1_BEGIN (STM32_INFLASH_BASE+STM32_INFLASH_PAGE_SIZE*STM32_INFLASH_SAVE_PAGE_1)
#define STM32_INFLASH_SAVE_ADDR_1_END (STM32_INFLASH_BASE+STM32_INFLASH_PAGE_SIZE*(STM32_INFLASH_SAVE_PAGE_1+1)-1)
//出厂配置存储位置
#define STM32_INFLASH_SAVE_PAGE_2 510
#define STM32_INFLASH_SAVE_ADDR_2_BEGIN (STM32_INFLASH_BASE+STM32_INFLASH_PAGE_SIZE*STM32_INFLASH_SAVE_PAGE_2)
#define STM32_INFLASH_SAVE_ADDR_2_END (STM32_INFLASH_BASE+STM32_INFLASH_PAGE_SIZE*(STM32_INFLASH_SAVE_PAGE_2+1)-1)
typedef enum{
FLAG_SAVE_INFLASH_HEAD = 0x55,
@ -17,12 +24,19 @@ typedef enum{
}FlagInFlashType_e;
#pragma pack(push,1)
typedef struct _config_info{
//用户可更改参数
typedef struct _usr_config_info{
u_int8_t flag_head;
u_int8_t addr; /* 设备地址 */
u_int16_t uart_baud; /* 串口波特率 */
u_int16_t speed_average_time; /* 风速平均时间 */
u_int16_t temp_hum_update_time; /* 温湿度平均时间 */
u_int8_t flag_end;
}usr_config_info;
//出厂配置参数
typedef struct _factory_config_info{
u_int8_t flag_head;
u_int16_t transducer_cfg_1R5; /* 换能器参数 */
u_int16_t transducer_cfg_4R5; /* 换能器参数 */
u_int16_t transducer_cfg_8R0; /* 换能器参数 */
@ -40,14 +54,18 @@ typedef struct _config_info{
u_int16_t linear_point_5_y; /* 线性插值点5Y */
u_int16_t linear_enable; /* 线性插值使能 */
u_int8_t flag_end;
}config_info;
}factory_config_info;
#pragma pack(pop)
extern config_info g_stConfigInfo;
extern usr_config_info g_usrConfigInfo;
extern factory_config_info g_stConfigInfo;
void InFlash_Erase_Pages(u_int16_t PageBeginNum, u_int16_t NbPages);
void InFlash_Write(u_int32_t addr, u_int64_t *pBuffer,u_int32_t NumToWrite);
void save_config_info(config_info save_config_info);
void delete_config_info();
void save_usr_config_info(usr_config_info save_config_info);
void save_factory_config_info(factory_config_info save_config_info);
BOOL read_usr_config_info();
BOOL read_factory_config_info();
BOOL read_config_info();
void delete_config_info();
#endif //_INFLASH_H_

View File

@ -627,7 +627,7 @@ void update_mcs_param(float new_wind_speed, float new_wind_dirction)
}
time_111s++;
if(time_111s==g_stConfigInfo.speed_average_time*60){
if(time_111s==g_usrConfigInfo.speed_average_time*60){
time_111s=0;
flag_time_10min = 1;
}
@ -795,12 +795,12 @@ void my_update_mcs_param(float new_wind_speed, float new_wind_dirction)
win_10min.speed_data[win_10min.index] = new_wind_speed; //添加新数据
win_10min.direction_data[win_10min.index] = new_wind_dirction;
if(win_10min.count < g_stConfigInfo.speed_average_time /*AVE_TIME*/)
if(win_10min.count < g_usrConfigInfo.speed_average_time /*AVE_TIME*/)
{
win_10min.count++;
}
if(win_10min.count > g_stConfigInfo.speed_average_time/*AVE_TIME*/){win_10min.count = /*AVE_TIME*/g_stConfigInfo.speed_average_time;}
if(win_10min.count > g_usrConfigInfo.speed_average_time/*AVE_TIME*/){win_10min.count = /*AVE_TIME*/g_usrConfigInfo.speed_average_time;}
//计算10min风速滑动平均值
win_10min.ave_speed_data[win_10min.index] = sum(win_10min.speed_data, win_10min.count) / win_10min.count;
//计算10min风向滑动平均值风向滑动平均值需要过零算法
@ -875,7 +875,7 @@ void my_update_mcs_param(float new_wind_speed, float new_wind_dirction)
g_stMcs_Para.trough_wind_speed = temp_trough_min_speed;
g_stMcs_Para.peak_wind_speed = temp_peak_max_speed;
win_10min.index = (win_10min.index + 1) % /*AVE_TIME*/g_stConfigInfo.speed_average_time; //更新索引
win_10min.index = (win_10min.index + 1) % /*AVE_TIME*/g_usrConfigInfo.speed_average_time; //更新索引
}
void tem_hum_update_task(void const * argument)
@ -884,14 +884,31 @@ void tem_hum_update_task(void const * argument)
uint32_t time_s_1Day = 0;
float backupTemperature;
get_temp_humi_data(&g_stMcs_Para.temperature, &g_stMcs_Para.humidity);//开机先采集一次
// 开机先采集一次大气压温湿度
{
// 采集HP203B传感器数据大气压
if(get_HP203_data(&backupTemperature, &g_stMcs_Para.pressure) == FALSE)
{
g_error_log.temp_error_HP203B = 1;
/// 错误处理
}
// 采集SHT30传感器数据温湿度
if(get_temp_humi_data(&g_stMcs_Para.temperature, &g_stMcs_Para.humidity) == FALSE)
{
g_error_log.temp_error_SHT30 = 1;
/// 错误处理
g_stMcs_Para.temperature = backupTemperature;
}
}
while(1)
{
osDelay(1000);
time_s_temp_humi ++;
time_s_1Day ++;
// 温湿度大气压更新
if (time_s_temp_humi >= 1/*g_stConfigInfo.temp_hum_update_time*/)
if (time_s_temp_humi >= g_usrConfigInfo.temp_hum_update_time)
{
// 采集HP203B传感器数据大气压
if(get_HP203_data(&backupTemperature, &g_stMcs_Para.pressure) == FALSE)

View File

@ -356,7 +356,7 @@ static u_int16_t FRT_ReadRegPrecipitationIntensity(void *pMsg)
static u_int16_t FRT_ReadRegDeviceAddr(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.addr;
u_int16_t value=g_usrConfigInfo.addr;
return FRT_swap_endian_16(value);
}
@ -368,7 +368,7 @@ static u_int16_t FRT_ReadRegDeviceAddr(void *pMsg)
static u_int16_t FRT_ReadRegCommuBaudRate(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.uart_baud;
u_int16_t value=g_usrConfigInfo.uart_baud;
// term_printf("%x", value);
return FRT_swap_endian_16(value);
}
@ -381,7 +381,7 @@ static u_int16_t FRT_ReadRegCommuBaudRate(void *pMsg)
static u_int16_t FRT_ReadRegSpeedAverageTime(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.speed_average_time;
u_int16_t value=g_usrConfigInfo.speed_average_time;
return FRT_swap_endian_16(value);
}
@ -393,7 +393,7 @@ static u_int16_t FRT_ReadRegSpeedAverageTime(void *pMsg)
static u_int16_t FRT_ReadRegTempHumUpdateTime(void *pMsg)
{
read_config_info();
u_int16_t value=g_stConfigInfo.temp_hum_update_time;
u_int16_t value=g_usrConfigInfo.temp_hum_update_time;
return FRT_swap_endian_16(value);
}
@ -416,8 +416,8 @@ static u_int16_t FRT_WriteRegDeviceAddr(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.addr = data;
save_config_info(g_stConfigInfo);
g_usrConfigInfo.addr = data;
save_usr_config_info(g_usrConfigInfo);
return 0;
}
@ -431,11 +431,11 @@ static u_int16_t FRT_WriteRegCommuBaudRate(void *pMsg)
{
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.uart_baud = data;
save_config_info(g_stConfigInfo);
g_usrConfigInfo.uart_baud = data;
save_usr_config_info(g_usrConfigInfo);
MX_USART3_UART_Init(g_stConfigInfo.uart_baud);
MX_USART1_UART_Init(g_stConfigInfo.uart_baud);
MX_USART3_UART_Init(g_usrConfigInfo.uart_baud);
MX_USART1_UART_Init(g_usrConfigInfo.uart_baud);
return 0;
}
@ -450,8 +450,8 @@ static u_int16_t FRT_WriteRegSpeedAverageTime(void *pMsg)
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.speed_average_time = data;
save_config_info(g_stConfigInfo);
g_usrConfigInfo.speed_average_time = data;
save_usr_config_info(g_usrConfigInfo);
return 0;
}
@ -466,8 +466,8 @@ static u_int16_t FRT_WriteRegTempHumUpdateTime(void *pMsg)
uint16_t *pMsgAddr = (uint16_t *)pMsg;
uint16_t data = *pMsgAddr;
g_stConfigInfo.temp_hum_update_time = data;
save_config_info(g_stConfigInfo);
g_usrConfigInfo.temp_hum_update_time = data;
save_usr_config_info(g_usrConfigInfo);
return 0;
}
@ -726,7 +726,7 @@ static u_int16_t FRT_WriteRegTransducerCFG1R5(void *pMsg)
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_cfg_1R5 = data;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -742,7 +742,7 @@ static u_int16_t FRT_WriteRegTransducerCFG4R5(void *pMsg)
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_cfg_4R5 = data;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -758,7 +758,7 @@ static u_int16_t FRT_WriteRegTransducerCFG8R0(void *pMsg)
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_cfg_8R0 = data;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -774,7 +774,7 @@ static u_int16_t FRT_WriteRegRSSIRange(void *pMsg)
uint16_t data = *pMsgAddr;
g_stConfigInfo.RSSI_range = data;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -790,7 +790,7 @@ static u_int16_t FRT_WriteRegDISTANCE(void *pMsg)
uint16_t data = *pMsgAddr;
g_stConfigInfo.transducer_distace = data;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -810,7 +810,7 @@ static u_int16_t FRT_WriteRegPoint_1Y(void *pMsg)
// 写入Flash
g_stConfigInfo.linear_point_1_x = PointX;
g_stConfigInfo.linear_point_1_y = PointY;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -830,7 +830,7 @@ static u_int16_t FRT_WriteRegPoint_2Y(void *pMsg)
// 写入Flash
g_stConfigInfo.linear_point_2_x = PointX;
g_stConfigInfo.linear_point_2_y = PointY;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -850,7 +850,7 @@ static u_int16_t FRT_WriteRegPoint_3Y(void *pMsg)
// 写入Flash
g_stConfigInfo.linear_point_3_x = PointX;
g_stConfigInfo.linear_point_3_y = PointY;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -870,7 +870,7 @@ static u_int16_t FRT_WriteRegPoint_4Y(void *pMsg)
// 写入Flash
g_stConfigInfo.linear_point_4_x = PointX;
g_stConfigInfo.linear_point_4_y = PointY;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -890,7 +890,7 @@ static u_int16_t FRT_WriteRegPoint_5Y(void *pMsg)
// 写入Flash
g_stConfigInfo.linear_point_5_x = PointX;
g_stConfigInfo.linear_point_5_y = PointY;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -906,7 +906,7 @@ static u_int16_t FRT_WriteRegLinearEnable(void *pMsg)
uint16_t data = *pMsgData;
g_stConfigInfo.linear_enable = data;
save_config_info(g_stConfigInfo);
save_factory_config_info(g_stConfigInfo);
return 0;
}
@ -922,7 +922,7 @@ static void send_uart_pack(device_handle device,FRT_MsgFunctionCode_e cmd_type,
memset(rs485_out_buff,0,sizeof(rs485_out_buff));
frt_climate_pack_resp *pack = (frt_climate_pack_resp*)rs485_out_buff;
pack->addr = g_stConfigInfo.addr;
pack->addr = g_usrConfigInfo.addr;
pack->func = FRT_FUNCTION_CODE_READ_REGISTER;
pack->data_len = len;
memcpy(pack->data, data, len);
@ -1087,7 +1087,7 @@ void FRT_MsgProc_WriteRegister(device_handle device, void *pMsg)
// 发回数据
uint8_t Trans_data[6];
Trans_data[0] = g_stConfigInfo.addr;
Trans_data[0] = g_usrConfigInfo.addr;
Trans_data[1] = data[1];
Trans_data[2] = start_reg_addr;
Trans_data[3] = reg_num;
@ -1141,7 +1141,7 @@ static int uart_read_frt_climate_pack(device_handle uart_handle,u_int8_t *buff,
// 判断首字符是否是地址,是地址再开始读取,不是则将索引退一步
if(offset == sizeof(unsigned char))
{
if(buff[0] != g_stConfigInfo.addr && buff[0] != 0xFF)
if(buff[0] != g_usrConfigInfo.addr && buff[0] != 0xFF)
{
memcpy(buff, buff + 1, offset - 1);
offset--;

View File

@ -3,21 +3,22 @@
#include "pdebug.h"
#include "assertions.h"
/*
*
* 10
* 35
* 65
*
*/
/* 默认参数 */
config_info g_stConfigInfo={
usr_config_info g_usrConfigInfo={
.flag_head = FLAG_SAVE_INFLASH_HEAD,
.addr = 0x30, /* 默认地址 */
.uart_baud = 9600, /* 默认波特率 */
.speed_average_time = 600, /* 平均风速默认10分钟更新 */
.temp_hum_update_time = 600, /* 温湿度更新时间 */
.flag_end = FLAG_SAVE_INFLASH_END,
};
/* 默认参数 */
factory_config_info g_stConfigInfo={
.flag_head = FLAG_SAVE_INFLASH_HEAD,
// .transducer_cfg_1R5 = 15, /* 大宇换能器参数 */
// .transducer_cfg_4R5 = 45, /* 大宇换能器参数 */
// .transducer_cfg_8R0 = 80, /* 大宇换能器参数 */
@ -83,32 +84,68 @@ void InFlash_Write(u_int32_t addr, u_int64_t *pBuffer,u_int32_t NumToWrite)
}
/**
* @brief
* @brief
* @param
* @retval
*/
void save_config_info(config_info save_config_info)
void save_usr_config_info(usr_config_info save_config_info)
{
HAL_FLASH_Unlock();
InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE_1, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
HAL_FLASH_Lock();
}
/**
* @brief
* @brief
* @param
* @retval
*/
BOOL read_config_info()
void save_factory_config_info(factory_config_info save_config_info)
{
config_info temp_config_info={0x00};
HAL_FLASH_Unlock();
InFlash_Write(STM32_INFLASH_SAVE_ADDR_2_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE_2, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_2_BEGIN, (u_int64_t*)&save_config_info, (sizeof(save_config_info)+7)/8);
HAL_FLASH_Lock();
}
memcpy((u_int8_t*)&temp_config_info,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_BEGIN, sizeof(config_info));
/**
* @brief
* @param
* @retval
*/
BOOL read_usr_config_info()
{
usr_config_info temp_config_info={0x00};
memcpy((u_int8_t*)&temp_config_info,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_1_BEGIN, sizeof(usr_config_info));
// term_printf("%d", temp_config_info.addr);
if((temp_config_info.flag_head == FLAG_SAVE_INFLASH_HEAD) && (temp_config_info.flag_end == FLAG_SAVE_INFLASH_END)){
memcpy((u_int8_t*)&g_stConfigInfo, (u_int8_t*)&temp_config_info, sizeof(config_info));
memcpy((u_int8_t*)&g_usrConfigInfo, (u_int8_t*)&temp_config_info, sizeof(usr_config_info));
// term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_usrConfigInfo.addr, g_usrConfigInfo.speed_average_time);
return TRUE;
}
// term_printf("!get config info from inflash error,use default parameter.\r\n");
// term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_usrConfigInfo.addr, g_usrConfigInfo.speed_average_time);
return FALSE;
}
/**
* @brief
* @param
* @retval
*/
BOOL read_factory_config_info()
{
factory_config_info temp_config_info={0x00};
memcpy((u_int8_t*)&temp_config_info,(u_int8_t*) STM32_INFLASH_SAVE_ADDR_2_BEGIN, sizeof(factory_config_info));
// term_printf("%d", temp_config_info.addr);
if((temp_config_info.flag_head == FLAG_SAVE_INFLASH_HEAD) && (temp_config_info.flag_end == FLAG_SAVE_INFLASH_END)){
memcpy((u_int8_t*)&g_stConfigInfo, (u_int8_t*)&temp_config_info, sizeof(factory_config_info));
// term_printf("Addr:%d, speed_average_time:%dmin\r\n", g_stConfigInfo.addr, g_stConfigInfo.speed_average_time);
return TRUE;
}
@ -119,17 +156,31 @@ BOOL read_config_info()
}
/**
* @brief
* @brief
* @param
* @retval
*/
BOOL read_config_info()
{
if(read_usr_config_info&&read_factory_config_info)
{
return TRUE;
}
return FALSE;
}
/**
* @brief
* @param
* @retval
*/
void delete_config_info()
{
config_info delete_config_info = {0};
usr_config_info delete_config_info = {0};
HAL_FLASH_Unlock();
InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
InFlash_Erase_Pages(STM32_INFLASH_SAVE_PAGE_1, 1);
InFlash_Write(STM32_INFLASH_SAVE_ADDR_1_BEGIN, (u_int64_t*)&delete_config_info, (sizeof(delete_config_info)+7)/8);
HAL_FLASH_Lock();
}

View File

@ -145,8 +145,8 @@ void Flash_EnableReadProtection(void)
/* Initialize all configured peripherals */
read_config_info();
uart_devices[0].uart_baudrate = g_stConfigInfo.uart_baud;
uart_devices[1].uart_baudrate = g_stConfigInfo.uart_baud;
uart_devices[0].uart_baudrate = g_usrConfigInfo.uart_baud;
uart_devices[1].uart_baudrate = g_usrConfigInfo.uart_baud;
MX_GPIO_Init();
MX_DMA_Init();