-
Notifications
You must be signed in to change notification settings - Fork 56
/
FaultHandler.cpp
227 lines (197 loc) · 7.07 KB
/
FaultHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* FaultHandler.cpp
*
Copyright (c) 2013 Collin Kidder, Michael Neuweiler, Charles Galpin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "FaultHandler.h"
#include "eeprom_layout.h"
FaultHandler::FaultHandler()
{
}
void FaultHandler::setup()
{
TickHandler::getInstance()->detach(this);
Logger::info("Initializing Fault Handler", FAULTSYS, this);
loadFromEEPROM();
//Use the heartbeat interval because it's slow and already exists so we can piggyback on the interrupt
//so as to not create more timers than necessary.
TickHandler::getInstance()->attach(this, CFG_TICK_INTERVAL_HEARTBEAT);
}
//Every tick update the global time and save it to EEPROM (delayed saving)
void FaultHandler::handleTick()
{
globalTime = baseTime + (millis() / 100);
memCache->Write(EE_FAULT_LOG + EEFAULT_RUNTIME, globalTime);
}
uint16_t FaultHandler::raiseFault(uint16_t device, uint16_t code, bool ongoing = false)
{
bool incPtr = false;
globalTime = baseTime + (millis() / 100);
//first try to see if this fault is already registered as ongoing. If so don't update the time but set ongoing status if necessary
bool found = false;
for (int j = 0; j < CFG_FAULT_HISTORY_SIZE; j++)
{
if (faultList[j].ongoing && faultList[j].device == device && faultList[j].faultCode == code)
{
found = true;
//faultList[j].timeStamp = globalTime;
faultList[j].ongoing = ongoing;
break; //quit searching
}
}
//nothing ongoing to register a new one
if (!found)
{
faultList[faultWritePointer].timeStamp = globalTime;
faultList[faultWritePointer].ack = false;
faultList[faultWritePointer].device = device;
faultList[faultWritePointer].faultCode = code;
faultList[faultWritePointer].ongoing = ongoing;
incPtr = true;
}
//write back to EEPROM cache
memCache->Write(EE_FAULT_LOG + EEFAULT_FAULTS_START + sizeof(FAULT) * faultWritePointer, &faultList[faultWritePointer], sizeof(FAULT));
if (incPtr)
{
//Cause the memory caching system to immediately write the page but only if incPtr is set (only if this is a new fault)
memCache->InvalidateAddress(EE_FAULT_LOG + EEFAULT_FAULTS_START + sizeof(FAULT)* faultWritePointer);
faultWritePointer = (faultWritePointer + 1) % CFG_FAULT_HISTORY_SIZE;
memCache->Write(EE_FAULT_LOG + EEFAULT_WRITEPTR , faultWritePointer);
//Cause the page to be immediately fully aged so that it is written very soon.
memCache->AgeFullyAddress(EE_FAULT_LOG + EEFAULT_WRITEPTR);
//Also announce fault on the console
Logger::error(FAULTSYS, "Fault %x raised by device %x at uptime %i", code, device, globalTime);
}
}
void FaultHandler::cancelOngoingFault(uint16_t device, uint16_t code)
{
for (int i = 0; i < CFG_FAULT_HISTORY_SIZE; i++)
{
if (faultList[i].ongoing && faultList[i].device == device && faultList[i].faultCode == code)
{
setFaultOngoing(i, false);
}
}
}
uint16_t FaultHandler::getFaultCount()
{
int count = 0;
for (int i = 0; i < CFG_FAULT_HISTORY_SIZE; i++)
{
if (faultList[i].ack == false && faultList[i].device != 0xFFFF)
{
count++;
}
}
return count;
}
//the fault handler isn't a device per se and uses more memory than a device would normally be allocated so
//it does not use PrefHandler
void FaultHandler::loadFromEEPROM()
{
uint8_t validByte;
memCache->Read(EE_FAULT_LOG, &validByte);
if (validByte == 0xB2) //magic byte value for a valid fault cache
{
memCache->Read(EE_FAULT_LOG + EEFAULT_READPTR, &faultReadPointer);
memCache->Read(EE_FAULT_LOG + EEFAULT_WRITEPTR, &faultWritePointer);
memCache->Read(EE_FAULT_LOG + EEFAULT_RUNTIME, &globalTime);
baseTime = globalTime;
for (int i = 0; i < CFG_FAULT_HISTORY_SIZE; i++)
{
memCache->Read(EE_FAULT_LOG + EEFAULT_FAULTS_START + sizeof(FAULT) * i, &faultList[i], sizeof(FAULT));
}
}
else //reinitialize the fault cache storage
{
validByte = 0xB2;
memCache->Write(EE_FAULT_LOG, validByte);
memCache->Write(EE_FAULT_LOG + EEFAULT_READPTR, (uint16_t)0);
memCache->Write(EE_FAULT_LOG + EEFAULT_WRITEPTR, (uint16_t)0);
globalTime = baseTime = millis() / 100;
memCache->Write(EE_FAULT_LOG + EEFAULT_RUNTIME, globalTime);
FAULT tempFault;
tempFault.ack = true;
tempFault.device = 0xFFFF;
tempFault.faultCode = 0xFFFF;
tempFault.ongoing = false;
tempFault.timeStamp = 0;
for (int i = 0; i < CFG_FAULT_HISTORY_SIZE; i++)
{
faultList[i] = tempFault;
}
saveToEEPROM();
}
}
void FaultHandler::saveToEEPROM()
{
memCache->Write(EE_FAULT_LOG + EEFAULT_READPTR, faultReadPointer);
memCache->Write(EE_FAULT_LOG + EEFAULT_WRITEPTR, faultWritePointer);
memCache->Write(EE_FAULT_LOG + EEFAULT_RUNTIME, globalTime);
for (int i = 0; i < CFG_FAULT_HISTORY_SIZE; i++)
{
memCache->Write(EE_FAULT_LOG + EEFAULT_FAULTS_START + sizeof(FAULT) * i, &faultList[i], sizeof(FAULT));
}
}
void FaultHandler::writeFaultToEEPROM(int faultnum)
{
if (faultnum > 0 && faultnum < CFG_FAULT_HISTORY_SIZE)
{
memCache->Write(EE_FAULT_LOG + EEFAULT_FAULTS_START + sizeof(FAULT) * faultnum, &faultList[faultnum], sizeof(FAULT));
}
}
bool FaultHandler::getNextFault(FAULT *fault)
{
uint16_t j;
for (int i = 0; i < CFG_FAULT_HISTORY_SIZE; i++)
{
j = (faultReadPointer + i + 1) % CFG_FAULT_HISTORY_SIZE;
if (faultList[j].ack == false) {
fault = &faultList[j];
faultReadPointer = j;
return true;
}
}
return false;
}
bool FaultHandler::getFault(uint16_t fault, FAULT *outFault)
{
if (fault > 0 && fault < CFG_FAULT_HISTORY_SIZE) {
outFault = &faultList[fault];
return true;
}
return false;
}
uint16_t FaultHandler::setFaultACK(uint16_t fault)
{
if (fault > 0 && fault < CFG_FAULT_HISTORY_SIZE)
{
faultList[fault].ack = 1;
writeFaultToEEPROM(fault);
}
}
uint16_t FaultHandler::setFaultOngoing(uint16_t fault, bool ongoing)
{
if (fault > 0 && fault < CFG_FAULT_HISTORY_SIZE)
{
faultList[fault].ongoing = ongoing;
writeFaultToEEPROM(fault);
}
}
FaultHandler faultHandler;