Digital Power Starter Kit 3 Firmware
dsPIC33C Buck Converter Voltage Mode Control Example
drv_trap_handler.c
1 /* **************************************************************************************************
2  * © 2006 Microchip Technology Inc.
3  *
4  * FileName: fdrv_TrapHandler.c
5  * Dependencies: Header file dvr_trap_handler.h
6  * Processor: dsPIC33FJxxx/dsPIC33EPxxx/dsPIC33CXxxx
7  * Compiler: MPLAB® X16 v1.36 or higher
8  *
9  * SOFTWARE LICENSE AGREEMENT:
10  * Microchip Technology Incorporated ("Microchip") retains all ownership and intellectual property
11  * rights in the code accompanying this message and in all derivatives hereto. You may use this
12  * code, and any derivatives created by any person or entity by or on your behalf, exclusively with
13  * Microchip's proprietary products. Your acceptance and/or use of this code constitutes agreement
14  * to the terms and conditions of this notice.
15  *
16  * CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS,
17  * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT,
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH
19  * MICROCHIP'S PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
20  *
21  * YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER IN CONTRACT,
22  * WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), STRICT LIABILITY, INDEMNITY,
23  * CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, PUNITIVE, EXEMPLARY, INCIDENTAL OR
24  * CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE CODE,
25  * HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
26  * FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
27  * IN ANY WAY RELATED TO THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP
28  * SPECIFICALLY TO HAVE THIS CODE DEVELOPED.
29  *
30  * You agree that you are solely responsible for testing the code and determining its suitability.
31  * Microchip has no obligation to modify, test, certify, or support the code.
32  *
33  * REVISION HISTORY:
34  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35  * Date Autor Company Version Comments
36  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37  *
38  * 03/09/2006 D. Settu MCHP 1.0 First Release
39  * 10/07/2009 Andreas Reiter HPC 1.1 added a header file and a default trap handler
40  * routine for better integration
41  * 07/19/2016 Andreas Reiter MCHP 1.2 Added data structure for trap monitoring
42  * 05/17/2019 M91406 MCHP 1.3 Added support for dsPIC33C and removed support for C30 C Complier
43  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44  *
45  * ADDITIONAL NOTES:
46  *
47  * 1. This file contains trap service routines (handlers) for hardware exceptions generated by
48  * the dsPIC33F device.
49  *
50  * 2. All trap service routines in this file simply ensure that device continuously executes code
51  * within the trap service routine. Users may modify the basic framework provided here to suit
52  * to the needs of their application.
53  *
54  * **************************************************************************************************/
55 
56 #include <xc.h>
57 #include <stdint.h>
58 #include <stdbool.h>
59 #include "drv_trap_handler.h"
60 
61 
72 volatile struct TRAP_LOGGER_s traplog;
73 
74 
75 
91  bool accumulator_a_overflow_trap_enable,
92  bool accumulator_b_overflow_trap_enable,
93  bool accumulator_catastrophic_overflow_trap_enable)
94 {
95  _OVATE = accumulator_a_overflow_trap_enable; // Enable Accumulator A Overflow Trap Enable bit
96  _OVBTE = accumulator_b_overflow_trap_enable; // Enable Accumulator B Overflow Trap Enable bit
97  _COVTE = accumulator_catastrophic_overflow_trap_enable; // Enable Catastrophic Overflow Trap Enable bit
98 
99  return(1);
100 }
101 
102 
103 
117 
118  // Capture Trap logger values
119  traplog.trap_id = trap_id; // Capture Trap ID
120  traplog.trap_count++; // Capture occurrence
121 
122  // These Nop()s can be used to place breakpoints
123  // during debugging.
124  Nop();
125  Nop();
126  Nop();
127  Nop();
128  Nop();
129  Nop();
130 
131  // If the CPU RESET TRIGGER is enabled, reset CPU here
133  {
134  traplog.reset_count++; // Capture RESET occurrence
135  asm volatile ("RESET\n"); // Reset CPU
136  }
137 
138  return;
139 }
140 
141 // =================================================================================================
142 //
143 // PRIMARY EXCEPTION VECTOR HANDLERS
144 //
145 // =================================================================================================
146 
147 
159 void __attribute__((interrupt, no_auto_psv)) _ReservedTrap5(void) {
160  DefaultTrapHandler(TRAP_RESERVED_TRAP_5_ERROR); // Call default trap handler
161 }
162 
163 
173 void __attribute__((interrupt, no_auto_psv)) _ReservedTrap7(void) {
174  DefaultTrapHandler(TRAP_RESERVED_TRAP_7_ERROR); // Call default trap handler
175 }
176 
177 
187 void __attribute__((interrupt, no_auto_psv)) _HardTrapError(void) {
188  DefaultTrapHandler(TRAP_HARD_TRAP_ERROR); // Call default trap handler
189 }
190 
191 
201 void __attribute__((interrupt, no_auto_psv)) _SoftTrapError(void) {
202  DefaultTrapHandler(TRAP_SOFT_TRAP_ERROR);
203 }
204 
205 
215 void __attribute__((interrupt, no_auto_psv)) _OscillatorFail(void) {
216  DefaultTrapHandler(TRAP_OSCILLATOR_FAIL);
217 }
218 
219 
230 void __attribute__((interrupt, no_auto_psv)) _AddressError(void) {
231  DefaultTrapHandler(TRAP_ADDRESS_ERROR);
232 }
233 
243 void __attribute__((interrupt, no_auto_psv)) _StackError(void) {
244  DefaultTrapHandler(TRAP_STACK_ERROR);
245 }
246 
247 
257 void __attribute__((interrupt, no_auto_psv)) _MathError(void) {
258  DefaultTrapHandler(TRAP_MATH_ERROR);
259 }
260 
262 #if (TRAP_DMA_SUPPORT == 1)
263 #if defined (_DMACError)
264 // =================================================================================================
265 // DMA Error Trap is captured, when an access error of the dual ported RAM occurred
266 // =================================================================================================
267 
268 void __attribute__((interrupt, no_auto_psv)) _DMACError(void) {
269  DefaultTrapHandler(TRAP_DMA_ERROR);
270 }
271 #endif
272 #endif
273 
274 // =================================================================================================
275 //
276 // ALTERNATE EXCEPTION VECTOR HANDLERS
277 //
278 // =================================================================================================
279 //
280 // These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
281 // simply ensure that device continuously executes code within the trap service routine.
282 //
283 // Users may modify the basic framework provided here to suit to the needs of their application.
284 //
285 // =================================================================================================
286 #if (__XC16_VERSION < 1030)
287 
288 void __attribute__((interrupt, no_auto_psv)) _AltHardTrapError(void) {
289  DefaultTrapHandler(TRAP_ALT_HARD_TRAP_ERROR); // Call default trap handler
290 }
291 
292 void __attribute__((interrupt, no_auto_psv)) _AltSoftTrapError(void) {
293  DefaultTrapHandler(TRAP_ALT_SOFT_TRAP_ERROR);
294 }
295 
296 void __attribute__((interrupt, no_auto_psv)) _AltOscillatorFail(void) {
297  DefaultTrapHandler(TRAP_ALT_OSCILLATOR_FAIL);
298 }
299 
300 void __attribute__((interrupt, no_auto_psv)) _AltAddressError(void) {
301  DefaultTrapHandler(TRAP_ALT_ADDRESS_ERROR);
302 }
303 
304 void __attribute__((interrupt, no_auto_psv)) _AltStackError(void) {
305  DefaultTrapHandler(TRAP_ALT_STACK_ERROR);
306 }
307 
308 void __attribute__((interrupt, no_auto_psv)) _AltMathError(void) {
309  DefaultTrapHandler(TRAP_ALT_MATH_ERROR);
310 }
311 
312 #if (TRAP_DMA_SUPPORT == 1)
313 
314 void __attribute__((interrupt, no_auto_psv)) _AltDMACError(void) {
315  DefaultTrapHandler(TRAP_ALT_DMA_ERROR);
316 }
317 #endif
318 
319 #endif
320 
321 
322 // EOF
TRAP_LOGGER_s::trap_id
enum TRAP_ID_e trap_id
Trap-ID of the captured incident.
Definition: drv_trap_handler.h:284
TRAP_LOGGER_s::status
volatile struct TRAPLOG_STATUS_s status
Status word of the Trap Logger object.
Definition: drv_trap_handler.h:282
TRAP_ID_e
TRAP_ID_e
defining trap-ID for primary and secondary vectors
Definition: drv_trap_handler.h:100
TRAP_LOGGER_s::trap_count
volatile uint16_t trap_count
Counter tracking the number of occurrences.
Definition: drv_trap_handler.h:285
TRAP_LOGGER_s::reset_count
volatile uint16_t reset_count
Counter of CPU RESET events (read/write)
Definition: drv_trap_handler.h:283
traplog
volatile struct TRAP_LOGGER_s traplog
data structure used as buffer for trap monitoring
Definition: drv_trap_handler.c:72
TRAPLOG_STATUS_s::cpu_reset_trigger
volatile bool cpu_reset_trigger
Definition: drv_trap_handler.h:231
drv_TrapHandler_SoftTrapsInitialize
volatile uint16_t drv_TrapHandler_SoftTrapsInitialize(bool accumulator_a_overflow_trap_enable, bool accumulator_b_overflow_trap_enable, bool accumulator_catastrophic_overflow_trap_enable)
Configures the software-configurable traps.
Definition: drv_trap_handler.c:90
DefaultTrapHandler
void DefaultTrapHandler(enum TRAP_ID_e trap_id)
Centralized trap handler routine.
Definition: drv_trap_handler.c:116
TRAP_LOGGER_s
Global data structure for trap event capturing.
Definition: drv_trap_handler.h:281