/**
******************************************************************************
* @file log.c
* @author MCD Application Team
* @version V1.0.0
* @date 10-October-2011
* @brief This file provides all the common 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.
*
*
© COPYRIGHT 2011 STMicroelectronics
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include
#include
#include
#include "brtc.h"
#include "ff.h"
#include "ff_shell.h"
#include "diskio.h"
#include "usart.h"
#include "exfuns.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 {
u32 timestamp;
struct {
u16 cv[8];
} tray[30];
} TTrayVoltLog, *PTrayVoltLog;
/* 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 u8 active_log = 0;
static u8 logging_time = 5; // Second
static u32 logging_tick = 0;
static u8 tray_active_log = false;
#ifdef CONSOLE_DEBUG
static u8 dis_active_log = 0;
static u8 dis_logging_time; // Second
static u8 dis_tray_active_log = 0;
#endif // #ifdef CONSOLE_DEBUG
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void LogWrite(BYTE *ldata, DWORD size)
{
FRESULT rc;
char *filename;
char *txt;
filename = mymalloc(50);
if (filename == NULL) return;
sprintf((char *)filename, "%s/CycleLogNow",
LOG_FILE_DIR
);
rc = FileCheck(filename);
if (rc == FR_NO_PATH)
{
rc = DirCreate(LOG_FILE_DIR);
rc = FileCreate(filename);
if (rc == FR_OK)
{
txt = mymalloc(512);
if (txt != NULL)
{
sprintf(txt, "%s", LOG_HEADER);
FileWrite(filename, (BYTE *)txt, 0, (DWORD)strlen(txt));
myfree(txt);
}
}
}
else if (rc == FR_NO_FILE)
{
rc = FileCreate(filename);
if (rc == FR_OK)
{
txt = mymalloc(512);
if (txt != NULL)
{
sprintf(txt, "%s", LOG_HEADER);
FileWrite(filename, (BYTE *)txt, 0, (DWORD)strlen(txt));
myfree(txt);
}
}
}
else if (rc == FR_OK)
{
rc = FileWrite(filename, ldata, 0xffffffff, size);
}
myfree(filename);
}
void LogWriteTrayV(BYTE *ldata, DWORD size)
{
FRESULT rc;
rc = FileCheck(CELL_VOLT_LOG_FILE);
if (rc == FR_NO_FILE)
{
rc = FileCreate(CELL_VOLT_LOG_FILE);
if (rc == FR_OK)
{
printf("Created %s\r\n", CELL_VOLT_LOG_FILE);
}
}
else if (rc == FR_OK)
{
// printf("Write %d \r\n", (u32)size);
rc = FileWrite(CELL_VOLT_LOG_FILE, ldata, 0xffffffff, size);
}
}
#ifdef CONSOLE_DEBUG
void LogPrint(const TCHAR *fname)
{
FRESULT rc;
u8 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 {
IWDG_ReloadCounter();
err++;
if (rc == FR_LOCKED)
delay_os_ms(500);
if (err > 5)
eflag = 1;
}
/* Reload IWDG counter */
IWDG_ReloadCounter();
delay_os_ms(1);
} while (eflag == 0);
printf("\r\nFile Read End (total: %d)\r\n", (u32)fOffset);
myfree(txt);
}
}
void TrayLogPrint(void)
{
FRESULT rc;
u8 eflag = 0;
DWORD fOffset = 0;
UINT rlen = 0, err = 0;
PTrayVoltLog data;
char *txt;
_TTIME time;
data = mymalloc(sizeof(TTrayVoltLog));
if (data != NULL) {
printf("\r\n");
do {
rc = FileRead(CELL_VOLT_LOG_FILE, (BYTE *)data, fOffset, sizeof(TTrayVoltLog), &rlen);
if (rc == FR_OK) {
err = 0;
if (rlen < sizeof(TTrayVoltLog)) {
eflag = 1;
} else if (rlen == sizeof(TTrayVoltLog)) {
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 {
IWDG_ReloadCounter();
err++;
if (rc == FR_LOCKED)
delay_os_ms(500);
if (err > 5)
eflag = 1;
}
/* Reload IWDG counter */
IWDG_ReloadCounter();
delay_os_ms(1);
} while (eflag == 0);
printf("\r\nFile Read End (total: %d)\r\n", (u32)fOffset);
myfree(txt);
}
}
void LogInterval(u8 *time)
{
u16 size, i, j;
s16 aParam;
u8 temp[10];
size = 0;
while (time[size] != 0x00) size++; /* Measure total string length */
i = 0;
if ((size == 0)||(size > 3)) return;
j = FIND_CHAR((u8 *)&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)
{
u16 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);
int j = 0;
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
sprintf(&txt[i], "%5.3f,", (float)device_value.Cell.voltage[j++] / 1000); i = strlen(txt);
j = 0;
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
sprintf(&txt[i], "%5.1f,", (float)device_value.Temp.temperature[j++] / 10); i = strlen(txt);
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.protection.value); i = strlen(txt);
sprintf(&txt[i], "\r\n"); i = strlen(txt);
LogWrite((BYTE *)txt, (DWORD)i);
myfree(txt);
}
}
void LogBodyTrayV(void)
{
// u16 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 TrayLogEnable(u16 time)
{
if (tray_active_log) {
UserMessage("[LOGGING]: TRAY LOG ALREADY START !!");
} else {
tray_active_log = true;
UserMessage("[LOGGING]: TRAY LOG START COMPLETE !!");
}
}
void TrayLogDisable(void)
{
if (tray_active_log) {
UserMessage("[LOGGING]: TRAY LOG STOP COMPLETE !!");
} else {
UserMessage("[LOGGING]: TRAY LOG ALREADY STOP !!");
}
tray_active_log = false;
}
void TrayLogDelete(void)
{
if (tray_active_log == true) {
UserMessage("Logging.... cannot delete tray log");
} else {
f_unlink(CELL_VOLT_LOG_FILE);
}
}
void LogEnable(u16 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 LogDelete(void)
{
TCHAR filename[30];
if (active_log == 1) {
UserMessage("Now Logging....");
} else {
sprintf((char *)filename, "%s/CycleLogNow",
LOG_FILE_DIR
);
f_unlink(filename);
}
}
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)
LogBodyTrayV();
logging_tick = GetTimeStamp();
}
}
}
}
#ifdef CONSOLE_DEBUG
void DisplayLogStatus(u8 p)
{
u8 text[20];
if (p ||(dis_active_log != active_log)) {
if (active_log == true)
RedOutXY( 65, 18, (u8 *)"LOGGING.");
else
GreenOutXY( 65, 18, (u8 *)"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, (u8 *)"LOGGING.");
else
GreenOutXY( 65, 20, (u8 *)"STOP ");
dis_tray_active_log = tray_active_log;
}
}
#endif // #ifdef CONSOLE_DEBUG