|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Microchip Technology Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <zephyr/kernel.h> |
| 7 | +#include <zephyr/logging/log.h> |
| 8 | + |
| 9 | +#ifndef _ASMLANGUAGE |
| 10 | +#include <xc.h> |
| 11 | +#ifdef __cplusplus |
| 12 | +extern "C" { |
| 13 | +#endif |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(dspic, 4); |
| 16 | + |
| 17 | +volatile uint32_t reason, address; |
| 18 | + |
| 19 | +#define EXCEPTION_HANDLER __attribute__((interrupt, no_auto_psv, weak)) |
| 20 | +#define BUS_ERROR_MASK 0xF |
| 21 | +#define MATH_ERROR_MASK 0x1F |
| 22 | +#define GENERAL_TRAP_MASK 0x8000000Fu |
| 23 | + |
| 24 | +void __attribute__((weak)) TRAPS_halt_on_error(void); |
| 25 | +void EXCEPTION_HANDLER _BusErrorTrap(void); |
| 26 | +void EXCEPTION_HANDLER _AddressErrorTrap(void); |
| 27 | +void EXCEPTION_HANDLER _IllegalInstructionTrap(void); |
| 28 | +void EXCEPTION_HANDLER _MathErrorTrap(void); |
| 29 | +void EXCEPTION_HANDLER _StackErrorTrap(void); |
| 30 | +void EXCEPTION_HANDLER _GeneralTrap(void); |
| 31 | +void EXCEPTION_HANDLER _ReservedTrap0(void); |
| 32 | +void EXCEPTION_HANDLER _ReservedTrap7(void); |
| 33 | + |
| 34 | +void EXCEPTION_HANDLER _ReservedTrap0(void) |
| 35 | +{ |
| 36 | +} |
| 37 | +void EXCEPTION_HANDLER _ReservedTrap7(void) |
| 38 | +{ |
| 39 | +} |
| 40 | + |
| 41 | +void __attribute__((weak)) TRAPS_halt_on_error(void) |
| 42 | +{ |
| 43 | + /* stay here forever */ |
| 44 | + while (1) { |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** Bus error.**/ |
| 49 | +void EXCEPTION_HANDLER _BusErrorTrap(void) |
| 50 | +{ |
| 51 | + /* Identify bus error via INTCON3, fetch trap address from |
| 52 | + * PCTRAP, and reset error flags |
| 53 | + */ |
| 54 | + reason = INTCON3 & BUS_ERROR_MASK; |
| 55 | + address = PCTRAP; |
| 56 | + LOG_ERR("ERROR !!! Exception reason = %d, address = 0x%x\n", reason, address); |
| 57 | + INTCON3 &= ~(BUS_ERROR_MASK); |
| 58 | + PCTRAP = 0; |
| 59 | + TRAPS_halt_on_error(); |
| 60 | +} |
| 61 | + |
| 62 | +/** Address error.**/ |
| 63 | +void EXCEPTION_HANDLER _AddressErrorTrap(void) |
| 64 | +{ |
| 65 | + /* fetch trap address from PCTRAP |
| 66 | + * and reset error flags |
| 67 | + */ |
| 68 | + address = PCTRAP; |
| 69 | + LOG_ERR("ERROR !!! Exception reason = %s, address = 0x%x\n", "Address Error", address); |
| 70 | + INTCON1bits.ADDRERR = 0; |
| 71 | + PCTRAP = 0; |
| 72 | + TRAPS_halt_on_error(); |
| 73 | +} |
| 74 | + |
| 75 | +/** Illegal instruction.**/ |
| 76 | +void EXCEPTION_HANDLER _IllegalInstructionTrap(void) |
| 77 | +{ |
| 78 | + address = PCTRAP; |
| 79 | + LOG_ERR("ERROR !!! Exception reason = %s, address = 0x%x\n", "Illegal Instruction", |
| 80 | + address); |
| 81 | + INTCON1bits.BADOPERR = 0; |
| 82 | + PCTRAP = 0; |
| 83 | + TRAPS_halt_on_error(); |
| 84 | +} |
| 85 | + |
| 86 | +/** Math error.**/ |
| 87 | +void EXCEPTION_HANDLER _MathErrorTrap(void) |
| 88 | +{ |
| 89 | + /* Identify math error via INTCON4, fetch trap address from |
| 90 | + * PCTRAP, and reset error flags |
| 91 | + */ |
| 92 | + reason = INTCON4 & MATH_ERROR_MASK; |
| 93 | + address = PCTRAP; |
| 94 | + LOG_ERR("ERROR !!! Exception reason = %d, address = 0x%x\n", reason, address); |
| 95 | + INTCON4 &= ~(MATH_ERROR_MASK); |
| 96 | + PCTRAP = 0; |
| 97 | + TRAPS_halt_on_error(); |
| 98 | +} |
| 99 | + |
| 100 | +/** Stack error.**/ |
| 101 | +void EXCEPTION_HANDLER _StackErrorTrap(void) |
| 102 | +{ |
| 103 | + INTCON1bits.STKERR = 0; |
| 104 | + PCTRAP = 0; |
| 105 | + TRAPS_halt_on_error(); |
| 106 | +} |
| 107 | + |
| 108 | +/** Generic error.**/ |
| 109 | +void EXCEPTION_HANDLER _GeneralTrap(void) |
| 110 | +{ |
| 111 | + reason = INTCON5 & GENERAL_TRAP_MASK; |
| 112 | + address = PCTRAP; |
| 113 | + LOG_ERR("ERROR !!! Exception reason = %d, address = 0x%x\n", reason, address); |
| 114 | + INTCON5 &= ~(GENERAL_TRAP_MASK); |
| 115 | + PCTRAP = 0; |
| 116 | + TRAPS_halt_on_error(); |
| 117 | +} |
| 118 | + |
| 119 | +#ifdef __cplusplus |
| 120 | +} |
| 121 | +#endif |
| 122 | + |
| 123 | +#endif /* _ASMLANGUAGE */ |
0 commit comments