2022-02-10 22:32:40 +01:00
|
|
|
#include "dtc.h"
|
2023-02-23 23:14:58 +01:00
|
|
|
#include "debugger.h"
|
2022-02-10 22:32:40 +01:00
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
DTCEntry_t DTCStorage[MAX_DTC_STORAGE];
|
2022-02-10 22:32:40 +01:00
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
void MaintainDTC(DTCNum_t DTC_no, boolean active, uint32_t DebugValue)
|
2022-02-10 22:32:40 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
|
|
|
{
|
|
|
|
if (DTCStorage[i].Number == DTC_no)
|
|
|
|
{
|
2022-03-09 20:25:02 +01:00
|
|
|
if (active && DTCStorage[i].active != DTC_ACTIVE)
|
2022-02-10 22:32:40 +01:00
|
|
|
{
|
2023-02-23 23:14:58 +01:00
|
|
|
Debug_pushMessage("DTC gone active: %d, DebugVal: %d\n", DTC_no, DebugValue);
|
2022-02-10 22:32:40 +01:00
|
|
|
DTCStorage[i].timestamp = millis();
|
|
|
|
DTCStorage[i].active = DTC_ACTIVE;
|
2022-09-01 22:46:00 +02:00
|
|
|
DTCStorage[i].debugVal = DebugValue;
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
2022-03-09 20:25:02 +01:00
|
|
|
if (!active && DTCStorage[i].active == DTC_ACTIVE)
|
2022-02-10 22:32:40 +01:00
|
|
|
{
|
2023-02-23 23:14:58 +01:00
|
|
|
Debug_pushMessage("DTC gone previous: %d\n", DTC_no);
|
2022-03-09 20:25:02 +01:00
|
|
|
DTCStorage[i].active = DTC_PREVIOUS;
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
2022-03-09 20:25:02 +01:00
|
|
|
return;
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DTC was not found with upper iteration, but is active
|
|
|
|
// so we need to look for free space to store DTC
|
|
|
|
if (active == true)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
|
|
|
{
|
2022-03-09 20:25:02 +01:00
|
|
|
if (DTCStorage[i].Number == DTC_LAST_DTC)
|
2022-02-10 22:32:40 +01:00
|
|
|
{
|
2023-02-23 23:14:58 +01:00
|
|
|
Debug_pushMessage("new DTC registered: %d, DebugVal: %d\n", DTC_no, DebugValue);
|
2022-03-09 20:25:02 +01:00
|
|
|
DTCStorage[i].Number = DTC_no;
|
2022-02-10 22:32:40 +01:00
|
|
|
DTCStorage[i].timestamp = millis();
|
|
|
|
DTCStorage[i].active = DTC_ACTIVE;
|
2022-09-01 22:46:00 +02:00
|
|
|
DTCStorage[i].debugVal = DebugValue;
|
2022-02-10 22:32:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
void ClearDTC(DTCNum_t DTC_no)
|
2022-02-10 22:32:40 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
|
|
|
{
|
|
|
|
if (DTCStorage[i].Number == DTC_no)
|
|
|
|
{
|
2022-03-09 20:25:02 +01:00
|
|
|
DTCStorage[i].Number = DTC_LAST_DTC;
|
2023-09-27 19:13:54 +02:00
|
|
|
DTCStorage[i].active = DTC_INACTIVE;
|
2022-03-09 20:25:02 +01:00
|
|
|
DTCStorage[i].timestamp = 0;
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearAllDTC()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
2022-03-09 20:25:02 +01:00
|
|
|
{
|
|
|
|
DTCStorage[i].Number = DTC_LAST_DTC;
|
2023-09-27 19:13:54 +02:00
|
|
|
DTCStorage[i].active = DTC_INACTIVE;
|
2022-03-09 20:25:02 +01:00
|
|
|
DTCStorage[i].timestamp = 0;
|
|
|
|
}
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
DTCNum_t getlastDTC(boolean only_active)
|
2022-02-10 22:32:40 +01:00
|
|
|
{
|
2022-03-09 20:25:02 +01:00
|
|
|
int8_t pointer = -1;
|
2022-02-10 22:32:40 +01:00
|
|
|
uint32_t lasttimestamp = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
|
|
|
{
|
|
|
|
if (DTCStorage[i].Number > 0 && DTCStorage[i].timestamp > lasttimestamp)
|
|
|
|
{
|
|
|
|
if (only_active == false || DTCStorage[i].active == DTC_ACTIVE)
|
|
|
|
{
|
|
|
|
pointer = i;
|
|
|
|
lasttimestamp = DTCStorage[i].timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:13:55 +02:00
|
|
|
return pointer >= 0 ? DTCStorage[pointer].Number : DTC_LAST_DTC;
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
DTCSeverity_t getSeverityForDTC(DTCNum_t targetCode)
|
2022-08-22 14:13:55 +02:00
|
|
|
{
|
2023-09-27 19:13:54 +02:00
|
|
|
for (int i = 0; i < DTC_LAST_DTC; i++)
|
2022-08-22 14:13:55 +02:00
|
|
|
{
|
2023-09-27 19:13:54 +02:00
|
|
|
if (dtc_definitions[i].code == targetCode)
|
2022-08-22 14:13:55 +02:00
|
|
|
{
|
2023-09-27 19:13:54 +02:00
|
|
|
return dtc_definitions[i].severity;
|
2022-08-22 14:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 19:13:54 +02:00
|
|
|
return DTC_NONE;
|
2023-02-24 19:24:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DTC_Process()
|
|
|
|
{
|
|
|
|
static tSystem_Status preserverSysStatusError;
|
2023-09-27 19:13:54 +02:00
|
|
|
DTCNum_t lastDTC = getlastDTC(true);
|
2023-02-24 19:24:26 +01:00
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
if (lastDTC < DTC_LAST_DTC)
|
2023-02-24 19:24:26 +01:00
|
|
|
{
|
|
|
|
globals.hasDTC = true;
|
2023-09-27 19:13:54 +02:00
|
|
|
|
|
|
|
if (getSeverityForDTC(lastDTC) == DTC_CRITICAL && globals.systemStatus != sysStat_Shutdown)
|
2023-02-24 19:24:26 +01:00
|
|
|
{
|
|
|
|
if (globals.systemStatus != sysStat_Error)
|
|
|
|
{
|
|
|
|
preserverSysStatusError = globals.systemStatus;
|
|
|
|
}
|
|
|
|
globals.systemStatus = sysStat_Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
globals.hasDTC = false;
|
2023-09-27 19:13:54 +02:00
|
|
|
|
|
|
|
if (globals.systemStatus == sysStat_Error)
|
|
|
|
{
|
|
|
|
globals.systemStatus = preserverSysStatusError;
|
|
|
|
}
|
2023-02-24 19:24:26 +01:00
|
|
|
}
|
2022-03-09 20:25:02 +01:00
|
|
|
}
|