1/* 2 * FreeRTOS Kernel V10.6.2 3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 * 5 * SPDX-License-Identifier: MIT 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included in all 15 * copies or substantial portions of the Software. 16 * 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. 23 * 24 * https://www.FreeRTOS.org 25 * https://github.com/FreeRTOS 26 * 27 */ 28 29#include "PriorityDefinitions.h" 30 31 PUBLIC _prvStartFirstTask 32 PUBLIC ___interrupt_27 33 34 EXTERN _pxCurrentTCB 35 EXTERN _vTaskSwitchContext 36 37 RSEG CODE:CODE(4) 38 39_prvStartFirstTask: 40 41 /* When starting the scheduler there is nothing that needs moving to the 42 interrupt stack because the function is not called from an interrupt. 43 Just ensure the current stack is the user stack. */ 44 SETPSW U 45 46 /* Obtain the location of the stack associated with which ever task 47 pxCurrentTCB is currently pointing to. */ 48 MOV.L #_pxCurrentTCB, R15 49 MOV.L [R15], R15 50 MOV.L [R15], R0 51 52 /* Restore the registers from the stack of the task pointed to by 53 pxCurrentTCB. */ 54 POP R15 55 56 /* Accumulator low 32 bits. */ 57 MVTACLO R15 58 POP R15 59 60 /* Accumulator high 32 bits. */ 61 MVTACHI R15 62 POP R15 63 64 /* Floating point status word. */ 65 MVTC R15, FPSW 66 67 /* R1 to R15 - R0 is not included as it is the SP. */ 68 POPM R1-R15 69 70 /* This pops the remaining registers. */ 71 RTE 72 NOP 73 NOP 74 75/*-----------------------------------------------------------*/ 76 77/* The software interrupt - overwrite the default 'weak' definition. */ 78___interrupt_27: 79 80 /* Re-enable interrupts. */ 81 SETPSW I 82 83 /* Move the data that was automatically pushed onto the interrupt stack when 84 the interrupt occurred from the interrupt stack to the user stack. 85 86 R15 is saved before it is clobbered. */ 87 PUSH.L R15 88 89 /* Read the user stack pointer. */ 90 MVFC USP, R15 91 92 /* Move the address down to the data being moved. */ 93 SUB #12, R15 94 MVTC R15, USP 95 96 /* Copy the data across, R15, then PC, then PSW. */ 97 MOV.L [ R0 ], [ R15 ] 98 MOV.L 4[ R0 ], 4[ R15 ] 99 MOV.L 8[ R0 ], 8[ R15 ] 100 101 /* Move the interrupt stack pointer to its new correct position. */ 102 ADD #12, R0 103 104 /* All the rest of the registers are saved directly to the user stack. */ 105 SETPSW U 106 107 /* Save the rest of the general registers (R15 has been saved already). */ 108 PUSHM R1-R14 109 110 /* Save the FPSW and accumulator. */ 111 MVFC FPSW, R15 112 PUSH.L R15 113 MVFACHI R15 114 PUSH.L R15 115 116 /* Middle word. */ 117 MVFACMI R15 118 119 /* Shifted left as it is restored to the low order word. */ 120 SHLL #16, R15 121 PUSH.L R15 122 123 /* Save the stack pointer to the TCB. */ 124 MOV.L #_pxCurrentTCB, R15 125 MOV.L [ R15 ], R15 126 MOV.L R0, [ R15 ] 127 128 /* Ensure the interrupt mask is set to the syscall priority while the kernel 129 structures are being accessed. */ 130 MVTIPL #configMAX_SYSCALL_INTERRUPT_PRIORITY 131 132 /* Select the next task to run. */ 133 BSR.A _vTaskSwitchContext 134 135 /* Reset the interrupt mask as no more data structure access is required. */ 136 MVTIPL #configKERNEL_INTERRUPT_PRIORITY 137 138 /* Load the stack pointer of the task that is now selected as the Running 139 state task from its TCB. */ 140 MOV.L #_pxCurrentTCB,R15 141 MOV.L [ R15 ], R15 142 MOV.L [ R15 ], R0 143 144 /* Restore the context of the new task. The PSW (Program Status Word) and 145 PC will be popped by the RTE instruction. */ 146 POP R15 147 MVTACLO R15 148 POP R15 149 MVTACHI R15 150 POP R15 151 MVTC R15, FPSW 152 POPM R1-R15 153 RTE 154 NOP 155 NOP 156 157/*-----------------------------------------------------------*/ 158 159 END 160