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