]> begriffs open source - freertos/blob - portable/IAR/ARM_CM4F_MPU/port.c
Add SPDX-License-Identifier: MIT to MIT licensed files.
[freertos] / portable / IAR / ARM_CM4F_MPU / 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 /*-----------------------------------------------------------\r
30 * Implementation of functions defined in portable.h for the ARM CM4F MPU port.\r
31 *----------------------------------------------------------*/\r
32 \r
33 /* IAR includes. */\r
34 #include <intrinsics.h>\r
35 \r
36 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
37  * all the API functions to use the MPU wrappers.  That should only be done when\r
38  * task.h is included from an application file. */\r
39 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
40 \r
41 /* Scheduler includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 \r
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
46 \r
47 #ifndef __ARMVFP__\r
48     #error This port can only be used when the project options are configured to enable hardware floating point support.\r
49 #endif\r
50 \r
51 #if ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )\r
52     #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.  See http: /*www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
53 #endif\r
54 \r
55 #ifndef configSYSTICK_CLOCK_HZ\r
56     #define configSYSTICK_CLOCK_HZ      configCPU_CLOCK_HZ\r
57     /* Ensure the SysTick is clocked at the same frequency as the core. */\r
58     #define portNVIC_SYSTICK_CLK_BIT    ( 1UL << 2UL )\r
59 #else\r
60 \r
61 /* The way the SysTick is clocked is not modified in case it is not the same\r
62  * as the core. */\r
63     #define portNVIC_SYSTICK_CLK_BIT    ( 0 )\r
64 #endif\r
65 \r
66 /* Constants required to manipulate the core.  Registers first... */\r
67 #define portNVIC_SYSTICK_CTRL_REG                 ( *( ( volatile uint32_t * ) 0xe000e010 ) )\r
68 #define portNVIC_SYSTICK_LOAD_REG                 ( *( ( volatile uint32_t * ) 0xe000e014 ) )\r
69 #define portNVIC_SYSTICK_CURRENT_VALUE_REG        ( *( ( volatile uint32_t * ) 0xe000e018 ) )\r
70 #define portNVIC_SHPR3_REG                        ( *( ( volatile uint32_t * ) 0xe000ed20 ) )\r
71 #define portNVIC_SHPR2_REG                        ( *( ( volatile uint32_t * ) 0xe000ed1c ) )\r
72 #define portNVIC_SYS_CTRL_STATE_REG               ( *( ( volatile uint32_t * ) 0xe000ed24 ) )\r
73 #define portNVIC_MEM_FAULT_ENABLE                 ( 1UL << 16UL )\r
74 \r
75 /* Constants required to access and manipulate the MPU. */\r
76 #define portMPU_TYPE_REG                          ( *( ( volatile uint32_t * ) 0xe000ed90 ) )\r
77 #define portMPU_REGION_BASE_ADDRESS_REG           ( *( ( volatile uint32_t * ) 0xe000ed9C ) )\r
78 #define portMPU_REGION_ATTRIBUTE_REG              ( *( ( volatile uint32_t * ) 0xe000edA0 ) )\r
79 #define portMPU_CTRL_REG                          ( *( ( volatile uint32_t * ) 0xe000ed94 ) )\r
80 #define portEXPECTED_MPU_TYPE_VALUE               ( portTOTAL_NUM_REGIONS << 8UL )\r
81 #define portMPU_ENABLE                            ( 0x01UL )\r
82 #define portMPU_BACKGROUND_ENABLE                 ( 1UL << 2UL )\r
83 #define portPRIVILEGED_EXECUTION_START_ADDRESS    ( 0UL )\r
84 #define portMPU_REGION_VALID                      ( 0x10UL )\r
85 #define portMPU_REGION_ENABLE                     ( 0x01UL )\r
86 #define portPERIPHERALS_START_ADDRESS             0x40000000UL\r
87 #define portPERIPHERALS_END_ADDRESS               0x5FFFFFFFUL\r
88 \r
89 /* ...then bits in the registers. */\r
90 #define portNVIC_SYSTICK_INT_BIT                  ( 1UL << 1UL )\r
91 #define portNVIC_SYSTICK_ENABLE_BIT               ( 1UL << 0UL )\r
92 #define portNVIC_SYSTICK_COUNT_FLAG_BIT           ( 1UL << 16UL )\r
93 #define portNVIC_PENDSVCLEAR_BIT                  ( 1UL << 27UL )\r
94 #define portNVIC_PEND_SYSTICK_CLEAR_BIT           ( 1UL << 25UL )\r
95 \r
96 /* Constants used to detect a Cortex-M7 r0p1 core, which should use the ARM_CM7\r
97  * r0p1 port. */\r
98 #define portCPUID                                 ( *( ( volatile uint32_t * ) 0xE000ed00 ) )\r
99 #define portCORTEX_M7_r0p1_ID                     ( 0x410FC271UL )\r
100 #define portCORTEX_M7_r0p0_ID                     ( 0x410FC270UL )\r
101 \r
102 #define portNVIC_PENDSV_PRI                       ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )\r
103 #define portNVIC_SYSTICK_PRI                      ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )\r
104 #define portNVIC_SVC_PRI                          ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )\r
105 \r
106 /* Constants required to check the validity of an interrupt priority. */\r
107 #define portFIRST_USER_INTERRUPT_NUMBER           ( 16 )\r
108 #define portNVIC_IP_REGISTERS_OFFSET_16           ( 0xE000E3F0 )\r
109 #define portAIRCR_REG                             ( *( ( volatile uint32_t * ) 0xE000ED0C ) )\r
110 #define portMAX_8_BIT_VALUE                       ( ( uint8_t ) 0xff )\r
111 #define portTOP_BIT_OF_BYTE                       ( ( uint8_t ) 0x80 )\r
112 #define portMAX_PRIGROUP_BITS                     ( ( uint8_t ) 7 )\r
113 #define portPRIORITY_GROUP_MASK                   ( 0x07UL << 8UL )\r
114 #define portPRIGROUP_SHIFT                        ( 8UL )\r
115 \r
116 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */\r
117 #define portVECTACTIVE_MASK                       ( 0xFFUL )\r
118 \r
119 /* Constants required to manipulate the VFP. */\r
120 #define portFPCCR                                 ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */\r
121 #define portASPEN_AND_LSPEN_BITS                  ( 0x3UL << 30UL )\r
122 \r
123 /* Constants required to set up the initial stack. */\r
124 #define portINITIAL_XPSR                          ( 0x01000000 )\r
125 #define portINITIAL_EXC_RETURN                    ( 0xfffffffd )\r
126 #define portINITIAL_CONTROL_IF_UNPRIVILEGED       ( 0x03 )\r
127 #define portINITIAL_CONTROL_IF_PRIVILEGED         ( 0x02 )\r
128 \r
129 /* Offsets in the stack to the parameters when inside the SVC handler. */\r
130 #define portOFFSET_TO_PC                          ( 6 )\r
131 \r
132 /* The systick is a 24-bit counter. */\r
133 #define portMAX_24_BIT_NUMBER                     ( 0xffffffUL )\r
134 \r
135 /* A fiddle factor to estimate the number of SysTick counts that would have\r
136  * occurred while the SysTick counter is stopped during tickless idle\r
137  * calculations. */\r
138 #define portMISSED_COUNTS_FACTOR                  ( 45UL )\r
139 \r
140 /* For strict compliance with the Cortex-M spec the task start address should\r
141  * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */\r
142 #define portSTART_ADDRESS_MASK                    ( ( StackType_t ) 0xfffffffeUL )\r
143 \r
144 /*\r
145  * Configure a number of standard MPU regions that are used by all tasks.\r
146  */\r
147 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;\r
148 \r
149 /*\r
150  * Return the smallest MPU region size that a given number of bytes will fit\r
151  * into.  The region size is returned as the value that should be programmed\r
152  * into the region attribute register for that region.\r
153  */\r
154 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;\r
155 \r
156 /*\r
157  * Setup the timer to generate the tick interrupts.  The implementation in this\r
158  * file is weak to allow application writers to change the timer used to\r
159  * generate the tick interrupt.\r
160  */\r
161 void vPortSetupTimerInterrupt( void );\r
162 \r
163 /*\r
164  * Exception handlers.\r
165  */\r
166 void xPortSysTickHandler( void ) PRIVILEGED_FUNCTION;\r
167 \r
168 /*\r
169  * Start first task is a separate function so it can be tested in isolation.\r
170  */\r
171 extern void vPortStartFirstTask( void ) PRIVILEGED_FUNCTION;\r
172 \r
173 /*\r
174  * Turn the VFP on.\r
175  */\r
176 extern void vPortEnableVFP( void );\r
177 \r
178 /*\r
179  * The C portion of the SVC handler.\r
180  */\r
181 void vPortSVCHandler_C( uint32_t * pulParam );\r
182 \r
183 /*\r
184  * Called from the SVC handler used to start the scheduler.\r
185  */\r
186 extern void vPortRestoreContextOfFirstTask( void ) PRIVILEGED_FUNCTION;\r
187 \r
188 /**\r
189  * @brief Calls the port specific code to raise the privilege.\r
190  *\r
191  * @return pdFALSE if privilege was raised, pdTRUE otherwise.\r
192  */\r
193 extern BaseType_t xPortRaisePrivilege( void );\r
194 \r
195 /**\r
196  * @brief If xRunningPrivileged is not pdTRUE, calls the port specific\r
197  * code to reset the privilege, otherwise does nothing.\r
198  */\r
199 extern void vPortResetPrivilege( BaseType_t xRunningPrivileged );\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /* Each task maintains its own interrupt status in the critical nesting\r
203  * variable. */\r
204 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
205 \r
206 /*\r
207  * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure\r
208  * FreeRTOS API functions are not called from interrupts that have been assigned\r
209  * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
210  */\r
211 #if ( configASSERT_DEFINED == 1 )\r
212     static uint8_t ucMaxSysCallPriority = 0;\r
213     static uint32_t ulMaxPRIGROUPValue = 0;\r
214     static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;\r
215 #endif /* configASSERT_DEFINED */\r
216 \r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /*\r
220  * See header file for description.\r
221  */\r
222 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,\r
223                                      TaskFunction_t pxCode,\r
224                                      void * pvParameters,\r
225                                      BaseType_t xRunPrivileged )\r
226 {\r
227     /* Simulate the stack frame as it would be created by a context switch\r
228      * interrupt. */\r
229 \r
230     /* Offset added to account for the way the MCU uses the stack on entry/exit\r
231      * of interrupts, and to ensure alignment. */\r
232     pxTopOfStack--;\r
233 \r
234     *pxTopOfStack = portINITIAL_XPSR;                                    /* xPSR */\r
235     pxTopOfStack--;\r
236     *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */\r
237     pxTopOfStack--;\r
238     *pxTopOfStack = ( StackType_t ) 0;                                   /* LR */\r
239 \r
240     /* Save code space by skipping register initialisation. */\r
241     pxTopOfStack -= 5;                            /* R12, R3, R2 and R1. */\r
242     *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
243 \r
244     /* A save method is being used that requires each task to maintain its\r
245      * own exec return value. */\r
246     pxTopOfStack--;\r
247     *pxTopOfStack = portINITIAL_EXC_RETURN;\r
248 \r
249     pxTopOfStack -= 9; /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
250 \r
251     if( xRunPrivileged == pdTRUE )\r
252     {\r
253         *pxTopOfStack = portINITIAL_CONTROL_IF_PRIVILEGED;\r
254     }\r
255     else\r
256     {\r
257         *pxTopOfStack = portINITIAL_CONTROL_IF_UNPRIVILEGED;\r
258     }\r
259 \r
260     return pxTopOfStack;\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vPortSVCHandler_C( uint32_t * pulParam )\r
265 {\r
266     uint8_t ucSVCNumber;\r
267     uint32_t ulPC;\r
268 \r
269     #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )\r
270         extern uint32_t __syscalls_flash_start__[];\r
271         extern uint32_t __syscalls_flash_end__[];\r
272     #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */\r
273 \r
274     /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first\r
275      * argument (r0) is pulParam[ 0 ]. */\r
276     ulPC = pulParam[ portOFFSET_TO_PC ];\r
277     ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];\r
278 \r
279     switch( ucSVCNumber )\r
280     {\r
281         case portSVC_START_SCHEDULER:\r
282             portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;\r
283             vPortRestoreContextOfFirstTask();\r
284             break;\r
285 \r
286         case portSVC_YIELD:\r
287             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
288 \r
289             /* Barriers are normally not required\r
290              * but do ensure the code is completely\r
291              * within the specified behaviour for the\r
292              * architecture. */\r
293             __asm volatile ( "dsb" ::: "memory" );\r
294             __asm volatile ( "isb" );\r
295 \r
296             break;\r
297 \r
298             #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )\r
299                 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the\r
300                                                * svc was raised from any of the\r
301                                                * system calls. */\r
302 \r
303                     if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&\r
304                         ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )\r
305                     {\r
306                         __asm volatile\r
307                         (\r
308                             "   mrs r1, control         \n"/* Obtain current control value. */\r
309                             "   bic r1, r1, #1          \n"/* Set privilege bit. */\r
310                             "   msr control, r1         \n"/* Write back new control value. */\r
311                             ::: "r1", "memory"\r
312                         );\r
313                     }\r
314 \r
315                     break;\r
316             #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */\r
317                 case portSVC_RAISE_PRIVILEGE:\r
318                     __asm volatile\r
319                     (\r
320                         "       mrs r1, control         \n"/* Obtain current control value. */\r
321                         "       bic r1, r1, #1          \n"/* Set privilege bit. */\r
322                         "       msr control, r1         \n"/* Write back new control value. */\r
323                         ::: "r1", "memory"\r
324                     );\r
325                     break;\r
326                     #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */\r
327 \r
328                 default: /* Unknown SVC call. */\r
329                     break;\r
330     }\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 /*\r
335  * See header file for description.\r
336  */\r
337 BaseType_t xPortStartScheduler( void )\r
338 {\r
339     /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
340      * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
341     configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
342 \r
343     /* This port can be used on all revisions of the Cortex-M7 core other than\r
344      * the r0p1 parts.  r0p1 parts should use the port from the\r
345      * /source/portable/GCC/ARM_CM7/r0p1 directory. */\r
346     configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );\r
347     configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );\r
348 \r
349     #if ( configASSERT_DEFINED == 1 )\r
350         {\r
351             volatile uint32_t ulOriginalPriority;\r
352             volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );\r
353             volatile uint8_t ucMaxPriorityValue;\r
354 \r
355             /* Determine the maximum priority from which ISR safe FreeRTOS API\r
356              * functions can be called.  ISR safe functions are those that end in\r
357              * "FromISR".  FreeRTOS maintains separate thread and ISR API functions to\r
358              * ensure interrupt entry is as fast and simple as possible.\r
359              *\r
360              * Save the interrupt priority value that is about to be clobbered. */\r
361             ulOriginalPriority = *pucFirstUserPriorityRegister;\r
362 \r
363             /* Determine the number of priority bits available.  First write to all\r
364              * possible bits. */\r
365             *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;\r
366 \r
367             /* Read the value back to see how many bits stuck. */\r
368             ucMaxPriorityValue = *pucFirstUserPriorityRegister;\r
369 \r
370             /* Use the same mask on the maximum system call priority. */\r
371             ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;\r
372 \r
373             /* Calculate the maximum acceptable priority group value for the number\r
374              * of bits read back. */\r
375             ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;\r
376 \r
377             while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )\r
378             {\r
379                 ulMaxPRIGROUPValue--;\r
380                 ucMaxPriorityValue <<= ( uint8_t ) 0x01;\r
381             }\r
382 \r
383             #ifdef __NVIC_PRIO_BITS\r
384                 {\r
385                     /* Check the CMSIS configuration that defines the number of\r
386                      * priority bits matches the number of priority bits actually queried\r
387                      * from the hardware. */\r
388                     configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );\r
389                 }\r
390             #endif\r
391 \r
392             #ifdef configPRIO_BITS\r
393                 {\r
394                     /* Check the FreeRTOS configuration that defines the number of\r
395                      * priority bits matches the number of priority bits actually queried\r
396                      * from the hardware. */\r
397                     configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );\r
398                 }\r
399             #endif\r
400 \r
401             /* Shift the priority group value back to its position within the AIRCR\r
402              * register. */\r
403             ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;\r
404             ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;\r
405 \r
406             /* Restore the clobbered interrupt priority register to its original\r
407              * value. */\r
408             *pucFirstUserPriorityRegister = ulOriginalPriority;\r
409         }\r
410     #endif /* conifgASSERT_DEFINED */\r
411 \r
412     /* Make PendSV and SysTick the lowest priority interrupts. */\r
413     portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;\r
414     portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;\r
415 \r
416     /* Configure the regions in the MPU that are common to all tasks. */\r
417     prvSetupMPU();\r
418 \r
419     /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
420      * here already. */\r
421     vPortSetupTimerInterrupt();\r
422 \r
423     /* Initialise the critical nesting count ready for the first task. */\r
424     uxCriticalNesting = 0;\r
425 \r
426     /* Ensure the VFP is enabled - it should be anyway. */\r
427     vPortEnableVFP();\r
428 \r
429     /* Lazy save always. */\r
430     *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
431 \r
432     /* Start the first task. */\r
433     vPortStartFirstTask();\r
434 \r
435     /* Should not get here! */\r
436     return 0;\r
437 }\r
438 /*-----------------------------------------------------------*/\r
439 \r
440 void vPortEndScheduler( void )\r
441 {\r
442     /* Not implemented in ports where there is nothing to return to.\r
443      * Artificially force an assert. */\r
444     configASSERT( uxCriticalNesting == 1000UL );\r
445 }\r
446 /*-----------------------------------------------------------*/\r
447 \r
448 void vPortEnterCritical( void )\r
449 {\r
450     BaseType_t xRunningPrivileged = xPortRaisePrivilege();\r
451 \r
452     portDISABLE_INTERRUPTS();\r
453     uxCriticalNesting++;\r
454 \r
455     vPortResetPrivilege( xRunningPrivileged );\r
456 \r
457     /* This is not the interrupt safe version of the enter critical function so\r
458      * assert() if it is being called from an interrupt context.  Only API\r
459      * functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
460      * the critical nesting count is 1 to protect against recursive calls if the\r
461      * assert function also uses a critical section. */\r
462     if( uxCriticalNesting == 1 )\r
463     {\r
464         configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );\r
465     }\r
466 }\r
467 /*-----------------------------------------------------------*/\r
468 \r
469 void vPortExitCritical( void )\r
470 {\r
471     BaseType_t xRunningPrivileged = xPortRaisePrivilege();\r
472 \r
473     configASSERT( uxCriticalNesting );\r
474 \r
475     uxCriticalNesting--;\r
476 \r
477     if( uxCriticalNesting == 0 )\r
478     {\r
479         portENABLE_INTERRUPTS();\r
480     }\r
481 \r
482     vPortResetPrivilege( xRunningPrivileged );\r
483 }\r
484 /*-----------------------------------------------------------*/\r
485 \r
486 void xPortSysTickHandler( void )\r
487 {\r
488     /* The SysTick runs at the lowest interrupt priority, so when this interrupt\r
489      * executes all interrupts must be unmasked.  There is therefore no need to\r
490      * save and then restore the interrupt mask value as its value is already\r
491      * known. */\r
492     portDISABLE_INTERRUPTS();\r
493     {\r
494         /* Increment the RTOS tick. */\r
495         if( xTaskIncrementTick() != pdFALSE )\r
496         {\r
497             /* A context switch is required.  Context switching is performed in\r
498              * the PendSV interrupt.  Pend the PendSV interrupt. */\r
499             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
500         }\r
501     }\r
502     portENABLE_INTERRUPTS();\r
503 }\r
504 /*-----------------------------------------------------------*/\r
505 \r
506 /*\r
507  * Setup the systick timer to generate the tick interrupts at the required\r
508  * frequency.\r
509  */\r
510 __weak void vPortSetupTimerInterrupt( void )\r
511 {\r
512     /* Stop and clear the SysTick. */\r
513     portNVIC_SYSTICK_CTRL_REG = 0UL;\r
514     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
515 \r
516     /* Configure SysTick to interrupt at the requested rate. */\r
517     portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
518     portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );\r
519 }\r
520 /*-----------------------------------------------------------*/\r
521 \r
522 static void prvSetupMPU( void )\r
523 {\r
524     extern uint32_t __privileged_functions_start__[];\r
525     extern uint32_t __privileged_functions_end__[];\r
526     extern uint32_t __FLASH_segment_start__[];\r
527     extern uint32_t __FLASH_segment_end__[];\r
528     extern uint32_t __privileged_data_start__[];\r
529     extern uint32_t __privileged_data_end__[];\r
530 \r
531     /* The only permitted number of regions are 8 or 16. */\r
532     configASSERT( ( portTOTAL_NUM_REGIONS == 8 ) || ( portTOTAL_NUM_REGIONS == 16 ) );\r
533 \r
534     /* Ensure that the configTOTAL_MPU_REGIONS is configured correctly. */\r
535     configASSERT( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE );\r
536 \r
537     /* Check the expected MPU is present. */\r
538     if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )\r
539     {\r
540         /* First setup the unprivileged flash for unprivileged read only access. */\r
541         portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */\r
542                                           ( portMPU_REGION_VALID ) |\r
543                                           ( portUNPRIVILEGED_FLASH_REGION );\r
544 \r
545         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |\r
546                                        ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |\r
547                                        ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |\r
548                                        ( portMPU_REGION_ENABLE );\r
549 \r
550         /* Setup the privileged flash for privileged only access.  This is where\r
551          * the kernel code is placed. */\r
552         portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */\r
553                                           ( portMPU_REGION_VALID ) |\r
554                                           ( portPRIVILEGED_FLASH_REGION );\r
555 \r
556         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |\r
557                                        ( ( configTEX_S_C_B_FLASH & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |\r
558                                        ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |\r
559                                        ( portMPU_REGION_ENABLE );\r
560 \r
561         /* Setup the privileged data RAM region.  This is where the kernel data\r
562          * is placed. */\r
563         portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */\r
564                                           ( portMPU_REGION_VALID ) |\r
565                                           ( portPRIVILEGED_RAM_REGION );\r
566 \r
567         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
568                                        ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |\r
569                                        prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |\r
570                                        ( portMPU_REGION_ENABLE );\r
571 \r
572         /* By default allow everything to access the general peripherals.  The\r
573          * system peripherals and registers are protected. */\r
574         portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |\r
575                                           ( portMPU_REGION_VALID ) |\r
576                                           ( portGENERAL_PERIPHERALS_REGION );\r
577 \r
578         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |\r
579                                        ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |\r
580                                        ( portMPU_REGION_ENABLE );\r
581 \r
582         /* Enable the memory fault exception. */\r
583         portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;\r
584 \r
585         /* Enable the MPU with the background region configured. */\r
586         portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );\r
587     }\r
588 }\r
589 /*-----------------------------------------------------------*/\r
590 \r
591 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )\r
592 {\r
593     uint32_t ulRegionSize, ulReturnValue = 4;\r
594 \r
595     /* 32 is the smallest region size, 31 is the largest valid value for\r
596      * ulReturnValue. */\r
597     for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )\r
598     {\r
599         if( ulActualSizeInBytes <= ulRegionSize )\r
600         {\r
601             break;\r
602         }\r
603         else\r
604         {\r
605             ulReturnValue++;\r
606         }\r
607     }\r
608 \r
609     /* Shift the code by one before returning so it can be written directly\r
610      * into the the correct bit position of the attribute register. */\r
611     return( ulReturnValue << 1UL );\r
612 }\r
613 /*-----------------------------------------------------------*/\r
614 \r
615 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,\r
616                                 const struct xMEMORY_REGION * const xRegions,\r
617                                 StackType_t * pxBottomOfStack,\r
618                                 uint32_t ulStackDepth )\r
619 {\r
620     extern uint32_t __SRAM_segment_start__[];\r
621     extern uint32_t __SRAM_segment_end__[];\r
622     extern uint32_t __privileged_data_start__[];\r
623     extern uint32_t __privileged_data_end__[];\r
624     int32_t lIndex;\r
625     uint32_t ul;\r
626 \r
627     if( xRegions == NULL )\r
628     {\r
629         /* No MPU regions are specified so allow access to all RAM. */\r
630         xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =\r
631             ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */\r
632             ( portMPU_REGION_VALID ) |\r
633             ( portSTACK_REGION );\r
634 \r
635         xMPUSettings->xRegion[ 0 ].ulRegionAttribute =\r
636             ( portMPU_REGION_READ_WRITE ) |\r
637             ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |\r
638             ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |\r
639             ( portMPU_REGION_ENABLE );\r
640 \r
641         /* Re-instate the privileged only RAM region as xRegion[ 0 ] will have\r
642          * just removed the privileged only parameters. */\r
643         xMPUSettings->xRegion[ 1 ].ulRegionBaseAddress =\r
644             ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */\r
645             ( portMPU_REGION_VALID ) |\r
646             ( portSTACK_REGION + 1 );\r
647 \r
648         xMPUSettings->xRegion[ 1 ].ulRegionAttribute =\r
649             ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
650             ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |\r
651             prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |\r
652             ( portMPU_REGION_ENABLE );\r
653 \r
654         /* Invalidate all other regions. */\r
655         for( ul = 2; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )\r
656         {\r
657             xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;\r
658             xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;\r
659         }\r
660     }\r
661     else\r
662     {\r
663         /* This function is called automatically when the task is created - in\r
664          * which case the stack region parameters will be valid.  At all other\r
665          * times the stack parameters will not be valid and it is assumed that the\r
666          * stack region has already been configured. */\r
667         if( ulStackDepth > 0 )\r
668         {\r
669             /* Define the region that allows access to the stack. */\r
670             xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =\r
671                 ( ( uint32_t ) pxBottomOfStack ) |\r
672                 ( portMPU_REGION_VALID ) |\r
673                 ( portSTACK_REGION ); /* Region number. */\r
674 \r
675             xMPUSettings->xRegion[ 0 ].ulRegionAttribute =\r
676                 ( portMPU_REGION_READ_WRITE ) | /* Read and write. */\r
677                 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |\r
678                 ( ( configTEX_S_C_B_SRAM & portMPU_RASR_TEX_S_C_B_MASK ) << portMPU_RASR_TEX_S_C_B_LOCATION ) |\r
679                 ( portMPU_REGION_ENABLE );\r
680         }\r
681 \r
682         lIndex = 0;\r
683 \r
684         for( ul = 1; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )\r
685         {\r
686             if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )\r
687             {\r
688                 /* Translate the generic region definition contained in\r
689                  * xRegions into the CM4 specific MPU settings that are then\r
690                  * stored in xMPUSettings. */\r
691                 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =\r
692                     ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |\r
693                     ( portMPU_REGION_VALID ) |\r
694                     ( portSTACK_REGION + ul ); /* Region number. */\r
695 \r
696                 xMPUSettings->xRegion[ ul ].ulRegionAttribute =\r
697                     ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |\r
698                     ( xRegions[ lIndex ].ulParameters ) |\r
699                     ( portMPU_REGION_ENABLE );\r
700             }\r
701             else\r
702             {\r
703                 /* Invalidate the region. */\r
704                 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;\r
705                 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;\r
706             }\r
707 \r
708             lIndex++;\r
709         }\r
710     }\r
711 }\r
712 /*-----------------------------------------------------------*/\r
713 \r
714 #if ( configASSERT_DEFINED == 1 )\r
715 \r
716     void vPortValidateInterruptPriority( void )\r
717     {\r
718         uint32_t ulCurrentInterrupt;\r
719         uint8_t ucCurrentPriority;\r
720 \r
721         /* Obtain the number of the currently executing interrupt. */\r
722         __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );\r
723 \r
724         /* Is the interrupt number a user defined interrupt? */\r
725         if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )\r
726         {\r
727             /* Look up the interrupt's priority. */\r
728             ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];\r
729 \r
730             /* The following assertion will fail if a service routine (ISR) for\r
731              * an interrupt that has been assigned a priority above\r
732              * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
733              * function.  ISR safe FreeRTOS API functions must *only* be called\r
734              * from interrupts that have been assigned a priority at or below\r
735              * configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
736              *\r
737              * Numerically low interrupt priority numbers represent logically high\r
738              * interrupt priorities, therefore the priority of the interrupt must\r
739              * be set to a value equal to or numerically *higher* than\r
740              * configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
741              *\r
742              * Interrupts that  use the FreeRTOS API must not be left at their\r
743              * default priority of      zero as that is the highest possible priority,\r
744              * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
745              * and      therefore also guaranteed to be invalid.\r
746              *\r
747              * FreeRTOS maintains separate thread and ISR API functions to ensure\r
748              * interrupt entry is as fast and simple as possible.\r
749              *\r
750              * The following links provide detailed information:\r
751              * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html\r
752              * https://www.FreeRTOS.org/FAQHelp.html */\r
753             configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );\r
754         }\r
755 \r
756         /* Priority grouping:  The interrupt controller (NVIC) allows the bits\r
757          * that define each interrupt's priority to be split between bits that\r
758          * define the interrupt's pre-emption priority bits and bits that define\r
759          * the interrupt's sub-priority.  For simplicity all bits must be defined\r
760          * to be pre-emption priority bits.  The following assertion will fail if\r
761          * this is not the case (if some bits represent a sub-priority).\r
762          *\r
763          * If the application only uses CMSIS libraries for interrupt\r
764          * configuration then the correct setting can be achieved on all Cortex-M\r
765          * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the\r
766          * scheduler.  Note however that some vendor specific peripheral libraries\r
767          * assume a non-zero priority group setting, in which cases using a value\r
768          * of zero will result in unpredictable behaviour. */\r
769         configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );\r
770     }\r
771 \r
772 #endif /* configASSERT_DEFINED */\r
773 /*-----------------------------------------------------------*/\r