Bar Logo 4kW dsPIC33C PSFB DC-DC DA (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
traps.c
Go to the documentation of this file.
1
17/*
18© [2024] Microchip Technology Inc. and its subsidiaries.
19
20 Subject to your compliance with these terms, you may use Microchip
21 software and any derivatives exclusively with Microchip products.
22 You are responsible for complying with 3rd party license terms
23 applicable to your use of 3rd party software (including open source
24 software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
25 NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
26 SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
27 MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
28 WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
29 INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
30 KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
31 MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
32 FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
33 TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
34 EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
35 THIS SOFTWARE.
36*/
37
38// Section: Included Files
39#include <xc.h>
40#include "../traps.h"
41
42#define ERROR_HANDLER __attribute__((interrupt,no_auto_psv))
43#define FAILSAFE_STACK_GUARDSIZE 8
44
45// A private place to store the error code if we run into a severe error
46
47static uint16_t TRAPS_error_code = -1;
48
49// Section: Driver Interface Function Definitions
50
51//@brief Halts
52void __attribute__((weak)) TRAPS_halt_on_error(uint16_t code)
53{
54 TRAPS_error_code = code;
55#ifdef __DEBUG
56
57 Nop();
58 Nop();
59 Nop();
60
61 /* If we are in debug mode, cause a software breakpoint in the debugger */
62 __builtin_software_breakpoint();
63// while(1)
64// {
65//
66// }
67#else
68 // Trigger software reset
69 __asm__ volatile ("reset");
70#endif
71}
72
73// @brief Sets the stack pointer to a backup area of memory, in case we run into
74// a stack error (in which case we can't really trust the stack pointer)
75
76inline static void use_failsafe_stack(void)
77{
78 static uint8_t failsafe_stack[32];
79 asm volatile (
80 " mov %[pstack], W15\n"
81 :
82 : [pstack]"r"(failsafe_stack)
83 );
84
85 /* Controls where the stack pointer limit is, relative to the end of the
86 * failsafe stack
87 */
88 SPLIM = (uint16_t)(((uint8_t *)failsafe_stack) + sizeof(failsafe_stack) - (uint16_t) FAILSAFE_STACK_GUARDSIZE);
89}
90
93{
94 INTCON1bits.ADDRERR = 0; //Clear the trap flag
95 TRAPS_halt_on_error(TRAPS_ADDRESS_ERR);
96}
97
100{
101 INTCON4bits.SGHT = 0; //Clear the trap flag
102 TRAPS_halt_on_error(TRAPS_HARD_ERR);
103}
104
107{
108 if(INTCON3bits.DOOVR == 1)
109 {
110 INTCON3bits.DOOVR = 0; //Clear the trap flag
111 TRAPS_halt_on_error(TRAPS_DOOVR_ERR);
112 }
113
114#ifdef _DAE
115 if(INTCON3bits.DAE == 1)
116 {
117 INTCON3bits.DAE = 0; //Clear the trap flag
118 TRAPS_halt_on_error(TRAPS_DAE_ERR);
119 }
120
121#endif
122 if(INTCON3bits.NAE == 1)
123 {
124 INTCON3bits.NAE = 0; //Clear the trap flag
125 TRAPS_halt_on_error(TRAPS_NVM_ERR);
126 }
127
128#ifdef _CAN
129 if(INTCON3bits.CAN == 1)
130 {
131 INTCON3bits.CAN = 0; //Clear the trap flag
132 TRAPS_halt_on_error(TRAPS_CAN_ERR);
133 }
134
135#endif
136 if(INTCON3bits.APLL == 1)
137 {
138 INTCON3bits.APLL = 0; //Clear the trap flag
139 TRAPS_halt_on_error(TRAPS_APLL_ERR);
140 }
141
142 while(1)
143 {
144 }
145}
146
149{
150 INTCON1bits.OSCFAIL = 0; //Clear the trap flag
151 TRAPS_halt_on_error(TRAPS_OSC_FAIL);
152}
153
156{
157 INTCON1bits.MATHERR = 0; //Clear the trap flag
158 TRAPS_halt_on_error(TRAPS_MATH_ERR);
159}
160
163{
164 /* We use a failsafe stack: the presence of a stack-pointer error
165 * means that we cannot trust the stack to operate correctly unless
166 * we set the stack pointer to a safe place.
167 */
169
170 INTCON1bits.STKERR = 0; //Clear the trap flag
171 TRAPS_halt_on_error(TRAPS_STACK_ERR);
172}
173
static void use_failsafe_stack(void)
Definition traps.c:76
void __attribute__((weak))
Definition traps.c:52
void ERROR_HANDLER _MathError(void)
Definition traps.c:155
void ERROR_HANDLER _StackError(void)
Definition traps.c:162
void ERROR_HANDLER _AddressError(void)
Definition traps.c:92
#define FAILSAFE_STACK_GUARDSIZE
Definition traps.c:43
void ERROR_HANDLER _SoftTrapError(void)
Definition traps.c:106
static uint16_t TRAPS_error_code
Definition traps.c:47
void ERROR_HANDLER _HardTrapError(void)
Definition traps.c:99
void ERROR_HANDLER _OscillatorFail(void)
Definition traps.c:148
#define ERROR_HANDLER
Definition traps.c:42
void TRAPS_halt_on_error(uint16_t code)
Stores the trap error code and waits forever. This is a weak attribute function. The user can overrid...