1 /*********************************************************************
2  *
3  *                  Generic Exception Handler
4  *
5  *********************************************************************
6  * FileName:        exceptions.c
7  * Dependencies:
8  *
9  * Processor:       PIC32
10  *
11  * Complier:        MPLAB C32
12  *                  MPLAB IDE
13  * Company:         Microchip Technology, Inc.
14  * Author:          Darren Wenn
15  *
16  * Software License Agreement
17  *
18  * The software supplied herewith by Microchip Technology Incorporated
19  * (the “Company”) for its PIC32/PIC24 Microcontroller is intended
20  * and supplied to you, the Company’s customer, for use solely and
21  * exclusively on Microchip PIC32/PIC24 Microcontroller products.
22  * The software is owned by the Company and/or its supplier, and is
23  * protected under applicable copyright laws. All rights are reserved.
24  * Any use in violation of the foregoing restrictions may subject the
25  * user to criminal sanctions under applicable laws, as well as to
26  * civil liability for the breach of the terms and conditions of this
27  * license.
28  *
29  * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
30  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
31  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
33  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
34  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
35  *
36  *
37  *
38  ********************************************************************/
39 
40 #include <p32xxxx.h>
41 
42 // declared static in case exception condition would prevent
43 // auto variable being created
44 static enum {
45     EXCEP_IRQ = 0,          // interrupt
46     EXCEP_AdEL = 4,         // address error exception (load or ifetch)
47     EXCEP_AdES,             // address error exception (store)
48     EXCEP_IBE,              // bus error (ifetch)
49     EXCEP_DBE,              // bus error (load/store)
50     EXCEP_Sys,              // syscall
51     EXCEP_Bp,               // breakpoint
52     EXCEP_RI,               // reserved instruction
53     EXCEP_CpU,              // coprocessor unusable
54     EXCEP_Overflow,         // arithmetic overflow
55     EXCEP_Trap,             // trap (possible divide by zero)
56     EXCEP_IS1 = 16,         // implementation specfic 1
57     EXCEP_CEU,              // CorExtend Unuseable
58     EXCEP_C2E               // coprocessor 2
59 } _excep_code;
60 
61 static unsigned int _epc_code;
62 static unsigned int _excep_addr;
63 
64 #include <rtthread.h>
65 // this function overrides the normal _weak_ generic handler
_general_exception_handler(void)66 void _general_exception_handler(void)
67 {
68     asm volatile("mfc0 %0,$13" : "=r" (_excep_code));
69     asm volatile("mfc0 %0,$14" : "=r" (_excep_addr));
70 
71     _excep_code = (_excep_code & 0x0000007C) >> 2;
72 
73     rt_kprintf("\r\n_excep_code : %08X\r\n",_excep_code);
74     rt_kprintf("_excep_addr : %08X\r\n",_excep_addr);
75     switch(_excep_code)
76     {
77         case EXCEP_IRQ:rt_kprintf("interrupt\r\n");break;
78         case EXCEP_AdEL:rt_kprintf("address error exception (load or ifetch)\r\n");break;
79         case EXCEP_AdES:rt_kprintf("address error exception (store)\r\n");break;
80         case EXCEP_IBE:rt_kprintf("bus error (ifetch)\r\n");break;
81         case EXCEP_DBE:rt_kprintf("bus error (load/store)\r\n");break;
82         case EXCEP_Sys:rt_kprintf("syscall\r\n");break;
83         case EXCEP_Bp:rt_kprintf("breakpoint\r\n");break;
84         case EXCEP_RI:rt_kprintf("reserved instruction\r\n");break;
85         case EXCEP_CpU:rt_kprintf("coprocessor unusable\r\n");break;
86         case EXCEP_Overflow:rt_kprintf("arithmetic overflow\r\n");break;
87         case EXCEP_Trap:rt_kprintf("trap (possible divide by zero)\r\n");break;
88         case EXCEP_IS1:rt_kprintf("implementation specfic 1\r\n");break;
89         case EXCEP_CEU:rt_kprintf("CorExtend Unuseable\r\n");break;
90         case EXCEP_C2E:rt_kprintf("coprocessor 2\r\n");break;
91         default : rt_kprintf("unkown exception\r\n");break;
92     }
93 
94     while (1) {
95         // Examine _excep_code to identify the type of exception
96         // Examine _excep_addr to find the address that caused the exception
97     }
98 }
99