2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* Standard includes. */
\r
69 #include <intrinsics.h>
\r
71 /* Scheduler includes. */
\r
72 #include "FreeRTOS.h"
\r
75 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS
\r
76 #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
79 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET
\r
80 #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
83 #ifndef configUNIQUE_INTERRUPT_PRIORITIES
\r
84 #error configUNIQUE_INTERRUPT_PRIORITIES must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
87 #ifndef configSETUP_TICK_INTERRUPT
\r
88 #error configSETUP_TICK_INTERRUPT() must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
89 #endif /* configSETUP_TICK_INTERRUPT */
\r
91 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY
\r
92 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
95 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0
\r
96 #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0
\r
99 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES
\r
100 #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
103 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
\r
104 /* Check the configuration. */
\r
105 #if( configMAX_PRIORITIES > 32 )
\r
106 #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
108 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
\r
110 /* In case security extensions are implemented. */
\r
111 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
\r
112 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
\r
115 /* A critical section is exited when the critical section nesting count reaches
\r
117 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
119 /* In all GICs 255 can be written to the priority mask register to unmask all
\r
120 (but the lowest) interrupt priority. */
\r
121 #define portUNMASK_VALUE ( 0xFF )
\r
123 /* Tasks are not created with a floating point context, but can be given a
\r
124 floating point context after they have been created. A variable is stored as
\r
125 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
\r
126 does not have an FPU context, or any other value if the task does have an FPU
\r
128 #define portNO_FLOATING_POINT_CONTEXT ( ( portSTACK_TYPE ) 0 )
\r
130 /* Constants required to setup the initial task context. */
\r
131 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
132 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
133 #define portINTERRUPT_ENABLE_BIT ( 0x80UL )
\r
134 #define portTHUMB_MODE_ADDRESS ( 0x01UL )
\r
136 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary
\r
138 #define portBINARY_POINT_BITS ( ( unsigned char ) 0x03 )
\r
140 /* Masks all bits in the APSR other than the mode bits. */
\r
141 #define portAPSR_MODE_BITS_MASK ( 0x1F )
\r
143 /* The value of the mode bits in the APSR when the CPU is executing in user
\r
145 #define portAPSR_USER_MODE ( 0x10 )
\r
147 /* Macro to unmask all interrupt priorities. */
\r
148 #define portCLEAR_INTERRUPT_MASK() \
\r
151 portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE; \
\r
157 /*-----------------------------------------------------------*/
\r
160 * Starts the first task executing. This function is necessarily written in
\r
161 * assembly code so is implemented in portASM.s.
\r
163 extern void vPortRestoreTaskContext( void );
\r
165 /*-----------------------------------------------------------*/
\r
167 /* A variable is used to keep track of the critical section nesting. This
\r
168 variable has to be stored as part of the task context and must be initialised to
\r
169 a non zero value to ensure interrupts don't inadvertently become unmasked before
\r
170 the scheduler starts. As it is stored as part of the task context it will
\r
171 automatically be set to 0 when the first task is started. */
\r
172 volatile unsigned long ulCriticalNesting = 9999UL;
\r
174 /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero then
\r
175 a floating point context must be saved and restored for the task. */
\r
176 unsigned long ulPortTaskHasFPUContext = pdFALSE;
\r
178 /* Set to 1 to pend a context switch from an ISR. */
\r
179 unsigned long ulPortYieldRequired = pdFALSE;
\r
181 /* Counts the interrupt nesting depth. A context switch is only performed if
\r
182 if the nesting depth is 0. */
\r
183 unsigned long ulPortInterruptNesting = 0UL;
\r
186 /*-----------------------------------------------------------*/
\r
189 * See header file for description.
\r
191 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
193 /* Setup the initial stack of the task. The stack is set exactly as
\r
194 expected by the portRESTORE_CONTEXT() macro.
\r
196 The fist real value on the stack is the status register, which is set for
\r
197 system mode, with interrupts enabled. A few NULLs are added first to ensure
\r
198 GDB does not try decoding a non-existent return address. */
\r
199 *pxTopOfStack = NULL;
\r
201 *pxTopOfStack = NULL;
\r
203 *pxTopOfStack = NULL;
\r
205 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
207 if( ( ( unsigned long ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
\r
209 /* The task will start in THUMB mode. */
\r
210 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
215 /* Next the return address, which in this case is the start of the task. */
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
219 /* Next all the registers other than the stack pointer. */
\r
220 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000; /* R14 */
\r
222 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
224 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
226 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
228 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
230 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
232 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
234 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
236 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
238 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
240 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
242 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
244 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
246 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
249 /* The task will start with a critical nesting count of 0 as interrupts are
\r
251 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
254 /* The task will start without a floating point context. A task that uses
\r
255 the floating point hardware must call vPortTaskUsesFPU() before executing
\r
256 any floating point instructions. */
\r
257 *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
\r
259 return pxTopOfStack;
\r
261 /*-----------------------------------------------------------*/
\r
263 portBASE_TYPE xPortStartScheduler( void )
\r
265 unsigned long ulAPSR;
\r
267 /* Only continue if the CPU is not in User mode. The CPU must be in a
\r
268 Privileged mode for the scheduler to start. */
\r
269 __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );
\r
270 ulAPSR &= portAPSR_MODE_BITS_MASK;
\r
271 configASSERT( ulAPSR != portAPSR_USER_MODE );
\r
273 if( ulAPSR != portAPSR_USER_MODE )
\r
275 /* Only continue if the binary point value is set to its lowest possible
\r
276 setting. See the comments in vPortValidateInterruptPriority() below for
\r
277 more information. */
\r
278 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
\r
280 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )
\r
282 /* Start the timer that generates the tick ISR. */
\r
283 configSETUP_TICK_INTERRUPT();
\r
286 vPortRestoreTaskContext();
\r
290 /* Will only get here if xTaskStartScheduler() was called with the CPU in
\r
291 a non-privileged mode or the binary point register was not set to its lowest
\r
295 /*-----------------------------------------------------------*/
\r
297 void vPortEndScheduler( void )
\r
299 /* It is unlikely that the ARM port will require this function as there
\r
300 is nothing to return to. */
\r
302 /*-----------------------------------------------------------*/
\r
304 void vPortEnterCritical( void )
\r
306 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
307 ulPortSetInterruptMask();
\r
309 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
310 directly. Increment ulCriticalNesting to keep a count of how many times
\r
311 portENTER_CRITICAL() has been called. */
\r
312 ulCriticalNesting++;
\r
314 /*-----------------------------------------------------------*/
\r
316 void vPortExitCritical( void )
\r
318 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
320 /* Decrement the nesting count as the critical section is being
\r
322 ulCriticalNesting--;
\r
324 /* If the nesting level has reached zero then all interrupt
\r
325 priorities must be re-enabled. */
\r
326 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
328 /* Critical nesting has reached zero so all interrupt priorities
\r
329 should be unmasked. */
\r
330 portCLEAR_INTERRUPT_MASK();
\r
334 /*-----------------------------------------------------------*/
\r
336 void FreeRTOS_Tick_Handler( void )
\r
338 /* Set interrupt mask before altering scheduler structures. The tick
\r
339 handler runs at the lowest priority, so interrupts cannot already be masked,
\r
340 so there is no need to save and restore the current mask value. */
\r
342 portICCPMR_PRIORITY_MASK_REGISTER = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
\r
347 /* Increment the RTOS tick. */
\r
348 if( xTaskIncrementTick() != pdFALSE )
\r
350 ulPortYieldRequired = pdTRUE;
\r
353 /* Ensure all interrupt priorities are active again. */
\r
354 portCLEAR_INTERRUPT_MASK();
\r
356 /*-----------------------------------------------------------*/
\r
358 void vPortTaskUsesFPU( void )
\r
360 unsigned long ulInitialFPSCR = 0;
\r
362 /* A task is registering the fact that it needs an FPU context. Set the
\r
363 FPU flag (which is saved as part of the task context). */
\r
364 ulPortTaskHasFPUContext = pdTRUE;
\r
366 /* Initialise the floating point status register. */
\r
367 __asm( "FMXR FPSCR, %0" :: "r" (ulInitialFPSCR) );
\r
369 /*-----------------------------------------------------------*/
\r
371 void vPortClearInterruptMask( unsigned long ulNewMaskValue )
\r
373 if( ulNewMaskValue == pdFALSE )
\r
375 portCLEAR_INTERRUPT_MASK();
\r
378 /*-----------------------------------------------------------*/
\r
380 unsigned long ulPortSetInterruptMask( void )
\r
382 unsigned long ulReturn;
\r
385 if( portICCPMR_PRIORITY_MASK_REGISTER == ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )
\r
387 /* Interrupts were already masked. */
\r
392 ulReturn = pdFALSE;
\r
393 portICCPMR_PRIORITY_MASK_REGISTER = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
\r
401 /*-----------------------------------------------------------*/
\r
403 #if( configASSERT_DEFINED == 1 )
\r
405 void vPortValidateInterruptPriority( void )
\r
407 /* The following assertion will fail if a service routine (ISR) for
\r
408 an interrupt that has been assigned a priority above
\r
409 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
\r
410 function. ISR safe FreeRTOS API functions must *only* be called
\r
411 from interrupts that have been assigned a priority at or below
\r
412 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
414 Numerically low interrupt priority numbers represent logically high
\r
415 interrupt priorities, therefore the priority of the interrupt must
\r
416 be set to a value equal to or numerically *higher* than
\r
417 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
419 FreeRTOS maintains separate thread and ISR API functions to ensure
\r
420 interrupt entry is as fast and simple as possible.
\r
422 The following links provide detailed information:
\r
423 http://www.freertos.org/RTOS-Cortex-M3-M4.html
\r
424 http://www.freertos.org/FAQHelp.html */
\r
425 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );
\r
427 /* Priority grouping: The interrupt controller (GIC) allows the bits
\r
428 that define each interrupt's priority to be split between bits that
\r
429 define the interrupt's pre-emption priority bits and bits that define
\r
430 the interrupt's sub-priority. For simplicity all bits must be defined
\r
431 to be pre-emption priority bits. The following assertion will fail if
\r
432 this is not the case (if some bits represent a sub-priority).
\r
434 The priority grouping is configured by the GIC's binary point register
\r
435 (ICCBPR). Writting 0 to ICCBPR will ensure it is set to its lowest
\r
436 possible value (which may be above 0). */
\r
437 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );
\r
440 #endif /* configASSERT_DEFINED */
\r