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