]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ARM_CM4F_MPU/port.c
Updates to CM4_MPU IAR port
[freertos] / FreeRTOS / Source / portable / IAR / ARM_CM4F_MPU / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the ARM CM4F port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* IAR includes. */\r
33 #include <intrinsics.h>\r
34 \r
35 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
36  * all the API functions to use the MPU wrappers.  That should only be done when\r
37  * task.h is included from an application file. */\r
38 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
39 \r
40 /* Scheduler includes. */\r
41 #include "FreeRTOS.h"\r
42 #include "task.h"\r
43 \r
44 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
45 \r
46 #ifndef __ARMVFP__\r
47         #error This port can only be used when the project options are configured to enable hardware floating point support.\r
48 #endif\r
49 \r
50 #if( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )\r
51         #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.  See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html\r
52 #endif\r
53 \r
54 #ifndef configSYSTICK_CLOCK_HZ\r
55         #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ\r
56         /* Ensure the SysTick is clocked at the same frequency as the core. */\r
57         #define portNVIC_SYSTICK_CLK_BIT        ( 1UL << 2UL )\r
58 #else\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_SYSPRI2_REG                                    ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )\r
69 #define portNVIC_SYSPRI1_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                             ( 8UL << 8UL ) /* 8 regions, unified. */\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, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )\r
221 {\r
222         /* Simulate the stack frame as it would be created by a context switch\r
223          * interrupt. */\r
224 \r
225         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
226          * of interrupts, and to ensure alignment. */\r
227         pxTopOfStack--;\r
228 \r
229         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
230         pxTopOfStack--;\r
231         *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK;    /* PC */\r
232         pxTopOfStack--;\r
233         *pxTopOfStack = ( StackType_t ) 0;      /* LR */\r
234 \r
235         /* Save code space by skipping register initialisation. */\r
236         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
237         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
238 \r
239         /* A save method is being used that requires each task to maintain its\r
240          * own exec return value. */\r
241         pxTopOfStack--;\r
242         *pxTopOfStack = portINITIAL_EXC_RETURN;\r
243 \r
244         pxTopOfStack -= 9;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
245 \r
246         if( xRunPrivileged == pdTRUE )\r
247         {\r
248                 *pxTopOfStack = portINITIAL_CONTROL_IF_PRIVILEGED;\r
249         }\r
250         else\r
251         {\r
252                 *pxTopOfStack = portINITIAL_CONTROL_IF_UNPRIVILEGED;\r
253         }\r
254 \r
255         return pxTopOfStack;\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 void vPortSVCHandler_C( uint32_t *pulParam )\r
260 {\r
261 uint8_t ucSVCNumber;\r
262 uint32_t ulPC;\r
263 #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )\r
264         extern uint32_t __syscalls_flash_start__[];\r
265         extern uint32_t __syscalls_flash_end__[];\r
266 #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */\r
267 \r
268         /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR. The first\r
269          * argument (r0) is pulParam[ 0 ]. */\r
270         ulPC = pulParam[ portOFFSET_TO_PC ];\r
271         ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];\r
272         switch( ucSVCNumber )\r
273         {\r
274                 case portSVC_START_SCHEDULER    :       portNVIC_SYSPRI1_REG |= portNVIC_SVC_PRI;\r
275                                                                                         vPortRestoreContextOfFirstTask();\r
276                                                                                         break;\r
277 \r
278                 case portSVC_YIELD                              :       portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
279                                                                                         /* Barriers are normally not required\r
280                                                                                          * but do ensure the code is completely\r
281                                                                                          * within the specified behaviour for the\r
282                                                                                          * architecture. */\r
283                                                                                         __asm volatile( "dsb" ::: "memory" );\r
284                                                                                         __asm volatile( "isb" );\r
285 \r
286                                                                                         break;\r
287 \r
288         #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )\r
289                 case portSVC_RAISE_PRIVILEGE    :       /* Only raise the privilege, if the\r
290                                                                                          * svc was raised from any of the\r
291                                                                                          * system calls. */\r
292                                                                                         if( ulPC >= ( uint32_t ) __syscalls_flash_start__ &&\r
293                                                                                                 ulPC <= ( uint32_t ) __syscalls_flash_end__ )\r
294                                                                                         {\r
295                                                                                                 __asm volatile\r
296                                                                                                 (\r
297                                                                                                         "       mrs r1, control         \n" /* Obtain current control value. */\r
298                                                                                                         "       bic r1, r1, #1          \n" /* Set privilege bit. */\r
299                                                                                                         "       msr control, r1         \n" /* Write back new control value. */\r
300                                                                                                         ::: "r1", "memory"\r
301                                                                                                 );\r
302                                                                                         }\r
303                                                                                         break;\r
304         #else\r
305                 case portSVC_RAISE_PRIVILEGE    :       __asm volatile\r
306                                                                                         (\r
307                                                                                                 "       mrs r1, control         \n" /* Obtain current control value. */\r
308                                                                                                 "       bic r1, r1, #1          \n" /* Set privilege bit. */\r
309                                                                                                 "       msr control, r1         \n" /* Write back new control value. */\r
310                                                                                                 ::: "r1", "memory"\r
311                                                                                         );\r
312                                                                                         break;\r
313         #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */\r
314 \r
315                 default                                                 :       /* Unknown SVC call. */\r
316                                                                                         break;\r
317         }\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 /*\r
322  * See header file for description.\r
323  */\r
324 BaseType_t xPortStartScheduler( void )\r
325 {\r
326         /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.\r
327          * See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
328         configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
329 \r
330         /* This port can be used on all revisions of the Cortex-M7 core other than\r
331          * the r0p1 parts.  r0p1 parts should use the port from the\r
332          * /source/portable/GCC/ARM_CM7/r0p1 directory. */\r
333         configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );\r
334         configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );\r
335 \r
336         #if( configASSERT_DEFINED == 1 )\r
337         {\r
338                 volatile uint32_t ulOriginalPriority;\r
339                 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );\r
340                 volatile uint8_t ucMaxPriorityValue;\r
341 \r
342                 /* Determine the maximum priority from which ISR safe FreeRTOS API\r
343                  * functions can be called.  ISR safe functions are those that end in\r
344                  * "FromISR".  FreeRTOS maintains separate thread and ISR API functions to\r
345                  * ensure interrupt entry is as fast and simple as possible.\r
346 \r
347                 Save the interrupt priority value that is about to be clobbered. */\r
348                 ulOriginalPriority = *pucFirstUserPriorityRegister;\r
349 \r
350                 /* Determine the number of priority bits available.  First write to all\r
351                  * possible bits. */\r
352                 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;\r
353 \r
354                 /* Read the value back to see how many bits stuck. */\r
355                 ucMaxPriorityValue = *pucFirstUserPriorityRegister;\r
356 \r
357                 /* Use the same mask on the maximum system call priority. */\r
358                 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;\r
359 \r
360                 /* Calculate the maximum acceptable priority group value for the number\r
361                  * of bits read back. */\r
362                 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;\r
363                 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )\r
364                 {\r
365                         ulMaxPRIGROUPValue--;\r
366                         ucMaxPriorityValue <<= ( uint8_t ) 0x01;\r
367                 }\r
368 \r
369                 #ifdef __NVIC_PRIO_BITS\r
370                 {\r
371                         /* Check the CMSIS configuration that defines the number of\r
372                          * priority bits matches the number of priority bits actually queried\r
373                          * from the hardware. */\r
374                         configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );\r
375                 }\r
376                 #endif\r
377 \r
378                 #ifdef configPRIO_BITS\r
379                 {\r
380                         /* Check the FreeRTOS configuration that defines the number of\r
381                          * priority bits matches the number of priority bits actually queried\r
382                          * from the hardware. */\r
383                         configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );\r
384                 }\r
385                 #endif\r
386 \r
387                 /* Shift the priority group value back to its position within the AIRCR\r
388                  * register. */\r
389                 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;\r
390                 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;\r
391 \r
392                 /* Restore the clobbered interrupt priority register to its original\r
393                  * value. */\r
394                 *pucFirstUserPriorityRegister = ulOriginalPriority;\r
395         }\r
396         #endif /* conifgASSERT_DEFINED */\r
397 \r
398         /* Make PendSV and SysTick the lowest priority interrupts. */\r
399         portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;\r
400         portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;\r
401 \r
402         /* Configure the regions in the MPU that are common to all tasks. */\r
403         prvSetupMPU();\r
404 \r
405         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
406          * here already. */\r
407         vPortSetupTimerInterrupt();\r
408 \r
409         /* Initialise the critical nesting count ready for the first task. */\r
410         uxCriticalNesting = 0;\r
411 \r
412         /* Ensure the VFP is enabled - it should be anyway. */\r
413         vPortEnableVFP();\r
414 \r
415         /* Lazy save always. */\r
416         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
417 \r
418         /* Start the first task. */\r
419         vPortStartFirstTask();\r
420 \r
421         /* Should not get here! */\r
422         return 0;\r
423 }\r
424 /*-----------------------------------------------------------*/\r
425 \r
426 void vPortEndScheduler( void )\r
427 {\r
428         /* Not implemented in ports where there is nothing to return to.\r
429          * Artificially force an assert. */\r
430         configASSERT( uxCriticalNesting == 1000UL );\r
431 }\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 void vPortEnterCritical( void )\r
435 {\r
436  BaseType_t xRunningPrivileged = xPortRaisePrivilege();\r
437 \r
438         portDISABLE_INTERRUPTS();\r
439         uxCriticalNesting++;\r
440 \r
441         vPortResetPrivilege( xRunningPrivileged );\r
442 \r
443         /* This is not the interrupt safe version of the enter critical function so\r
444          * assert() if it is being called from an interrupt context.  Only API\r
445          * functions that end in "FromISR" can be used in an interrupt.  Only assert if\r
446          * the critical nesting count is 1 to protect against recursive calls if the\r
447          * assert function also uses a critical section. */\r
448         if( uxCriticalNesting == 1 )\r
449         {\r
450                 configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );\r
451         }\r
452 }\r
453 /*-----------------------------------------------------------*/\r
454 \r
455 void vPortExitCritical( void )\r
456 {\r
457 BaseType_t xRunningPrivileged = xPortRaisePrivilege();\r
458 \r
459         configASSERT( uxCriticalNesting );\r
460 \r
461         uxCriticalNesting--;\r
462         if( uxCriticalNesting == 0 )\r
463         {\r
464                 portENABLE_INTERRUPTS();\r
465         }\r
466 \r
467         vPortResetPrivilege( xRunningPrivileged );\r
468 }\r
469 /*-----------------------------------------------------------*/\r
470 \r
471 void xPortSysTickHandler( void )\r
472 {\r
473         /* The SysTick runs at the lowest interrupt priority, so when this interrupt\r
474          * executes all interrupts must be unmasked.  There is therefore no need to\r
475          * save and then restore the interrupt mask value as its value is already\r
476          * known. */\r
477         portDISABLE_INTERRUPTS();\r
478         {\r
479                 /* Increment the RTOS tick. */\r
480                 if( xTaskIncrementTick() != pdFALSE )\r
481                 {\r
482                         /* A context switch is required.  Context switching is performed in\r
483                          * the PendSV interrupt.  Pend the PendSV interrupt. */\r
484                         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
485                 }\r
486         }\r
487         portENABLE_INTERRUPTS();\r
488 }\r
489 /*-----------------------------------------------------------*/\r
490 \r
491 /*\r
492  * Setup the systick timer to generate the tick interrupts at the required\r
493  * frequency.\r
494  */\r
495 __weak void vPortSetupTimerInterrupt( void )\r
496 {\r
497         /* Stop and clear the SysTick. */\r
498         portNVIC_SYSTICK_CTRL_REG = 0UL;\r
499         portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
500 \r
501         /* Configure SysTick to interrupt at the requested rate. */\r
502         portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
503         portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );\r
504 }\r
505 /*-----------------------------------------------------------*/\r
506 \r
507 static void prvSetupMPU( void )\r
508 {\r
509 extern uint32_t __privileged_functions_end__[];\r
510 extern uint32_t __FLASH_segment_start__[];\r
511 extern uint32_t __FLASH_segment_end__[];\r
512 extern uint32_t __privileged_data_start__[];\r
513 extern uint32_t __privileged_data_end__[];\r
514 \r
515         /* Check the expected MPU is present. */\r
516         if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )\r
517         {\r
518                 /* First setup the entire flash for unprivileged read only access. */\r
519                 portMPU_REGION_BASE_ADDRESS_REG =       ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */\r
520                                                                                         ( portMPU_REGION_VALID ) |\r
521                                                                                         ( portUNPRIVILEGED_FLASH_REGION );\r
522 \r
523                 portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_READ_ONLY ) |\r
524                                                                                 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
525                                                                                 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |\r
526                                                                                 ( portMPU_REGION_ENABLE );\r
527 \r
528                 /* Setup the first 16K for privileged only access (even though less\r
529                  * than 10K is actually being used).  This is where the kernel code is\r
530                  * placed. */\r
531                 portMPU_REGION_BASE_ADDRESS_REG =       ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */\r
532                                                                                         ( portMPU_REGION_VALID ) |\r
533                                                                                         ( portPRIVILEGED_FLASH_REGION );\r
534 \r
535                 portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |\r
536                                                                                 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
537                                                                                 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |\r
538                                                                                 ( portMPU_REGION_ENABLE );\r
539 \r
540                 /* Setup the privileged data RAM region.  This is where the kernel data\r
541                  * is placed. */\r
542                 portMPU_REGION_BASE_ADDRESS_REG =       ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */\r
543                                                                                         ( portMPU_REGION_VALID ) |\r
544                                                                                         ( portPRIVILEGED_RAM_REGION );\r
545 \r
546                 portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
547                                                                                 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
548                                                                                 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |\r
549                                                                                 ( portMPU_REGION_ENABLE );\r
550 \r
551                 /* By default allow everything to access the general peripherals.  The\r
552                  * system peripherals and registers are protected. */\r
553                 portMPU_REGION_BASE_ADDRESS_REG =       ( portPERIPHERALS_START_ADDRESS ) |\r
554                                                                                         ( portMPU_REGION_VALID ) |\r
555                                                                                         ( portGENERAL_PERIPHERALS_REGION );\r
556 \r
557                 portMPU_REGION_ATTRIBUTE_REG =  ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |\r
558                                                                                 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |\r
559                                                                                 ( portMPU_REGION_ENABLE );\r
560 \r
561                 /* Enable the memory fault exception. */\r
562                 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;\r
563 \r
564                 /* Enable the MPU with the background region configured. */\r
565                 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );\r
566         }\r
567 }\r
568 /*-----------------------------------------------------------*/\r
569 \r
570 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )\r
571 {\r
572 uint32_t ulRegionSize, ulReturnValue = 4;\r
573 \r
574         /* 32 is the smallest region size, 31 is the largest valid value for\r
575          * ulReturnValue. */\r
576         for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )\r
577         {\r
578                 if( ulActualSizeInBytes <= ulRegionSize )\r
579                 {\r
580                         break;\r
581                 }\r
582                 else\r
583                 {\r
584                         ulReturnValue++;\r
585                 }\r
586         }\r
587 \r
588         /* Shift the code by one before returning so it can be written directly\r
589          * into the the correct bit position of the attribute register. */\r
590         return ( ulReturnValue << 1UL );\r
591 }\r
592 /*-----------------------------------------------------------*/\r
593 \r
594 void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth )\r
595 {\r
596 extern uint32_t __SRAM_segment_start__[];\r
597 extern uint32_t __SRAM_segment_end__[];\r
598 extern uint32_t __privileged_data_start__[];\r
599 extern uint32_t __privileged_data_end__[];\r
600 int32_t lIndex;\r
601 uint32_t ul;\r
602 \r
603         if( xRegions == NULL )\r
604         {\r
605                 /* No MPU regions are specified so allow access to all RAM. */\r
606                 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =\r
607                                 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */\r
608                                 ( portMPU_REGION_VALID ) |\r
609                                 ( portSTACK_REGION );\r
610 \r
611                 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =\r
612                                 ( portMPU_REGION_READ_WRITE ) |\r
613                                 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
614                                 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |\r
615                                 ( portMPU_REGION_ENABLE );\r
616 \r
617                 /* Re-instate the privileged only RAM region as xRegion[ 0 ] will have\r
618                  * just removed the privileged only parameters. */\r
619                 xMPUSettings->xRegion[ 1 ].ulRegionBaseAddress =\r
620                                 ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */\r
621                                 ( portMPU_REGION_VALID ) |\r
622                                 ( portSTACK_REGION + 1 );\r
623 \r
624                 xMPUSettings->xRegion[ 1 ].ulRegionAttribute =\r
625                                 ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |\r
626                                 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
627                                 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |\r
628                                 ( portMPU_REGION_ENABLE );\r
629 \r
630                 /* Invalidate all other regions. */\r
631                 for( ul = 2; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )\r
632                 {\r
633                         xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;\r
634                         xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;\r
635                 }\r
636         }\r
637         else\r
638         {\r
639                 /* This function is called automatically when the task is created - in\r
640                  * which case the stack region parameters will be valid.  At all other\r
641                  * times the stack parameters will not be valid and it is assumed that the\r
642                  * stack region has already been configured. */\r
643                 if( ulStackDepth > 0 )\r
644                 {\r
645                         /* Define the region that allows access to the stack. */\r
646                         xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =\r
647                                         ( ( uint32_t ) pxBottomOfStack ) |\r
648                                         ( portMPU_REGION_VALID ) |\r
649                                         ( portSTACK_REGION ); /* Region number. */\r
650 \r
651                         xMPUSettings->xRegion[ 0 ].ulRegionAttribute =\r
652                                         ( portMPU_REGION_READ_WRITE ) | /* Read and write. */\r
653                                         ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |\r
654                                         ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |\r
655                                         ( portMPU_REGION_ENABLE );\r
656                 }\r
657 \r
658                 lIndex = 0;\r
659 \r
660                 for( ul = 1; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )\r
661                 {\r
662                         if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )\r
663                         {\r
664                                 /* Translate the generic region definition contained in\r
665                                  * xRegions into the CM3 specific MPU settings that are then\r
666                                  * stored in xMPUSettings. */\r
667                                 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =\r
668                                                 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |\r
669                                                 ( portMPU_REGION_VALID ) |\r
670                                                 ( portSTACK_REGION + ul ); /* Region number. */\r
671 \r
672                                 xMPUSettings->xRegion[ ul ].ulRegionAttribute =\r
673                                                 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |\r
674                                                 ( xRegions[ lIndex ].ulParameters ) |\r
675                                                 ( portMPU_REGION_ENABLE );\r
676                         }\r
677                         else\r
678                         {\r
679                                 /* Invalidate the region. */\r
680                                 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;\r
681                                 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;\r
682                         }\r
683 \r
684                         lIndex++;\r
685                 }\r
686         }\r
687 }\r
688 /*-----------------------------------------------------------*/\r
689 \r
690 #if( configASSERT_DEFINED == 1 )\r
691 \r
692         void vPortValidateInterruptPriority( void )\r
693         {\r
694         uint32_t ulCurrentInterrupt;\r
695         uint8_t ucCurrentPriority;\r
696 \r
697                 /* Obtain the number of the currently executing interrupt. */\r
698                 __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) :: "memory" );\r
699 \r
700                 /* Is the interrupt number a user defined interrupt? */\r
701                 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )\r
702                 {\r
703                         /* Look up the interrupt's priority. */\r
704                         ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];\r
705 \r
706                         /* The following assertion will fail if a service routine (ISR) for\r
707                          * an interrupt that has been assigned a priority above\r
708                          * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API\r
709                          * function.  ISR safe FreeRTOS API functions must *only* be called\r
710                          * from interrupts that have been assigned a priority at or below\r
711                          * configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
712 \r
713                          * Numerically low interrupt priority numbers represent logically high\r
714                          * interrupt priorities, therefore the priority of the interrupt must\r
715                          * be set to a value equal to or numerically *higher* than\r
716                          * configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
717 \r
718                          * Interrupts that      use the FreeRTOS API must not be left at their\r
719                          * default priority of  zero as that is the highest possible priority,\r
720                          * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
721                          * and  therefore also guaranteed to be invalid.\r
722 \r
723                          * FreeRTOS maintains separate thread and ISR API functions to ensure\r
724                          * interrupt entry is as fast and simple as possible.\r
725 \r
726                          * The following links provide detailed information:\r
727                          * http://www.freertos.org/RTOS-Cortex-M3-M4.html\r
728                          * http://www.freertos.org/FAQHelp.html */\r
729                         configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );\r
730                 }\r
731 \r
732                 /* Priority grouping:  The interrupt controller (NVIC) allows the bits\r
733                  * that define each interrupt's priority to be split between bits that\r
734                  * define the interrupt's pre-emption priority bits and bits that define\r
735                  * the interrupt's sub-priority.  For simplicity all bits must be defined\r
736                  * to be pre-emption priority bits.  The following assertion will fail if\r
737                  * this is not the case (if some bits represent a sub-priority).\r
738 \r
739                  * If the application only uses CMSIS libraries for interrupt\r
740                  * configuration then the correct setting can be achieved on all Cortex-M\r
741                  * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the\r
742                  * scheduler.  Note however that some vendor specific peripheral libraries\r
743                  * assume a non-zero priority group setting, in which cases using a value\r
744                  * of zero will result in unpredictable behaviour. */\r
745                 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );\r
746         }\r
747 \r
748 #endif /* configASSERT_DEFINED */\r
749 /*-----------------------------------------------------------*/\r