]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MZ/ISR_Support.h
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MZ / ISR_Support.h
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 #include "FreeRTOSConfig.h"\r
71 \r
72 #define portCONTEXT_SIZE                160\r
73 #define portEPC_STACK_LOCATION          152\r
74 #define portSTATUS_STACK_LOCATION       156\r
75 #define portFPCSR_STACK_LOCATION        0\r
76 #define portTASK_HAS_FPU_STACK_LOCATION     0\r
77 #define portFPU_CONTEXT_SIZE            264\r
78 \r
79 /******************************************************************/\r
80 .macro  portSAVE_FPU_REGS    offset, base\r
81     /* Macro to assist with saving just the FPU registers to the\r
82      * specified address and base offset,\r
83      * offset is a constant, base is the base pointer register  */\r
84 \r
85         sdc1            $f31, \offset + 248(\base)\r
86         sdc1            $f30, \offset + 240(\base)\r
87         sdc1            $f29, \offset + 232(\base)\r
88         sdc1            $f28, \offset + 224(\base)\r
89         sdc1            $f27, \offset + 216(\base)\r
90         sdc1            $f26, \offset + 208(\base)\r
91         sdc1            $f25, \offset + 200(\base)\r
92         sdc1            $f24, \offset + 192(\base)\r
93         sdc1            $f23, \offset + 184(\base)\r
94         sdc1            $f22, \offset + 176(\base)\r
95         sdc1            $f21, \offset + 168(\base)\r
96         sdc1            $f20, \offset + 160(\base)\r
97         sdc1            $f19, \offset + 152(\base)\r
98         sdc1            $f18, \offset + 144(\base)\r
99         sdc1            $f17, \offset + 136(\base)\r
100         sdc1            $f16, \offset + 128(\base)\r
101         sdc1            $f15, \offset + 120(\base)\r
102         sdc1            $f14, \offset + 112(\base)\r
103         sdc1            $f13, \offset + 104(\base)\r
104         sdc1            $f12, \offset + 96(\base)\r
105         sdc1            $f11, \offset + 88(\base)\r
106         sdc1            $f10, \offset + 80(\base)\r
107         sdc1            $f9, \offset + 72(\base)\r
108         sdc1            $f8, \offset + 64(\base)\r
109         sdc1            $f7, \offset + 56(\base)\r
110         sdc1            $f6, \offset + 48(\base)\r
111         sdc1            $f5, \offset + 40(\base)\r
112         sdc1            $f4, \offset + 32(\base)\r
113         sdc1            $f3, \offset + 24(\base)\r
114         sdc1            $f2, \offset + 16(\base)\r
115         sdc1            $f1, \offset + 8(\base)\r
116         sdc1            $f0, \offset + 0(\base)\r
117 \r
118     .endm\r
119 \r
120 /******************************************************************/\r
121 .macro  portLOAD_FPU_REGS    offset, base\r
122     /* Macro to assist with loading just the FPU registers from the\r
123      * specified address and base offset, offset is a constant,\r
124      * base is the base pointer register  */\r
125 \r
126         ldc1            $f0, \offset + 0(\base)\r
127         ldc1            $f1, \offset + 8(\base)\r
128         ldc1            $f2, \offset + 16(\base)\r
129         ldc1            $f3, \offset + 24(\base)\r
130         ldc1            $f4, \offset + 32(\base)\r
131         ldc1            $f5, \offset + 40(\base)\r
132         ldc1            $f6, \offset + 48(\base)\r
133         ldc1            $f7, \offset + 56(\base)\r
134         ldc1            $f8, \offset + 64(\base)\r
135         ldc1            $f9, \offset + 72(\base)\r
136         ldc1            $f10, \offset + 80(\base)\r
137         ldc1            $f11, \offset + 88(\base)\r
138         ldc1            $f12, \offset + 96(\base)\r
139         ldc1            $f13, \offset + 104(\base)\r
140         ldc1            $f14, \offset + 112(\base)\r
141         ldc1            $f15, \offset + 120(\base)\r
142         ldc1            $f16, \offset + 128(\base)\r
143         ldc1            $f17, \offset + 136(\base)\r
144         ldc1            $f18, \offset + 144(\base)\r
145         ldc1            $f19, \offset + 152(\base)\r
146         ldc1            $f20, \offset + 160(\base)\r
147         ldc1            $f21, \offset + 168(\base)\r
148         ldc1            $f22, \offset + 176(\base)\r
149         ldc1            $f23, \offset + 184(\base)\r
150         ldc1            $f24, \offset + 192(\base)\r
151         ldc1            $f25, \offset + 200(\base)\r
152         ldc1            $f26, \offset + 208(\base)\r
153         ldc1            $f27, \offset + 216(\base)\r
154         ldc1            $f28, \offset + 224(\base)\r
155         ldc1            $f29, \offset + 232(\base)\r
156         ldc1            $f30, \offset + 240(\base)\r
157         ldc1            $f31, \offset + 248(\base)\r
158 \r
159     .endm\r
160 \r
161 /******************************************************************/\r
162 .macro  portSAVE_CONTEXT\r
163 \r
164         /* Make room for the context. First save the current status so it can be\r
165         manipulated, and the cause and EPC registers so their original values are\r
166         captured. */\r
167         mfc0            k0, _CP0_CAUSE\r
168         addiu           sp, sp, -portCONTEXT_SIZE\r
169 \r
170         #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
171                 /* Test if we are already using the system stack. Only tasks may use the\r
172                 FPU so if we are already in a nested interrupt then the FPU context does\r
173                 not require saving. */\r
174                 la                      k1, uxInterruptNesting\r
175                 lw                      k1, 0(k1)\r
176                 bne                     k1, zero, 2f\r
177                 nop\r
178 \r
179                 /* Test if the current task needs the FPU context saving. */\r
180                 la                      k1, ulTaskHasFPUContext\r
181                 lw                      k1, 0(k1)\r
182                 beq                     k1, zero, 1f\r
183                 nop\r
184 \r
185                 /* Adjust the stack to account for the additional FPU context.*/\r
186                 addiu           sp, sp, -portFPU_CONTEXT_SIZE\r
187 \r
188         1:\r
189                 /* Save the ulTaskHasFPUContext flag. */\r
190                 sw                      k1, portTASK_HAS_FPU_STACK_LOCATION(sp)\r
191 \r
192         2:\r
193         #endif\r
194 \r
195         mfc0            k1, _CP0_STATUS\r
196 \r
197         /* Also save s7, s6 and s5 so they can be used.  Any nesting interrupts\r
198         should maintain the values of these registers across the ISR. */\r
199         sw                      s7, 48(sp)\r
200         sw                      s6, 44(sp)\r
201         sw                      s5, 40(sp)\r
202         sw                      k1, portSTATUS_STACK_LOCATION(sp)\r
203 \r
204         /* Prepare to enable interrupts above the current priority. */\r
205         srl                     k0, k0, 0xa\r
206         ins             k1, k0, 10, 7\r
207         srl                     k0, k0, 0x7 /* This copies the MSB of the IPL, but it would be an error if it was set anyway. */\r
208         ins             k1, k0, 18, 1\r
209         ins                     k1, zero, 1, 4\r
210 \r
211         /* s5 is used as the frame pointer. */\r
212         add                     s5, zero, sp\r
213 \r
214         /* Check the nesting count value. */\r
215         la                      k0, uxInterruptNesting\r
216         lw                      s6, (k0)\r
217 \r
218         /* If the nesting count is 0 then swap to the the system stack, otherwise\r
219         the system stack is already being used. */\r
220         bne                     s6, zero, 1f\r
221         nop\r
222 \r
223         /* Swap to the system stack. */\r
224         la                      sp, xISRStackTop\r
225         lw                      sp, (sp)\r
226 \r
227         /* Increment and save the nesting count. */\r
228 1:      addiu           s6, s6, 1\r
229         sw                      s6, 0(k0)\r
230 \r
231         /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */\r
232         mfc0            s6, _CP0_EPC\r
233 \r
234         /* Re-enable interrupts. */\r
235         mtc0            k1, _CP0_STATUS\r
236 \r
237         /* Save the context into the space just created.  s6 is saved again\r
238         here as it now contains the EPC value.  No other s registers need be\r
239         saved. */\r
240         sw                      ra, 120(s5)\r
241         sw                      s8, 116(s5)\r
242         sw                      t9, 112(s5)\r
243         sw                      t8, 108(s5)\r
244         sw                      t7, 104(s5)\r
245         sw                      t6, 100(s5)\r
246         sw                      t5, 96(s5)\r
247         sw                      t4, 92(s5)\r
248         sw                      t3, 88(s5)\r
249         sw                      t2, 84(s5)\r
250         sw                      t1, 80(s5)\r
251         sw                      t0, 76(s5)\r
252         sw                      a3, 72(s5)\r
253         sw                      a2, 68(s5)\r
254         sw                      a1, 64(s5)\r
255         sw                      a0, 60(s5)\r
256         sw                      v1, 56(s5)\r
257         sw                      v0, 52(s5)\r
258         sw                      s6, portEPC_STACK_LOCATION(s5)\r
259         sw                      $1, 16(s5)\r
260 \r
261         /* Save the AC0, AC1, AC2, AC3 registers from the DSP.  s6 is used as a\r
262         scratch register. */\r
263         mfhi            s6, $ac1\r
264         sw                      s6, 128(s5)\r
265         mflo            s6, $ac1\r
266         sw                      s6, 124(s5)\r
267 \r
268         mfhi            s6, $ac2\r
269         sw                      s6, 136(s5)\r
270         mflo            s6, $ac2\r
271         sw                      s6, 132(s5)\r
272 \r
273         mfhi            s6, $ac3\r
274         sw                      s6, 144(s5)\r
275         mflo            s6, $ac3\r
276         sw                      s6, 140(s5)\r
277 \r
278         /* Save the DSP Control register */\r
279         rddsp           s6\r
280         sw                      s6, 148(s5)\r
281 \r
282         /* ac0 is done separately to match the MX port. */\r
283         mfhi            s6, $ac0\r
284         sw                      s6, 12(s5)\r
285         mflo            s6, $ac0\r
286         sw                      s6, 8(s5)\r
287 \r
288         /* Save the FPU context if the nesting count was zero. */\r
289         #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
290                 la                      s6, uxInterruptNesting\r
291                 lw                      s6, 0(s6)\r
292                 addiu           s6, s6, -1\r
293                 bne                     s6, zero, 1f\r
294                 nop\r
295 \r
296                 /* Test if the current task needs the FPU context saving. */\r
297                 lw                      s6, portTASK_HAS_FPU_STACK_LOCATION(s5)\r
298                 beq                     s6, zero, 1f\r
299                 nop\r
300 \r
301                 /* Save the FPU registers. */\r
302                 portSAVE_FPU_REGS ( portCONTEXT_SIZE + 8 ), s5\r
303 \r
304                 /* Save the FPU status register */\r
305                 cfc1            s6, $f31\r
306                 sw                      s6, (portCONTEXT_SIZE + portFPCSR_STACK_LOCATION)(s5)\r
307 \r
308                 1:\r
309         #endif\r
310 \r
311         /* Update the task stack pointer value if nesting is zero. */\r
312         la                      s6, uxInterruptNesting\r
313         lw                      s6, (s6)\r
314         addiu           s6, s6, -1\r
315         bne                     s6, zero, 1f\r
316         nop\r
317 \r
318         /* Save the stack pointer. */\r
319         la                      s6, uxSavedTaskStackPointer\r
320         sw                      s5, (s6)\r
321 1:\r
322         .endm\r
323 \r
324 /******************************************************************/\r
325 .macro  portRESTORE_CONTEXT\r
326 \r
327         /* Restore the stack pointer from the TCB.  This is only done if the\r
328         nesting count is 1. */\r
329         la                      s6, uxInterruptNesting\r
330         lw                      s6, (s6)\r
331         addiu           s6, s6, -1\r
332         bne                     s6, zero, 1f\r
333         nop\r
334         la                      s6, uxSavedTaskStackPointer\r
335         lw                      s5, (s6)\r
336 \r
337     #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
338                 /* Restore the FPU context if required. */\r
339                 lw                      s6, portTASK_HAS_FPU_STACK_LOCATION(s5)\r
340                 beq                     s6, zero, 1f\r
341                 nop\r
342 \r
343                 /* Restore the FPU registers. */\r
344                 portLOAD_FPU_REGS   ( portCONTEXT_SIZE + 8 ), s5\r
345 \r
346                 /* Restore the FPU status register. */\r
347                 lw                      s6, ( portCONTEXT_SIZE + portFPCSR_STACK_LOCATION )(s5)\r
348                 ctc1            s6, $f31\r
349         #endif\r
350 \r
351 1:\r
352 \r
353         /* Restore the context. */\r
354         lw                      s6, 128(s5)\r
355         mthi            s6, $ac1\r
356         lw                      s6, 124(s5)\r
357         mtlo            s6, $ac1\r
358 \r
359         lw                      s6, 136(s5)\r
360         mthi            s6, $ac2\r
361         lw                      s6, 132(s5)\r
362         mtlo            s6, $ac2\r
363 \r
364         lw                      s6, 144(s5)\r
365         mthi            s6, $ac3\r
366         lw                      s6, 140(s5)\r
367         mtlo            s6, $ac3\r
368 \r
369         /* Restore DSPControl. */\r
370         lw                      s6, 148(s5)\r
371         wrdsp           s6\r
372 \r
373         lw                      s6, 8(s5)\r
374         mtlo            s6, $ac0\r
375         lw                      s6, 12(s5)\r
376         mthi            s6, $ac0\r
377         lw                      $1, 16(s5)\r
378 \r
379         /* s6 is loaded as it was used as a scratch register and therefore saved\r
380         as part of the interrupt context. */\r
381         lw                      s7, 48(s5)\r
382         lw                      s6, 44(s5)\r
383         lw                      v0, 52(s5)\r
384         lw                      v1, 56(s5)\r
385         lw                      a0, 60(s5)\r
386         lw                      a1, 64(s5)\r
387         lw                      a2, 68(s5)\r
388         lw                      a3, 72(s5)\r
389         lw                      t0, 76(s5)\r
390         lw                      t1, 80(s5)\r
391         lw                      t2, 84(s5)\r
392         lw                      t3, 88(s5)\r
393         lw                      t4, 92(s5)\r
394         lw                      t5, 96(s5)\r
395         lw                      t6, 100(s5)\r
396         lw                      t7, 104(s5)\r
397         lw                      t8, 108(s5)\r
398         lw                      t9, 112(s5)\r
399         lw                      s8, 116(s5)\r
400         lw                      ra, 120(s5)\r
401 \r
402         /* Protect access to the k registers, and others. */\r
403         di\r
404         ehb\r
405 \r
406         /* Decrement the nesting count. */\r
407         la                      k0, uxInterruptNesting\r
408         lw                      k1, (k0)\r
409         addiu           k1, k1, -1\r
410         sw                      k1, 0(k0)\r
411 \r
412         #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
413                 /* If the nesting count is now zero then the FPU context may be restored. */\r
414                 bne                     k1, zero, 1f\r
415                 nop\r
416 \r
417                 /* Restore the value of ulTaskHasFPUContext */\r
418                 la                      k0, ulTaskHasFPUContext\r
419                 lw                      k1, 0(s5)\r
420                 sw                      k1, 0(k0)\r
421 \r
422                 /* If the task does not have an FPU context then adjust the stack normally. */\r
423                 beq                     k1, zero, 1f\r
424                 nop\r
425 \r
426                 /* Restore the STATUS and EPC registers */\r
427                 lw                      k0, portSTATUS_STACK_LOCATION(s5)\r
428                 lw                      k1, portEPC_STACK_LOCATION(s5)\r
429 \r
430                 /* Leave the stack in its original state.  First load sp from s5, then\r
431                 restore s5 from the stack. */\r
432                 add                     sp, zero, s5\r
433                 lw                      s5, 40(sp)\r
434 \r
435                 /* Adjust the stack pointer to remove the FPU context */\r
436                 addiu           sp, sp, portFPU_CONTEXT_SIZE\r
437                 beq                     zero, zero, 2f\r
438                 nop\r
439 \r
440                 1:  /* Restore the STATUS and EPC registers */\r
441                 lw                      k0, portSTATUS_STACK_LOCATION(s5)\r
442                 lw                      k1, portEPC_STACK_LOCATION(s5)\r
443 \r
444                 /* Leave the stack in its original state.  First load sp from s5, then\r
445                 restore s5 from the stack. */\r
446                 add                     sp, zero, s5\r
447                 lw                      s5, 40(sp)\r
448 \r
449                 2:  /* Adjust the stack pointer */\r
450                 addiu           sp, sp, portCONTEXT_SIZE\r
451 \r
452         #else\r
453 \r
454                 /* Restore the frame when there is no hardware FP support. */\r
455                 lw                      k0, portSTATUS_STACK_LOCATION(s5)\r
456                 lw                      k1, portEPC_STACK_LOCATION(s5)\r
457 \r
458                 /* Leave the stack in its original state.  First load sp from s5, then\r
459                 restore s5 from the stack. */\r
460                 add                     sp, zero, s5\r
461                 lw                      s5, 40(sp)\r
462 \r
463                 addiu           sp, sp, portCONTEXT_SIZE\r
464 \r
465         #endif // ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
466 \r
467         mtc0            k0, _CP0_STATUS\r
468         mtc0            k1, _CP0_EPC\r
469         ehb\r
470         eret\r
471         nop\r
472 \r
473         .endm\r
474 \r