/*
 * inflash.c
 *
 *  Created on: 2024年6月24日
 *      Author: psx
 */

#include "inflash.h"
#include "flash.h"
#include "sl_protocol.h"

config_info g_slConfigInfo = {
        .constantCurrentV = 1000,
        .constantVoltageV = 1400,
        .floatI = 10,
        .startSolarOpenCircuitV = 1700,
        .stopSolarOpenCircuitV = 1500,
        .noBatteryChargeV = 1200,
        .FloatTime = 10,
        .checkSolarOpenCircuitVTime = 10,
        .registerRefreshTime = 1,
        .resRefreshTime = 1,
        .sensorEnableBroadcastTime = 20,
        .HighSideMosTemperature_stop = 70,
        .HighSideMosTemperature_end = 50,
        .HighSideMosTemperature_start = 40,
};


/**
 * @brief   保存配置信息
 * @param   save_config_info 需要保存的配置信息
 * @retval
 */
void save_config_info(config_info save_config_info)
{
    SPI_Flash_Write((uint8_t *)&save_config_info, FLASH_SAVE_ADDR_BEGIN, CONFIG_INFO_SIZE);
}


/**
 * @brief   读取配置信息
 * @param   read_config_info 读取的配置信息
 * @retval  0   flash中读取配置失败
 *          1   flash中读取配置成功
 */
uint8_t read_config_info(void)
{
    config_info temp_config_info;
    SPI_Flash_Read((uint8_t *)&temp_config_info, FLASH_SAVE_ADDR_BEGIN, CONFIG_INFO_SIZE);
    if (temp_config_info.start_Flag[0] == 'S'
            && temp_config_info.start_Flag[1] == 'L'
            && temp_config_info.end_Flag == 0x16) {
        g_slConfigInfo = temp_config_info;
        return 1;
    }
    else {
        g_slConfigInfo.start_Flag[0] = defaultValue.start_Flag[0];
        g_slConfigInfo.start_Flag[1] = defaultValue.start_Flag[1];
        g_slConfigInfo.address[0] = defaultValue.address[0];
        g_slConfigInfo.address[1] = defaultValue.address[1];
        g_slConfigInfo.address[2] = defaultValue.address[2];
        g_slConfigInfo.address[3] = defaultValue.address[3];
        g_slConfigInfo.address[4] = defaultValue.address[4];
        g_slConfigInfo.address[5] = defaultValue.address[5];
        g_slConfigInfo.address[6] = defaultValue.address[6];
        g_slConfigInfo.end_Flag = defaultValue.end_Flag;
    }



    return 0;
}





