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
29 /* Standard includes. */
\r
33 #include <intrinsics.h>
\r
35 /* Scheduler includes. */
\r
36 #include "FreeRTOS.h"
\r
39 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS
\r
40 #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
43 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET
\r
44 #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
47 #ifndef configUNIQUE_INTERRUPT_PRIORITIES
\r
48 #error configUNIQUE_INTERRUPT_PRIORITIES must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
51 #ifndef configSETUP_TICK_INTERRUPT
\r
52 #error configSETUP_TICK_INTERRUPT() must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
53 #endif /* configSETUP_TICK_INTERRUPT */
\r
55 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY
\r
56 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
59 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0
\r
60 #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0
\r
63 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES
\r
64 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be less than or equal to configUNIQUE_INTERRUPT_PRIORITIES as the lower the numeric priority value the higher the logical interrupt priority
\r
67 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
\r
68 /* Check the configuration. */
\r
69 #if( configMAX_PRIORITIES > 32 )
\r
70 #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
72 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
\r
74 /* In case security extensions are implemented. */
\r
75 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
\r
76 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
\r
79 #ifndef configCLEAR_TICK_INTERRUPT
\r
80 #define configCLEAR_TICK_INTERRUPT()
\r
83 /* A critical section is exited when the critical section nesting count reaches
\r
85 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
87 /* In all GICs 255 can be written to the priority mask register to unmask all
\r
88 (but the lowest) interrupt priority. */
\r
89 #define portUNMASK_VALUE ( 0xFFUL )
\r
91 /* Tasks are not created with a floating point context, but can be given a
\r
92 floating point context after they have been created. A variable is stored as
\r
93 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
\r
94 does not have an FPU context, or any other value if the task does have an FPU
\r
96 #define portNO_FLOATING_POINT_CONTEXT ( ( StackType_t ) 0 )
\r
98 /* Constants required to setup the initial task context. */
\r
99 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
100 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
\r
101 #define portTHUMB_MODE_ADDRESS ( 0x01UL )
\r
103 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary
\r
105 #define portBINARY_POINT_BITS ( ( uint8_t ) 0x03 )
\r
107 /* Masks all bits in the APSR other than the mode bits. */
\r
108 #define portAPSR_MODE_BITS_MASK ( 0x1F )
\r
110 /* The value of the mode bits in the APSR when the CPU is executing in user
\r
112 #define portAPSR_USER_MODE ( 0x10 )
\r
114 /* Macro to unmask all interrupt priorities. */
\r
115 #define portCLEAR_INTERRUPT_MASK() \
\r
118 portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE; \
\r
124 /*-----------------------------------------------------------*/
\r
127 * Starts the first task executing. This function is necessarily written in
\r
128 * assembly code so is implemented in portASM.s.
\r
130 extern void vPortRestoreTaskContext( void );
\r
133 * Used to catch tasks that attempt to return from their implementing function.
\r
135 static void prvTaskExitError( void );
\r
137 /*-----------------------------------------------------------*/
\r
139 /* A variable is used to keep track of the critical section nesting. This
\r
140 variable has to be stored as part of the task context and must be initialised to
\r
141 a non zero value to ensure interrupts don't inadvertently become unmasked before
\r
142 the scheduler starts. As it is stored as part of the task context it will
\r
143 automatically be set to 0 when the first task is started. */
\r
144 volatile uint32_t ulCriticalNesting = 9999UL;
\r
146 /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero
\r
147 then a floating point context must be saved and restored for the task. */
\r
148 uint32_t ulPortTaskHasFPUContext = pdFALSE;
\r
150 /* Set to 1 to pend a context switch from an ISR. */
\r
151 uint32_t ulPortYieldRequired = pdFALSE;
\r
153 /* Counts the interrupt nesting depth. A context switch is only performed if
\r
154 if the nesting depth is 0. */
\r
155 uint32_t ulPortInterruptNesting = 0UL;
\r
158 /*-----------------------------------------------------------*/
\r
161 * See header file for description.
\r
163 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
165 /* Setup the initial stack of the task. The stack is set exactly as
\r
166 expected by the portRESTORE_CONTEXT() macro.
\r
168 The fist real value on the stack is the status register, which is set for
\r
169 system mode, with interrupts enabled. A few NULLs are added first to ensure
\r
170 GDB does not try decoding a non-existent return address. */
\r
171 *pxTopOfStack = NULL;
\r
173 *pxTopOfStack = NULL;
\r
175 *pxTopOfStack = NULL;
\r
177 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
\r
179 if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
\r
181 /* The task will start in THUMB mode. */
\r
182 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
187 /* Next the return address, which in this case is the start of the task. */
\r
188 *pxTopOfStack = ( StackType_t ) pxCode;
\r
191 /* Next all the registers other than the stack pointer. */
\r
192 *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* R14 */
\r
194 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
\r
196 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
\r
198 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
\r
200 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
\r
202 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
\r
204 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
\r
206 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
\r
208 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
\r
210 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
\r
212 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
\r
214 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
\r
216 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
\r
218 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
221 /* The task will start with a critical nesting count of 0 as interrupts are
\r
223 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
226 /* The task will start without a floating point context. A task that uses
\r
227 the floating point hardware must call vPortTaskUsesFPU() before executing
\r
228 any floating point instructions. */
\r
229 *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
\r
231 return pxTopOfStack;
\r
233 /*-----------------------------------------------------------*/
\r
235 static void prvTaskExitError( void )
\r
237 /* A function that implements a task must not exit or attempt to return to
\r
238 its caller as there is nothing to return to. If a task wants to exit it
\r
239 should instead call vTaskDelete( NULL ).
\r
241 Artificially force an assert() to be triggered if configASSERT() is
\r
242 defined, then stop here so application writers can catch the error. */
\r
243 configASSERT( ulPortInterruptNesting == ~0UL );
\r
244 portDISABLE_INTERRUPTS();
\r
247 /*-----------------------------------------------------------*/
\r
249 BaseType_t xPortStartScheduler( void )
\r
253 /* Only continue if the CPU is not in User mode. The CPU must be in a
\r
254 Privileged mode for the scheduler to start. */
\r
255 __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );
\r
256 ulAPSR &= portAPSR_MODE_BITS_MASK;
\r
257 configASSERT( ulAPSR != portAPSR_USER_MODE );
\r
259 if( ulAPSR != portAPSR_USER_MODE )
\r
261 /* Only continue if the binary point value is set to its lowest possible
\r
262 setting. See the comments in vPortValidateInterruptPriority() below for
\r
263 more information. */
\r
264 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
\r
266 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )
\r
268 /* Start the timer that generates the tick ISR. */
\r
269 configSETUP_TICK_INTERRUPT();
\r
272 vPortRestoreTaskContext();
\r
276 /* Will only get here if vTaskStartScheduler() was called with the CPU in
\r
277 a non-privileged mode or the binary point register was not set to its lowest
\r
281 /*-----------------------------------------------------------*/
\r
283 void vPortEndScheduler( void )
\r
285 /* Not implemented in ports where there is nothing to return to.
\r
286 Artificially force an assert. */
\r
287 configASSERT( ulCriticalNesting == 1000UL );
\r
289 /*-----------------------------------------------------------*/
\r
291 void vPortEnterCritical( void )
\r
293 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
294 ulPortSetInterruptMask();
\r
296 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
297 directly. Increment ulCriticalNesting to keep a count of how many times
\r
298 portENTER_CRITICAL() has been called. */
\r
299 ulCriticalNesting++;
\r
301 /* This is not the interrupt safe version of the enter critical function so
\r
302 assert() if it is being called from an interrupt context. Only API
\r
303 functions that end in "FromISR" can be used in an interrupt. Only assert if
\r
304 the critical nesting count is 1 to protect against recursive calls if the
\r
305 assert function also uses a critical section. */
\r
306 if( ulCriticalNesting == 1 )
\r
308 configASSERT( ulPortInterruptNesting == 0 );
\r
311 /*-----------------------------------------------------------*/
\r
313 void vPortExitCritical( void )
\r
315 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
317 /* Decrement the nesting count as the critical section is being
\r
319 ulCriticalNesting--;
\r
321 /* If the nesting level has reached zero then all interrupt
\r
322 priorities must be re-enabled. */
\r
323 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
325 /* Critical nesting has reached zero so all interrupt priorities
\r
326 should be unmasked. */
\r
327 portCLEAR_INTERRUPT_MASK();
\r
331 /*-----------------------------------------------------------*/
\r
333 void FreeRTOS_Tick_Handler( void )
\r
335 /* Set interrupt mask before altering scheduler structures. The tick
\r
336 handler runs at the lowest priority, so interrupts cannot already be masked,
\r
337 so there is no need to save and restore the current mask value. */
\r
339 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
\r
344 /* Increment the RTOS tick. */
\r
345 if( xTaskIncrementTick() != pdFALSE )
\r
347 ulPortYieldRequired = pdTRUE;
\r
350 /* Ensure all interrupt priorities are active again. */
\r
351 portCLEAR_INTERRUPT_MASK();
\r
352 configCLEAR_TICK_INTERRUPT();
\r
354 /*-----------------------------------------------------------*/
\r
356 void vPortTaskUsesFPU( void )
\r
358 uint32_t ulInitialFPSCR = 0;
\r
360 /* A task is registering the fact that it needs an FPU context. Set the
\r
361 FPU flag (which is saved as part of the task context). */
\r
362 ulPortTaskHasFPUContext = pdTRUE;
\r
364 /* Initialise the floating point status register. */
\r
365 __asm( "FMXR FPSCR, %0" :: "r" (ulInitialFPSCR) );
\r
367 /*-----------------------------------------------------------*/
\r
369 void vPortClearInterruptMask( uint32_t ulNewMaskValue )
\r
371 if( ulNewMaskValue == pdFALSE )
\r
373 portCLEAR_INTERRUPT_MASK();
\r
376 /*-----------------------------------------------------------*/
\r
378 uint32_t ulPortSetInterruptMask( void )
\r
383 if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )
\r
385 /* Interrupts were already masked. */
\r
390 ulReturn = pdFALSE;
\r
391 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
\r
399 /*-----------------------------------------------------------*/
\r
401 #if( configASSERT_DEFINED == 1 )
\r
403 void vPortValidateInterruptPriority( void )
\r
405 /* The following assertion will fail if a service routine (ISR) for
\r
406 an interrupt that has been assigned a priority above
\r
407 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
\r
408 function. ISR safe FreeRTOS API functions must *only* be called
\r
409 from interrupts that have been assigned a priority at or below
\r
410 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
412 Numerically low interrupt priority numbers represent logically high
\r
413 interrupt priorities, therefore the priority of the interrupt must
\r
414 be set to a value equal to or numerically *higher* than
\r
415 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
417 FreeRTOS maintains separate thread and ISR API functions to ensure
\r
418 interrupt entry is as fast and simple as possible.
\r
420 The following links provide detailed information:
\r
421 http://www.freertos.org/RTOS-Cortex-M3-M4.html
\r
422 http://www.freertos.org/FAQHelp.html */
\r
423 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );
\r
425 /* Priority grouping: The interrupt controller (GIC) allows the bits
\r
426 that define each interrupt's priority to be split between bits that
\r
427 define the interrupt's pre-emption priority bits and bits that define
\r
428 the interrupt's sub-priority. For simplicity all bits must be defined
\r
429 to be pre-emption priority bits. The following assertion will fail if
\r
430 this is not the case (if some bits represent a sub-priority).
\r
432 The priority grouping is configured by the GIC's binary point register
\r
433 (ICCBPR). Writting 0 to ICCBPR will ensure it is set to its lowest
\r
434 possible value (which may be above 0). */
\r
435 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
\r
438 #endif /* configASSERT_DEFINED */
\r