Files

433 lines
9.6 KiB
C

/**
******************************************************************************
* @file log.c
* @author AMG Energy Solution Team
* @version V1.0.0
* @date 07-Feb-2022
* @brief This file provides all the codes of the log process.
******************************************************************************
* @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>&copy; COPYRIGHT 2022 AMG</center></h2>
******************************************************************************
*/
/** @addtogroup mBMS2
* @{
*/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <includes.h>
#include "brtc.h"
#include "usart.h"
#include "malloc.h"
#include "device_param.h"
#include "temp.h"
#include "log.h"
#include "date_calc.h"
#include "asutil.h"
#include "delay.h"
#include "can_comm.h"
/* Private typedef -----------------------------------------------------------*/
typedef struct {
uint32_t timestamp;
struct {
uint16_t cv[DEVICE_MAX_CELL + 1];
} module[DEVICE_MAX_MODULE];
} TModuleVoltLog, *PModuleVoltLog;
typedef struct {
uint32_t timestamp;
struct {
uint16_t ct[DEVICE_MAX_TEMP];
} module[DEVICE_MAX_MODULE];
} TModuleTempLog, *PModuleTempLog;
/* Private define ------------------------------------------------------------*/
#define LOG_FILE_DIR "0:/log"
#define CELL_VOLT_LOG_FILE "0:/log/CVLOG.dat"
#define CELL_TEMP_LOG_FILE "0:/log/CTLOG.dat"
/* Private macro -------------------------------------------------------------*/
const char LOG_HEADER[] =
"OpTime,"
"BatVoltage,"
"Current,"
"SOC,"
"Cell_01,"
"Cell_02,"
"Cell_03,"
"Cell_04,"
"Cell_05,"
"Cell_06,"
"Cell_07,"
"Cell_08,"
"Cell_09,"
"Cell_10,"
"Cell_11,"
"Cell_12,"
"Cell_13,"
"Cell_14,"
"Cell_15,"
"Temp_01,"
"Temp_02,"
"Temp_03,"
"Temp_04,"
"Temp_05,"
"Temp_06,"
"OpSatatus,"
"Warning,"
"Protection,"
"\r\n"
;
/* Private variables ---------------------------------------------------------*/
static uint8_t active_log = 0;
static uint8_t logging_time = 5; // Second
static uint32_t logging_tick = 0;
static uint8_t tray_active_log = false;
#ifdef CONSOLE_DEBUG
static uint8_t dis_active_log = 0;
static uint8_t dis_logging_time; // Second
static uint8_t dis_tray_active_log = 0;
#endif // #ifdef CONSOLE_DEBUG
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void LogWrite(uint8_t *ldata, uint32_t size)
{
}
void LogWriteTrayV(uint8_t *ldata, uint32_t size)
{
}
#ifdef CONSOLE_DEBUG
void LogPrint(const TCHAR *fname)
{
FRESULT rc;
uint8_t eflag = 0;
DWORD fOffset = 0;
UINT rlen = 0, err = 0;
char *txt;
txt = mymalloc(130);
if (txt != NULL) {
printf("\r\n");
do {
rc = FileRead(fname, (BYTE *)txt, fOffset, 128, &rlen);
if (rc == FR_OK) {
err = 0;
if (rlen < 128) {
txt[rlen] = 0;
printf("%s", txt);
eflag = 1;
} else if (rlen == 128) {
txt[rlen] = 0;
printf("%s", txt);
}
fOffset += rlen;
} else {
WDT_RefreshCounter();
err++;
if (rc == FR_LOCKED)
delay_os_ms(500);
if (err > 5)
eflag = 1;
}
/* Reload IWDG counter */
WDT_RefreshCounter();
delay_os_ms(1);
} while (eflag == 0);
printf("\r\nFile Read End (total: %d)\r\n", (uint32_t)fOffset);
myfree(txt);
}
}
void ModuleLogPrint(void)
{
FRESULT rc;
uint8_t eflag = 0;
DWORD fOffset = 0;
UINT rlen = 0, err = 0;
PModuleVoltLog data;
char *txt;
_TTIME time;
data = mymalloc(sizeof(TModuleVoltLog));
if (data != NULL) {
printf("\r\n");
do {
rc = FileRead(CELL_VOLT_LOG_FILE, (BYTE *)data, fOffset, sizeof(TModuleVoltLog), &rlen);
if (rc == FR_OK) {
err = 0;
if (rlen < sizeof(TModuleVoltLog)) {
eflag = 1;
} else if (rlen == sizeof(TModuleVoltLog)) {
CalcTimeStampToDateTime(data->timestamp + UTC_KOREA, &time);
printf("%04d/%02d/%02d %02d:%02d:%02d,",
time.year + UTC_YEAR,
time.month,
time.date,
time.hour,
time.min,
time.sec);
// for (i = 0; i < TRAY_MAX; i++) {
// for (j = 0; j < TRAY_MAX_CELL; j++) {
// printf("%5.3f,", (float)data->tray[i].cv[j] / 1000);
// }
// }
printf("\r\n");
}
fOffset += rlen;
} else {
WDT_RefreshCounter();
err++;
if (rc == FR_LOCKED)
delay_os_ms(500);
if (err > 5)
eflag = 1;
}
/* Reload IWDG counter */
WDT_RefreshCounter();
delay_os_ms(1);
} while (eflag == 0);
printf("\r\nFile Read End (total: %d)\r\n", (uint32_t)fOffset);
myfree(txt);
}
}
void LogInterval(uint8_t *time)
{
uint16_t size, i, j;
int16_t aParam;
uint8_t temp[10];
size = 0;
while (time[size] != 0x00) size++; /* Measure total string length */
i = 0;
if ((size == 0)||(size > 3)) return;
j = FIND_CHAR((uint8_t *)&time[i], temp, size - i, ' ');
i = i + j + 1;
temp[j] = 0x00;
aParam = StrToInt(temp, j); /* Value */
if (time == 0)
logging_time = 5;
else
logging_time = aParam;
}
#endif // #ifdef CONSOLE_DEBUG
void LogHeader(void)
{
char *txt;
txt = mymalloc(512);
if (txt != NULL)
{
sprintf(txt, "%s", LOG_HEADER);
// LogWrite((BYTE *)txt, (DWORD)strlen(txt));
myfree(txt);
}
}
void LogBody(void)
{
uint16_t i;
char *txt;
// float ckw, dkw;
txt = mymalloc(1024);
if (txt != NULL)
{
i = 0;
// ckw = ((float)StoreSOC.CalcSOC.ChargeCapacity / 60 / 60 / 100) * ((float)device_value.BatVoltage / 10);
// dkw = ((float)StoreSOC.CalcSOC.DischargeCapacity / 60 / 60 / 100) * ((float)device_value.BatVoltage / 10);
sprintf(&txt[i], "%04d/%02d/%02d %02d:%02d:%02d,",
GetTimeYear(),
GetTimeMonth(),
GetTimeDate(),
GetTimeHour(),
GetTimeMinute(),
GetTimeSec()
); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.BatVoltage / 10); i = strlen(txt);
sprintf(&txt[i], "%6.2f,", (float)device_value.Current / 100); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.RelativeStateOfCharge / 10); i = strlen(txt);
for (int j = 0; j < DEVICE_MAX_CELL; j++)
{
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
}
for (int j = 0; j < DEVICE_MAX_TEMP; j++)
{
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
}
sprintf(&txt[i], "0x%04X,", device_status.op_status); i = strlen(txt);
sprintf(&txt[i], "0x%04X,", device_status.warning.value); i = strlen(txt);
sprintf(&txt[i], "0x%04X,", device_status.protect.value); i = strlen(txt);
sprintf(&txt[i], "\r\n"); i = strlen(txt);
// LogWrite((BYTE *)txt, (DWORD)i);
myfree(txt);
}
}
void LogBodyModuleV(void)
{
// uint16_t i, j;
// PTrayVoltLog tvl;
//
// tvl = mymalloc(sizeof(TTrayVoltLog));
//
// if (tvl != NULL) {
// tvl->timestamp = GetTimeStamp(); // Time
// for (i = 0; i < TRAY_MAX; i++) {
// for (j = 0; j < TRAY_MAX_CELL; j++) {
// tvl->tray[i].cv[j] = GetTrayVoltage(i, j);
// }
// }
// LogWriteTrayV((BYTE *)tvl, (DWORD)sizeof(TTrayVoltLog));
// myfree(tvl);
// }
}
void ModuleLogEnable(uint16_t time)
{
if (tray_active_log) {
UserMessage("[LOGGING]: MODULE LOG ALREADY START !!");
} else {
tray_active_log = true;
UserMessage("[LOGGING]: MODULE LOG START COMPLETE !!");
}
}
void ModuleLogDisable(void)
{
if (tray_active_log) {
UserMessage("[LOGGING]: MODULE LOG STOP COMPLETE !!");
} else {
UserMessage("[LOGGING]: MODULE LOG ALREADY STOP !!");
}
tray_active_log = false;
}
void ModuleLogDelete(void)
{
if (tray_active_log == true) {
UserMessage("Logging.... cannot delete tray log");
} else {
}
}
void LogEnable(uint16_t time)
{
if (active_log) {
UserMessage("[LOGGING]: ALREADY START !!");
} else {
if (time == 0)
logging_time = 5;
else
logging_time = time;
active_log = 1;
UserMessage("[LOGGING]: START COMPLETE !!");
}
}
void LogDisable(void)
{
if (active_log) {
UserMessage("[LOGGING]: STOP COMPLETE !!");
} else {
UserMessage("[LOGGING]: ALREADY STOP !!");
}
active_log = 0;
logging_time = 5;
}
void LoggingProcess(void)
{
if (logging_time > 0)
{
if (active_log == 1)
{
if ((logging_tick != GetTimeStamp())&&((GetTimeStamp() % logging_time) == 0))
{
LogBody();
if (tray_active_log == true)
LogBodyModuleV();
logging_tick = GetTimeStamp();
}
}
}
}
#ifdef CONSOLE_DEBUG
void DisplayLogStatus(uint8_t p)
{
uint8_t text[20];
if (p ||(dis_active_log != active_log)) {
if (active_log == true)
RedOutXY( 65, 18, (uint8_t *)"LOGGING.");
else
GreenOutXY( 65, 18, (uint8_t *)"STOP ");
dis_active_log = active_log;
}
if (p ||(dis_logging_time != logging_time)) {
sprintf((char *)text, "%6d", logging_time);
GreenOutXY( 67, 19, text);
dis_logging_time = logging_time;
}
if (p ||(dis_tray_active_log != tray_active_log)) {
if (tray_active_log == true)
RedOutXY( 65, 20, (uint8_t *)"LOGGING.");
else
GreenOutXY( 65, 20, (uint8_t *)"STOP ");
dis_tray_active_log = tray_active_log;
}
}
#endif // #ifdef CONSOLE_DEBUG
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/