]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MX/port_asm.S
ddada0b4cce45622cc5c31d1a37d4a91f1145d6b
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MX / port_asm.S
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 #include <xc.h>\r
97 #include <sys/asm.h>\r
98 #include "ISR_Support.h"\r
99 \r
100 \r
101         .set    nomips16\r
102         .set    noreorder\r
103 \r
104         .extern pxCurrentTCB\r
105         .extern vTaskSwitchContext\r
106         .extern vPortIncrementTick\r
107         .extern xISRStackTop\r
108 \r
109         .global vPortStartFirstTask\r
110         .global vPortYieldISR\r
111         .global vPortTickInterruptHandler\r
112 \r
113 \r
114 /******************************************************************/\r
115 \r
116         .set            noreorder\r
117         .set            noat\r
118         .ent            vPortTickInterruptHandler\r
119 \r
120 vPortTickInterruptHandler:\r
121 \r
122         portSAVE_CONTEXT\r
123 \r
124         jal             vPortIncrementTick\r
125         nop\r
126 \r
127         portRESTORE_CONTEXT\r
128 \r
129         .end vPortTickInterruptHandler\r
130 \r
131 /******************************************************************/\r
132 \r
133         .set            noreorder\r
134         .set            noat\r
135         .ent            vPortStartFirstTask\r
136 \r
137 vPortStartFirstTask:\r
138 \r
139         /* Simply restore the context of the highest priority task that has been\r
140         created so far. */\r
141         portRESTORE_CONTEXT\r
142 \r
143         .end vPortStartFirstTask\r
144 \r
145 \r
146 \r
147 /*******************************************************************/\r
148 \r
149         .set            noreorder\r
150         .set            noat\r
151         .ent            vPortYieldISR\r
152 \r
153 vPortYieldISR:\r
154 \r
155         /* Make room for the context. First save the current status so it can be\r
156         manipulated. */\r
157         addiu           sp, sp, -portCONTEXT_SIZE\r
158         mfc0            k1, _CP0_STATUS\r
159 \r
160         /* Also save s6 and s5 so they can be used.  Any nesting interrupts should\r
161         maintain the values of these registers across the ISR. */\r
162         sw                      s6, 44(sp)\r
163         sw                      s5, 40(sp)\r
164         sw                      k1, portSTATUS_STACK_LOCATION(sp)\r
165 \r
166         /* Prepare to re-enabled interrupt above the kernel priority. */\r
167         ins             k1, zero, 10, 6\r
168         ori                     k1, k1, ( configMAX_SYSCALL_INTERRUPT_PRIORITY << 10 )\r
169         ins                     k1, zero, 1, 4\r
170 \r
171         /* s5 is used as the frame pointer. */\r
172         add                     s5, zero, sp\r
173 \r
174         /* Swap to the system stack.  This is not conditional on the nesting\r
175         count as this interrupt is always the lowest priority and therefore\r
176         the nesting is always 0. */\r
177         la                      sp, xISRStackTop\r
178         lw                      sp, (sp)\r
179 \r
180         /* Set the nesting count. */\r
181         la                      k0, uxInterruptNesting\r
182         addiu           s6, zero, 1\r
183         sw                      s6, 0(k0)\r
184 \r
185         /* s6 holds the EPC value, this is saved with the rest of the context\r
186         after interrupts are enabled. */\r
187         mfc0            s6, _CP0_EPC\r
188 \r
189         /* Re-enable interrupts above configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
190         mtc0            k1, _CP0_STATUS\r
191 \r
192         /* Save the context into the space just created.  s6 is saved again\r
193         here as it now contains the EPC value. */\r
194         sw                      ra, 120(s5)\r
195         sw                      s8, 116(s5)\r
196         sw                      t9, 112(s5)\r
197         sw                      t8, 108(s5)\r
198         sw                      t7, 104(s5)\r
199         sw                      t6, 100(s5)\r
200         sw                      t5, 96(s5)\r
201         sw                      t4, 92(s5)\r
202         sw                      t3, 88(s5)\r
203         sw                      t2, 84(s5)\r
204         sw                      t1, 80(s5)\r
205         sw                      t0, 76(s5)\r
206         sw                      a3, 72(s5)\r
207         sw                      a2, 68(s5)\r
208         sw                      a1, 64(s5)\r
209         sw                      a0, 60(s5)\r
210         sw                      v1, 56(s5)\r
211         sw                      v0, 52(s5)\r
212         sw                      s7, 48(s5)\r
213         sw                      s6, portEPC_STACK_LOCATION(s5)\r
214         /* s5 and s6 has already been saved. */\r
215         sw                      s4, 36(s5)\r
216         sw                      s3, 32(s5)\r
217         sw                      s2, 28(s5)\r
218         sw                      s1, 24(s5)\r
219         sw                      s0, 20(s5)\r
220         sw                      $1, 16(s5)\r
221 \r
222         /* s7 is used as a scratch register as this should always be saved across\r
223         nesting interrupts. */\r
224         mfhi            s7\r
225         sw                      s7, 12(s5)\r
226         mflo            s7\r
227         sw                      s7, 8(s5)\r
228 \r
229         /* Save the stack pointer to the task. */\r
230         la                      s7, pxCurrentTCB\r
231         lw                      s7, (s7)\r
232         sw                      s5, (s7)\r
233 \r
234         /* Set the interrupt mask to the max priority that can use the API.  The\r
235         yield handler will only be called at configKERNEL_INTERRUPT_PRIORITY which\r
236         is below configMAX_SYSCALL_INTERRUPT_PRIORITY - so this can only ever\r
237         raise the IPL value and never lower it. */\r
238         di\r
239         ehb\r
240         mfc0            s7, _CP0_STATUS\r
241         ins             s7, zero, 10, 6\r
242         ori                     s6, s7, ( configMAX_SYSCALL_INTERRUPT_PRIORITY << 10 ) | 1\r
243 \r
244         /* This mtc0 re-enables interrupts, but only above\r
245         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
246         mtc0            s6, _CP0_STATUS\r
247         ehb\r
248 \r
249         /* Clear the software interrupt in the core. */\r
250         mfc0            s6, _CP0_CAUSE\r
251         ins                     s6, zero, 8, 1\r
252         mtc0            s6, _CP0_CAUSE\r
253         ehb\r
254 \r
255         /* Clear the interrupt in the interrupt controller. */\r
256         la                      s6, IFS0CLR\r
257         addiu           s4, zero, 2\r
258         sw                      s4, (s6)\r
259 \r
260         jal                     vTaskSwitchContext\r
261         nop\r
262 \r
263         /* Clear the interrupt mask again.  The saved status value is still in s7. */\r
264         mtc0            s7, _CP0_STATUS\r
265         ehb\r
266 \r
267         /* Restore the stack pointer from the TCB. */\r
268         la                      s0, pxCurrentTCB\r
269         lw                      s0, (s0)\r
270         lw                      s5, (s0)\r
271 \r
272         /* Restore the rest of the context. */\r
273         lw                      s0, 8(s5)\r
274         mtlo            s0\r
275         lw                      s0, 12(s5)\r
276         mthi            s0\r
277         lw                      $1, 16(s5)\r
278         lw                      s0, 20(s5)\r
279         lw                      s1, 24(s5)\r
280         lw                      s2, 28(s5)\r
281         lw                      s3, 32(s5)\r
282         lw                      s4, 36(s5)\r
283         /* s5 is loaded later. */\r
284         lw                      s6, 44(s5)\r
285         lw                      s7, 48(s5)\r
286         lw                      v0, 52(s5)\r
287         lw                      v1, 56(s5)\r
288         lw                      a0, 60(s5)\r
289         lw                      a1, 64(s5)\r
290         lw                      a2, 68(s5)\r
291         lw                      a3, 72(s5)\r
292         lw                      t0, 76(s5)\r
293         lw                      t1, 80(s5)\r
294         lw                      t2, 84(s5)\r
295         lw                      t3, 88(s5)\r
296         lw                      t4, 92(s5)\r
297         lw                      t5, 96(s5)\r
298         lw                      t6, 100(s5)\r
299         lw                      t7, 104(s5)\r
300         lw                      t8, 108(s5)\r
301         lw                      t9, 112(s5)\r
302         lw                      s8, 116(s5)\r
303         lw                      ra, 120(s5)\r
304 \r
305         /* Protect access to the k registers, and others. */\r
306         di\r
307         ehb\r
308 \r
309         /* Set nesting back to zero.  As the lowest priority interrupt this\r
310         interrupt cannot have nested. */\r
311         la                      k0, uxInterruptNesting\r
312         sw                      zero, 0(k0)\r
313 \r
314         /* Switch back to use the real stack pointer. */\r
315         add                     sp, zero, s5\r
316 \r
317         /* Restore the real s5 value. */\r
318         lw                      s5, 40(sp)\r
319 \r
320         /* Pop the status and epc values. */\r
321         lw                      k1, portSTATUS_STACK_LOCATION(sp)\r
322         lw                      k0, portEPC_STACK_LOCATION(sp)\r
323 \r
324         /* Remove stack frame. */\r
325         addiu           sp, sp, portCONTEXT_SIZE\r
326 \r
327         mtc0            k1, _CP0_STATUS\r
328         mtc0            k0, _CP0_EPC\r
329         ehb\r
330         eret\r
331         nop\r
332 \r
333         .end            vPortYieldISR\r
334 \r
335 \r
336 \r