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