2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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
29 /* Standard includes. */
33 /* Scheduler includes. */
37 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS
38 #error "configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined. See www.FreeRTOS.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html"
41 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET
42 #error "configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined. See www.FreeRTOS.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html"
45 #ifndef configUNIQUE_INTERRUPT_PRIORITIES
46 #error "configUNIQUE_INTERRUPT_PRIORITIES must be defined. See www.FreeRTOS.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html"
49 #ifndef configSETUP_TICK_INTERRUPT
50 #error "configSETUP_TICK_INTERRUPT() must be defined. See www.FreeRTOS.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html"
51 #endif /* configSETUP_TICK_INTERRUPT */
53 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY
54 #error "configMAX_API_CALL_INTERRUPT_PRIORITY must be defined. See www.FreeRTOS.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html"
57 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0
58 #error "configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0"
61 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES
62 #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"
65 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
66 /* Check the configuration. */
67 #if ( configMAX_PRIORITIES > 32 )
68 #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."
70 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
72 /* In case security extensions are implemented. */
73 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
74 #error "configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )"
77 /* Some vendor specific files default configCLEAR_TICK_INTERRUPT() in
79 #ifndef configCLEAR_TICK_INTERRUPT
80 #define configCLEAR_TICK_INTERRUPT()
83 /* A critical section is exited when the critical section nesting count reaches
85 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
87 /* In all GICs 255 can be written to the priority mask register to unmask all
88 * (but the lowest) interrupt priority. */
89 #define portUNMASK_VALUE ( 0xFFUL )
91 /* Tasks are not created with a floating point context, but can be given a
92 * floating point context after they have been created. A variable is stored as
93 * part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
94 * does not have an FPU context, or any other value if the task does have an FPU
96 #define portNO_FLOATING_POINT_CONTEXT ( ( StackType_t ) 0 )
98 /* Constants required to setup the initial task context. */
99 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, IRQ enabled FIQ enabled. */
100 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
101 #define portINTERRUPT_ENABLE_BIT ( 0x80UL )
102 #define portTHUMB_MODE_ADDRESS ( 0x01UL )
104 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary
106 #define portBINARY_POINT_BITS ( ( uint8_t ) 0x03 )
108 /* Masks all bits in the APSR other than the mode bits. */
109 #define portAPSR_MODE_BITS_MASK ( 0x1F )
111 /* The value of the mode bits in the APSR when the CPU is executing in user
113 #define portAPSR_USER_MODE ( 0x10 )
115 /* The critical section macros only mask interrupts up to an application
116 * determined priority level. Sometimes it is necessary to turn interrupt off in
117 * the CPU itself before modifying certain hardware registers. */
118 #define portCPU_IRQ_DISABLE() \
119 __asm volatile ( "CPSID i" ::: "memory" ); \
120 __asm volatile ( "DSB" ); \
121 __asm volatile ( "ISB" );
123 #define portCPU_IRQ_ENABLE() \
124 __asm volatile ( "CPSIE i" ::: "memory" ); \
125 __asm volatile ( "DSB" ); \
126 __asm volatile ( "ISB" );
129 /* Macro to unmask all interrupt priorities. */
130 #define portCLEAR_INTERRUPT_MASK() \
132 portCPU_IRQ_DISABLE(); \
133 portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE; \
134 __asm volatile ( "DSB \n" \
136 portCPU_IRQ_ENABLE(); \
139 #define portINTERRUPT_PRIORITY_REGISTER_OFFSET 0x400UL
140 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
141 #define portBIT_0_SET ( ( uint8_t ) 0x01 )
143 /* Let the user override the pre-loading of the initial LR with the address of
144 * prvTaskExitError() in case it messes up unwinding of the stack in the
146 #ifdef configTASK_RETURN_ADDRESS
147 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
149 #define portTASK_RETURN_ADDRESS prvTaskExitError
152 /* The space on the stack required to hold the FPU registers. This is 32 64-bit
153 * registers, plus a 32-bit status register. */
154 #define portFPU_REGISTER_WORDS ( ( 32 * 2 ) + 1 )
156 /*-----------------------------------------------------------*/
159 * Starts the first task executing. This function is necessarily written in
160 * assembly code so is implemented in portASM.s.
162 extern void vPortRestoreTaskContext( void );
165 * Used to catch tasks that attempt to return from their implementing function.
167 static void prvTaskExitError( void );
170 * If the application provides an implementation of vApplicationIRQHandler(),
171 * then it will get called directly without saving the FPU registers on
172 * interrupt entry, and this weak implementation of
173 * vApplicationFPUSafeIRQHandler() is just provided to remove linkage errors -
174 * it should never actually get called so its implementation contains a
175 * call to configASSERT() that will always fail.
177 * If the application provides its own implementation of
178 * vApplicationFPUSafeIRQHandler() then the implementation of
179 * vApplicationIRQHandler() provided in portASM.S will save the FPU registers
182 * Therefore, if the application writer wants FPU registers to be saved on
183 * interrupt entry their IRQ handler must be called
184 * vApplicationFPUSafeIRQHandler(), and if the application writer does not want
185 * FPU registers to be saved on interrupt entry their IRQ handler must be
186 * called vApplicationIRQHandler().
188 void vApplicationFPUSafeIRQHandler( uint32_t ulICCIAR ) __attribute__( ( weak ) );
190 /*-----------------------------------------------------------*/
192 /* A variable is used to keep track of the critical section nesting. This
193 * variable has to be stored as part of the task context and must be initialised to
194 * a non zero value to ensure interrupts don't inadvertently become unmasked before
195 * the scheduler starts. As it is stored as part of the task context it will
196 * automatically be set to 0 when the first task is started. */
197 volatile uint32_t ulCriticalNesting = 9999UL;
199 /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero then
200 * a floating point context must be saved and restored for the task. */
201 volatile uint32_t ulPortTaskHasFPUContext = pdFALSE;
203 /* Set to 1 to pend a context switch from an ISR. */
204 volatile uint32_t ulPortYieldRequired = pdFALSE;
206 /* Counts the interrupt nesting depth. A context switch is only performed if
207 * if the nesting depth is 0. */
208 volatile uint32_t ulPortInterruptNesting = 0UL;
210 /* Used in the asm file. */
211 __attribute__( ( used ) ) const uint32_t ulICCIARAddress = portICCIAR_INTERRUPT_ACKNOWLEDGE_REGISTER_ADDRESS;
212 __attribute__( ( used ) ) const uint32_t ulICCEOIRAddress = portICCEOIR_END_OF_INTERRUPT_REGISTER_ADDRESS;
213 __attribute__( ( used ) ) const uint32_t ulICCPMRAddress = portICCPMR_PRIORITY_MASK_REGISTER_ADDRESS;
214 __attribute__( ( used ) ) const uint32_t ulMaxAPIPriorityMask = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
216 /*-----------------------------------------------------------*/
219 * See header file for description.
221 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
222 TaskFunction_t pxCode,
223 void * pvParameters )
225 /* Setup the initial stack of the task. The stack is set exactly as
226 * expected by the portRESTORE_CONTEXT() macro.
228 * The fist real value on the stack is the status register, which is set for
229 * system mode, with interrupts enabled. A few NULLs are added first to ensure
230 * GDB does not try decoding a non-existent return address. */
231 *pxTopOfStack = ( StackType_t ) NULL;
233 *pxTopOfStack = ( StackType_t ) NULL;
235 *pxTopOfStack = ( StackType_t ) NULL;
237 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
239 if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
241 /* The task will start in THUMB mode. */
242 *pxTopOfStack |= portTHUMB_MODE_BIT;
247 /* Next the return address, which in this case is the start of the task. */
248 *pxTopOfStack = ( StackType_t ) pxCode;
251 /* Next all the registers other than the stack pointer. */
252 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* R14 */
254 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
256 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
258 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
260 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
262 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
264 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
266 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
268 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
270 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
272 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
274 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
276 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
278 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
281 /* The task will start with a critical nesting count of 0 as interrupts are
283 *pxTopOfStack = portNO_CRITICAL_NESTING;
285 #if ( configUSE_TASK_FPU_SUPPORT == 1 )
287 /* The task will start without a floating point context. A task that
288 * uses the floating point hardware must call vPortTaskUsesFPU() before
289 * executing any floating point instructions. */
291 *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
293 #elif ( configUSE_TASK_FPU_SUPPORT == 2 )
295 /* The task will start with a floating point context. Leave enough
296 * space for the registers - and ensure they are initialised to 0. */
297 pxTopOfStack -= portFPU_REGISTER_WORDS;
298 memset( pxTopOfStack, 0x00, portFPU_REGISTER_WORDS * sizeof( StackType_t ) );
301 *pxTopOfStack = pdTRUE;
302 ulPortTaskHasFPUContext = pdTRUE;
304 #else /* if ( configUSE_TASK_FPU_SUPPORT == 1 ) */
306 #error "Invalid configUSE_TASK_FPU_SUPPORT setting - configUSE_TASK_FPU_SUPPORT must be set to 1, 2, or left undefined."
308 #endif /* if ( configUSE_TASK_FPU_SUPPORT == 1 ) */
312 /*-----------------------------------------------------------*/
314 static void prvTaskExitError( void )
316 /* A function that implements a task must not exit or attempt to return to
317 * its caller as there is nothing to return to. If a task wants to exit it
318 * should instead call vTaskDelete( NULL ).
320 * Artificially force an assert() to be triggered if configASSERT() is
321 * defined, then stop here so application writers can catch the error. */
322 configASSERT( ulPortInterruptNesting == ~0UL );
323 portDISABLE_INTERRUPTS();
329 /*-----------------------------------------------------------*/
331 BaseType_t xPortStartScheduler( void )
335 #if ( configASSERT_DEFINED == 1 )
337 volatile uint8_t ucOriginalPriority;
338 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + portINTERRUPT_PRIORITY_REGISTER_OFFSET );
339 volatile uint8_t ucMaxPriorityValue;
341 /* Determine how many priority bits are implemented in the GIC.
343 * Save the interrupt priority value that is about to be clobbered. */
344 ucOriginalPriority = *pucFirstUserPriorityRegister;
346 /* Determine the number of priority bits available. First write to
347 * all possible bits. */
348 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
350 /* Read the value back to see how many bits stuck. */
351 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
353 /* Shift to the least significant bits. */
354 while( ( ucMaxPriorityValue & portBIT_0_SET ) != portBIT_0_SET )
356 ucMaxPriorityValue >>= ( uint8_t ) 0x01;
359 /* Sanity check configUNIQUE_INTERRUPT_PRIORITIES matches the read
361 configASSERT( ucMaxPriorityValue == portLOWEST_INTERRUPT_PRIORITY );
363 /* Restore the clobbered interrupt priority register to its original
365 *pucFirstUserPriorityRegister = ucOriginalPriority;
367 #endif /* configASSERT_DEFINED */
370 /* Only continue if the CPU is not in User mode. The CPU must be in a
371 * Privileged mode for the scheduler to start. */
372 __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR )::"memory" );
373 ulAPSR &= portAPSR_MODE_BITS_MASK;
374 configASSERT( ulAPSR != portAPSR_USER_MODE );
376 if( ulAPSR != portAPSR_USER_MODE )
378 /* Only continue if the binary point value is set to its lowest possible
379 * setting. See the comments in vPortValidateInterruptPriority() below for
380 * more information. */
381 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
383 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )
385 /* Interrupts are turned off in the CPU itself to ensure tick does
386 * not execute while the scheduler is being started. Interrupts are
387 * automatically turned back on in the CPU when the first task starts
389 portCPU_IRQ_DISABLE();
391 /* Start the timer that generates the tick ISR. */
392 configSETUP_TICK_INTERRUPT();
394 /* Start the first task executing. */
395 vPortRestoreTaskContext();
399 /* Will only get here if vTaskStartScheduler() was called with the CPU in
400 * a non-privileged mode or the binary point register was not set to its lowest
401 * possible value. prvTaskExitError() is referenced to prevent a compiler
402 * warning about it being defined but not referenced in the case that the user
403 * defines their own exit address. */
404 ( void ) prvTaskExitError;
407 /*-----------------------------------------------------------*/
409 void vPortEndScheduler( void )
411 /* Not implemented in ports where there is nothing to return to.
412 * Artificially force an assert. */
413 configASSERT( ulCriticalNesting == 1000UL );
415 /*-----------------------------------------------------------*/
417 void vPortEnterCritical( void )
419 /* Mask interrupts up to the max syscall interrupt priority. */
420 ulPortSetInterruptMask();
422 /* Now that interrupts are disabled, ulCriticalNesting can be accessed
423 * directly. Increment ulCriticalNesting to keep a count of how many times
424 * portENTER_CRITICAL() has been called. */
427 /* This is not the interrupt safe version of the enter critical function so
428 * assert() if it is being called from an interrupt context. Only API
429 * functions that end in "FromISR" can be used in an interrupt. Only assert if
430 * the critical nesting count is 1 to protect against recursive calls if the
431 * assert function also uses a critical section. */
432 if( ulCriticalNesting == 1 )
434 configASSERT( ulPortInterruptNesting == 0 );
437 /*-----------------------------------------------------------*/
439 void vPortExitCritical( void )
441 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
443 /* Decrement the nesting count as the critical section is being
447 /* If the nesting level has reached zero then all interrupt
448 * priorities must be re-enabled. */
449 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
451 /* Critical nesting has reached zero so all interrupt priorities
452 * should be unmasked. */
453 portCLEAR_INTERRUPT_MASK();
457 /*-----------------------------------------------------------*/
459 void FreeRTOS_Tick_Handler( void )
461 /* Set interrupt mask before altering scheduler structures. The tick
462 * handler runs at the lowest priority, so interrupts cannot already be masked,
463 * so there is no need to save and restore the current mask value. It is
464 * necessary to turn off interrupts in the CPU itself while the ICCPMR is being
466 portCPU_IRQ_DISABLE();
467 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
468 __asm volatile ( "dsb \n"
469 "isb \n" ::: "memory" );
470 portCPU_IRQ_ENABLE();
472 /* Increment the RTOS tick. */
473 if( xTaskIncrementTick() != pdFALSE )
475 ulPortYieldRequired = pdTRUE;
478 /* Ensure all interrupt priorities are active again. */
479 portCLEAR_INTERRUPT_MASK();
480 configCLEAR_TICK_INTERRUPT();
482 /*-----------------------------------------------------------*/
484 #if ( configUSE_TASK_FPU_SUPPORT != 2 )
486 void vPortTaskUsesFPU( void )
488 uint32_t ulInitialFPSCR = 0;
490 /* A task is registering the fact that it needs an FPU context. Set the
491 * FPU flag (which is saved as part of the task context). */
492 ulPortTaskHasFPUContext = pdTRUE;
494 /* Initialise the floating point status register. */
495 __asm volatile ( "FMXR FPSCR, %0" ::"r" ( ulInitialFPSCR ) : "memory" );
498 #endif /* configUSE_TASK_FPU_SUPPORT */
499 /*-----------------------------------------------------------*/
501 void vPortClearInterruptMask( uint32_t ulNewMaskValue )
503 if( ulNewMaskValue == pdFALSE )
505 portCLEAR_INTERRUPT_MASK();
508 /*-----------------------------------------------------------*/
510 uint32_t ulPortSetInterruptMask( void )
514 /* Interrupt in the CPU must be turned off while the ICCPMR is being
516 portCPU_IRQ_DISABLE();
518 if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )
520 /* Interrupts were already masked. */
526 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
527 __asm volatile ( "dsb \n"
528 "isb \n" ::: "memory" );
531 portCPU_IRQ_ENABLE();
535 /*-----------------------------------------------------------*/
537 #if ( configASSERT_DEFINED == 1 )
539 void vPortValidateInterruptPriority( void )
541 /* The following assertion will fail if a service routine (ISR) for
542 * an interrupt that has been assigned a priority above
543 * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
544 * function. ISR safe FreeRTOS API functions must *only* be called
545 * from interrupts that have been assigned a priority at or below
546 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
548 * Numerically low interrupt priority numbers represent logically high
549 * interrupt priorities, therefore the priority of the interrupt must
550 * be set to a value equal to or numerically *higher* than
551 * configMAX_SYSCALL_INTERRUPT_PRIORITY.
553 * FreeRTOS maintains separate thread and ISR API functions to ensure
554 * interrupt entry is as fast and simple as possible. */
555 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );
557 /* Priority grouping: The interrupt controller (GIC) allows the bits
558 * that define each interrupt's priority to be split between bits that
559 * define the interrupt's pre-emption priority bits and bits that define
560 * the interrupt's sub-priority. For simplicity all bits must be defined
561 * to be pre-emption priority bits. The following assertion will fail if
562 * this is not the case (if some bits represent a sub-priority).
564 * The priority grouping is configured by the GIC's binary point register
565 * (ICCBPR). Writing 0 to ICCBPR will ensure it is set to its lowest
566 * possible value (which may be above 0). */
567 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
570 #endif /* configASSERT_DEFINED */
571 /*-----------------------------------------------------------*/
573 void vApplicationFPUSafeIRQHandler( uint32_t ulICCIAR )
576 configASSERT( ( volatile void * ) NULL );