]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CA9/portASM.S
Update version number in preparation for maintenance release.
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CA9 / portASM.S
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
9      *    Complete, revised, and edited pdf reference manuals are also       *\r
10      *    available.                                                         *\r
11      *                                                                       *\r
12      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
13      *    ensuring you get running as quickly as possible and with an        *\r
14      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
15      *    the FreeRTOS project to continue with its mission of providing     *\r
16      *    professional grade, cross platform, de facto standard solutions    *\r
17      *    for microcontrollers - completely free of charge!                  *\r
18      *                                                                       *\r
19      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
20      *                                                                       *\r
21      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
22      *                                                                       *\r
23     ***************************************************************************\r
24 \r
25 \r
26     This file is part of the FreeRTOS distribution.\r
27 \r
28     FreeRTOS is free software; you can redistribute it and/or modify it under\r
29     the terms of the GNU General Public License (version 2) as published by the\r
30     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
31     >>>NOTE<<< The modification to the GPL is included to allow you to\r
32     distribute a combined work that includes FreeRTOS without being obliged to\r
33     provide the source code for proprietary components outside of the FreeRTOS\r
34     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
35     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
36     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
37     more details. You should have received a copy of the GNU General Public\r
38     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
39     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
40     by writing to Richard Barry, contact details for whom are available on the\r
41     FreeRTOS WEB site.\r
42 \r
43     1 tab == 4 spaces!\r
44 \r
45     http://www.FreeRTOS.org - Documentation, latest information, license and\r
46     contact details.\r
47 \r
48     http://www.SafeRTOS.com - A version that is certified for use in safety\r
49     critical systems.\r
50 \r
51     http://www.OpenRTOS.com - Commercial support, development, porting,\r
52     licensing and training services.\r
53 */\r
54 \r
55         .text\r
56         .arm\r
57 \r
58         .set SYS_MODE,  0x1f\r
59         .set SVC_MODE,  0x13\r
60         .set IRQ_MODE,  0x12\r
61 \r
62         /* Hardware registers. */\r
63         .extern ulICCIAR\r
64         .extern ulICCEOIR\r
65         .extern ulICCPMR\r
66 \r
67         /* Variables and functions. */\r
68         .extern ulMaxAPIPriorityMask\r
69         .extern _freertos_vector_table\r
70         .extern pxCurrentTCB\r
71         .extern vTaskSwitchContext\r
72         .extern vApplicationIRQHandler\r
73         .extern ulPortInterruptNesting\r
74         .extern ulPortTaskHasFPUContext\r
75 \r
76         .global FreeRTOS_IRQ_Handler\r
77         .global FreeRTOS_SWI_Handler\r
78         .global vPortRestoreTaskContext\r
79 \r
80 \r
81 \r
82 \r
83 .macro portSAVE_CONTEXT\r
84 \r
85         /* Save the LR and SPSR onto the system mode stack before switching to\r
86         system mode to save the remaining system mode registers. */\r
87         SRSDB   sp!, #SYS_MODE\r
88         CPS             #SYS_MODE\r
89         PUSH    {R0-R12, R14}\r
90 \r
91         /* Push the critical nesting count. */\r
92         LDR             R2, ulCriticalNestingConst\r
93         LDR             R1, [R2]\r
94         PUSH    {R1}\r
95 \r
96         /* Does the task have a floating point context that needs saving?  If\r
97         ulPortTaskHasFPUContext is 0 then no. */\r
98         LDR             R2, ulPortTaskHasFPUContextConst\r
99         LDR             R3, [R2]\r
100         CMP             R3, #0\r
101 \r
102         /* Save the floating point context, if any. */\r
103         FMRXNE  R1,  FPSCR\r
104         VPUSHNE {D0-D15}\r
105         VPUSHNE {D16-D31}\r
106         PUSHNE  {R1}\r
107 \r
108         /* Save ulPortTaskHasFPUContext itself. */\r
109         PUSH    {R3}\r
110 \r
111         /* Save the stack pointer in the TCB. */\r
112         LDR             R0, pxCurrentTCBConst\r
113         LDR             R1, [R0]\r
114         STR             SP, [R1]\r
115 \r
116         .endm\r
117 \r
118 ; /**********************************************************************/\r
119 \r
120 .macro portRESTORE_CONTEXT\r
121 \r
122         /* Set the SP to point to the stack of the task being restored. */\r
123         LDR             R0, pxCurrentTCBConst\r
124         LDR             R1, [R0]\r
125         LDR             SP, [R1]\r
126 \r
127         /* Is there a floating point context to restore?  If the restored\r
128         ulPortTaskHasFPUContext is zero then no. */\r
129         LDR             R0, ulPortTaskHasFPUContextConst\r
130         POP             {R1}\r
131         STR             R1, [R0]\r
132         CMP             R1, #0\r
133 \r
134         /* Restore the floating point context, if any. */\r
135         POPNE   {R0}\r
136         VPOPNE  {D16-D31}\r
137         VPOPNE  {D0-D15}\r
138         VMSRNE  FPSCR, R0\r
139 \r
140         /* Restore the critical section nesting depth. */\r
141         LDR             R0, ulCriticalNestingConst\r
142         POP             {R1}\r
143         STR             R1, [R0]\r
144 \r
145         /* Ensure the priority mask is correct for the critical nesting depth. */\r
146         LDR             R2, ulICCPMRConst\r
147         LDR             R2, [R2]\r
148         CMP             R1, #0\r
149         MOVEQ   R4, #255\r
150         LDRNE   R4, ulMaxAPIPriorityMaskConst\r
151         LDRNE   R4, [R4]\r
152         STR             R4, [R2]\r
153 \r
154         /* Restore all system mode registers other than the SP (which is already\r
155         being used). */\r
156         POP             {R0-R12, R14}\r
157 \r
158         /* Return to the task code, loading CPSR on the way. */\r
159         RFEIA   sp!\r
160 \r
161         .endm\r
162 \r
163 \r
164 \r
165 \r
166 /******************************************************************************\r
167  * SVC handler is used to start the scheduler.\r
168  *****************************************************************************/\r
169 .align 4\r
170 .type FreeRTOS_SWI_Handler, %function\r
171 FreeRTOS_SWI_Handler:\r
172         /* Save the context of the current task and select a new task to run. */\r
173         portSAVE_CONTEXT\r
174         LDR R0, vTaskSwitchContextConst\r
175         BLX     R0\r
176         portRESTORE_CONTEXT\r
177 \r
178 \r
179 /******************************************************************************\r
180  * vPortRestoreTaskContext is used to start the scheduler.\r
181  *****************************************************************************/\r
182 .type vPortRestoreTaskContext, %function\r
183 vPortRestoreTaskContext:\r
184         /* Switch to system mode. */\r
185         CPS             #SYS_MODE\r
186         portRESTORE_CONTEXT\r
187 \r
188 .align 4\r
189 .type FreeRTOS_IRQ_Handler, %function\r
190 FreeRTOS_IRQ_Handler:\r
191         /* Return to the interrupted instruction. */\r
192         SUB             lr, lr, #4\r
193 \r
194         /* Push the return address and SPSR. */\r
195         PUSH    {lr}\r
196         MRS             lr, SPSR\r
197         PUSH    {lr}\r
198 \r
199         /* Change to supervisor mode to allow reentry. */\r
200         CPS             #SVC_MODE\r
201 \r
202         /* Push used registers. */\r
203         PUSH    {r0-r4, r12}\r
204 \r
205         /* Increment nesting count.  r3 holds the address of ulPortInterruptNesting\r
206         for future use.  r1 holds the original ulPortInterruptNesting value for\r
207         future use. */\r
208         LDR             r3, ulPortInterruptNestingConst\r
209         LDR             r1, [r3]\r
210         ADD             r4, r1, #1\r
211         STR             r4, [r3]\r
212 \r
213         /* Read value from the interrupt acknowledge register, which is stored in r0\r
214         for future parameter and interrupt clearing use. */\r
215         LDR     r2, ulICCIARConst\r
216         LDR             r2, [r2]\r
217         LDR             r0, [r2]\r
218 \r
219         /* Ensure bit 2 of the stack pointer is clear.  r2 holds the bit 2 value for\r
220         future use.  _RB_ Does this ever actually need to be done provided the start\r
221         of the stack is 8-byte aligned? */\r
222         MOV             r2, sp\r
223         AND             r2, r2, #4\r
224         SUB             sp, sp, r2\r
225 \r
226         /* Call the interrupt handler.  r4 pushed to maintain alignment. */\r
227         PUSH    {r0-r4, lr}\r
228         LDR             r1, vApplicationIRQHandlerConst\r
229         BLX             r1\r
230         POP             {r0-r4, lr}\r
231         ADD             sp, sp, r2\r
232 \r
233         CPSID   i\r
234         DSB\r
235         ISB\r
236 \r
237         /* Write the value read from ICCIAR to ICCEOIR. */\r
238         LDR     r4, ulICCEOIRConst\r
239         LDR             r4, [r4]\r
240         STR             r0, [r4]\r
241 \r
242         /* Restore the old nesting count. */\r
243         STR             r1, [r3]\r
244 \r
245         /* A context switch is never performed if the nesting count is not 0. */\r
246         CMP             r1, #0\r
247         BNE             exit_without_switch\r
248 \r
249         /* Did the interrupt request a context switch?  r1 holds the address of\r
250         ulPortYieldRequired and r0 the value of ulPortYieldRequired for future\r
251         use. */\r
252         LDR             r1, =ulPortYieldRequired\r
253         LDR             r0, [r1]\r
254         CMP             r0, #0\r
255         BNE             switch_before_exit\r
256 \r
257 exit_without_switch:\r
258         /* No context switch.  Restore used registers, LR_irq and SPSR before\r
259         returning. */\r
260         POP             {r0-r4, r12}\r
261         CPS             #IRQ_MODE\r
262         POP             {LR}\r
263         MSR             SPSR_cxsf, LR\r
264         POP             {LR}\r
265         MOVS    PC, LR\r
266 \r
267 switch_before_exit:\r
268         /* A context swtich is to be performed.  Clear the context switch pending\r
269         flag. */\r
270         MOV             r0, #0\r
271         STR             r0, [r1]\r
272 \r
273         /* Restore used registers, LR-irq and SPSR before saving the context\r
274         to the task stack. */\r
275         POP             {r0-r4, r12}\r
276         CPS             #IRQ_MODE\r
277         POP             {LR}\r
278         MSR             SPSR_cxsf, LR\r
279         POP             {LR}\r
280         portSAVE_CONTEXT\r
281 \r
282         /* Call the function that selects the new task to execute.\r
283         vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD\r
284         instructions, or 8 byte aligned stack allocated data.  LR does not need\r
285         saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */\r
286         LDR             R0, vTaskSwitchContextConst\r
287         BLX             R0\r
288 \r
289         /* Restore the context of, and branch to, the task selected to execute\r
290         next. */\r
291         portRESTORE_CONTEXT\r
292 \r
293 \r
294 /******************************************************************************\r
295  * If the application provides an implementation of vApplicationIRQHandler(),\r
296  * then it will get called directly without saving the FPU registers on\r
297  * interrupt entry, and this weak implementation of\r
298  * vApplicationIRQHandler() will not get called.\r
299  *\r
300  * If the application provides its own implementation of\r
301  * vApplicationFPUSafeIRQHandler() then this implementation of\r
302  * vApplicationIRQHandler() will be called, save the FPU registers, and then\r
303  * call vApplicationFPUSafeIRQHandler().\r
304  *\r
305  * Therefore, if the application writer wants FPU registers to be saved on\r
306  * interrupt entry their IRQ handler must be called\r
307  * vApplicationFPUSafeIRQHandler(), and if the application writer does not want\r
308  * FPU registers to be saved on interrupt entry their IRQ handler must be\r
309  * called vApplicationIRQHandler().\r
310  *****************************************************************************/\r
311 \r
312 .align 4\r
313 .weak vApplicationIRQHandler\r
314 .type vApplicationIRQHandler, %function\r
315 vApplicationIRQHandler:\r
316         PUSH    {LR}\r
317         FMRX    R1,  FPSCR\r
318         VPUSH   {D0-D15}\r
319         VPUSH   {D16-D31}\r
320         PUSH    {R1}\r
321 \r
322         LDR             r1, vApplicationFPUSafeIRQHandlerConst\r
323         BLX             r1\r
324 \r
325         POP             {R0}\r
326         VPOP    {D16-D31}\r
327         VPOP    {D0-D15}\r
328         VMSR    FPSCR, R0\r
329 \r
330         POP {PC}\r
331 \r
332 \r
333 ulICCIARConst:  .word ulICCIAR\r
334 ulICCEOIRConst: .word ulICCEOIR\r
335 ulICCPMRConst: .word ulICCPMR\r
336 pxCurrentTCBConst: .word pxCurrentTCB\r
337 ulCriticalNestingConst: .word ulCriticalNesting\r
338 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext\r
339 ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask\r
340 vTaskSwitchContextConst: .word vTaskSwitchContext\r
341 vApplicationIRQHandlerConst: .word vApplicationIRQHandler\r
342 ulPortInterruptNestingConst: .word ulPortInterruptNesting\r
343 vApplicationFPUSafeIRQHandlerConst: .word vApplicationFPUSafeIRQHandler\r
344 \r
345 .end\r
346 \r
347 \r
348 \r
349 \r
350 \r