2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* Standard includes. */
\r
79 #include <intrinsics.h>
\r
81 /* Scheduler includes. */
\r
82 #include "FreeRTOS.h"
\r
85 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS
\r
86 #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
89 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET
\r
90 #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
93 #ifndef configUNIQUE_INTERRUPT_PRIORITIES
\r
94 #error configUNIQUE_INTERRUPT_PRIORITIES must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
97 #ifndef configSETUP_TICK_INTERRUPT
\r
98 #error configSETUP_TICK_INTERRUPT() must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
99 #endif /* configSETUP_TICK_INTERRUPT */
\r
101 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY
\r
102 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined. See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html
\r
105 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0
\r
106 #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0
\r
109 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES
\r
110 #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
113 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
\r
114 /* Check the configuration. */
\r
115 #if( configMAX_PRIORITIES > 32 )
\r
116 #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
118 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
\r
120 /* In case security extensions are implemented. */
\r
121 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
\r
122 #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )
\r
125 /* A critical section is exited when the critical section nesting count reaches
\r
127 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
129 /* In all GICs 255 can be written to the priority mask register to unmask all
\r
130 (but the lowest) interrupt priority. */
\r
131 #define portUNMASK_VALUE ( 0xFF )
\r
133 /* Tasks are not created with a floating point context, but can be given a
\r
134 floating point context after they have been created. A variable is stored as
\r
135 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
\r
136 does not have an FPU context, or any other value if the task does have an FPU
\r
138 #define portNO_FLOATING_POINT_CONTEXT ( ( portSTACK_TYPE ) 0 )
\r
140 /* Constants required to setup the initial task context. */
\r
141 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
142 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
143 #define portINTERRUPT_ENABLE_BIT ( 0x80UL )
\r
144 #define portTHUMB_MODE_ADDRESS ( 0x01UL )
\r
146 /* Masks all bits in the APSR other than the mode bits. */
\r
147 #define portAPSR_MODE_BITS_MASK ( 0x1F )
\r
149 /* The value of the mode bits in the APSR when the CPU is executing in user
\r
151 #define portAPSR_USER_MODE ( 0x10 )
\r
153 /* Macro to unmask all interrupt priorities. */
\r
154 #define portCLEAR_INTERRUPT_MASK() \
\r
157 portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE; \
\r
163 /*-----------------------------------------------------------*/
\r
166 * Starts the first task executing. This function is necessarily written in
\r
167 * assembly code so is implemented in portASM.s.
\r
169 extern void vPortRestoreTaskContext( void );
\r
171 /*-----------------------------------------------------------*/
\r
173 /* A variable is used to keep track of the critical section nesting. This
\r
174 variable has to be stored as part of the task context and must be initialised to
\r
175 a non zero value to ensure interrupts don't inadvertently become unmasked before
\r
176 the scheduler starts. As it is stored as part of the task context it will
\r
177 automatically be set to 0 when the first task is started. */
\r
178 volatile unsigned long ulCriticalNesting = 9999UL;
\r
180 /* The value to be written to the interrupt controllers priority mask register
\r
181 to mask interrupts that can use the FreeRTOS API without masking higher priority
\r
183 const unsigned long ulPortAPIPriorityMask = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
\r
185 /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero then
\r
186 a floating point context must be saved and restored for the task. */
\r
187 unsigned long ulPortTaskHasFPUContext = pdFALSE;
\r
189 /* Set to 1 to pend a context switch from an ISR. */
\r
190 unsigned long ulPortYieldRequired = pdFALSE;
\r
192 /* Counts the interrupt nesting depth. A context switch is only performed if
\r
193 if the nesting depth is 0. */
\r
194 unsigned long ulPortInterruptNesting = 0UL;
\r
196 /*-----------------------------------------------------------*/
\r
199 * See header file for description.
\r
201 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
203 /* Setup the initial stack of the task. The stack is set exactly as
\r
204 expected by the portRESTORE_CONTEXT() macro.
\r
206 The fist real value on the stack is the status register, which is set for
\r
207 system mode, with interrupts enabled. A few NULLs are added first to ensure
\r
208 GDB does not try decoding a non-existent return address. */
\r
209 *pxTopOfStack = NULL;
\r
211 *pxTopOfStack = NULL;
\r
213 *pxTopOfStack = NULL;
\r
215 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
217 if( ( ( unsigned long ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
\r
219 /* The task will start in THUMB mode. */
\r
220 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
225 /* Next the return address, which in this case is the start of the task. */
\r
226 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
229 /* Next all the registers other than the stack pointer. */
\r
230 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
\r
232 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
234 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
236 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
238 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
240 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
242 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
244 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
246 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
248 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
250 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
252 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
254 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
256 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
259 /* The task will start with a critical nesting count of 0 as interrupts are
\r
261 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
264 /* The task will start without a floating point context. A task that uses
\r
265 the floating point hardware must call vPortTaskUsesFPU() before executing
\r
266 any floating point instructions. */
\r
267 *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
\r
269 return pxTopOfStack;
\r
271 /*-----------------------------------------------------------*/
\r
273 portBASE_TYPE xPortStartScheduler( void )
\r
275 unsigned long ulAPSR;
\r
277 /* Only continue if the CPU is not in User mode. The CPU must be in a
\r
278 Privileged mode for the scheduler to start. */
\r
279 __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );
\r
280 ulAPSR &= portAPSR_MODE_BITS_MASK;
\r
281 configASSERT( ulAPSR != portAPSR_USER_MODE );
\r
283 if( ulAPSR != portAPSR_USER_MODE )
\r
285 /* Start the timer that generates the tick ISR. */
\r
286 configSETUP_TICK_INTERRUPT();
\r
289 vPortRestoreTaskContext();
\r
292 /* Will only get here if xTaskStartScheduler() was called with the CPU in
\r
293 a non-privileged mode. */
\r
296 /*-----------------------------------------------------------*/
\r
298 void vPortEndScheduler( void )
\r
300 /* It is unlikely that the ARM port will require this function as there
\r
301 is nothing to return to. */
\r
303 /*-----------------------------------------------------------*/
\r
305 void vPortEnterCritical( void )
\r
307 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
308 ulPortSetInterruptMask();
\r
310 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
311 directly. Increment ulCriticalNesting to keep a count of how many times
\r
312 portENTER_CRITICAL() has been called. */
\r
313 ulCriticalNesting++;
\r
315 /*-----------------------------------------------------------*/
\r
317 void vPortExitCritical( void )
\r
319 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
321 /* Decrement the nesting count as the critical section is being
\r
323 ulCriticalNesting--;
\r
325 /* If the nesting level has reached zero then all interrupt
\r
326 priorities must be re-enabled. */
\r
327 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
329 /* Critical nesting has reached zero so all interrupt priorities
\r
330 should be unmasked. */
\r
331 portCLEAR_INTERRUPT_MASK();
\r
335 /*-----------------------------------------------------------*/
\r
337 void FreeRTOS_Tick_Handler( void )
\r
339 /* Set interrupt mask before altering scheduler structures. The tick
\r
340 handler runs at the lowest priority, so interrupts cannot already be masked,
\r
341 so there is no need to save and restore the current mask value. */
\r
343 portICCPMR_PRIORITY_MASK_REGISTER = ulPortAPIPriorityMask;
\r
348 /* Increment the RTOS tick. */
\r
349 if( xTaskIncrementTick() != pdFALSE )
\r
351 ulPortYieldRequired = pdTRUE;
\r
354 /* Ensure all interrupt priorities are active again. */
\r
355 portCLEAR_INTERRUPT_MASK();
\r
357 /*-----------------------------------------------------------*/
\r
359 void vPortTaskUsesFPU( void )
\r
361 unsigned long ulInitialFPSCR = 0;
\r
363 /* A task is registering the fact that it needs an FPU context. Set the
\r
364 FPU flag (which is saved as part of the task context). */
\r
365 ulPortTaskHasFPUContext = pdTRUE;
\r
367 /* Initialise the floating point status register. */
\r
368 __asm( "FMXR FPSCR, %0" :: "r" (ulInitialFPSCR) );
\r
370 /*-----------------------------------------------------------*/
\r
372 void vPortClearInterruptMask( unsigned long ulNewMaskValue )
\r
374 if( ulNewMaskValue == pdFALSE )
\r
376 portCLEAR_INTERRUPT_MASK();
\r
379 /*-----------------------------------------------------------*/
\r
381 unsigned long ulPortSetInterruptMask( void )
\r
383 unsigned long ulReturn;
\r
386 if( portICCPMR_PRIORITY_MASK_REGISTER == ulPortAPIPriorityMask )
\r
388 /* Interrupts were already masked. */
\r
393 ulReturn = pdFALSE;
\r
394 portICCPMR_PRIORITY_MASK_REGISTER = ulPortAPIPriorityMask;
\r