356 lines
12 KiB
C
356 lines
12 KiB
C
|
||
#include "inFlash.h"
|
||
#include "parameter.h"
|
||
#include "pDebug.h"
|
||
#include "cfg_protocol.h"
|
||
|
||
static void save_config_info(config_info *save_config_info);
|
||
|
||
/**
|
||
* @brief 保存配置信息
|
||
* @param save_config_info 需要保存的配置信息
|
||
* @retval None
|
||
*/
|
||
void save_config_info(config_info *save_config_info)
|
||
{
|
||
write_Flash((uint8_t *)save_config_info, CONFIG_SAVE_ADDR_BEGIN, CONFIG_INFO_SIZE);
|
||
}
|
||
|
||
// /**
|
||
// * @brief 保存配置信息到备份区
|
||
// * @param save_config_info 需要保存的配置信息
|
||
// * @retval None
|
||
// */
|
||
// static void save_backups_config_info(config_info *save_config_info)
|
||
// {
|
||
// write_Flash((uint8_t *)save_config_info, CONFIG_SAVE_addr, CONFIG_INFO_SIZE);
|
||
// }
|
||
|
||
/**
|
||
* @brief 读取配置信息
|
||
* @param read_config_info 读取配置信息并保存在output_config_info中
|
||
* @retval None
|
||
*/
|
||
void read_config_info(config_info *output_config_info)
|
||
{
|
||
read_Flash((uint8_t *)output_config_info, CONFIG_SAVE_ADDR_BEGIN, CONFIG_INFO_SIZE);
|
||
}
|
||
|
||
// /**
|
||
// * @brief 读取备份的配置信息
|
||
// * @param read_config_info 读取配置信息并保存在output_config_info中
|
||
// * @retval None
|
||
// */
|
||
// static void read_backups_config_info(config_info *output_config_info)
|
||
// {
|
||
// read_Flash((uint8_t *)output_config_info, CONFIG_SAVE_addr, CONFIG_INFO_SIZE);
|
||
// }
|
||
|
||
void saveConfigInfo(config_info *configInfo)
|
||
{
|
||
save_config_info(configInfo);
|
||
// save_backups_config_info(config_info);
|
||
}
|
||
|
||
/**
|
||
* @brief 检测flash中是否有配置文件或者文件是否有损坏,若两处flash中都损坏则使用默认文件
|
||
* @param config_info 读取的配置信息
|
||
* @retval None
|
||
*
|
||
*/
|
||
static void readFlashContent(config_info *configInfo)
|
||
{
|
||
read_config_info(configInfo);
|
||
/* 配置文件正确就返回 */
|
||
// static volatile uint16_t tempCrc1, tempCrc2;
|
||
// static volatile config_info *tempConfigInfo1;
|
||
// tempConfigInfo1 = configInfo;
|
||
// tempCrc1 = configInfo->crc;
|
||
// tempCrc2 = checkModebusCrc((uint8_t *)configInfo, CONFIG_INFO_SIZE - 2);
|
||
// if (tempCrc1 == tempCrc2) {
|
||
// return;
|
||
// }
|
||
if (configInfo->crc == checkModebusCrc((uint8_t *)configInfo, CONFIG_INFO_SIZE - 2)) {
|
||
return;
|
||
}
|
||
|
||
// /* 更深处的配置文件正确就返回 */
|
||
// read_backups_config_info(config_info);
|
||
// if (config_info->crc == configCheckFunc((uint8_t *)config_info, CONFIG_INFO_SIZE - 2)) {
|
||
// save_config_info(config_info);
|
||
// return;
|
||
// }
|
||
|
||
/* 配置文件错误使用默认配置 */
|
||
configInfo->address[0] = 0x11;
|
||
configInfo->address[1] = 0x11;
|
||
configInfo->address[2] = 0x11;
|
||
configInfo->address[3] = 0x11;
|
||
configInfo->address[4] = 0x11;
|
||
configInfo->address[5] = 0x11;
|
||
configInfo->address[6] = 0x11;
|
||
// config_info->Access_Node_Type = 0x01;
|
||
// config_info->Communication_Methods = 0x02;
|
||
configInfo->gw485_Baud = 9600;
|
||
configInfo->bat485_Baud = 115200;
|
||
|
||
configInfo->hardwareID[0] = 0x48;
|
||
configInfo->hardwareID[1] = 0x59;
|
||
configInfo->hardwareID[2] = 0x30;
|
||
configInfo->hardwareID[3] = 0x30;
|
||
configInfo->hardwareID[4] = 0x30;
|
||
configInfo->hardwareID[5] = 0x31;
|
||
configInfo->communicationID[0] = 0x00;
|
||
configInfo->communicationID[1] = 0x00;
|
||
configInfo->communicationID[2] = 0x00;
|
||
configInfo->communicationID[3] = 0x01;
|
||
configInfo->protocolType = 0x01;
|
||
|
||
configInfo->CommunicationProtocolType = 0x01;
|
||
configInfo->onlyPower = 0x01;
|
||
|
||
configInfo->constantVoltageV = 14;
|
||
configInfo->floatI = 0.02;
|
||
configInfo->startSolarOpenCircuitV = 17;
|
||
configInfo->stopSolarOpenCircuitV = 15;
|
||
configInfo->constantVoltageChargeV = 14.4;
|
||
configInfo->FloatChargeV = 14;
|
||
configInfo->HighSideMosTemperature_stop = 100;
|
||
configInfo->HighSideMosTemperature_end = 90;
|
||
configInfo->HighSideMosTemperature_start = 50;
|
||
|
||
// configInfo->checkSolarOpenCircuitVTime = 10;
|
||
// configInfo->sensorEnableBroadcastTime = 20;
|
||
configInfo->outputAgainFlagTime = 10;
|
||
configInfo->excessiveLoadFlagTime = 60;
|
||
configInfo->eLAgainTime = 1800;
|
||
}
|
||
|
||
/**
|
||
* @brief 通过配置文件初始化系统参数
|
||
* @param None
|
||
* @retval None
|
||
*
|
||
*/
|
||
void config_info_start(void)
|
||
{
|
||
Flash_Init();
|
||
|
||
config_info temp_configInfo;
|
||
readFlashContent(&temp_configInfo);
|
||
|
||
g_cfgParameter.constantVoltageV = temp_configInfo.constantVoltageV;
|
||
g_cfgParameter.floatI = temp_configInfo.floatI;
|
||
g_cfgParameter.startSolarOpenCircuitV = temp_configInfo.startSolarOpenCircuitV;
|
||
g_cfgParameter.stopSolarOpenCircuitV = temp_configInfo.stopSolarOpenCircuitV;
|
||
g_cfgParameter.constantVoltageChargeV = temp_configInfo.constantVoltageChargeV;
|
||
g_cfgParameter.FloatV = temp_configInfo.FloatChargeV;
|
||
g_cfgParameter.HighSideMosTemperature_stop = temp_configInfo.HighSideMosTemperature_stop;
|
||
g_cfgParameter.HighSideMosTemperature_end = temp_configInfo.HighSideMosTemperature_end;
|
||
g_cfgParameter.HighSideMosTemperature_start = temp_configInfo.HighSideMosTemperature_start;
|
||
// g_cfgParameter.sensorEnableBroadcastTime = temp_configInfo.sensorEnableBroadcastTime;
|
||
// g_cfgParameter.checkSolarOpenCircuitVTime = temp_configInfo.checkSolarOpenCircuitVTime;
|
||
g_cfgParameter.outputAgainFlagTime = temp_configInfo.outputAgainFlagTime;
|
||
g_cfgParameter.excessiveLoadFlagTime = temp_configInfo.excessiveLoadFlagTime;
|
||
g_cfgParameter.eLAgainTime = temp_configInfo.eLAgainTime;
|
||
g_cfgParameter.collectOpenCircuitVoltageTime= 3600;
|
||
g_cfgParameter.address[0] = temp_configInfo.address[0];
|
||
g_cfgParameter.address[1] = temp_configInfo.address[1];
|
||
g_cfgParameter.address[2] = temp_configInfo.address[2];
|
||
g_cfgParameter.address[3] = temp_configInfo.address[3];
|
||
g_cfgParameter.address[4] = temp_configInfo.address[4];
|
||
g_cfgParameter.address[5] = temp_configInfo.address[5];
|
||
g_cfgParameter.address[6] = temp_configInfo.address[6];
|
||
// g_cfgParameter.Access_Node_Type = temp_configInfo.Access_Node_Type;
|
||
// g_cfgParameter.Communication_Methods = temp_configInfo.Communication_Methods;
|
||
g_cfgParameter.hardwareID[0] = temp_configInfo.hardwareID[0];
|
||
g_cfgParameter.hardwareID[1] = temp_configInfo.hardwareID[1];
|
||
g_cfgParameter.hardwareID[2] = temp_configInfo.hardwareID[2];
|
||
g_cfgParameter.hardwareID[3] = temp_configInfo.hardwareID[3];
|
||
g_cfgParameter.hardwareID[4] = temp_configInfo.hardwareID[4];
|
||
g_cfgParameter.hardwareID[5] = temp_configInfo.hardwareID[5];
|
||
g_cfgParameter.communicationID[0] = temp_configInfo.communicationID[0];
|
||
g_cfgParameter.communicationID[1] = temp_configInfo.communicationID[1];
|
||
g_cfgParameter.communicationID[2] = temp_configInfo.communicationID[2];
|
||
g_cfgParameter.communicationID[3] = temp_configInfo.communicationID[3];
|
||
g_cfgParameter.protocolType = temp_configInfo.protocolType;
|
||
// g_cfgParameter.CommunicationProtocolType = temp_configInfo.CommunicationProtocolType;
|
||
g_cfgParameter.onlyPower = temp_configInfo.onlyPower;
|
||
|
||
g_cfgParameter.startFlagSL[0] = 'S';
|
||
g_cfgParameter.startFlagSL[1] = 'L';
|
||
g_cfgParameter.endFlagSL = 0x16;
|
||
g_cfgParameter.startFlagHY = 0x68;
|
||
g_cfgParameter.endFlagHY = 0x16;
|
||
|
||
// if (g_cfgParameter.CommunicationProtocolType == 0x00) {
|
||
// g_cfgParameter.gw485_Baud = temp_configInfo.gw485_Baud;
|
||
// g_cfgParameter.bat485_Baud = temp_configInfo.bat485_Baud;
|
||
// } else if (g_cfgParameter.CommunicationProtocolType == 0x01) {
|
||
// g_cfgParameter.bat485_Baud = temp_configInfo.bat485_Baud;
|
||
// if (g_cfgParameter.protocolType == 0x01) {
|
||
// g_cfgParameter.gw485_Baud = 9600;
|
||
// } else if (g_cfgParameter.protocolType == 0x02) {
|
||
// g_cfgParameter.gw485_Baud = 115200;
|
||
// }
|
||
// }
|
||
g_cfgParameter.gw485_Baud = 115200;
|
||
g_cfgParameter.bat485_Baud = 115200;
|
||
|
||
float fTemp;
|
||
readLoopImpedance(&fTemp);
|
||
/* 读取的回路阻抗偏差过大则不使用 */
|
||
if (fTemp > (float)0.005 && fTemp < (float)1) {
|
||
g_cfgParameter.loopImpedance = fTemp;
|
||
}
|
||
else {
|
||
g_cfgParameter.loopImpedance = 0;
|
||
saveLoopImpedance(&g_cfgParameter.loopImpedance);
|
||
}
|
||
readtotalElectricityConsumption(&fTemp);
|
||
totalElectricityConsumptionInt(fTemp);
|
||
readtotalChargCapacity(&fTemp);
|
||
totalChargCapacityInt(fTemp);
|
||
|
||
timeInfo time;
|
||
readTime(&time);
|
||
setLastTime(time);
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief 保存回路阻抗在flash中
|
||
* @param
|
||
*/
|
||
void saveLoopImpedance(float *loopImpedance)
|
||
{
|
||
write_Flash((uint8_t *)loopImpedance, LoopImpedance_SAVE_addr, sizeof(float));
|
||
}
|
||
|
||
/**
|
||
* @brief 读取flash中的回路阻抗
|
||
* @param
|
||
*/
|
||
void readLoopImpedance(float *loopImpedance)
|
||
{
|
||
read_Flash((uint8_t *)loopImpedance, LoopImpedance_SAVE_addr, sizeof(float));
|
||
}
|
||
|
||
/**
|
||
* @brief 保存放电量在flash中
|
||
* @param
|
||
*/
|
||
void savetotalElectricityConsumption(float *totalElectricityConsumption)
|
||
{
|
||
write_Flash((uint8_t *)totalElectricityConsumption, totalElectricityConsumption_SAVE_addr, sizeof(float));
|
||
}
|
||
|
||
/**
|
||
* @brief 读取flash中的放电量
|
||
* @param
|
||
*/
|
||
void readtotalElectricityConsumption(float *totalElectricityConsumption)
|
||
{
|
||
read_Flash((uint8_t *)totalElectricityConsumption, totalElectricityConsumption_SAVE_addr, sizeof(float));
|
||
}
|
||
|
||
/**
|
||
* @brief 保存充电量在flash中
|
||
* @param
|
||
*/
|
||
void savetotalChargCapacity(float *totalChargCapacity)
|
||
{
|
||
write_Flash((uint8_t *)totalChargCapacity, totalChargCapacity_SAVE_addr, sizeof(float));
|
||
}
|
||
|
||
/**
|
||
* @brief 读取flash中的充电量
|
||
* @param
|
||
*/
|
||
void readtotalChargCapacity(float *totalChargCapacity)
|
||
{
|
||
read_Flash((uint8_t *)totalChargCapacity, totalChargCapacity_SAVE_addr, sizeof(float));
|
||
}
|
||
|
||
/**
|
||
* @brief 读取flash中的时间,这个时间是上次读取充放电量的时间
|
||
* @param
|
||
*/
|
||
void saveTime(timeInfo *time)
|
||
{
|
||
write_Flash((uint8_t *)time, time_SAVE_addr, sizeof(timeInfo));
|
||
}
|
||
|
||
/**
|
||
* @brief 读取flash中的时间
|
||
* @param
|
||
*/
|
||
void readTime(timeInfo *time)
|
||
{
|
||
read_Flash((uint8_t *)time, time_SAVE_addr, sizeof(timeInfo));
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief 初始化事件顺序记录
|
||
* @param
|
||
*/
|
||
|
||
void eventsOrderRecordStartInit(void)
|
||
{
|
||
|
||
}
|
||
|
||
/**
|
||
* @brief 将事件顺序记录在flash中
|
||
* @param
|
||
*/
|
||
void setEventsOrderRecord(eventsOrderRecordMode mode)
|
||
{
|
||
static uint32_t len = sizeof(int);
|
||
float temp;
|
||
write_Flash((uint8_t *)&mode, eventsOrderRecordStartAddr + len, sizeof(mode));
|
||
len += sizeof(mode);
|
||
temp = getDischargCurrent();
|
||
write_Flash((uint8_t *)&temp, eventsOrderRecordStartAddr + len, sizeof(float));
|
||
len += sizeof(float);
|
||
temp = getOutputVoltage();
|
||
write_Flash((uint8_t *)&temp, eventsOrderRecordStartAddr + len, sizeof(float));
|
||
|
||
int count = 0;
|
||
read_Flash((uint8_t *)&count, eventsOrderRecordStartAddr, sizeof(count));
|
||
count++;
|
||
write_Flash((uint8_t *)&count, eventsOrderRecordStartAddr, sizeof(count));
|
||
}
|
||
|
||
/**
|
||
* @brief 将事件从flash中依次读取出来
|
||
* @param
|
||
*/
|
||
|
||
void printfEventsOrderRecord(void)
|
||
{
|
||
int count = 0;
|
||
read_Flash((uint8_t *)&count, eventsOrderRecordStartAddr, sizeof(count));
|
||
|
||
float temp;
|
||
eventsOrderRecordMode mode;
|
||
|
||
for (int i = 0; i < count; i++) {
|
||
read_Flash((uint8_t *)&temp, eventsOrderRecordStartAddr + sizeof(int)
|
||
+ i * (sizeof(float) + sizeof(float)), sizeof(mode));
|
||
log_info("eventsOrderRecordMode:%d\n", mode);
|
||
|
||
read_Flash((uint8_t *)&temp, eventsOrderRecordStartAddr + sizeof(int)
|
||
+ i * (sizeof(float) + sizeof(float)) + sizeof(mode), sizeof(float));
|
||
log_info("dischargCurrent:%f\n", temp);
|
||
|
||
read_Flash((uint8_t *)&temp, eventsOrderRecordStartAddr + sizeof(int)
|
||
+ i * (sizeof(float) + sizeof(float)) + sizeof(mode) + sizeof(float), sizeof(float));
|
||
log_info("OutputVoltage:%f\n", temp);
|
||
}
|
||
}
|
||
|
||
|