]> git.sur5r.net Git - freertos/blob - Source/portable/MPLAB/PIC32MX/ISR_Support.h
Update ready for V5.1.0 release.
[freertos] / Source / portable / MPLAB / PIC32MX / ISR_Support.h
1 /*\r
2         FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 #include "FreeRTOSConfig.h"\r
51 \r
52 #define portCONTEXT_SIZE 132\r
53 #define portEPC_STACK_LOCATION  124\r
54 #define portSTATUS_STACK_LOCATION 128\r
55 \r
56 /******************************************************************/    \r
57 .macro  portSAVE_CONTEXT\r
58 \r
59         /* Make room for the context. First save the current status so we can \r
60         manipulate it, and the cause and EPC registers so we capture their \r
61         original values in case of interrupt nesting. */\r
62         mfc0            k0, _CP0_CAUSE\r
63         addiu           sp,     sp, -portCONTEXT_SIZE\r
64         mfc0            k1, _CP0_STATUS\r
65 \r
66         /* Also save s6 and s5 so we can use them during this interrupt.  Any\r
67         nesting interrupts should maintain the values of these registers\r
68         across the ISR. */\r
69         sw                      s6, 44(sp)\r
70         sw                      s5, 40(sp)\r
71         sw                      k1, portSTATUS_STACK_LOCATION(sp)\r
72 \r
73         /* Enable interrupts above the current priority. */\r
74         srl                     k0, k0, 0xa\r
75         ins             k1, k0, 10, 6\r
76         ins                     k1, zero, 1, 4\r
77 \r
78         /* s5 is used as the frame pointer. */\r
79         add                     s5, zero, sp\r
80 \r
81         /* Check the nesting count value. */\r
82         la                      k0, uxInterruptNesting\r
83         lw                      s6, (k0)\r
84 \r
85         /* If the nesting count is 0 then swap to the the system stack, otherwise\r
86         the system stack is already being used. */\r
87         bne                     s6, zero, .+20\r
88         nop\r
89 \r
90         /* Swap to the system stack. */\r
91         la                      sp, xISRStackTop\r
92         lw                      sp, (sp)\r
93 \r
94         /* Increment and save the nesting count. */\r
95         addiu           s6, s6, 1\r
96         sw                      s6, 0(k0)\r
97 \r
98         /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */\r
99         mfc0            s6, _CP0_EPC\r
100 \r
101         /* Re-enable interrupts. */\r
102         mtc0            k1, _CP0_STATUS\r
103 \r
104         /* Save the context into the space just created.  s6 is saved again\r
105         here as it now contains the EPC value.  No other s registers need be\r
106         saved. */\r
107         sw                      ra,     120(s5)\r
108         sw                      s8, 116(s5)\r
109         sw                      t9, 112(s5)\r
110         sw                      t8,     108(s5)\r
111         sw                      t7,     104(s5)\r
112         sw                      t6, 100(s5)\r
113         sw                      t5, 96(s5)\r
114         sw                      t4, 92(s5)\r
115         sw                      t3, 88(s5)\r
116         sw                      t2, 84(s5)\r
117         sw                      t1, 80(s5)\r
118         sw                      t0, 76(s5)\r
119         sw                      a3, 72(s5)\r
120         sw                      a2, 68(s5)\r
121         sw                      a1, 64(s5)\r
122         sw                      a0, 60(s5)\r
123         sw                      v1, 56(s5)\r
124         sw                      v0, 52(s5)\r
125         sw                      s6, portEPC_STACK_LOCATION(s5)\r
126         sw                      $1, 16(s5)\r
127 \r
128         /* s6 is used as a scratch register. */\r
129         mfhi            s6\r
130         sw                      s6, 12(s5)\r
131         mflo            s6\r
132         sw                      s6, 8(s5)\r
133 \r
134         /* Update the task stack pointer value if nesting is zero. */\r
135         la                      s6, uxInterruptNesting\r
136         lw                      s6, (s6)\r
137         addiu           s6, s6, -1\r
138         bne                     s6, zero, .+20\r
139         nop\r
140 \r
141         /* Save the stack pointer. */\r
142         la                      s6, uxSavedTaskStackPointer\r
143         sw                      s5, (s6)\r
144 \r
145         .endm\r
146         \r
147 /******************************************************************/    \r
148 .macro  portRESTORE_CONTEXT\r
149 \r
150         /* Restore the stack pointer from the TCB.  This is only done if the\r
151         nesting count is 1. */\r
152         la                      s6, uxInterruptNesting\r
153         lw                      s6, (s6)\r
154         addiu           s6, s6, -1\r
155         bne                     s6, zero, .+20\r
156         nop\r
157         la                      s6, uxSavedTaskStackPointer\r
158         lw                      s5, (s6)\r
159         \r
160         /* Restore the context. */\r
161         lw                      s6, 8(s5)\r
162         mtlo            s6\r
163         lw                      s6, 12(s5)\r
164         mthi            s6\r
165         lw                      $1, 16(s5)\r
166         /* s6 is loaded as it was used as a scratch register and therefore saved\r
167         as part of the interrupt context. */\r
168         lw                      s6, 44(s5)\r
169         lw                      v0, 52(s5)\r
170         lw                      v1, 56(s5)\r
171         lw                      a0, 60(s5)\r
172         lw                      a1, 64(s5)\r
173         lw                      a2, 68(s5)\r
174         lw                      a3, 72(s5)\r
175         lw                      t0, 76(s5)\r
176         lw                      t1, 80(s5)\r
177         lw                      t2, 84(s5)\r
178         lw                      t3, 88(s5)\r
179         lw                      t4, 92(s5)\r
180         lw                      t5, 96(s5)\r
181         lw                      t6, 100(s5)\r
182         lw                      t7, 104(s5)\r
183         lw                      t8, 108(s5)\r
184         lw                      t9, 112(s5)\r
185         lw                      s8, 116(s5)\r
186         lw                      ra, 120(s5)\r
187 \r
188         /* Protect access to the k registers, and others. */\r
189         di\r
190 \r
191         /* Decrement the nesting count. */\r
192         la                      k0, uxInterruptNesting\r
193         lw                      k1, (k0)\r
194         addiu           k1, k1, -1\r
195         sw                      k1, 0(k0)\r
196 \r
197         lw                      k0, portSTATUS_STACK_LOCATION(s5)\r
198         lw                      k1, portEPC_STACK_LOCATION(s5)\r
199 \r
200         /* Leave the stack how we found it.  First load sp from s5, then restore\r
201         s5 from the stack. */\r
202         add                     sp, zero, s5\r
203         lw                      s5, 40(sp)\r
204         addiu           sp,     sp,     portCONTEXT_SIZE\r
205 \r
206         mtc0            k0, _CP0_STATUS\r
207         ehb\r
208         mtc0            k1, _CP0_EPC\r
209         eret \r
210         nop\r
211 \r
212         .endm\r
213 \r