260 lines
6.7 KiB
C
260 lines
6.7 KiB
C
/**
|
|
******************************************************************************
|
|
* @file device_param.c
|
|
* @author AMG Energy Solution Team
|
|
* @version V1.0.0
|
|
* @date 07-Feb-2022
|
|
* @brief This file provides all the codes of parameter functions.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
|
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
|
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
|
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
|
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
|
*
|
|
* <h2><center>© COPYRIGHT 2022 AMG</center></h2>
|
|
******************************************************************************
|
|
*/
|
|
|
|
/** @addtogroup mBMS2
|
|
* @{
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "includes.h"
|
|
#include "device_param.h"
|
|
#include "flash_if.h"
|
|
#include "dflash_sf1.h"
|
|
#include "dflash_chg_ctrl.h"
|
|
#include "dflash_cal.h"
|
|
#include "dflash_sbs.h"
|
|
#include "asutil.h"
|
|
#include "temp.h"
|
|
#include "malloc.h"
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
#define CALIB_INIT_DATA 0xBBBB0004
|
|
#define INV_INIT_DATA 0xCCCC0003
|
|
#define PARAM_INIT_DATA 0xAAAA4001
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
TDEVICE_VALUE device_value, bakdevice_value, dis_device_value;
|
|
TDEVICE_STATUS device_status, dis_device_status;
|
|
TDEVICE_CALIB_DATA device_calib_data, dis_device_calib_data;
|
|
TDEVICE_PARAM device_param, dis_device_param;
|
|
TDEVICE_INV device_inv, dis_device_inv;
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
|
/* Private function ----------------------------------------------------------*/
|
|
void SetCellQuantity(uint8_t cQuantity)
|
|
{
|
|
if ((cQuantity > 0) && (cQuantity <= 16)) {
|
|
device_param.info.CellQuantity = cQuantity;
|
|
Save_Param();
|
|
}
|
|
}
|
|
|
|
void Param_Init(uint8_t flag, uint8_t factory)
|
|
{
|
|
if (factory) {
|
|
Cal_InitData();
|
|
}
|
|
if (flag) {
|
|
SBS_InitData();
|
|
}
|
|
SF1_InitData();
|
|
ChgCtrlCfg_InitData();
|
|
}
|
|
|
|
/*
|
|
Inventory Data Load and Save Function
|
|
*/
|
|
|
|
void Inv_Init(void)
|
|
{
|
|
device_inv.ManufactureDate = 0;
|
|
memset(device_inv.ModuleNo, 0x00, sizeof(device_inv.ModuleNo));
|
|
memset(device_inv.SerialNo, 0x00, sizeof(device_inv.SerialNo));
|
|
}
|
|
|
|
void Load_InvData(void)
|
|
{
|
|
memcpy(&device_inv, (uint8_t *)mBMS_INV_BASE_ADDR, sizeof(TDEVICE_INV));
|
|
if (device_inv.initdata != INV_INIT_DATA) {
|
|
#ifdef CONSOLE_DEBUG
|
|
printf("\r\n Initialize Calibration Data");
|
|
#endif
|
|
device_inv.initdata = INV_INIT_DATA;
|
|
// Inventory init
|
|
Inv_Init();
|
|
}
|
|
}
|
|
|
|
void Save_InvData(void)
|
|
{
|
|
uint32_t result = 0;
|
|
uint32_t sector_addr = mBMS_INV_BASE_ADDR;
|
|
|
|
result = FLASH_If_SingleSectorErase(sector_addr);
|
|
|
|
if (result == 0) {
|
|
uint32_t base = (uint32_t)&device_inv;
|
|
uint32_t len = sizeof(TDEVICE_INV);
|
|
|
|
if ((len % 4) > 0) {
|
|
len = len / 4 + 1;
|
|
} else {
|
|
len = len / 4;
|
|
}
|
|
result = FLASH_If_Write(§or_addr, (uint32_t *)base, len);
|
|
|
|
#ifdef CONSOLE_DEBUG
|
|
if (result != 0)
|
|
printf("\r\n Param save fail (%d)", result);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
/*
|
|
Calibration Data Load and Save Function
|
|
*/
|
|
void Load_CalibData(void)
|
|
{
|
|
memcpy(&device_calib_data, (uint8_t *)mBMS_CALIB_BASE_ADDR, sizeof(TDEVICE_CALIB_DATA));
|
|
if (device_calib_data.initdata != CALIB_INIT_DATA) {
|
|
#ifdef CONSOLE_DEBUG
|
|
printf("\r\n Initialize Calibration Data");
|
|
#endif
|
|
device_calib_data.initdata = CALIB_INIT_DATA;
|
|
Cal_InitData();
|
|
Save_CalibData();
|
|
}
|
|
}
|
|
|
|
void Save_CalibData(void)
|
|
{
|
|
uint32_t result = 0;
|
|
uint32_t sector_addr = mBMS_CALIB_BASE_ADDR;
|
|
|
|
result = FLASH_If_SingleSectorErase(sector_addr);
|
|
|
|
if (result == 0) {
|
|
uint32_t base = (uint32_t)&device_calib_data;
|
|
uint32_t len = sizeof(TDEVICE_CALIB_DATA);
|
|
|
|
if ((len % 4) > 0)
|
|
len = len / 4 + 1;
|
|
else
|
|
len = len / 4;
|
|
|
|
result = FLASH_If_Write(§or_addr, (uint32_t *)base, len);
|
|
|
|
#ifdef CONSOLE_DEBUG
|
|
if (result != 0)
|
|
printf("\r\n Param save fail (%d)", result);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void Load_Param(void)
|
|
{
|
|
Load_CalibData();
|
|
Load_InvData();
|
|
|
|
memcpy(&device_param, (uint8_t *)mBMS_PARAM_BASE_ADDR, sizeof(TDEVICE_PARAM));
|
|
|
|
if (device_param.initdata != PARAM_INIT_DATA) {
|
|
device_param.initdata = PARAM_INIT_DATA;
|
|
// Parameter init
|
|
Param_Init(true, true);
|
|
|
|
Save_Param();
|
|
}
|
|
|
|
// device_param.safety.current.OC_Dsg_Rec_Time = 5;
|
|
ADC_Param_Load();
|
|
|
|
/* KT Requirement: Cell deviation 200mV threshold (Warning/Time unused, CV_Diff_ProtectHoldTime 24h default) */
|
|
if ((device_param.safety.cv_diff.Cell_Voltage_Diff_Threshold == 0) ||
|
|
(device_param.safety.cv_diff.Cell_Voltage_Diff_Threshold > 200)) {
|
|
device_param.safety.cv_diff.Cell_Voltage_Diff_Threshold = 200;
|
|
}
|
|
|
|
device_param.chg_ctrl.current_thr.Discharge_Detection_Threshold = 6000; // 0.05C = 100 * 0.05 = 5A
|
|
device_param.chg_ctrl.current_thr.Charge_Detection_Threshold = 6000;
|
|
|
|
/* KT Requirement: Cell balancing threshold set to 3350mV in RAM at boot */
|
|
device_param.chg_ctrl.cell_bal_cfg.Cell_Balance_Threshold = 3350;
|
|
}
|
|
|
|
void Save_Param(void)
|
|
{
|
|
uint32_t result = 0;
|
|
uint32_t sector_addr = mBMS_PARAM_BASE_ADDR;
|
|
|
|
result = FLASH_If_SingleSectorErase(sector_addr);
|
|
|
|
if (result == 0) {
|
|
uint32_t base = (uint32_t)&device_param;
|
|
uint32_t len = sizeof(TDEVICE_PARAM);
|
|
|
|
if ((len % 4) > 0) {
|
|
len = len / 4 + 1;
|
|
} else {
|
|
len = len / 4;
|
|
}
|
|
result = FLASH_If_Write(§or_addr, (uint32_t *)base, len);
|
|
|
|
#ifdef CONSOLE_DEBUG
|
|
if (result != 0) {
|
|
printf("\r\n Param save fail (%d)", result);
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void LoadFwInfo(void)
|
|
{
|
|
memcpy(&fwinfo, (uint8_t *)mBMS_FW_INFO_BASE_ADDR, sizeof(FW_INFO));
|
|
}
|
|
|
|
uint32_t SaveFwInfo(void)
|
|
{
|
|
uint32_t result = 0;
|
|
uint32_t sector_addr = mBMS_FW_INFO_BASE_ADDR;
|
|
|
|
/* init flash */
|
|
FLASH_If_Init();
|
|
result = FLASH_If_SingleSectorErase(sector_addr);
|
|
|
|
if (result == 0) {
|
|
uint32_t base = (uint32_t)&fwinfo;
|
|
uint32_t len = sizeof(FW_INFO);
|
|
|
|
if ((len % 4) > 0) {
|
|
len = len / 4 + 1;
|
|
} else {
|
|
len = len / 4;
|
|
}
|
|
result = FLASH_If_Write(§or_addr, (uint32_t *)base, len);
|
|
if ((result == 0) &&
|
|
((sector_addr != (mBMS_FW_INFO_BASE_ADDR + len * 4)) ||
|
|
(memcmp((const void *)mBMS_FW_INFO_BASE_ADDR, &fwinfo, sizeof(FW_INFO)) != 0))) {
|
|
result = 3; /* Incomplete write or flash readback mismatch. */
|
|
}
|
|
|
|
#ifdef CONSOLE_DEBUG
|
|
if (result != 0)
|
|
printf("\r\n Fw information save fail (%d)", result);
|
|
#endif
|
|
}
|
|
return result;
|
|
}
|