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