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