2 * FreeRTOS Kernel V10.4.6
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 /* Variables and functions. */
37 .extern ulMaxAPIPriorityMask
38 .extern _freertos_vector_table
40 .extern vTaskSwitchContext
41 .extern vApplicationIRQHandler
42 .extern ulPortInterruptNesting
43 .extern ulPortTaskHasFPUContext
45 .extern ulPortYieldRequired
47 .global FreeRTOS_IRQ_Handler
48 .global FreeRTOS_SVC_Handler
49 .global vPortRestoreTaskContext
52 .macro portSAVE_CONTEXT
54 /* Save the LR and SPSR onto the system mode stack before switching to
55 system mode to save the remaining system mode registers. */
60 /* Push the critical nesting count. */
61 LDR R2, ulCriticalNestingConst
65 /* Does the task have a floating point context that needs saving? If
66 ulPortTaskHasFPUContext is 0 then no. */
67 LDR R2, ulPortTaskHasFPUContextConst
71 /* Save the floating point context, if any. */
74 #if configFPU_D32 == 1
76 #endif /* configFPU_D32 */
79 /* Save ulPortTaskHasFPUContext itself. */
82 /* Save the stack pointer in the TCB. */
83 LDR R0, pxCurrentTCBConst
89 ; /**********************************************************************/
91 .macro portRESTORE_CONTEXT
93 /* Set the SP to point to the stack of the task being restored. */
94 LDR R0, pxCurrentTCBConst
98 /* Is there a floating point context to restore? If the restored
99 ulPortTaskHasFPUContext is zero then no. */
100 LDR R0, ulPortTaskHasFPUContextConst
105 /* Restore the floating point context, if any. */
107 #if configFPU_D32 == 1
109 #endif /* configFPU_D32 */
113 /* Restore the critical section nesting depth. */
114 LDR R0, ulCriticalNestingConst
118 /* Restore all system mode registers other than the SP (which is already
122 /* Return to the task code, loading CPSR on the way. */
130 /******************************************************************************
131 * SVC handler is used to yield.
132 *****************************************************************************/
134 .type FreeRTOS_SVC_Handler, %function
135 FreeRTOS_SVC_Handler:
136 /* Save the context of the current task and select a new task to run. */
138 LDR R0, vTaskSwitchContextConst
143 /******************************************************************************
144 * vPortRestoreTaskContext is used to start the scheduler.
145 *****************************************************************************/
147 .type vPortRestoreTaskContext, %function
148 vPortRestoreTaskContext:
149 /* Switch to system mode. */
154 .type FreeRTOS_IRQ_Handler, %function
155 FreeRTOS_IRQ_Handler:
156 /* Return to the interrupted instruction. */
159 /* Push the return address and SPSR. */
164 /* Change to supervisor mode to allow reentry. */
167 /* Push used registers. */
170 /* Increment nesting count. r3 holds the address of ulPortInterruptNesting
171 for future use. r1 holds the original ulPortInterruptNesting value for
173 LDR r3, ulPortInterruptNestingConst
178 /* Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
184 /* Call the interrupt handler. */
186 LDR r1, vApplicationIRQHandlerConst
195 /* Write to the EOI register. */
196 LDR r0, ulICCEOIRConst
200 /* Restore the old nesting count. */
203 /* A context switch is never performed if the nesting count is not 0. */
205 BNE exit_without_switch
207 /* Did the interrupt request a context switch? r1 holds the address of
208 ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
210 LDR r1, ulPortYieldRequiredConst
213 BNE switch_before_exit
216 /* No context switch. Restore used registers, LR_irq and SPSR before
226 /* A context swtich is to be performed. Clear the context switch pending
231 /* Restore used registers, LR-irq and SPSR before saving the context
232 to the task stack. */
240 /* Call the function that selects the new task to execute.
241 vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
242 instructions, or 8 byte aligned stack allocated data. LR does not need
243 saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */
244 LDR R0, vTaskSwitchContextConst
247 /* Restore the context of, and branch to, the task selected to execute
251 ulICCEOIRConst: .word ulICCEOIR
252 pxCurrentTCBConst: .word pxCurrentTCB
253 ulCriticalNestingConst: .word ulCriticalNesting
254 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext
255 vTaskSwitchContextConst: .word vTaskSwitchContext
256 vApplicationIRQHandlerConst: .word vApplicationIRQHandler
257 ulPortInterruptNestingConst: .word ulPortInterruptNesting
258 ulPortYieldRequiredConst: .word ulPortYieldRequired