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 CM7 port.
31 *----------------------------------------------------------*/
32 
33 /* Scheduler includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 
37 #ifndef __ARM_FP
38     #error This port can only be used when the project options are configured to enable hardware floating point support.
39 #endif
40 
41 /* Prototype of all Interrupt Service Routines (ISRs). */
42 typedef void ( * portISR_t )( void );
43 
44 /* Constants required to manipulate the core.  Registers first... */
45 #define portNVIC_SYSTICK_CTRL_REG             ( *( ( volatile uint32_t * ) 0xe000e010 ) )
46 #define portNVIC_SYSTICK_LOAD_REG             ( *( ( volatile uint32_t * ) 0xe000e014 ) )
47 #define portNVIC_SYSTICK_CURRENT_VALUE_REG    ( *( ( volatile uint32_t * ) 0xe000e018 ) )
48 #define portNVIC_SHPR2_REG                    ( *( ( volatile uint32_t * ) 0xe000ed1c ) )
49 #define portNVIC_SHPR3_REG                    ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
50 /* ...then bits in the registers. */
51 #define portNVIC_SYSTICK_CLK_BIT              ( 1UL << 2UL )
52 #define portNVIC_SYSTICK_INT_BIT              ( 1UL << 1UL )
53 #define portNVIC_SYSTICK_ENABLE_BIT           ( 1UL << 0UL )
54 #define portNVIC_SYSTICK_COUNT_FLAG_BIT       ( 1UL << 16UL )
55 #define portNVIC_PENDSVCLEAR_BIT              ( 1UL << 27UL )
56 #define portNVIC_PEND_SYSTICK_SET_BIT         ( 1UL << 26UL )
57 #define portNVIC_PEND_SYSTICK_CLEAR_BIT       ( 1UL << 25UL )
58 
59 #define portMIN_INTERRUPT_PRIORITY            ( 255UL )
60 #define portNVIC_PENDSV_PRI                   ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
61 #define portNVIC_SYSTICK_PRI                  ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
62 
63 /* Constants used to check the installation of the FreeRTOS interrupt handlers. */
64 #define portSCB_VTOR_REG                      ( *( ( portISR_t ** ) 0xE000ED08 ) )
65 #define portVECTOR_INDEX_SVC                  ( 11 )
66 #define portVECTOR_INDEX_PENDSV               ( 14 )
67 
68 /* Constants required to check the validity of an interrupt priority. */
69 #define portFIRST_USER_INTERRUPT_NUMBER       ( 16 )
70 #define portNVIC_IP_REGISTERS_OFFSET_16       ( 0xE000E3F0 )
71 #define portAIRCR_REG                         ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
72 #define portMAX_8_BIT_VALUE                   ( ( uint8_t ) 0xff )
73 #define portTOP_BIT_OF_BYTE                   ( ( uint8_t ) 0x80 )
74 #define portMAX_PRIGROUP_BITS                 ( ( uint8_t ) 7 )
75 #define portPRIORITY_GROUP_MASK               ( 0x07UL << 8UL )
76 #define portPRIGROUP_SHIFT                    ( 8UL )
77 
78 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
79 #define portVECTACTIVE_MASK                   ( 0xFFUL )
80 
81 /* Constants required to manipulate the VFP. */
82 #define portFPCCR                             ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
83 #define portASPEN_AND_LSPEN_BITS              ( 0x3UL << 30UL )
84 
85 /* Constants required to set up the initial stack. */
86 #define portINITIAL_XPSR                      ( 0x01000000 )
87 #define portINITIAL_EXC_RETURN                ( 0xfffffffd )
88 
89 /* The systick is a 24-bit counter. */
90 #define portMAX_24_BIT_NUMBER                 ( 0xffffffUL )
91 
92 /* For strict compliance with the Cortex-M spec the task start address should
93  * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
94 #define portSTART_ADDRESS_MASK                ( ( StackType_t ) 0xfffffffeUL )
95 
96 /* A fiddle factor to estimate the number of SysTick counts that would have
97  * occurred while the SysTick counter is stopped during tickless idle
98  * calculations. */
99 #define portMISSED_COUNTS_FACTOR              ( 94UL )
100 
101 /* Let the user override the default SysTick clock rate.  If defined by the
102  * user, this symbol must equal the SysTick clock rate when the CLK bit is 0 in the
103  * configuration register. */
104 #ifndef configSYSTICK_CLOCK_HZ
105     #define configSYSTICK_CLOCK_HZ             ( configCPU_CLOCK_HZ )
106     /* Ensure the SysTick is clocked at the same frequency as the core. */
107     #define portNVIC_SYSTICK_CLK_BIT_CONFIG    ( portNVIC_SYSTICK_CLK_BIT )
108 #else
109     /* Select the option to clock SysTick not at the same frequency as the core. */
110     #define portNVIC_SYSTICK_CLK_BIT_CONFIG    ( 0 )
111 #endif
112 
113 /* Let the user override the pre-loading of the initial LR with the address of
114  * prvTaskExitError() in case it messes up unwinding of the stack in the
115  * debugger. */
116 #ifdef configTASK_RETURN_ADDRESS
117     #define portTASK_RETURN_ADDRESS    configTASK_RETURN_ADDRESS
118 #else
119     #define portTASK_RETURN_ADDRESS    prvTaskExitError
120 #endif
121 
122 /*
123  * Setup the timer to generate the tick interrupts.  The implementation in this
124  * file is weak to allow application writers to change the timer used to
125  * generate the tick interrupt.
126  */
127 void vPortSetupTimerInterrupt( void );
128 
129 /*
130  * Exception handlers.
131  */
132 void xPortPendSVHandler( void ) __attribute__( ( naked ) );
133 void xPortSysTickHandler( void );
134 void vPortSVCHandler( void ) __attribute__( ( naked ) );
135 
136 /*
137  * Start first task is a separate function so it can be tested in isolation.
138  */
139 static void prvPortStartFirstTask( void ) __attribute__( ( naked ) );
140 
141 /*
142  * Function to enable the VFP.
143  */
144 static void vPortEnableVFP( void ) __attribute__( ( naked ) );
145 
146 /*
147  * Used to catch tasks that attempt to return from their implementing function.
148  */
149 static void prvTaskExitError( void );
150 
151 /*-----------------------------------------------------------*/
152 
153 /* Each task maintains its own interrupt status in the critical nesting
154  * variable. */
155 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
156 
157 /*
158  * The number of SysTick increments that make up one tick period.
159  */
160 #if ( configUSE_TICKLESS_IDLE == 1 )
161     static uint32_t ulTimerCountsForOneTick = 0;
162 #endif /* configUSE_TICKLESS_IDLE */
163 
164 /*
165  * The maximum number of tick periods that can be suppressed is limited by the
166  * 24 bit resolution of the SysTick timer.
167  */
168 #if ( configUSE_TICKLESS_IDLE == 1 )
169     static uint32_t xMaximumPossibleSuppressedTicks = 0;
170 #endif /* configUSE_TICKLESS_IDLE */
171 
172 /*
173  * Compensate for the CPU cycles that pass while the SysTick is stopped (low
174  * power functionality only.
175  */
176 #if ( configUSE_TICKLESS_IDLE == 1 )
177     static uint32_t ulStoppedTimerCompensation = 0;
178 #endif /* configUSE_TICKLESS_IDLE */
179 
180 /*
181  * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
182  * FreeRTOS API functions are not called from interrupts that have been assigned
183  * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
184  */
185 #if ( configASSERT_DEFINED == 1 )
186     static uint8_t ucMaxSysCallPriority = 0;
187     static uint32_t ulMaxPRIGROUPValue = 0;
188     static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
189 #endif /* configASSERT_DEFINED */
190 
191 /*-----------------------------------------------------------*/
192 
193 /*
194  * See header file for description.
195  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)196 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
197                                      TaskFunction_t pxCode,
198                                      void * pvParameters )
199 {
200     /* Simulate the stack frame as it would be created by a context switch
201      * interrupt. */
202 
203     /* Offset added to account for the way the MCU uses the stack on entry/exit
204      * of interrupts, and to ensure alignment. */
205     pxTopOfStack--;
206 
207     *pxTopOfStack = portINITIAL_XPSR;                                    /* xPSR */
208     pxTopOfStack--;
209     *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
210     pxTopOfStack--;
211     *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;             /* LR */
212 
213     /* Save code space by skipping register initialisation. */
214     pxTopOfStack -= 5;                            /* R12, R3, R2 and R1. */
215     *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
216 
217     /* A save method is being used that requires each task to maintain its
218      * own exec return value. */
219     pxTopOfStack--;
220     *pxTopOfStack = portINITIAL_EXC_RETURN;
221 
222     pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
223 
224     return pxTopOfStack;
225 }
226 /*-----------------------------------------------------------*/
227 
prvTaskExitError(void)228 static void prvTaskExitError( void )
229 {
230     volatile uint32_t ulDummy = 0;
231 
232     /* A function that implements a task must not exit or attempt to return to
233      * its caller as there is nothing to return to.  If a task wants to exit it
234      * should instead call vTaskDelete( NULL ).
235      *
236      * Artificially force an assert() to be triggered if configASSERT() is
237      * defined, then stop here so application writers can catch the error. */
238     configASSERT( uxCriticalNesting == ~0UL );
239     portDISABLE_INTERRUPTS();
240 
241     while( ulDummy == 0 )
242     {
243         /* This file calls prvTaskExitError() after the scheduler has been
244          * started to remove a compiler warning about the function being defined
245          * but never called.  ulDummy is used purely to quieten other warnings
246          * about code appearing after this function is called - making ulDummy
247          * volatile makes the compiler think the function could return and
248          * therefore not output an 'unreachable code' warning for code that appears
249          * after it. */
250     }
251 }
252 /*-----------------------------------------------------------*/
253 
vPortSVCHandler(void)254 void vPortSVCHandler( void )
255 {
256     __asm volatile (
257         "   ldr r3, =pxCurrentTCB           \n" /* Restore the context. */
258         "   ldr r1, [r3]                    \n" /* Get the pxCurrentTCB address. */
259         "   ldr r0, [r1]                    \n" /* The first item in pxCurrentTCB is the task top of stack. */
260         "   ldmia r0!, {r4-r11, r14}        \n" /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
261         "   msr psp, r0                     \n" /* Restore the task stack pointer. */
262         "   isb                             \n"
263         "   mov r0, #0                      \n"
264         "   msr basepri, r0                 \n"
265         "   bx r14                          \n"
266         "                                   \n"
267         "   .ltorg                          \n"
268         );
269 }
270 /*-----------------------------------------------------------*/
271 
prvPortStartFirstTask(void)272 static void prvPortStartFirstTask( void )
273 {
274     /* Start the first task.  This also clears the bit that indicates the FPU is
275      * in use in case the FPU was used before the scheduler was started - which
276      * would otherwise result in the unnecessary leaving of space in the SVC stack
277      * for lazy saving of FPU registers. */
278     __asm volatile (
279         " ldr r0, =0xE000ED08   \n" /* Use the NVIC offset register to locate the stack. */
280         " ldr r0, [r0]          \n"
281         " ldr r0, [r0]          \n"
282         " msr msp, r0           \n" /* Set the msp back to the start of the stack. */
283         " mov r0, #0            \n" /* Clear the bit that indicates the FPU is in use, see comment above. */
284         " msr control, r0       \n"
285         " cpsie i               \n" /* Globally enable interrupts. */
286         " cpsie f               \n"
287         " dsb                   \n"
288         " isb                   \n"
289         " svc 0                 \n" /* System call to start first task. */
290         " nop                   \n"
291         " .ltorg                \n"
292         );
293 }
294 /*-----------------------------------------------------------*/
295 
296 /*
297  * See header file for description.
298  */
xPortStartScheduler(void)299 BaseType_t xPortStartScheduler( void )
300 {
301     /* An application can install FreeRTOS interrupt handlers in one of the
302      * following ways:
303      * 1. Direct Routing - Install the functions vPortSVCHandler and
304      *    xPortPendSVHandler for SVCall and PendSV interrupts respectively.
305      * 2. Indirect Routing - Install separate handlers for SVCall and PendSV
306      *    interrupts and route program control from those handlers to
307      *    vPortSVCHandler and xPortPendSVHandler functions.
308      *
309      * Applications that use Indirect Routing must set
310      * configCHECK_HANDLER_INSTALLATION to 0 in their FreeRTOSConfig.h. Direct
311      * routing, which is validated here when configCHECK_HANDLER_INSTALLATION
312      * is 1, should be preferred when possible. */
313     #if ( configCHECK_HANDLER_INSTALLATION == 1 )
314     {
315         const portISR_t * const pxVectorTable = portSCB_VTOR_REG;
316 
317         /* Validate that the application has correctly installed the FreeRTOS
318          * handlers for SVCall and PendSV interrupts. We do not check the
319          * installation of the SysTick handler because the application may
320          * choose to drive the RTOS tick using a timer other than the SysTick
321          * timer by overriding the weak function vPortSetupTimerInterrupt().
322          *
323          * Assertion failures here indicate incorrect installation of the
324          * FreeRTOS handlers. For help installing the FreeRTOS handlers, see
325          * https://www.freertos.org/Why-FreeRTOS/FAQs.
326          *
327          * Systems with a configurable address for the interrupt vector table
328          * can also encounter assertion failures or even system faults here if
329          * VTOR is not set correctly to point to the application's vector table. */
330         configASSERT( pxVectorTable[ portVECTOR_INDEX_SVC ] == vPortSVCHandler );
331         configASSERT( pxVectorTable[ portVECTOR_INDEX_PENDSV ] == xPortPendSVHandler );
332     }
333     #endif /* configCHECK_HANDLER_INSTALLATION */
334 
335     #if ( configASSERT_DEFINED == 1 )
336     {
337         volatile uint8_t ucOriginalPriority;
338         volatile uint32_t ulImplementedPrioBits = 0;
339         volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
340         volatile uint8_t ucMaxPriorityValue;
341 
342         /* Determine the maximum priority from which ISR safe FreeRTOS API
343          * functions can be called.  ISR safe functions are those that end in
344          * "FromISR".  FreeRTOS maintains separate thread and ISR API functions to
345          * ensure interrupt entry is as fast and simple as possible.
346          *
347          * Save the interrupt priority value that is about to be clobbered. */
348         ucOriginalPriority = *pucFirstUserPriorityRegister;
349 
350         /* Determine the number of priority bits available.  First write to all
351          * possible bits. */
352         *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
353 
354         /* Read the value back to see how many bits stuck. */
355         ucMaxPriorityValue = *pucFirstUserPriorityRegister;
356 
357         /* Use the same mask on the maximum system call priority. */
358         ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
359 
360         /* Check that the maximum system call priority is nonzero after
361          * accounting for the number of priority bits supported by the
362          * hardware. A priority of 0 is invalid because setting the BASEPRI
363          * register to 0 unmasks all interrupts, and interrupts with priority 0
364          * cannot be masked using BASEPRI.
365          * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
366         configASSERT( ucMaxSysCallPriority );
367 
368         /* Check that the bits not implemented in hardware are zero in
369          * configMAX_SYSCALL_INTERRUPT_PRIORITY. */
370         configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & ( ~ucMaxPriorityValue ) ) == 0U );
371 
372         /* Calculate the maximum acceptable priority group value for the number
373          * of bits read back. */
374 
375         while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
376         {
377             ulImplementedPrioBits++;
378             ucMaxPriorityValue <<= ( uint8_t ) 0x01;
379         }
380 
381         if( ulImplementedPrioBits == 8 )
382         {
383             /* When the hardware implements 8 priority bits, there is no way for
384              * the software to configure PRIGROUP to not have sub-priorities. As
385              * a result, the least significant bit is always used for sub-priority
386              * and there are 128 preemption priorities and 2 sub-priorities.
387              *
388              * This may cause some confusion in some cases - for example, if
389              * configMAX_SYSCALL_INTERRUPT_PRIORITY is set to 5, both 5 and 4
390              * priority interrupts will be masked in Critical Sections as those
391              * are at the same preemption priority. This may appear confusing as
392              * 4 is higher (numerically lower) priority than
393              * configMAX_SYSCALL_INTERRUPT_PRIORITY and therefore, should not
394              * have been masked. Instead, if we set configMAX_SYSCALL_INTERRUPT_PRIORITY
395              * to 4, this confusion does not happen and the behaviour remains the same.
396              *
397              * The following assert ensures that the sub-priority bit in the
398              * configMAX_SYSCALL_INTERRUPT_PRIORITY is clear to avoid the above mentioned
399              * confusion. */
400             configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY & 0x1U ) == 0U );
401             ulMaxPRIGROUPValue = 0;
402         }
403         else
404         {
405             ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS - ulImplementedPrioBits;
406         }
407 
408         /* Shift the priority group value back to its position within the AIRCR
409          * register. */
410         ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
411         ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
412 
413         /* Restore the clobbered interrupt priority register to its original
414          * value. */
415         *pucFirstUserPriorityRegister = ucOriginalPriority;
416     }
417     #endif /* configASSERT_DEFINED */
418 
419     /* Make PendSV and SysTick the lowest priority interrupts, and make SVCall
420      * the highest priority. */
421     portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
422     portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
423     portNVIC_SHPR2_REG = 0;
424 
425     /* Start the timer that generates the tick ISR.  Interrupts are disabled
426      * here already. */
427     vPortSetupTimerInterrupt();
428 
429     /* Initialise the critical nesting count ready for the first task. */
430     uxCriticalNesting = 0;
431 
432     /* Ensure the VFP is enabled - it should be anyway. */
433     vPortEnableVFP();
434 
435     /* Lazy save always. */
436     *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
437 
438     /* Start the first task. */
439     prvPortStartFirstTask();
440 
441     /* Should never get here as the tasks will now be executing!  Call the task
442      * exit error function to prevent compiler warnings about a static function
443      * not being called in the case that the application writer overrides this
444      * functionality by defining configTASK_RETURN_ADDRESS.  Call
445      * vTaskSwitchContext() so link time optimisation does not remove the
446      * symbol. */
447     vTaskSwitchContext();
448     prvTaskExitError();
449 
450     /* Should not get here! */
451     return 0;
452 }
453 /*-----------------------------------------------------------*/
454 
vPortEndScheduler(void)455 void vPortEndScheduler( void )
456 {
457     /* Not implemented in ports where there is nothing to return to.
458      * Artificially force an assert. */
459     configASSERT( uxCriticalNesting == 1000UL );
460 }
461 /*-----------------------------------------------------------*/
462 
vPortEnterCritical(void)463 void vPortEnterCritical( void )
464 {
465     portDISABLE_INTERRUPTS();
466     uxCriticalNesting++;
467 
468     /* This is not the interrupt safe version of the enter critical function so
469      * assert() if it is being called from an interrupt context.  Only API
470      * functions that end in "FromISR" can be used in an interrupt.  Only assert if
471      * the critical nesting count is 1 to protect against recursive calls if the
472      * assert function also uses a critical section. */
473     if( uxCriticalNesting == 1 )
474     {
475         configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
476     }
477 }
478 /*-----------------------------------------------------------*/
479 
vPortExitCritical(void)480 void vPortExitCritical( void )
481 {
482     configASSERT( uxCriticalNesting );
483     uxCriticalNesting--;
484 
485     if( uxCriticalNesting == 0 )
486     {
487         portENABLE_INTERRUPTS();
488     }
489 }
490 /*-----------------------------------------------------------*/
491 
xPortPendSVHandler(void)492 void xPortPendSVHandler( void )
493 {
494     /* This is a naked function. */
495 
496     __asm volatile
497     (
498         "   mrs r0, psp                         \n"
499         "   isb                                 \n"
500         "                                       \n"
501         "   ldr r3, =pxCurrentTCB               \n" /* Get the location of the current TCB. */
502         "   ldr r2, [r3]                        \n"
503         "                                       \n"
504         "   tst r14, #0x10                      \n" /* Is the task using the FPU context?  If so, push high vfp registers. */
505         "   it eq                               \n"
506         "   vstmdbeq r0!, {s16-s31}             \n"
507         "                                       \n"
508         "   stmdb r0!, {r4-r11, r14}            \n" /* Save the core registers. */
509         "   str r0, [r2]                        \n" /* Save the new top of stack into the first member of the TCB. */
510         "                                       \n"
511         "   stmdb sp!, {r0, r3}                 \n"
512         "   mov r0, %0                          \n"
513         "   cpsid i                             \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
514         "   msr basepri, r0                     \n"
515         "   dsb                                 \n"
516         "   isb                                 \n"
517         "   cpsie i                             \n" /* ARM Cortex-M7 r0p1 Errata 837070 workaround. */
518         "   bl vTaskSwitchContext               \n"
519         "   mov r0, #0                          \n"
520         "   msr basepri, r0                     \n"
521         "   ldmia sp!, {r0, r3}                 \n"
522         "                                       \n"
523         "   ldr r1, [r3]                        \n" /* The first item in pxCurrentTCB is the task top of stack. */
524         "   ldr r0, [r1]                        \n"
525         "                                       \n"
526         "   ldmia r0!, {r4-r11, r14}            \n" /* Pop the core registers. */
527         "                                       \n"
528         "   tst r14, #0x10                      \n" /* Is the task using the FPU context?  If so, pop the high vfp registers too. */
529         "   it eq                               \n"
530         "   vldmiaeq r0!, {s16-s31}             \n"
531         "                                       \n"
532         "   msr psp, r0                         \n"
533         "   isb                                 \n"
534         "                                       \n"
535         #ifdef WORKAROUND_PMU_CM001 /* XMC4000 specific errata workaround. */
536             #if WORKAROUND_PMU_CM001 == 1
537                 "           push { r14 }                \n"
538                 "           pop { pc }                  \n"
539             #endif
540         #endif
541         "                                       \n"
542         "   bx r14                              \n"
543         "                                       \n"
544         "   .ltorg                              \n"
545         ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
546     );
547 }
548 /*-----------------------------------------------------------*/
549 
xPortSysTickHandler(void)550 void xPortSysTickHandler( void )
551 {
552     /* The SysTick runs at the lowest interrupt priority, so when this interrupt
553      * executes all interrupts must be unmasked.  There is therefore no need to
554      * save and then restore the interrupt mask value as its value is already
555      * known. */
556     portDISABLE_INTERRUPTS();
557     traceISR_ENTER();
558     {
559         /* Increment the RTOS tick. */
560         if( xTaskIncrementTick() != pdFALSE )
561         {
562             traceISR_EXIT_TO_SCHEDULER();
563 
564             /* A context switch is required.  Context switching is performed in
565              * the PendSV interrupt.  Pend the PendSV interrupt. */
566             portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
567         }
568         else
569         {
570             traceISR_EXIT();
571         }
572     }
573     portENABLE_INTERRUPTS();
574 }
575 /*-----------------------------------------------------------*/
576 
577 #if ( configUSE_TICKLESS_IDLE == 1 )
578 
vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime)579     __attribute__( ( weak ) ) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
580     {
581         uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickDecrementsLeft;
582         TickType_t xModifiableIdleTime;
583 
584         /* Make sure the SysTick reload value does not overflow the counter. */
585         if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
586         {
587             xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
588         }
589 
590         /* Enter a critical section but don't use the taskENTER_CRITICAL()
591          * method as that will mask interrupts that should exit sleep mode. */
592         __asm volatile ( "cpsid i" ::: "memory" );
593         __asm volatile ( "dsb" );
594         __asm volatile ( "isb" );
595 
596         /* If a context switch is pending or a task is waiting for the scheduler
597          * to be unsuspended then abandon the low power entry. */
598         if( eTaskConfirmSleepModeStatus() == eAbortSleep )
599         {
600             /* Re-enable interrupts - see comments above the cpsid instruction
601              * above. */
602             __asm volatile ( "cpsie i" ::: "memory" );
603         }
604         else
605         {
606             /* Stop the SysTick momentarily.  The time the SysTick is stopped for
607              * is accounted for as best it can be, but using the tickless mode will
608              * inevitably result in some tiny drift of the time maintained by the
609              * kernel with respect to calendar time. */
610             portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT );
611 
612             /* Use the SysTick current-value register to determine the number of
613              * SysTick decrements remaining until the next tick interrupt.  If the
614              * current-value register is zero, then there are actually
615              * ulTimerCountsForOneTick decrements remaining, not zero, because the
616              * SysTick requests the interrupt when decrementing from 1 to 0. */
617             ulSysTickDecrementsLeft = portNVIC_SYSTICK_CURRENT_VALUE_REG;
618 
619             if( ulSysTickDecrementsLeft == 0 )
620             {
621                 ulSysTickDecrementsLeft = ulTimerCountsForOneTick;
622             }
623 
624             /* Calculate the reload value required to wait xExpectedIdleTime
625              * tick periods.  -1 is used because this code normally executes part
626              * way through the first tick period.  But if the SysTick IRQ is now
627              * pending, then clear the IRQ, suppressing the first tick, and correct
628              * the reload value to reflect that the second tick period is already
629              * underway.  The expected idle time is always at least two ticks. */
630             ulReloadValue = ulSysTickDecrementsLeft + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
631 
632             if( ( portNVIC_INT_CTRL_REG & portNVIC_PEND_SYSTICK_SET_BIT ) != 0 )
633             {
634                 portNVIC_INT_CTRL_REG = portNVIC_PEND_SYSTICK_CLEAR_BIT;
635                 ulReloadValue -= ulTimerCountsForOneTick;
636             }
637 
638             if( ulReloadValue > ulStoppedTimerCompensation )
639             {
640                 ulReloadValue -= ulStoppedTimerCompensation;
641             }
642 
643             /* Set the new reload value. */
644             portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
645 
646             /* Clear the SysTick count flag and set the count value back to
647              * zero. */
648             portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
649 
650             /* Restart SysTick. */
651             portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
652 
653             /* Sleep until something happens.  configPRE_SLEEP_PROCESSING() can
654              * set its parameter to 0 to indicate that its implementation contains
655              * its own wait for interrupt or wait for event instruction, and so wfi
656              * should not be executed again.  However, the original expected idle
657              * time variable must remain unmodified, so a copy is taken. */
658             xModifiableIdleTime = xExpectedIdleTime;
659             configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
660 
661             if( xModifiableIdleTime > 0 )
662             {
663                 __asm volatile ( "dsb" ::: "memory" );
664                 __asm volatile ( "wfi" );
665                 __asm volatile ( "isb" );
666             }
667 
668             configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
669 
670             /* Re-enable interrupts to allow the interrupt that brought the MCU
671              * out of sleep mode to execute immediately.  See comments above
672              * the cpsid instruction above. */
673             __asm volatile ( "cpsie i" ::: "memory" );
674             __asm volatile ( "dsb" );
675             __asm volatile ( "isb" );
676 
677             /* Disable interrupts again because the clock is about to be stopped
678              * and interrupts that execute while the clock is stopped will increase
679              * any slippage between the time maintained by the RTOS and calendar
680              * time. */
681             __asm volatile ( "cpsid i" ::: "memory" );
682             __asm volatile ( "dsb" );
683             __asm volatile ( "isb" );
684 
685             /* Disable the SysTick clock without reading the
686              * portNVIC_SYSTICK_CTRL_REG register to ensure the
687              * portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set.  Again,
688              * the time the SysTick is stopped for is accounted for as best it can
689              * be, but using the tickless mode will inevitably result in some tiny
690              * drift of the time maintained by the kernel with respect to calendar
691              * time*/
692             portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT );
693 
694             /* Determine whether the SysTick has already counted to zero. */
695             if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
696             {
697                 uint32_t ulCalculatedLoadValue;
698 
699                 /* The tick interrupt ended the sleep (or is now pending), and
700                  * a new tick period has started.  Reset portNVIC_SYSTICK_LOAD_REG
701                  * with whatever remains of the new tick period. */
702                 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
703 
704                 /* Don't allow a tiny value, or values that have somehow
705                  * underflowed because the post sleep hook did something
706                  * that took too long or because the SysTick current-value register
707                  * is zero. */
708                 if( ( ulCalculatedLoadValue <= ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
709                 {
710                     ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
711                 }
712 
713                 portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
714 
715                 /* As the pending tick will be processed as soon as this
716                  * function exits, the tick value maintained by the tick is stepped
717                  * forward by one less than the time spent waiting. */
718                 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
719             }
720             else
721             {
722                 /* Something other than the tick interrupt ended the sleep. */
723 
724                 /* Use the SysTick current-value register to determine the
725                  * number of SysTick decrements remaining until the expected idle
726                  * time would have ended. */
727                 ulSysTickDecrementsLeft = portNVIC_SYSTICK_CURRENT_VALUE_REG;
728                 #if ( portNVIC_SYSTICK_CLK_BIT_CONFIG != portNVIC_SYSTICK_CLK_BIT )
729                 {
730                     /* If the SysTick is not using the core clock, the current-
731                      * value register might still be zero here.  In that case, the
732                      * SysTick didn't load from the reload register, and there are
733                      * ulReloadValue decrements remaining in the expected idle
734                      * time, not zero. */
735                     if( ulSysTickDecrementsLeft == 0 )
736                     {
737                         ulSysTickDecrementsLeft = ulReloadValue;
738                     }
739                 }
740                 #endif /* portNVIC_SYSTICK_CLK_BIT_CONFIG */
741 
742                 /* Work out how long the sleep lasted rounded to complete tick
743                  * periods (not the ulReload value which accounted for part
744                  * ticks). */
745                 ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - ulSysTickDecrementsLeft;
746 
747                 /* How many complete tick periods passed while the processor
748                  * was waiting? */
749                 ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
750 
751                 /* The reload value is set to whatever fraction of a single tick
752                  * period remains. */
753                 portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
754             }
755 
756             /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG again,
757              * then set portNVIC_SYSTICK_LOAD_REG back to its standard value.  If
758              * the SysTick is not using the core clock, temporarily configure it to
759              * use the core clock.  This configuration forces the SysTick to load
760              * from portNVIC_SYSTICK_LOAD_REG immediately instead of at the next
761              * cycle of the other clock.  Then portNVIC_SYSTICK_LOAD_REG is ready
762              * to receive the standard value immediately. */
763             portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
764             portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
765             #if ( portNVIC_SYSTICK_CLK_BIT_CONFIG == portNVIC_SYSTICK_CLK_BIT )
766             {
767                 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
768             }
769             #else
770             {
771                 /* The temporary usage of the core clock has served its purpose,
772                  * as described above.  Resume usage of the other clock. */
773                 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT;
774 
775                 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
776                 {
777                     /* The partial tick period already ended.  Be sure the SysTick
778                      * counts it only once. */
779                     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0;
780                 }
781 
782                 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
783                 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;
784             }
785             #endif /* portNVIC_SYSTICK_CLK_BIT_CONFIG */
786 
787             /* Step the tick to account for any tick periods that elapsed. */
788             vTaskStepTick( ulCompleteTickPeriods );
789 
790             /* Exit with interrupts enabled. */
791             __asm volatile ( "cpsie i" ::: "memory" );
792         }
793     }
794 
795 #endif /* #if configUSE_TICKLESS_IDLE */
796 /*-----------------------------------------------------------*/
797 
798 /*
799  * Setup the systick timer to generate the tick interrupts at the required
800  * frequency.
801  */
vPortSetupTimerInterrupt(void)802 __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
803 {
804     /* Calculate the constants required to configure the tick interrupt. */
805     #if ( configUSE_TICKLESS_IDLE == 1 )
806     {
807         ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
808         xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
809         ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
810     }
811     #endif /* configUSE_TICKLESS_IDLE */
812 
813     /* Stop and clear the SysTick. */
814     portNVIC_SYSTICK_CTRL_REG = 0UL;
815     portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
816 
817     /* Configure SysTick to interrupt at the requested rate. */
818     portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
819     portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT_CONFIG | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
820 }
821 /*-----------------------------------------------------------*/
822 
823 /* This is a naked function. */
vPortEnableVFP(void)824 static void vPortEnableVFP( void )
825 {
826     __asm volatile
827     (
828         "   ldr.w r0, =0xE000ED88       \n" /* The FPU enable bits are in the CPACR. */
829         "   ldr r1, [r0]                \n"
830         "                               \n"
831         "   orr r1, r1, #( 0xf << 20 )  \n" /* Enable CP10 and CP11 coprocessors, then save back. */
832         "   str r1, [r0]                \n"
833         "   bx r14                      \n"
834         "   .ltorg                      \n"
835     );
836 }
837 /*-----------------------------------------------------------*/
838 
839 #if ( configASSERT_DEFINED == 1 )
840 
vPortValidateInterruptPriority(void)841     void vPortValidateInterruptPriority( void )
842     {
843         uint32_t ulCurrentInterrupt;
844         uint8_t ucCurrentPriority;
845 
846         /* Obtain the number of the currently executing interrupt. */
847         __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
848 
849         /* Is the interrupt number a user defined interrupt? */
850         if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
851         {
852             /* Look up the interrupt's priority. */
853             ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
854 
855             /* The following assertion will fail if a service routine (ISR) for
856              * an interrupt that has been assigned a priority above
857              * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
858              * function.  ISR safe FreeRTOS API functions must *only* be called
859              * from interrupts that have been assigned a priority at or below
860              * configMAX_SYSCALL_INTERRUPT_PRIORITY.
861              *
862              * Numerically low interrupt priority numbers represent logically high
863              * interrupt priorities, therefore the priority of the interrupt must
864              * be set to a value equal to or numerically *higher* than
865              * configMAX_SYSCALL_INTERRUPT_PRIORITY.
866              *
867              * Interrupts that  use the FreeRTOS API must not be left at their
868              * default priority of  zero as that is the highest possible priority,
869              * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
870              * and  therefore also guaranteed to be invalid.
871              *
872              * FreeRTOS maintains separate thread and ISR API functions to ensure
873              * interrupt entry is as fast and simple as possible.
874              *
875              * The following links provide detailed information:
876              * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
877              * https://www.freertos.org/Why-FreeRTOS/FAQs */
878             configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
879         }
880 
881         /* Priority grouping:  The interrupt controller (NVIC) allows the bits
882          * that define each interrupt's priority to be split between bits that
883          * define the interrupt's pre-emption priority bits and bits that define
884          * the interrupt's sub-priority.  For simplicity all bits must be defined
885          * to be pre-emption priority bits.  The following assertion will fail if
886          * this is not the case (if some bits represent a sub-priority).
887          *
888          * If the application only uses CMSIS libraries for interrupt
889          * configuration then the correct setting can be achieved on all Cortex-M
890          * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
891          * scheduler.  Note however that some vendor specific peripheral libraries
892          * assume a non-zero priority group setting, in which cases using a value
893          * of zero will result in unpredictable behaviour. */
894         configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
895     }
896 
897 #endif /* configASSERT_DEFINED */
898