]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MEC14xx/port_asm.S
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MEC14xx / port_asm.S
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* FreeRTOS includes. */\r
30 #include "FreeRTOSConfig.h"\r
31 #include "ISR_Support.h"\r
32 \r
33 /* Microchip includes. */\r
34 #include <xc.h>\r
35 #include <sys/asm.h>\r
36 \r
37         .extern pxCurrentTCB\r
38         .extern vTaskSwitchContext\r
39         .extern vPortIncrementTick\r
40         .extern xISRStackTop\r
41 \r
42         PORT_CPP_JTVIC_BASE = 0xBFFFC000\r
43         PORT_CCP_JTVIC_GIRQ24_SRC = 0xBFFFC100\r
44 \r
45         .global vPortStartFirstTask .text\r
46         .global vPortYieldISR .text\r
47         .global vPortTickInterruptHandler .text\r
48 \r
49 \r
50 /******************************************************************/\r
51 \r
52 \r
53 /***************************************************************\r
54 *  The following is needed to locate the\r
55 *  vPortTickInterruptHandler function into the correct vector\r
56 *  MEC14xx - This ISR will only be used if HW timers' interrupts\r
57 *  in GIRQ23 are disaggregated.\r
58 *\r
59 ***************************************************************/\r
60 \r
61         .set  noreorder\r
62         .set  noat\r
63         .set  micromips\r
64 \r
65         .section .text, code\r
66         .ent    vPortTickInterruptHandler\r
67 \r
68 #if configTIMERS_DISAGGREGATED_ISRS == 0\r
69 \r
70         .globl girq23_isr\r
71 \r
72 girq23_isr:\r
73 vPortTickInterruptHandler:\r
74 \r
75         portSAVE_CONTEXT\r
76 \r
77         jal             girq23_handler\r
78         nop\r
79 \r
80         portRESTORE_CONTEXT\r
81 \r
82 .end vPortTickInterruptHandler\r
83 \r
84 #else\r
85 \r
86         .globl girq23_b4\r
87 \r
88 girq23_b4:\r
89 vPortTickInterruptHandler:\r
90 \r
91         portSAVE_CONTEXT\r
92 \r
93         jal             vPortIncrementTick\r
94         nop\r
95 \r
96         portRESTORE_CONTEXT\r
97 \r
98 .end vPortTickInterruptHandler\r
99 \r
100 #endif /* #if configTIMERS_DISAGGREGATED_ISRS == 0 */\r
101 \r
102 /******************************************************************/\r
103 \r
104         .set    micromips\r
105         .set    noreorder\r
106         .set    noat\r
107 \r
108         .section .text, code\r
109         .ent    vPortStartFirstTask\r
110 \r
111 vPortStartFirstTask:\r
112 \r
113         /* Simply restore the context of the highest priority task that has\r
114         been created so far. */\r
115         portRESTORE_CONTEXT\r
116 \r
117 .end vPortStartFirstTask\r
118 \r
119 \r
120 \r
121 /*******************************************************************/\r
122 \r
123 /***************************************************************\r
124 *  The following is needed to locate the vPortYieldISR function into the correct\r
125 * vector.\r
126 ***************************************************************/\r
127 \r
128         .set micromips\r
129         .set noreorder\r
130         .set noat\r
131 \r
132         .section .text, code\r
133 \r
134         .global vPortYieldISR\r
135 \r
136 \r
137 #if configCPU_DISAGGREGATED_ISRS == 0\r
138         .global girq24_isr\r
139         .ent girq24_isr\r
140 girq24_isr:\r
141         la              k0, PORT_CPP_JTVIC_BASE\r
142         lw              k0, 0x10C(k0)\r
143         andi    k1, k0, 0x2\r
144         bgtz    k1, vPortYieldISR\r
145         nop\r
146 \r
147         portSAVE_CONTEXT\r
148 \r
149         jal             girq24_b_0_2\r
150 \r
151         portRESTORE_CONTEXT\r
152 \r
153         .end girq24_isr\r
154 \r
155 #else\r
156         .global girq24_b1\r
157 girq24_b1:\r
158 #endif\r
159                 .ent  vPortYieldISR\r
160 vPortYieldISR:\r
161 \r
162         /* Make room for the context. First save the current status so it can be\r
163         manipulated, and the cause and EPC registers so thier original values\r
164         are captured. */\r
165         addiu   sp, sp, -portCONTEXT_SIZE\r
166         mfc0    k1, _CP0_STATUS\r
167 \r
168         /* Also save s6 and s5 so they can be used.  Any nesting interrupts should\r
169         maintain the values of these registers across the ISR. */\r
170         sw              s6, 44(sp)\r
171         sw              s5, 40(sp)\r
172         sw              k1, portSTATUS_STACK_LOCATION(sp)\r
173 \r
174         /* Prepare to re-enable interrupts above the kernel priority. */\r
175         ins             k1, zero, 10, 7          /* Clear IPL bits 0:6. */\r
176         ins             k1, zero, 18, 1          /* Clear IPL bit 7  */\r
177         ori             k1, k1, ( configMAX_SYSCALL_INTERRUPT_PRIORITY << 10 )\r
178         ins             k1, zero, 1, 4            /* Clear EXL, ERL and UM. */\r
179 \r
180         /* s5 is used as the frame pointer. */\r
181         add             s5, zero, sp\r
182 \r
183         /* Swap to the system stack.  This is not conditional on the nesting\r
184         count as this interrupt is always the lowest priority and therefore\r
185         the nesting is always 0. */\r
186         la              sp, xISRStackTop\r
187         lw              sp, (sp)\r
188 \r
189         /* Set the nesting count. */\r
190         la              k0, uxInterruptNesting\r
191         addiu   s6, zero, 1\r
192         sw              s6, 0(k0)\r
193 \r
194         /* s6 holds the EPC value, this is saved with the rest of the context\r
195         after interrupts are enabled. */\r
196         mfc0    s6, _CP0_EPC\r
197 \r
198         /* Re-enable interrupts above configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
199         mtc0    k1, _CP0_STATUS\r
200 \r
201         /* Save the context into the space just created.  s6 is saved again\r
202         here as it now contains the EPC value. */\r
203         sw              ra, 120(s5)\r
204         sw              s8, 116(s5)\r
205         sw              t9, 112(s5)\r
206         sw              t8, 108(s5)\r
207         sw              t7, 104(s5)\r
208         sw              t6, 100(s5)\r
209         sw              t5, 96(s5)\r
210         sw              t4, 92(s5)\r
211         sw              t3, 88(s5)\r
212         sw              t2, 84(s5)\r
213         sw              t1, 80(s5)\r
214         sw              t0, 76(s5)\r
215         sw              a3, 72(s5)\r
216         sw              a2, 68(s5)\r
217         sw              a1, 64(s5)\r
218         sw              a0, 60(s5)\r
219         sw              v1, 56(s5)\r
220         sw              v0, 52(s5)\r
221         sw              s7, 48(s5)\r
222         sw              s6, portEPC_STACK_LOCATION(s5)\r
223         /* s5 and s6 has already been saved. */\r
224         sw              s4, 36(s5)\r
225         sw              s3, 32(s5)\r
226         sw              s2, 28(s5)\r
227         sw              s1, 24(s5)\r
228         sw              s0, 20(s5)\r
229         sw              $1, 16(s5)\r
230 \r
231         /* s7 is used as a scratch register as this should always be saved acro ss\r
232         nesting interrupts. */\r
233         mfhi    s7\r
234         sw              s7, 12(s5)\r
235         mflo    s7\r
236         sw              s7, 8(s5)\r
237 \r
238         /* Save the stack pointer to the task. */\r
239         la              s7, pxCurrentTCB\r
240         lw              s7, (s7)\r
241         sw              s5, (s7)\r
242 \r
243         /* Set the interrupt mask to the max priority that can use the API.\r
244         The yield handler will only be called at configKERNEL_INTERRUPT_PRIORITY\r
245         which is below configMAX_SYSCALL_INTERRUPT_PRIORITY - so this can only\r
246         ever raise the IPL value and never lower it. */\r
247         di\r
248         ehb\r
249         mfc0    s7, _CP0_STATUS\r
250         ins             s7, zero, 10, 7\r
251         ins             s7, zero, 18, 1\r
252         ori             s6, s7, ( configMAX_SYSCALL_INTERRUPT_PRIORITY << 10 ) | 1\r
253 \r
254         /* This mtc0 re-enables interrupts, but only above\r
255         configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
256         mtc0    s6, _CP0_STATUS\r
257         ehb\r
258 \r
259         /* Clear the software interrupt in the core. */\r
260         mfc0    s6, _CP0_CAUSE\r
261         ins             s6, zero, 8, 1\r
262         mtc0    s6, _CP0_CAUSE\r
263         ehb\r
264 \r
265         /* Clear the interrupt in the interrupt controller.\r
266         MEC14xx GIRQ24 Source bit[1] = 1 to clear */\r
267         la              s6, PORT_CCP_JTVIC_GIRQ24_SRC\r
268         addiu   s4, zero, 2\r
269         sw              s4, (s6)\r
270         jal             vTaskSwitchContext\r
271         nop\r
272 \r
273         /* Clear the interrupt mask again. The saved status value is still in s7 */\r
274         mtc0    s7, _CP0_STATUS\r
275         ehb\r
276 \r
277         /* Restore the stack pointer from the TCB. */\r
278         la              s0, pxCurrentTCB\r
279         lw              s0, (s0)\r
280         lw              s5, (s0)\r
281 \r
282         /* Restore the rest of the context. */\r
283         lw              s0, 8(s5)\r
284         mtlo    s0\r
285         lw              s0, 12(s5)\r
286         mthi    s0\r
287 \r
288         lw              $1, 16(s5)\r
289         lw              s0, 20(s5)\r
290         lw              s1, 24(s5)\r
291         lw              s2, 28(s5)\r
292         lw              s3, 32(s5)\r
293         lw              s4, 36(s5)\r
294 \r
295         /* s5 is loaded later. */\r
296         lw              s6, 44(s5)\r
297         lw              s7, 48(s5)\r
298         lw              v0, 52(s5)\r
299         lw              v1, 56(s5)\r
300         lw              a0, 60(s5)\r
301         lw              a1, 64(s5)\r
302         lw              a2, 68(s5)\r
303         lw              a3, 72(s5)\r
304         lw              t0, 76(s5)\r
305         lw              t1, 80(s5)\r
306         lw              t2, 84(s5)\r
307         lw              t3, 88(s5)\r
308         lw              t4, 92(s5)\r
309         lw              t5, 96(s5)\r
310         lw              t6, 100(s5)\r
311         lw              t7, 104(s5)\r
312         lw              t8, 108(s5)\r
313         lw              t9, 112(s5)\r
314         lw              s8, 116(s5)\r
315         lw              ra, 120(s5)\r
316 \r
317         /* Protect access to the k registers, and others. */\r
318         di\r
319         ehb\r
320 \r
321         /* Set nesting back to zero.  As the lowest priority interrupt this\r
322         interrupt cannot have nested. */\r
323         la              k0, uxInterruptNesting\r
324         sw              zero, 0(k0)\r
325 \r
326         /* Switch back to use the real stack pointer. */\r
327         add             sp, zero, s5\r
328 \r
329         /* Restore the real s5 value. */\r
330         lw              s5, 40(sp)\r
331 \r
332         /* Pop the status and epc values. */\r
333         lw              k1, portSTATUS_STACK_LOCATION(sp)\r
334         lw              k0, portEPC_STACK_LOCATION(sp)\r
335 \r
336         /* Remove stack frame. */\r
337         addiu   sp, sp, portCONTEXT_SIZE\r
338 \r
339         mtc0    k1, _CP0_STATUS\r
340         mtc0    k0, _CP0_EPC\r
341         ehb\r
342         eret\r
343         nop\r
344 \r
345 .end    vPortYieldISR\r
346 \r
347 \r
348 \r
349 \r