]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CA5_No_GIC/port.c
Continue working on the GIC-less Cortex-A5 port for IAR:
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CA5_No_GIC / port.c
1 /*\r
2     FreeRTOS V8.0.1 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdlib.h>\r
68 \r
69 /* IAR includes. */\r
70 #include <intrinsics.h>\r
71 \r
72 /* Scheduler includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
77         /* Check the configuration. */\r
78         #if( configMAX_PRIORITIES > 32 )\r
79                 #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
80         #endif\r
81 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
82 \r
83 /* A critical section is exited when the critical section nesting count reaches\r
84 this value. */\r
85 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
86 \r
87 /* Tasks are not created with a floating point context, but can be given a\r
88 floating point context after they have been created.  A variable is stored as\r
89 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
90 does not have an FPU context, or any other value if the task does have an FPU\r
91 context. */\r
92 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
93 \r
94 /* Constants required to setup the initial task context. */\r
95 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
96 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
97 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
98 \r
99 /* Masks all bits in the APSR other than the mode bits. */\r
100 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
101 \r
102 /* The value of the mode bits in the APSR when the CPU is executing in user\r
103 mode. */\r
104 #define portAPSR_USER_MODE                              ( 0x10 )\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * Starts the first task executing.  This function is necessarily written in\r
110  * assembly code so is implemented in portASM.s.\r
111  */\r
112 extern void vPortRestoreTaskContext( void );\r
113 \r
114 /*\r
115  * Used to catch tasks that attempt to return from their implementing function.\r
116  */\r
117 static void prvTaskExitError( void );\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /* A variable is used to keep track of the critical section nesting.  This\r
122 variable has to be stored as part of the task context and must be initialised to\r
123 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
124 the scheduler starts.  As it is stored as part of the task context it will\r
125 automatically be set to 0 when the first task is started. */\r
126 volatile uint32_t ulCriticalNesting = 9999UL;\r
127 \r
128 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero\r
129 then a floating point context must be saved and restored for the task. */\r
130 uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
131 \r
132 /* Set to 1 to pend a context switch from an ISR. */\r
133 uint32_t ulPortYieldRequired = pdFALSE;\r
134 \r
135 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
136 if the nesting depth is 0. */\r
137 uint32_t ulPortInterruptNesting = 0UL;\r
138 \r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 #warning What about branch distance in asm file.\r
143 #warning Does not support flop use in ISRs.\r
144 #warning Level interrupts must be cleared in their handling function.\r
145 \r
146 /*\r
147  * See header file for description.\r
148  */\r
149 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
150 {\r
151         /* Setup the initial stack of the task.  The stack is set exactly as\r
152         expected by the portRESTORE_CONTEXT() macro.\r
153 \r
154         The fist real value on the stack is the status register, which is set for\r
155         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
156         GDB does not try decoding a non-existent return address. */\r
157         *pxTopOfStack = NULL;\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = NULL;\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = NULL;\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
164 \r
165         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
166         {\r
167                 /* The task will start in THUMB mode. */\r
168                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
169         }\r
170 \r
171         pxTopOfStack--;\r
172 \r
173         /* Next the return address, which in this case is the start of the task. */\r
174         *pxTopOfStack = ( StackType_t ) pxCode;\r
175         pxTopOfStack--;\r
176 \r
177         /* Next all the registers other than the stack pointer. */\r
178         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* R14 */\r
179         pxTopOfStack--;\r
180         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
181         pxTopOfStack--;\r
182         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
183         pxTopOfStack--;\r
184         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
185         pxTopOfStack--;\r
186         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
189         pxTopOfStack--;\r
190         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
191         pxTopOfStack--;\r
192         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
193         pxTopOfStack--;\r
194         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
195         pxTopOfStack--;\r
196         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
197         pxTopOfStack--;\r
198         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
199         pxTopOfStack--;\r
200         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
201         pxTopOfStack--;\r
202         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
203         pxTopOfStack--;\r
204         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
205         pxTopOfStack--;\r
206 \r
207         /* The task will start with a critical nesting count of 0 as interrupts are\r
208         enabled. */\r
209         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
210         pxTopOfStack--;\r
211 \r
212         /* The task will start without a floating point context.  A task that uses\r
213         the floating point hardware must call vPortTaskUsesFPU() before executing\r
214         any floating point instructions. */\r
215         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
216 \r
217         return pxTopOfStack;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvTaskExitError( void )\r
222 {\r
223         /* A function that implements a task must not exit or attempt to return to\r
224         its caller as there is nothing to return to.  If a task wants to exit it\r
225         should instead call vTaskDelete( NULL ).\r
226 \r
227         Artificially force an assert() to be triggered if configASSERT() is\r
228         defined, then stop here so application writers can catch the error. */\r
229         configASSERT( ulPortInterruptNesting == ~0UL );\r
230         portDISABLE_INTERRUPTS();\r
231         for( ;; );\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 BaseType_t xPortStartScheduler( void )\r
236 {\r
237 uint32_t ulAPSR;\r
238 \r
239         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
240         Privileged mode for the scheduler to start. */\r
241         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
242         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
243         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
244 \r
245         if( ulAPSR != portAPSR_USER_MODE )\r
246         {\r
247                 /* Start the timer that generates the tick ISR. */\r
248                 configSETUP_TICK_INTERRUPT();\r
249                 __enable_irq();\r
250                 vPortRestoreTaskContext();\r
251         }\r
252 \r
253         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
254         a non-privileged mode or the binary point register was not set to its lowest\r
255         possible value. */\r
256         return 0;\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 void vPortEndScheduler( void )\r
261 {\r
262         /* Not implemented in ports where there is nothing to return to.\r
263         Artificially force an assert. */\r
264         configASSERT( ulCriticalNesting == 1000UL );\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 void vPortEnterCritical( void )\r
269 {\r
270         portDISABLE_INTERRUPTS();\r
271 \r
272         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
273         directly.  Increment ulCriticalNesting to keep a count of how many times\r
274         portENTER_CRITICAL() has been called. */\r
275         ulCriticalNesting++;\r
276 \r
277         /* This is not the interrupt safe version of the enter critical function so\r
278         assert() if it is being called from an interrupt context.  Only API\r
279         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
280         the critical nesting count is 1 to protect against recursive calls if the\r
281         assert function also uses a critical section. */\r
282         if( ulCriticalNesting == 1 )\r
283         {\r
284                 configASSERT( ulPortInterruptNesting == 0 );\r
285         }\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 void vPortExitCritical( void )\r
290 {\r
291         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
292         {\r
293                 /* Decrement the nesting count as the critical section is being\r
294                 exited. */\r
295                 ulCriticalNesting--;\r
296 \r
297                 /* If the nesting level has reached zero then all interrupt\r
298                 priorities must be re-enabled. */\r
299                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
300                 {\r
301                         /* Critical nesting has reached zero so all interrupt priorities\r
302                         should be unmasked. */\r
303                         portENABLE_INTERRUPTS();\r
304                 }\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 void FreeRTOS_Tick_Handler( void )\r
310 {\r
311         portDISABLE_INTERRUPTS();\r
312 \r
313         /* Increment the RTOS tick. */\r
314         if( xTaskIncrementTick() != pdFALSE )\r
315         {\r
316                 ulPortYieldRequired = pdTRUE;\r
317         }\r
318 \r
319         portENABLE_INTERRUPTS();\r
320         configCLEAR_TICK_INTERRUPT();\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 void vPortTaskUsesFPU( void )\r
325 {\r
326 uint32_t ulInitialFPSCR = 0;\r
327 \r
328         /* A task is registering the fact that it needs an FPU context.  Set the\r
329         FPU flag (which is saved as part of the task context). */\r
330         ulPortTaskHasFPUContext = pdTRUE;\r
331 \r
332         /* Initialise the floating point status register. */\r
333         __asm( "FMXR    FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 \r
338 \r