]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CA9/port.c
IAR ARM Cortex-A port layer.
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CA9 / port.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
32 \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
36     kernel.\r
37 \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
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \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
65 \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
69 \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
73 */\r
74 \r
75 /* Standard includes. */\r
76 #include <stdlib.h>\r
77 \r
78 /* IAR includes. */\r
79 #include <intrinsics.h>\r
80 \r
81 /* Scheduler includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 \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
87 #endif\r
88 \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
91 #endif\r
92 \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
95 #endif\r
96 \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
100 \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
103 #endif\r
104 \r
105 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
106         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
107 #endif\r
108 \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
111 #endif\r
112 \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
117         #endif\r
118 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
119 \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
123 #endif\r
124 \r
125 /* A critical section is exited when the critical section nesting count reaches\r
126 this value. */\r
127 #define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )\r
128 \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
132 \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
137 context. */\r
138 #define portNO_FLOATING_POINT_CONTEXT   ( ( portSTACK_TYPE ) 0 )\r
139 \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
145 \r
146 /* Masks all bits in the APSR other than the mode bits. */\r
147 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
148 \r
149 /* The value of the mode bits in the APSR when the CPU is executing in user\r
150 mode. */\r
151 #define portAPSR_USER_MODE                              ( 0x10 )\r
152 \r
153 /* Macro to unmask all interrupt priorities. */\r
154 #define portCLEAR_INTERRUPT_MASK()                                                                                      \\r
155 {                                                                                                                                                       \\r
156         __disable_irq();                                                                                                                \\r
157         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                                   \\r
158         __asm(  "DSB            \n"                                                                                                     \\r
159                         "ISB            \n" );                                                                                          \\r
160         __enable_irq();                                                                                                                 \\r
161 }\r
162 \r
163 /*-----------------------------------------------------------*/\r
164 \r
165 /*\r
166  * Starts the first task executing.  This function is necessarily written in\r
167  * assembly code so is implemented in portASM.s.\r
168  */\r
169 extern void vPortRestoreTaskContext( void );\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \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
179 \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
182 interrupts. */\r
183 const unsigned long ulPortAPIPriorityMask = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
184 \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
188 \r
189 /* Set to 1 to pend a context switch from an ISR. */\r
190 unsigned long ulPortYieldRequired = pdFALSE;\r
191 \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
195 \r
196 /*-----------------------------------------------------------*/\r
197 \r
198 /*\r
199  * See header file for description.\r
200  */\r
201 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
202 {\r
203         /* Setup the initial stack of the task.  The stack is set exactly as\r
204         expected by the portRESTORE_CONTEXT() macro.\r
205 \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
210         pxTopOfStack--;\r
211         *pxTopOfStack = NULL;\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = NULL;\r
214         pxTopOfStack--;\r
215         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
216 \r
217         if( ( ( unsigned long ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
218         {\r
219                 /* The task will start in THUMB mode. */\r
220                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
221         }\r
222 \r
223         pxTopOfStack--;\r
224 \r
225         /* Next the return address, which in this case is the start of the task. */\r
226         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;\r
227         pxTopOfStack--;\r
228 \r
229         /* Next all the registers other than the stack pointer. */\r
230         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa;  /* R14 */\r
231         pxTopOfStack--;\r
232         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
233         pxTopOfStack--;\r
234         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
235         pxTopOfStack--;\r
236         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
237         pxTopOfStack--;\r
238         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
239         pxTopOfStack--;\r
240         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
241         pxTopOfStack--;\r
242         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
243         pxTopOfStack--;\r
244         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
245         pxTopOfStack--;\r
246         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
247         pxTopOfStack--;\r
248         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
249         pxTopOfStack--;\r
250         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
251         pxTopOfStack--;\r
252         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
253         pxTopOfStack--;\r
254         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
255         pxTopOfStack--;\r
256         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
257         pxTopOfStack--;\r
258 \r
259         /* The task will start with a critical nesting count of 0 as interrupts are\r
260         enabled. */\r
261         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
262         pxTopOfStack--;\r
263 \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
268 \r
269         return pxTopOfStack;\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 portBASE_TYPE xPortStartScheduler( void )\r
274 {\r
275 unsigned long ulAPSR;\r
276 \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
282 \r
283         if( ulAPSR != portAPSR_USER_MODE )\r
284         {\r
285                 /* Start the timer that generates the tick ISR. */\r
286                 configSETUP_TICK_INTERRUPT();\r
287 \r
288                 __enable_irq();\r
289                 vPortRestoreTaskContext();\r
290         }\r
291 \r
292         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
293         a non-privileged mode. */\r
294         return 0;\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 void vPortEndScheduler( void )\r
299 {\r
300         /* It is unlikely that the ARM port will require this function as there\r
301         is nothing to return to. */\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 void vPortEnterCritical( void )\r
306 {\r
307         /* Disable interrupts as per portDISABLE_INTERRUPTS();  */\r
308         ulPortSetInterruptMask();\r
309 \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
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 void vPortExitCritical( void )\r
318 {\r
319         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
320         {\r
321                 /* Decrement the nesting count as the critical section is being\r
322                 exited. */\r
323                 ulCriticalNesting--;\r
324 \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
328                 {\r
329                         /* Critical nesting has reached zero so all interrupt priorities\r
330                         should be unmasked. */\r
331                         portCLEAR_INTERRUPT_MASK();\r
332                 }\r
333         }\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 void FreeRTOS_Tick_Handler( void )\r
338 {\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
342         __disable_irq();\r
343         portICCPMR_PRIORITY_MASK_REGISTER = ulPortAPIPriorityMask;\r
344         __asm(  "DSB            \n"\r
345                         "ISB            \n" );\r
346         __enable_irq();\r
347 \r
348         /* Increment the RTOS tick. */\r
349         if( xTaskIncrementTick() != pdFALSE )\r
350         {\r
351                 ulPortYieldRequired = pdTRUE;\r
352         }\r
353 \r
354         /* Ensure all interrupt priorities are active again. */\r
355         portCLEAR_INTERRUPT_MASK();\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 void vPortTaskUsesFPU( void )\r
360 {\r
361 unsigned long ulInitialFPSCR = 0;\r
362 \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
366 \r
367         /* Initialise the floating point status register. */\r
368         __asm( "FMXR    FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r
372 void vPortClearInterruptMask( unsigned long ulNewMaskValue )\r
373 {\r
374         if( ulNewMaskValue == pdFALSE )\r
375         {\r
376                 portCLEAR_INTERRUPT_MASK();\r
377         }\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r
381 unsigned long ulPortSetInterruptMask( void )\r
382 {\r
383 unsigned long ulReturn;\r
384 \r
385         __disable_irq();\r
386         if( portICCPMR_PRIORITY_MASK_REGISTER == ulPortAPIPriorityMask )\r
387         {\r
388                 /* Interrupts were already masked. */\r
389                 ulReturn = pdTRUE;\r
390         }\r
391         else\r
392         {\r
393                 ulReturn = pdFALSE;\r
394                 portICCPMR_PRIORITY_MASK_REGISTER = ulPortAPIPriorityMask;\r
395                 __asm(  "DSB            \n"\r
396                                 "ISB            \n" );\r
397         }\r
398         __enable_irq();\r
399 \r
400         return ulReturn;\r
401 }\r
402 \r
403 \r