]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CA9/port.c
650c377c96f2e589d9592786df57d871f17f8168
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CA9 / port.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* Standard includes. */\r
97 #include <stdlib.h>\r
98 \r
99 /* Scheduler includes. */\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 \r
103 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS\r
104         #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
105 #endif\r
106 \r
107 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET\r
108         #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
109 #endif\r
110 \r
111 #ifndef configUNIQUE_INTERRUPT_PRIORITIES\r
112         #error configUNIQUE_INTERRUPT_PRIORITIES must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
113 #endif\r
114 \r
115 #ifndef configSETUP_TICK_INTERRUPT\r
116         #error configSETUP_TICK_INTERRUPT() must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
117 #endif /* configSETUP_TICK_INTERRUPT */\r
118 \r
119 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY\r
120         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
121 #endif\r
122 \r
123 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
124         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
125 #endif\r
126 \r
127 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES\r
128         #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
129 #endif\r
130 \r
131 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
132         /* Check the configuration. */\r
133         #if( configMAX_PRIORITIES > 32 )\r
134                 #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
135         #endif\r
136 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
137 \r
138 /* In case security extensions are implemented. */\r
139 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
140         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
141 #endif\r
142 \r
143 /* Some vendor specific files default configCLEAR_TICK_INTERRUPT() in\r
144 portmacro.h. */\r
145 #ifndef configCLEAR_TICK_INTERRUPT\r
146         #define configCLEAR_TICK_INTERRUPT()\r
147 #endif\r
148 \r
149 /* A critical section is exited when the critical section nesting count reaches\r
150 this value. */\r
151 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
152 \r
153 /* In all GICs 255 can be written to the priority mask register to unmask all\r
154 (but the lowest) interrupt priority. */\r
155 #define portUNMASK_VALUE                                ( 0xFFUL )\r
156 \r
157 /* Tasks are not created with a floating point context, but can be given a\r
158 floating point context after they have been created.  A variable is stored as\r
159 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
160 does not have an FPU context, or any other value if the task does have an FPU\r
161 context. */\r
162 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
163 \r
164 /* Constants required to setup the initial task context. */\r
165 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, IRQ enabled FIQ enabled. */\r
166 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
167 #define portINTERRUPT_ENABLE_BIT                ( 0x80UL )\r
168 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
169 \r
170 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary\r
171 point is zero. */\r
172 #define portBINARY_POINT_BITS                   ( ( uint8_t ) 0x03 )\r
173 \r
174 /* Masks all bits in the APSR other than the mode bits. */\r
175 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
176 \r
177 /* The value of the mode bits in the APSR when the CPU is executing in user\r
178 mode. */\r
179 #define portAPSR_USER_MODE                              ( 0x10 )\r
180 \r
181 /* The critical section macros only mask interrupts up to an application\r
182 determined priority level.  Sometimes it is necessary to turn interrupt off in\r
183 the CPU itself before modifying certain hardware registers. */\r
184 #define portCPU_IRQ_DISABLE()                                                                           \\r
185         __asm volatile ( "CPSID i" );                                                                   \\r
186         __asm volatile ( "DSB" );                                                                               \\r
187         __asm volatile ( "ISB" );\r
188 \r
189 #define portCPU_IRQ_ENABLE()                                                                            \\r
190         __asm volatile ( "CPSIE i" );                                                                   \\r
191         __asm volatile ( "DSB" );                                                                               \\r
192         __asm volatile ( "ISB" );\r
193 \r
194 \r
195 /* Macro to unmask all interrupt priorities. */\r
196 #define portCLEAR_INTERRUPT_MASK()                                                                      \\r
197 {                                                                                                                                       \\r
198         portCPU_IRQ_DISABLE();                                                                                  \\r
199         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                   \\r
200         __asm volatile (        "DSB            \n"                                                             \\r
201                                                 "ISB            \n" );                                                  \\r
202         portCPU_IRQ_ENABLE();                                                                                   \\r
203 }\r
204 \r
205 #define portINTERRUPT_PRIORITY_REGISTER_OFFSET          0x400UL\r
206 #define portMAX_8_BIT_VALUE                                                     ( ( uint8_t ) 0xff )\r
207 #define portBIT_0_SET                                                           ( ( uint8_t ) 0x01 )\r
208 \r
209 /* Let the user override the pre-loading of the initial LR with the address of\r
210 prvTaskExitError() in case is messes up unwinding of the stack in the\r
211 debugger. */\r
212 #ifdef configTASK_RETURN_ADDRESS\r
213         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
214 #else\r
215         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
216 #endif\r
217 \r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /*\r
221  * Starts the first task executing.  This function is necessarily written in\r
222  * assembly code so is implemented in portASM.s.\r
223  */\r
224 extern void vPortRestoreTaskContext( void );\r
225 \r
226 /*\r
227  * Used to catch tasks that attempt to return from their implementing function.\r
228  */\r
229 static void prvTaskExitError( void );\r
230 \r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /* A variable is used to keep track of the critical section nesting.  This\r
234 variable has to be stored as part of the task context and must be initialised to\r
235 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
236 the scheduler starts.  As it is stored as part of the task context it will\r
237 automatically be set to 0 when the first task is started. */\r
238 volatile uint32_t ulCriticalNesting = 9999UL;\r
239 \r
240 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero then\r
241 a floating point context must be saved and restored for the task. */\r
242 uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
243 \r
244 /* Set to 1 to pend a context switch from an ISR. */\r
245 uint32_t ulPortYieldRequired = pdFALSE;\r
246 \r
247 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
248 if the nesting depth is 0. */\r
249 uint32_t ulPortInterruptNesting = 0UL;\r
250 \r
251 __attribute__(( used )) const uint32_t ulICCIAR = portICCIAR_INTERRUPT_ACKNOWLEDGE_REGISTER_ADDRESS;\r
252 __attribute__(( used )) const uint32_t ulICCEOIR = portICCEOIR_END_OF_INTERRUPT_REGISTER_ADDRESS;\r
253 __attribute__(( used )) const uint32_t ulICCPMR = portICCPMR_PRIORITY_MASK_REGISTER_ADDRESS;\r
254 __attribute__(( used )) const uint32_t ulMaxAPIPriorityMask = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
255 \r
256 /*-----------------------------------------------------------*/\r
257 \r
258 /*\r
259  * See header file for description.\r
260  */\r
261 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
262 {\r
263         /* Setup the initial stack of the task.  The stack is set exactly as\r
264         expected by the portRESTORE_CONTEXT() macro.\r
265 \r
266         The fist real value on the stack is the status register, which is set for\r
267         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
268         GDB does not try decoding a non-existent return address. */\r
269         *pxTopOfStack = ( StackType_t ) NULL;\r
270         pxTopOfStack--;\r
271         *pxTopOfStack = ( StackType_t ) NULL;\r
272         pxTopOfStack--;\r
273         *pxTopOfStack = ( StackType_t ) NULL;\r
274         pxTopOfStack--;\r
275         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
276 \r
277         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
278         {\r
279                 /* The task will start in THUMB mode. */\r
280                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
281         }\r
282 \r
283         pxTopOfStack--;\r
284 \r
285         /* Next the return address, which in this case is the start of the task. */\r
286         *pxTopOfStack = ( StackType_t ) pxCode;\r
287         pxTopOfStack--;\r
288 \r
289         /* Next all the registers other than the stack pointer. */\r
290         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;        /* R14 */\r
291         pxTopOfStack--;\r
292         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
293         pxTopOfStack--;\r
294         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
295         pxTopOfStack--;\r
296         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
297         pxTopOfStack--;\r
298         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
299         pxTopOfStack--;\r
300         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
301         pxTopOfStack--;\r
302         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
303         pxTopOfStack--;\r
304         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
305         pxTopOfStack--;\r
306         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
307         pxTopOfStack--;\r
308         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
309         pxTopOfStack--;\r
310         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
311         pxTopOfStack--;\r
312         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
313         pxTopOfStack--;\r
314         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
315         pxTopOfStack--;\r
316         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
317         pxTopOfStack--;\r
318 \r
319         /* The task will start with a critical nesting count of 0 as interrupts are\r
320         enabled. */\r
321         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
322         pxTopOfStack--;\r
323 \r
324         /* The task will start without a floating point context.  A task that uses\r
325         the floating point hardware must call vPortTaskUsesFPU() before executing\r
326         any floating point instructions. */\r
327         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
328 \r
329         return pxTopOfStack;\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 static void prvTaskExitError( void )\r
334 {\r
335         /* A function that implements a task must not exit or attempt to return to\r
336         its caller as there is nothing to return to.  If a task wants to exit it\r
337         should instead call vTaskDelete( NULL ).\r
338 \r
339         Artificially force an assert() to be triggered if configASSERT() is\r
340         defined, then stop here so application writers can catch the error. */\r
341         configASSERT( ulPortInterruptNesting == ~0UL );\r
342         portDISABLE_INTERRUPTS();\r
343         for( ;; );\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 BaseType_t xPortStartScheduler( void )\r
348 {\r
349 uint32_t ulAPSR;\r
350 \r
351         #if( configASSERT_DEFINED == 1 )\r
352         {\r
353                 volatile uint32_t ulOriginalPriority;\r
354                 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + portINTERRUPT_PRIORITY_REGISTER_OFFSET );\r
355                 volatile uint8_t ucMaxPriorityValue;\r
356 \r
357                 /* Determine how many priority bits are implemented in the GIC.\r
358 \r
359                 Save the interrupt priority value that is about to be clobbered. */\r
360                 ulOriginalPriority = *pucFirstUserPriorityRegister;\r
361 \r
362                 /* Determine the number of priority bits available.  First write to\r
363                 all possible bits. */\r
364                 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;\r
365 \r
366                 /* Read the value back to see how many bits stuck. */\r
367                 ucMaxPriorityValue = *pucFirstUserPriorityRegister;\r
368 \r
369                 /* Shift to the least significant bits. */\r
370                 while( ( ucMaxPriorityValue & portBIT_0_SET ) != portBIT_0_SET )\r
371                 {\r
372                         ucMaxPriorityValue >>= ( uint8_t ) 0x01;\r
373                 }\r
374 \r
375                 /* Sanity check configUNIQUE_INTERRUPT_PRIORITIES matches the read\r
376                 value. */\r
377                 configASSERT( ucMaxPriorityValue == portLOWEST_INTERRUPT_PRIORITY );\r
378 \r
379                 /* Restore the clobbered interrupt priority register to its original\r
380                 value. */\r
381                 *pucFirstUserPriorityRegister = ulOriginalPriority;\r
382         }\r
383         #endif /* conifgASSERT_DEFINED */\r
384 \r
385 \r
386         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
387         Privileged mode for the scheduler to start. */\r
388         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
389         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
390         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
391 \r
392         if( ulAPSR != portAPSR_USER_MODE )\r
393         {\r
394                 /* Only continue if the binary point value is set to its lowest possible\r
395                 setting.  See the comments in vPortValidateInterruptPriority() below for\r
396                 more information. */\r
397                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
398 \r
399                 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )\r
400                 {\r
401                         /* Interrupts are turned off in the CPU itself to ensure tick does\r
402                         not execute     while the scheduler is being started.  Interrupts are\r
403                         automatically turned back on in the CPU when the first task starts\r
404                         executing. */\r
405                         portCPU_IRQ_DISABLE();\r
406 \r
407                         /* Start the timer that generates the tick ISR. */\r
408                         configSETUP_TICK_INTERRUPT();\r
409 \r
410                         /* Start the first task executing. */\r
411                         vPortRestoreTaskContext();\r
412                 }\r
413         }\r
414 \r
415         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
416         a non-privileged mode or the binary point register was not set to its lowest\r
417         possible value.  prvTaskExitError() is referenced to prevent a compiler\r
418         warning about it being defined but not referenced in the case that the user\r
419         defines their own exit address. */\r
420         ( void ) prvTaskExitError;\r
421         return 0;\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r
425 void vPortEndScheduler( void )\r
426 {\r
427         /* Not implemented in ports where there is nothing to return to.\r
428         Artificially force an assert. */\r
429         configASSERT( ulCriticalNesting == 1000UL );\r
430 }\r
431 /*-----------------------------------------------------------*/\r
432 \r
433 void vPortEnterCritical( void )\r
434 {\r
435         /* Mask interrupts up to the max syscall interrupt priority. */\r
436         ulPortSetInterruptMask();\r
437 \r
438         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
439         directly.  Increment ulCriticalNesting to keep a count of how many times\r
440         portENTER_CRITICAL() has been called. */\r
441         ulCriticalNesting++;\r
442 \r
443         /* This is not the interrupt safe version of the enter critical function so\r
444         assert() if it is being called from an interrupt context.  Only API\r
445         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
446         the critical nesting count is 1 to protect against recursive calls if the\r
447         assert function also uses a critical section. */\r
448         if( ulCriticalNesting == 1 )\r
449         {\r
450                 configASSERT( ulPortInterruptNesting == 0 );\r
451         }\r
452 }\r
453 /*-----------------------------------------------------------*/\r
454 \r
455 void vPortExitCritical( void )\r
456 {\r
457         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
458         {\r
459                 /* Decrement the nesting count as the critical section is being\r
460                 exited. */\r
461                 ulCriticalNesting--;\r
462 \r
463                 /* If the nesting level has reached zero then all interrupt\r
464                 priorities must be re-enabled. */\r
465                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
466                 {\r
467                         /* Critical nesting has reached zero so all interrupt priorities\r
468                         should be unmasked. */\r
469                         portCLEAR_INTERRUPT_MASK();\r
470                 }\r
471         }\r
472 }\r
473 /*-----------------------------------------------------------*/\r
474 \r
475 void FreeRTOS_Tick_Handler( void )\r
476 {\r
477         /* Set interrupt mask before altering scheduler structures.   The tick\r
478         handler runs at the lowest priority, so interrupts cannot already be masked,\r
479         so there is no need to save and restore the current mask value.  It is\r
480         necessary to turn off interrupts in the CPU itself while the ICCPMR is being\r
481         updated. */\r
482         portCPU_IRQ_DISABLE();\r
483         portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
484         __asm volatile (        "dsb            \n"\r
485                                                 "isb            \n" );\r
486         portCPU_IRQ_ENABLE();\r
487 \r
488         /* Increment the RTOS tick. */\r
489         if( xTaskIncrementTick() != pdFALSE )\r
490         {\r
491                 ulPortYieldRequired = pdTRUE;\r
492         }\r
493 \r
494         /* Ensure all interrupt priorities are active again. */\r
495         portCLEAR_INTERRUPT_MASK();\r
496         configCLEAR_TICK_INTERRUPT();\r
497 }\r
498 /*-----------------------------------------------------------*/\r
499 \r
500 void vPortTaskUsesFPU( void )\r
501 {\r
502 uint32_t ulInitialFPSCR = 0;\r
503 \r
504         /* A task is registering the fact that it needs an FPU context.  Set the\r
505         FPU flag (which is saved as part of the task context). */\r
506         ulPortTaskHasFPUContext = pdTRUE;\r
507 \r
508         /* Initialise the floating point status register. */\r
509         __asm volatile ( "FMXR  FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
510 }\r
511 /*-----------------------------------------------------------*/\r
512 \r
513 void vPortClearInterruptMask( uint32_t ulNewMaskValue )\r
514 {\r
515         if( ulNewMaskValue == pdFALSE )\r
516         {\r
517                 portCLEAR_INTERRUPT_MASK();\r
518         }\r
519 }\r
520 /*-----------------------------------------------------------*/\r
521 \r
522 uint32_t ulPortSetInterruptMask( void )\r
523 {\r
524 uint32_t ulReturn;\r
525 \r
526         /* Interrupt in the CPU must be turned off while the ICCPMR is being\r
527         updated. */\r
528         portCPU_IRQ_DISABLE();\r
529         if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )\r
530         {\r
531                 /* Interrupts were already masked. */\r
532                 ulReturn = pdTRUE;\r
533         }\r
534         else\r
535         {\r
536                 ulReturn = pdFALSE;\r
537                 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
538                 __asm volatile (        "dsb            \n"\r
539                                                         "isb            \n" );\r
540         }\r
541         portCPU_IRQ_ENABLE();\r
542 \r
543         return ulReturn;\r
544 }\r
545 /*-----------------------------------------------------------*/\r
546 \r
547 #if( configASSERT_DEFINED == 1 )\r
548 \r
549         void vPortValidateInterruptPriority( void )\r
550         {\r
551                 /* The following assertion will fail if a service routine (ISR) for\r
552                 an interrupt that has been assigned a priority above\r
553                 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
554                 function.  ISR safe FreeRTOS API functions must *only* be called\r
555                 from interrupts that have been assigned a priority at or below\r
556                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
557 \r
558                 Numerically low interrupt priority numbers represent logically high\r
559                 interrupt priorities, therefore the priority of the interrupt must\r
560                 be set to a value equal to or numerically *higher* than\r
561                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
562 \r
563                 FreeRTOS maintains separate thread and ISR API functions to ensure\r
564                 interrupt entry is as fast and simple as possible. */\r
565                 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );\r
566 \r
567                 /* Priority grouping:  The interrupt controller (GIC) allows the bits\r
568                 that define each interrupt's priority to be split between bits that\r
569                 define the interrupt's pre-emption priority bits and bits that define\r
570                 the interrupt's sub-priority.  For simplicity all bits must be defined\r
571                 to be pre-emption priority bits.  The following assertion will fail if\r
572                 this is not the case (if some bits represent a sub-priority).\r
573 \r
574                 The priority grouping is configured by the GIC's binary point register\r
575                 (ICCBPR).  Writting 0 to ICCBPR will ensure it is set to its lowest\r
576                 possible value (which may be above 0). */\r
577                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
578         }\r
579 \r
580 #endif /* configASSERT_DEFINED */\r
581 /*-----------------------------------------------------------*/\r
582 \r