2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
30 /*-----------------------------------------------------------
\r
31 * Components that can be compiled to either ARM or THUMB mode are
\r
32 * contained in port.c The ISR routines, which can only be compiled
\r
33 * to ARM mode, are contained in this file.
\r
34 *----------------------------------------------------------*/
\r
39 + The assembler statements are now included in a single asm block rather
\r
40 than each line having its own asm block.
\r
44 /* Scheduler includes. */
\r
45 #include "FreeRTOS.h"
\r
48 /* Constants required to handle interrupts. */
\r
49 #define portCLEAR_AIC_INTERRUPT ( ( uint32_t ) 0 )
\r
51 /* Constants required to handle critical sections. */
\r
52 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
53 volatile uint32_t ulCriticalNesting = 9999UL;
\r
55 /*-----------------------------------------------------------*/
\r
57 /* ISR to handle manual context switches (from a call to taskYIELD()). */
\r
58 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));
\r
61 * The scheduler can only be started from ARM mode, hence the inclusion of this
\r
64 void vPortISRStartFirstTask( void );
\r
65 /*-----------------------------------------------------------*/
\r
67 void vPortISRStartFirstTask( void )
\r
69 /* Simply start the scheduler. This is included here as it can only be
\r
70 called from ARM mode. */
\r
71 portRESTORE_CONTEXT();
\r
73 /*-----------------------------------------------------------*/
\r
76 * Called by portYIELD() or taskYIELD() to manually force a context switch.
\r
78 * When a context switch is performed from the task level the saved task
\r
79 * context is made to look as if it occurred from within the tick ISR. This
\r
80 * way the same restore context function can be used when restoring the context
\r
81 * saved from the ISR or that saved from a call to vPortYieldProcessor.
\r
83 void vPortYieldProcessor( void )
\r
85 /* Within an IRQ ISR the link register has an offset from the true return
\r
86 address, but an SWI ISR does not. Add the offset manually so the same
\r
87 ISR return code can be used in both cases. */
\r
88 asm volatile ( "ADD LR, LR, #4" );
\r
90 /* Perform the context switch. First save the context of the current task. */
\r
93 /* Find the highest priority task that is ready to run. */
\r
94 vTaskSwitchContext();
\r
96 /* Restore the context of the new task. */
\r
97 portRESTORE_CONTEXT();
\r
99 /*-----------------------------------------------------------*/
\r
102 * The ISR used for the scheduler tick depends on whether the cooperative or
\r
103 * the preemptive scheduler is being used.
\r
106 #if configUSE_PREEMPTION == 0
\r
108 /* The cooperative scheduler requires a normal IRQ service routine to
\r
109 simply increment the system tick. */
\r
110 void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));
\r
111 void vNonPreemptiveTick( void )
\r
113 static volatile uint32_t ulDummy;
\r
115 /* Clear tick timer interrupt indication. */
\r
116 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
\r
118 xTaskIncrementTick();
\r
120 /* Acknowledge the interrupt at AIC level... */
\r
121 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;
\r
124 #else /* else preemption is turned on */
\r
126 /* The preemptive scheduler is defined as "naked" as the full context is
\r
127 saved on entry as part of the context switch. */
\r
128 void vPreemptiveTick( void ) __attribute__((naked));
\r
129 void vPreemptiveTick( void )
\r
131 /* Save the context of the interrupted task. */
\r
132 portSAVE_CONTEXT();
\r
134 /* WARNING - Do not use local (stack) variables here. Use globals
\r
136 static volatile uint32_t ulDummy;
\r
138 /* Clear tick timer interrupt indication. */
\r
139 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
\r
141 /* Increment the RTOS tick count, then look for the highest priority
\r
142 task that is ready to run. */
\r
143 if( xTaskIncrementTick() != pdFALSE )
\r
145 vTaskSwitchContext();
\r
148 /* Acknowledge the interrupt at AIC level... */
\r
149 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;
\r
151 /* Restore the context of the new task. */
\r
152 portRESTORE_CONTEXT();
\r
156 /*-----------------------------------------------------------*/
\r
159 * The interrupt management utilities can only be called from ARM mode. When
\r
160 * THUMB_INTERWORK is defined the utilities are defined as functions here to
\r
161 * ensure a switch to ARM mode. When THUMB_INTERWORK is not defined then
\r
162 * the utilities are defined as macros in portmacro.h - as per other ports.
\r
164 #ifdef THUMB_INTERWORK
\r
166 void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
\r
167 void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));
\r
169 void vPortDisableInterruptsFromThumb( void )
\r
172 "STMDB SP!, {R0} \n\t" /* Push R0. */
\r
173 "MRS R0, CPSR \n\t" /* Get CPSR. */
\r
174 "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
\r
175 "MSR CPSR, R0 \n\t" /* Write back modified value. */
\r
176 "LDMIA SP!, {R0} \n\t" /* Pop R0. */
\r
177 "BX R14" ); /* Return back to thumb. */
\r
180 void vPortEnableInterruptsFromThumb( void )
\r
183 "STMDB SP!, {R0} \n\t" /* Push R0. */
\r
184 "MRS R0, CPSR \n\t" /* Get CPSR. */
\r
185 "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
\r
186 "MSR CPSR, R0 \n\t" /* Write back modified value. */
\r
187 "LDMIA SP!, {R0} \n\t" /* Pop R0. */
\r
188 "BX R14" ); /* Return back to thumb. */
\r
191 #endif /* THUMB_INTERWORK */
\r
193 /* The code generated by the GCC compiler uses the stack in different ways at
\r
194 different optimisation levels. The interrupt flags can therefore not always
\r
195 be saved to the stack. Instead the critical section nesting level is stored
\r
196 in a variable, which is then saved as part of the stack context. */
\r
197 void vPortEnterCritical( void )
\r
199 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
201 "STMDB SP!, {R0} \n\t" /* Push R0. */
\r
202 "MRS R0, CPSR \n\t" /* Get CPSR. */
\r
203 "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
\r
204 "MSR CPSR, R0 \n\t" /* Write back modified value. */
\r
205 "LDMIA SP!, {R0}" ); /* Pop R0. */
\r
207 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
208 directly. Increment ulCriticalNesting to keep a count of how many times
\r
209 portENTER_CRITICAL() has been called. */
\r
210 ulCriticalNesting++;
\r
213 void vPortExitCritical( void )
\r
215 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
217 /* Decrement the nesting count as we are leaving a critical section. */
\r
218 ulCriticalNesting--;
\r
220 /* If the nesting level has reached zero then interrupts should be
\r
222 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
224 /* Enable interrupts as per portEXIT_CRITICAL(). */
\r
226 "STMDB SP!, {R0} \n\t" /* Push R0. */
\r
227 "MRS R0, CPSR \n\t" /* Get CPSR. */
\r
228 "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
\r
229 "MSR CPSR, R0 \n\t" /* Write back modified value. */
\r
230 "LDMIA SP!, {R0}" ); /* Pop R0. */
\r