]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM_CA9/port.c
RVDS ARM Cortex-A port layer.
[freertos] / FreeRTOS / Source / portable / RVDS / 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 /* Scheduler includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 \r
82 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS\r
83         #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
84 #endif\r
85 \r
86 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET\r
87         #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
88 #endif\r
89 \r
90 #ifndef configUNIQUE_INTERRUPT_PRIORITIES\r
91         #error configUNIQUE_INTERRUPT_PRIORITIES must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
92 #endif\r
93 \r
94 #ifndef configSETUP_TICK_INTERRUPT\r
95         #error configSETUP_TICK_INTERRUPT() must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
96 #endif /* configSETUP_TICK_INTERRUPT */\r
97 \r
98 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY\r
99         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
100 #endif\r
101 \r
102 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
103         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
104 #endif\r
105 \r
106 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES\r
107         #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
108 #endif\r
109 \r
110 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
111         /* Check the configuration. */\r
112         #if( configMAX_PRIORITIES > 32 )\r
113                 #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
114         #endif\r
115 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
116 \r
117 /* In case security extensions are implemented. */\r
118 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
119         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
120 #endif\r
121 \r
122 /* The number of bits to shift for an interrupt priority is dependent on the\r
123 number of bits implemented by the interrupt controller. */\r
124 #if configUNIQUE_INTERRUPT_PRIORITIES == 16\r
125         #define portPRIORITY_SHIFT 4\r
126 #elif configUNIQUE_INTERRUPT_PRIORITIES == 32\r
127         #define portPRIORITY_SHIFT 3\r
128 #elif configUNIQUE_INTERRUPT_PRIORITIES == 64\r
129         #define portPRIORITY_SHIFT 2\r
130 #elif configUNIQUE_INTERRUPT_PRIORITIES == 128\r
131         #define portPRIORITY_SHIFT 1\r
132 #elif configUNIQUE_INTERRUPT_PRIORITIES == 256\r
133         #define portPRIORITY_SHIFT 0\r
134 #else\r
135         #error Invalid configUNIQUE_INTERRUPT_PRIORITIES setting.  configUNIQUE_INTERRUPT_PRIORITIES must be set to the number of unique priorities implemented by the target hardware\r
136 #endif\r
137 \r
138 /* A critical section is exited when the critical section nesting count reaches\r
139 this value. */\r
140 #define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )\r
141 \r
142 /* In all GICs 255 can be written to the priority mask register to unmask all\r
143 (but the lowest) interrupt priority. */\r
144 #define portUNMASK_VALUE                                ( 0xFF )\r
145 \r
146 /* Tasks are not created with a floating point context, but can be given a\r
147 floating point context after they have been created.  A variable is stored as\r
148 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
149 does not have an FPU context, or any other value if the task does have an FPU\r
150 context. */\r
151 #define portNO_FLOATING_POINT_CONTEXT   ( ( portSTACK_TYPE ) 0 )\r
152 \r
153 /* Interrupt controller access addresses. */\r
154 #define portICCPMR_PRIORITY_MASK_OFFSET  ( 0x04 )\r
155 #define portICCIAR_INTERRUPT_ACKNOWLEDGE_OFFSET ( 0x0C )\r
156 #define portICCEOIR_END_OF_INTERRUPT_OFFSET ( 0x10 )\r
157 #define portINTERRUPT_CONTROLLER_CPU_INTERFACE_ADDRESS ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET )\r
158 #define portICCPMR_PRIORITY_MASK_REGISTER ( *( ( volatile unsigned long * ) ( portINTERRUPT_CONTROLLER_CPU_INTERFACE_ADDRESS + portICCPMR_PRIORITY_MASK_OFFSET ) ) )\r
159 #define portICCIAR_INTERRUPT_ACKNOWLEDGE_REGISTER_ADDRESS ( portINTERRUPT_CONTROLLER_CPU_INTERFACE_ADDRESS + portICCIAR_INTERRUPT_ACKNOWLEDGE_OFFSET )\r
160 #define portICCEOIR_END_OF_INTERRUPT_REGISTER_ADDRESS ( portINTERRUPT_CONTROLLER_CPU_INTERFACE_ADDRESS + portICCEOIR_END_OF_INTERRUPT_OFFSET )\r
161 #define portICCPMR_PRIORITY_MASK_REGISTER_ADDRESS ( portINTERRUPT_CONTROLLER_CPU_INTERFACE_ADDRESS + portICCPMR_PRIORITY_MASK_OFFSET )\r
162 \r
163 /* Constants required to setup the initial task context. */\r
164 #define portINITIAL_SPSR                                ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
165 #define portTHUMB_MODE_BIT                              ( ( portSTACK_TYPE ) 0x20 )\r
166 #define portINTERRUPT_ENABLE_BIT                ( 0x80UL )\r
167 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
168 \r
169 /* Masks all bits in the APSR other than the mode bits. */\r
170 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
171 \r
172 /* The value of the mode bits in the APSR when the CPU is executing in user\r
173 mode. */\r
174 #define portAPSR_USER_MODE                              ( 0x10 )\r
175 \r
176 /* Macro to unmask all interrupt priorities. */\r
177 #define portCLEAR_INTERRUPT_MASK()                                                                                      \\r
178 {                                                                                                                                                       \\r
179         __disable_irq();                                                                                                                \\r
180         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                                   \\r
181         __asm(  "DSB            \n"                                                                                                     \\r
182                         "ISB            \n" );                                                                                          \\r
183         __enable_irq();                                                                                                                 \\r
184 }\r
185 \r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /*\r
189  * Starts the first task executing.  This function is necessarily written in\r
190  * assembly code so is implemented in portASM.s.\r
191  */\r
192 extern void vPortRestoreTaskContext( void );\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /* A variable is used to keep track of the critical section nesting.  This\r
197 variable has to be stored as part of the task context and must be initialised to\r
198 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
199 the scheduler starts.  As it is stored as part of the task context it will\r
200 automatically be set to 0 when the first task is started. */\r
201 volatile unsigned long ulCriticalNesting = 9999UL;\r
202 \r
203 /* The value to be written to the interrupt controllers priority mask register\r
204 to mask interrupts that can use the FreeRTOS API without masking higher priority\r
205 interrupts. */\r
206 const unsigned long ulPortAPIPriorityMask = ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
207 \r
208 /* Used to pass constants into the ASM code.  The address at which variables are\r
209 placed is the constant value so indirect loads in the asm code are not\r
210 required. */\r
211 unsigned long ulICCIAR __attribute__( ( at( portICCIAR_INTERRUPT_ACKNOWLEDGE_REGISTER_ADDRESS ) ) );\r
212 unsigned long ulICCEOIR __attribute__( ( at( portICCEOIR_END_OF_INTERRUPT_REGISTER_ADDRESS ) ) );\r
213 unsigned long ulICCPMR __attribute__( ( at( portICCPMR_PRIORITY_MASK_REGISTER_ADDRESS ) ) );\r
214 unsigned long ulAsmAPIPriorityMask __attribute__( ( at( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) ) );\r
215 \r
216 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero then\r
217 a floating point context must be saved and restored for the task. */\r
218 unsigned long ulPortTaskHasFPUContext = pdFALSE;\r
219 \r
220 /* Set to 1 to pend a context switch from an ISR. */\r
221 unsigned long ulPortYieldRequired = pdFALSE;\r
222 \r
223 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
224 if the nesting depth is 0. */\r
225 unsigned long ulPortInterruptNesting = 0UL;\r
226 \r
227 /*-----------------------------------------------------------*/\r
228 \r
229 /*\r
230  * See header file for description.\r
231  */\r
232 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
233 {\r
234         /* Setup the initial stack of the task.  The stack is set exactly as\r
235         expected by the portRESTORE_CONTEXT() macro.\r
236 \r
237         The fist real value on the stack is the status register, which is set for\r
238         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
239         GDB does not try decoding a non-existent return address. */\r
240         *pxTopOfStack = NULL;\r
241         pxTopOfStack--;\r
242         *pxTopOfStack = NULL;\r
243         pxTopOfStack--;\r
244         *pxTopOfStack = NULL;\r
245         pxTopOfStack--;\r
246         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
247 \r
248         if( ( ( unsigned long ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
249         {\r
250                 /* The task will start in THUMB mode. */\r
251                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
252         }\r
253 \r
254         pxTopOfStack--;\r
255 \r
256         /* Next the return address, which in this case is the start of the task. */\r
257         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;\r
258         pxTopOfStack--;\r
259 \r
260         /* Next all the registers other than the stack pointer. */\r
261         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa;  /* R14 */\r
262         pxTopOfStack--;\r
263         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
264         pxTopOfStack--;\r
265         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
266         pxTopOfStack--;\r
267         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
268         pxTopOfStack--;\r
269         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
270         pxTopOfStack--;\r
271         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
272         pxTopOfStack--;\r
273         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
274         pxTopOfStack--;\r
275         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
276         pxTopOfStack--;\r
277         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
278         pxTopOfStack--;\r
279         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
280         pxTopOfStack--;\r
281         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
282         pxTopOfStack--;\r
283         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
284         pxTopOfStack--;\r
285         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
286         pxTopOfStack--;\r
287         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
288         pxTopOfStack--;\r
289 \r
290         /* The task will start with a critical nesting count of 0 as interrupts are\r
291         enabled. */\r
292         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
293         pxTopOfStack--;\r
294 \r
295         /* The task will start without a floating point context.  A task that uses\r
296         the floating point hardware must call vPortTaskUsesFPU() before executing\r
297         any floating point instructions. */\r
298         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
299 \r
300         return pxTopOfStack;\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 portBASE_TYPE xPortStartScheduler( void )\r
305 {\r
306 unsigned long ulAPSR;\r
307 \r
308         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
309         Privileged mode for the scheduler to start. */\r
310         __asm( "MRS ulAPSR, APSR" );\r
311         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
312         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
313 \r
314         if( ulAPSR != portAPSR_USER_MODE )\r
315         {\r
316                 /* Start the timer that generates the tick ISR. */\r
317                 configSETUP_TICK_INTERRUPT();\r
318 \r
319                 __enable_irq();\r
320                 vPortRestoreTaskContext();\r
321         }\r
322 \r
323         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
324         a non-privileged mode. */\r
325         return 0;\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 void vPortEndScheduler( void )\r
330 {\r
331         /* It is unlikely that the ARM port will require this function as there\r
332         is nothing to return to. */\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 void vPortEnterCritical( void )\r
337 {\r
338         /* Disable interrupts as per portDISABLE_INTERRUPTS();  */\r
339         ulPortSetInterruptMask();\r
340 \r
341         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
342         directly.  Increment ulCriticalNesting to keep a count of how many times\r
343         portENTER_CRITICAL() has been called. */\r
344         ulCriticalNesting++;\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 void vPortExitCritical( void )\r
349 {\r
350         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
351         {\r
352                 /* Decrement the nesting count as the critical section is being\r
353                 exited. */\r
354                 ulCriticalNesting--;\r
355 \r
356                 /* If the nesting level has reached zero then all interrupt\r
357                 priorities must be re-enabled. */\r
358                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
359                 {\r
360                         /* Critical nesting has reached zero so all interrupt priorities\r
361                         should be unmasked. */\r
362                         portCLEAR_INTERRUPT_MASK();\r
363                 }\r
364         }\r
365 }\r
366 /*-----------------------------------------------------------*/\r
367 \r
368 void FreeRTOS_Tick_Handler( void )\r
369 {\r
370         /* Set interrupt mask before altering scheduler structures.   The tick\r
371         handler runs at the lowest priority, so interrupts cannot already be masked,\r
372         so there is no need to save and restore the current mask value. */\r
373         __disable_irq();\r
374         portICCPMR_PRIORITY_MASK_REGISTER = ulPortAPIPriorityMask;\r
375         __asm(  "DSB            \n"\r
376                         "ISB            \n" );\r
377         __enable_irq();\r
378 \r
379         /* Increment the RTOS tick. */\r
380         if( xTaskIncrementTick() != pdFALSE )\r
381         {\r
382                 ulPortYieldRequired = pdTRUE;\r
383         }\r
384 \r
385         /* Ensure all interrupt priorities are active again. */\r
386         portCLEAR_INTERRUPT_MASK();\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 void vPortTaskUsesFPU( void )\r
391 {\r
392 unsigned long ulInitialFPSCR = 0;\r
393 \r
394         /* A task is registering the fact that it needs an FPU context.  Set the\r
395         FPU flag (which is saved as part of the task context). */\r
396         ulPortTaskHasFPUContext = pdTRUE;\r
397 \r
398         /* Initialise the floating point status register. */\r
399         __asm( "FMXR    FPSCR, ulInitialFPSCR" );\r
400 }\r
401 /*-----------------------------------------------------------*/\r
402 \r
403 void vPortClearInterruptMask( unsigned long ulNewMaskValue )\r
404 {\r
405         if( ulNewMaskValue == pdFALSE )\r
406         {\r
407                 portCLEAR_INTERRUPT_MASK();\r
408         }\r
409 }\r
410 /*-----------------------------------------------------------*/\r
411 \r
412 unsigned long ulPortSetInterruptMask( void )\r
413 {\r
414 unsigned long ulReturn;\r
415 \r
416         __disable_irq();\r
417         if( portICCPMR_PRIORITY_MASK_REGISTER == ulPortAPIPriorityMask )\r
418         {\r
419                 /* Interrupts were already masked. */\r
420                 ulReturn = pdTRUE;\r
421         }\r
422         else\r
423         {\r
424                 ulReturn = pdFALSE;\r
425                 portICCPMR_PRIORITY_MASK_REGISTER = ulPortAPIPriorityMask;\r
426                 __asm(  "DSB            \n"\r
427                                 "ISB            \n" );\r
428         }\r
429         __enable_irq();\r
430 \r
431         return ulReturn;\r
432 }\r
433 \r
434 \r