1 /*******************************************************************************
2 Copyright (c) 2006-2015 Cadence Design Systems Inc.
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 ******************************************************************************/
23
24 /******************************************************************************
25 Xtensa-specific interrupt and exception functions for RTOS ports.
26 Also see xtensa_intr_asm.S.
27 ******************************************************************************/
28
29 #include <stdlib.h>
30
31 #include <xtensa/config/core.h>
32
33 #include "esp_osal/esp_osal.h"
34 #include "esp_osal/portable.h"
35 #include "xtensa/xtensa_api.h"
36 #include "sdkconfig.h"
37 #include "esp_rom_sys.h"
38
39 #if XCHAL_HAVE_EXCEPTIONS
40
41 /* Handler table is in xtensa_intr_asm.S */
42
43 extern xt_exc_handler _xt_exception_table[];
44
45
46 /*
47 Default handler for unhandled exceptions.
48 CHANGED: We do this in panic.c now
49 */
50
51 //void xt_unhandled_exception(XtExcFrame *frame)
52 //{
53 //exit(-1);
54 //}
55 extern void xt_unhandled_exception(XtExcFrame *frame);
56
57
58 /*
59 This function registers a handler for the specified exception.
60 The function returns the address of the previous handler.
61 On error, it returns 0.
62 */
xt_set_exception_handler(int n,xt_exc_handler f)63 xt_exc_handler xt_set_exception_handler(int n, xt_exc_handler f)
64 {
65 xt_exc_handler old;
66
67 if( n < 0 || n >= XCHAL_EXCCAUSE_NUM )
68 return 0; /* invalid exception number */
69
70 /* Convert exception number to _xt_exception_table name */
71 n = n * portNUM_PROCESSORS + xPortGetCoreID();
72 old = _xt_exception_table[n];
73
74 if (f) {
75 _xt_exception_table[n] = f;
76 }
77 else {
78 _xt_exception_table[n] = &xt_unhandled_exception;
79 }
80
81 return ((old == &xt_unhandled_exception) ? 0 : old);
82 }
83
84 #endif
85
86 #if XCHAL_HAVE_INTERRUPTS
87
88 /* Handler table is in xtensa_intr_asm.S */
89
90 typedef struct xt_handler_table_entry {
91 void * handler;
92 void * arg;
93 } xt_handler_table_entry;
94
95 extern xt_handler_table_entry _xt_interrupt_table[XCHAL_NUM_INTERRUPTS*portNUM_PROCESSORS];
96
97
98 /*
99 Default handler for unhandled interrupts.
100 */
xt_unhandled_interrupt(void * arg)101 void IRAM_ATTR xt_unhandled_interrupt(void * arg)
102 {
103 esp_rom_printf("Unhandled interrupt %d on cpu %d!\n", (int)arg, xPortGetCoreID());
104 }
105
106 //Returns true if handler for interrupt is not the default unhandled interrupt handler
xt_int_has_handler(int intr,int cpu)107 bool xt_int_has_handler(int intr, int cpu)
108 {
109 return (_xt_interrupt_table[intr*portNUM_PROCESSORS+cpu].handler != xt_unhandled_interrupt);
110 }
111
112 /*
113 This function registers a handler for the specified interrupt. The "arg"
114 parameter specifies the argument to be passed to the handler when it is
115 invoked. The function returns the address of the previous handler.
116 On error, it returns 0.
117 */
xt_set_interrupt_handler(int n,xt_handler f,void * arg)118 xt_handler xt_set_interrupt_handler(int n, xt_handler f, void * arg)
119 {
120 xt_handler_table_entry * entry;
121 xt_handler old;
122
123 if( n < 0 || n >= XCHAL_NUM_INTERRUPTS )
124 return 0; /* invalid interrupt number */
125 if( Xthal_intlevel[n] > XCHAL_EXCM_LEVEL )
126 return 0; /* priority level too high to safely handle in C */
127
128 /* Convert exception number to _xt_exception_table name */
129 n = n * portNUM_PROCESSORS + xPortGetCoreID();
130
131 entry = _xt_interrupt_table + n;
132 old = entry->handler;
133
134 if (f) {
135 entry->handler = f;
136 entry->arg = arg;
137 }
138 else {
139 entry->handler = &xt_unhandled_interrupt;
140 entry->arg = (void*)n;
141 }
142
143 return ((old == &xt_unhandled_interrupt) ? 0 : old);
144 }
145
146 #if CONFIG_SYSVIEW_ENABLE
xt_get_interrupt_handler_arg(int n)147 void * xt_get_interrupt_handler_arg(int n)
148 {
149 xt_handler_table_entry * entry;
150
151 if( n < 0 || n >= XCHAL_NUM_INTERRUPTS )
152 return 0; /* invalid interrupt number */
153
154 /* Convert exception number to _xt_exception_table name */
155 n = n * portNUM_PROCESSORS + xPortGetCoreID();
156
157 entry = _xt_interrupt_table + n;
158 return entry->arg;
159 }
160 #endif
161
162 #endif /* XCHAL_HAVE_INTERRUPTS */
163