|  | Dual Active Bridge Development Board (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
47 static uint16_t TRAPS_error_code = -1;
48
49// Section: Driver Interface Function Definitions
50
51//@brief Halts
52 void __attribute__((weak)) TRAPS_halt_on_error(uint16_t code)
53{
54 TRAPS_error_code = code;
55#ifdef __DEBUG
56 /* If we are in debug mode, cause a software breakpoint in the debugger */
57 __builtin_software_breakpoint();
58 while(1)
59 {
60
61 }
62#else
63 // Trigger software reset
64 __asm__ volatile ("reset");
65#endif
66}
67
68// @brief Sets the stack pointer to a backup area of memory, in case we run into
69// a stack error (in which case we can't really trust the stack pointer)
70
71 inline static void use_failsafe_stack(void)
72{
73 static uint8_t failsafe_stack[32];
74 asm volatile (
75 " mov %[pstack], W15\n"
76 :
77 : [pstack]"r"(failsafe_stack)
78 );
79
80 /* Controls where the stack pointer limit is, relative to the end of the
81 * failsafe stack
82 */
83 SPLIM = (uint16_t)(((uint8_t *)failsafe_stack) + sizeof(failsafe_stack) - (uint16_t) FAILSAFE_STACK_GUARDSIZE);
84}
85
87 void ERROR_HANDLER _AddressError(void)
88{
89 INTCON1bits.ADDRERR = 0; //Clear the trap flag
90 TRAPS_halt_on_error(TRAPS_ADDRESS_ERR);
91}
92
94 void ERROR_HANDLER _HardTrapError(void)
95{
96 INTCON4bits.SGHT = 0; //Clear the trap flag
97 TRAPS_halt_on_error(TRAPS_HARD_ERR);
98}
99
101 void ERROR_HANDLER _SoftTrapError(void)
102{
103 if(INTCON3bits.DOOVR == 1)
104 {
105 INTCON3bits.DOOVR = 0; //Clear the trap flag
106 TRAPS_halt_on_error(TRAPS_DOOVR_ERR);
107 }
108
109#ifdef _DAE
110 if(INTCON3bits.DAE == 1)
111 {
112 INTCON3bits.DAE = 0; //Clear the trap flag
113 TRAPS_halt_on_error(TRAPS_DAE_ERR);
114 }
115
116#endif
117 if(INTCON3bits.NAE == 1)
118 {
119 INTCON3bits.NAE = 0; //Clear the trap flag
120 TRAPS_halt_on_error(TRAPS_NVM_ERR);
121 }
122
123#ifdef _CAN
124 if(INTCON3bits.CAN == 1)
125 {
126 INTCON3bits.CAN = 0; //Clear the trap flag
127 TRAPS_halt_on_error(TRAPS_CAN_ERR);
128 }
129
130#endif
131 if(INTCON3bits.APLL == 1)
132 {
133 INTCON3bits.APLL = 0; //Clear the trap flag
134 TRAPS_halt_on_error(TRAPS_APLL_ERR);
135 }
136
137 while(1)
138 {
139 }
140}
141
143 void ERROR_HANDLER _OscillatorFail(void)
144{
145 INTCON1bits.OSCFAIL = 0; //Clear the trap flag
146 TRAPS_halt_on_error(TRAPS_OSC_FAIL);
147}
148
150 void ERROR_HANDLER _MathError(void)
151{
152 INTCON1bits.MATHERR = 0; //Clear the trap flag
153 TRAPS_halt_on_error(TRAPS_MATH_ERR);
154}
155
157 void ERROR_HANDLER _StackError(void)
158{
159 /* We use a failsafe stack: the presence of a stack-pointer error
160 * means that we cannot trust the stack to operate correctly unless
161 * we set the stack pointer to a safe place.
162 */
163 use_failsafe_stack();
164
165 INTCON1bits.STKERR = 0; //Clear the trap flag
166 TRAPS_halt_on_error(TRAPS_STACK_ERR);
167}
168
use_failsafe_stack
static void use_failsafe_stack(void)
Definition traps.c:71
__attribute__
void __attribute__((weak))
Definition traps.c:52
_MathError
void ERROR_HANDLER _MathError(void)
Definition traps.c:150
_StackError
void ERROR_HANDLER _StackError(void)
Definition traps.c:157
_AddressError
void ERROR_HANDLER _AddressError(void)
Definition traps.c:87
FAILSAFE_STACK_GUARDSIZE
#define FAILSAFE_STACK_GUARDSIZE
Definition traps.c:43
_SoftTrapError
void ERROR_HANDLER _SoftTrapError(void)
Definition traps.c:101
TRAPS_error_code
static uint16_t TRAPS_error_code
Definition traps.c:47
_HardTrapError
void ERROR_HANDLER _HardTrapError(void)
Definition traps.c:94
_OscillatorFail
void ERROR_HANDLER _OscillatorFail(void)
Definition traps.c:143
ERROR_HANDLER
#define ERROR_HANDLER
Definition traps.c:42
TRAPS_halt_on_error
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...