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
29 /*-----------------------------------------------------------
30 * Components that can be compiled to either ARM or THUMB mode are
31 * contained in port.c The ISR routines, which can only be compiled
32 * to ARM mode, are contained in this file.
33 *----------------------------------------------------------*/
35 /* Scheduler includes. */
39 /* Constants required to handle interrupts. */
40 #define portTIMER_MATCH_ISR_BIT ( ( uint8_t ) 0x01 )
41 #define portCLEAR_VIC_INTERRUPT ( ( uint32_t ) 0 )
43 /* Constants required to handle critical sections. */
44 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
45 volatile uint32_t ulCriticalNesting = 9999UL;
47 /*-----------------------------------------------------------*/
49 /* ISR to handle manual context switches (from a call to taskYIELD()). */
50 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));
53 * The scheduler can only be started from ARM mode, hence the inclusion of this
56 void vPortISRStartFirstTask( void );
57 /*-----------------------------------------------------------*/
59 void vPortISRStartFirstTask( void )
61 /* Simply start the scheduler. This is included here as it can only be
62 called from ARM mode. */
63 portRESTORE_CONTEXT();
65 /*-----------------------------------------------------------*/
68 * Called by portYIELD() or taskYIELD() to manually force a context switch.
70 * When a context switch is performed from the task level the saved task
71 * context is made to look as if it occurred from within the tick ISR. This
72 * way the same restore context function can be used when restoring the context
73 * saved from the ISR or that saved from a call to vPortYieldProcessor.
75 void vPortYieldProcessor( void )
77 /* Within an IRQ ISR the link register has an offset from the true return
78 address, but an SWI ISR does not. Add the offset manually so the same
79 ISR return code can be used in both cases. */
80 __asm volatile ( "ADD LR, LR, #4" );
82 /* Perform the context switch. First save the context of the current task. */
85 /* Find the highest priority task that is ready to run. */
86 __asm volatile( "bl vTaskSwitchContext" );
88 /* Restore the context of the new task. */
89 portRESTORE_CONTEXT();
91 /*-----------------------------------------------------------*/
94 * The ISR used for the scheduler tick depends on whether the cooperative or
95 * the preemptive scheduler is being used.
99 #if configUSE_PREEMPTION == 0
101 /* The cooperative scheduler requires a normal IRQ service routine to
102 simply increment the system tick. */
103 void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));
104 void vNonPreemptiveTick( void )
106 xTaskIncrementTick();
108 VICVectAddr = portCLEAR_VIC_INTERRUPT;
113 /* The preemptive scheduler is defined as "naked" as the full context is
114 saved on entry as part of the context switch. */
115 void vPreemptiveTick( void ) __attribute__((naked));
116 void vPreemptiveTick( void )
118 /* Save the context of the interrupted task. */
121 /* Increment the RTOS tick count, then look for the highest priority
122 task that is ready to run. */
125 " bl xTaskIncrementTick \t\n" \
127 " beq SkipContextSwitch \t\n" \
128 " bl vTaskSwitchContext \t\n" \
129 "SkipContextSwitch: \t\n"
132 /* Ready for the next interrupt. */
134 VICVectAddr = portCLEAR_VIC_INTERRUPT;
136 /* Restore the context of the new task. */
137 portRESTORE_CONTEXT();
141 /*-----------------------------------------------------------*/
144 * The interrupt management utilities can only be called from ARM mode. When
145 * THUMB_INTERWORK is defined the utilities are defined as functions here to
146 * ensure a switch to ARM mode. When THUMB_INTERWORK is not defined then
147 * the utilities are defined as macros in portmacro.h - as per other ports.
149 #ifdef THUMB_INTERWORK
151 void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
152 void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));
154 void vPortDisableInterruptsFromThumb( void )
157 "STMDB SP!, {R0} \n\t" /* Push R0. */
158 "MRS R0, CPSR \n\t" /* Get CPSR. */
159 "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
160 "MSR CPSR, R0 \n\t" /* Write back modified value. */
161 "LDMIA SP!, {R0} \n\t" /* Pop R0. */
162 "BX R14" ); /* Return back to thumb. */
165 void vPortEnableInterruptsFromThumb( void )
168 "STMDB SP!, {R0} \n\t" /* Push R0. */
169 "MRS R0, CPSR \n\t" /* Get CPSR. */
170 "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
171 "MSR CPSR, R0 \n\t" /* Write back modified value. */
172 "LDMIA SP!, {R0} \n\t" /* Pop R0. */
173 "BX R14" ); /* Return back to thumb. */
176 #endif /* THUMB_INTERWORK */
178 /* The code generated by the GCC compiler uses the stack in different ways at
179 different optimisation levels. The interrupt flags can therefore not always
180 be saved to the stack. Instead the critical section nesting level is stored
181 in a variable, which is then saved as part of the stack context. */
182 void vPortEnterCritical( void )
184 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
186 "STMDB SP!, {R0} \n\t" /* Push R0. */
187 "MRS R0, CPSR \n\t" /* Get CPSR. */
188 "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
189 "MSR CPSR, R0 \n\t" /* Write back modified value. */
190 "LDMIA SP!, {R0}" ); /* Pop R0. */
192 /* Now interrupts are disabled ulCriticalNesting can be accessed
193 directly. Increment ulCriticalNesting to keep a count of how many times
194 portENTER_CRITICAL() has been called. */
198 void vPortExitCritical( void )
200 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
202 /* Decrement the nesting count as we are leaving a critical section. */
205 /* If the nesting level has reached zero then interrupts should be
207 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
209 /* Enable interrupts as per portEXIT_CRITICAL(). */
211 "STMDB SP!, {R0} \n\t" /* Push R0. */
212 "MRS R0, CPSR \n\t" /* Get CPSR. */
213 "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
214 "MSR CPSR, R0 \n\t" /* Write back modified value. */
215 "LDMIA SP!, {R0}" ); /* Pop R0. */