2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 #include <intrinsics.h>
\r
32 /* Scheduler includes. */
\r
33 #include "FreeRTOS.h"
\r
36 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
\r
37 /* Check the configuration. */
\r
38 #if( configMAX_PRIORITIES > 32 )
\r
39 #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
\r
41 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
\r
43 #ifndef configSETUP_TICK_INTERRUPT
\r
44 #error configSETUP_TICK_INTERRUPT() must be defined in FreeRTOSConfig.h to call the function that sets up the tick interrupt. A default that uses the PIT is provided in the official demo application.
\r
47 #ifndef configCLEAR_TICK_INTERRUPT
\r
48 #error configCLEAR_TICK_INTERRUPT must be defined in FreeRTOSConfig.h to clear which ever interrupt was used to generate the tick interrupt. A default that uses the PIT is provided in the official demo application.
\r
51 /* A critical section is exited when the critical section nesting count reaches
\r
53 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
55 /* Tasks are not created with a floating point context, but can be given a
\r
56 floating point context after they have been created. A variable is stored as
\r
57 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
\r
58 does not have an FPU context, or any other value if the task does have an FPU
\r
60 #define portNO_FLOATING_POINT_CONTEXT ( ( StackType_t ) 0 )
\r
62 /* Constants required to setup the initial task context. */
\r
63 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
64 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
\r
65 #define portTHUMB_MODE_ADDRESS ( 0x01UL )
\r
67 /* Masks all bits in the APSR other than the mode bits. */
\r
68 #define portAPSR_MODE_BITS_MASK ( 0x1F )
\r
70 /* The value of the mode bits in the APSR when the CPU is executing in user
\r
72 #define portAPSR_USER_MODE ( 0x10 )
\r
74 /*-----------------------------------------------------------*/
\r
77 * Starts the first task executing. This function is necessarily written in
\r
78 * assembly code so is implemented in portASM.s.
\r
80 extern void vPortRestoreTaskContext( void );
\r
83 * Used to catch tasks that attempt to return from their implementing function.
\r
85 static void prvTaskExitError( void );
\r
87 /*-----------------------------------------------------------*/
\r
89 /* A variable is used to keep track of the critical section nesting. This
\r
90 variable has to be stored as part of the task context and must be initialised to
\r
91 a non zero value to ensure interrupts don't inadvertently become unmasked before
\r
92 the scheduler starts. As it is stored as part of the task context it will
\r
93 automatically be set to 0 when the first task is started. */
\r
94 volatile uint32_t ulCriticalNesting = 9999UL;
\r
96 /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero
\r
97 then a floating point context must be saved and restored for the task. */
\r
98 uint32_t ulPortTaskHasFPUContext = pdFALSE;
\r
100 /* Set to 1 to pend a context switch from an ISR. */
\r
101 uint32_t ulPortYieldRequired = pdFALSE;
\r
103 /* Counts the interrupt nesting depth. A context switch is only performed if
\r
104 if the nesting depth is 0. */
\r
105 uint32_t ulPortInterruptNesting = 0UL;
\r
108 /*-----------------------------------------------------------*/
\r
111 * See header file for description.
\r
113 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
115 /* Setup the initial stack of the task. The stack is set exactly as
\r
116 expected by the portRESTORE_CONTEXT() macro.
\r
118 The fist real value on the stack is the status register, which is set for
\r
119 system mode, with interrupts enabled. A few NULLs are added first to ensure
\r
120 GDB does not try decoding a non-existent return address. */
\r
121 *pxTopOfStack = NULL;
\r
123 *pxTopOfStack = NULL;
\r
125 *pxTopOfStack = NULL;
\r
127 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
\r
129 if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
\r
131 /* The task will start in THUMB mode. */
\r
132 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
137 /* Next the return address, which in this case is the start of the task. */
\r
138 *pxTopOfStack = ( StackType_t ) pxCode;
\r
141 /* Next all the registers other than the stack pointer. */
\r
142 *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* R14 */
\r
144 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
\r
146 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
\r
148 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
\r
150 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
\r
152 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
\r
154 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
\r
156 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
\r
158 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
\r
160 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
\r
162 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
\r
164 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
\r
166 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
\r
168 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
171 /* The task will start with a critical nesting count of 0 as interrupts are
\r
173 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
176 /* The task will start without a floating point context. A task that uses
\r
177 the floating point hardware must call vPortTaskUsesFPU() before executing
\r
178 any floating point instructions. */
\r
179 *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
\r
181 return pxTopOfStack;
\r
183 /*-----------------------------------------------------------*/
\r
185 static void prvTaskExitError( void )
\r
187 /* A function that implements a task must not exit or attempt to return to
\r
188 its caller as there is nothing to return to. If a task wants to exit it
\r
189 should instead call vTaskDelete( NULL ).
\r
191 Artificially force an assert() to be triggered if configASSERT() is
\r
192 defined, then stop here so application writers can catch the error. */
\r
193 configASSERT( ulPortInterruptNesting == ~0UL );
\r
194 portDISABLE_INTERRUPTS();
\r
197 /*-----------------------------------------------------------*/
\r
199 BaseType_t xPortStartScheduler( void )
\r
203 /* Only continue if the CPU is not in User mode. The CPU must be in a
\r
204 Privileged mode for the scheduler to start. */
\r
205 __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );
\r
206 ulAPSR &= portAPSR_MODE_BITS_MASK;
\r
207 configASSERT( ulAPSR != portAPSR_USER_MODE );
\r
209 if( ulAPSR != portAPSR_USER_MODE )
\r
211 /* Start the timer that generates the tick ISR. */
\r
212 configSETUP_TICK_INTERRUPT();
\r
213 vPortRestoreTaskContext();
\r
216 /* Will only get here if vTaskStartScheduler() was called with the CPU in
\r
217 a non-privileged mode or the binary point register was not set to its lowest
\r
221 /*-----------------------------------------------------------*/
\r
223 void vPortEndScheduler( void )
\r
225 /* Not implemented in ports where there is nothing to return to.
\r
226 Artificially force an assert. */
\r
227 configASSERT( ulCriticalNesting == 1000UL );
\r
229 /*-----------------------------------------------------------*/
\r
231 void vPortEnterCritical( void )
\r
233 portDISABLE_INTERRUPTS();
\r
235 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
236 directly. Increment ulCriticalNesting to keep a count of how many times
\r
237 portENTER_CRITICAL() has been called. */
\r
238 ulCriticalNesting++;
\r
240 /* This is not the interrupt safe version of the enter critical function so
\r
241 assert() if it is being called from an interrupt context. Only API
\r
242 functions that end in "FromISR" can be used in an interrupt. Only assert if
\r
243 the critical nesting count is 1 to protect against recursive calls if the
\r
244 assert function also uses a critical section. */
\r
245 if( ulCriticalNesting == 1 )
\r
247 configASSERT( ulPortInterruptNesting == 0 );
\r
250 /*-----------------------------------------------------------*/
\r
252 void vPortExitCritical( void )
\r
254 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
256 /* Decrement the nesting count as the critical section is being
\r
258 ulCriticalNesting--;
\r
260 /* If the nesting level has reached zero then all interrupt
\r
261 priorities must be re-enabled. */
\r
262 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
264 /* Critical nesting has reached zero so all interrupt priorities
\r
265 should be unmasked. */
\r
266 portENABLE_INTERRUPTS();
\r
270 /*-----------------------------------------------------------*/
\r
272 void FreeRTOS_Tick_Handler( void )
\r
274 portDISABLE_INTERRUPTS();
\r
276 /* Increment the RTOS tick. */
\r
277 if( xTaskIncrementTick() != pdFALSE )
\r
279 ulPortYieldRequired = pdTRUE;
\r
282 portENABLE_INTERRUPTS();
\r
283 configCLEAR_TICK_INTERRUPT();
\r
285 /*-----------------------------------------------------------*/
\r
287 void vPortTaskUsesFPU( void )
\r
289 uint32_t ulInitialFPSCR = 0;
\r
291 /* A task is registering the fact that it needs an FPU context. Set the
\r
292 FPU flag (which is saved as part of the task context). */
\r
293 ulPortTaskHasFPUContext = pdTRUE;
\r
295 /* Initialise the floating point status register. */
\r
296 __asm( "FMXR FPSCR, %0" :: "r" (ulInitialFPSCR) );
\r
298 /*-----------------------------------------------------------*/
\r