]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CA9/port.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CA9 / port.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\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
11      *                                                                       *\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
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \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
25 \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
29     >>! kernel.\r
30 \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
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \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
53 \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
57 \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
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* Standard includes. */\r
66 #include <stdlib.h>\r
67 \r
68 /* IAR includes. */\r
69 #include <intrinsics.h>\r
70 \r
71 /* Scheduler includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \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
77 #endif\r
78 \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
81 #endif\r
82 \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
85 #endif\r
86 \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
90 \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
93 #endif\r
94 \r
95 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
96         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
97 #endif\r
98 \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
101 #endif\r
102 \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
107         #endif\r
108 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
109 \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
113 #endif\r
114 \r
115 /* A critical section is exited when the critical section nesting count reaches\r
116 this value. */\r
117 #define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )\r
118 \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
122 \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
127 context. */\r
128 #define portNO_FLOATING_POINT_CONTEXT   ( ( portSTACK_TYPE ) 0 )\r
129 \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
135 \r
136 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary\r
137 point is zero. */\r
138 #define portBINARY_POINT_BITS                   ( ( unsigned char ) 0x03 )\r
139 \r
140 /* Masks all bits in the APSR other than the mode bits. */\r
141 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
142 \r
143 /* The value of the mode bits in the APSR when the CPU is executing in user\r
144 mode. */\r
145 #define portAPSR_USER_MODE                              ( 0x10 )\r
146 \r
147 /* Macro to unmask all interrupt priorities. */\r
148 #define portCLEAR_INTERRUPT_MASK()                                                                                      \\r
149 {                                                                                                                                                       \\r
150         __disable_irq();                                                                                                                \\r
151         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                                   \\r
152         __asm(  "DSB            \n"                                                                                                     \\r
153                         "ISB            \n" );                                                                                          \\r
154         __enable_irq();                                                                                                                 \\r
155 }\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * Starts the first task executing.  This function is necessarily written in\r
161  * assembly code so is implemented in portASM.s.\r
162  */\r
163 extern void vPortRestoreTaskContext( void );\r
164 \r
165 /*-----------------------------------------------------------*/\r
166 \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
173 \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
177 \r
178 /* Set to 1 to pend a context switch from an ISR. */\r
179 unsigned long ulPortYieldRequired = pdFALSE;\r
180 \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
184 \r
185 \r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /*\r
189  * See header file for description.\r
190  */\r
191 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
192 {\r
193         /* Setup the initial stack of the task.  The stack is set exactly as\r
194         expected by the portRESTORE_CONTEXT() macro.\r
195 \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
200         pxTopOfStack--;\r
201         *pxTopOfStack = NULL;\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = NULL;\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
206 \r
207         if( ( ( unsigned long ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
208         {\r
209                 /* The task will start in THUMB mode. */\r
210                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
211         }\r
212 \r
213         pxTopOfStack--;\r
214 \r
215         /* Next the return address, which in this case is the start of the task. */\r
216         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;\r
217         pxTopOfStack--;\r
218 \r
219         /* Next all the registers other than the stack pointer. */\r
220         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;  /* R14 */\r
221         pxTopOfStack--;\r
222         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
223         pxTopOfStack--;\r
224         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
225         pxTopOfStack--;\r
226         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
227         pxTopOfStack--;\r
228         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
229         pxTopOfStack--;\r
230         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
231         pxTopOfStack--;\r
232         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
233         pxTopOfStack--;\r
234         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
235         pxTopOfStack--;\r
236         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
237         pxTopOfStack--;\r
238         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
239         pxTopOfStack--;\r
240         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
241         pxTopOfStack--;\r
242         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
243         pxTopOfStack--;\r
244         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
245         pxTopOfStack--;\r
246         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
247         pxTopOfStack--;\r
248 \r
249         /* The task will start with a critical nesting count of 0 as interrupts are\r
250         enabled. */\r
251         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
252         pxTopOfStack--;\r
253 \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
258 \r
259         return pxTopOfStack;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 portBASE_TYPE xPortStartScheduler( void )\r
264 {\r
265 unsigned long ulAPSR;\r
266 \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
272 \r
273         if( ulAPSR != portAPSR_USER_MODE )\r
274         {\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
279 \r
280                 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )\r
281                 {\r
282                         /* Start the timer that generates the tick ISR. */\r
283                         configSETUP_TICK_INTERRUPT();\r
284 \r
285                         __enable_irq();\r
286                         vPortRestoreTaskContext();\r
287                 }\r
288         }\r
289 \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
292         possible value. */\r
293         return 0;\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 void vPortEndScheduler( void )\r
298 {\r
299         /* It is unlikely that the ARM port will require this function as there\r
300         is nothing to return to. */\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 void vPortEnterCritical( void )\r
305 {\r
306         /* Disable interrupts as per portDISABLE_INTERRUPTS();  */\r
307         ulPortSetInterruptMask();\r
308 \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
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 void vPortExitCritical( void )\r
317 {\r
318         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
319         {\r
320                 /* Decrement the nesting count as the critical section is being\r
321                 exited. */\r
322                 ulCriticalNesting--;\r
323 \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
327                 {\r
328                         /* Critical nesting has reached zero so all interrupt priorities\r
329                         should be unmasked. */\r
330                         portCLEAR_INTERRUPT_MASK();\r
331                 }\r
332         }\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 void FreeRTOS_Tick_Handler( void )\r
337 {\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
341         __disable_irq();\r
342         portICCPMR_PRIORITY_MASK_REGISTER = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
343         __asm(  "DSB            \n"\r
344                         "ISB            \n" );\r
345         __enable_irq();\r
346 \r
347         /* Increment the RTOS tick. */\r
348         if( xTaskIncrementTick() != pdFALSE )\r
349         {\r
350                 ulPortYieldRequired = pdTRUE;\r
351         }\r
352 \r
353         /* Ensure all interrupt priorities are active again. */\r
354         portCLEAR_INTERRUPT_MASK();\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 void vPortTaskUsesFPU( void )\r
359 {\r
360 unsigned long ulInitialFPSCR = 0;\r
361 \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
365 \r
366         /* Initialise the floating point status register. */\r
367         __asm( "FMXR    FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 void vPortClearInterruptMask( unsigned long ulNewMaskValue )\r
372 {\r
373         if( ulNewMaskValue == pdFALSE )\r
374         {\r
375                 portCLEAR_INTERRUPT_MASK();\r
376         }\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 unsigned long ulPortSetInterruptMask( void )\r
381 {\r
382 unsigned long ulReturn;\r
383 \r
384         __disable_irq();\r
385         if( portICCPMR_PRIORITY_MASK_REGISTER == ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )\r
386         {\r
387                 /* Interrupts were already masked. */\r
388                 ulReturn = pdTRUE;\r
389         }\r
390         else\r
391         {\r
392                 ulReturn = pdFALSE;\r
393                 portICCPMR_PRIORITY_MASK_REGISTER = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
394                 __asm(  "DSB            \n"\r
395                                 "ISB            \n" );\r
396         }\r
397         __enable_irq();\r
398 \r
399         return ulReturn;\r
400 }\r
401 /*-----------------------------------------------------------*/\r
402 \r
403 #if( configASSERT_DEFINED == 1 )\r
404 \r
405         void vPortValidateInterruptPriority( void )\r
406         {\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
413 \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
418 \r
419                 FreeRTOS maintains separate thread and ISR API functions to ensure\r
420                 interrupt entry is as fast and simple as possible.\r
421 \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
426 \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
433 \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
438         }\r
439 \r
440 #endif /* configASSERT_DEFINED */\r
441 \r
442 \r