EPC9143 300 W 16th Brick DC/DC Module Reference Design
drv_fault_handler.c
1 /*
2  * File: fault_handler.c
3  * Author: M91406
4  *
5  * Created on December 27, 2019, 12:21 PM
6  */
39 #include <xc.h> // include processor files - each processor file is guarded.
40 #include <stdint.h> // include standard integer data types
41 #include <stdbool.h> // include standard boolean data types
42 #include <stdlib.h> // include standard library data types and macros
43 
44 #include "drv_fault_handler.h" //
45 
46 
47 
56 volatile struct FAULT_OBJECT_s fltObjectClear =
57 {
58  .Status.bits.CompareType = FLTCMP_NONE,
59  .Status.bits.Enabled = false,
60  .Status.bits.FaultActive = true,
61  .Status.bits.FaultStatus = true,
62  .Counter = 0,
63  .SourceObject.ptrObject = NULL,
64  .SourceObject.bitMask = 0xFFFF,
65  .ReferenceObject.ptrObject = NULL,
66  .ReferenceObject.bitMask = 0xFFFF,
67  .TripResponse.compareThreshold = 0,
68  .TripResponse.eventThreshold = 0,
69  .TripResponse.ptrResponseFunction = NULL,
70  .RecoveryResponse.compareThreshold = 0,
71  .RecoveryResponse.eventThreshold = 0,
72  .RecoveryResponse.ptrResponseFunction = NULL,
73  };
74 
75 
168 volatile uint16_t drv_FaultHandler_CheckObject(volatile struct FAULT_OBJECT_s* fltObject) {
169 
170  volatile uint16_t retval=1;
171  volatile uint16_t source=0;
172 
173  // If the fault object is not initialized, exit here with error
174  if (fltObject == NULL)
175  return(0);
176 
177  // If FAULT CHECK is disabled, exit here
178  if (!fltObject->Status.bits.Enabled) {
179  fltObject->Counter = 0; // Clear Counter
180  fltObject->Status.bits.FaultActive = false; // Clear immediate fault flag
181  fltObject->Status.bits.FaultStatus = false; // Clear fault status flag
182  return(1); // Return success
183  }
184 
185  // If the source object is not initialized, exit here with error
186  if (fltObject->SourceObject.ptrObject == NULL)
187  return(0);
188 
189  // Read most recent fault object value with bit-mask
190  source = (*fltObject->SourceObject.ptrObject & fltObject->SourceObject.bitMask);
191 
192  // If a reference object has been defined, read reference object value and override source with
193  // absolute value of difference between source and reference object values
194  if(fltObject->ReferenceObject.ptrObject != NULL) {
195 
196  uint16_t reference = (*fltObject->ReferenceObject.ptrObject & fltObject->ReferenceObject.bitMask);
197  source = (volatile uint16_t)
198  abs((volatile int32_t)source - (volatile int32_t)reference); // Load most recent value
199  }
200 
201  // Check fault condition
202 
203  switch(fltObject->Status.bits.CompareType) {
204 
205  case FLTCMP_GREATER_THAN: // Check if SOURCE > TRIP_LEVEL
206 
207  if (source > fltObject->TripResponse.compareThreshold) // Check if SOURCE > TRIP_LEVEL
208  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
209  else if (source < fltObject->RecoveryResponse.compareThreshold) // Check if SOURCE < RECOVERY_LEVEL
210  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
211  break;
212 
213  case FLTCMP_LESS_THAN:
214  if(source < fltObject->TripResponse.compareThreshold) // Check if SOURCE < TRIP_LEVEL
215  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
216  else if(source > fltObject->RecoveryResponse.compareThreshold) // Check if SOURCE > RECOVERY_LEVEL
217  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
218  break;
219 
220  case FLTCMP_IS_EQUAL:
221  if(source == fltObject->TripResponse.compareThreshold) // Check if SOURCE == TRIP_LEVEL
222  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
223  else if(source != fltObject->TripResponse.compareThreshold) // Check if SOURCE != TRIP_LEVEL
224  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
225  break;
226 
227  case FLTCMP_IS_NOT_EQUAL:
228  if(source != fltObject->TripResponse.compareThreshold) // Check if SOURCE != TRIP_LEVEL
229  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
230  else if(source == fltObject->TripResponse.compareThreshold) // Check if SOURCE == TRIP_LEVEL
231  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
232  break;
233 
234  case FLTCMP_BETWEEN:
235  // Check if SOURCE is between "RECOVERY_LEVEL ti TRIP_LEVEL"
236  if((fltObject->RecoveryResponse.compareThreshold < source) && (source < fltObject->TripResponse.compareThreshold))
237  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
238  else
239  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
240  break;
241 
242  case FLTCMP_OUTSIDE:
243  // Check if SOURCE is outside "RECOVERY_LEVEL to TRIP_LEVEL"
244  if((source < fltObject->RecoveryResponse.compareThreshold) || (fltObject->TripResponse.compareThreshold < source))
245  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
246  else
247  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
248  break;
249 
250  default:
251  return(0); // Return=>Error (Ignore fault check if compare type is not defined)
252  break;
253 
254  }
255 
256  // If a fault condition has been detected while no FAULT has been tripped....
257  if ((fltObject->Status.bits.FaultActive) && (!fltObject->Status.bits.FaultStatus)) {
258 
259  fltObject->Counter++; // Increment fault event counter
260 
261  // Trigger on FAULT conditions
262  if (fltObject->Counter >= fltObject->TripResponse.eventThreshold)
263  {
264  fltObject->Status.bits.FaultStatus = true; // Set FAULT STATUS FLAG BIT
265  fltObject->Counter = fltObject->TripResponse.eventThreshold; // Set fault event counter to threshold level
266  if (fltObject->TripResponse.ptrResponseFunction != NULL) // If a user function has been defined,
267  retval = fltObject->TripResponse.ptrResponseFunction(); // => call this function and capture return value
268  }
269 
270  }
271  // If a FAULT has been tripped but no fault condition has been detected anymore....
272  else if ((fltObject->Status.bits.FaultStatus) && (!fltObject->Status.bits.FaultActive)) {
273 
274  fltObject->Counter++; // Increment fault event counter
275 
276  // Trigger on RECOVERY conditions
277  if (fltObject->Counter >= fltObject->RecoveryResponse.eventThreshold)
278  {
279  fltObject->Status.bits.FaultStatus = false; // Clear FAULT STATUS FLAG BIT
280  fltObject->Counter = fltObject->RecoveryResponse.eventThreshold; // Set fault event counter to threshold level
281  if (fltObject->RecoveryResponse.ptrResponseFunction != NULL) // If a user function has been defined,
282  retval = fltObject->RecoveryResponse.ptrResponseFunction(); // => call this function and capture return value
283  }
284 
285  }
286  // If everything is OK, reset counter
287  else
288  {
289  fltObject->Counter = 0; // clear fault event counter
290  }
291 
292 
293  return (retval); // Fault handler executed successfully
294 }
295 
296 
297 // end of file
FLT_COMPARE_OBJECT_s::ptrObject
volatile uint16_t * ptrObject
Pointer to register or variable which should be monitored.
Definition: drv_fault_handler.h:106
FLT_OBJECT_STATUS_s::Enabled
volatile bool Enabled
Bit 15: Control bit enabling/disabling monitoring of the fault object.
Definition: drv_fault_handler.h:89
FAULT_OBJECT_s::Counter
volatile uint16_t Counter
Fault event counter (controlled by FAULT HANDLER)
Definition: drv_fault_handler.h:134
FAULT_OBJECT_s::RecoveryResponse
volatile struct FLT_EVENT_RESPONSE_s RecoveryResponse
Settings defining the fault recovery event.
Definition: drv_fault_handler.h:138
FLT_OBJECT_STATUS_s::FaultStatus
volatile bool FaultStatus
Bit 0: Flag bit indicating if FAULT has been tripped.
Definition: drv_fault_handler.h:84
FAULT_OBJECT_s::Status
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
Definition: drv_fault_handler.h:133
FLT_OBJECT_STATUS_s::CompareType
enum FLT_COMPARE_TYPE_e CompareType
Bit <10:8>: Fault check comparison type control bits.
Definition: drv_fault_handler.h:87
FLT_COMPARE_OBJECT_s::bitMask
volatile uint16_t bitMask
Bit mask will be &-ed with source as value (use 0xFFFF for full value comparison)
Definition: drv_fault_handler.h:107
drv_FaultHandler_CheckObject
volatile uint16_t drv_FaultHandler_CheckObject(volatile struct FAULT_OBJECT_s *fltObject)
Check current fault status of a user-defined fault object.
Definition: drv_fault_handler.c:168
FLT_OBJECT_STATUS_s::FaultActive
volatile bool FaultActive
Bit 1: Flag bit indicating if fault condition has been detected but FAULT has not been tripped yet.
Definition: drv_fault_handler.h:85
fltObjectClear
volatile struct FAULT_OBJECT_s fltObjectClear
Clears the fault objects.
Definition: drv_fault_handler.c:56
FLT_EVENT_RESPONSE_s::eventThreshold
volatile uint16_t eventThreshold
Bit mask will be &-ed with source as value (use 0xFFFF for full value comparison)
Definition: drv_fault_handler.h:121
FAULT_OBJECT_s::ReferenceObject
volatile struct FLT_COMPARE_OBJECT_s ReferenceObject
Reference object the source should be compared with.
Definition: drv_fault_handler.h:136
FLT_EVENT_RESPONSE_s::compareThreshold
volatile uint16_t compareThreshold
Signal level at which the fault condition will be detected.
Definition: drv_fault_handler.h:120
FAULT_OBJECT_s::TripResponse
volatile struct FLT_EVENT_RESPONSE_s TripResponse
Settings defining the fault trip event.
Definition: drv_fault_handler.h:137
FAULT_OBJECT_s
This data structure is a collection of data structures for fault handling.
Definition: drv_fault_handler.h:131
FLT_EVENT_RESPONSE_s::ptrResponseFunction
volatile uint16_t(* ptrResponseFunction)(void)
pointer to a user-defined function called when a defined fault monitoring event is detected
Definition: drv_fault_handler.h:122
FAULT_OBJECT_s::SourceObject
volatile struct FLT_COMPARE_OBJECT_s SourceObject
Object which should be monitored.
Definition: drv_fault_handler.h:135