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