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