NULL
This commit is contained in:
426
user/Compute/SavingData.c
Executable file
426
user/Compute/SavingData.c
Executable file
@@ -0,0 +1,426 @@
|
||||
#include "../main/SystemInclude.h"
|
||||
/******************************************************************************
|
||||
recordTime must be define
|
||||
*******************************************************************************/
|
||||
// for save data
|
||||
u16 historyPeriodSet;
|
||||
static u16 lastState;
|
||||
u16 saveTimeCounter;
|
||||
|
||||
/******************************************************************************/
|
||||
void SaveParameterInit(void)
|
||||
{
|
||||
// history record period
|
||||
recordTime = (u16)ReadShortParameterFromMemory(RECORD_TIME, RECORD_TIME_DEFAULT);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void CopyTimeToBackUpArray(u8 *backups)
|
||||
{
|
||||
backups[0] = timer[YEAR];
|
||||
backups[1] = timer[MONTH];
|
||||
backups[2] = timer[DATE];
|
||||
backups[3] = timer[HOUR];
|
||||
backups[4] = timer[MINUTE];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void CopyAccToBackUpArray(u8 *backups)
|
||||
{
|
||||
backups[5] = Total[0];
|
||||
backups[6] = Total[1];
|
||||
backups[7] = Total[2];
|
||||
backups[8] = Total[3];
|
||||
backups[9] = Total[4];
|
||||
backups[10] = Total[5];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 WriteaAnAlarmData(u16 pointer)
|
||||
{
|
||||
u8 temp[ALARM_WIDTH];
|
||||
|
||||
pointer = ALARM_BASE + pointer * ALARM_WIDTH;
|
||||
|
||||
CopyTimeToBackUpArray(temp);
|
||||
CopyAccToBackUpArray(&temp[5]); // 6 Byte
|
||||
//------------------------------------------------------------------------------
|
||||
tempL.Word[0] = alarmState;
|
||||
temp[11] = tempL.Byte[1];
|
||||
temp[12] = tempL.Byte[0];
|
||||
|
||||
tempL.Word[0] = voltageDetected[NIDX];
|
||||
temp[13] = tempL.Byte[1];
|
||||
temp[14] = tempL.Byte[0];
|
||||
|
||||
tempL.Word[0] = voltageDetected[RHRES];
|
||||
temp[15] = tempL.Byte[1];
|
||||
temp[16] = tempL.Byte[0];
|
||||
|
||||
tempL.Word[0] = voltageDetected[RHVOL];
|
||||
temp[17] = tempL.Byte[1];
|
||||
temp[18] = tempL.Byte[0];
|
||||
|
||||
tempL.Word[0] = voltageDetected[VHPA];
|
||||
temp[19] = tempL.Byte[1];
|
||||
temp[20] = tempL.Byte[0];
|
||||
|
||||
tempL.Word[0] = voltageDetected[IVHDX];
|
||||
temp[21] = tempL.Byte[1];
|
||||
temp[22] = tempL.Byte[0];
|
||||
|
||||
tempL.Word[0] = voltageDetected[BATT];
|
||||
temp[23] = tempL.Byte[1];
|
||||
temp[24] = tempL.Byte[0];
|
||||
//------------------------------------------------------------------------------
|
||||
temp[ALARM_WIDTH_CRC] = CRC8(temp, ALARM_WIDTH_CRC);
|
||||
|
||||
WriteMultiByteToEEPROM(pointer, temp, ALARM_WIDTH, DATA_EEPROM);
|
||||
//ReadMultiByteFromEEPROM(pointer, buffer, ALARM_WIDTH, DATA_EEPROM);
|
||||
|
||||
//for(pointer=0; pointer<ALARM_WIDTH; pointer++)
|
||||
//{
|
||||
// if(buffer[pointer] != temp[pointer]) return 1;
|
||||
//}
|
||||
|
||||
return 0;
|
||||
}
|
||||
/******************************************************************************/
|
||||
// write alarm data state
|
||||
u16 AlarmWrite(void)
|
||||
{
|
||||
|
||||
// write alarm information into external EEPROM
|
||||
// input: date, alarmState, VL, VH, AlarmPointer
|
||||
// output: AlarmPointer
|
||||
|
||||
#ifndef REC_ALARM_DATA
|
||||
#pragma message("[undefined] REC_ALARM_DATA")
|
||||
#elif(REC_ALARM_DATA)
|
||||
|
||||
// The Zero record: Factory initial state
|
||||
ReadMultiByteFromEEPROM(ALARM_POINTER, tempL.Byte, ALARM_POINTER_WIDTH, DATA_EEPROM);
|
||||
if(tempL.Byte[ALARM_POINTER_CRC] != CRC8(tempL.Byte, ALARM_POINTER_CRC)) tempL.Word[0] = 1;
|
||||
else if(tempL.Word[0] >= ALARM_DEPTH) tempL.Word[0] = 1;
|
||||
else if(tempL.Word[0] == 0) tempL.Word[0] = 1;
|
||||
else tempL.Word[0]++;
|
||||
|
||||
tempL.Byte[ALARM_POINTER_CRC] = CRC8(tempL.Byte, ALARM_POINTER_CRC);
|
||||
WriteMultiByteToEEPROM(ALARM_POINTER, tempL.Byte, ALARM_POINTER_WIDTH, DATA_EEPROM);
|
||||
|
||||
if(WriteaAnAlarmData(tempL.Word[0])) return 1;
|
||||
else return 0;
|
||||
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
/******************************************************************************/
|
||||
void SetHistoryRecordTime(void)
|
||||
{
|
||||
/*
|
||||
u16 wrt_base, pointer;
|
||||
|
||||
ReadMultiByteFromEEPROM(HISTORY_POINTER_H, temp, 2, DATA_EEPROM);
|
||||
pointer = make16(temp[0], temp[1]);
|
||||
|
||||
historyPeriodSet = (unsigned int)RTCHOUR;
|
||||
historyPeriodSet *= 60;
|
||||
historyPeriodSet += (unsigned int)RTCMIN;
|
||||
if(pointer >= HISTORY_DEPTH) { historyPeriodSet++; return; }
|
||||
|
||||
wrt_base = HISTORY_BASE + pointer * HISTORY_WIDTH;
|
||||
ReadMultiByteFromEEPROM(wrt_base, timer, 5, DATA_EEPROM);
|
||||
|
||||
wrt_base = (unsigned int)timer[HOUR];
|
||||
wrt_base *= 60;
|
||||
wrt_base += (unsigned int)timer[MINUTE];
|
||||
wrt_base += historyPeriod;
|
||||
if(wrt_base >= 1440) { historyPeriodSet = 0; return;}
|
||||
if(historyPeriodSet < wrt_base) historyPeriodSet = wrt_base;
|
||||
*/
|
||||
historyPeriodSet = 0xffff;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 WriteAnHistoryData(u16 pointer)
|
||||
{
|
||||
u8 temp[HISTORY_WIDTH];
|
||||
|
||||
pointer = HISTORY_BASE + tempL.Word[0] * HISTORY_WIDTH;
|
||||
|
||||
CopyTimeToBackUpArray(temp); // 5 Byte
|
||||
CopyAccToBackUpArray(&temp[5]); // 6 Byte
|
||||
|
||||
tempL.Word[0] = alarmState;
|
||||
temp[11] = tempL.Byte[1];
|
||||
temp[12] = tempL.Byte[0];
|
||||
|
||||
tempL.DWord = flowRate;
|
||||
temp[13] = tempL.Byte[2];
|
||||
temp[14] = tempL.Byte[1];
|
||||
temp[15] = tempL.Byte[0];
|
||||
|
||||
temp[HISTORY_WIDTH_CRC] = CRC8(temp, HISTORY_WIDTH_CRC);
|
||||
|
||||
WriteMultiByteToEEPROM(pointer, temp, HISTORY_WIDTH, DATA_EEPROM);
|
||||
// ReadMultiByteFromEEPROM(pointer, buffer, HISTORY_WIDTH, DATA_EEPROM);
|
||||
//
|
||||
// for(pointer=0; pointer<HISTORY_WIDTH; pointer++)
|
||||
// {
|
||||
// if(buffer[pointer] != temp[pointer]) return 1;
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 HistoryRecordWrite(void)
|
||||
{
|
||||
#ifndef REC_HISTORY_DATA
|
||||
#pragma message("[undefined] REC_HISTORY_DATA")
|
||||
#elif(REC_HISTORY_DATA)
|
||||
|
||||
ReadMultiByteFromEEPROM(HISTORY_POINTER, tempL.Byte, HISTORY_POINTER_WIDTH, DATA_EEPROM);
|
||||
if(tempL.Byte[HISTORY_POINTER_CRC] != CRC8(tempL.Byte, HISTORY_POINTER_CRC)) tempL.Word[0] = 1;
|
||||
else if(tempL.Word[0] >= HISTORY_DEPTH) tempL.Word[0] = 1;
|
||||
else if(tempL.Word[0] == 0) tempL.Word[0] = 1;
|
||||
else tempL.Word[0]++;
|
||||
|
||||
tempL.Byte[HISTORY_POINTER_CRC] = CRC8(tempL.Byte, HISTORY_POINTER_CRC);
|
||||
WriteMultiByteToEEPROM(HISTORY_POINTER, tempL.Byte, HISTORY_POINTER_WIDTH, DATA_EEPROM);
|
||||
if(WriteAnHistoryData(tempL.Word[0])) return 1;
|
||||
else return 0;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 ReadRecentHistoryData(u8 *recent)
|
||||
{
|
||||
ReadMultiByteFromEEPROM(HISTORY_POINTER, tempL.Byte, HISTORY_POINTER_WIDTH, DATA_EEPROM);
|
||||
if(tempL.Byte[HISTORY_POINTER_CRC] != CRC8(tempL.Byte, HISTORY_POINTER_CRC)) return 0;
|
||||
|
||||
tempL.Word[0] = HISTORY_BASE + tempL.Word[0] * HISTORY_WIDTH;
|
||||
ReadMultiByteFromEEPROM(tempL.Word[0], recent, HISTORY_WIDTH, DATA_EEPROM);
|
||||
|
||||
if(recent[HISTORY_WIDTH_CRC] != CRC8(recent, HISTORY_WIDTH_CRC)) return 0;
|
||||
else return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// write history record
|
||||
// record: 2000
|
||||
// format: year-month-date-hour-min-second-ACC-FLOW-STATE
|
||||
/******************************************************************************/
|
||||
void WriteRecordTimeData(void)
|
||||
{
|
||||
u8 temp[RECORD_TIME_WIDTH];
|
||||
|
||||
CopyTimeToBackUpArray(temp);
|
||||
temp[RECORD_TIME_CRC] = CRC8(temp, RECORD_TIME_CRC);
|
||||
WriteMultiByteToEEPROM(HISTORY_BASE, temp, RECORD_TIME_WIDTH, DATA_EEPROM);
|
||||
|
||||
saveTimeCounter = 0;
|
||||
HWState.EnableSaveData = 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// write date acc every day
|
||||
/******************************************************************************/
|
||||
u16 ReadRecentDateAccData(u8 *recent)
|
||||
{
|
||||
ReadMultiByteFromEEPROM(DATE_POINTER, tempL.Byte, DATE_POINTER_WIDTH, DATA_EEPROM);
|
||||
if(tempL.Byte[DATE_POINTER_CRC] != CRC8(tempL.Byte, DATE_POINTER_CRC)) return 0;
|
||||
if(tempL.Word[0] >= DATE_DEPTH) return 0;
|
||||
|
||||
tempL.Word[0] = DATE_BASE + tempL.Word[0] * DATE_WIDTH;
|
||||
ReadMultiByteFromEEPROM(tempL.Word[0], recent, DATE_WIDTH, DATA_EEPROM);
|
||||
|
||||
if(recent[DATE_WIDTH_CRC] != CRC8(recent, DATE_WIDTH_CRC)) return 0;
|
||||
else return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u16 DateAccWrite(void)
|
||||
{
|
||||
u8 temp[DATE_WIDTH];
|
||||
|
||||
// write date and accumulation into external EEPROM and update dateAccPointer
|
||||
// input: date, flowAcc, dateAccPointer
|
||||
// output: dateAccPointer
|
||||
|
||||
#ifndef REC_DATE_DATA
|
||||
#pragma message("[undefined] REC_DATE_DATA")
|
||||
#elif(REC_DATE_DATA)
|
||||
u16 wrt_base;
|
||||
unsigned char buffer[DATE_WIDTH];
|
||||
|
||||
ReadMultiByteFromEEPROM(DATE_POINTER, tempL.Byte, DATE_POINTER_WIDTH, DATA_EEPROM);
|
||||
if(tempL.Byte[DATE_POINTER_CRC] != CRC8(tempL.Byte, DATE_POINTER_CRC)) tempL.Word[0] = 1;
|
||||
if(tempL.Word[0] >= DATE_DEPTH) tempL.Word[0] = 1;
|
||||
else if(tempL.Word[0] >= DATE_DEPTH) tempL.Word[0] = 1;
|
||||
else tempL.Word[0]++;
|
||||
|
||||
tempL.Byte[DATE_POINTER_CRC] = CRC8(tempL.Byte, DATE_POINTER_CRC);
|
||||
WriteMultiByteToEEPROM(DATE_POINTER, tempL.Byte, DATE_POINTER_WIDTH, DATA_EEPROM);
|
||||
|
||||
wrt_base = DATE_BASE + tempL.Word[0] * DATE_WIDTH;
|
||||
|
||||
CopyTimeToBackUpArray(temp);
|
||||
CopyAccToBackUpArray(&temp[5]); // 6 Byte
|
||||
temp[DATE_WIDTH_CRC] = CRC8(temp, DATE_WIDTH_CRC);
|
||||
|
||||
WriteMultiByteToEEPROM(wrt_base, temp, DATE_WIDTH, DATA_EEPROM);
|
||||
ReadMultiByteFromEEPROM(wrt_base, buffer, DATE_WIDTH, DATA_EEPROM);
|
||||
for(wrt_base=0; wrt_base<DATE_WIDTH; wrt_base++)
|
||||
{
|
||||
if(buffer[wrt_base] != temp[wrt_base]) return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void ClearAlarmRecord(void)
|
||||
{
|
||||
u16 I, wrtBase;
|
||||
|
||||
wrtBase = ALARM_BASE + ALARM_DEPTH*ALARM_WIDTH;
|
||||
I = ALARM_BASE + ALARM_WIDTH;
|
||||
while(1)
|
||||
{
|
||||
if(wrtBase > I+100)
|
||||
{
|
||||
ClearMultiByteToEEPROM(I, 100, DATA_EEPROM);
|
||||
I += 100;
|
||||
}
|
||||
else
|
||||
{
|
||||
wrtBase -= I;
|
||||
ClearMultiByteToEEPROM(I, wrtBase, DATA_EEPROM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tempL.Word[0] = 0;
|
||||
tempL.Byte[ALARM_POINTER_CRC] = CRC8(tempL.Byte, ALARM_POINTER_CRC);
|
||||
WriteMultiByteToEEPROM(ALARM_POINTER, tempL.Byte, ALARM_POINTER_WIDTH, DATA_EEPROM);
|
||||
|
||||
}
|
||||
/******************************************************************************/
|
||||
void ClearHistoryRecord(void)
|
||||
{
|
||||
u16 I, wrtBase;
|
||||
|
||||
wrtBase = HISTORY_BASE + HISTORY_DEPTH*HISTORY_WIDTH;
|
||||
I = HISTORY_BASE + HISTORY_WIDTH;
|
||||
while(1)
|
||||
{
|
||||
if(wrtBase > I+100)
|
||||
{
|
||||
ClearMultiByteToEEPROM(I, 100, DATA_EEPROM);
|
||||
I += 100;
|
||||
}
|
||||
else
|
||||
{
|
||||
wrtBase -= I;
|
||||
ClearMultiByteToEEPROM(I, wrtBase, DATA_EEPROM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tempL.Word[0] = 0;
|
||||
tempL.Byte[HISTORY_POINTER_CRC] = CRC8(tempL.Byte, HISTORY_POINTER_CRC);
|
||||
WriteMultiByteToEEPROM(HISTORY_POINTER, tempL.Byte, HISTORY_POINTER_WIDTH, DATA_EEPROM);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void ClearDateRecord(void)
|
||||
{
|
||||
u16 I, wrtBase;
|
||||
|
||||
wrtBase = DATE_BASE + DATE_DEPTH*DATE_WIDTH;
|
||||
I = DATE_BASE + DATE_WIDTH;
|
||||
|
||||
while(1)
|
||||
{
|
||||
if(wrtBase > I+100)
|
||||
{
|
||||
ClearMultiByteToEEPROM(I, 100, DATA_EEPROM);
|
||||
I += 100;
|
||||
}
|
||||
else
|
||||
{
|
||||
wrtBase -= I;
|
||||
ClearMultiByteToEEPROM(I, wrtBase, DATA_EEPROM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tempL.Word[0] = 0;
|
||||
tempL.Byte[DATE_POINTER_CRC] = CRC8(tempL.Byte, DATE_POINTER_CRC);
|
||||
WriteMultiByteToEEPROM(DATE_POINTER, tempL.Byte, DATE_POINTER_WIDTH, DATA_EEPROM);
|
||||
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void DataStorageManagement(void)
|
||||
{
|
||||
systemProcessing.Bit.SaveHandle = 0;
|
||||
|
||||
if(alarmState != lastState)
|
||||
{
|
||||
lastState = alarmState;
|
||||
disable_interrupts();
|
||||
if(AlarmWrite()) systemAlarm.Bit.EEPROMAlarm = 1;
|
||||
else systemAlarm.Bit.EEPROMAlarm = 0;
|
||||
enable_interrupts();
|
||||
}
|
||||
|
||||
// save history data
|
||||
if(HWState.EnableSaveHistoryData)
|
||||
{
|
||||
disable_interrupts();
|
||||
if(HistoryRecordWrite()) systemAlarm.Bit.EEPROMAlarm = 1;
|
||||
else systemAlarm.Bit.EEPROMAlarm = 0;
|
||||
historyPeriodSet = 0;
|
||||
HWState.EnableSaveHistoryData = 0;
|
||||
enable_interrupts();
|
||||
}
|
||||
|
||||
// save acc data
|
||||
if(HWState.EnableSaveDateAcc)
|
||||
{
|
||||
disable_interrupts();
|
||||
if(DateAccWrite()) systemAlarm.Bit.EEPROMAlarm = 1;
|
||||
else systemAlarm.Bit.EEPROMAlarm = 0;
|
||||
HWState.EnableSaveDateAcc = 0;
|
||||
enable_interrupts();
|
||||
}
|
||||
|
||||
if(HWState.EnableSaveRTC)
|
||||
{
|
||||
disable_interrupts();
|
||||
WriteRecordTimeData();
|
||||
enable_interrupts();
|
||||
}
|
||||
|
||||
HWState.EnableSaveData = 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void ReadRecordToComBuf(void)
|
||||
{
|
||||
disable_interrupts();
|
||||
ReadMultiByteFromEEPROM(MBBuf.StartAddr, &MBBuf.RxPointer[3], MBBuf.ByteNumber, DATA_EEPROM);
|
||||
MBBuf.ByteNumber -= 1;
|
||||
|
||||
if(MBBuf.RxPointer[3+MBBuf.ByteNumber] == CRC8(&MBBuf.RxPointer[3], MBBuf.ByteNumber)) MBBuf.RxPointer[3+MBBuf.ByteNumber] = 0xaa;
|
||||
enable_interrupts();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user