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