2 * FreeRTOS Kernel V10.2.0
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
35 /* Hardware registers. */
40 /* Variables and functions. */
41 .extern ulMaxAPIPriorityMask
42 .extern _freertos_vector_table
44 .extern vTaskSwitchContext
45 .extern vApplicationIRQHandler
46 .extern ulPortInterruptNesting
47 .extern ulPortTaskHasFPUContext
49 .global FreeRTOS_IRQ_Handler
50 .global FreeRTOS_SWI_Handler
51 .global vPortRestoreTaskContext
53 .macro portSAVE_CONTEXT
55 /* Save the LR and SPSR onto the system mode stack before switching to
56 system mode to save the remaining system mode registers. */
61 /* Push the critical nesting count. */
62 LDR R2, ulCriticalNestingConst
66 /* Does the task have a floating point context that needs saving? If
67 ulPortTaskHasFPUContext is 0 then no. */
68 LDR R2, ulPortTaskHasFPUContextConst
72 /* Save the floating point context, if any. */
78 /* Save ulPortTaskHasFPUContext itself. */
81 /* Save the stack pointer in the TCB. */
82 LDR R0, pxCurrentTCBConst
88 ; /**********************************************************************/
90 .macro portRESTORE_CONTEXT
92 /* Set the SP to point to the stack of the task being restored. */
93 LDR R0, pxCurrentTCBConst
97 /* Is there a floating point context to restore? If the restored
98 ulPortTaskHasFPUContext is zero then no. */
99 LDR R0, ulPortTaskHasFPUContextConst
104 /* Restore the floating point context, if any. */
110 /* Restore the critical section nesting depth. */
111 LDR R0, ulCriticalNestingConst
115 /* Ensure the priority mask is correct for the critical nesting depth. */
116 LDR R2, ulICCPMRConst
120 LDRNE R4, ulMaxAPIPriorityMaskConst
124 /* Restore all system mode registers other than the SP (which is already
128 /* Return to the task code, loading CPSR on the way. */
136 /******************************************************************************
137 * SVC handler is used to start the scheduler.
138 *****************************************************************************/
140 .type FreeRTOS_SWI_Handler, %function
141 FreeRTOS_SWI_Handler:
142 /* Save the context of the current task and select a new task to run. */
144 LDR R0, vTaskSwitchContextConst
149 /******************************************************************************
150 * vPortRestoreTaskContext is used to start the scheduler.
151 *****************************************************************************/
152 .type vPortRestoreTaskContext, %function
153 vPortRestoreTaskContext:
154 /* Switch to system mode. */
159 .type FreeRTOS_IRQ_Handler, %function
160 FreeRTOS_IRQ_Handler:
162 /* Return to the interrupted instruction. */
165 /* Push the return address and SPSR. */
170 /* Change to supervisor mode to allow reentry. */
173 /* Push used registers. */
176 /* Increment nesting count. r3 holds the address of ulPortInterruptNesting
177 for future use. r1 holds the original ulPortInterruptNesting value for
179 LDR r3, ulPortInterruptNestingConst
184 /* Read value from the interrupt acknowledge register, which is stored in r0
185 for future parameter and interrupt clearing use. */
186 LDR r2, ulICCIARConst
190 /* Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
191 future use. _RB_ Is this ever needed provided the start of the stack is
192 alligned on an 8-byte boundary? */
197 /* Call the interrupt handler. */
199 LDR r1, vApplicationIRQHandlerConst
208 /* Write the value read from ICCIAR to ICCEOIR. */
209 LDR r4, ulICCEOIRConst
213 /* Restore the old nesting count. */
216 /* A context switch is never performed if the nesting count is not 0. */
218 BNE exit_without_switch
220 /* Did the interrupt request a context switch? r1 holds the address of
221 ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
223 LDR r1, =ulPortYieldRequired
226 BNE switch_before_exit
229 /* No context switch. Restore used registers, LR_irq and SPSR before
239 /* A context swtich is to be performed. Clear the context switch pending
244 /* Restore used registers, LR-irq and SPSR before saving the context
245 to the task stack. */
253 /* Call the function that selects the new task to execute.
254 vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
255 instructions, or 8 byte aligned stack allocated data. LR does not need
256 saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */
257 LDR R0, vTaskSwitchContextConst
260 /* Restore the context of, and branch to, the task selected to execute
264 ulICCIARConst: .word ulICCIAR
265 ulICCEOIRConst: .word ulICCEOIR
266 ulICCPMRConst: .word ulICCPMR
267 pxCurrentTCBConst: .word pxCurrentTCB
268 ulCriticalNestingConst: .word ulCriticalNesting
269 ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext
270 ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask
271 vTaskSwitchContextConst: .word vTaskSwitchContext
272 vApplicationIRQHandlerConst: .word vApplicationIRQHandler
273 ulPortInterruptNestingConst: .word ulPortInterruptNesting