Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
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 the source object is not initialized, exit here with error
178  if (fltObject->SourceObject.ptrObject == NULL)
179  return(0);
180 
181  // Read most recent fault object value with bit-mask
182  source = (*fltObject->SourceObject.ptrObject & fltObject->SourceObject.bitMask);
183 
184  // If a reference object has been defined, read reference object value and override source with
185  // absolute value of difference between source and reference object values
186  if(fltObject->ReferenceObject.ptrObject != NULL) {
187 
188  uint16_t reference = (*fltObject->ReferenceObject.ptrObject & fltObject->ReferenceObject.bitMask);
189  source = (volatile uint16_t)
190  abs((volatile int32_t)source - (volatile int32_t)reference); // Load most recent value
191  }
192 
193  // Check fault condition
194 
195  switch(fltObject->Status.bits.CompareType) {
196 
197  case FLTCMP_GREATER_THAN: // Check if SOURCE > TRIP_LEVEL
198 
199  if (source > fltObject->TripResponse.compareThreshold) // Check if SOURCE > TRIP_LEVEL
200  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
201  else if (source < fltObject->RecoveryResponse.compareThreshold) // Check if SOURCE < RECOVERY_LEVEL
202  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
203  break;
204 
205  case FLTCMP_LESS_THAN:
206  if(source < fltObject->TripResponse.compareThreshold) // Check if SOURCE < TRIP_LEVEL
207  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
208  else if(source > fltObject->RecoveryResponse.compareThreshold) // Check if SOURCE > RECOVERY_LEVEL
209  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
210  break;
211 
212  case FLTCMP_IS_EQUAL:
213  if(source == fltObject->TripResponse.compareThreshold) // Check if SOURCE == TRIP_LEVEL
214  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
215  else if(source != fltObject->TripResponse.compareThreshold) // Check if SOURCE != TRIP_LEVEL
216  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
217  break;
218 
219  case FLTCMP_IS_NOT_EQUAL:
220  if(source != fltObject->TripResponse.compareThreshold) // Check if SOURCE != TRIP_LEVEL
221  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
222  else if(source == fltObject->TripResponse.compareThreshold) // Check if SOURCE == TRIP_LEVEL
223  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
224  break;
225 
226  case FLTCMP_BETWEEN:
227  // Check if SOURCE is between "RECOVERY_LEVEL ti TRIP_LEVEL"
228  if((fltObject->RecoveryResponse.compareThreshold < source) && (source < fltObject->TripResponse.compareThreshold))
229  fltObject->Status.bits.FaultActive = true; // Set FAULT_ACTIVE status flag bit
230  else
231  fltObject->Status.bits.FaultActive = false; // Clear FAULT_ACTIVE status flag bit
232  break;
233 
234  case FLTCMP_OUTSIDE:
235  // Check if SOURCE is outside "RECOVERY_LEVEL to TRIP_LEVEL"
236  if((source < fltObject->RecoveryResponse.compareThreshold) || (fltObject->TripResponse.compareThreshold < source))
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  default:
243  return(0); // Return=>Error (Ignore fault check if compare type is not defined)
244  break;
245 
246  }
247 
248  // If a fault condition has been detected while no FAULT has been tripped....
249  if ((fltObject->Status.bits.FaultActive) && (!fltObject->Status.bits.FaultStatus) &&
250  (fltObject->Status.bits.Enabled)) {
251 
252  fltObject->Counter++; // Increment fault event counter
253 
254  // Trigger on FAULT conditions
255  if (fltObject->Counter >= fltObject->TripResponse.eventThreshold)
256  {
257  fltObject->Status.bits.FaultStatus = true; // Set FAULT STATUS FLAG BIT
258  fltObject->Counter = fltObject->TripResponse.eventThreshold; // Set fault event counter to threshold level
259  if (fltObject->TripResponse.ptrResponseFunction != NULL) // If a user function has been defined,
260  retval = fltObject->TripResponse.ptrResponseFunction(); // => call this function and capture return value
261  }
262 
263  }
264  // If a FAULT has been tripped but no fault condition has been detected anymore....
265  else if (((fltObject->Status.bits.FaultStatus) && (!fltObject->Status.bits.FaultActive)) ||
266  (!fltObject->Status.bits.Enabled))
267  {
268 
269  fltObject->Counter++; // Increment fault event counter
270 
271  // Trigger on RECOVERY conditions
272  if (fltObject->Counter >= fltObject->RecoveryResponse.eventThreshold)
273  {
274  fltObject->Status.bits.FaultStatus = false; // Clear FAULT STATUS FLAG BIT
275  fltObject->Counter = fltObject->RecoveryResponse.eventThreshold; // Set fault event counter to threshold level
276  if (fltObject->RecoveryResponse.ptrResponseFunction != NULL) // If a user function has been defined,
277  retval = fltObject->RecoveryResponse.ptrResponseFunction(); // => call this function and capture return value
278  }
279 
280  }
281  // If everything is OK, reset counter
282  else
283  {
284  fltObject->Counter = 0; // clear fault event counter
285  }
286 
287 
288  return (retval); // Fault handler executed successfully
289 }
290 
291 
292 // end of file
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
FAULT_OBJECT_s::RecoveryResponse
volatile struct FLT_EVENT_RESPONSE_s RecoveryResponse
Settings defining the fault recovery event.
Definition: drv_fault_handler.h:138
FAULT_OBJECT_s::Status
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
Definition: drv_fault_handler.h:133
FAULT_OBJECT_s::TripResponse
volatile struct FLT_EVENT_RESPONSE_s TripResponse
Settings defining the fault trip event.
Definition: drv_fault_handler.h:137
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
FAULT_OBJECT_s::SourceObject
volatile struct FLT_COMPARE_OBJECT_s SourceObject
Object which should be monitored.
Definition: drv_fault_handler.h:135
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::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
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
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
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_COMPARE_OBJECT_s::ptrObject
volatile uint16_t * ptrObject
Pointer to register or variable which should be monitored.
Definition: drv_fault_handler.h:106
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::Counter
volatile uint16_t Counter
Fault event counter (controlled by FAULT HANDLER)
Definition: drv_fault_handler.h:134
FAULT_OBJECT_s
This data structure is a collection of data structures for fault handling.
Definition: drv_fault_handler.h:131
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
FLT_OBJECT_STATUS_s::FaultStatus
volatile bool FaultStatus
Bit 0: Flag bit indicating if FAULT has been tripped.
Definition: drv_fault_handler.h:84