2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @file dtc.cpp
|
|
|
|
* @brief Implementation of functions related to Diagnostic Trouble Codes (DTCs).
|
|
|
|
*
|
|
|
|
* This file contains the implementation of functions that manage the status
|
|
|
|
* and registration of Diagnostic Trouble Codes in the system.
|
|
|
|
*
|
|
|
|
* @author Marcel Peterkau
|
|
|
|
* @date 09.01.2024
|
|
|
|
*/
|
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
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Function implementations...
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Maintains the status of Diagnostic Trouble Codes (DTCs) in the DTCStorage array.
|
|
|
|
* Updates the status of existing DTCs or adds new ones based on their activity.
|
|
|
|
*
|
|
|
|
* @param DTC_no The number of the Diagnostic Trouble Code.
|
|
|
|
* @param active Indicates whether the DTC is active (true) or inactive (false).
|
|
|
|
* @param DebugValue Additional debugging information associated with the DTC.
|
|
|
|
*/
|
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
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Iterate through the existing DTCs in the storage
|
2022-02-10 22:32:40 +01:00
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check if the DTC with the specified number exists
|
2022-02-10 22:32:40 +01:00
|
|
|
if (DTCStorage[i].Number == DTC_no)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// If the DTC is active and was not active before, update its status
|
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
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
// If the DTC is not active anymore, update its status to previous
|
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
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
return; // DTC found and processed, exit the function
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// DTC was not found in the existing storage, but it is active,
|
|
|
|
// so look for free space to store the new DTC
|
2022-02-10 22:32:40 +01:00
|
|
|
if (active == true)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_DTC_STORAGE; i++)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check for an empty slot in the storage
|
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;
|
2024-01-09 12:54:05 +01:00
|
|
|
return; // New DTC registered, exit the function
|
2022-02-10 22:32:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Clears a specific Diagnostic Trouble Code (DTC) entry.
|
|
|
|
*
|
|
|
|
* This function clears the information related to a specific DTC entry,
|
|
|
|
* setting its status to inactive and timestamp to zero.
|
|
|
|
*
|
|
|
|
* @param DTC_no The Diagnostic Trouble Code number to be cleared.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Clears all Diagnostic Trouble Code (DTC) entries.
|
|
|
|
*
|
|
|
|
* This function clears all DTC entries, setting their status to inactive and
|
|
|
|
* timestamps to zero.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Gets the last recorded Diagnostic Trouble Code (DTC) number.
|
|
|
|
*
|
|
|
|
* This function retrieves the DTC number of the last recorded DTC based on the
|
|
|
|
* timestamp. Optionally, it can filter only active DTCs.
|
|
|
|
*
|
|
|
|
* @param only_active If true, considers only active DTCs; otherwise, considers all.
|
|
|
|
* @return The DTC number of the last recorded DTC or DTC_LAST_DTC if none found.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Gets the severity level for a specific Diagnostic Trouble Code (DTC).
|
|
|
|
*
|
|
|
|
* This function looks up the severity level associated with the provided DTC code
|
|
|
|
* from the predefined list of DTC definitions.
|
|
|
|
*
|
|
|
|
* @param targetCode The DTC code for which to retrieve the severity.
|
|
|
|
* @return The severity level of the specified DTC or DTC_NONE if not found.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Processes Diagnostic Trouble Codes (DTCs) and updates system status accordingly.
|
|
|
|
*
|
|
|
|
* This function checks for the presence of active DTCs and adjusts the system status
|
|
|
|
* based on the severity of the most critical DTC. If a critical DTC is detected,
|
|
|
|
* the system status is set to sysStat_Error, potentially triggering a system shutdown.
|
|
|
|
*
|
|
|
|
* @note The function also preserves the original system status when transitioning to an error state
|
|
|
|
* and restores it when all DTCs are cleared.
|
|
|
|
*/
|
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;
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2023-09-27 19:13:54 +02:00
|
|
|
if (globals.systemStatus == sysStat_Error)
|
|
|
|
{
|
|
|
|
globals.systemStatus = preserverSysStatusError;
|
|
|
|
}
|
2023-02-24 19:24:26 +01:00
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
}
|