]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CA9/portASM.S
Tidy up GCC Cortex-A port layer - still a work in progress.
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CA9 / portASM.S
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 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 \r
57         .set SYS_MODE,  0x1f\r
58         .set SVC_MODE,  0x13\r
59         .set IRQ_MODE,  0x12\r
60 \r
61         /* Hardware registers. */\r
62         .extern ulICCIAR\r
63         .extern ulICCEOIR\r
64         .extern ulICCPMR\r
65 \r
66         /* Variables and functions. */\r
67         .extern ulMaxAPIPriorityMask\r
68         .extern _freertos_vector_table\r
69         .extern pxCurrentTCB\r
70         .extern vTaskSwitchContext\r
71         .extern vApplicationIRQHandler\r
72         .extern ulPortInterruptNesting\r
73 \r
74         .global FreeRTOS_IRQ_Handler\r
75         .global FreeRTOS_SWI_Handler\r
76         .global vPortRestoreTaskContext\r
77         .global vPortInstallFreeRTOSVectorTable\r
78 \r
79 \r
80 \r
81 \r
82 .macro portSAVE_CONTEXT\r
83 \r
84         /* Save the LR and SPSR onto the system mode stack before switching to\r
85         system mode to save the remaining system mode registers. */\r
86         SRSDB   sp!, #SYS_MODE\r
87         CPS             #SYS_MODE\r
88         PUSH    {R0-R12, R14}\r
89 \r
90         /* Push the critical nesting count. */\r
91         LDR             R2, ulCriticalNestingConst\r
92         LDR             R1, [R2]\r
93         PUSH    {R1}\r
94 \r
95         /* Does the task have a floating point context that needs saving?  If\r
96         ulPortTaskHasFPUContext is 0 then no. */\r
97         LDR             R2, ulPortTaskHasFPUContextConst\r
98         LDR             R3, [R2]\r
99         CMP             R3, #0\r
100 \r
101         /* Save the floating point context, if any. */\r
102         FMRXNE  R1,  FPSCR\r
103         VPUSHNE {D0-D15}\r
104         VPUSHNE {D16-D31}\r
105         PUSHNE  {R1}\r
106 \r
107         /* Save ulPortTaskHasFPUContext itself. */\r
108         PUSH    {R3}\r
109 \r
110         /* Save the stack pointer in the TCB. */\r
111         LDR             R0, pxCurrentTCBConst\r
112         LDR             R1, [R0]\r
113         STR             SP, [R1]\r
114 \r
115         .endm\r
116 \r
117 ; /**********************************************************************/\r
118 \r
119 .macro portRESTORE_CONTEXT\r
120 \r
121         /* Switch to system mode. */\r
122         CPS             #SYS_MODE\r
123 \r
124         /* Set the SP to point to the stack of the task being restored. */\r
125         LDR             R0, pxCurrentTCBConst\r
126         LDR             R1, [R0]\r
127         LDR             SP, [R1]\r
128 \r
129         /* Is there a floating point context to restore?  If the restored\r
130         ulPortTaskHasFPUContext is zero then no. */\r
131         LDR             R0, ulPortTaskHasFPUContextConst\r
132         POP             {R1}\r
133         STR             R1, [R0]\r
134         CMP             R1, #0\r
135 \r
136         /* Restore the floating point context, if any. */\r
137         POPNE   {R0}\r
138         VPOPNE  {D16-D31}\r
139         VPOPNE  {D0-D15}\r
140         VMSRNE  FPSCR, R0\r
141 \r
142         /* Restore the critical section nesting depth. */\r
143         LDR             R0, ulCriticalNestingConst\r
144         POP             {R1}\r
145         STR             R1, [R0]\r
146 \r
147         /* Ensure the priority mask is correct for the critical nesting depth. */\r
148         LDR             R2, ulICCPMRConst\r
149         LDR             R2, [R2]\r
150         CMP             R1, #0\r
151         MOVEQ   R4, #255\r
152         LDRNE   R4, ulMaxAPIPriorityMaskConst\r
153         LDRNE   R4, [R4]\r
154         STR             R4, [R2]\r
155 \r
156         /* Restore all system mode registers other than the SP (which is already\r
157         being used). */\r
158         POP             {R0-R12, R14}\r
159 \r
160         /* Return to the task code, loading CPSR on the way. */\r
161         RFEIA   sp!\r
162 \r
163         .endm\r
164 \r
165 \r
166 \r
167 \r
168 /******************************************************************************\r
169  * SVC handler is used to start the scheduler and yield a task.\r
170  *****************************************************************************/\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 \r
177 vPortRestoreTaskContext:\r
178         portRESTORE_CONTEXT\r
179 \r
180 FreeRTOS_IRQ_Handler:\r
181         /* Return to the interrupted instruction. */\r
182         SUB             lr, lr, #4\r
183 \r
184         /* Push the return address and SPSR. */\r
185         PUSH    {lr}\r
186         MRS             lr, SPSR\r
187         PUSH    {lr}\r
188 \r
189         /* Change to supervisor mode to allow reentry. */\r
190         CPS             #SVC_MODE\r
191 \r
192         /* Push used registers. */\r
193         PUSH    {r0-r4, r12}\r
194 \r
195         /* Increment nesting count.  r3 holds the address of ulPortInterruptNesting\r
196         for future use.  r1 holds the original ulPortInterruptNesting value for\r
197         future use. */\r
198         LDR             r3, ulPortInterruptNestingConst\r
199         LDR             r1, [r3]\r
200         ADD             r4, r1, #1\r
201         STR             r4, [r3]\r
202 \r
203         /* Read value from the interrupt acknowledge register, which is stored in r0\r
204         for future parameter and interrupt clearing use. */\r
205         LDR     r2, ulICCIARConst\r
206         LDR             r2, [r2]\r
207         LDR             r0, [r2]\r
208 \r
209         /* Ensure bit 2 of the stack pointer is clear.  r2 holds the bit 2 value for\r
210         future use. */\r
211         MOV             r2, sp\r
212         AND             r2, r2, #4\r
213         SUB             sp, sp, r2\r
214 \r
215         /* Call the interrupt handler. */\r
216         PUSH    {r0-r3, lr}\r
217         LDR             r1, vApplicationIRQHandlerConst\r
218         BLX             r1\r
219         POP             {r0-r3, lr}\r
220         ADD             sp, sp, r2\r
221 \r
222         CPSID   i\r
223         DSB\r
224         ISB\r
225 \r
226         /* Write the value read from ICCIAR to ICCEOIR. */\r
227         LDR     r4, ulICCEOIRConst\r
228         LDR             r4, [r4]\r
229         STR             r0, [r4]\r
230 \r
231         /* Restore the old nesting count. */\r
232         STR             r1, [r3]\r
233 \r
234         /* A context switch is never performed if the nesting count is not 0. */\r
235         CMP             r1, #0\r
236         BNE             exit_without_switch\r
237 \r
238         /* Did the interrupt request a context switch?  r1 holds the address of\r
239         ulPortYieldRequired and r0 the value of ulPortYieldRequired for future\r
240         use. */\r
241         LDR             r1, =ulPortYieldRequired\r
242         LDR             r0, [r1]\r
243         CMP             r0, #0\r
244         BNE             switch_before_exit\r
245 \r
246 exit_without_switch:\r
247         /* No context switch.  Restore used registers, LR_irq and SPSR before\r
248         returning. */\r
249         POP             {r0-r4, r12}\r
250         CPS             #IRQ_MODE\r
251         POP             {LR}\r
252         MSR             SPSR_cxsf, LR\r
253         POP             {LR}\r
254         MOVS    PC, LR\r
255 \r
256 switch_before_exit:\r
257         /* A context swtich is to be performed.  Clear the context switch pending\r
258         flag. */\r
259         MOV             r0, #0\r
260         STR             r0, [r1]\r
261 \r
262         /* Restore used registers, LR-irq and SPSR before saving the context\r
263         to the task stack. */\r
264         POP             {r0-r4, r12}\r
265         CPS             #IRQ_MODE\r
266         POP             {LR}\r
267         MSR             SPSR_cxsf, LR\r
268         POP             {LR}\r
269         portSAVE_CONTEXT\r
270 \r
271         /* Call the function that selects the new task to execute.\r
272         vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD\r
273         instructions, or 8 byte aligned stack allocated data.  LR does not need\r
274         saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */\r
275         LDR             R0, vTaskSwitchContextConst\r
276         BLX             R0\r
277 \r
278         /* Restore the context of, and branch to, the task selected to execute\r
279         next. */\r
280         portRESTORE_CONTEXT\r
281 \r
282 vPortInstallFreeRTOSVectorTable:\r
283         /* Set VBAR to the vector table that contains the FreeRTOS handlers. */\r
284         ldr     r0, =_freertos_vector_table\r
285         mcr     p15, 0, r0, c12, c0, 0\r
286         dsb\r
287         isb\r
288         bx lr\r
289 \r
290 ulICCIARConst:  .word ulICCIAR\r
291 ulICCEOIRConst: .word ulICCEOIR\r
292 ulICCPMRConst: .word ulICCPMR\r
293 pxCurrentTCBConst: .word pxCurrentTCB\r
294 ulCriticalNestingConst: .word ulCriticalNesting\r
295 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext\r
296 ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask\r
297 vTaskSwitchContextConst: .word vTaskSwitchContext\r
298 vApplicationIRQHandlerConst: .word vApplicationIRQHandler\r
299 ulPortInterruptNestingConst: .word ulPortInterruptNesting\r
300 \r
301 .end\r
302 \r
303 \r
304 \r
305 \r
306 \r