Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
app_fault_monitor.c
1 /*
2  * @file app_fault_monitor.c
3  * Author: M91406
4  * @brief Fault monitor application layer source file
5  * Revision history:
6  */
7 
8 #include <stddef.h>
9 
10 #include "config/hal.h"
11 #include "config/apps.h"
12 
49 // Define fault objects
50 volatile struct FAULT_OBJECT_s fltobj_BuckUVLO;
51 volatile struct FAULT_OBJECT_s fltobj_BuckOVLO;
52 volatile struct FAULT_OBJECT_s fltobj_BuckOCP;
53 volatile struct FAULT_OBJECT_s fltobj_BuckRegErr;
54 
55 // Declare private function prototypes
56 volatile uint16_t __attribute__((always_inline)) uvlo_FaultInitialize(void);
57 volatile uint16_t __attribute__((always_inline)) ovlo_FaultInitialize(void);
58 volatile uint16_t __attribute__((always_inline)) ocp_FaultInitialize(void);
59 volatile uint16_t __attribute__((always_inline)) regerr_FaultInitialize(void);
60 
61 // Variables
62 volatile uint16_t FaultRecoveryCounter=0;
63 
64 
81 volatile uint16_t appFaultMonitor_Execute(void)
82 {
83  volatile uint16_t retval=1;
84  static bool pre_fault_active=false;
85 
86  // Call fault handler
91 
92  // Combine individual fault bits to a common fault indicator
93  buck.status.bits.fault_active = (bool) (
98  );
99 
100  // If system has recovered from a global fault condition,
101  // trigger fault recovery function
102  if ((pre_fault_active) && (!buck.status.bits.fault_active))
103  {
104  if (FaultRecoveryCounter++ < BUCK_FAULT_RESTART_CYCLES)
105  { retval = appPowerSupply_Resume(); } // Initiate restart attempt
106  else
107  { // Latch Fault Condition keeping power supply disabled
108  buck.status.bits.fault_active = true;
109  FaultRecoveryCounter = BUCK_FAULT_RESTART_CYCLES;
110  }
111  }
112 
113  // If power supply controller passes POWER GOOD and reaches state ONLINE,
114  // reset restart cycle counter
115  if (buck.state_id.bits.opstate_id == BUCK_OPSTATE_ONLINE)
116  FaultRecoveryCounter = 0;
117 
118  // Track global fault bit transitions
119  pre_fault_active = buck.status.bits.fault_active;
120 
121 
122  return (retval);
123 }
124 
125 
138 volatile uint16_t appFaultMonitor_Initialize(void)
139 {
140  volatile uint16_t retval=1;
141 
142  // Initialize user fault objects
143  retval &= uvlo_FaultInitialize();
144  retval &= ovlo_FaultInitialize();
145  retval &= ocp_FaultInitialize();
146  retval &= regerr_FaultInitialize();
147 
148  return(retval);
149 }
150 
151 
165 volatile uint16_t appFaultMonitor_Dispose(void)
166 {
167  fltobj_BuckUVLO = fltObjectClear; // Delete Under Voltage Lock Out object
168  fltobj_BuckOVLO = fltObjectClear; // Delete Over Voltage Lock Out object
169  fltobj_BuckRegErr = fltObjectClear; // Delete Regulation Error object
170  fltobj_BuckOCP = fltObjectClear; // Delete Over Current Protection object
171 
172  return(1);
173 }
174 
175 
176 /* *********************************************************************************
177  * PRIVATE FUNCTIONS
178  * ********************************************************************************/
179 
180 
194 volatile uint16_t uvlo_FaultInitialize(void)
195 {
196  volatile uint16_t retval=1;
197 
198  // Initialize UVLO fault object
199  fltobj_BuckUVLO = fltObjectClear; // Pre-initialize fault object
200 
201  fltobj_BuckUVLO.SourceObject.ptrObject = &buck.data.v_in; // Set pointer to variable to monitor
202  fltobj_BuckUVLO.SourceObject.bitMask = 0xFFFF; // Compare all bits of SOURCE (no bit filter)
203  fltobj_BuckUVLO.ReferenceObject.ptrObject = NULL; // Clear pointer to "compare against" variable
204  fltobj_BuckUVLO.ReferenceObject.bitMask = 0xFFFF; // Clear pointer to "compare against" variable
205 
206  fltobj_BuckUVLO.Status.bits.CompareType = FLTCMP_LESS_THAN; // Select Compare-Type
207 
209  fltobj_BuckUVLO.TripResponse.eventThreshold = BUCK_UVLO_TDLY; // Set counter level at which a FAULT condition will be tripped
210  fltobj_BuckUVLO.TripResponse.ptrResponseFunction = &appPowerSupply_Suspend; // Set pointer to user-function which should be called when a FAULT is tripped
211 
213  fltobj_BuckUVLO.RecoveryResponse.eventThreshold = BUCK_UVLO_RDLY; // Set counter level at which a FAULT condition will be cleared
214  fltobj_BuckUVLO.RecoveryResponse.ptrResponseFunction = NULL; // Clear recovery function pointer
215 
216  fltobj_BuckUVLO.Counter = 0; // Clear fault event counter
217  fltobj_BuckUVLO.Status.bits.FaultActive = true; // Set fault condition flag (must be cleared by fault check)
218  fltobj_BuckUVLO.Status.bits.FaultStatus = true; // Set fault flag (must be cleared by fault check)
219  fltobj_BuckUVLO.Status.bits.Enabled = true; // Enable fault checks
220 
221  return(retval);
222 
223 }
224 
225 
239 volatile uint16_t ovlo_FaultInitialize(void)
240 {
241  volatile uint16_t retval=1;
242 
243  // Initialize OVLO fault object
244  fltobj_BuckOVLO = fltObjectClear; // Pre-initialize fault object
245 
246  fltobj_BuckOVLO.SourceObject.ptrObject = &buck.data.v_in; // Set pointer to variable to monitor
247  fltobj_BuckOVLO.SourceObject.bitMask = 0xFFFF; // Compare all bits of SOURCE (no bit filter)
248  fltobj_BuckOVLO.ReferenceObject.ptrObject = NULL; // Clear pointer to "compare against" variable
249  fltobj_BuckOVLO.ReferenceObject.bitMask = 0xFFFF; // Compare all bits of SOURCE (no bit filter)
250  fltobj_BuckOVLO.Status.bits.CompareType = FLTCMP_GREATER_THAN; // Select Compare-Type
251 
253  fltobj_BuckOVLO.TripResponse.eventThreshold = BUCK_OVLO_TDLY; // Set counter level at which a FAULT condition will be tripped
254  fltobj_BuckOVLO.TripResponse.ptrResponseFunction = &appPowerSupply_Suspend; // Set pointer to user-function which should be called when a FAULT is tripped
255 
257  fltobj_BuckOVLO.RecoveryResponse.eventThreshold = BUCK_OVLO_RDLY; // Set counter level at which a FAULT condition will be cleared
258  fltobj_BuckOVLO.RecoveryResponse.ptrResponseFunction = NULL; // Clear recovery function pointer
259 
260  fltobj_BuckOVLO.Counter = 0; // Clear fault event counter
261  fltobj_BuckOVLO.Status.bits.FaultActive = true; // Set fault condition flag (must be cleared by fault check)
262  fltobj_BuckOVLO.Status.bits.FaultStatus = true; // Set fault flag (must be cleared by fault check)
263  fltobj_BuckOVLO.Status.bits.Enabled = true; // Enable fault checks
264 
265  return(retval);
266 
267 }
268 
269 
282 volatile uint16_t regerr_FaultInitialize(void)
283 {
284  volatile uint16_t retval=1;
285 
286  // Initialize regulation error fault object
287  fltobj_BuckRegErr = fltObjectClear; // Pre-initialize fault object
288 
289  fltobj_BuckRegErr.SourceObject.ptrObject = &buck.data.v_out; // Set pointer to variable to monitor
290  fltobj_BuckRegErr.SourceObject.bitMask = 0xFFFF; // Compare all bits of SOURCE (no bit filter)
291  fltobj_BuckRegErr.ReferenceObject.ptrObject = &buck.set_values.v_ref; // Set pointer to "compare against" variable
292  fltobj_BuckRegErr.ReferenceObject.bitMask = 0xFFFF; // Compare all bits of Reference (no bit filter)
293  fltobj_BuckRegErr.Status.bits.CompareType = FLTCMP_GREATER_THAN; // Select Compare-Type
294 
296  fltobj_BuckRegErr.TripResponse.eventThreshold = BUCK_REGERR_TDLY; // Set counter level at which a FAULT condition will be tripped
297  fltobj_BuckRegErr.TripResponse.ptrResponseFunction = &appPowerSupply_Suspend; // Set pointer to user-function which should be called when a FAULT is tripped
298 
300  fltobj_BuckRegErr.RecoveryResponse.eventThreshold = BUCK_REGERR_RDLY; // Set counter level at which a FAULT condition will be cleared
301  fltobj_BuckRegErr.RecoveryResponse.ptrResponseFunction = NULL; // Clear recovery function pointer
302 
303  fltobj_BuckRegErr.Counter = 0; // Clear fault event counter
304  fltobj_BuckRegErr.Status.bits.FaultActive = false; // Set fault condition flag (must be cleared by fault check)
305  fltobj_BuckRegErr.Status.bits.FaultStatus = false; // Set fault flag (must be cleared by fault check)
306  fltobj_BuckRegErr.Status.bits.Enabled = false; // Disable fault checks at startup
307 
308  return(retval);
309 
310 }
311 
312 
325 volatile uint16_t ocp_FaultInitialize(void)
326 {
327  volatile uint16_t retval=1;
328 
329  // Initialize OCP fault object
330  fltobj_BuckOCP = fltObjectClear; // Pre-initialize fault object
331 
332  fltobj_BuckOCP.SourceObject.ptrObject = &buck.data.i_out; // Set pointer to variable to monitor
333  fltobj_BuckOCP.SourceObject.bitMask = 0xFFFF; // Compare all bits of SOURCE (no bit filter)
334  fltobj_BuckOCP.ReferenceObject.ptrObject = NULL; // Clear pointer to "compare against" variable
335  fltobj_BuckOCP.ReferenceObject.bitMask = 0xFFFF; // Compare all bits of SOURCE (no bit filter)
336  fltobj_BuckOCP.Status.bits.CompareType = FLTCMP_GREATER_THAN; // Select Compare-Type
337 
338  fltobj_BuckOCP.TripResponse.compareThreshold = BUCK_ISNS_OCL; // Set fault trip level
339  fltobj_BuckOCP.TripResponse.eventThreshold = BUCK_OCP_TDLY; // Set counter level at which a FAULT condition will be tripped
340  fltobj_BuckOCP.TripResponse.ptrResponseFunction = &appPowerSupply_Suspend; // Set pointer to user-function which should be called when a FAULT is tripped
341 
343  fltobj_BuckOCP.RecoveryResponse.eventThreshold = BUCK_OCP_RDLY; // Set counter level at which a FAULT condition will be cleared
344  fltobj_BuckOCP.RecoveryResponse.ptrResponseFunction = NULL; // Clear recovery function pointer
345 
346  fltobj_BuckOCP.Counter = 0; // Clear fault event counter
347  fltobj_BuckOCP.Status.bits.FaultActive = false; // Set fault condition flag (must be cleared by fault check)
348  fltobj_BuckOCP.Status.bits.FaultStatus = false; // Set fault flag (must be cleared by fault check)
349  fltobj_BuckOCP.Status.bits.Enabled = false; // Enable fault checks
350 
351  return(retval);
352 
353 }
354 
355 // end of file
BUCK_OVLO_RDLY
#define BUCK_OVLO_RDLY
over voltage lock out recovery delay conversion nacro
Definition: dpsk3_hwdescr.h:882
appPowerSupply_Resume
volatile uint16_t appPowerSupply_Resume(void)
This function resumes the power supply operation.
Definition: app_power_control.c:233
BUCK_VOUT_DEV_TRIP
#define BUCK_VOUT_DEV_TRIP
Macro calculating the integer number equivalent of the maximum allowed output voltage deviation given...
Definition: dpsk3_hwdescr.h:533
BUCK_OCP_TDLY
#define BUCK_OCP_TDLY
over current protection trip Delay conversion macro
Definition: dpsk3_hwdescr.h:885
fltobj_BuckRegErr
volatile struct FAULT_OBJECT_s fltobj_BuckRegErr
Regulation Error Fault Object.
Definition: app_fault_monitor.c:53
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
BUCK_REGERR_RDLY
#define BUCK_REGERR_RDLY
regulation error recovery delay conversion macro
Definition: dpsk3_hwdescr.h:884
FAULT_OBJECT_s::RecoveryResponse
volatile struct FLT_EVENT_RESPONSE_s RecoveryResponse
Settings defining the fault recovery event.
Definition: drv_fault_handler.h:138
BUCK_CONVERTER_DATA_s::v_in
volatile uint16_t v_in
BUCK input voltage.
Definition: dev_buck_typedef.h:300
fltobj_BuckUVLO
volatile struct FAULT_OBJECT_s fltobj_BuckUVLO
Under Voltage Lock Out Fault Object.
Definition: app_fault_monitor.c:50
BUCK_CONVERTER_s::status
volatile struct BUCK_CONVERTER_STATUS_s status
BUCK operation status bits.
Definition: dev_buck_typedef.h:502
FAULT_OBJECT_s::Status
volatile struct FLT_OBJECT_STATUS_s Status
Status word of this fault object.
Definition: drv_fault_handler.h:133
BUCK_UVLO_RDLY
#define BUCK_UVLO_RDLY
under voltage lock out recovery delay conversion nacro
Definition: dpsk3_hwdescr.h:880
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
BUCK_CONVERTER_STATUS_s::fault_active
volatile bool fault_active
Bit #5: Flag bit indicating system is in enforced shut down mode (usually due to a fault condition)
Definition: dev_buck_typedef.h:213
BUCK_ISNS_OCL_RELEASE
#define BUCK_ISNS_OCL_RELEASE
Over Current Release Level.
Definition: dpsk3_hwdescr.h:618
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
appPowerSupply_Suspend
volatile uint16_t appPowerSupply_Suspend(void)
This function stops the power supply operation.
Definition: app_power_control.c:212
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
BUCK_CONVERTER_s::data
volatile struct BUCK_CONVERTER_DATA_s data
BUCK runtime data.
Definition: dev_buck_typedef.h:506
BUCK_UVLO_TDLY
#define BUCK_UVLO_TDLY
Conversion Macros of Fault Response Timing Settings.
Definition: dpsk3_hwdescr.h:879
BUCK_ISNS_OCL
#define BUCK_ISNS_OCL
Over Current Limit.
Definition: dpsk3_hwdescr.h:617
FLT_COMPARE_OBJECT_s::ptrObject
volatile uint16_t * ptrObject
Pointer to register or variable which should be monitored.
Definition: drv_fault_handler.h:106
BUCK_CONVERTER_SETTINGS_s::v_ref
volatile uint16_t v_ref
User reference setting used to control the power converter controller.
Definition: dev_buck_typedef.h:326
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
BUCK_CONVERTER_s::set_values
volatile struct BUCK_CONVERTER_SETTINGS_s set_values
Control field for global access to references.
Definition: dev_buck_typedef.h:505
BUCK_CONVERTER_DATA_s::i_out
volatile uint16_t i_out
BUCK common output current.
Definition: dev_buck_typedef.h:299
FAULT_OBJECT_s::Counter
volatile uint16_t Counter
Fault event counter (controlled by FAULT HANDLER)
Definition: drv_fault_handler.h:134
BUCK_VIN_OVLO_TRIP
#define BUCK_VIN_OVLO_TRIP
Over Voltage LOck Out voltage.
Definition: dpsk3_hwdescr.h:455
FAULT_OBJECT_s
This data structure is a collection of data structures for fault handling.
Definition: drv_fault_handler.h:131
ocp_FaultInitialize
volatile uint16_t ocp_FaultInitialize(void)
Initializes the user-defined fault objects for overcurrent protection.
Definition: app_fault_monitor.c:325
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
fltobj_BuckOVLO
volatile struct FAULT_OBJECT_s fltobj_BuckOVLO
Over Voltage Lock Out Fault Object.
Definition: app_fault_monitor.c:51
BUCK_VOUT_DEV_RELEASE
#define BUCK_VOUT_DEV_RELEASE
Macro calculating the integer number equivalent of the maximum allowed output voltage deviation given...
Definition: dpsk3_hwdescr.h:534
FLT_OBJECT_STATUS_s::FaultStatus
volatile bool FaultStatus
Bit 0: Flag bit indicating if FAULT has been tripped.
Definition: drv_fault_handler.h:84
fltobj_BuckOCP
volatile struct FAULT_OBJECT_s fltobj_BuckOCP
Over Current Protection Fault Object.
Definition: app_fault_monitor.c:52
appFaultMonitor_Execute
volatile uint16_t appFaultMonitor_Execute(void)
Application wide fault object monitoring routine.
Definition: app_fault_monitor.c:56
BUCK_STATE_ID_s::bits
struct BUCK_STATE_ID_s::@3::@4 bits
BUCK_VIN_OVLO_RELEASE
#define BUCK_VIN_OVLO_RELEASE
Over Voltage LOck Out voltage.
Definition: dpsk3_hwdescr.h:456
BUCK_CONVERTER_s::state_id
volatile struct BUCK_STATE_ID_s state_id
BUCK state machine operating state ID.
Definition: dev_buck_typedef.h:503
BUCK_VIN_UVLO_TRIP
#define BUCK_VIN_UVLO_TRIP
Under Voltage LOck Out voltage.
Definition: dpsk3_hwdescr.h:453
appFaultMonitor_Dispose
volatile uint16_t appFaultMonitor_Dispose(void)
Function clearing all fault object settings.
Definition: app_fault_monitor.c:165
regerr_FaultInitialize
volatile uint16_t regerr_FaultInitialize(void)
Initializes the user-defined fault objects for regulation error.
Definition: app_fault_monitor.c:282
BUCK_OCP_RDLY
#define BUCK_OCP_RDLY
over current protection recovery delay conversion nacro
Definition: dpsk3_hwdescr.h:886
ovlo_FaultInitialize
volatile uint16_t ovlo_FaultInitialize(void)
Initializes the user-defined fault objects for overvoltage lockout.
Definition: app_fault_monitor.c:239
appFaultMonitor_Initialize
volatile uint16_t appFaultMonitor_Initialize(void)
Initialization of user-defined fault objects.
Definition: app_fault_monitor.c:138
uvlo_FaultInitialize
volatile uint16_t uvlo_FaultInitialize(void)
Initializes the user-defined fault objects for under-voltage lockout.
Definition: app_fault_monitor.c:194
BUCK_VIN_UVLO_RELEASE
#define BUCK_VIN_UVLO_RELEASE
Under Voltage LOck Out voltage.
Definition: dpsk3_hwdescr.h:454
buck
volatile struct BUCK_CONVERTER_s buck
Global data object for a BUCK CONVERTER.
Definition: app_power_control.c:28
BUCK_FAULT_RESTART_CYCLES
#define BUCK_FAULT_RESTART_CYCLES
Maximum number of restart cycles: Fault state will be latched until CPU reset or power cycle.
Definition: dpsk3_hwdescr.h:863
BUCK_CONVERTER_DATA_s::v_out
volatile uint16_t v_out
BUCK output voltage.
Definition: dev_buck_typedef.h:301
BUCK_REGERR_TDLY
#define BUCK_REGERR_TDLY
regulation error trip delay conversion macro
Definition: dpsk3_hwdescr.h:883
BUCK_OVLO_TDLY
#define BUCK_OVLO_TDLY
over voltage lock out trip delay conversion nacro
Definition: dpsk3_hwdescr.h:881