Index: firmware/App/Services/PersistentAlarm.c =================================================================== diff -u --- firmware/App/Services/PersistentAlarm.c (revision 0) +++ firmware/App/Services/PersistentAlarm.c (revision 4d47390287ee4870b048bcc015398f0f56e33d75) @@ -0,0 +1,174 @@ +/************************************************************************** +* +* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* +* THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN +* WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. +* +* @file PersistentAlarm.c +* +* @author (last) Quang Nguyen +* @date (last) 12-Aug-2020 +* +* @author (original) Quang Nguyen +* @date (original) 12-Aug-2020 +* +***************************************************************************/ + +#include "AlarmMgmt.h" +#include "PersistentAlarm.h" + +/** + * @addtogroup PersistentAlarm + * @{ + */ + +// ********** private definitions ********** + +#define MAX_NUMBER_OF_PERSISTENT_ALARM 10 ///< Maximum number of persistent alarm allowed + +/// Persistent alarm structure +typedef struct +{ + ALARM_ID_T alarm; ///< Alarm id + ALARM_DATA_TYPES_T alarmType; ///< Alarm data type + + U32 persistentClearCount; ///< Persistent count limit before clear alarm + U32 persistentTriggerCount; ///< Persistent count limit before trigger alarm + + U32 inRangeCounter; ///< Data in range persistent counter + U32 outOfRangeCounter; ///< Data out of range persistent counter +} PERSISTENT_ALARM_T; + +// ********** private data ********** + +PERSISTENT_ALARM_T persistentAlarms[ MAX_NUMBER_OF_PERSISTENT_ALARM ]; ///< Array of persistent alarm structure +static U32 alarmCount = 0U; ///< Persistent alarm count + +// ********** private function prototypes ********** + +static U32 getPersistentAlarmIndex( ALARM_ID_T alarm ); +static void setAlarm( U32 const alarmIndex, F32 const data ); + +/************************************************************************* + * @brief + * The initPersistentAlarm function initializes the PersistentAlarm module + * when the alarm count lower than maximum persistent alarm allowed. + * @details + * Inputs : none + * Outputs : PersistentAlarm module initialized + * @param alarm Alarm id + * @param alarmType Alarm's data type + * @param persistentClearCount Persistent count limit before clear alarm + * @param persistentTriggerCount Persistent count limit before trigger alarm + * @return none + *************************************************************************/ +void initPersistentAlarm( ALARM_ID_T alarm, ALARM_DATA_TYPES_T alarmType, F32 persistentClearCount, F32 persistentTriggerCount ) +{ + if ( alarmCount < MAX_NUMBER_OF_PERSISTENT_ALARM ) + { + persistentAlarms[ alarmCount ].alarm = alarm; + persistentAlarms[ alarmCount ].alarmType = alarmType; + persistentAlarms[ alarmCount ].persistentClearCount = persistentClearCount; + persistentAlarms[ alarmCount ].persistentTriggerCount = persistentTriggerCount; + persistentAlarms[ alarmCount ].inRangeCounter = 0U; + persistentAlarms[ alarmCount ].outOfRangeCounter = 0U; + alarmCount++; + } + else + { + activateAlarmNoData( ALARM_ID_DG_SOFTWARE_FAULT ); + } +} + +/************************************************************************* + * @brief + * The checkPersistentAlarm function check whether data is out of range or + * not. Then the function set or clear alarm once the persistent counter + * exceeds the limit. + * @details + * Inputs : none + * Outputs : Checks for out of range data and set/clear alarm + * @param alarm Alarm id + * @param isOutOfRange Flag indicates data out of range + * @param data Data to be check for out of range + * @return none + *************************************************************************/ +void checkPersistentAlarm( ALARM_ID_T const alarm, BOOL const isOutOfRange, F32 const data ) +{ + U32 const alarmIndex = getPersistentAlarmIndex( alarm ); + + if ( alarmIndex < MAX_NUMBER_OF_PERSISTENT_ALARM ) + { + if ( isOutOfRange ) + { + ++persistentAlarms[ alarmIndex ].outOfRangeCounter; + persistentAlarms[ alarmIndex ].inRangeCounter = 0; + if ( persistentAlarms[ alarmIndex ].outOfRangeCounter > persistentAlarms[ alarmIndex ].persistentTriggerCount ) + { + setAlarm( alarmIndex, data ); + } + } + else + { + ++persistentAlarms[ alarmIndex ].inRangeCounter; + persistentAlarms[ alarmIndex ].outOfRangeCounter = 0; + if ( persistentAlarms[ alarmIndex ].inRangeCounter > persistentAlarms[ alarmIndex ].persistentClearCount ) + { + clearAlarm( persistentAlarms[ alarmIndex ].alarm ); + } + } + } + else + { + activateAlarmNoData( ALARM_ID_DG_SOFTWARE_FAULT ); + } +} + +/************************************************************************* + * @brief + * The getPersistentAlarmIndex function gets corresponding alarm index for + * a given alarm. + * @details + * Inputs : none + * Outputs : none + * @param alarm Alarm id + * @return alarm index + *************************************************************************/ +static U32 getPersistentAlarmIndex( ALARM_ID_T alarm ) +{ + U32 ii; + U32 result = MAX_NUMBER_OF_PERSISTENT_ALARM; + for ( ii = 0; ii < alarmCount; ++ii ) + { + if ( persistentAlarms[ii].alarm == alarm ) + { + result = ii; + } + } + return result; +} + +/************************************************************************* + * @brief + * The setAlarm function sets corresponding alarm with type of data + * @details + * Inputs : none + * Outputs : Set alarm + * @param alarmIndex Persistent alarm index + * @param data Data to be reported with the alarm + * @return none + *************************************************************************/ +static void setAlarm( U32 const alarmIndex, F32 const data ) +{ + if ( persistentAlarms[ alarmIndex ].alarmType == ALARM_DATA_TYPE_F32 ) + { + SET_ALARM_WITH_1_F32_DATA( persistentAlarms[ alarmIndex ].alarm, data ); + } + else + { + SET_ALARM_WITH_1_U32_DATA( persistentAlarms[ alarmIndex ].alarm, data ); + } +} + +/**@}*/ Index: firmware/App/Services/PersistentAlarm.h =================================================================== diff -u --- firmware/App/Services/PersistentAlarm.h (revision 0) +++ firmware/App/Services/PersistentAlarm.h (revision 4d47390287ee4870b048bcc015398f0f56e33d75) @@ -0,0 +1,39 @@ +/************************************************************************** +* +* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* +* THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN +* WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. +* +* @file PersistentAlarm.h +* +* @author (last) Quang Nguyen +* @date (last) 12-Aug-2020 +* +* @author (original) Quang Nguyen +* @date (original) 12-Aug-2020 +* +***************************************************************************/ + +#ifndef __PERSISTENTALARM_H__ +#define __PERSISTENTALARM_H__ + +#include "DGCommon.h" + +/** + * @defgroup PersistentAlarm PersistentAlarm + * @brief Persistent Alarm monitor module. + * Check for persistent error and throw appropriate alarm. + * + * @addtogroup PersistentAlarm + * @{ + */ + +// ********** public function prototypes ********** + +void initPersistentAlarm( ALARM_ID_T alarm, ALARM_DATA_TYPES_T alarmType, F32 persistentClearCount, F32 persistentTriggerCount ); +void checkPersistentAlarm( ALARM_ID_T const alarm, BOOL const isOutOfRange, F32 const data ); + +/**@}*/ + +#endif