]> begriffs open source - freertos/blob - portable/GCC/ARM_CM3_MPU/port.c
CI-CD Updates (#768)
[freertos] / portable / GCC / ARM_CM3_MPU / port.c
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ARM CM3 MPU port.
31 *----------------------------------------------------------*/
32
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34  * all the API functions to use the MPU wrappers.  That should only be done when
35  * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
37
38 /* Scheduler includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41
42 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
43
44 #ifndef configSYSTICK_CLOCK_HZ
45     #define configSYSTICK_CLOCK_HZ    configCPU_CLOCK_HZ
46     /* Ensure the SysTick is clocked at the same frequency as the core. */
47     #define portNVIC_SYSTICK_CLK      ( 1UL << 2UL )
48 #else
49
50 /* The way the SysTick is clocked is not modified in case it is not the same
51  * as the core. */
52     #define portNVIC_SYSTICK_CLK    ( 0 )
53 #endif
54
55 #ifndef configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS
56     #warning "configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS is not defined. We recommend defining it to 0 in FreeRTOSConfig.h for better security."
57     #define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS    1
58 #endif
59
60 /* Constants required to access and manipulate the NVIC. */
61 #define portNVIC_SYSTICK_CTRL_REG                 ( *( ( volatile uint32_t * ) 0xe000e010 ) )
62 #define portNVIC_SYSTICK_LOAD_REG                 ( *( ( volatile uint32_t * ) 0xe000e014 ) )
63 #define portNVIC_SYSTICK_CURRENT_VALUE_REG        ( *( ( volatile uint32_t * ) 0xe000e018 ) )
64 #define portNVIC_SHPR3_REG                        ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
65 #define portNVIC_SHPR2_REG                        ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
66 #define portNVIC_SYS_CTRL_STATE_REG               ( *( ( volatile uint32_t * ) 0xe000ed24 ) )
67 #define portNVIC_MEM_FAULT_ENABLE                 ( 1UL << 16UL )
68
69 /* Constants required to access and manipulate the MPU. */
70 #define portMPU_TYPE_REG                          ( *( ( volatile uint32_t * ) 0xe000ed90 ) )
71 #define portMPU_REGION_BASE_ADDRESS_REG           ( *( ( volatile uint32_t * ) 0xe000ed9C ) )
72 #define portMPU_REGION_ATTRIBUTE_REG              ( *( ( volatile uint32_t * ) 0xe000edA0 ) )
73 #define portMPU_CTRL_REG                          ( *( ( volatile uint32_t * ) 0xe000ed94 ) )
74 #define portEXPECTED_MPU_TYPE_VALUE               ( 8UL << 8UL ) /* 8 regions, unified. */
75 #define portMPU_ENABLE                            ( 0x01UL )
76 #define portMPU_BACKGROUND_ENABLE                 ( 1UL << 2UL )
77 #define portPRIVILEGED_EXECUTION_START_ADDRESS    ( 0UL )
78 #define portMPU_REGION_VALID                      ( 0x10UL )
79 #define portMPU_REGION_ENABLE                     ( 0x01UL )
80 #define portPERIPHERALS_START_ADDRESS             0x40000000UL
81 #define portPERIPHERALS_END_ADDRESS               0x5FFFFFFFUL
82
83 /* Constants required to access and manipulate the SysTick. */
84 #define portNVIC_SYSTICK_INT                      ( 0x00000002UL )
85 #define portNVIC_SYSTICK_ENABLE                   ( 0x00000001UL )
86 #define portMIN_INTERRUPT_PRIORITY                ( 255UL )
87 #define portNVIC_PENDSV_PRI                       ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
88 #define portNVIC_SYSTICK_PRI                      ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
89 #define portNVIC_SVC_PRI                          ( ( ( uint32_t ) configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ) << 24UL )
90
91 /* Constants required to set up the initial stack. */
92 #define portINITIAL_XPSR                          ( 0x01000000 )
93 #define portINITIAL_EXC_RETURN                    ( 0xfffffffdUL )
94 #define portINITIAL_CONTROL_IF_UNPRIVILEGED       ( 0x03 )
95 #define portINITIAL_CONTROL_IF_PRIVILEGED         ( 0x02 )
96
97 /* Constants required to check the validity of an interrupt priority. */
98 #define portFIRST_USER_INTERRUPT_NUMBER           ( 16 )
99 #define portNVIC_IP_REGISTERS_OFFSET_16           ( 0xE000E3F0 )
100 #define portAIRCR_REG                             ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
101 #define portMAX_8_BIT_VALUE                       ( ( uint8_t ) 0xff )
102 #define portTOP_BIT_OF_BYTE                       ( ( uint8_t ) 0x80 )
103 #define portMAX_PRIGROUP_BITS                     ( ( uint8_t ) 7 )
104 #define portPRIORITY_GROUP_MASK                   ( 0x07UL << 8UL )
105 #define portPRIGROUP_SHIFT                        ( 8UL )
106
107 /* Constants used during system call enter and exit. */
108 #define portPSR_STACK_PADDING_MASK                ( 1UL << 9UL )
109
110 /* Offsets in the stack to the parameters when inside the SVC handler. */
111 #define portOFFSET_TO_LR                          ( 5 )
112 #define portOFFSET_TO_PC                          ( 6 )
113 #define portOFFSET_TO_PSR                         ( 7 )
114
115 /* For strict compliance with the Cortex-M spec the task start address should
116  * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
117 #define portSTART_ADDRESS_MASK                    ( ( StackType_t ) 0xfffffffeUL )
118
119 /* Does addr lie within [start, end] address range? */
120 #define portIS_ADDRESS_WITHIN_RANGE( addr, start, end ) \
121     ( ( ( addr ) >= ( start ) ) && ( ( addr ) <= ( end ) ) )
122
123 /* Is the access request satisfied by the available permissions? */
124 #define portIS_AUTHORIZED( accessRequest, permissions ) \
125     ( ( ( permissions ) & ( accessRequest ) ) == accessRequest )
126
127 /* Max value that fits in a uint32_t type. */
128 #define portUINT32_MAX    ( ~( ( uint32_t ) 0 ) )
129
130 /* Check if adding a and b will result in overflow. */
131 #define portADD_UINT32_WILL_OVERFLOW( a, b )    ( ( a ) > ( portUINT32_MAX - ( b ) ) )
132 /*-----------------------------------------------------------*/
133
134 /*
135  * Configure a number of standard MPU regions that are used by all tasks.
136  */
137 static void prvSetupMPU( void ) PRIVILEGED_FUNCTION;
138
139 /*
140  * Return the smallest MPU region size that a given number of bytes will fit
141  * into.  The region size is returned as the value that should be programmed
142  * into the region attribute register for that region.
143  */
144 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes ) PRIVILEGED_FUNCTION;
145
146 /*
147  * Setup the timer to generate the tick interrupts.  The implementation in this
148  * file is weak to allow application writers to change the timer used to
149  * generate the tick interrupt.
150  */
151 void vPortSetupTimerInterrupt( void );
152
153 /*
154  * Standard FreeRTOS exception handlers.
155  */
156 void xPortPendSVHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
157 void xPortSysTickHandler( void )  __attribute__( ( optimize( "3" ) ) ) PRIVILEGED_FUNCTION;
158 void vPortSVCHandler( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
159
160 /*
161  * Starts the scheduler by restoring the context of the first task to run.
162  */
163 static void prvRestoreContextOfFirstTask( void ) __attribute__( ( naked ) ) PRIVILEGED_FUNCTION;
164
165 /*
166  * C portion of the SVC handler.  The SVC handler is split between an asm entry
167  * and a C wrapper for simplicity of coding and maintenance.
168  */
169 void vSVCHandler_C( uint32_t * pulRegisters ) __attribute__( ( noinline ) ) PRIVILEGED_FUNCTION;
170
171 /**
172  * @brief Checks whether or not the processor is privileged.
173  *
174  * @return 1 if the processor is already privileged, 0 otherwise.
175  */
176 BaseType_t xIsPrivileged( void ) __attribute__( ( naked ) );
177
178 /**
179  * @brief Lowers the privilege level by setting the bit 0 of the CONTROL
180  * register.
181  *
182  * Bit 0 of the CONTROL register defines the privilege level of Thread Mode.
183  *  Bit[0] = 0 --> The processor is running privileged
184  *  Bit[0] = 1 --> The processor is running unprivileged.
185  */
186 void vResetPrivilege( void ) __attribute__( ( naked ) );
187
188 /**
189  * @brief Enter critical section.
190  */
191 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
192     void vPortEnterCritical( void ) FREERTOS_SYSTEM_CALL;
193 #else
194     void vPortEnterCritical( void ) PRIVILEGED_FUNCTION;
195 #endif
196
197 /**
198  * @brief Exit from critical section.
199  */
200 #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
201     void vPortExitCritical( void ) FREERTOS_SYSTEM_CALL;
202 #else
203     void vPortExitCritical( void ) PRIVILEGED_FUNCTION;
204 #endif
205
206 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
207
208 /**
209  * @brief Sets up the system call stack so that upon returning from
210  * SVC, the system call stack is used.
211  *
212  * It is used for the system calls with up to 4 parameters.
213  *
214  * @param pulTaskStack The current SP when the SVC was raised.
215  */
216     void vSystemCallEnter( uint32_t * pulTaskStack ) PRIVILEGED_FUNCTION;
217
218 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
219
220 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
221
222 /**
223  * @brief Sets up the system call stack so that upon returning from
224  * SVC, the system call stack is used.
225  *
226  * It is used for the system calls with 5 parameters.
227  *
228  * @param pulTaskStack The current SP when the SVC was raised.
229  */
230     void vSystemCallEnter_1( uint32_t * pulTaskStack ) PRIVILEGED_FUNCTION;
231
232 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
233
234 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
235
236 /**
237  * @brief Sets up the task stack so that upon returning from
238  * SVC, the task stack is used again.
239  *
240  * @param pulSystemCallStack The current SP when the SVC was raised.
241  */
242     void vSystemCallExit( uint32_t * pulSystemCallStack ) PRIVILEGED_FUNCTION;
243
244 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
245
246 /**
247  * @brief Checks whether or not the calling task is privileged.
248  *
249  * @return pdTRUE if the calling task is privileged, pdFALSE otherwise.
250  */
251 BaseType_t xPortIsTaskPrivileged( void ) PRIVILEGED_FUNCTION;
252 /*-----------------------------------------------------------*/
253
254 /* Each task maintains its own interrupt status in the critical nesting
255  * variable.  Note this is not saved as part of the task context as context
256  * switches can only occur when uxCriticalNesting is zero. */
257 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
258
259 /*
260  * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
261  * FreeRTOS API functions are not called from interrupts that have been assigned
262  * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
263  */
264 #if ( configASSERT_DEFINED == 1 )
265     static uint8_t ucMaxSysCallPriority = 0;
266     static uint32_t ulMaxPRIGROUPValue = 0;
267     static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
268 #endif /* configASSERT_DEFINED */
269 /*-----------------------------------------------------------*/
270
271 /*
272  * See header file for description.
273  */
274 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
275                                      TaskFunction_t pxCode,
276                                      void * pvParameters,
277                                      BaseType_t xRunPrivileged,
278                                      xMPU_SETTINGS * xMPUSettings )
279 {
280     if( xRunPrivileged == pdTRUE )
281     {
282         xMPUSettings->ulTaskFlags |= portTASK_IS_PRIVILEGED_FLAG;
283         xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_PRIVILEGED;
284     }
285     else
286     {
287         xMPUSettings->ulTaskFlags &= ( ~portTASK_IS_PRIVILEGED_FLAG );
288         xMPUSettings->ulContext[ 0 ] = portINITIAL_CONTROL_IF_UNPRIVILEGED;
289     }
290
291     xMPUSettings->ulContext[ 1 ] = 0x04040404;                                        /* r4. */
292     xMPUSettings->ulContext[ 2 ] = 0x05050505;                                        /* r5. */
293     xMPUSettings->ulContext[ 3 ] = 0x06060606;                                        /* r6. */
294     xMPUSettings->ulContext[ 4 ] = 0x07070707;                                        /* r7. */
295     xMPUSettings->ulContext[ 5 ] = 0x08080808;                                        /* r8. */
296     xMPUSettings->ulContext[ 6 ] = 0x09090909;                                        /* r9. */
297     xMPUSettings->ulContext[ 7 ] = 0x10101010;                                        /* r10. */
298     xMPUSettings->ulContext[ 8 ] = 0x11111111;                                        /* r11. */
299     xMPUSettings->ulContext[ 9 ] = portINITIAL_EXC_RETURN;                            /* EXC_RETURN. */
300
301     xMPUSettings->ulContext[ 10 ] = ( uint32_t ) ( pxTopOfStack - 8 );                /* PSP with the hardware saved stack. */
302     xMPUSettings->ulContext[ 11 ] = ( uint32_t ) pvParameters;                        /* r0. */
303     xMPUSettings->ulContext[ 12 ] = 0x01010101;                                       /* r1. */
304     xMPUSettings->ulContext[ 13 ] = 0x02020202;                                       /* r2. */
305     xMPUSettings->ulContext[ 14 ] = 0x03030303;                                       /* r3. */
306     xMPUSettings->ulContext[ 15 ] = 0x12121212;                                       /* r12. */
307     xMPUSettings->ulContext[ 16 ] = 0;                                                /* LR. */
308     xMPUSettings->ulContext[ 17 ] = ( ( uint32_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC. */
309     xMPUSettings->ulContext[ 18 ] = portINITIAL_XPSR;                                 /* xPSR. */
310
311     #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
312     {
313         /* Ensure that the system call stack is double word aligned. */
314         xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = &( xMPUSettings->xSystemCallStackInfo.ulSystemCallStackBuffer[ configSYSTEM_CALL_STACK_SIZE - 1 ] );
315         xMPUSettings->xSystemCallStackInfo.pulSystemCallStack = ( uint32_t * ) ( ( uint32_t ) ( xMPUSettings->xSystemCallStackInfo.pulSystemCallStack ) &
316                                                                                  ( uint32_t ) ( ~( portBYTE_ALIGNMENT_MASK ) ) );
317
318         /* This is not NULL only for the duration of a system call. */
319         xMPUSettings->xSystemCallStackInfo.pulTaskStack = NULL;
320     }
321     #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
322
323     return &( xMPUSettings->ulContext[ 19 ] );
324 }
325 /*-----------------------------------------------------------*/
326
327 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
328
329     void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
330     {
331         __asm volatile
332         (
333             ".syntax unified                \n"
334             ".extern vSVCHandler_C          \n"
335             ".extern vSystemCallEnter       \n"
336             ".extern vSystemCallEnter_1     \n"
337             ".extern vSystemCallExit        \n"
338             "                               \n"
339             "tst lr, #4                     \n"
340             "ite eq                         \n"
341             "mrseq r0, msp                  \n"
342             "mrsne r0, psp                  \n"
343             "                               \n"
344             "ldr r1, [r0, #24]              \n"
345             "ldrb r2, [r1, #-2]             \n"
346             "cmp r2, %0                     \n"
347             "beq vSystemCallEnter           \n"
348             "cmp r2, %1                     \n"
349             "beq vSystemCallEnter_1         \n"
350             "cmp r2, %2                     \n"
351             "beq vSystemCallExit            \n"
352             "b vSVCHandler_C                \n"
353             "                               \n"
354             : /* No outputs. */
355             : "i" ( portSVC_SYSTEM_CALL_ENTER ), "i" ( portSVC_SYSTEM_CALL_ENTER_1 ), "i" ( portSVC_SYSTEM_CALL_EXIT )
356             : "r0", "r1", "r2", "memory"
357         );
358     }
359
360 #else /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
361
362     void vPortSVCHandler( void ) /* __attribute__( ( naked ) ) PRIVILEGED_FUNCTION */
363     {
364         /* Assumes psp was in use. */
365         __asm volatile
366         (
367             #ifndef USE_PROCESS_STACK /* Code should not be required if a main() is using the process stack. */
368                 "   tst lr, #4                      \n"
369                 "   ite eq                          \n"
370                 "   mrseq r0, msp                   \n"
371                 "   mrsne r0, psp                   \n"
372             #else
373                 "   mrs r0, psp                     \n"
374             #endif
375             "   b %0                            \n"
376             ::"i" ( vSVCHandler_C ) : "r0", "memory"
377         );
378     }
379
380 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
381 /*-----------------------------------------------------------*/
382
383 void vSVCHandler_C( uint32_t * pulParam ) /* PRIVILEGED_FUNCTION */
384 {
385     uint8_t ucSVCNumber;
386     uint32_t ulPC;
387
388     #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
389         #if defined( __ARMCC_VERSION )
390
391             /* Declaration when these variable are defined in code instead of being
392              * exported from linker scripts. */
393             extern uint32_t * __syscalls_flash_start__;
394             extern uint32_t * __syscalls_flash_end__;
395         #else
396             /* Declaration when these variable are exported from linker scripts. */
397             extern uint32_t __syscalls_flash_start__[];
398             extern uint32_t __syscalls_flash_end__[];
399         #endif /* #if defined( __ARMCC_VERSION ) */
400     #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
401
402     /* The stack contains: r0, r1, r2, r3, r12, LR, PC and xPSR.  The first
403      * argument (r0) is pulParam[ 0 ]. */
404     ulPC = pulParam[ portOFFSET_TO_PC ];
405     ucSVCNumber = ( ( uint8_t * ) ulPC )[ -2 ];
406
407     switch( ucSVCNumber )
408     {
409         case portSVC_START_SCHEDULER:
410             portNVIC_SHPR2_REG |= portNVIC_SVC_PRI;
411             prvRestoreContextOfFirstTask();
412             break;
413
414         case portSVC_YIELD:
415             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
416
417             /* Barriers are normally not required
418              * but do ensure the code is completely
419              * within the specified behaviour for the
420              * architecture. */
421             __asm volatile ( "dsb" ::: "memory" );
422             __asm volatile ( "isb" );
423
424             break;
425
426             #if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 )
427                 case portSVC_RAISE_PRIVILEGE: /* Only raise the privilege, if the
428                                                * svc was raised from any of the
429                                                * system calls. */
430
431                     if( ( ulPC >= ( uint32_t ) __syscalls_flash_start__ ) &&
432                         ( ulPC <= ( uint32_t ) __syscalls_flash_end__ ) )
433                     {
434                         __asm volatile
435                         (
436                             "   mrs r1, control     \n" /* Obtain current control value. */
437                             "   bic r1, #1          \n" /* Set privilege bit. */
438                             "   msr control, r1     \n" /* Write back new control value. */
439                             ::: "r1", "memory"
440                         );
441                     }
442
443                     break;
444             #else /* if ( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
445                 case portSVC_RAISE_PRIVILEGE:
446                     __asm volatile
447                     (
448                         "   mrs r1, control     \n" /* Obtain current control value. */
449                         "   bic r1, #1          \n" /* Set privilege bit. */
450                         "   msr control, r1     \n" /* Write back new control value. */
451                         ::: "r1", "memory"
452                     );
453                     break;
454                     #endif /* #if( configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY == 1 ) */
455
456                 default: /* Unknown SVC call. */
457                     break;
458     }
459 }
460 /*-----------------------------------------------------------*/
461
462 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
463
464     void vSystemCallEnter( uint32_t * pulTaskStack ) /* PRIVILEGED_FUNCTION */
465     {
466         extern TaskHandle_t pxCurrentTCB;
467         xMPU_SETTINGS * pxMpuSettings;
468         uint32_t * pulSystemCallStack;
469         uint32_t ulSystemCallLocation, i;
470         const uint32_t ulStackFrameSize = 8;
471
472         #if defined( __ARMCC_VERSION )
473
474             /* Declaration when these variable are defined in code instead of being
475              * exported from linker scripts. */
476             extern uint32_t * __syscalls_flash_start__;
477             extern uint32_t * __syscalls_flash_end__;
478         #else
479             /* Declaration when these variable are exported from linker scripts. */
480             extern uint32_t __syscalls_flash_start__[];
481             extern uint32_t __syscalls_flash_end__[];
482         #endif /* #if defined( __ARMCC_VERSION ) */
483
484         ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
485
486         /* If the request did not come from the system call section, do nothing. */
487         if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
488             ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
489         {
490             pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
491             pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
492
493             /* This is not NULL only for the duration of the system call. */
494             configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
495
496             /* Make space on the system call stack for the stack frame. */
497             pulSystemCallStack = pulSystemCallStack - ulStackFrameSize;
498
499             /* Copy the stack frame. */
500             for( i = 0; i < ulStackFrameSize; i++ )
501             {
502                 pulSystemCallStack[ i ] = pulTaskStack[ i ];
503             }
504
505             /* Use the pulSystemCallStack in thread mode. */
506             __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
507
508             /* Raise the privilege for the duration of the system call. */
509             __asm volatile (
510                 " mrs r1, control     \n" /* Obtain current control value. */
511                 " bic r1, #1          \n" /* Clear nPRIV bit. */
512                 " msr control, r1     \n" /* Write back new control value. */
513                 ::: "r1", "memory"
514                 );
515
516             /* Remember the location where we should copy the stack frame when we exit from
517              * the system call. */
518             pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
519
520             /* Store the value of the Link Register before the SVC was raised. We need to
521              * restore it when we exit from the system call. */
522             pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
523
524             /* Record if the hardware used padding to force the stack pointer
525              * to be double word aligned. */
526             if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
527             {
528                 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
529             }
530             else
531             {
532                 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
533             }
534
535             /* We ensure in pxPortInitialiseStack that the system call stack is
536              * double word aligned and therefore, there is no need of padding.
537              * Clear the bit[9] of stacked xPSR. */
538             pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
539         }
540     }
541
542 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
543 /*-----------------------------------------------------------*/
544
545 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
546
547     void vSystemCallEnter_1( uint32_t * pulTaskStack ) /* PRIVILEGED_FUNCTION */
548     {
549         extern TaskHandle_t pxCurrentTCB;
550         xMPU_SETTINGS * pxMpuSettings;
551         uint32_t * pulSystemCallStack;
552         uint32_t ulSystemCallLocation, i;
553         const uint32_t ulStackFrameSize = 8;
554
555         #if defined( __ARMCC_VERSION )
556
557             /* Declaration when these variable are defined in code instead of being
558              * exported from linker scripts. */
559             extern uint32_t * __syscalls_flash_start__;
560             extern uint32_t * __syscalls_flash_end__;
561         #else
562             /* Declaration when these variable are exported from linker scripts. */
563             extern uint32_t __syscalls_flash_start__[];
564             extern uint32_t __syscalls_flash_end__[];
565         #endif /* #if defined( __ARMCC_VERSION ) */
566
567         ulSystemCallLocation = pulTaskStack[ portOFFSET_TO_PC ];
568
569         /* If the request did not come from the system call section, do nothing. */
570         if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
571             ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
572         {
573             pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
574             pulSystemCallStack = pxMpuSettings->xSystemCallStackInfo.pulSystemCallStack;
575
576             /* This is not NULL only for the duration of the system call. */
577             configASSERT( pxMpuSettings->xSystemCallStackInfo.pulTaskStack == NULL );
578
579             /* Make space on the system call stack for the stack frame and
580              * the parameter passed on the stack. We only need to copy one
581              * parameter but we still reserve 2 spaces to keep the stack
582              * double word aligned. */
583             pulSystemCallStack = pulSystemCallStack - ulStackFrameSize - 2UL;
584
585             /* Copy the stack frame. */
586             for( i = 0; i < ulStackFrameSize; i++ )
587             {
588                 pulSystemCallStack[ i ] = pulTaskStack[ i ];
589             }
590
591             /* Copy the parameter which is passed the stack. */
592             if( ( pulTaskStack[ portOFFSET_TO_PSR ] & portPSR_STACK_PADDING_MASK ) == portPSR_STACK_PADDING_MASK )
593             {
594                 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize + 1 ];
595
596                 /* Record if the hardware used padding to force the stack pointer
597                  * to be double word aligned. */
598                 pxMpuSettings->ulTaskFlags |= portSTACK_FRAME_HAS_PADDING_FLAG;
599             }
600             else
601             {
602                 pulSystemCallStack[ ulStackFrameSize ] = pulTaskStack[ ulStackFrameSize ];
603
604                 /* Record if the hardware used padding to force the stack pointer
605                  * to be double word aligned. */
606                 pxMpuSettings->ulTaskFlags &= ( ~portSTACK_FRAME_HAS_PADDING_FLAG );
607             }
608
609             /* Use the pulSystemCallStack in thread mode. */
610             __asm volatile ( "msr psp, %0" : : "r" ( pulSystemCallStack ) );
611
612             /* Raise the privilege for the duration of the system call. */
613             __asm volatile (
614                 " mrs r1, control     \n" /* Obtain current control value. */
615                 " bic r1, #1          \n" /* Clear nPRIV bit. */
616                 " msr control, r1     \n" /* Write back new control value. */
617                 ::: "r1", "memory"
618                 );
619
620             /* Remember the location where we should copy the stack frame when we exit from
621              * the system call. */
622             pxMpuSettings->xSystemCallStackInfo.pulTaskStack = pulTaskStack + ulStackFrameSize;
623
624             /* Store the value of the Link Register before the SVC was raised. We need to
625              * restore it when we exit from the system call. */
626             pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry = pulTaskStack[ portOFFSET_TO_LR ];
627
628             /* We ensure in pxPortInitialiseStack that the system call stack is
629              * double word aligned and therefore, there is no need of padding.
630              * Clear the bit[9] of stacked xPSR. */
631             pulSystemCallStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
632         }
633     }
634
635 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
636 /*-----------------------------------------------------------*/
637
638 #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
639
640     void vSystemCallExit( uint32_t * pulSystemCallStack ) /* PRIVILEGED_FUNCTION */
641     {
642         extern TaskHandle_t pxCurrentTCB;
643         xMPU_SETTINGS * pxMpuSettings;
644         uint32_t * pulTaskStack;
645         uint32_t ulSystemCallLocation, i;
646         const uint32_t ulStackFrameSize = 8;
647
648         #if defined( __ARMCC_VERSION )
649
650             /* Declaration when these variable are defined in code instead of being
651              * exported from linker scripts. */
652             extern uint32_t * __syscalls_flash_start__;
653             extern uint32_t * __syscalls_flash_end__;
654         #else
655             /* Declaration when these variable are exported from linker scripts. */
656             extern uint32_t __syscalls_flash_start__[];
657             extern uint32_t __syscalls_flash_end__[];
658         #endif /* #if defined( __ARMCC_VERSION ) */
659
660         ulSystemCallLocation = pulSystemCallStack[ portOFFSET_TO_PC ];
661
662         /* If the request did not come from the system call section, do nothing. */
663         if( ( ulSystemCallLocation >= ( uint32_t ) __syscalls_flash_start__ ) &&
664             ( ulSystemCallLocation <= ( uint32_t ) __syscalls_flash_end__ ) )
665         {
666             pxMpuSettings = xTaskGetMPUSettings( pxCurrentTCB );
667             pulTaskStack = pxMpuSettings->xSystemCallStackInfo.pulTaskStack;
668
669             /* Make space on the task stack for the stack frame. */
670             pulTaskStack = pulTaskStack - ulStackFrameSize;
671
672             /* Copy the stack frame. */
673             for( i = 0; i < ulStackFrameSize; i++ )
674             {
675                 pulTaskStack[ i ] = pulSystemCallStack[ i ];
676             }
677
678             /* Use the pulTaskStack in thread mode. */
679             __asm volatile ( "msr psp, %0" : : "r" ( pulTaskStack ) );
680
681             /* Drop the privilege before returning to the thread mode. */
682             __asm volatile (
683                 " mrs r1, control     \n" /* Obtain current control value. */
684                 " orr r1, #1          \n" /* Set nPRIV bit. */
685                 " msr control, r1     \n" /* Write back new control value. */
686                 ::: "r1", "memory"
687                 );
688
689             /* Restore the stacked link register to what it was at the time of
690              * system call entry. */
691             pulTaskStack[ portOFFSET_TO_LR ] = pxMpuSettings->xSystemCallStackInfo.ulLinkRegisterAtSystemCallEntry;
692
693             /* If the hardware used padding to force the stack pointer
694              * to be double word aligned, set the stacked xPSR bit[9],
695              * otherwise clear it. */
696             if( ( pxMpuSettings->ulTaskFlags & portSTACK_FRAME_HAS_PADDING_FLAG ) == portSTACK_FRAME_HAS_PADDING_FLAG )
697             {
698                 pulTaskStack[ portOFFSET_TO_PSR ] |= portPSR_STACK_PADDING_MASK;
699             }
700             else
701             {
702                 pulTaskStack[ portOFFSET_TO_PSR ] &= ( ~portPSR_STACK_PADDING_MASK );
703             }
704
705             /* This is not NULL only for the duration of the system call. */
706             pxMpuSettings->xSystemCallStackInfo.pulTaskStack = NULL;
707         }
708     }
709
710 #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
711 /*-----------------------------------------------------------*/
712
713 BaseType_t xPortIsTaskPrivileged( void ) /* PRIVILEGED_FUNCTION */
714 {
715     BaseType_t xTaskIsPrivileged = pdFALSE;
716     const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
717
718     if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
719     {
720         xTaskIsPrivileged = pdTRUE;
721     }
722
723     return xTaskIsPrivileged;
724 }
725 /*-----------------------------------------------------------*/
726
727 static void prvRestoreContextOfFirstTask( void )
728 {
729     __asm volatile
730     (
731         " ldr r0, =0xE000ED08                   \n" /* Use the NVIC offset register to locate the stack. */
732         " ldr r0, [r0]                          \n"
733         " ldr r0, [r0]                          \n"
734         " msr msp, r0                           \n" /* Set the msp back to the start of the stack. */
735         "                                       \n"
736         /*------------ Program MPU. ------------ */
737         " ldr r3, pxCurrentTCBConst2            \n" /* r3 = pxCurrentTCBConst2. */
738         " ldr r2, [r3]                          \n" /* r2 = pxCurrentTCB. */
739         " add r2, r2, #4                        \n" /* r2 = Second item in the TCB which is xMPUSettings. */
740         "                                       \n"
741         " dmb                                   \n" /* Complete outstanding transfers before disabling MPU. */
742         " ldr r0, =0xe000ed94                   \n" /* MPU_CTRL register. */
743         " ldr r3, [r0]                          \n" /* Read the value of MPU_CTRL. */
744         " bic r3, #1                            \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
745         " str r3, [r0]                          \n" /* Disable MPU. */
746         "                                       \n"
747         " ldr r0, =0xe000ed9c                   \n" /* Region Base Address register. */
748         " ldmia r2!, {r4-r11}                   \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
749         " stmia r0, {r4-r11}                    \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
750         "                                       \n"
751         " ldr r0, =0xe000ed94                   \n" /* MPU_CTRL register. */
752         " ldr r3, [r0]                          \n" /* Read the value of MPU_CTRL. */
753         " orr r3, #1                            \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
754         " str r3, [r0]                          \n" /* Enable MPU. */
755         " dsb                                   \n" /* Force memory writes before continuing. */
756         "                                       \n"
757         /*---------- Restore Context. ---------- */
758         " ldr r3, pxCurrentTCBConst2            \n" /* r3 = pxCurrentTCBConst2. */
759         " ldr r2, [r3]                          \n" /* r2 = pxCurrentTCB. */
760         " ldr r1, [r2]                          \n" /* r1 = Location of saved context in TCB. */
761         "                                       \n"
762         " ldmdb r1!, {r0, r4-r11}               \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
763         " msr psp, r0                           \n"
764         " stmia r0, {r4-r11}                    \n" /* Copy the hardware saved context on the task stack. */
765         " ldmdb r1!, {r3-r11, lr}               \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
766         " msr control, r3                       \n"
767         " str r1, [r2]                          \n" /* Save the location where the context should be saved next as the first member of TCB. */
768         "                                       \n"
769         " mov r0, #0                            \n"
770         " msr basepri, r0                       \n"
771         " bx lr                                 \n"
772         "                                       \n"
773         " .ltorg                                \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
774         " .align 4                              \n"
775         "pxCurrentTCBConst2: .word pxCurrentTCB \n"
776     );
777 }
778 /*-----------------------------------------------------------*/
779
780 /*
781  * See header file for description.
782  */
783 BaseType_t xPortStartScheduler( void )
784 {
785     #if ( configASSERT_DEFINED == 1 )
786     {
787         volatile uint8_t ucOriginalPriority;
788         volatile uint32_t ulImplementedPrioBits = 0;
789         volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
790         volatile uint8_t ucMaxPriorityValue;
791
792         /* Determine the maximum priority from which ISR safe FreeRTOS API
793          * functions can be called.  ISR safe functions are those that end in
794          * "FromISR".  FreeRTOS maintains separate thread and ISR API functions
795          * to ensure interrupt entry is as fast and simple as possible.
796          *
797          * Save the interrupt priority value that is about to be clobbered. */
798         ucOriginalPriority = *pucFirstUserPriorityRegister;
799
800         /* Determine the number of priority bits available.  First write to all
801          * possible bits. */
802         *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
803
804         /* Read the value back to see how many bits stuck. */
805         ucMaxPriorityValue = *pucFirstUserPriorityRegister;
806
807         /* Use the same mask on the maximum system call priority. */
808         ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
809
810         /* Check that the maximum system call priority is nonzero after
811          * accounting for the number of priority bits supported by the
812          * hardware. A priority of 0 is invalid because setting the BASEPRI
813          * register to 0 unmasks all interrupts, and interrupts with priority 0
814          * cannot be masked using BASEPRI.
815          * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
816         configASSERT( ucMaxSysCallPriority );
817
818         /* Check that the bits not implemented in hardware are zero in
819          * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
820         configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
821
822         /* Calculate the maximum acceptable priority group value for the number
823          * of bits read back. */
824
825         while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
826         {
827             ulImplementedPrioBits++;
828             ucMaxPriorityValue <<= ( uint8_t ) 0x01;
829         }
830
831         if( ulImplementedPrioBits == 8 )
832         {
833             /* When the hardware implements 8 priority bits, there is no way for
834              * the software to configure PRIGROUP to not have sub-priorities. As
835              * a result, the least significant bit is always used for sub-priority
836              * and there are 128 preemption priorities and 2 sub-priorities.
837              *
838              * This may cause some confusion in some cases - for example, if
839              * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
840              * priority interrupts will be masked in Critical Sections as those
841              * are at the same preemption priority. This may appear confusing as
842              * 4 is higher (numerically lower) priority than
843              * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
844              * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
845              * to 4, this confusion does not happen and the behaviour remains the same.
846              *
847              * The following assert ensures that the sub-priority bit in the
848              * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
849              * confusion. */
850             configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
851             ulMaxPRIGROUPValue = 0;
852         }
853         else
854         {
855             ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
856         }
857
858         /* Shift the priority group value back to its position within the AIRCR
859          * register. */
860         ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
861         ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
862
863         /* Restore the clobbered interrupt priority register to its original
864          * value. */
865         *pucFirstUserPriorityRegister = ucOriginalPriority;
866     }
867     #endif /* configASSERT_DEFINED */
868
869     /* Make PendSV and SysTick the same priority as the kernel, and the SVC
870      * handler higher priority so it can be used to exit a critical section (where
871      * lower priorities are masked). */
872     portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
873     portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
874
875     /* Configure the regions in the MPU that are common to all tasks. */
876     prvSetupMPU();
877
878     /* Start the timer that generates the tick ISR.  Interrupts are disabled
879      * here already. */
880     vPortSetupTimerInterrupt();
881
882     /* Initialise the critical nesting count ready for the first task. */
883     uxCriticalNesting = 0;
884
885     /* Start the first task. */
886     __asm volatile (
887         " ldr r0, =0xE000ED08   \n" /* Use the NVIC offset register to locate the stack. */
888         " ldr r0, [r0]          \n"
889         " ldr r0, [r0]          \n"
890         " msr msp, r0           \n" /* Set the msp back to the start of the stack. */
891         " cpsie i               \n" /* Globally enable interrupts. */
892         " cpsie f               \n"
893         " dsb                   \n"
894         " isb                   \n"
895         " svc %0                \n" /* System call to start first task. */
896         " nop                   \n"
897         " .ltorg                \n"
898         ::"i" ( portSVC_START_SCHEDULER ) : "memory" );
899
900     /* Should not get here! */
901     return 0;
902 }
903 /*-----------------------------------------------------------*/
904
905 void vPortEndScheduler( void )
906 {
907     /* Not implemented in ports where there is nothing to return to.
908      * Artificially force an assert. */
909     configASSERT( uxCriticalNesting == 1000UL );
910 }
911 /*-----------------------------------------------------------*/
912
913 void vPortEnterCritical( void )
914 {
915     #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
916         if( portIS_PRIVILEGED() == pdFALSE )
917         {
918             portRAISE_PRIVILEGE();
919             portMEMORY_BARRIER();
920
921             portDISABLE_INTERRUPTS();
922             uxCriticalNesting++;
923             portMEMORY_BARRIER();
924
925             portRESET_PRIVILEGE();
926             portMEMORY_BARRIER();
927         }
928         else
929         {
930             portDISABLE_INTERRUPTS();
931             uxCriticalNesting++;
932         }
933     #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
934         portDISABLE_INTERRUPTS();
935         uxCriticalNesting++;
936     #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
937 }
938 /*-----------------------------------------------------------*/
939
940 void vPortExitCritical( void )
941 {
942     #if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 )
943         if( portIS_PRIVILEGED() == pdFALSE )
944         {
945             portRAISE_PRIVILEGE();
946             portMEMORY_BARRIER();
947
948             configASSERT( uxCriticalNesting );
949             uxCriticalNesting--;
950
951             if( uxCriticalNesting == 0 )
952             {
953                 portENABLE_INTERRUPTS();
954             }
955
956             portMEMORY_BARRIER();
957
958             portRESET_PRIVILEGE();
959             portMEMORY_BARRIER();
960         }
961         else
962         {
963             configASSERT( uxCriticalNesting );
964             uxCriticalNesting--;
965
966             if( uxCriticalNesting == 0 )
967             {
968                 portENABLE_INTERRUPTS();
969             }
970         }
971     #else /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
972         configASSERT( uxCriticalNesting );
973         uxCriticalNesting--;
974
975         if( uxCriticalNesting == 0 )
976         {
977             portENABLE_INTERRUPTS();
978         }
979     #endif /* if ( configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS == 1 ) */
980 }
981 /*-----------------------------------------------------------*/
982
983 void xPortPendSVHandler( void )
984 {
985     /* This is a naked function. */
986
987     __asm volatile
988     (
989         " ldr r3, pxCurrentTCBConst             \n" /* r3 = pxCurrentTCBConst. */
990         " ldr r2, [r3]                          \n" /* r2 = pxCurrentTCB. */
991         " ldr r1, [r2]                          \n" /* r1 = Location where the context should be saved. */
992         "                                       \n"
993         /*------------ Save Context. ----------- */
994         " mrs r3, control                       \n"
995         " mrs r0, psp                           \n"
996         " isb                                   \n"
997         "                                       \n"
998         " stmia r1!, {r3-r11, lr}               \n" /* Store CONTROL register, r4-r11 and LR. */
999         " ldmia r0, {r4-r11}                    \n" /* Copy hardware saved context into r4-r11. */
1000         " stmia r1!, {r0, r4-r11}               \n" /* Store original PSP (after hardware has saved context) and the hardware saved context. */
1001         " str r1, [r2]                          \n" /* Save the location from where the context should be restored as the first member of TCB. */
1002         "                                       \n"
1003         /*---------- Select next task. --------- */
1004         " mov r0, %0                            \n"
1005         " msr basepri, r0                       \n"
1006         " dsb                                   \n"
1007         " isb                                   \n"
1008         " bl vTaskSwitchContext                 \n"
1009         " mov r0, #0                            \n"
1010         " msr basepri, r0                       \n"
1011         "                                       \n"
1012         /*------------ Program MPU. ------------ */
1013         " ldr r3, pxCurrentTCBConst             \n" /* r3 = pxCurrentTCBConst. */
1014         " ldr r2, [r3]                          \n" /* r2 = pxCurrentTCB. */
1015         " add r2, r2, #4                        \n" /* r2 = Second item in the TCB which is xMPUSettings. */
1016         "                                       \n"
1017         " dmb                                   \n" /* Complete outstanding transfers before disabling MPU. */
1018         " ldr r0, =0xe000ed94                   \n" /* MPU_CTRL register. */
1019         " ldr r3, [r0]                          \n" /* Read the value of MPU_CTRL. */
1020         " bic r3, #1                            \n" /* r3 = r3 & ~1 i.e. Clear the bit 0 in r3. */
1021         " str r3, [r0]                          \n" /* Disable MPU. */
1022         "                                       \n"
1023         " ldr r0, =0xe000ed9c                   \n" /* Region Base Address register. */
1024         " ldmia r2!, {r4-r11}                   \n" /* Read 4 sets of MPU registers [MPU Region # 0 - 3]. */
1025         " stmia r0, {r4-r11}                    \n" /* Write 4 sets of MPU registers [MPU Region # 0 - 3]. */
1026         "                                       \n"
1027         " ldr r0, =0xe000ed94                   \n" /* MPU_CTRL register. */
1028         " ldr r3, [r0]                          \n" /* Read the value of MPU_CTRL. */
1029         " orr r3, #1                            \n" /* r3 = r3 | 1 i.e. Set the bit 0 in r3. */
1030         " str r3, [r0]                          \n" /* Enable MPU. */
1031         " dsb                                   \n" /* Force memory writes before continuing. */
1032         "                                       \n"
1033         /*---------- Restore Context. ---------- */
1034         " ldr r3, pxCurrentTCBConst             \n" /* r3 = pxCurrentTCBConst. */
1035         " ldr r2, [r3]                          \n" /* r2 = pxCurrentTCB. */
1036         " ldr r1, [r2]                          \n" /* r1 = Location of saved context in TCB. */
1037         "                                       \n"
1038         " ldmdb r1!, {r0, r4-r11}               \n" /* r0 contains PSP after the hardware had saved context. r4-r11 contain hardware saved context. */
1039         " msr psp, r0                           \n"
1040         " stmia r0, {r4-r11}                    \n" /* Copy the hardware saved context on the task stack. */
1041         " ldmdb r1!, {r3-r11, lr}               \n" /* r3 contains CONTROL register. r4-r11 and LR restored. */
1042         " msr control, r3                       \n"
1043         "                                       \n"
1044         " str r1, [r2]                          \n" /* Save the location where the context should be saved next as the first member of TCB. */
1045         " bx lr                                 \n"
1046         "                                       \n"
1047         " .ltorg                                \n" /* Assemble current literal pool to avoid offset-out-of-bound errors with lto. */
1048         " .align 4                              \n"
1049         "pxCurrentTCBConst: .word pxCurrentTCB  \n"
1050         ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
1051     );
1052 }
1053 /*-----------------------------------------------------------*/
1054
1055 void xPortSysTickHandler( void )
1056 {
1057     uint32_t ulDummy;
1058
1059     ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
1060     {
1061         /* Increment the RTOS tick. */
1062         if( xTaskIncrementTick() != pdFALSE )
1063         {
1064             /* Pend a context switch. */
1065             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
1066         }
1067     }
1068     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
1069 }
1070 /*-----------------------------------------------------------*/
1071
1072 /*
1073  * Setup the systick timer to generate the tick interrupts at the required
1074  * frequency.
1075  */
1076 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
1077 {
1078     /* Stop and clear the SysTick. */
1079     portNVIC_SYSTICK_CTRL_REG = 0UL;
1080     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
1081
1082     /* Configure SysTick to interrupt at the requested rate. */
1083     portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
1084     portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE );
1085 }
1086 /*-----------------------------------------------------------*/
1087
1088 static void prvSetupMPU( void )
1089 {
1090     extern uint32_t __privileged_functions_start__[];
1091     extern uint32_t __privileged_functions_end__[];
1092     extern uint32_t __FLASH_segment_start__[];
1093     extern uint32_t __FLASH_segment_end__[];
1094     extern uint32_t __privileged_data_start__[];
1095     extern uint32_t __privileged_data_end__[];
1096
1097     /* Check the expected MPU is present. */
1098     if( portMPU_TYPE_REG == portEXPECTED_MPU_TYPE_VALUE )
1099     {
1100         /* First setup the unprivileged flash for unprivileged read only access. */
1101         portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __FLASH_segment_start__ ) | /* Base address. */
1102                                           ( portMPU_REGION_VALID ) |
1103                                           ( portUNPRIVILEGED_FLASH_REGION );
1104
1105         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_ONLY ) |
1106                                        ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1107                                        ( prvGetMPURegionSizeSetting( ( uint32_t ) __FLASH_segment_end__ - ( uint32_t ) __FLASH_segment_start__ ) ) |
1108                                        ( portMPU_REGION_ENABLE );
1109
1110         /* Setup the privileged flash for privileged only access.  This is where
1111          * the kernel code is * placed. */
1112         portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_functions_start__ ) | /* Base address. */
1113                                           ( portMPU_REGION_VALID ) |
1114                                           ( portPRIVILEGED_FLASH_REGION );
1115
1116         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_ONLY ) |
1117                                        ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1118                                        ( prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_functions_end__ - ( uint32_t ) __privileged_functions_start__ ) ) |
1119                                        ( portMPU_REGION_ENABLE );
1120
1121         /* Setup the privileged data RAM region.  This is where the kernel data
1122          * is placed. */
1123         portMPU_REGION_BASE_ADDRESS_REG = ( ( uint32_t ) __privileged_data_start__ ) | /* Base address. */
1124                                           ( portMPU_REGION_VALID ) |
1125                                           ( portPRIVILEGED_RAM_REGION );
1126
1127         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_PRIVILEGED_READ_WRITE ) |
1128                                        ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1129                                        ( portMPU_REGION_EXECUTE_NEVER ) |
1130                                        prvGetMPURegionSizeSetting( ( uint32_t ) __privileged_data_end__ - ( uint32_t ) __privileged_data_start__ ) |
1131                                        ( portMPU_REGION_ENABLE );
1132
1133         /* By default allow everything to access the general peripherals.  The
1134          * system peripherals and registers are protected. */
1135         portMPU_REGION_BASE_ADDRESS_REG = ( portPERIPHERALS_START_ADDRESS ) |
1136                                           ( portMPU_REGION_VALID ) |
1137                                           ( portGENERAL_PERIPHERALS_REGION );
1138
1139         portMPU_REGION_ATTRIBUTE_REG = ( portMPU_REGION_READ_WRITE | portMPU_REGION_EXECUTE_NEVER ) |
1140                                        ( prvGetMPURegionSizeSetting( portPERIPHERALS_END_ADDRESS - portPERIPHERALS_START_ADDRESS ) ) |
1141                                        ( portMPU_REGION_ENABLE );
1142
1143         /* Enable the memory fault exception. */
1144         portNVIC_SYS_CTRL_STATE_REG |= portNVIC_MEM_FAULT_ENABLE;
1145
1146         /* Enable the MPU with the background region configured. */
1147         portMPU_CTRL_REG |= ( portMPU_ENABLE | portMPU_BACKGROUND_ENABLE );
1148     }
1149 }
1150 /*-----------------------------------------------------------*/
1151
1152 static uint32_t prvGetMPURegionSizeSetting( uint32_t ulActualSizeInBytes )
1153 {
1154     uint32_t ulRegionSize, ulReturnValue = 4;
1155
1156     /* 32 is the smallest region size, 31 is the largest valid value for
1157      * ulReturnValue. */
1158     for( ulRegionSize = 32UL; ulReturnValue < 31UL; ( ulRegionSize <<= 1UL ) )
1159     {
1160         if( ulActualSizeInBytes <= ulRegionSize )
1161         {
1162             break;
1163         }
1164         else
1165         {
1166             ulReturnValue++;
1167         }
1168     }
1169
1170     /* Shift the code by one before returning so it can be written directly
1171      * into the the correct bit position of the attribute register. */
1172     return( ulReturnValue << 1UL );
1173 }
1174 /*-----------------------------------------------------------*/
1175
1176 BaseType_t xIsPrivileged( void ) /* __attribute__ (( naked )) */
1177 {
1178     __asm volatile
1179     (
1180         "   mrs r0, control                         \n" /* r0 = CONTROL. */
1181         "   tst r0, #1                              \n" /* Perform r0 & 1 (bitwise AND) and update the conditions flag. */
1182         "   ite ne                                  \n"
1183         "   movne r0, #0                            \n" /* CONTROL[0]!=0. Return false to indicate that the processor is not privileged. */
1184         "   moveq r0, #1                            \n" /* CONTROL[0]==0. Return true to indicate that the processor is privileged. */
1185         "   bx lr                                   \n" /* Return. */
1186         "                                           \n"
1187         "   .align 4                                \n"
1188         ::: "r0", "memory"
1189     );
1190 }
1191 /*-----------------------------------------------------------*/
1192
1193 void vResetPrivilege( void ) /* __attribute__ (( naked )) */
1194 {
1195     __asm volatile
1196     (
1197         "   mrs r0, control                         \n" /* r0 = CONTROL. */
1198         "   orr r0, #1                              \n" /* r0 = r0 | 1. */
1199         "   msr control, r0                         \n" /* CONTROL = r0. */
1200         "   bx lr                                   \n" /* Return to the caller. */
1201         ::: "r0", "memory"
1202     );
1203 }
1204 /*-----------------------------------------------------------*/
1205
1206 void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
1207                                 const struct xMEMORY_REGION * const xRegions,
1208                                 StackType_t * pxBottomOfStack,
1209                                 uint32_t ulStackDepth )
1210 {
1211     extern uint32_t __SRAM_segment_start__[];
1212     extern uint32_t __SRAM_segment_end__[];
1213     extern uint32_t __privileged_data_start__[];
1214     extern uint32_t __privileged_data_end__[];
1215     int32_t lIndex;
1216     uint32_t ul;
1217
1218     if( xRegions == NULL )
1219     {
1220         /* No MPU regions are specified so allow access to all RAM. */
1221         xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1222             ( ( uint32_t ) __SRAM_segment_start__ ) | /* Base address. */
1223             ( portMPU_REGION_VALID ) |
1224             ( portSTACK_REGION );                     /* Region number. */
1225
1226         xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1227             ( portMPU_REGION_READ_WRITE ) |
1228             ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1229             ( portMPU_REGION_EXECUTE_NEVER ) |
1230             ( prvGetMPURegionSizeSetting( ( uint32_t ) __SRAM_segment_end__ - ( uint32_t ) __SRAM_segment_start__ ) ) |
1231             ( portMPU_REGION_ENABLE );
1232
1233         xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) __SRAM_segment_start__;
1234         xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) __SRAM_segment_end__;
1235         xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1236                                                                    tskMPU_WRITE_PERMISSION );
1237
1238         /* Invalidate user configurable regions. */
1239         for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1240         {
1241             xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1242             xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1243             xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1244             xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1245             xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1246         }
1247     }
1248     else
1249     {
1250         /* This function is called automatically when the task is created - in
1251          * which case the stack region parameters will be valid.  At all other
1252          * times the stack parameters will not be valid and it is assumed that the
1253          * stack region has already been configured. */
1254         if( ulStackDepth > 0 )
1255         {
1256             /* Define the region that allows access to the stack. */
1257             xMPUSettings->xRegion[ 0 ].ulRegionBaseAddress =
1258                 ( ( uint32_t ) pxBottomOfStack ) |
1259                 ( portMPU_REGION_VALID ) |
1260                 ( portSTACK_REGION ); /* Region number. */
1261
1262             xMPUSettings->xRegion[ 0 ].ulRegionAttribute =
1263                 ( portMPU_REGION_READ_WRITE ) |
1264                 ( portMPU_REGION_EXECUTE_NEVER ) |
1265                 ( prvGetMPURegionSizeSetting( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) ) |
1266                 ( portMPU_REGION_CACHEABLE_BUFFERABLE ) |
1267                 ( portMPU_REGION_ENABLE );
1268             xMPUSettings->xRegionSettings[ 0 ].ulRegionStartAddress = ( uint32_t ) pxBottomOfStack;
1269             xMPUSettings->xRegionSettings[ 0 ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) ( pxBottomOfStack ) +
1270                                                                                    ( ulStackDepth * ( uint32_t ) sizeof( StackType_t ) ) - 1UL );
1271             xMPUSettings->xRegionSettings[ 0 ].ulRegionPermissions = ( tskMPU_READ_PERMISSION |
1272                                                                        tskMPU_WRITE_PERMISSION );
1273         }
1274
1275         lIndex = 0;
1276
1277         for( ul = 1UL; ul <= portNUM_CONFIGURABLE_REGIONS; ul++ )
1278         {
1279             if( ( xRegions[ lIndex ] ).ulLengthInBytes > 0UL )
1280             {
1281                 /* Translate the generic region definition contained in
1282                  * xRegions into the CM3 specific MPU settings that are then
1283                  * stored in xMPUSettings. */
1284                 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress =
1285                     ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress ) |
1286                     ( portMPU_REGION_VALID ) |
1287                     ( ul - 1UL ); /* Region number. */
1288
1289                 xMPUSettings->xRegion[ ul ].ulRegionAttribute =
1290                     ( prvGetMPURegionSizeSetting( xRegions[ lIndex ].ulLengthInBytes ) ) |
1291                     ( xRegions[ lIndex ].ulParameters ) |
1292                     ( portMPU_REGION_ENABLE );
1293
1294                 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = ( uint32_t ) xRegions[ lIndex ].pvBaseAddress;
1295                 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = ( uint32_t ) ( ( uint32_t ) xRegions[ lIndex ].pvBaseAddress + xRegions[ lIndex ].ulLengthInBytes - 1UL );
1296                 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1297
1298                 if( ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_ONLY ) == portMPU_REGION_READ_ONLY ) ||
1299                     ( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) == portMPU_REGION_PRIVILEGED_READ_WRITE_UNPRIV_READ_ONLY ) )
1300                 {
1301                     xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = tskMPU_READ_PERMISSION;
1302                 }
1303
1304                 if( ( xRegions[ lIndex ].ulParameters & portMPU_REGION_READ_WRITE ) == portMPU_REGION_READ_WRITE )
1305                 {
1306                     xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = ( tskMPU_READ_PERMISSION | tskMPU_WRITE_PERMISSION );
1307                 }
1308             }
1309             else
1310             {
1311                 /* Invalidate the region. */
1312                 xMPUSettings->xRegion[ ul ].ulRegionBaseAddress = ( ( ul - 1UL ) | portMPU_REGION_VALID );
1313                 xMPUSettings->xRegion[ ul ].ulRegionAttribute = 0UL;
1314                 xMPUSettings->xRegionSettings[ ul ].ulRegionStartAddress = 0UL;
1315                 xMPUSettings->xRegionSettings[ ul ].ulRegionEndAddress = 0UL;
1316                 xMPUSettings->xRegionSettings[ ul ].ulRegionPermissions = 0UL;
1317             }
1318
1319             lIndex++;
1320         }
1321     }
1322 }
1323 /*-----------------------------------------------------------*/
1324
1325 BaseType_t xPortIsAuthorizedToAccessBuffer( const void * pvBuffer,
1326                                             uint32_t ulBufferLength,
1327                                             uint32_t ulAccessRequested ) /* PRIVILEGED_FUNCTION */
1328
1329 {
1330     uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
1331     BaseType_t xAccessGranted = pdFALSE;
1332     const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */
1333
1334     if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
1335     {
1336         xAccessGranted = pdTRUE;
1337     }
1338     else
1339     {
1340         if( portADD_UINT32_WILL_OVERFLOW( ( ( uint32_t ) pvBuffer ), ( ulBufferLength - 1UL ) ) == pdFALSE )
1341         {
1342             ulBufferStartAddress = ( uint32_t ) pvBuffer;
1343             ulBufferEndAddress = ( ( ( uint32_t ) pvBuffer ) + ulBufferLength - 1UL );
1344
1345             for( i = 0; i < portTOTAL_NUM_REGIONS_IN_TCB; i++ )
1346             {
1347                 if( portIS_ADDRESS_WITHIN_RANGE( ulBufferStartAddress,
1348                                                  xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1349                                                  xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1350                     portIS_ADDRESS_WITHIN_RANGE( ulBufferEndAddress,
1351                                                  xTaskMpuSettings->xRegionSettings[ i ].ulRegionStartAddress,
1352                                                  xTaskMpuSettings->xRegionSettings[ i ].ulRegionEndAddress ) &&
1353                     portIS_AUTHORIZED( ulAccessRequested, xTaskMpuSettings->xRegionSettings[ i ].ulRegionPermissions ) )
1354                 {
1355                     xAccessGranted = pdTRUE;
1356                     break;
1357                 }
1358             }
1359         }
1360     }
1361
1362     return xAccessGranted;
1363 }
1364 /*-----------------------------------------------------------*/
1365
1366 #if ( configASSERT_DEFINED == 1 )
1367
1368     void vPortValidateInterruptPriority( void )
1369     {
1370         uint32_t ulCurrentInterrupt;
1371         uint8_t ucCurrentPriority;
1372
1373         /* Obtain the number of the currently executing interrupt. */
1374         __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
1375
1376         /* Is the interrupt number a user defined interrupt? */
1377         if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
1378         {
1379             /* Look up the interrupt's priority. */
1380             ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
1381
1382             /* The following assertion will fail if a service routine (ISR) for
1383              * an interrupt that has been assigned a priority above
1384              * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
1385              * function.  ISR safe FreeRTOS API functions must *only* be called
1386              * from interrupts that have been assigned a priority at or below
1387              * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1388              *
1389              * Numerically low interrupt priority numbers represent logically high
1390              * interrupt priorities, therefore the priority of the interrupt must
1391              * be set to a value equal to or numerically *higher* than
1392              * configMAX_SYSCALL_INTERRUPT_PRIORITY.
1393              *
1394              * Interrupts that  use the FreeRTOS API must not be left at their
1395              * default priority of  zero as that is the highest possible priority,
1396              * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
1397              * and  therefore also guaranteed to be invalid.
1398              *
1399              * FreeRTOS maintains separate thread and ISR API functions to ensure
1400              * interrupt entry is as fast and simple as possible.
1401              *
1402              * The following links provide detailed information:
1403              * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
1404              * https://www.FreeRTOS.org/FAQHelp.html */
1405             configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
1406         }
1407
1408         /* Priority grouping:  The interrupt controller (NVIC) allows the bits
1409          * that define each interrupt's priority to be split between bits that
1410          * define the interrupt's pre-emption priority bits and bits that define
1411          * the interrupt's sub-priority.  For simplicity all bits must be defined
1412          * to be pre-emption priority bits.  The following assertion will fail if
1413          * this is not the case (if some bits represent a sub-priority).
1414          *
1415          * If the application only uses CMSIS libraries for interrupt
1416          * configuration then the correct setting can be achieved on all Cortex-M
1417          * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
1418          * scheduler.  Note however that some vendor specific peripheral libraries
1419          * assume a non-zero priority group setting, in which cases using a value
1420          * of zero will result in unpredictable behaviour. */
1421         configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
1422     }
1423
1424 #endif /* configASSERT_DEFINED */
1425 /*-----------------------------------------------------------*/