]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CA9/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CA9 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 /* IAR includes. */\r
32 #include <intrinsics.h>\r
33 \r
34 /* Scheduler includes. */\r
35 #include "FreeRTOS.h"\r
36 #include "task.h"\r
37 \r
38 #ifndef configINTERRUPT_CONTROLLER_BASE_ADDRESS\r
39         #error configINTERRUPT_CONTROLLER_BASE_ADDRESS must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
40 #endif\r
41 \r
42 #ifndef configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET\r
43         #error configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
44 #endif\r
45 \r
46 #ifndef configUNIQUE_INTERRUPT_PRIORITIES\r
47         #error configUNIQUE_INTERRUPT_PRIORITIES must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
48 #endif\r
49 \r
50 #ifndef configSETUP_TICK_INTERRUPT\r
51         #error configSETUP_TICK_INTERRUPT() must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
52 #endif /* configSETUP_TICK_INTERRUPT */\r
53 \r
54 #ifndef configMAX_API_CALL_INTERRUPT_PRIORITY\r
55         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be defined.  See http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
56 #endif\r
57 \r
58 #if configMAX_API_CALL_INTERRUPT_PRIORITY == 0\r
59         #error configMAX_API_CALL_INTERRUPT_PRIORITY must not be set to 0\r
60 #endif\r
61 \r
62 #if configMAX_API_CALL_INTERRUPT_PRIORITY > configUNIQUE_INTERRUPT_PRIORITIES\r
63         #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
64 #endif\r
65 \r
66 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1\r
67         /* Check the configuration. */\r
68         #if( configMAX_PRIORITIES > 32 )\r
69                 #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
70         #endif\r
71 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
72 \r
73 /* In case security extensions are implemented. */\r
74 #if configMAX_API_CALL_INTERRUPT_PRIORITY <= ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
75         #error configMAX_API_CALL_INTERRUPT_PRIORITY must be greater than ( configUNIQUE_INTERRUPT_PRIORITIES / 2 )\r
76 #endif\r
77 \r
78 #ifndef configCLEAR_TICK_INTERRUPT\r
79         #define configCLEAR_TICK_INTERRUPT()\r
80 #endif\r
81 \r
82 /* A critical section is exited when the critical section nesting count reaches\r
83 this value. */\r
84 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
85 \r
86 /* In all GICs 255 can be written to the priority mask register to unmask all\r
87 (but the lowest) interrupt priority. */\r
88 #define portUNMASK_VALUE                                ( 0xFFUL )\r
89 \r
90 /* Tasks are not created with a floating point context, but can be given a\r
91 floating point context after they have been created.  A variable is stored as\r
92 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task\r
93 does not have an FPU context, or any other value if the task does have an FPU\r
94 context. */\r
95 #define portNO_FLOATING_POINT_CONTEXT   ( ( StackType_t ) 0 )\r
96 \r
97 /* Constants required to setup the initial task context. */\r
98 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
99 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
100 #define portTHUMB_MODE_ADDRESS                  ( 0x01UL )\r
101 \r
102 /* Used by portASSERT_IF_INTERRUPT_PRIORITY_INVALID() when ensuring the binary\r
103 point is zero. */\r
104 #define portBINARY_POINT_BITS                   ( ( uint8_t ) 0x03 )\r
105 \r
106 /* Masks all bits in the APSR other than the mode bits. */\r
107 #define portAPSR_MODE_BITS_MASK                 ( 0x1F )\r
108 \r
109 /* The value of the mode bits in the APSR when the CPU is executing in user\r
110 mode. */\r
111 #define portAPSR_USER_MODE                              ( 0x10 )\r
112 \r
113 /* Macro to unmask all interrupt priorities. */\r
114 #define portCLEAR_INTERRUPT_MASK()                                                                                      \\r
115 {                                                                                                                                                       \\r
116         __disable_irq();                                                                                                                \\r
117         portICCPMR_PRIORITY_MASK_REGISTER = portUNMASK_VALUE;                                   \\r
118         __asm(  "DSB            \n"                                                                                                     \\r
119                         "ISB            \n" );                                                                                          \\r
120         __enable_irq();                                                                                                                 \\r
121 }\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\r
126  * Starts the first task executing.  This function is necessarily written in\r
127  * assembly code so is implemented in portASM.s.\r
128  */\r
129 extern void vPortRestoreTaskContext( void );\r
130 \r
131 /*\r
132  * Used to catch tasks that attempt to return from their implementing function.\r
133  */\r
134 static void prvTaskExitError( void );\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 /* A variable is used to keep track of the critical section nesting.  This\r
139 variable has to be stored as part of the task context and must be initialised to\r
140 a non zero value to ensure interrupts don't inadvertently become unmasked before\r
141 the scheduler starts.  As it is stored as part of the task context it will\r
142 automatically be set to 0 when the first task is started. */\r
143 volatile uint32_t ulCriticalNesting = 9999UL;\r
144 \r
145 /* Saved as part of the task context.  If ulPortTaskHasFPUContext is non-zero\r
146 then a floating point context must be saved and restored for the task. */\r
147 uint32_t ulPortTaskHasFPUContext = pdFALSE;\r
148 \r
149 /* Set to 1 to pend a context switch from an ISR. */\r
150 uint32_t ulPortYieldRequired = pdFALSE;\r
151 \r
152 /* Counts the interrupt nesting depth.  A context switch is only performed if\r
153 if the nesting depth is 0. */\r
154 uint32_t ulPortInterruptNesting = 0UL;\r
155 \r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * See header file for description.\r
161  */\r
162 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
163 {\r
164         /* Setup the initial stack of the task.  The stack is set exactly as\r
165         expected by the portRESTORE_CONTEXT() macro.\r
166 \r
167         The fist real value on the stack is the status register, which is set for\r
168         system mode, with interrupts enabled.  A few NULLs are added first to ensure\r
169         GDB does not try decoding a non-existent return address. */\r
170         *pxTopOfStack = NULL;\r
171         pxTopOfStack--;\r
172         *pxTopOfStack = NULL;\r
173         pxTopOfStack--;\r
174         *pxTopOfStack = NULL;\r
175         pxTopOfStack--;\r
176         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
177 \r
178         if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )\r
179         {\r
180                 /* The task will start in THUMB mode. */\r
181                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
182         }\r
183 \r
184         pxTopOfStack--;\r
185 \r
186         /* Next the return address, which in this case is the start of the task. */\r
187         *pxTopOfStack = ( StackType_t ) pxCode;\r
188         pxTopOfStack--;\r
189 \r
190         /* Next all the registers other than the stack pointer. */\r
191         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* R14 */\r
192         pxTopOfStack--;\r
193         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
194         pxTopOfStack--;\r
195         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
196         pxTopOfStack--;\r
197         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
198         pxTopOfStack--;\r
199         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
200         pxTopOfStack--;\r
201         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
208         pxTopOfStack--;\r
209         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
210         pxTopOfStack--;\r
211         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
214         pxTopOfStack--;\r
215         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
216         pxTopOfStack--;\r
217         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
218         pxTopOfStack--;\r
219 \r
220         /* The task will start with a critical nesting count of 0 as interrupts are\r
221         enabled. */\r
222         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
223         pxTopOfStack--;\r
224 \r
225         /* The task will start without a floating point context.  A task that uses\r
226         the floating point hardware must call vPortTaskUsesFPU() before executing\r
227         any floating point instructions. */\r
228         *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;\r
229 \r
230         return pxTopOfStack;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void prvTaskExitError( void )\r
235 {\r
236         /* A function that implements a task must not exit or attempt to return to\r
237         its caller as there is nothing to return to.  If a task wants to exit it\r
238         should instead call vTaskDelete( NULL ).\r
239 \r
240         Artificially force an assert() to be triggered if configASSERT() is\r
241         defined, then stop here so application writers can catch the error. */\r
242         configASSERT( ulPortInterruptNesting == ~0UL );\r
243         portDISABLE_INTERRUPTS();\r
244         for( ;; );\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 BaseType_t xPortStartScheduler( void )\r
249 {\r
250 uint32_t ulAPSR;\r
251 \r
252         /* Only continue if the CPU is not in User mode.  The CPU must be in a\r
253         Privileged mode for the scheduler to start. */\r
254         __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );\r
255         ulAPSR &= portAPSR_MODE_BITS_MASK;\r
256         configASSERT( ulAPSR != portAPSR_USER_MODE );\r
257 \r
258         if( ulAPSR != portAPSR_USER_MODE )\r
259         {\r
260                 /* Only continue if the binary point value is set to its lowest possible\r
261                 setting.  See the comments in vPortValidateInterruptPriority() below for\r
262                 more information. */\r
263                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
264 \r
265                 if( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE )\r
266                 {\r
267                         /* Start the timer that generates the tick ISR. */\r
268                         configSETUP_TICK_INTERRUPT();\r
269 \r
270                         __enable_irq();\r
271                         vPortRestoreTaskContext();\r
272                 }\r
273         }\r
274 \r
275         /* Will only get here if vTaskStartScheduler() was called with the CPU in\r
276         a non-privileged mode or the binary point register was not set to its lowest\r
277         possible value. */\r
278         return 0;\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 void vPortEndScheduler( void )\r
283 {\r
284         /* Not implemented in ports where there is nothing to return to.\r
285         Artificially force an assert. */\r
286         configASSERT( ulCriticalNesting == 1000UL );\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 void vPortEnterCritical( void )\r
291 {\r
292         /* Disable interrupts as per portDISABLE_INTERRUPTS();  */\r
293         ulPortSetInterruptMask();\r
294 \r
295         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
296         directly.  Increment ulCriticalNesting to keep a count of how many times\r
297         portENTER_CRITICAL() has been called. */\r
298         ulCriticalNesting++;\r
299         \r
300         /* This is not the interrupt safe version of the enter critical function so\r
301         assert() if it is being called from an interrupt context.  Only API \r
302         functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
303         the critical nesting count is 1 to protect against recursive calls if the\r
304         assert function also uses a critical section. */\r
305         if( ulCriticalNesting == 1 )\r
306         {\r
307                 configASSERT( ulPortInterruptNesting == 0 );\r
308         }\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 void vPortExitCritical( void )\r
313 {\r
314         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
315         {\r
316                 /* Decrement the nesting count as the critical section is being\r
317                 exited. */\r
318                 ulCriticalNesting--;\r
319 \r
320                 /* If the nesting level has reached zero then all interrupt\r
321                 priorities must be re-enabled. */\r
322                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
323                 {\r
324                         /* Critical nesting has reached zero so all interrupt priorities\r
325                         should be unmasked. */\r
326                         portCLEAR_INTERRUPT_MASK();\r
327                 }\r
328         }\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 void FreeRTOS_Tick_Handler( void )\r
333 {\r
334         /* Set interrupt mask before altering scheduler structures.   The tick\r
335         handler runs at the lowest priority, so interrupts cannot already be masked,\r
336         so there is no need to save and restore the current mask value. */\r
337         __disable_irq();\r
338         portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
339         __asm(  "DSB            \n"\r
340                         "ISB            \n" );\r
341         __enable_irq();\r
342 \r
343         /* Increment the RTOS tick. */\r
344         if( xTaskIncrementTick() != pdFALSE )\r
345         {\r
346                 ulPortYieldRequired = pdTRUE;\r
347         }\r
348 \r
349         /* Ensure all interrupt priorities are active again. */\r
350         portCLEAR_INTERRUPT_MASK();\r
351         configCLEAR_TICK_INTERRUPT();\r
352 }\r
353 /*-----------------------------------------------------------*/\r
354 \r
355 void vPortTaskUsesFPU( void )\r
356 {\r
357 uint32_t ulInitialFPSCR = 0;\r
358 \r
359         /* A task is registering the fact that it needs an FPU context.  Set the\r
360         FPU flag (which is saved as part of the task context). */\r
361         ulPortTaskHasFPUContext = pdTRUE;\r
362 \r
363         /* Initialise the floating point status register. */\r
364         __asm( "FMXR    FPSCR, %0" :: "r" (ulInitialFPSCR) );\r
365 }\r
366 /*-----------------------------------------------------------*/\r
367 \r
368 void vPortClearInterruptMask( uint32_t ulNewMaskValue )\r
369 {\r
370         if( ulNewMaskValue == pdFALSE )\r
371         {\r
372                 portCLEAR_INTERRUPT_MASK();\r
373         }\r
374 }\r
375 /*-----------------------------------------------------------*/\r
376 \r
377 uint32_t ulPortSetInterruptMask( void )\r
378 {\r
379 uint32_t ulReturn;\r
380 \r
381         __disable_irq();\r
382         if( portICCPMR_PRIORITY_MASK_REGISTER == ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) )\r
383         {\r
384                 /* Interrupts were already masked. */\r
385                 ulReturn = pdTRUE;\r
386         }\r
387         else\r
388         {\r
389                 ulReturn = pdFALSE;\r
390                 portICCPMR_PRIORITY_MASK_REGISTER = ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
391                 __asm(  "DSB            \n"\r
392                                 "ISB            \n" );\r
393         }\r
394         __enable_irq();\r
395 \r
396         return ulReturn;\r
397 }\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 #if( configASSERT_DEFINED == 1 )\r
401 \r
402         void vPortValidateInterruptPriority( void )\r
403         {\r
404                 /* The following assertion will fail if a service routine (ISR) for\r
405                 an interrupt that has been assigned a priority above\r
406                 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
407                 function.  ISR safe FreeRTOS API functions must *only* be called\r
408                 from interrupts that have been assigned a priority at or below\r
409                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
410 \r
411                 Numerically low interrupt priority numbers represent logically high\r
412                 interrupt priorities, therefore the priority of the interrupt must\r
413                 be set to a value equal to or numerically *higher* than\r
414                 configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
415 \r
416                 FreeRTOS maintains separate thread and ISR API functions to ensure\r
417                 interrupt entry is as fast and simple as possible.\r
418 \r
419                 The following links provide detailed information:\r
420                 http://www.freertos.org/RTOS-Cortex-M3-M4.html\r
421                 http://www.freertos.org/FAQHelp.html */\r
422                 configASSERT( portICCRPR_RUNNING_PRIORITY_REGISTER >= ( uint32_t ) ( configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT ) );\r
423 \r
424                 /* Priority grouping:  The interrupt controller (GIC) allows the bits\r
425                 that define each interrupt's priority to be split between bits that\r
426                 define the interrupt's pre-emption priority bits and bits that define\r
427                 the interrupt's sub-priority.  For simplicity all bits must be defined\r
428                 to be pre-emption priority bits.  The following assertion will fail if\r
429                 this is not the case (if some bits represent a sub-priority).\r
430 \r
431                 The priority grouping is configured by the GIC's binary point register\r
432                 (ICCBPR).  Writting 0 to ICCBPR will ensure it is set to its lowest\r
433                 possible value (which may be above 0). */\r
434                 configASSERT( ( portICCBPR_BINARY_POINT_REGISTER & portBINARY_POINT_BITS ) <= portMAX_BINARY_POINT_VALUE );\r
435         }\r
436 \r
437 #endif /* configASSERT_DEFINED */\r
438 \r
439 \r