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