Bar Logo 3.8/7.6 kw Totem pole Demonstration Application (Part-No. (not specified))
 
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 /* 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
71inline 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
88{
89 INTCON1bits.ADDRERR = 0; //Clear the trap flag
90 TRAPS_halt_on_error(TRAPS_ADDRESS_ERR);
91}
92
95{
96 INTCON4bits.SGHT = 0; //Clear the trap flag
97 TRAPS_halt_on_error(TRAPS_HARD_ERR);
98}
99
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 if(INTCON3bits.APLL == 1)
124 {
125 INTCON3bits.APLL = 0; //Clear the trap flag
126 TRAPS_halt_on_error(TRAPS_APLL_ERR);
127 }
128
129 while(1)
130 {
131 }
132}
133
136{
137 INTCON1bits.OSCFAIL = 0; //Clear the trap flag
138 TRAPS_halt_on_error(TRAPS_OSC_FAIL);
139}
140
143{
144 INTCON1bits.MATHERR = 0; //Clear the trap flag
145 TRAPS_halt_on_error(TRAPS_MATH_ERR);
146}
147
150{
151 /* We use a failsafe stack: the presence of a stack-pointer error
152 * means that we cannot trust the stack to operate correctly unless
153 * we set the stack pointer to a safe place.
154 */
156
157 INTCON1bits.STKERR = 0; //Clear the trap flag
158 TRAPS_halt_on_error(TRAPS_STACK_ERR);
159}
160
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...
Definition traps.c:52
static void use_failsafe_stack(void)
Definition traps.c:71
void ERROR_HANDLER _MathError(void)
Definition traps.c:158
void ERROR_HANDLER _StackError(void)
Definition traps.c:165
void ERROR_HANDLER _AddressError(void)
Definition traps.c:87
#define FAILSAFE_STACK_GUARDSIZE
Definition traps.c:43
void ERROR_HANDLER _SoftTrapError(void)
Definition traps.c:101
static uint16_t TRAPS_error_code
Definition traps.c:47
void ERROR_HANDLER _HardTrapError(void)
Definition traps.c:94
void ERROR_HANDLER _OscillatorFail(void)
Definition traps.c:151
#define ERROR_HANDLER
Definition traps.c:42