2 * FreeRTOS Kernel V11.1.0
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
30 /*-----------------------------------------------------------
31 * Components that can be compiled to either ARM or THUMB mode are
32 * contained in port.c The ISR routines, which can only be compiled
33 * to ARM mode, are contained in this file.
34 *----------------------------------------------------------*/
39 + The assembler statements are now included in a single asm block rather
40 + than each line having its own asm block.
44 /* Scheduler includes. */
48 /* Constants required to handle interrupts. */
49 #define portCLEAR_AIC_INTERRUPT ( ( uint32_t ) 0 )
51 /* Constants required to handle critical sections. */
52 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
53 volatile uint32_t ulCriticalNesting = 9999UL;
55 /*-----------------------------------------------------------*/
57 /* ISR to handle manual context switches (from a call to taskYIELD()). */
58 void vPortYieldProcessor( void ) __attribute__( ( interrupt( "SWI" ), naked ) );
61 * The scheduler can only be started from ARM mode, hence the inclusion of this
64 void vPortISRStartFirstTask( void );
65 /*-----------------------------------------------------------*/
67 void vPortISRStartFirstTask( void )
69 /* Simply start the scheduler. This is included here as it can only be
70 * called from ARM mode. */
71 portRESTORE_CONTEXT();
73 /*-----------------------------------------------------------*/
76 * Called by portYIELD() or taskYIELD() to manually force a context switch.
78 * When a context switch is performed from the task level the saved task
79 * context is made to look as if it occurred from within the tick ISR. This
80 * way the same restore context function can be used when restoring the context
81 * saved from the ISR or that saved from a call to vPortYieldProcessor.
83 void vPortYieldProcessor( void )
85 /* Within an IRQ ISR the link register has an offset from the true return
86 * address, but an SWI ISR does not. Add the offset manually so the same
87 * ISR return code can be used in both cases. */
88 asm volatile ( "ADD LR, LR, #4" );
90 /* Perform the context switch. First save the context of the current task. */
93 /* Find the highest priority task that is ready to run. */
96 /* Restore the context of the new task. */
97 portRESTORE_CONTEXT();
99 /*-----------------------------------------------------------*/
102 * The ISR used for the scheduler tick depends on whether the cooperative or
103 * the preemptive scheduler is being used.
106 #if configUSE_PREEMPTION == 0
108 /* The cooperative scheduler requires a normal IRQ service routine to
109 * simply increment the system tick. */
110 void vNonPreemptiveTick( void ) __attribute__( ( interrupt( "IRQ" ) ) );
111 void vNonPreemptiveTick( void )
113 static volatile uint32_t ulDummy;
115 /* Clear tick timer interrupt indication. */
116 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
118 xTaskIncrementTick();
120 /* Acknowledge the interrupt at AIC level... */
121 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;
124 #else /* else preemption is turned on */
126 /* The preemptive scheduler is defined as "naked" as the full context is
127 * saved on entry as part of the context switch. */
128 void vPreemptiveTick( void ) __attribute__( ( naked ) );
129 void vPreemptiveTick( void )
131 /* Save the context of the interrupted task. */
134 /* WARNING - Do not use local (stack) variables here. Use globals
136 static volatile uint32_t ulDummy;
138 /* Clear tick timer interrupt indication. */
139 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
141 /* Increment the RTOS tick count, then look for the highest priority
142 * task that is ready to run. */
143 if( xTaskIncrementTick() != pdFALSE )
145 vTaskSwitchContext();
148 /* Acknowledge the interrupt at AIC level... */
149 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;
151 /* Restore the context of the new task. */
152 portRESTORE_CONTEXT();
155 #endif /* if configUSE_PREEMPTION == 0 */
156 /*-----------------------------------------------------------*/
159 * The interrupt management utilities can only be called from ARM mode. When
160 * THUMB_INTERWORK is defined the utilities are defined as functions here to
161 * ensure a switch to ARM mode. When THUMB_INTERWORK is not defined then
162 * the utilities are defined as macros in portmacro.h - as per other ports.
164 #ifdef THUMB_INTERWORK
166 void vPortDisableInterruptsFromThumb( void ) __attribute__( ( naked ) );
167 void vPortEnableInterruptsFromThumb( void ) __attribute__( ( naked ) );
169 void vPortDisableInterruptsFromThumb( void )
172 "STMDB SP!, {R0} \n\t" /* Push R0. */
173 "MRS R0, CPSR \n\t" /* Get CPSR. */
174 "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
175 "MSR CPSR, R0 \n\t" /* Write back modified value. */
176 "LDMIA SP!, {R0} \n\t" /* Pop R0. */
177 "BX R14" ); /* Return back to thumb. */
180 void vPortEnableInterruptsFromThumb( void )
183 "STMDB SP!, {R0} \n\t" /* Push R0. */
184 "MRS R0, CPSR \n\t" /* Get CPSR. */
185 "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
186 "MSR CPSR, R0 \n\t" /* Write back modified value. */
187 "LDMIA SP!, {R0} \n\t" /* Pop R0. */
188 "BX R14" ); /* Return back to thumb. */
191 #endif /* THUMB_INTERWORK */
193 /* The code generated by the GCC compiler uses the stack in different ways at
194 * different optimisation levels. The interrupt flags can therefore not always
195 * be saved to the stack. Instead the critical section nesting level is stored
196 * in a variable, which is then saved as part of the stack context. */
197 void vPortEnterCritical( void )
199 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
201 "STMDB SP!, {R0} \n\t" /* Push R0. */
202 "MRS R0, CPSR \n\t" /* Get CPSR. */
203 "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
204 "MSR CPSR, R0 \n\t" /* Write back modified value. */
205 "LDMIA SP!, {R0}" ); /* Pop R0. */
207 /* Now that interrupts are disabled, ulCriticalNesting can be accessed
208 * directly. Increment ulCriticalNesting to keep a count of how many times
209 * portENTER_CRITICAL() has been called. */
213 void vPortExitCritical( void )
215 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
217 /* Decrement the nesting count as we are leaving a critical section. */
220 /* If the nesting level has reached zero then interrupts should be
222 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
224 /* Enable interrupts as per portEXIT_CRITICAL(). */
226 "STMDB SP!, {R0} \n\t" /* Push R0. */
227 "MRS R0, CPSR \n\t" /* Get CPSR. */
228 "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
229 "MSR CPSR, R0 \n\t" /* Write back modified value. */
230 "LDMIA SP!, {R0}" ); /* Pop R0. */