2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the ARM CM3 port.
\r
31 *----------------------------------------------------------*/
\r
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
34 all the API functions to use the MPU wrappers. That should only be done when
\r
35 task.h is included from an application file. */
\r
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
38 /* Scheduler includes. */
\r
39 #include "FreeRTOS.h"
\r
42 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
44 #ifndef configSYSTICK_CLOCK_HZ
\r
45 #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
\r
46 /* Ensure the SysTick is clocked at the same frequency as the core. */
\r
47 #define portNVIC_SYSTICK_CLK ( 1UL << 2UL )
\r
49 /* The way the SysTick is clocked is not modified in case it is not the same
\r
51 #define portNVIC_SYSTICK_CLK ( 0 )
\r
54 /* Constants required to access and manipulate the NVIC. */
\r
55 #define portNVIC_SYSTICK_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000e010 ) )
\r
56 #define portNVIC_SYSTICK_LOAD_REG ( * ( ( volatile uint32_t * ) 0xe000e014 ) )
\r
57 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( * ( ( volatile uint32_t * ) 0xe000e018 ) )
\r
58 #define portNVIC_SYSPRI2_REG ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )
\r
59 #define portNVIC_SYSPRI1_REG ( * ( ( volatile uint32_t * ) 0xe000ed1c ) )
\r
60 #define portNVIC_SYS_CTRL_STATE_REG ( * ( ( volatile uint32_t * ) 0xe000ed24 ) )
\r
61 #define portNVIC_MEM_FAULT_ENABLE ( 1UL << 16UL )
\r
63 /* Constants required to access and manipulate the MPU. */
\r
64 #define portMPU_TYPE_REG ( * ( ( volatile uint32_t * ) 0xe000ed90 ) )
\r
65 #define portMPU_REGION_BASE_ADDRESS_REG ( * ( ( volatile uint32_t * ) 0xe000ed9C ) )
\r
66 #define portMPU_REGION_ATTRIBUTE_REG ( * ( ( volatile uint32_t * ) 0xe000edA0 ) )
\r
67 #define portMPU_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000ed94 ) )
\r
68 #define portEXPECTED_MPU_TYPE_VALUE ( 8UL << 8UL ) /* 8 regions, unified. */
\r
69 #define portMPU_ENABLE ( 0x01UL )
\r
70 #define portMPU_BACKGROUND_ENABLE ( 1UL << 2UL )
\r
71 #define portPRIVILEGED_EXECUTION_START_ADDRESS ( 0UL )
\r
72 #define portMPU_REGION_VALID ( 0x10UL )
\r
73 #define portMPU_REGION_ENABLE ( 0x01UL )
\r
74 #define portPERIPHERALS_START_ADDRESS 0x40000000UL
\r
75 #define portPERIPHERALS_END_ADDRESS 0x5FFFFFFFUL
\r
77 /* Constants required to access and manipulate the SysTick. */
\r
78 #define portNVIC_SYSTICK_INT ( 0x00000002UL )
\r
79 #define portNVIC_SYSTICK_ENABLE ( 0x00000001UL )
\r
80 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
\r
81 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
\r
82 #define portNVIC_SVC_PRI ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
\r
84 /* Constants required to set up the initial stack. */
\r
85 #define portINITIAL_XPSR ( 0x01000000 )
\r
86 #define portINITIAL_CONTROL_IF_UNPRIVILEGED ( 0x03 )
\r
87 #define portINITIAL_CONTROL_IF_PRIVILEGED ( 0x02 )
\r
89 /* Constants required to check the validity of an interrupt priority. */
\r
90 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
\r
91 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
\r
92 #define portAIRCR_REG ( * ( ( volatile uint32_t * ) 0xE000ED0C ) )
\r
93 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
\r
94 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
\r
95 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
\r
96 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
\r
97 #define portPRIGROUP_SHIFT ( 8UL )
\r
99 /* Offsets in the stack to the parameters when inside the SVC handler. */
\r
100 #define portOFFSET_TO_PC ( 6 )
\r
102 /* For strict compliance with the Cortex-M spec the task start address should
\r
103 have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
\r
104 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
\r
107 * Configure a number of standard MPU regions that are used by all tasks.
\r
109 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
\r
112 * Return the smallest MPU region size that a given number of bytes will fit
\r
113 * into. The region size is returned as the value that should be programmed
\r
114 * into the region attribute register for that region.
\r
116 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
\r
119 * Checks to see if being called from the context of an unprivileged task, and
\r
120 * if so raises the privilege level and returns false - otherwise does nothing
\r
121 * other than return true.
\r
123 BaseType_t xPortRaisePrivilege( void ) __attribute__(( naked ));
\r
126 * Setup the timer to generate the tick interrupts. The implementation in this
\r
127 * file is weak to allow application writers to change the timer used to
\r
128 * generate the tick interrupt.
\r
130 void vPortSetupTimerInterrupt( void );
\r
133 * Standard FreeRTOS exception handlers.
\r
135 void xPortPendSVHandler( void ) __attribute__ (( naked )) PRIVILEGED_FUNCTION;
\r
136 void xPortSysTickHandler( void ) __attribute__ ((optimize("3"))) PRIVILEGED_FUNCTION;
\r
137 void vPortSVCHandler( void ) __attribute__ (( naked )) PRIVILEGED_FUNCTION;
\r
140 * Starts the scheduler by restoring the context of the first task to run.
\r
142 static void prvRestoreContextOfFirstTask( void ) __attribute__(( naked )) PRIVILEGED_FUNCTION;
\r
145 * C portion of the SVC handler. The SVC handler is split between an asm entry
\r
146 * and a C wrapper for simplicity of coding and maintenance.
\r
148 static void prvSVCHandler( uint32_t *pulRegisters ) __attribute__(( noinline )) PRIVILEGED_FUNCTION;
\r
150 /*-----------------------------------------------------------*/
\r
152 /* Each task maintains its own interrupt status in the critical nesting
\r
153 variable. Note this is not saved as part of the task context as context
\r
154 switches can only occur when uxCriticalNesting is zero. */
\r
155 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
\r
158 * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
\r
159 * FreeRTOS API functions are not called from interrupts that have been assigned
\r
160 * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
162 #if ( configASSERT_DEFINED == 1 )
\r
163 static uint8_t ucMaxSysCallPriority = 0;
\r
164 static uint32_t ulMaxPRIGROUPValue = 0;
\r
165 static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
\r
166 #endif /* configASSERT_DEFINED */
\r
168 /*-----------------------------------------------------------*/
\r
171 * See header file for description.
\r
173 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )
\r
175 /* Simulate the stack frame as it would be created by a context switch
\r
177 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
\r
178 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
\r
180 *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
\r
182 *pxTopOfStack = 0; /* LR */
\r
183 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
\r
184 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
185 pxTopOfStack -= 9; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
\r
187 if( xRunPrivileged == pdTRUE )
\r
189 *pxTopOfStack = portINITIAL_CONTROL_IF_PRIVILEGED;
\r
193 *pxTopOfStack = portINITIAL_CONTROL_IF_UNPRIVILEGED;
\r
196 return pxTopOfStack;
\r
198 /*-----------------------------------------------------------*/
\r
200 void vPortSVCHandler( void )
\r
202 /* Assumes psp was in use. */
\r
205 #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
\r
208 " mrseq r0, msp \n"
\r
209 " mrsne r0, psp \n"
\r
214 ::"i"(prvSVCHandler):"r0", "memory"
\r
217 /*-----------------------------------------------------------*/
\r
219 static void prvSVCHandler( uint32_t *pulParam )
\r
221 uint8_t ucSVCNumber;
\r
223 /* The stack contains: r0, r1, r2, r3, r12, r14, the return address and
\r
224 xPSR. The first argument (r0) is pulParam[ 0 ]. */
\r
225 ucSVCNumber = ( ( uint8_t * ) pulParam[ portOFFSET_TO_PC ] )[ -2 ];
\r
226 switch( ucSVCNumber )
\r
228 case portSVC_START_SCHEDULER : portNVIC_SYSPRI1_REG |= portNVIC_SVC_PRI;
\r
229 prvRestoreContextOfFirstTask();
\r
232 case portSVC_YIELD : portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
233 /* Barriers are normally not required
\r
234 but do ensure the code is completely
\r
235 within the specified behaviour for the
\r
237 __asm volatile( "dsb" ::: "memory" );
\r
238 __asm volatile( "isb" );
\r
242 case portSVC_RAISE_PRIVILEGE : __asm volatile
\r
244 " mrs r1, control \n" /* Obtain current control value. */
\r
245 " bic r1, #1 \n" /* Set privilege bit. */
\r
246 " msr control, r1 \n" /* Write back new control value. */
\r
251 default : /* Unknown SVC call. */
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvRestoreContextOfFirstTask( void )
\r
261 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
\r
264 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
\r
265 " ldr r3, pxCurrentTCBConst2 \n" /* Restore the context. */
\r
267 " ldr r0, [r1] \n" /* The first item in the TCB is the task top of stack. */
\r
268 " add r1, r1, #4 \n" /* Move onto the second item in the TCB... */
\r
269 " ldr r2, =0xe000ed9c \n" /* Region Base Address register. */
\r
270 " ldmia r1!, {r4-r11} \n" /* Read 4 sets of MPU registers. */
\r
271 " stmia r2!, {r4-r11} \n" /* Write 4 sets of MPU registers. */
\r
272 " ldmia r0!, {r3, r4-r11} \n" /* Pop the registers that are not automatically saved on exception entry. */
\r
273 " msr control, r3 \n"
\r
274 " msr psp, r0 \n" /* Restore the task stack pointer. */
\r
276 " msr basepri, r0 \n"
\r
277 " ldr r14, =0xfffffffd \n" /* Load exec return code. */
\r
281 "pxCurrentTCBConst2: .word pxCurrentTCB \n"
\r
284 /*-----------------------------------------------------------*/
\r
287 * See header file for description.
\r
289 BaseType_t xPortStartScheduler( void )
\r
291 /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See
\r
292 http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
293 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );
\r
295 #if( configASSERT_DEFINED == 1 )
\r
297 volatile uint32_t ulOriginalPriority;
\r
298 volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
\r
299 volatile uint8_t ucMaxPriorityValue;
\r
301 /* Determine the maximum priority from which ISR safe FreeRTOS API
\r
302 functions can be called. ISR safe functions are those that end in
\r
303 "FromISR". FreeRTOS maintains separate thread and ISR API functions to
\r
304 ensure interrupt entry is as fast and simple as possible.
\r
306 Save the interrupt priority value that is about to be clobbered. */
\r
307 ulOriginalPriority = *pucFirstUserPriorityRegister;
\r
309 /* Determine the number of priority bits available. First write to all
\r
311 *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
\r
313 /* Read the value back to see how many bits stuck. */
\r
314 ucMaxPriorityValue = *pucFirstUserPriorityRegister;
\r
316 /* Use the same mask on the maximum system call priority. */
\r
317 ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
\r
319 /* Calculate the maximum acceptable priority group value for the number
\r
320 of bits read back. */
\r
321 ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
\r
322 while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
\r
324 ulMaxPRIGROUPValue--;
\r
325 ucMaxPriorityValue <<= ( uint8_t ) 0x01;
\r
328 #ifdef __NVIC_PRIO_BITS
\r
330 /* Check the CMSIS configuration that defines the number of
\r
331 priority bits matches the number of priority bits actually queried
\r
332 from the hardware. */
\r
333 configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
\r
337 #ifdef configPRIO_BITS
\r
339 /* Check the FreeRTOS configuration that defines the number of
\r
340 priority bits matches the number of priority bits actually queried
\r
341 from the hardware. */
\r
342 configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
\r
346 /* Shift the priority group value back to its position within the AIRCR
\r
348 ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
\r
349 ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
\r
351 /* Restore the clobbered interrupt priority register to its original
\r
353 *pucFirstUserPriorityRegister = ulOriginalPriority;
\r
355 #endif /* conifgASSERT_DEFINED */
\r
357 /* Make PendSV and SysTick the same priority as the kernel, and the SVC
\r
358 handler higher priority so it can be used to exit a critical section (where
\r
359 lower priorities are masked). */
\r
360 portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;
\r
361 portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;
\r
363 /* Configure the regions in the MPU that are common to all tasks. */
\r
366 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
368 vPortSetupTimerInterrupt();
\r
370 /* Initialise the critical nesting count ready for the first task. */
\r
371 uxCriticalNesting = 0;
\r
373 /* Start the first task. */
\r
375 " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
\r
378 " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
\r
379 " cpsie i \n" /* Globally enable interrupts. */
\r
383 " svc %0 \n" /* System call to start first task. */
\r
385 :: "i" (portSVC_START_SCHEDULER) : "memory" );
\r
387 /* Should not get here! */
\r
390 /*-----------------------------------------------------------*/
\r
392 void vPortEndScheduler( void )
\r
394 /* Not implemented in ports where there is nothing to return to.
\r
395 Artificially force an assert. */
\r
396 configASSERT( uxCriticalNesting == 1000UL );
\r
398 /*-----------------------------------------------------------*/
\r
400 void vPortEnterCritical( void )
\r
402 BaseType_t xRunningPrivileged = xPortRaisePrivilege();
\r
404 portDISABLE_INTERRUPTS();
\r
405 uxCriticalNesting++;
\r
407 vPortResetPrivilege( xRunningPrivileged );
\r
409 /*-----------------------------------------------------------*/
\r
411 void vPortExitCritical( void )
\r
413 BaseType_t xRunningPrivileged = xPortRaisePrivilege();
\r
415 configASSERT( uxCriticalNesting );
\r
416 uxCriticalNesting--;
\r
417 if( uxCriticalNesting == 0 )
\r
419 portENABLE_INTERRUPTS();
\r
421 vPortResetPrivilege( xRunningPrivileged );
\r
423 /*-----------------------------------------------------------*/
\r
425 void xPortPendSVHandler( void )
\r
427 /* This is a naked function. */
\r
433 " ldr r3, pxCurrentTCBConst \n" /* Get the location of the current TCB. */
\r
436 " mrs r1, control \n"
\r
437 " stmdb r0!, {r1, r4-r11} \n" /* Save the remaining registers. */
\r
438 " str r0, [r2] \n" /* Save the new top of stack into the first member of the TCB. */
\r
440 " stmdb sp!, {r3, r14} \n"
\r
442 " msr basepri, r0 \n"
\r
445 " bl vTaskSwitchContext \n"
\r
447 " msr basepri, r0 \n"
\r
448 " ldmia sp!, {r3, r14} \n"
\r
449 " \n" /* Restore the context. */
\r
451 " ldr r0, [r1] \n" /* The first item in the TCB is the task top of stack. */
\r
452 " add r1, r1, #4 \n" /* Move onto the second item in the TCB... */
\r
453 " ldr r2, =0xe000ed9c \n" /* Region Base Address register. */
\r
454 " ldmia r1!, {r4-r11} \n" /* Read 4 sets of MPU registers. */
\r
455 " stmia r2!, {r4-r11} \n" /* Write 4 sets of MPU registers. */
\r
456 " ldmia r0!, {r3, r4-r11} \n" /* Pop the registers that are not automatically saved on exception entry. */
\r
457 " msr control, r3 \n"
\r
463 "pxCurrentTCBConst: .word pxCurrentTCB \n"
\r
464 ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY)
\r
467 /*-----------------------------------------------------------*/
\r
469 void xPortSysTickHandler( void )
\r
473 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
\r
475 /* Increment the RTOS tick. */
\r
476 if( xTaskIncrementTick() != pdFALSE )
\r
478 /* Pend a context switch. */
\r
479 portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
\r
482 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
\r
484 /*-----------------------------------------------------------*/
\r
487 * Setup the systick timer to generate the tick interrupts at the required
\r
490 __attribute__(( weak )) void vPortSetupTimerInterrupt( void )
\r
492 /* Stop and clear the SysTick. */
\r
493 portNVIC_SYSTICK_CTRL_REG = 0UL;
\r
494 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
\r
496 /* Configure SysTick to interrupt at the requested rate. */
\r
497 portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
498 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE );
\r
500 /*-----------------------------------------------------------*/
\r
502 static void prvSetupMPU( void )
\r
504 extern uint32_t __privileged_functions_end__[];
\r
505 extern uint32_t __FLASH_segment_start__[];
\r
506 extern uint32_t __FLASH_segment_end__[];
\r
507 extern uint32_t __privileged_data_start__[];
\r
508 extern uint32_t __privileged_data_end__[];
\r
510 /* Check the expected MPU is present. */
\r
511 if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
\r
513 /* First setup the entire flash for unprivileged read only access. */
\r
514 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
\r
515 ( portMPU_REGION_VALID ) |
\r
516 ( portUNPRIVILEGED_FLASH_REGION );
\r
518 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
\r
519 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
520 ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
\r
521 ( portMPU_REGION_ENABLE );
\r
523 /* Setup the first 16K for privileged only access (even though less
\r
524 than 10K is actually being used). This is where the kernel code is
\r
526 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
\r
527 ( portMPU_REGION_VALID ) |
\r
528 ( portPRIVILEGED_FLASH_REGION );
\r
530 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
\r
531 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
532 ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
\r
533 ( portMPU_REGION_ENABLE );
\r
535 /* Setup the privileged data RAM region. This is where the kernel data
\r
537 portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
\r
538 ( portMPU_REGION_VALID ) |
\r
539 ( portPRIVILEGED_RAM_REGION );
\r
541 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
\r
542 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
543 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
\r
544 ( portMPU_REGION_ENABLE );
\r
546 /* By default allow everything to access the general peripherals. The
\r
547 system peripherals and registers are protected. */
\r
548 portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
\r
549 ( portMPU_REGION_VALID ) |
\r
550 ( portGENERAL_PERIPHERALS_REGION );
\r
552 portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
\r
553 ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
\r
554 ( portMPU_REGION_ENABLE );
\r
556 /* Enable the memory fault exception. */
\r
557 portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
\r
559 /* Enable the MPU with the background region configured. */
\r
560 portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
\r
563 /*-----------------------------------------------------------*/
\r
565 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
\r
567 uint32_t ulRegionSize, ulReturnValue = 4;
\r
569 /* 32 is the smallest region size, 31 is the largest valid value for
\r
571 for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
\r
573 if( ulActualSizeInBytes <= ulRegionSize )
\r
583 /* Shift the code by one before returning so it can be written directly
\r
584 into the the correct bit position of the attribute register. */
\r
585 return ( ulReturnValue << 1UL );
\r
587 /*-----------------------------------------------------------*/
\r
589 BaseType_t xPortRaisePrivilege( void )
\r
593 " mrs r0, control \n"
\r
594 " tst r0, #1 \n" /* Is the task running privileged? */
\r
596 " movne r0, #0 \n" /* CONTROL[0]!=0, return false. */
\r
597 " svcne %0 \n" /* Switch to privileged. */
\r
598 " moveq r0, #1 \n" /* CONTROL[0]==0, return true. */
\r
600 :: "i" (portSVC_RAISE_PRIVILEGE) : "r0", "memory"
\r
605 /*-----------------------------------------------------------*/
\r
607 void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth )
\r
609 extern uint32_t __SRAM_segment_start__[];
\r
610 extern uint32_t __SRAM_segment_end__[];
\r
611 extern uint32_t __privileged_data_start__[];
\r
612 extern uint32_t __privileged_data_end__[];
\r
616 if( xRegions == NULL )
\r
618 /* No MPU regions are specified so allow access to all RAM. */
\r
619 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
\r
620 ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
\r
621 ( portMPU_REGION_VALID ) |
\r
622 ( portSTACK_REGION );
\r
624 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
\r
625 ( portMPU_REGION_READ_WRITE ) |
\r
626 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
627 ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
\r
628 ( portMPU_REGION_ENABLE );
\r
630 /* Re-instate the privileged only RAM region as xRegion[ 0 ] will have
\r
631 just removed the privileged only parameters. */
\r
632 xMPUSettings->xRegion[ 1 ].ulRegionBaseAddress =
\r
633 ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
\r
634 ( portMPU_REGION_VALID ) |
\r
635 ( portSTACK_REGION + 1 );
\r
637 xMPUSettings->xRegion[ 1 ].ulRegionAttribute =
\r
638 ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
\r
639 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
640 prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
\r
641 ( portMPU_REGION_ENABLE );
\r
643 /* Invalidate all other regions. */
\r
644 for( ul = 2; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
\r
646 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;
\r
647 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
\r
652 /* This function is called automatically when the task is created - in
\r
653 which case the stack region parameters will be valid. At all other
\r
654 times the stack parameters will not be valid and it is assumed that the
\r
655 stack region has already been configured. */
\r
656 if( ulStackDepth > 0 )
\r
658 /* Define the region that allows access to the stack. */
\r
659 xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
\r
660 ( ( uint32_t ) pxBottomOfStack ) |
\r
661 ( portMPU_REGION_VALID ) |
\r
662 ( portSTACK_REGION ); /* Region number. */
\r
664 xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
\r
665 ( portMPU_REGION_READ_WRITE ) | /* Read and write. */
\r
666 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
\r
667 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
\r
668 ( portMPU_REGION_ENABLE );
\r
673 for( ul = 1; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
\r
675 if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
\r
677 /* Translate the generic region definition contained in
\r
678 xRegions into the CM3 specific MPU settings that are then
\r
679 stored in xMPUSettings. */
\r
680 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
\r
681 ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
\r
682 ( portMPU_REGION_VALID ) |
\r
683 ( portSTACK_REGION + ul ); /* Region number. */
\r
685 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
\r
686 ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
\r
687 ( xRegions[ lIndex ].ulParameters ) |
\r
688 ( portMPU_REGION_ENABLE );
\r
692 /* Invalidate the region. */
\r
693 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( portSTACK_REGION + ul ) | portMPU_REGION_VALID;
\r
694 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
\r
701 /*-----------------------------------------------------------*/
\r
703 #if( configASSERT_DEFINED == 1 )
\r
705 void vPortValidateInterruptPriority( void )
\r
707 uint32_t ulCurrentInterrupt;
\r
708 uint8_t ucCurrentPriority;
\r
710 /* Obtain the number of the currently executing interrupt. */
\r
711 __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) :: "memory" );
\r
713 /* Is the interrupt number a user defined interrupt? */
\r
714 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
\r
716 /* Look up the interrupt's priority. */
\r
717 ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
\r
719 /* The following assertion will fail if a service routine (ISR) for
\r
720 an interrupt that has been assigned a priority above
\r
721 configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
\r
722 function. ISR safe FreeRTOS API functions must *only* be called
\r
723 from interrupts that have been assigned a priority at or below
\r
724 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
726 Numerically low interrupt priority numbers represent logically high
\r
727 interrupt priorities, therefore the priority of the interrupt must
\r
728 be set to a value equal to or numerically *higher* than
\r
729 configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
731 Interrupts that use the FreeRTOS API must not be left at their
\r
732 default priority of zero as that is the highest possible priority,
\r
733 which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
\r
734 and therefore also guaranteed to be invalid.
\r
736 FreeRTOS maintains separate thread and ISR API functions to ensure
\r
737 interrupt entry is as fast and simple as possible.
\r
739 The following links provide detailed information:
\r
740 http://www.freertos.org/RTOS-Cortex-M3-M4.html
\r
741 http://www.freertos.org/FAQHelp.html */
\r
742 configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
\r
745 /* Priority grouping: The interrupt controller (NVIC) allows the bits
\r
746 that define each interrupt's priority to be split between bits that
\r
747 define the interrupt's pre-emption priority bits and bits that define
\r
748 the interrupt's sub-priority. For simplicity all bits must be defined
\r
749 to be pre-emption priority bits. The following assertion will fail if
\r
750 this is not the case (if some bits represent a sub-priority).
\r
752 If the application only uses CMSIS libraries for interrupt
\r
753 configuration then the correct setting can be achieved on all Cortex-M
\r
754 devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
\r
755 scheduler. Note however that some vendor specific peripheral libraries
\r
756 assume a non-zero priority group setting, in which cases using a value
\r
757 of zero will result in unpredicable behaviour. */
\r
758 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
\r
761 #endif /* configASSERT_DEFINED */
\r
762 /*-----------------------------------------------------------*/
\r