]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM_CRx_No_GIC/port.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Source / portable / GCC / ARM_CRx_No_GIC / 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 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 \r
77 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
78         /* Check the configuration. */\r
79         #if( configMAX_PRIORITIES > 32 )\r
80                 #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
81         #endif\r
82 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
83 \r
84 #ifndef configSETUP_TICK_INTERRUPT\r
85         #error configSETUP_TICK_INTERRUPT() must be defined in FreeRTOSConfig.h to call the function that sets up the tick interrupt.\r
86 #endif\r
87 \r
88 #ifndef configCLEAR_TICK_INTERRUPT\r
89         #error configCLEAR_TICK_INTERRUPT must be defined in FreeRTOSConfig.h to clear which ever interrupt was used to generate the tick interrupt.\r
90 #endif\r
91 \r
92 /* A critical section is exited when the critical section nesting count reaches\r
93 this value. */\r
94 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
95 \r
96 /* Tasks are not created with a floating point context, but can be given a\r
97 floating point context after they have been created.  A variable is stored as\r
98 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
99 does not have an FPU context, or any other value if the task does have an FPU\r
100 context. */\r
101 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
102 \r
103 /* Constants required to setup the initial task context. */\r
104 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, IRQ enabled FIQ enabled. */\r
105 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
106 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
107 \r
108 /* Masks all bits in the APSR other than the mode bits. */\r
109 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
110 \r
111 /* The value of the mode bits in the APSR when the CPU is executing in user\r
112 mode. */\r
113 #define portAPSR_USER_MODE                              ( 0x10 )\r
114 \r
115 /* Let the user override the pre-loading of the initial LR with the address of\r
116 prvTaskExitError() in case it messes up unwinding of the stack in the\r
117 debugger. */\r
118 #ifdef configTASK_RETURN_ADDRESS\r
119         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
120 #else\r
121         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
122 #endif\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /*\r
127  * Starts the first task executing.  This function is necessarily written in\r
128  * assembly code so is implemented in portASM.s.\r
129  */\r
130 extern void vPortRestoreTaskContext( void );\r
131 \r
132 /*\r
133  * Used to catch tasks that attempt to return from their implementing function.\r
134  */\r
135 static void prvTaskExitError( void );\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* A variable is used to keep track of the critical section nesting.  This\r
140 variable has to be stored as part of the task context and must be initialised to\r
141 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
142 the scheduler starts.  As it is stored as part of the task context it will\r
143 automatically be set to 0 when the first task is started. */\r
144 volatile uint32_t ulCriticalNesting = 9999UL;\r
145 \r
146 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero then\r
147 a floating point context must be saved and restored for the task. */\r
148 volatile uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
149 \r
150 /* Set to 1 to pend a context switch from an ISR. */\r
151 volatile uint32_t ulPortYieldRequired = pdFALSE;\r
152 \r
153 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
154 if the nesting depth is 0. */\r
155 volatile uint32_t ulPortInterruptNesting = 0UL;\r
156 \r
157 /* Used in the asm file to clear an interrupt. */\r
158 __attribute__(( used )) const uint32_t ulICCEOIR = configEOI_ADDRESS;\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /*\r
163  * See header file for description.\r
164  */\r
165 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
166 {\r
167         /* Setup the initial stack of the task.  The stack is set exactly as\r
168         expected by the portRESTORE_CONTEXT() macro.\r
169 \r
170         The fist real value on the stack is the status register, which is set for\r
171         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
172         GDB does not try decoding a non-existent return address. */\r
173         *pxTopOfStack = ( StackType_t ) NULL;\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( StackType_t ) NULL;\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = ( StackType_t ) NULL;\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
180 \r
181         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
182         {\r
183                 /* The task will start in THUMB mode. */\r
184                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
185         }\r
186 \r
187         pxTopOfStack--;\r
188 \r
189         /* Next the return address, which in this case is the start of the task. */\r
190         *pxTopOfStack = ( StackType_t ) pxCode;\r
191         pxTopOfStack--;\r
192 \r
193         /* Next all the registers other than the stack pointer. */\r
194         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;        /* R14 */\r
195         pxTopOfStack--;\r
196         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
197         pxTopOfStack--;\r
198         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
199         pxTopOfStack--;\r
200         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
201         pxTopOfStack--;\r
202         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
203         pxTopOfStack--;\r
204         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
205         pxTopOfStack--;\r
206         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
207         pxTopOfStack--;\r
208         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
209         pxTopOfStack--;\r
210         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
211         pxTopOfStack--;\r
212         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
213         pxTopOfStack--;\r
214         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
215         pxTopOfStack--;\r
216         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
217         pxTopOfStack--;\r
218         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
219         pxTopOfStack--;\r
220         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
221         pxTopOfStack--;\r
222 \r
223         /* The task will start with a critical nesting count of 0 as interrupts are\r
224         enabled. */\r
225         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
226         pxTopOfStack--;\r
227 \r
228         /* The task will start without a floating point context.  A task that uses\r
229         the floating point hardware must call vPortTaskUsesFPU() before executing\r
230         any floating point instructions. */\r
231         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
232 \r
233         return pxTopOfStack;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvTaskExitError( void )\r
238 {\r
239         /* A function that implements a task must not exit or attempt to return to\r
240         its caller as there is nothing to return to.  If a task wants to exit it\r
241         should instead call vTaskDelete( NULL ).\r
242 \r
243         Artificially force an assert() to be triggered if configASSERT() is\r
244         defined, then stop here so application writers can catch the error. */\r
245         configASSERT( ulPortInterruptNesting == ~0UL );\r
246         portDISABLE_INTERRUPTS();\r
247         for( ;; );\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 BaseType_t xPortStartScheduler( void )\r
252 {\r
253 uint32_t ulAPSR;\r
254 \r
255         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
256         Privileged mode for the scheduler to start. */\r
257         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
258         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
259         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
260 \r
261         if( ulAPSR != portAPSR_USER_MODE )\r
262         {\r
263                 /* Start the timer that generates the tick ISR. */\r
264                 portDISABLE_INTERRUPTS();\r
265                 configSETUP_TICK_INTERRUPT();\r
266 \r
267                 /* Start the first task executing. */\r
268                 vPortRestoreTaskContext();\r
269         }\r
270 \r
271         /* Will only get here if xTaskStartScheduler() was called with the CPU in\r
272         a non-privileged mode or the binary point register was not set to its lowest\r
273         possible value.  prvTaskExitError() is referenced to prevent a compiler\r
274         warning about it being defined but not referenced in the case that the user\r
275         defines their own exit address. */\r
276         ( void ) prvTaskExitError;\r
277         return 0;\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 void vPortEndScheduler( void )\r
282 {\r
283         /* Not implemented in ports where there is nothing to return to.\r
284         Artificially force an assert. */\r
285         configASSERT( ulCriticalNesting == 1000UL );\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 void vPortEnterCritical( void )\r
290 {\r
291         portDISABLE_INTERRUPTS();\r
292 \r
293         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
294         directly.  Increment ulCriticalNesting to keep a count of how many times\r
295         portENTER_CRITICAL() has been called. */\r
296         ulCriticalNesting++;\r
297 \r
298         /* This is not the interrupt safe version of the enter critical function so\r
299         assert() if it is being called from an interrupt context.  Only API\r
300         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
301         the critical nesting count is 1 to protect against recursive calls if the\r
302         assert function also uses a critical section. */\r
303         if( ulCriticalNesting == 1 )\r
304         {\r
305                 configASSERT( ulPortInterruptNesting == 0 );\r
306         }\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 void vPortExitCritical( void )\r
311 {\r
312         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
313         {\r
314                 /* Decrement the nesting count as the critical section is being\r
315                 exited. */\r
316                 ulCriticalNesting--;\r
317 \r
318                 /* If the nesting level has reached zero then all interrupt\r
319                 priorities must be re-enabled. */\r
320                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
321                 {\r
322                         /* Critical nesting has reached zero so all interrupt priorities\r
323                         should be unmasked. */\r
324                         portENABLE_INTERRUPTS();\r
325                 }\r
326         }\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 void FreeRTOS_Tick_Handler( void )\r
331 {\r
332 uint32_t ulInterruptStatus;\r
333 \r
334         ulInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
335 \r
336         /* Increment the RTOS tick. */\r
337         if( xTaskIncrementTick() != pdFALSE )\r
338         {\r
339                 ulPortYieldRequired = pdTRUE;\r
340         }\r
341 \r
342         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulInterruptStatus );\r
343 \r
344         configCLEAR_TICK_INTERRUPT();\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 void vPortTaskUsesFPU( void )\r
349 {\r
350 uint32_t ulInitialFPSCR = 0;\r
351 \r
352         /* A task is registering the fact that it needs an FPU context.  Set the\r
353         FPU flag (which is saved as part of the task context). */\r
354         ulPortTaskHasFPUContext = pdTRUE;\r
355 \r
356         /* Initialise the floating point status register. */\r
357         __asm volatile ( "FMXR  FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 \r