2 * FreeRTOS Kernel V11.2.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
36 /* Hardware registers. */
41 /* Variables and functions. */
42 .extern ulMaxAPIPriorityMask
43 .extern _freertos_vector_table
45 .extern vTaskSwitchContext
46 .extern vApplicationIRQHandler
47 .extern ulPortInterruptNesting
49 #if defined( __ARM_FP )
50 .extern ulPortTaskHasFPUContext
53 .global FreeRTOS_IRQ_Handler
54 .global FreeRTOS_SWI_Handler
55 .global vPortRestoreTaskContext
57 .macro portSAVE_CONTEXT
59 /* Save the LR and SPSR onto the system mode stack before switching to
60 system mode to save the remaining system mode registers. */
65 /* Push the critical nesting count. */
66 LDR R2, ulCriticalNestingConst
70 #if defined( __ARM_FP )
71 /* Does the task have a floating point context that needs saving? If
72 ulPortTaskHasFPUContext is 0 then no. */
73 LDR R2, ulPortTaskHasFPUContextConst
77 /* Save the floating point context, if any. */
82 /* Save ulPortTaskHasFPUContext itself. */
86 /* Save the stack pointer in the TCB. */
87 LDR R0, pxCurrentTCBConst
93 ; /**********************************************************************/
95 .macro portRESTORE_CONTEXT
97 /* Set the SP to point to the stack of the task being restored. */
98 LDR R0, pxCurrentTCBConst
102 #if defined( __ARM_FP )
104 * Is there a floating point context to restore? If the restored
105 * ulPortTaskHasFPUContext is zero then no.
107 LDR R0, ulPortTaskHasFPUContextConst
112 /* Restore the floating point context, if any. */
116 #endif /* __ARM_FP */
118 /* Restore the critical section nesting depth. */
119 LDR R0, ulCriticalNestingConst
123 /* Ensure the priority mask is correct for the critical nesting depth. */
124 LDR R2, ulICCPMRConst
128 LDRNE R4, ulMaxAPIPriorityMaskConst
132 /* Restore all system mode registers other than the SP (which is already
136 /* Return to the task code, loading CPSR on the way. */
142 /******************************************************************************
143 * SVC handler is used to start the scheduler.
144 *****************************************************************************/
146 .type FreeRTOS_SWI_Handler, %function
147 FreeRTOS_SWI_Handler:
148 /* Save the context of the current task and select a new task to run. */
151 /* Ensure bit 2 of the stack pointer is clear. */
156 LDR R0, vTaskSwitchContextConst
162 /******************************************************************************
163 * vPortRestoreTaskContext is used to start the scheduler.
164 *****************************************************************************/
165 .type vPortRestoreTaskContext, %function
166 vPortRestoreTaskContext:
167 /* Switch to system mode. */
172 .type FreeRTOS_IRQ_Handler, %function
173 FreeRTOS_IRQ_Handler:
175 /* Return to the interrupted instruction. */
178 /* Push the return address and SPSR. */
183 /* Change to supervisor mode to allow reentry. */
186 /* Push used registers. */
189 /* Increment nesting count. r3 holds the address of ulPortInterruptNesting
190 for future use. r1 holds the original ulPortInterruptNesting value for
192 LDR r3, ulPortInterruptNestingConst
197 /* Read value from the interrupt acknowledge register, which is stored in r0
198 for future parameter and interrupt clearing use. */
199 LDR r2, ulICCIARConst
203 /* Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
204 future use. _RB_ Is this ever needed provided the start of the stack is
205 alligned on an 8-byte boundary? */
210 /* Call the interrupt handler. */
212 LDR r1, vApplicationIRQHandlerConst
221 /* Write the value read from ICCIAR to ICCEOIR. */
222 LDR r4, ulICCEOIRConst
226 /* Restore the old nesting count. */
229 /* A context switch is never performed if the nesting count is not 0. */
231 BNE exit_without_switch
233 /* Did the interrupt request a context switch? r1 holds the address of
234 ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
236 LDR r1, =ulPortYieldRequired
239 BNE switch_before_exit
242 /* No context switch. Restore used registers, LR_irq and SPSR before
252 /* A context switch is to be performed. Clear the context switch pending
257 /* Restore used registers, LR-irq and SPSR before saving the context
258 to the task stack. */
266 /* Ensure bit 2 of the stack pointer is clear. */
271 /* Call the function that selects the new task to execute.
272 vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
273 instructions, or 8 byte aligned stack allocated data. LR does not need
274 saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */
275 LDR R0, vTaskSwitchContextConst
278 /* Restore the context of, and branch to, the task selected to execute
282 /******************************************************************************
283 * If the application provides an implementation of vApplicationIRQHandler(),
284 * then it will get called directly without saving the FPU registers on
285 * interrupt entry, and this weak implementation of
286 * vApplicationIRQHandler() will not get called.
288 * If the application provides its own implementation of
289 * vApplicationFPUSafeIRQHandler() then this implementation of
290 * vApplicationIRQHandler() will be called, save the FPU registers, and then
291 * call vApplicationFPUSafeIRQHandler().
293 * Therefore, if the application writer wants FPU registers to be saved on
294 * interrupt entry their IRQ handler must be called
295 * vApplicationFPUSafeIRQHandler(), and if the application writer does not want
296 * FPU registers to be saved on interrupt entry their IRQ handler must be
297 * called vApplicationIRQHandler().
298 *****************************************************************************/
300 .weak vApplicationIRQHandler
301 .type vApplicationIRQHandler, %function
302 vApplicationIRQHandler:
306 #if defined( __ARM_FP )
311 LDR r1, vApplicationFPUSafeIRQHandlerConst
317 #endif /* __ARM_FP */
321 ulICCIARConst: .word ulICCIAR
322 ulICCEOIRConst: .word ulICCEOIR
323 ulICCPMRConst: .word ulICCPMR
324 pxCurrentTCBConst: .word pxCurrentTCB
325 ulCriticalNestingConst: .word ulCriticalNesting
327 #if defined( __ARM_FP )
328 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext
329 vApplicationFPUSafeIRQHandlerConst: .word vApplicationFPUSafeIRQHandler
330 #endif /* __ARM_FP */
332 ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask
333 vTaskSwitchContextConst: .word vTaskSwitchContext
334 vApplicationIRQHandlerConst: .word vApplicationIRQHandler
335 ulPortInterruptNestingConst: .word ulPortInterruptNesting