]> git.sur5r.net Git - freertos/blob - Source/portable/MPLAB/PIC32MX/port_asm.S
a7f65552e27c1e72c79fe0f56143dbf8b1662f4e
[freertos] / Source / portable / MPLAB / PIC32MX / port_asm.S
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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. */\r
199         di\r
200         mfc0            s7, _CP0_STATUS\r
201         ori                     s7, s7, 1\r
202         ori                     s6, s7, configMAX_SYSCALL_INTERRUPT_PRIORITY << 10\r
203 \r
204         /* This mtc0 re-enables interrupts, but only above \r
205         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
206         mtc0            s6, _CP0_STATUS\r
207 \r
208         /* Clear the software interrupt in the core. */\r
209         mfc0            s6, _CP0_CAUSE\r
210         addiu       s4,zero,-257\r
211         and                     s6, s6, s4\r
212         mtc0            s6, _CP0_CAUSE\r
213 \r
214         /* Clear the interrupt in the interrupt controller. */\r
215         la                      s6, IFS0CLR\r
216         addiu           s4, zero, 2\r
217         sw                      s4, (s6)\r
218 \r
219         jal                     vTaskSwitchContext\r
220         nop\r
221 \r
222         /* Clear the interrupt mask again.  The saved status value is still in s7. */\r
223         mtc0            s7, _CP0_STATUS\r
224 \r
225         /* Restore the stack pointer from the TCB. */\r
226         la                      s0, pxCurrentTCB\r
227         lw                      s0, (s0)\r
228         lw                      s5, (s0)\r
229 \r
230         /* Restore the rest of the context. */\r
231         lw                      s0, 8(s5)\r
232         mtlo            s0\r
233         lw                      s0, 12(s5)\r
234         mthi            s0\r
235         lw                      $1, 16(s5)\r
236         lw                      s0, 20(s5)\r
237         lw                      s1, 24(s5)\r
238         lw                      s2, 28(s5)\r
239         lw                      s3, 32(s5)\r
240         lw                      s4, 36(s5)\r
241         /* s5 is loaded later. */\r
242         lw                      s6, 44(s5)\r
243         lw                      s7, 48(s5)\r
244         lw                      v0, 52(s5)\r
245         lw                      v1, 56(s5)\r
246         lw                      a0, 60(s5)\r
247         lw                      a1, 64(s5)\r
248         lw                      a2, 68(s5)\r
249         lw                      a3, 72(s5)\r
250         lw                      t0, 76(s5)\r
251         lw                      t1, 80(s5)\r
252         lw                      t2, 84(s5)\r
253         lw                      t3, 88(s5)\r
254         lw                      t4, 92(s5)\r
255         lw                      t5, 96(s5)\r
256         lw                      t6, 100(s5)\r
257         lw                      t7, 104(s5)\r
258         lw                      t8, 108(s5)\r
259         lw                      t9, 112(s5)\r
260         lw                      s8, 116(s5)\r
261         lw                      ra, 120(s5)\r
262 \r
263         /* Protect access to the k registers, and others. */\r
264         di\r
265 \r
266         /* Set nesting back to zero.  As the lowest priority interrupt this\r
267         interrupt cannot have nested. */\r
268         la                      k0, uxInterruptNesting\r
269         sw                      zero, 0(k0)\r
270 \r
271         /* Switch back to use the real stack pointer. */\r
272         add                     sp, zero, s5\r
273 \r
274         /* Restore the real s5 value. */\r
275         lw                      s5, 40(sp)\r
276 \r
277         /* Pop the status and epc values. */\r
278         lw                      k1, portSTATUS_STACK_LOCATION(sp)\r
279         lw                      k0, portEPC_STACK_LOCATION(sp)\r
280 \r
281         /* Remove stack frame. */\r
282         addiu           sp,     sp,     portCONTEXT_SIZE\r
283 \r
284         mtc0            k1, _CP0_STATUS\r
285         ehb     \r
286         mtc0            k0, _CP0_EPC\r
287         eret \r
288         nop\r
289 \r
290         .end            vPortYieldISR\r
291 \r
292 \r
293 \r