]> git.sur5r.net Git - freertos/blob - Source/portable/MPLAB/PIC32MX/port_asm.S
ff45856ea249c82906876ef48012b817fc7e5d5a
[freertos] / Source / portable / MPLAB / PIC32MX / port_asm.S
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53  \r
54 #include <p32xxxx.h>\r
55 #include <sys/asm.h>\r
56 #include "ISR_Support.h"\r
57  \r
58 \r
59         .set    nomips16\r
60         .set    noreorder\r
61         \r
62         .extern pxCurrentTCB\r
63         .extern vTaskSwitchContext\r
64         .extern vPortIncrementTick\r
65         .extern xISRStackTop\r
66         \r
67         .global vPortStartFirstTask\r
68         .global vPortYieldISR\r
69         .global vT1InterruptHandler\r
70 \r
71 \r
72 /******************************************************************/\r
73 \r
74         .section        .FreeRTOS, "ax", @progbits\r
75         .set            noreorder\r
76         .set            noat\r
77         .ent            vT1InterruptHandler\r
78         \r
79 vT1InterruptHandler:\r
80 \r
81         portSAVE_CONTEXT\r
82 \r
83         jal             vPortIncrementTick\r
84         nop\r
85 \r
86         portRESTORE_CONTEXT\r
87 \r
88         .end vT1InterruptHandler\r
89 \r
90 /******************************************************************/\r
91 \r
92         .section        .FreeRTOS, "ax", @progbits\r
93         .set            noreorder\r
94         .set            noat\r
95         .ent            xPortStartScheduler\r
96 \r
97 vPortStartFirstTask:\r
98 \r
99         /* Simply restore the context of the highest priority task that has been\r
100         created so far. */\r
101         portRESTORE_CONTEXT\r
102 \r
103         .end xPortStartScheduler\r
104 \r
105 \r
106 \r
107 /*******************************************************************/\r
108 \r
109         .section        .FreeRTOS, "ax", @progbits\r
110         .set            noreorder\r
111         .set            noat\r
112         .ent            vPortYieldISR\r
113 \r
114 vPortYieldISR:\r
115 \r
116         /* Make room for the context. First save the current status so we can \r
117         manipulate it, and the cause and EPC registers so we capture their \r
118         original values in case of interrupt nesting. */\r
119         mfc0            k0, _CP0_CAUSE\r
120         addiu           sp,     sp, -portCONTEXT_SIZE\r
121         mfc0            k1, _CP0_STATUS\r
122 \r
123         /* Also save s6 and s5 so we can use them during this interrupt.  Any\r
124         nesting interrupts should maintain the values of these registers\r
125         across the ISR. */\r
126         sw                      s6, 44(sp)\r
127         sw                      s5, 40(sp)\r
128         sw                      k1, portSTATUS_STACK_LOCATION(sp)\r
129 \r
130         /* Enable interrupts above the current priority. */\r
131         srl                     k0, k0, 0xa\r
132         ins             k1, k0, 10, 6\r
133         ins                     k1, zero, 1, 4\r
134 \r
135         /* s5 is used as the frame pointer. */\r
136         add                     s5, zero, sp\r
137 \r
138         /* Swap to the system stack.  This is not conditional on the nesting\r
139         count as this interrupt is always the lowest priority and therefore\r
140         the nesting is always 0. */\r
141         la                      sp, xISRStackTop\r
142         lw                      sp, (sp)\r
143 \r
144         /* Set the nesting count. */\r
145         la                      k0, uxInterruptNesting\r
146         addiu           s6, zero, 1\r
147         sw                      s6, 0(k0)\r
148 \r
149         /* s6 holds the EPC value, this is saved with the rest of the context\r
150         after interrupts are enabled. */\r
151         mfc0            s6, _CP0_EPC\r
152 \r
153         /* Re-enable interrupts. */\r
154         mtc0            k1, _CP0_STATUS\r
155 \r
156         /* Save the context into the space just created.  s6 is saved again\r
157         here as it now contains the EPC value. */\r
158         sw                      ra,     120(s5)\r
159         sw                      s8, 116(s5)\r
160         sw                      t9, 112(s5)\r
161         sw                      t8,     108(s5)\r
162         sw                      t7,     104(s5)\r
163         sw                      t6, 100(s5)\r
164         sw                      t5, 96(s5)\r
165         sw                      t4, 92(s5)\r
166         sw                      t3, 88(s5)\r
167         sw                      t2, 84(s5)\r
168         sw                      t1, 80(s5)\r
169         sw                      t0, 76(s5)\r
170         sw                      a3, 72(s5)\r
171         sw                      a2, 68(s5)\r
172         sw                      a1, 64(s5)\r
173         sw                      a0, 60(s5)\r
174         sw                      v1, 56(s5)\r
175         sw                      v0, 52(s5)\r
176         sw                      s7, 48(s5)\r
177         sw                      s6, portEPC_STACK_LOCATION(s5)\r
178         /* s5 and s6 has already been saved. */\r
179         sw                      s4,     36(s5)\r
180         sw                      s3, 32(s5)\r
181         sw                      s2, 28(s5)\r
182         sw                      s1, 24(s5)\r
183         sw                      s0, 20(s5)\r
184         sw                      $1, 16(s5)\r
185 \r
186         /* s7 is used as a scratch register as this should always be saved across\r
187         nesting interrupts. */\r
188         mfhi            s7\r
189         sw                      s7, 12(s5)\r
190         mflo            s7\r
191         sw                      s7, 8(s5)\r
192 \r
193         /* Save the stack pointer to the task. */\r
194         la                      s7, pxCurrentTCB\r
195         lw                      s7, (s7)\r
196         sw                      s5, (s7)\r
197 \r
198         /* Set the interrupt mask to the max priority that can use the API.  The\r
199         yield handler will only be called at configKERNEL_INTERRUPT_PRIORITY which\r
200         is below configMAX_SYSCALL_INTERRUPT_PRIORITY - so this can only ever\r
201         raise the IPL value and never lower it. */\r
202         di\r
203         mfc0            s7, _CP0_STATUS\r
204         ins             s7, $0, 10, 6\r
205         ori                     s6, s7, ( configMAX_SYSCALL_INTERRUPT_PRIORITY << 10 ) | 1\r
206 \r
207         /* This mtc0 re-enables interrupts, but only above \r
208         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
209         mtc0            s6, _CP0_STATUS\r
210 \r
211         /* Clear the software interrupt in the core. */\r
212         mfc0            s6, _CP0_CAUSE\r
213         addiu       s4,zero,-257\r
214         and                     s6, s6, s4\r
215         mtc0            s6, _CP0_CAUSE\r
216 \r
217         /* Clear the interrupt in the interrupt controller. */\r
218         la                      s6, IFS0CLR\r
219         addiu           s4, zero, 2\r
220         sw                      s4, (s6)\r
221 \r
222         jal                     vTaskSwitchContext\r
223         nop\r
224 \r
225         /* Clear the interrupt mask again.  The saved status value is still in s7. */\r
226         mtc0            s7, _CP0_STATUS\r
227 \r
228         /* Restore the stack pointer from the TCB. */\r
229         la                      s0, pxCurrentTCB\r
230         lw                      s0, (s0)\r
231         lw                      s5, (s0)\r
232 \r
233         /* Restore the rest of the context. */\r
234         lw                      s0, 8(s5)\r
235         mtlo            s0\r
236         lw                      s0, 12(s5)\r
237         mthi            s0\r
238         lw                      $1, 16(s5)\r
239         lw                      s0, 20(s5)\r
240         lw                      s1, 24(s5)\r
241         lw                      s2, 28(s5)\r
242         lw                      s3, 32(s5)\r
243         lw                      s4, 36(s5)\r
244         /* s5 is loaded later. */\r
245         lw                      s6, 44(s5)\r
246         lw                      s7, 48(s5)\r
247         lw                      v0, 52(s5)\r
248         lw                      v1, 56(s5)\r
249         lw                      a0, 60(s5)\r
250         lw                      a1, 64(s5)\r
251         lw                      a2, 68(s5)\r
252         lw                      a3, 72(s5)\r
253         lw                      t0, 76(s5)\r
254         lw                      t1, 80(s5)\r
255         lw                      t2, 84(s5)\r
256         lw                      t3, 88(s5)\r
257         lw                      t4, 92(s5)\r
258         lw                      t5, 96(s5)\r
259         lw                      t6, 100(s5)\r
260         lw                      t7, 104(s5)\r
261         lw                      t8, 108(s5)\r
262         lw                      t9, 112(s5)\r
263         lw                      s8, 116(s5)\r
264         lw                      ra, 120(s5)\r
265 \r
266         /* Protect access to the k registers, and others. */\r
267         di\r
268 \r
269         /* Set nesting back to zero.  As the lowest priority interrupt this\r
270         interrupt cannot have nested. */\r
271         la                      k0, uxInterruptNesting\r
272         sw                      zero, 0(k0)\r
273 \r
274         /* Switch back to use the real stack pointer. */\r
275         add                     sp, zero, s5\r
276 \r
277         /* Restore the real s5 value. */\r
278         lw                      s5, 40(sp)\r
279 \r
280         /* Pop the status and epc values. */\r
281         lw                      k1, portSTATUS_STACK_LOCATION(sp)\r
282         lw                      k0, portEPC_STACK_LOCATION(sp)\r
283 \r
284         /* Remove stack frame. */\r
285         addiu           sp,     sp,     portCONTEXT_SIZE\r
286 \r
287         mtc0            k1, _CP0_STATUS \r
288         mtc0            k0, _CP0_EPC\r
289         ehb\r
290         eret \r
291         nop\r
292 \r
293         .end            vPortYieldISR\r
294 \r
295 \r
296 \r