| Digital Power Starter Kit 3 Firmware dsPIC33C Boost 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 /**************************************************************************************************
76 * @fn uint16_t drv_TrapHandler_SoftTrapsInitialize(
77 * bool accumulator_a_overflow_trap_enable,
78 * bool accumulator_b_overflow_trap_enable,
79 * bool accumulator_catastrophic_overflow_trap_enable)
80 * @ingroup lib-layer-fault-functions-public
81 * @brief Configures the software-configurable traps
82 * @param accumulator_a_overflow_trap_enable Flag of type Boolean enabling/disabling accumulator A trap at 1.31 overflow
83 * @param accumulator_b_overflow_trap_enable Flag of type Boolean enabling/disabling accumulator B trap at 1.31 overflow
84 * @param accumulator_catastrophic_overflow_trap_enable Flag of type Boolean enabling/disabling catastrophic accumulator trap at 9.31 overflow
85 *
86 * @details
87 * This routine sets the DSP-specific traps for overflow-events of accumulator A and B.
88 *
89 *************************************************************************************************/
90 volatile uint16_t drv_TrapHandler_SoftTrapsInitialize(
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 /**************************************************************************************************
104 * @ingroup lib-layer-fault-functions-public
105 * @fn void DefaultTrapHandler(enum TRAP_ID_e trap_id)
106 * @brief Centralized trap handler routine
107 *
108 * @details
109 * This routine is used as centralized trap handler for all traps. Each trap is identified
110 * and logged by a unique trap ID and the status bits of traps and interrupt vectors are
111 * captured
112 *
113 * Users may modify the basic framework provided here to suit to the needs of their application.
114 *
115 *************************************************************************************************/
116 void DefaultTrapHandler(enum TRAP_ID_e trap_id) {
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
132 if (traplog.status.bits.cpu_reset_trigger)
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 /**************************************************************************************************
148 * @ingroup lib-layer-fault-functions-public
149 * @{
150 * @fn void __attribute__((interrupt, no_auto_psv)) _ReservedTrap5(void)
151 * @brief basic framework for trap handler routine
152 *
153 * @details
154 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
155 * simply ensure that device continuously executes code within the trap service routine.
156 *
157 * Users may modify the basic framework provided here to suit to the needs of their application.
158 *************************************************************************************************/
159 void __attribute__((interrupt, no_auto_psv)) _ReservedTrap5(void) {
160 DefaultTrapHandler(TRAP_RESERVED_TRAP_5_ERROR); // Call default trap handler
161 }
162
163 /**************************************************************************************************
164 * @fn void _ReservedTrap7(void)
165 * @brief basic framework for trap handler routine
166 *
167 * @details
168 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
169 * simply ensure that device continuously executes code within the trap service routine.
170 *
171 * Users may modify the basic framework provided here to suit to the needs of their application.
172 *************************************************************************************************/
173 void __attribute__((interrupt, no_auto_psv)) _ReservedTrap7(void) {
174 DefaultTrapHandler(TRAP_RESERVED_TRAP_7_ERROR); // Call default trap handler
175 }
176
177 /**************************************************************************************************
178 * @fn void _HardTrapError(void)
179 * @brief basic framework for capturing Hard Trap Error
180 *
181 * @details
182 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
183 * simply ensure that device continuously executes code within the trap service routine.
184 *
185 * In this function the hard trap error is captured.
186 *************************************************************************************************/
187 void __attribute__((interrupt, no_auto_psv)) _HardTrapError(void) {
188 DefaultTrapHandler(TRAP_HARD_TRAP_ERROR); // Call default trap handler
189 }
190
191 /**************************************************************************************************
192 * @fn void _SoftTrapError(void)
193 * @brief basic framework for capturing Soft Trap Error
194 *
195 * @details
196 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
197 * simply ensure that device continuously executes code within the trap service routine.
198 *
199 * In this function the soft trap error is captured.
200 **************************************************************************************************/
201 void __attribute__((interrupt, no_auto_psv)) _SoftTrapError(void) {
202 DefaultTrapHandler(TRAP_SOFT_TRAP_ERROR);
203 }
204
205 /**************************************************************************************************
206 * @fn void _OscillatorFail(void)
207 * @brief captures Oscillator failure
208 *
209 * @details
210 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
211 * simply ensure that device continuously executes code within the trap service routine.
212 *
213 * Oscillator Failure Trap is captured, when the system clock becomes unstable
214 *************************************************************************************************/
215 void __attribute__((interrupt, no_auto_psv)) _OscillatorFail(void) {
216 DefaultTrapHandler(TRAP_OSCILLATOR_FAIL);
217 }
218
219 /**************************************************************************************************
220 * @fn void _AddressError(void)
221 * @brief captures Address error
222 *
223 * @details
224 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
225 * simply ensure that device continuously executes code within the trap service routine.
226 *
227 *Address Error Trap is captured, when a routine tries to access unknown memory addresses
228 *in RAM or Flash via PSV.
229 *************************************************************************************************/
230 void __attribute__((interrupt, no_auto_psv)) _AddressError(void) {
231 DefaultTrapHandler(TRAP_ADDRESS_ERROR);
232 }
233 /**************************************************************************************************
234 * @fn void _StackError(void)
235 * @brief captures Stack error
236 *
237 * @details
238 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
239 * simply ensure that device continuously executes code within the trap service routine.
240 *
241 * Stack Error Trap is captured, when a stack address error occurred.
242 *************************************************************************************************/
243 void __attribute__((interrupt, no_auto_psv)) _StackError(void) {
244 DefaultTrapHandler(TRAP_STACK_ERROR);
245 }
246
247 /**************************************************************************************************
248 * @fn void _MathError(void)
249 * @brief captures Math error
250 *
251 * @details
252 * These routines are used if INTCON2bits.ALTIVT = 1. All trap service routines in this file
253 * simply ensure that device continuously executes code within the trap service routine.
254 *
255 * Math Error Trap is captured, when a math operation cannot be solved (e.g. division by zero)
256 *************************************************************************************************/
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::status
volatile struct TRAPLOG_STATUS_s status
Status word of the Trap Logger object.
Definition: drv_trap_handler.h:282
TRAP_RESERVED_TRAP_7_ERROR
Trap ID for generic exception vector.
Definition: drv_trap_handler.h:110
TRAP_ALT_ADDRESS_ERROR
Trap ID for alternate exception vector.
Definition: drv_trap_handler.h:113
traplog
volatile struct TRAP_LOGGER_s traplog
data structure used as buffer for trap monitoring
Definition: drv_trap_handler.c:72
TRAP_STACK_ERROR
Trap ID for primary exception vector.
Definition: drv_trap_handler.h:103
TRAP_ALT_MATH_ERROR
Trap ID for alternate exception vector.
Definition: drv_trap_handler.h:115
TRAP_ALT_DMA_ERROR
Trap ID for alternate exception vector.
Definition: drv_trap_handler.h:116
TRAP_LOGGER_s::trap_count
volatile uint16_t trap_count
Counter tracking the number of occurrences.
Definition: drv_trap_handler.h:285
TRAP_ALT_STACK_ERROR
Trap ID for alternate exception vector.
Definition: drv_trap_handler.h:114
TRAP_MATH_ERROR
Trap ID for primary exception vector.
Definition: drv_trap_handler.h:104
TRAP_LOGGER_s::reset_count
volatile uint16_t reset_count
Counter of CPU RESET events (read/write)
Definition: drv_trap_handler.h:283
TRAPLOG_STATUS_s::cpu_reset_trigger
volatile bool cpu_reset_trigger
Definition: drv_trap_handler.h:231
TRAP_ALT_OSCILLATOR_FAIL
Trap ID for alternate exception vector.
Definition: drv_trap_handler.h:112
TRAP_ALT_SOFT_TRAP_ERROR
Trap ID for alternate generic soft trap exception vector.
Definition: drv_trap_handler.h:118
TRAP_ALT_HARD_TRAP_ERROR
Trap ID for alternate generic hard trap exception vector.
Definition: drv_trap_handler.h:119
TRAP_ADDRESS_ERROR
Trap ID for primary exception vector.
Definition: drv_trap_handler.h:102
TRAP_HARD_TRAP_ERROR
Trap ID for generic hard trap exception vector.
Definition: drv_trap_handler.h:108
TRAP_LOGGER_s::trap_id
enum TRAP_ID_e trap_id
Trap-ID of the captured incident.
Definition: drv_trap_handler.h:284
TRAP_SOFT_TRAP_ERROR
Trap ID for generic soft trap exception vector.
Definition: drv_trap_handler.h:107
TRAP_DMA_ERROR
Trap ID for primary exception vector.
Definition: drv_trap_handler.h:105
TRAP_OSCILLATOR_FAIL
Trap ID for primary exception vector.
Definition: drv_trap_handler.h:101
TRAP_LOGGER_s
Definition: drv_trap_handler.h:280
TRAP_ID_e
TRAP_ID_e
Definition: drv_trap_handler.h:99
TRAP_RESERVED_TRAP_5_ERROR
Trap ID for generic exception vector.
Definition: drv_trap_handler.h:109