]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CA9/port.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CA9 / port.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdlib.h>\r
72 \r
73 /* IAR includes. */\r
74 #include <intrinsics.h>\r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 \r
80 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS\r
81         #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
82 #endif\r
83 \r
84 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET\r
85         #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
86 #endif\r
87 \r
88 #ifndef configUNIQUE_INTERRUPT_PRIORITIES\r
89         #error configUNIQUE_INTERRUPT_PRIORITIES must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
90 #endif\r
91 \r
92 #ifndef configSETUP_TICK_INTERRUPT\r
93         #error configSETUP_TICK_INTERRUPT() must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
94 #endif /* configSETUP_TICK_INTERRUPT */\r
95 \r
96 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY\r
97         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
98 #endif\r
99 \r
100 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
101         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
102 #endif\r
103 \r
104 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES\r
105         #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
106 #endif\r
107 \r
108 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
109         /* Check the configuration. */\r
110         #if( configMAX_PRIORITIES > 32 )\r
111                 #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
112         #endif\r
113 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
114 \r
115 /* In case security extensions are implemented. */\r
116 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
117         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
118 #endif\r
119 \r
120 #ifndef configCLEAR_TICK_INTERRUPT\r
121         #define configCLEAR_TICK_INTERRUPT()\r
122 #endif\r
123 \r
124 /* A critical section is exited when the critical section nesting count reaches\r
125 this value. */\r
126 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
127 \r
128 /* In all GICs 255 can be written to the priority mask register to unmask all\r
129 (but the lowest) interrupt priority. */\r
130 #define portUNMASK_VALUE                                ( 0xFFUL )\r
131 \r
132 /* Tasks are not created with a floating point context, but can be given a\r
133 floating point context after they have been created.  A variable is stored as\r
134 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
135 does not have an FPU context, or any other value if the task does have an FPU\r
136 context. */\r
137 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
138 \r
139 /* Constants required to setup the initial task context. */\r
140 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
141 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
142 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
143 \r
144 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary\r
145 point is zero. */\r
146 #define portBINARY_POINT_BITS                   ( ( uint8_t ) 0x03 )\r
147 \r
148 /* Masks all bits in the APSR other than the mode bits. */\r
149 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
150 \r
151 /* The value of the mode bits in the APSR when the CPU is executing in user\r
152 mode. */\r
153 #define portAPSR_USER_MODE                              ( 0x10 )\r
154 \r
155 /* Macro to unmask all interrupt priorities. */\r
156 #define portCLEAR_INTERRUPT_MASK()                                                                                      \\r
157 {                                                                                                                                                       \\r
158         __disable_irq();                                                                                                                \\r
159         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                                   \\r
160         __asm(  "DSB            \n"                                                                                                     \\r
161                         "ISB            \n" );                                                                                          \\r
162         __enable_irq();                                                                                                                 \\r
163 }\r
164 \r
165 /*-----------------------------------------------------------*/\r
166 \r
167 /*\r
168  * Starts the first task executing.  This function is necessarily written in\r
169  * assembly code so is implemented in portASM.s.\r
170  */\r
171 extern void vPortRestoreTaskContext( void );\r
172 \r
173 /*\r
174  * Used to catch tasks that attempt to return from their implementing function.\r
175  */\r
176 static void prvTaskExitError( void );\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 \r
180 /* A variable is used to keep track of the critical section nesting.  This\r
181 variable has to be stored as part of the task context and must be initialised to\r
182 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
183 the scheduler starts.  As it is stored as part of the task context it will\r
184 automatically be set to 0 when the first task is started. */\r
185 volatile uint32_t ulCriticalNesting = 9999UL;\r
186 \r
187 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero\r
188 then a floating point context must be saved and restored for the task. */\r
189 uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
190 \r
191 /* Set to 1 to pend a context switch from an ISR. */\r
192 uint32_t ulPortYieldRequired = pdFALSE;\r
193 \r
194 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
195 if the nesting depth is 0. */\r
196 uint32_t ulPortInterruptNesting = 0UL;\r
197 \r
198 \r
199 /*-----------------------------------------------------------*/\r
200 \r
201 /*\r
202  * See header file for description.\r
203  */\r
204 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
205 {\r
206         /* Setup the initial stack of the task.  The stack is set exactly as\r
207         expected by the portRESTORE_CONTEXT() macro.\r
208 \r
209         The fist real value on the stack is the status register, which is set for\r
210         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
211         GDB does not try decoding a non-existent return address. */\r
212         *pxTopOfStack = NULL;\r
213         pxTopOfStack--;\r
214         *pxTopOfStack = NULL;\r
215         pxTopOfStack--;\r
216         *pxTopOfStack = NULL;\r
217         pxTopOfStack--;\r
218         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
219 \r
220         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
221         {\r
222                 /* The task will start in THUMB mode. */\r
223                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
224         }\r
225 \r
226         pxTopOfStack--;\r
227 \r
228         /* Next the return address, which in this case is the start of the task. */\r
229         *pxTopOfStack = ( StackType_t ) pxCode;\r
230         pxTopOfStack--;\r
231 \r
232         /* Next all the registers other than the stack pointer. */\r
233         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* R14 */\r
234         pxTopOfStack--;\r
235         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
236         pxTopOfStack--;\r
237         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
238         pxTopOfStack--;\r
239         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
240         pxTopOfStack--;\r
241         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
242         pxTopOfStack--;\r
243         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
244         pxTopOfStack--;\r
245         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
246         pxTopOfStack--;\r
247         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
248         pxTopOfStack--;\r
249         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
250         pxTopOfStack--;\r
251         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
252         pxTopOfStack--;\r
253         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
254         pxTopOfStack--;\r
255         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
256         pxTopOfStack--;\r
257         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
258         pxTopOfStack--;\r
259         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
260         pxTopOfStack--;\r
261 \r
262         /* The task will start with a critical nesting count of 0 as interrupts are\r
263         enabled. */\r
264         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
265         pxTopOfStack--;\r
266 \r
267         /* The task will start without a floating point context.  A task that uses\r
268         the floating point hardware must call vPortTaskUsesFPU() before executing\r
269         any floating point instructions. */\r
270         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
271 \r
272         return pxTopOfStack;\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 static void prvTaskExitError( void )\r
277 {\r
278         /* A function that implements a task must not exit or attempt to return to\r
279         its caller as there is nothing to return to.  If a task wants to exit it\r
280         should instead call vTaskDelete( NULL ).\r
281 \r
282         Artificially force an assert() to be triggered if configASSERT() is\r
283         defined, then stop here so application writers can catch the error. */\r
284         configASSERT( ulPortInterruptNesting == ~0UL );\r
285         portDISABLE_INTERRUPTS();\r
286         for( ;; );\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 BaseType_t xPortStartScheduler( void )\r
291 {\r
292 uint32_t ulAPSR;\r
293 \r
294         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
295         Privileged mode for the scheduler to start. */\r
296         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
297         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
298         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
299 \r
300         if( ulAPSR != portAPSR_USER_MODE )\r
301         {\r
302                 /* Only continue if the binary point value is set to its lowest possible\r
303                 setting.  See the comments in vPortValidateInterruptPriority() below for\r
304                 more information. */\r
305                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
306 \r
307                 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )\r
308                 {\r
309                         /* Start the timer that generates the tick ISR. */\r
310                         configSETUP_TICK_INTERRUPT();\r
311 \r
312                         __enable_irq();\r
313                         vPortRestoreTaskContext();\r
314                 }\r
315         }\r
316 \r
317         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
318         a non-privileged mode or the binary point register was not set to its lowest\r
319         possible value. */\r
320         return 0;\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 void vPortEndScheduler( void )\r
325 {\r
326         /* Not implemented in ports where there is nothing to return to.\r
327         Artificially force an assert. */\r
328         configASSERT( ulCriticalNesting == 1000UL );\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 void vPortEnterCritical( void )\r
333 {\r
334         /* Disable interrupts as per portDISABLE_INTERRUPTS();  */\r
335         ulPortSetInterruptMask();\r
336 \r
337         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
338         directly.  Increment ulCriticalNesting to keep a count of how many times\r
339         portENTER_CRITICAL() has been called. */\r
340         ulCriticalNesting++;\r
341         \r
342         /* This is not the interrupt safe version of the enter critical function so\r
343         assert() if it is being called from an interrupt context.  Only API \r
344         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
345         the critical nesting count is 1 to protect against recursive calls if the\r
346         assert function also uses a critical section. */\r
347         if( ulCriticalNesting == 1 )\r
348         {\r
349                 configASSERT( ulPortInterruptNesting == 0 );\r
350         }\r
351 }\r
352 /*-----------------------------------------------------------*/\r
353 \r
354 void vPortExitCritical( void )\r
355 {\r
356         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
357         {\r
358                 /* Decrement the nesting count as the critical section is being\r
359                 exited. */\r
360                 ulCriticalNesting--;\r
361 \r
362                 /* If the nesting level has reached zero then all interrupt\r
363                 priorities must be re-enabled. */\r
364                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
365                 {\r
366                         /* Critical nesting has reached zero so all interrupt priorities\r
367                         should be unmasked. */\r
368                         portCLEAR_INTERRUPT_MASK();\r
369                 }\r
370         }\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 void FreeRTOS_Tick_Handler( void )\r
375 {\r
376         /* Set interrupt mask before altering scheduler structures.   The tick\r
377         handler runs at the lowest priority, so interrupts cannot already be masked,\r
378         so there is no need to save and restore the current mask value. */\r
379         __disable_irq();\r
380         portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
381         __asm(  "DSB            \n"\r
382                         "ISB            \n" );\r
383         __enable_irq();\r
384 \r
385         /* Increment the RTOS tick. */\r
386         if( xTaskIncrementTick() != pdFALSE )\r
387         {\r
388                 ulPortYieldRequired = pdTRUE;\r
389         }\r
390 \r
391         /* Ensure all interrupt priorities are active again. */\r
392         portCLEAR_INTERRUPT_MASK();\r
393         configCLEAR_TICK_INTERRUPT();\r
394 }\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 void vPortTaskUsesFPU( void )\r
398 {\r
399 uint32_t ulInitialFPSCR = 0;\r
400 \r
401         /* A task is registering the fact that it needs an FPU context.  Set the\r
402         FPU flag (which is saved as part of the task context). */\r
403         ulPortTaskHasFPUContext = pdTRUE;\r
404 \r
405         /* Initialise the floating point status register. */\r
406         __asm( "FMXR    FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
407 }\r
408 /*-----------------------------------------------------------*/\r
409 \r
410 void vPortClearInterruptMask( uint32_t ulNewMaskValue )\r
411 {\r
412         if( ulNewMaskValue == pdFALSE )\r
413         {\r
414                 portCLEAR_INTERRUPT_MASK();\r
415         }\r
416 }\r
417 /*-----------------------------------------------------------*/\r
418 \r
419 uint32_t ulPortSetInterruptMask( void )\r
420 {\r
421 uint32_t ulReturn;\r
422 \r
423         __disable_irq();\r
424         if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )\r
425         {\r
426                 /* Interrupts were already masked. */\r
427                 ulReturn = pdTRUE;\r
428         }\r
429         else\r
430         {\r
431                 ulReturn = pdFALSE;\r
432                 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
433                 __asm(  "DSB            \n"\r
434                                 "ISB            \n" );\r
435         }\r
436         __enable_irq();\r
437 \r
438         return ulReturn;\r
439 }\r
440 /*-----------------------------------------------------------*/\r
441 \r
442 #if( configASSERT_DEFINED == 1 )\r
443 \r
444         void vPortValidateInterruptPriority( void )\r
445         {\r
446                 /* The following assertion will fail if a service routine (ISR) for\r
447                 an interrupt that has been assigned a priority above\r
448                 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
449                 function.  ISR safe FreeRTOS API functions must *only* be called\r
450                 from interrupts that have been assigned a priority at or below\r
451                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
452 \r
453                 Numerically low interrupt priority numbers represent logically high\r
454                 interrupt priorities, therefore the priority of the interrupt must\r
455                 be set to a value equal to or numerically *higher* than\r
456                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
457 \r
458                 FreeRTOS maintains separate thread and ISR API functions to ensure\r
459                 interrupt entry is as fast and simple as possible.\r
460 \r
461                 The following links provide detailed information:\r
462                 http://www.freertos.org/RTOS-Cortex-M3-M4.html\r
463                 http://www.freertos.org/FAQHelp.html */\r
464                 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );\r
465 \r
466                 /* Priority grouping:  The interrupt controller (GIC) allows the bits\r
467                 that define each interrupt's priority to be split between bits that\r
468                 define the interrupt's pre-emption priority bits and bits that define\r
469                 the interrupt's sub-priority.  For simplicity all bits must be defined\r
470                 to be pre-emption priority bits.  The following assertion will fail if\r
471                 this is not the case (if some bits represent a sub-priority).\r
472 \r
473                 The priority grouping is configured by the GIC's binary point register\r
474                 (ICCBPR).  Writting 0 to ICCBPR will ensure it is set to its lowest\r
475                 possible value (which may be above 0). */\r
476                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
477         }\r
478 \r
479 #endif /* configASSERT_DEFINED */\r
480 \r
481 \r