]> begriffs open source - freertos/blob - portable/IAR/ARM_CM23/non_secure/port.c
Ensure interrupts are enabled at first task start (#214)
[freertos] / portable / IAR / ARM_CM23 / non_secure / port.c
1 /*\r
2  * FreeRTOS Kernel V10.4.1\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  * https://www.FreeRTOS.org\r
23  * https://github.com/FreeRTOS\r
24  *\r
25  */\r
26 \r
27 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
28  * all the API functions to use the MPU wrappers. That should only be done when\r
29  * task.h is included from an application file. */\r
30 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* MPU wrappers includes. */\r
37 #include "mpu_wrappers.h"\r
38 \r
39 /* Portasm includes. */\r
40 #include "portasm.h"\r
41 \r
42 #if ( configENABLE_TRUSTZONE == 1 )\r
43     /* Secure components includes. */\r
44     #include "secure_context.h"\r
45     #include "secure_init.h"\r
46 #endif /* configENABLE_TRUSTZONE */\r
47 \r
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
49 \r
50 /**\r
51  * The FreeRTOS Cortex M33 port can be configured to run on the Secure Side only\r
52  * i.e. the processor boots as secure and never jumps to the non-secure side.\r
53  * The Trust Zone support in the port must be disabled in order to run FreeRTOS\r
54  * on the secure side. The following are the valid configuration seetings:\r
55  *\r
56  * 1. Run FreeRTOS on the Secure Side:\r
57  *    configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0\r
58  *\r
59  * 2. Run FreeRTOS on the Non-Secure Side with Secure Side function call support:\r
60  *    configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1\r
61  *\r
62  * 3. Run FreeRTOS on the Non-Secure Side only i.e. no Secure Side function call support:\r
63  *    configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0\r
64  */\r
65 #if ( ( configRUN_FREERTOS_SECURE_ONLY == 1 ) && ( configENABLE_TRUSTZONE == 1 ) )\r
66     #error TrustZone needs to be disabled in order to run FreeRTOS on the Secure Side.\r
67 #endif\r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /**\r
71  * @brief Constants required to manipulate the NVIC.\r
72  */\r
73 #define portNVIC_SYSTICK_CTRL_REG             ( *( ( volatile uint32_t * ) 0xe000e010 ) )\r
74 #define portNVIC_SYSTICK_LOAD_REG             ( *( ( volatile uint32_t * ) 0xe000e014 ) )\r
75 #define portNVIC_SYSTICK_CURRENT_VALUE_REG    ( *( ( volatile uint32_t * ) 0xe000e018 ) )\r
76 #define portNVIC_SHPR3_REG                    ( *( ( volatile uint32_t * ) 0xe000ed20 ) )\r
77 #define portNVIC_SYSTICK_ENABLE_BIT           ( 1UL << 0UL )\r
78 #define portNVIC_SYSTICK_INT_BIT              ( 1UL << 1UL )\r
79 #define portNVIC_SYSTICK_COUNT_FLAG_BIT       ( 1UL << 16UL )\r
80 #define portMIN_INTERRUPT_PRIORITY            ( 255UL )\r
81 #define portNVIC_PENDSV_PRI                   ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
82 #define portNVIC_SYSTICK_PRI                  ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
83 #ifndef configSYSTICK_CLOCK_HZ\r
84     #define configSYSTICK_CLOCK_HZ            configCPU_CLOCK_HZ\r
85     /* Ensure the SysTick is clocked at the same frequency as the core. */\r
86     #define portNVIC_SYSTICK_CLK_BIT          ( 1UL << 2UL )\r
87 #else\r
88 \r
89 /* The way the SysTick is clocked is not modified in case it is not the\r
90  * same a the core. */\r
91     #define portNVIC_SYSTICK_CLK_BIT    ( 0 )\r
92 #endif\r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /**\r
96  * @brief Constants required to manipulate the SCB.\r
97  */\r
98 #define portSCB_SYS_HANDLER_CTRL_STATE_REG    ( *( volatile uint32_t * ) 0xe000ed24 )\r
99 #define portSCB_MEM_FAULT_ENABLE_BIT          ( 1UL << 16UL )\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /**\r
103  * @brief Constants required to manipulate the FPU.\r
104  */\r
105 #define portCPACR               ( ( volatile uint32_t * ) 0xe000ed88 )              /* Coprocessor Access Control Register. */\r
106 #define portCPACR_CP10_VALUE    ( 3UL )\r
107 #define portCPACR_CP11_VALUE    portCPACR_CP10_VALUE\r
108 #define portCPACR_CP10_POS      ( 20UL )\r
109 #define portCPACR_CP11_POS      ( 22UL )\r
110 \r
111 #define portFPCCR               ( ( volatile uint32_t * ) 0xe000ef34 )              /* Floating Point Context Control Register. */\r
112 #define portFPCCR_ASPEN_POS     ( 31UL )\r
113 #define portFPCCR_ASPEN_MASK    ( 1UL << portFPCCR_ASPEN_POS )\r
114 #define portFPCCR_LSPEN_POS     ( 30UL )\r
115 #define portFPCCR_LSPEN_MASK    ( 1UL << portFPCCR_LSPEN_POS )\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /**\r
119  * @brief Constants required to manipulate the MPU.\r
120  */\r
121 #define portMPU_TYPE_REG                      ( *( ( volatile uint32_t * ) 0xe000ed90 ) )\r
122 #define portMPU_CTRL_REG                      ( *( ( volatile uint32_t * ) 0xe000ed94 ) )\r
123 #define portMPU_RNR_REG                       ( *( ( volatile uint32_t * ) 0xe000ed98 ) )\r
124 \r
125 #define portMPU_RBAR_REG                      ( *( ( volatile uint32_t * ) 0xe000ed9c ) )\r
126 #define portMPU_RLAR_REG                      ( *( ( volatile uint32_t * ) 0xe000eda0 ) )\r
127 \r
128 #define portMPU_RBAR_A1_REG                   ( *( ( volatile uint32_t * ) 0xe000eda4 ) )\r
129 #define portMPU_RLAR_A1_REG                   ( *( ( volatile uint32_t * ) 0xe000eda8 ) )\r
130 \r
131 #define portMPU_RBAR_A2_REG                   ( *( ( volatile uint32_t * ) 0xe000edac ) )\r
132 #define portMPU_RLAR_A2_REG                   ( *( ( volatile uint32_t * ) 0xe000edb0 ) )\r
133 \r
134 #define portMPU_RBAR_A3_REG                   ( *( ( volatile uint32_t * ) 0xe000edb4 ) )\r
135 #define portMPU_RLAR_A3_REG                   ( *( ( volatile uint32_t * ) 0xe000edb8 ) )\r
136 \r
137 #define portMPU_MAIR0_REG                     ( *( ( volatile uint32_t * ) 0xe000edc0 ) )\r
138 #define portMPU_MAIR1_REG                     ( *( ( volatile uint32_t * ) 0xe000edc4 ) )\r
139 \r
140 #define portMPU_RBAR_ADDRESS_MASK             ( 0xffffffe0 ) /* Must be 32-byte aligned. */\r
141 #define portMPU_RLAR_ADDRESS_MASK             ( 0xffffffe0 ) /* Must be 32-byte aligned. */\r
142 \r
143 #define portMPU_MAIR_ATTR0_POS                ( 0UL )\r
144 #define portMPU_MAIR_ATTR0_MASK               ( 0x000000ff )\r
145 \r
146 #define portMPU_MAIR_ATTR1_POS                ( 8UL )\r
147 #define portMPU_MAIR_ATTR1_MASK               ( 0x0000ff00 )\r
148 \r
149 #define portMPU_MAIR_ATTR2_POS                ( 16UL )\r
150 #define portMPU_MAIR_ATTR2_MASK               ( 0x00ff0000 )\r
151 \r
152 #define portMPU_MAIR_ATTR3_POS                ( 24UL )\r
153 #define portMPU_MAIR_ATTR3_MASK               ( 0xff000000 )\r
154 \r
155 #define portMPU_MAIR_ATTR4_POS                ( 0UL )\r
156 #define portMPU_MAIR_ATTR4_MASK               ( 0x000000ff )\r
157 \r
158 #define portMPU_MAIR_ATTR5_POS                ( 8UL )\r
159 #define portMPU_MAIR_ATTR5_MASK               ( 0x0000ff00 )\r
160 \r
161 #define portMPU_MAIR_ATTR6_POS                ( 16UL )\r
162 #define portMPU_MAIR_ATTR6_MASK               ( 0x00ff0000 )\r
163 \r
164 #define portMPU_MAIR_ATTR7_POS                ( 24UL )\r
165 #define portMPU_MAIR_ATTR7_MASK               ( 0xff000000 )\r
166 \r
167 #define portMPU_RLAR_ATTR_INDEX0              ( 0UL << 1UL )\r
168 #define portMPU_RLAR_ATTR_INDEX1              ( 1UL << 1UL )\r
169 #define portMPU_RLAR_ATTR_INDEX2              ( 2UL << 1UL )\r
170 #define portMPU_RLAR_ATTR_INDEX3              ( 3UL << 1UL )\r
171 #define portMPU_RLAR_ATTR_INDEX4              ( 4UL << 1UL )\r
172 #define portMPU_RLAR_ATTR_INDEX5              ( 5UL << 1UL )\r
173 #define portMPU_RLAR_ATTR_INDEX6              ( 6UL << 1UL )\r
174 #define portMPU_RLAR_ATTR_INDEX7              ( 7UL << 1UL )\r
175 \r
176 #define portMPU_RLAR_REGION_ENABLE            ( 1UL )\r
177 \r
178 /* Enable privileged access to unmapped region. */\r
179 #define portMPU_PRIV_BACKGROUND_ENABLE_BIT    ( 1UL << 2UL )\r
180 \r
181 /* Enable MPU. */\r
182 #define portMPU_ENABLE_BIT                    ( 1UL << 0UL )\r
183 \r
184 /* Expected value of the portMPU_TYPE register. */\r
185 #define portEXPECTED_MPU_TYPE_VALUE           ( 8UL << 8UL ) /* 8 regions, unified. */\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /**\r
189  * @brief The maximum 24-bit number.\r
190  *\r
191  * It is needed because the systick is a 24-bit counter.\r
192  */\r
193 #define portMAX_24_BIT_NUMBER       ( 0xffffffUL )\r
194 \r
195 /**\r
196  * @brief A fiddle factor to estimate the number of SysTick counts that would\r
197  * have occurred while the SysTick counter is stopped during tickless idle\r
198  * calculations.\r
199  */\r
200 #define portMISSED_COUNTS_FACTOR    ( 45UL )\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /**\r
204  * @brief Constants required to set up the initial stack.\r
205  */\r
206 #define portINITIAL_XPSR    ( 0x01000000 )\r
207 \r
208 #if ( configRUN_FREERTOS_SECURE_ONLY == 1 )\r
209 \r
210 /**\r
211  * @brief Initial EXC_RETURN value.\r
212  *\r
213  *     FF         FF         FF         FD\r
214  * 1111 1111  1111 1111  1111 1111  1111 1101\r
215  *\r
216  * Bit[6] - 1 --> The exception was taken from the Secure state.\r
217  * Bit[5] - 1 --> Do not skip stacking of additional state context.\r
218  * Bit[4] - 1 --> The PE did not allocate space on the stack for FP context.\r
219  * Bit[3] - 1 --> Return to the Thread mode.\r
220  * Bit[2] - 1 --> Restore registers from the process stack.\r
221  * Bit[1] - 0 --> Reserved, 0.\r
222  * Bit[0] - 1 --> The exception was taken to the Secure state.\r
223  */\r
224     #define portINITIAL_EXC_RETURN    ( 0xfffffffd )\r
225 #else\r
226 \r
227 /**\r
228  * @brief Initial EXC_RETURN value.\r
229  *\r
230  *     FF         FF         FF         BC\r
231  * 1111 1111  1111 1111  1111 1111  1011 1100\r
232  *\r
233  * Bit[6] - 0 --> The exception was taken from the Non-Secure state.\r
234  * Bit[5] - 1 --> Do not skip stacking of additional state context.\r
235  * Bit[4] - 1 --> The PE did not allocate space on the stack for FP context.\r
236  * Bit[3] - 1 --> Return to the Thread mode.\r
237  * Bit[2] - 1 --> Restore registers from the process stack.\r
238  * Bit[1] - 0 --> Reserved, 0.\r
239  * Bit[0] - 0 --> The exception was taken to the Non-Secure state.\r
240  */\r
241     #define portINITIAL_EXC_RETURN    ( 0xffffffbc )\r
242 #endif /* configRUN_FREERTOS_SECURE_ONLY */\r
243 \r
244 /**\r
245  * @brief CONTROL register privileged bit mask.\r
246  *\r
247  * Bit[0] in CONTROL register tells the privilege:\r
248  *  Bit[0] = 0 ==> The task is privileged.\r
249  *  Bit[0] = 1 ==> The task is not privileged.\r
250  */\r
251 #define portCONTROL_PRIVILEGED_MASK         ( 1UL << 0UL )\r
252 \r
253 /**\r
254  * @brief Initial CONTROL register values.\r
255  */\r
256 #define portINITIAL_CONTROL_UNPRIVILEGED    ( 0x3 )\r
257 #define portINITIAL_CONTROL_PRIVILEGED      ( 0x2 )\r
258 \r
259 /**\r
260  * @brief Let the user override the pre-loading of the initial LR with the\r
261  * address of prvTaskExitError() in case it messes up unwinding of the stack\r
262  * in the debugger.\r
263  */\r
264 #ifdef configTASK_RETURN_ADDRESS\r
265     #define portTASK_RETURN_ADDRESS    configTASK_RETURN_ADDRESS\r
266 #else\r
267     #define portTASK_RETURN_ADDRESS    prvTaskExitError\r
268 #endif\r
269 \r
270 /**\r
271  * @brief If portPRELOAD_REGISTERS then registers will be given an initial value\r
272  * when a task is created. This helps in debugging at the cost of code size.\r
273  */\r
274 #define portPRELOAD_REGISTERS    1\r
275 \r
276 /**\r
277  * @brief A task is created without a secure context, and must call\r
278  * portALLOCATE_SECURE_CONTEXT() to give itself a secure context before it makes\r
279  * any secure calls.\r
280  */\r
281 #define portNO_SECURE_CONTEXT    0\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 /**\r
285  * @brief Used to catch tasks that attempt to return from their implementing\r
286  * function.\r
287  */\r
288 static void prvTaskExitError( void );\r
289 \r
290 #if ( configENABLE_MPU == 1 )\r
291 \r
292 /**\r
293  * @brief Setup the Memory Protection Unit (MPU).\r
294  */\r
295     static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;\r
296 #endif /* configENABLE_MPU */\r
297 \r
298 #if ( configENABLE_FPU == 1 )\r
299 \r
300 /**\r
301  * @brief Setup the Floating Point Unit (FPU).\r
302  */\r
303     static void prvSetupFPU( void ) PRIVILEGED_FUNCTION;\r
304 #endif /* configENABLE_FPU */\r
305 \r
306 /**\r
307  * @brief Setup the timer to generate the tick interrupts.\r
308  *\r
309  * The implementation in this file is weak to allow application writers to\r
310  * change the timer used to generate the tick interrupt.\r
311  */\r
312 void vPortSetupTimerInterrupt( void ) PRIVILEGED_FUNCTION;\r
313 \r
314 /**\r
315  * @brief Checks whether the current execution context is interrupt.\r
316  *\r
317  * @return pdTRUE if the current execution context is interrupt, pdFALSE\r
318  * otherwise.\r
319  */\r
320 BaseType_t xPortIsInsideInterrupt( void );\r
321 \r
322 /**\r
323  * @brief Yield the processor.\r
324  */\r
325 void vPortYield( void ) PRIVILEGED_FUNCTION;\r
326 \r
327 /**\r
328  * @brief Enter critical section.\r
329  */\r
330 void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;\r
331 \r
332 /**\r
333  * @brief Exit from critical section.\r
334  */\r
335 void vPortExitCritical( void ) PRIVILEGED_FUNCTION;\r
336 \r
337 /**\r
338  * @brief SysTick handler.\r
339  */\r
340 void SysTick_Handler( void ) PRIVILEGED_FUNCTION;\r
341 \r
342 /**\r
343  * @brief C part of SVC handler.\r
344  */\r
345 portDONT_DISCARD void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) PRIVILEGED_FUNCTION;\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 /**\r
349  * @brief Each task maintains its own interrupt status in the critical nesting\r
350  * variable.\r
351  */\r
352 PRIVILEGED_DATA static volatile uint32_t ulCriticalNesting = 0xaaaaaaaaUL;\r
353 \r
354 #if ( configENABLE_TRUSTZONE == 1 )\r
355 \r
356 /**\r
357  * @brief Saved as part of the task context to indicate which context the\r
358  * task is using on the secure side.\r
359  */\r
360     PRIVILEGED_DATA portDONT_DISCARD volatile SecureContextHandle_t xSecureContext = portNO_SECURE_CONTEXT;\r
361 #endif /* configENABLE_TRUSTZONE */\r
362 \r
363 #if ( configUSE_TICKLESS_IDLE == 1 )\r
364 \r
365 /**\r
366  * @brief The number of SysTick increments that make up one tick period.\r
367  */\r
368     PRIVILEGED_DATA static uint32_t ulTimerCountsForOneTick = 0;\r
369 \r
370 /**\r
371  * @brief The maximum number of tick periods that can be suppressed is\r
372  * limited by the 24 bit resolution of the SysTick timer.\r
373  */\r
374     PRIVILEGED_DATA static uint32_t xMaximumPossibleSuppressedTicks = 0;\r
375 \r
376 /**\r
377  * @brief Compensate for the CPU cycles that pass while the SysTick is\r
378  * stopped (low power functionality only).\r
379  */\r
380     PRIVILEGED_DATA static uint32_t ulStoppedTimerCompensation = 0;\r
381 #endif /* configUSE_TICKLESS_IDLE */\r
382 /*-----------------------------------------------------------*/\r
383 \r
384 #if ( configUSE_TICKLESS_IDLE == 1 )\r
385     __attribute__( ( weak ) ) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
386     {\r
387         uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;\r
388         TickType_t xModifiableIdleTime;\r
389 \r
390         /* Make sure the SysTick reload value does not overflow the counter. */\r
391         if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )\r
392         {\r
393             xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
394         }\r
395 \r
396         /* Stop the SysTick momentarily. The time the SysTick is stopped for is\r
397          * accounted for as best it can be, but using the tickless mode will\r
398          * inevitably result in some tiny drift of the time maintained by the\r
399          * kernel with respect to calendar time. */\r
400         portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;\r
401 \r
402         /* Calculate the reload value required to wait xExpectedIdleTime\r
403          * tick periods. -1 is used because this code will execute part way\r
404          * through one of the tick periods. */\r
405         ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );\r
406 \r
407         if( ulReloadValue > ulStoppedTimerCompensation )\r
408         {\r
409             ulReloadValue -= ulStoppedTimerCompensation;\r
410         }\r
411 \r
412         /* Enter a critical section but don't use the taskENTER_CRITICAL()\r
413          * method as that will mask interrupts that should exit sleep mode. */\r
414         __asm volatile ( "cpsid i" ::: "memory" );\r
415         __asm volatile ( "dsb" );\r
416         __asm volatile ( "isb" );\r
417 \r
418         /* If a context switch is pending or a task is waiting for the scheduler\r
419          * to be un-suspended then abandon the low power entry. */\r
420         if( eTaskConfirmSleepModeStatus() == eAbortSleep )\r
421         {\r
422             /* Restart from whatever is left in the count register to complete\r
423              * this tick period. */\r
424             portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
425 \r
426             /* Restart SysTick. */\r
427             portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
428 \r
429             /* Reset the reload register to the value required for normal tick\r
430              * periods. */\r
431             portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
432 \r
433             /* Re-enable interrupts - see comments above the cpsid instruction()\r
434              * above. */\r
435             __asm volatile ( "cpsie i" ::: "memory" );\r
436         }\r
437         else\r
438         {\r
439             /* Set the new reload value. */\r
440             portNVIC_SYSTICK_LOAD_REG = ulReloadValue;\r
441 \r
442             /* Clear the SysTick count flag and set the count value back to\r
443              * zero. */\r
444             portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
445 \r
446             /* Restart SysTick. */\r
447             portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
448 \r
449             /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can\r
450              * set its parameter to 0 to indicate that its implementation\r
451              * contains its own wait for interrupt or wait for event\r
452              * instruction, and so wfi should not be executed again. However,\r
453              * the original expected idle time variable must remain unmodified,\r
454              * so a copy is taken. */\r
455             xModifiableIdleTime = xExpectedIdleTime;\r
456             configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
457 \r
458             if( xModifiableIdleTime > 0 )\r
459             {\r
460                 __asm volatile ( "dsb" ::: "memory" );\r
461                 __asm volatile ( "wfi" );\r
462                 __asm volatile ( "isb" );\r
463             }\r
464 \r
465             configPOST_SLEEP_PROCESSING( xExpectedIdleTime );\r
466 \r
467             /* Re-enable interrupts to allow the interrupt that brought the MCU\r
468              * out of sleep mode to execute immediately. See comments above\r
469              * the cpsid instruction above. */\r
470             __asm volatile ( "cpsie i" ::: "memory" );\r
471             __asm volatile ( "dsb" );\r
472             __asm volatile ( "isb" );\r
473 \r
474             /* Disable interrupts again because the clock is about to be stopped\r
475              * and interrupts that execute while the clock is stopped will\r
476              * increase any slippage between the time maintained by the RTOS and\r
477              * calendar time. */\r
478             __asm volatile ( "cpsid i" ::: "memory" );\r
479             __asm volatile ( "dsb" );\r
480             __asm volatile ( "isb" );\r
481 \r
482             /* Disable the SysTick clock without reading the\r
483              * portNVIC_SYSTICK_CTRL_REG register to ensure the\r
484              * portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set.\r
485              * Again, the time the SysTick is stopped for is accounted for as\r
486              * best it can be, but using the tickless mode will inevitably\r
487              * result in some tiny drift of the time maintained by the kernel\r
488              * with respect to calendar time*/\r
489             portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );\r
490 \r
491             /* Determine if the SysTick clock has already counted to zero and\r
492              * been set back to the current reload value (the reload back being\r
493              * correct for the entire expected idle time) or if the SysTick is\r
494              * yet to count to zero (in which case an interrupt other than the\r
495              * SysTick must have brought the system out of sleep mode). */\r
496             if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )\r
497             {\r
498                 uint32_t ulCalculatedLoadValue;\r
499 \r
500                 /* The tick interrupt is already pending, and the SysTick count\r
501                  * reloaded with ulReloadValue.  Reset the\r
502                  * portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick\r
503                  * period. */\r
504                 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
505 \r
506                 /* Don't allow a tiny value, or values that have somehow\r
507                  * underflowed because the post sleep hook did something\r
508                  * that took too long. */\r
509                 if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )\r
510                 {\r
511                     ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );\r
512                 }\r
513 \r
514                 portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;\r
515 \r
516                 /* As the pending tick will be processed as soon as this\r
517                  * function exits, the tick value maintained by the tick is\r
518                  * stepped forward by one less than the time spent waiting. */\r
519                 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
520             }\r
521             else\r
522             {\r
523                 /* Something other than the tick interrupt ended the sleep.\r
524                  * Work out how long the sleep lasted rounded to complete tick\r
525                  * periods (not the ulReload value which accounted for part\r
526                  * ticks). */\r
527                 ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
528 \r
529                 /* How many complete tick periods passed while the processor\r
530                  * was waiting? */\r
531                 ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;\r
532 \r
533                 /* The reload value is set to whatever fraction of a single tick\r
534                  * period remains. */\r
535                 portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;\r
536             }\r
537 \r
538             /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG\r
539              * again, then set portNVIC_SYSTICK_LOAD_REG back to its standard\r
540              * value. */\r
541             portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
542             portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
543             vTaskStepTick( ulCompleteTickPeriods );\r
544             portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
545 \r
546             /* Exit with interrupts enabled. */\r
547             __asm volatile ( "cpsie i" ::: "memory" );\r
548         }\r
549     }\r
550 #endif /* configUSE_TICKLESS_IDLE */\r
551 /*-----------------------------------------------------------*/\r
552 \r
553 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void ) /* PRIVILEGED_FUNCTION */\r
554 {\r
555     /* Calculate the constants required to configure the tick interrupt. */\r
556     #if ( configUSE_TICKLESS_IDLE == 1 )\r
557         {\r
558             ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );\r
559             xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;\r
560             ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );\r
561         }\r
562     #endif /* configUSE_TICKLESS_IDLE */\r
563 \r
564     /* Stop and reset the SysTick. */\r
565     portNVIC_SYSTICK_CTRL_REG = 0UL;\r
566     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
567 \r
568     /* Configure SysTick to interrupt at the requested rate. */\r
569     portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
570     portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;\r
571 }\r
572 /*-----------------------------------------------------------*/\r
573 \r
574 static void prvTaskExitError( void )\r
575 {\r
576     volatile uint32_t ulDummy = 0UL;\r
577 \r
578     /* A function that implements a task must not exit or attempt to return to\r
579      * its caller as there is nothing to return to. If a task wants to exit it\r
580      * should instead call vTaskDelete( NULL ). Artificially force an assert()\r
581      * to be triggered if configASSERT() is defined, then stop here so\r
582      * application writers can catch the error. */\r
583     configASSERT( ulCriticalNesting == ~0UL );\r
584     portDISABLE_INTERRUPTS();\r
585 \r
586     while( ulDummy == 0 )\r
587     {\r
588         /* This file calls prvTaskExitError() after the scheduler has been\r
589          * started to remove a compiler warning about the function being\r
590          * defined but never called.  ulDummy is used purely to quieten other\r
591          * warnings about code appearing after this function is called - making\r
592          * ulDummy volatile makes the compiler think the function could return\r
593          * and therefore not output an 'unreachable code' warning for code that\r
594          * appears after it. */\r
595     }\r
596 }\r
597 /*-----------------------------------------------------------*/\r
598 \r
599 #if ( configENABLE_MPU == 1 )\r
600     static void prvSetupMPU( void ) /* PRIVILEGED_FUNCTION */\r
601     {\r
602         #if defined( __ARMCC_VERSION )\r
603 \r
604             /* Declaration when these variable are defined in code instead of being\r
605              * exported from linker scripts. */\r
606             extern uint32_t * __privileged_functions_start__;\r
607             extern uint32_t * __privileged_functions_end__;\r
608             extern uint32_t * __syscalls_flash_start__;\r
609             extern uint32_t * __syscalls_flash_end__;\r
610             extern uint32_t * __unprivileged_flash_start__;\r
611             extern uint32_t * __unprivileged_flash_end__;\r
612             extern uint32_t * __privileged_sram_start__;\r
613             extern uint32_t * __privileged_sram_end__;\r
614         #else /* if defined( __ARMCC_VERSION ) */\r
615             /* Declaration when these variable are exported from linker scripts. */\r
616             extern uint32_t __privileged_functions_start__[];\r
617             extern uint32_t __privileged_functions_end__[];\r
618             extern uint32_t __syscalls_flash_start__[];\r
619             extern uint32_t __syscalls_flash_end__[];\r
620             extern uint32_t __unprivileged_flash_start__[];\r
621             extern uint32_t __unprivileged_flash_end__[];\r
622             extern uint32_t __privileged_sram_start__[];\r
623             extern uint32_t __privileged_sram_end__[];\r
624         #endif /* defined( __ARMCC_VERSION ) */\r
625 \r
626         /* Check that the MPU is present. */\r
627         if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )\r
628         {\r
629             /* MAIR0 - Index 0. */\r
630             portMPU_MAIR0_REG |= ( ( portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE << portMPU_MAIR_ATTR0_POS ) & portMPU_MAIR_ATTR0_MASK );\r
631             /* MAIR0 - Index 1. */\r
632             portMPU_MAIR0_REG |= ( ( portMPU_DEVICE_MEMORY_nGnRE << portMPU_MAIR_ATTR1_POS ) & portMPU_MAIR_ATTR1_MASK );\r
633 \r
634             /* Setup privileged flash as Read Only so that privileged tasks can\r
635              * read it but not modify. */\r
636             portMPU_RNR_REG = portPRIVILEGED_FLASH_REGION;\r
637             portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_functions_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
638                                ( portMPU_REGION_NON_SHAREABLE ) |\r
639                                ( portMPU_REGION_PRIVILEGED_READ_ONLY );\r
640             portMPU_RLAR_REG = ( ( ( uint32_t ) __privileged_functions_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
641                                ( portMPU_RLAR_ATTR_INDEX0 ) |\r
642                                ( portMPU_RLAR_REGION_ENABLE );\r
643 \r
644             /* Setup unprivileged flash as Read Only by both privileged and\r
645              * unprivileged tasks. All tasks can read it but no-one can modify. */\r
646             portMPU_RNR_REG = portUNPRIVILEGED_FLASH_REGION;\r
647             portMPU_RBAR_REG = ( ( ( uint32_t ) __unprivileged_flash_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
648                                ( portMPU_REGION_NON_SHAREABLE ) |\r
649                                ( portMPU_REGION_READ_ONLY );\r
650             portMPU_RLAR_REG = ( ( ( uint32_t ) __unprivileged_flash_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
651                                ( portMPU_RLAR_ATTR_INDEX0 ) |\r
652                                ( portMPU_RLAR_REGION_ENABLE );\r
653 \r
654             /* Setup unprivileged syscalls flash as Read Only by both privileged\r
655              * and unprivileged tasks. All tasks can read it but no-one can modify. */\r
656             portMPU_RNR_REG = portUNPRIVILEGED_SYSCALLS_REGION;\r
657             portMPU_RBAR_REG = ( ( ( uint32_t ) __syscalls_flash_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
658                                ( portMPU_REGION_NON_SHAREABLE ) |\r
659                                ( portMPU_REGION_READ_ONLY );\r
660             portMPU_RLAR_REG = ( ( ( uint32_t ) __syscalls_flash_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
661                                ( portMPU_RLAR_ATTR_INDEX0 ) |\r
662                                ( portMPU_RLAR_REGION_ENABLE );\r
663 \r
664             /* Setup RAM containing kernel data for privileged access only. */\r
665             portMPU_RNR_REG = portPRIVILEGED_RAM_REGION;\r
666             portMPU_RBAR_REG = ( ( ( uint32_t ) __privileged_sram_start__ ) & portMPU_RBAR_ADDRESS_MASK ) |\r
667                                ( portMPU_REGION_NON_SHAREABLE ) |\r
668                                ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
669                                ( portMPU_REGION_EXECUTE_NEVER );\r
670             portMPU_RLAR_REG = ( ( ( uint32_t ) __privileged_sram_end__ ) & portMPU_RLAR_ADDRESS_MASK ) |\r
671                                ( portMPU_RLAR_ATTR_INDEX0 ) |\r
672                                ( portMPU_RLAR_REGION_ENABLE );\r
673 \r
674             /* Enable mem fault. */\r
675             portSCB_SYS_HANDLER_CTRL_STATE_REG |= portSCB_MEM_FAULT_ENABLE_BIT;\r
676 \r
677             /* Enable MPU with privileged background access i.e. unmapped\r
678              * regions have privileged access. */\r
679             portMPU_CTRL_REG |= ( portMPU_PRIV_BACKGROUND_ENABLE_BIT | portMPU_ENABLE_BIT );\r
680         }\r
681     }\r
682 #endif /* configENABLE_MPU */\r
683 /*-----------------------------------------------------------*/\r
684 \r
685 #if ( configENABLE_FPU == 1 )\r
686     static void prvSetupFPU( void ) /* PRIVILEGED_FUNCTION */\r
687     {\r
688         #if ( configENABLE_TRUSTZONE == 1 )\r
689             {\r
690                 /* Enable non-secure access to the FPU. */\r
691                 SecureInit_EnableNSFPUAccess();\r
692             }\r
693         #endif /* configENABLE_TRUSTZONE */\r
694 \r
695         /* CP10 = 11 ==> Full access to FPU i.e. both privileged and\r
696          * unprivileged code should be able to access FPU. CP11 should be\r
697          * programmed to the same value as CP10. */\r
698         *( portCPACR ) |= ( ( portCPACR_CP10_VALUE << portCPACR_CP10_POS ) |\r
699                             ( portCPACR_CP11_VALUE << portCPACR_CP11_POS )\r
700                             );\r
701 \r
702         /* ASPEN = 1 ==> Hardware should automatically preserve floating point\r
703          * context on exception entry and restore on exception return.\r
704          * LSPEN = 1 ==> Enable lazy context save of FP state. */\r
705         *( portFPCCR ) |= ( portFPCCR_ASPEN_MASK | portFPCCR_LSPEN_MASK );\r
706     }\r
707 #endif /* configENABLE_FPU */\r
708 /*-----------------------------------------------------------*/\r
709 \r
710 void vPortYield( void ) /* PRIVILEGED_FUNCTION */\r
711 {\r
712     /* Set a PendSV to request a context switch. */\r
713     portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
714 \r
715     /* Barriers are normally not required but do ensure the code is\r
716      * completely within the specified behaviour for the architecture. */\r
717     __asm volatile ( "dsb" ::: "memory" );\r
718     __asm volatile ( "isb" );\r
719 }\r
720 /*-----------------------------------------------------------*/\r
721 \r
722 void vPortEnterCritical( void ) /* PRIVILEGED_FUNCTION */\r
723 {\r
724     portDISABLE_INTERRUPTS();\r
725     ulCriticalNesting++;\r
726 \r
727     /* Barriers are normally not required but do ensure the code is\r
728      * completely within the specified behaviour for the architecture. */\r
729     __asm volatile ( "dsb" ::: "memory" );\r
730     __asm volatile ( "isb" );\r
731 }\r
732 /*-----------------------------------------------------------*/\r
733 \r
734 void vPortExitCritical( void ) /* PRIVILEGED_FUNCTION */\r
735 {\r
736     configASSERT( ulCriticalNesting );\r
737     ulCriticalNesting--;\r
738 \r
739     if( ulCriticalNesting == 0 )\r
740     {\r
741         portENABLE_INTERRUPTS();\r
742     }\r
743 }\r
744 /*-----------------------------------------------------------*/\r
745 \r
746 void SysTick_Handler( void ) /* PRIVILEGED_FUNCTION */\r
747 {\r
748     uint32_t ulPreviousMask;\r
749 \r
750     ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
751     {\r
752         /* Increment the RTOS tick. */\r
753         if( xTaskIncrementTick() != pdFALSE )\r
754         {\r
755             /* Pend a context switch. */\r
756             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
757         }\r
758     }\r
759     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
760 }\r
761 /*-----------------------------------------------------------*/\r
762 \r
763 void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTION portDONT_DISCARD */\r
764 {\r
765     #if ( configENABLE_MPU == 1 )\r
766         #if defined( __ARMCC_VERSION )\r
767 \r
768             /* Declaration when these variable are defined in code instead of being\r
769              * exported from linker scripts. */\r
770             extern uint32_t * __syscalls_flash_start__;\r
771             extern uint32_t * __syscalls_flash_end__;\r
772         #else\r
773             /* Declaration when these variable are exported from linker scripts. */\r
774             extern uint32_t __syscalls_flash_start__[];\r
775             extern uint32_t __syscalls_flash_end__[];\r
776         #endif /* defined( __ARMCC_VERSION ) */\r
777     #endif /* configENABLE_MPU */\r
778 \r
779     uint32_t ulPC;\r
780 \r
781     #if ( configENABLE_TRUSTZONE == 1 )\r
782         uint32_t ulR0;\r
783         #if ( configENABLE_MPU == 1 )\r
784             uint32_t ulControl, ulIsTaskPrivileged;\r
785         #endif /* configENABLE_MPU */\r
786     #endif /* configENABLE_TRUSTZONE */\r
787     uint8_t ucSVCNumber;\r
788 \r
789     /* Register are stored on the stack in the following order - R0, R1, R2, R3,\r
790      * R12, LR, PC, xPSR. */\r
791     ulPC = pulCallerStackAddress[ 6 ];\r
792     ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];\r
793 \r
794     switch( ucSVCNumber )\r
795     {\r
796         #if ( configENABLE_TRUSTZONE == 1 )\r
797             case portSVC_ALLOCATE_SECURE_CONTEXT:\r
798 \r
799                 /* R0 contains the stack size passed as parameter to the\r
800                  * vPortAllocateSecureContext function. */\r
801                 ulR0 = pulCallerStackAddress[ 0 ];\r
802 \r
803                 #if ( configENABLE_MPU == 1 )\r
804                     {\r
805                         /* Read the CONTROL register value. */\r
806                         __asm volatile ( "mrs %0, control"  : "=r" ( ulControl ) );\r
807 \r
808                         /* The task that raised the SVC is privileged if Bit[0]\r
809                          * in the CONTROL register is 0. */\r
810                         ulIsTaskPrivileged = ( ( ulControl & portCONTROL_PRIVILEGED_MASK ) == 0 );\r
811 \r
812                         /* Allocate and load a context for the secure task. */\r
813                         xSecureContext = SecureContext_AllocateContext( ulR0, ulIsTaskPrivileged );\r
814                     }\r
815                 #else /* if ( configENABLE_MPU == 1 ) */\r
816                     {\r
817                         /* Allocate and load a context for the secure task. */\r
818                         xSecureContext = SecureContext_AllocateContext( ulR0 );\r
819                     }\r
820                 #endif /* configENABLE_MPU */\r
821 \r
822                 configASSERT( xSecureContext != NULL );\r
823                 SecureContext_LoadContext( xSecureContext );\r
824                 break;\r
825 \r
826             case portSVC_FREE_SECURE_CONTEXT:\r
827                 /* R0 contains the secure context handle to be freed. */\r
828                 ulR0 = pulCallerStackAddress[ 0 ];\r
829 \r
830                 /* Free the secure context. */\r
831                 SecureContext_FreeContext( ( SecureContextHandle_t ) ulR0 );\r
832                 break;\r
833         #endif /* configENABLE_TRUSTZONE */\r
834 \r
835         case portSVC_START_SCHEDULER:\r
836             #if ( configENABLE_TRUSTZONE == 1 )\r
837                 {\r
838                     /* De-prioritize the non-secure exceptions so that the\r
839                      * non-secure pendSV runs at the lowest priority. */\r
840                     SecureInit_DePrioritizeNSExceptions();\r
841 \r
842                     /* Initialize the secure context management system. */\r
843                     SecureContext_Init();\r
844                 }\r
845             #endif /* configENABLE_TRUSTZONE */\r
846 \r
847             #if ( configENABLE_FPU == 1 )\r
848                 {\r
849                     /* Setup the Floating Point Unit (FPU). */\r
850                     prvSetupFPU();\r
851                 }\r
852             #endif /* configENABLE_FPU */\r
853 \r
854             /* Setup the context of the first task so that the first task starts\r
855              * executing. */\r
856             vRestoreContextOfFirstTask();\r
857             break;\r
858 \r
859             #if ( configENABLE_MPU == 1 )\r
860                 case portSVC_RAISE_PRIVILEGE:\r
861 \r
862                     /* Only raise the privilege, if the svc was raised from any of\r
863                      * the system calls. */\r
864                     if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&\r
865                         ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )\r
866                     {\r
867                         vRaisePrivilege();\r
868                     }\r
869                     break;\r
870             #endif /* configENABLE_MPU */\r
871 \r
872         default:\r
873             /* Incorrect SVC call. */\r
874             configASSERT( pdFALSE );\r
875     }\r
876 }\r
877 /*-----------------------------------------------------------*/\r
878 /* *INDENT-OFF* */\r
879 #if ( configENABLE_MPU == 1 )\r
880     StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,\r
881                                          StackType_t * pxEndOfStack,\r
882                                          TaskFunction_t pxCode,\r
883                                          void * pvParameters,\r
884                                          BaseType_t xRunPrivileged ) /* PRIVILEGED_FUNCTION */\r
885 #else\r
886     StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,\r
887                                          StackType_t * pxEndOfStack,\r
888                                          TaskFunction_t pxCode,\r
889                                          void * pvParameters ) /* PRIVILEGED_FUNCTION */\r
890 #endif /* configENABLE_MPU */\r
891 /* *INDENT-ON* */\r
892 {\r
893     /* Simulate the stack frame as it would be created by a context switch\r
894      * interrupt. */\r
895     #if ( portPRELOAD_REGISTERS == 0 )\r
896         {\r
897             pxTopOfStack--;                                          /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
898             *pxTopOfStack = portINITIAL_XPSR;                        /* xPSR */\r
899             pxTopOfStack--;\r
900             *pxTopOfStack = ( StackType_t ) pxCode;                  /* PC */\r
901             pxTopOfStack--;\r
902             *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */\r
903             pxTopOfStack -= 5;                                       /* R12, R3, R2 and R1. */\r
904             *pxTopOfStack = ( StackType_t ) pvParameters;            /* R0 */\r
905             pxTopOfStack -= 9;                                       /* R11..R4, EXC_RETURN. */\r
906             *pxTopOfStack = portINITIAL_EXC_RETURN;\r
907 \r
908             #if ( configENABLE_MPU == 1 )\r
909                 {\r
910                     pxTopOfStack--;\r
911 \r
912                     if( xRunPrivileged == pdTRUE )\r
913                     {\r
914                         *pxTopOfStack = portINITIAL_CONTROL_PRIVILEGED; /* Slot used to hold this task's CONTROL value. */\r
915                     }\r
916                     else\r
917                     {\r
918                         *pxTopOfStack = portINITIAL_CONTROL_UNPRIVILEGED; /* Slot used to hold this task's CONTROL value. */\r
919                     }\r
920                 }\r
921             #endif /* configENABLE_MPU */\r
922 \r
923             pxTopOfStack--;\r
924             *pxTopOfStack = ( StackType_t ) pxEndOfStack; /* Slot used to hold this task's PSPLIM value. */\r
925 \r
926             #if ( configENABLE_TRUSTZONE == 1 )\r
927                 {\r
928                     pxTopOfStack--;\r
929                     *pxTopOfStack = portNO_SECURE_CONTEXT; /* Slot used to hold this task's xSecureContext value. */\r
930                 }\r
931             #endif /* configENABLE_TRUSTZONE */\r
932         }\r
933     #else /* portPRELOAD_REGISTERS */\r
934         {\r
935             pxTopOfStack--;                                          /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
936             *pxTopOfStack = portINITIAL_XPSR;                        /* xPSR */\r
937             pxTopOfStack--;\r
938             *pxTopOfStack = ( StackType_t ) pxCode;                  /* PC */\r
939             pxTopOfStack--;\r
940             *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */\r
941             pxTopOfStack--;\r
942             *pxTopOfStack = ( StackType_t ) 0x12121212UL;            /* R12 */\r
943             pxTopOfStack--;\r
944             *pxTopOfStack = ( StackType_t ) 0x03030303UL;            /* R3 */\r
945             pxTopOfStack--;\r
946             *pxTopOfStack = ( StackType_t ) 0x02020202UL;            /* R2 */\r
947             pxTopOfStack--;\r
948             *pxTopOfStack = ( StackType_t ) 0x01010101UL;            /* R1 */\r
949             pxTopOfStack--;\r
950             *pxTopOfStack = ( StackType_t ) pvParameters;            /* R0 */\r
951             pxTopOfStack--;\r
952             *pxTopOfStack = ( StackType_t ) 0x11111111UL;            /* R11 */\r
953             pxTopOfStack--;\r
954             *pxTopOfStack = ( StackType_t ) 0x10101010UL;            /* R10 */\r
955             pxTopOfStack--;\r
956             *pxTopOfStack = ( StackType_t ) 0x09090909UL;            /* R09 */\r
957             pxTopOfStack--;\r
958             *pxTopOfStack = ( StackType_t ) 0x08080808UL;            /* R08 */\r
959             pxTopOfStack--;\r
960             *pxTopOfStack = ( StackType_t ) 0x07070707UL;            /* R07 */\r
961             pxTopOfStack--;\r
962             *pxTopOfStack = ( StackType_t ) 0x06060606UL;            /* R06 */\r
963             pxTopOfStack--;\r
964             *pxTopOfStack = ( StackType_t ) 0x05050505UL;            /* R05 */\r
965             pxTopOfStack--;\r
966             *pxTopOfStack = ( StackType_t ) 0x04040404UL;            /* R04 */\r
967             pxTopOfStack--;\r
968             *pxTopOfStack = portINITIAL_EXC_RETURN;                  /* EXC_RETURN */\r
969 \r
970             #if ( configENABLE_MPU == 1 )\r
971                 {\r
972                     pxTopOfStack--;\r
973 \r
974                     if( xRunPrivileged == pdTRUE )\r
975                     {\r
976                         *pxTopOfStack = portINITIAL_CONTROL_PRIVILEGED; /* Slot used to hold this task's CONTROL value. */\r
977                     }\r
978                     else\r
979                     {\r
980                         *pxTopOfStack = portINITIAL_CONTROL_UNPRIVILEGED; /* Slot used to hold this task's CONTROL value. */\r
981                     }\r
982                 }\r
983             #endif /* configENABLE_MPU */\r
984 \r
985             pxTopOfStack--;\r
986             *pxTopOfStack = ( StackType_t ) pxEndOfStack; /* Slot used to hold this task's PSPLIM value. */\r
987 \r
988             #if ( configENABLE_TRUSTZONE == 1 )\r
989                 {\r
990                     pxTopOfStack--;\r
991                     *pxTopOfStack = portNO_SECURE_CONTEXT; /* Slot used to hold this task's xSecureContext value. */\r
992                 }\r
993             #endif /* configENABLE_TRUSTZONE */\r
994         }\r
995     #endif /* portPRELOAD_REGISTERS */\r
996 \r
997     return pxTopOfStack;\r
998 }\r
999 /*-----------------------------------------------------------*/\r
1000 \r
1001 BaseType_t xPortStartScheduler( void ) /* PRIVILEGED_FUNCTION */\r
1002 {\r
1003     /* Make PendSV, CallSV and SysTick the same priority as the kernel. */\r
1004     portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;\r
1005     portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;\r
1006 \r
1007     #if ( configENABLE_MPU == 1 )\r
1008         {\r
1009             /* Setup the Memory Protection Unit (MPU). */\r
1010             prvSetupMPU();\r
1011         }\r
1012     #endif /* configENABLE_MPU */\r
1013 \r
1014     /* Start the timer that generates the tick ISR. Interrupts are disabled\r
1015      * here already. */\r
1016     vPortSetupTimerInterrupt();\r
1017 \r
1018     /* Initialize the critical nesting count ready for the first task. */\r
1019     ulCriticalNesting = 0;\r
1020 \r
1021     /* Start the first task. */\r
1022     vStartFirstTask();\r
1023 \r
1024     /* Should never get here as the tasks will now be executing. Call the task\r
1025      * exit error function to prevent compiler warnings about a static function\r
1026      * not being called in the case that the application writer overrides this\r
1027      * functionality by defining configTASK_RETURN_ADDRESS. Call\r
1028      * vTaskSwitchContext() so link time optimization does not remove the\r
1029      * symbol. */\r
1030     vTaskSwitchContext();\r
1031     prvTaskExitError();\r
1032 \r
1033     /* Should not get here. */\r
1034     return 0;\r
1035 }\r
1036 /*-----------------------------------------------------------*/\r
1037 \r
1038 void vPortEndScheduler( void ) /* PRIVILEGED_FUNCTION */\r
1039 {\r
1040     /* Not implemented in ports where there is nothing to return to.\r
1041      * Artificially force an assert. */\r
1042     configASSERT( ulCriticalNesting == 1000UL );\r
1043 }\r
1044 /*-----------------------------------------------------------*/\r
1045 \r
1046 #if ( configENABLE_MPU == 1 )\r
1047     void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,\r
1048                                     const struct xMEMORY_REGION * const xRegions,\r
1049                                     StackType_t * pxBottomOfStack,\r
1050                                     uint32_t ulStackDepth )\r
1051     {\r
1052         uint32_t ulRegionStartAddress, ulRegionEndAddress, ulRegionNumber;\r
1053         int32_t lIndex = 0;\r
1054 \r
1055         #if defined( __ARMCC_VERSION )\r
1056 \r
1057             /* Declaration when these variable are defined in code instead of being\r
1058              * exported from linker scripts. */\r
1059             extern uint32_t * __privileged_sram_start__;\r
1060             extern uint32_t * __privileged_sram_end__;\r
1061         #else\r
1062             /* Declaration when these variable are exported from linker scripts. */\r
1063             extern uint32_t __privileged_sram_start__[];\r
1064             extern uint32_t __privileged_sram_end__[];\r
1065         #endif /* defined( __ARMCC_VERSION ) */\r
1066 \r
1067         /* Setup MAIR0. */\r
1068         xMPUSettings->ulMAIR0 = ( ( portMPU_NORMAL_MEMORY_BUFFERABLE_CACHEABLE << portMPU_MAIR_ATTR0_POS ) & portMPU_MAIR_ATTR0_MASK );\r
1069         xMPUSettings->ulMAIR0 |= ( ( portMPU_DEVICE_MEMORY_nGnRE << portMPU_MAIR_ATTR1_POS ) & portMPU_MAIR_ATTR1_MASK );\r
1070 \r
1071         /* This function is called automatically when the task is created - in\r
1072          * which case the stack region parameters will be valid.  At all other\r
1073          * times the stack parameters will not be valid and it is assumed that\r
1074          * the stack region has already been configured. */\r
1075         if( ulStackDepth > 0 )\r
1076         {\r
1077             ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;\r
1078             ulRegionEndAddress = ( uint32_t ) pxBottomOfStack + ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1;\r
1079 \r
1080             /* If the stack is within the privileged SRAM, do not protect it\r
1081              * using a separate MPU region. This is needed because privileged\r
1082              * SRAM is already protected using an MPU region and ARMv8-M does\r
1083              * not allow overlapping MPU regions. */\r
1084             if( ( ulRegionStartAddress >= ( uint32_t ) __privileged_sram_start__ ) &&\r
1085                 ( ulRegionEndAddress <= ( uint32_t ) __privileged_sram_end__ ) )\r
1086             {\r
1087                 xMPUSettings->xRegionsSettings[ 0 ].ulRBAR = 0;\r
1088                 xMPUSettings->xRegionsSettings[ 0 ].ulRLAR = 0;\r
1089             }\r
1090             else\r
1091             {\r
1092                 /* Define the region that allows access to the stack. */\r
1093                 ulRegionStartAddress &= portMPU_RBAR_ADDRESS_MASK;\r
1094                 ulRegionEndAddress &= portMPU_RLAR_ADDRESS_MASK;\r
1095 \r
1096                 xMPUSettings->xRegionsSettings[ 0 ].ulRBAR = ( ulRegionStartAddress ) |\r
1097                                                              ( portMPU_REGION_NON_SHAREABLE ) |\r
1098                                                              ( portMPU_REGION_READ_WRITE ) |\r
1099                                                              ( portMPU_REGION_EXECUTE_NEVER );\r
1100 \r
1101                 xMPUSettings->xRegionsSettings[ 0 ].ulRLAR = ( ulRegionEndAddress ) |\r
1102                                                              ( portMPU_RLAR_ATTR_INDEX0 ) |\r
1103                                                              ( portMPU_RLAR_REGION_ENABLE );\r
1104             }\r
1105         }\r
1106 \r
1107         /* User supplied configurable regions. */\r
1108         for( ulRegionNumber = 1; ulRegionNumber <= portNUM_CONFIGURABLE_REGIONS; ulRegionNumber++ )\r
1109         {\r
1110             /* If xRegions is NULL i.e. the task has not specified any MPU\r
1111              * region, the else part ensures that all the configurable MPU\r
1112              * regions are invalidated. */\r
1113             if( ( xRegions != NULL ) && ( xRegions[ lIndex ].ulLengthInBytes > 0UL ) )\r
1114             {\r
1115                 /* Translate the generic region definition contained in xRegions\r
1116                  * into the ARMv8 specific MPU settings that are then stored in\r
1117                  * xMPUSettings. */\r
1118                 ulRegionStartAddress = ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) & portMPU_RBAR_ADDRESS_MASK;\r
1119                 ulRegionEndAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1;\r
1120                 ulRegionEndAddress &= portMPU_RLAR_ADDRESS_MASK;\r
1121 \r
1122                 /* Start address. */\r
1123                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR = ( ulRegionStartAddress ) |\r
1124                                                                           ( portMPU_REGION_NON_SHAREABLE );\r
1125 \r
1126                 /* RO/RW. */\r
1127                 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_READ_ONLY ) != 0 )\r
1128                 {\r
1129                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_READ_ONLY );\r
1130                 }\r
1131                 else\r
1132                 {\r
1133                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_READ_WRITE );\r
1134                 }\r
1135 \r
1136                 /* XN. */\r
1137                 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_EXECUTE_NEVER ) != 0 )\r
1138                 {\r
1139                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR |= ( portMPU_REGION_EXECUTE_NEVER );\r
1140                 }\r
1141 \r
1142                 /* End Address. */\r
1143                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR = ( ulRegionEndAddress ) |\r
1144                                                                           ( portMPU_RLAR_REGION_ENABLE );\r
1145 \r
1146                 /* Normal memory/ Device memory. */\r
1147                 if( ( xRegions[ lIndex ].ulParameters & tskMPU_REGION_DEVICE_MEMORY ) != 0 )\r
1148                 {\r
1149                     /* Attr1 in MAIR0 is configured as device memory. */\r
1150                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR |= portMPU_RLAR_ATTR_INDEX1;\r
1151                 }\r
1152                 else\r
1153                 {\r
1154                     /* Attr1 in MAIR0 is configured as normal memory. */\r
1155                     xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR |= portMPU_RLAR_ATTR_INDEX0;\r
1156                 }\r
1157             }\r
1158             else\r
1159             {\r
1160                 /* Invalidate the region. */\r
1161                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRBAR = 0UL;\r
1162                 xMPUSettings->xRegionsSettings[ ulRegionNumber ].ulRLAR = 0UL;\r
1163             }\r
1164 \r
1165             lIndex++;\r
1166         }\r
1167     }\r
1168 #endif /* configENABLE_MPU */\r
1169 /*-----------------------------------------------------------*/\r
1170 \r
1171 BaseType_t xPortIsInsideInterrupt( void )\r
1172 {\r
1173     uint32_t ulCurrentInterrupt;\r
1174     BaseType_t xReturn;\r
1175 \r
1176     /* Obtain the number of the currently executing interrupt. Interrupt Program\r
1177      * Status Register (IPSR) holds the exception number of the currently-executing\r
1178      * exception or zero for Thread mode.*/\r
1179     __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );\r
1180 \r
1181     if( ulCurrentInterrupt == 0 )\r
1182     {\r
1183         xReturn = pdFALSE;\r
1184     }\r
1185     else\r
1186     {\r
1187         xReturn = pdTRUE;\r
1188     }\r
1189 \r
1190     return xReturn;\r
1191 }\r
1192 /*-----------------------------------------------------------*/\r