2 ; * FreeRTOS Kernel V10.5.1
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
29 #include "porthardware.h"
31 ; Declare all extern symbols here - including any ISRs that are referenced in
38 ; Functions used by scheduler
39 ; ---------------------------
40 EXTERN vTaskSwitchContext
42 EXTERN xTaskIncrementTick
43 EXTERN uxCriticalNesting
45 ; Functions implemented in this file
46 ; ----------------------------------
48 PUBLIC vPortYieldFromTick
49 PUBLIC vPortYieldFromISR
52 ; Interrupt vector table.
53 ; -----------------------
55 ; For simplicity the RTOS tick interrupt routine uses the __task keyword.
56 ; As the IAR compiler does not permit a function to be declared using both
57 ; __task and __interrupt, the use of __task necessitates that the interrupt
58 ; vector table be setup manually.
60 ; To write an ISR, implement the ISR function using the __interrupt keyword
61 ; but do not install the interrupt using the "#pragma vector=ABC" method.
62 ; Instead manually place the name of the ISR in the vector table using an
63 ; ORG and jmp instruction as demonstrated below.
64 ; You will also have to add an EXTERN statement at the top of the file.
68 ORG TICK_INT_vect ; Vector address
73 CLR_INT MACRO FLAG_REG, FLAG_MASK
81 ; Saving and Restoring a Task Context and Task Switching
82 ; ------------------------------------------------------
84 ; The IAR compiler does not fully support inline assembler, so saving and
85 ; restoring a task context has to be written in an asm file.
87 ; vPortYield() and vPortYieldFromTick() are usually written in C. Doing
88 ; so in this case would required calls to be made to portSAVE_CONTEXT() and
89 ; portRESTORE_CONTEXT(). This is dis-advantageous as the context switch
90 ; function would require two extra jump and return instructions over the
93 ; To avoid this I have opted to implement both vPortYield() and
94 ; vPortYieldFromTick() in this assembly file. For convenience
95 ; portSAVE_CONTEXT and portRESTORE_CONTEXT are implemented as macros.
97 portSAVE_CONTEXT MACRO
98 st -y, r0 ; First save the r0 register - we need to use this.
99 in r0, SREG ; Obtain the SREG value so we can disable interrupts...
100 cli ; ... as soon as possible.
101 st -y, r0 ; Store the SREG as it was before we disabled interrupts.
103 in r0, SPL ; Next store the hardware stack pointer. The IAR...
104 st -y, r0 ; ... compiler uses the hardware stack as a call stack ...
105 in r0, SPH ; ... only.
108 st -y, r1 ; Now store the rest of the registers. Dont store the ...
109 st -y, r2 ; ... the Y register here as it is used as the software
110 st -y, r3 ; stack pointer and will get saved into the TCB.
138 lds r0, uxCriticalNesting
139 st -y, r0 ; Store the critical nesting counter.
141 lds r26, pxCurrentTCB ; Finally save the software stack pointer (Y ...
142 lds r27, pxCurrentTCB + 1 ; ... register) into the TCB.
149 portRESTORE_CONTEXT MACRO
150 lds r26, pxCurrentTCB
151 lds r27, pxCurrentTCB + 1 ; Restore the software stack pointer from ...
152 ld r28, x+ ; the TCB into the software stack pointer (...
153 ld r29, x+ ; ... the Y register).
156 sts uxCriticalNesting, r0
158 ld r31, y+ ; Restore the registers down to R0. The Y
159 ld r30, y+ ; register is missing from this list as it
160 ld r27, y+ ; has already been restored.
188 ld r0, y+ ; The next thing on the stack is the ...
189 out SPH, r0 ; ... hardware stack pointer.
193 ld r0, y+ ; Next there is the SREG register.
196 ld r0, y+ ; Finally we have finished with r0, so restore r0.
202 ; vPortYield(), vPortYieldFromTick() and vPortYieldFromISR()
203 ; -------------------------------------
205 ; Manual and preemptive context switch functions respectively.
206 ; The IAR compiler does not fully support inline assembler,
207 ; so these are implemented here rather than the more usually
208 ; place of within port.c.
211 portSAVE_CONTEXT ; Save the context of the current task.
212 call vTaskSwitchContext ; Call the scheduler.
213 portRESTORE_CONTEXT ; Restore the context of whichever task the ...
214 ret ; ... scheduler decided should run.
217 CLR_INT INT_FLAGS, INT_MASK ; Clear tick interrupt flag
219 portSAVE_CONTEXT ; Save the context of the current task.
220 call xTaskIncrementTick ; Call the timer tick function.
223 call vTaskSwitchContext ; Call the scheduler.
226 portRESTORE_CONTEXT ; Restore the context of whichever task the ...
227 reti ; ... scheduler decided should run.
230 portSAVE_CONTEXT ; Save the context of the current task.
231 call vTaskSwitchContext ; Call the scheduler.
232 portRESTORE_CONTEXT ; Restore the context of whichever task the ...
233 reti ; ... scheduler decided should run.
238 ; Again due to the lack of inline assembler, this is required
239 ; to get access to the portRESTORE_CONTEXT macro.
245 ; Just a filler for unused interrupt vectors.