]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CRx_No_GIC/port.c
Fix some build issues in older kernel demo projects.
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CRx_No_GIC / port.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <stdlib.h>\r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 \r
35 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
36         /* Check the configuration. */\r
37         #if( configMAX_PRIORITIES > 32 )\r
38                 #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
39         #endif\r
40 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
41 \r
42 #ifndef configSETUP_TICK_INTERRUPT\r
43         #error configSETUP_TICK_INTERRUPT() must be defined in FreeRTOSConfig.h to call the function that sets up the tick interrupt.\r
44 #endif\r
45 \r
46 #ifndef configCLEAR_TICK_INTERRUPT\r
47         #error configCLEAR_TICK_INTERRUPT must be defined in FreeRTOSConfig.h to clear which ever interrupt was used to generate the tick interrupt.\r
48 #endif\r
49 \r
50 /* A critical section is exited when the critical section nesting count reaches\r
51 this value. */\r
52 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
53 \r
54 /* Tasks are not created with a floating point context, but can be given a\r
55 floating point context after they have been created.  A variable is stored as\r
56 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
57 does not have an FPU context, or any other value if the task does have an FPU\r
58 context. */\r
59 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
60 \r
61 /* Constants required to setup the initial task context. */\r
62 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, IRQ enabled FIQ enabled. */\r
63 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
64 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
65 \r
66 /* Masks all bits in the APSR other than the mode bits. */\r
67 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
68 \r
69 /* The value of the mode bits in the APSR when the CPU is executing in user\r
70 mode. */\r
71 #define portAPSR_USER_MODE                              ( 0x10 )\r
72 \r
73 /* Let the user override the pre-loading of the initial LR with the address of\r
74 prvTaskExitError() in case it messes up unwinding of the stack in the\r
75 debugger. */\r
76 #ifdef configTASK_RETURN_ADDRESS\r
77         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
78 #else\r
79         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
80 #endif\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /*\r
85  * Starts the first task executing.  This function is necessarily written in\r
86  * assembly code so is implemented in portASM.s.\r
87  */\r
88 extern void vPortRestoreTaskContext( void );\r
89 \r
90 /*\r
91  * Used to catch tasks that attempt to return from their implementing function.\r
92  */\r
93 static void prvTaskExitError( void );\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* A variable is used to keep track of the critical section nesting.  This\r
98 variable has to be stored as part of the task context and must be initialised to\r
99 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
100 the scheduler starts.  As it is stored as part of the task context it will\r
101 automatically be set to 0 when the first task is started. */\r
102 volatile uint32_t ulCriticalNesting = 9999UL;\r
103 \r
104 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero then\r
105 a floating point context must be saved and restored for the task. */\r
106 volatile uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
107 \r
108 /* Set to 1 to pend a context switch from an ISR. */\r
109 volatile uint32_t ulPortYieldRequired = pdFALSE;\r
110 \r
111 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
112 if the nesting depth is 0. */\r
113 volatile uint32_t ulPortInterruptNesting = 0UL;\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * See header file for description.\r
119  */\r
120 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
121 {\r
122         /* Setup the initial stack of the task.  The stack is set exactly as\r
123         expected by the portRESTORE_CONTEXT() macro.\r
124 \r
125         The fist real value on the stack is the status register, which is set for\r
126         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
127         GDB does not try decoding a non-existent return address. */\r
128         *pxTopOfStack = ( StackType_t ) NULL;\r
129         pxTopOfStack--;\r
130         *pxTopOfStack = ( StackType_t ) NULL;\r
131         pxTopOfStack--;\r
132         *pxTopOfStack = ( StackType_t ) NULL;\r
133         pxTopOfStack--;\r
134         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
135 \r
136         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
137         {\r
138                 /* The task will start in THUMB mode. */\r
139                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
140         }\r
141 \r
142         pxTopOfStack--;\r
143 \r
144         /* Next the return address, which in this case is the start of the task. */\r
145         *pxTopOfStack = ( StackType_t ) pxCode;\r
146         pxTopOfStack--;\r
147 \r
148         /* Next all the registers other than the stack pointer. */\r
149         *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;        /* R14 */\r
150         pxTopOfStack--;\r
151         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
152         pxTopOfStack--;\r
153         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
154         pxTopOfStack--;\r
155         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
156         pxTopOfStack--;\r
157         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
164         pxTopOfStack--;\r
165         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
176         pxTopOfStack--;\r
177 \r
178         /* The task will start with a critical nesting count of 0 as interrupts are\r
179         enabled. */\r
180         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
181         pxTopOfStack--;\r
182 \r
183         /* The task will start without a floating point context.  A task that uses\r
184         the floating point hardware must call vPortTaskUsesFPU() before executing\r
185         any floating point instructions. */\r
186         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
187 \r
188         return pxTopOfStack;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void prvTaskExitError( void )\r
193 {\r
194         /* A function that implements a task must not exit or attempt to return to\r
195         its caller as there is nothing to return to.  If a task wants to exit it\r
196         should instead call vTaskDelete( NULL ).\r
197 \r
198         Artificially force an assert() to be triggered if configASSERT() is\r
199         defined, then stop here so application writers can catch the error. */\r
200         configASSERT( ulPortInterruptNesting == ~0UL );\r
201         portDISABLE_INTERRUPTS();\r
202         for( ;; );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 BaseType_t xPortStartScheduler( void )\r
207 {\r
208 uint32_t ulAPSR;\r
209 \r
210         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
211         Privileged mode for the scheduler to start. */\r
212         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
213         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
214         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
215 \r
216         if( ulAPSR != portAPSR_USER_MODE )\r
217         {\r
218                 /* Start the timer that generates the tick ISR. */\r
219                 portDISABLE_INTERRUPTS();\r
220                 configSETUP_TICK_INTERRUPT();\r
221 \r
222                 /* Start the first task executing. */\r
223                 vPortRestoreTaskContext();\r
224         }\r
225 \r
226         /* Will only get here if vTaskStartScheduler() was called with the CPU in\r
227         a non-privileged mode or the binary point register was not set to its lowest\r
228         possible value.  prvTaskExitError() is referenced to prevent a compiler\r
229         warning about it being defined but not referenced in the case that the user\r
230         defines their own exit address. */\r
231         ( void ) prvTaskExitError;\r
232         return 0;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vPortEndScheduler( void )\r
237 {\r
238         /* Not implemented in ports where there is nothing to return to.\r
239         Artificially force an assert. */\r
240         configASSERT( ulCriticalNesting == 1000UL );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 void vPortEnterCritical( void )\r
245 {\r
246         portDISABLE_INTERRUPTS();\r
247 \r
248         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
249         directly.  Increment ulCriticalNesting to keep a count of how many times\r
250         portENTER_CRITICAL() has been called. */\r
251         ulCriticalNesting++;\r
252 \r
253         /* This is not the interrupt safe version of the enter critical function so\r
254         assert() if it is being called from an interrupt context.  Only API\r
255         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
256         the critical nesting count is 1 to protect against recursive calls if the\r
257         assert function also uses a critical section. */\r
258         if( ulCriticalNesting == 1 )\r
259         {\r
260                 configASSERT( ulPortInterruptNesting == 0 );\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vPortExitCritical( void )\r
266 {\r
267         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
268         {\r
269                 /* Decrement the nesting count as the critical section is being\r
270                 exited. */\r
271                 ulCriticalNesting--;\r
272 \r
273                 /* If the nesting level has reached zero then all interrupt\r
274                 priorities must be re-enabled. */\r
275                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
276                 {\r
277                         /* Critical nesting has reached zero so all interrupt priorities\r
278                         should be unmasked. */\r
279                         portENABLE_INTERRUPTS();\r
280                 }\r
281         }\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 void FreeRTOS_Tick_Handler( void )\r
286 {\r
287 uint32_t ulInterruptStatus;\r
288 \r
289         ulInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
290 \r
291         /* Increment the RTOS tick. */\r
292         if( xTaskIncrementTick() != pdFALSE )\r
293         {\r
294                 ulPortYieldRequired = pdTRUE;\r
295         }\r
296 \r
297         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulInterruptStatus );\r
298 \r
299         configCLEAR_TICK_INTERRUPT();\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 void vPortTaskUsesFPU( void )\r
304 {\r
305 uint32_t ulInitialFPSCR = 0;\r
306 \r
307         /* A task is registering the fact that it needs an FPU context.  Set the\r
308         FPU flag (which is saved as part of the task context). */\r
309         ulPortTaskHasFPUContext = pdTRUE;\r
310 \r
311         /* Initialise the floating point status register. */\r
312         __asm volatile ( "FMXR  FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 \r