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