2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ARM CM4F port.
31 *----------------------------------------------------------*/
33 /* Scheduler includes. */
37 /* Constants required to manipulate the NVIC. */
38 #define portNVIC_SYSTICK_CTRL ( ( volatile uint32_t * ) 0xe000e010 )
39 #define portNVIC_SYSTICK_LOAD ( ( volatile uint32_t * ) 0xe000e014 )
40 #define portNVIC_SHPR3_REG ( ( volatile uint32_t * ) 0xe000ed20 )
41 #define portNVIC_SYSTICK_CLK 0x00000004
42 #define portNVIC_SYSTICK_INT 0x00000002
43 #define portNVIC_SYSTICK_ENABLE 0x00000001
44 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
45 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 16UL )
46 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) portMIN_INTERRUPT_PRIORITY ) << 24UL )
48 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
49 #define portVECTACTIVE_MASK ( 0xFFUL )
51 /* Constants required to manipulate the VFP. */
52 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
53 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
55 /* Constants required to set up the initial stack. */
56 #define portINITIAL_XPSR ( 0x01000000 )
57 #define portINITIAL_EXC_RETURN ( 0xfffffffd )
59 /* Let the user override the pre-loading of the initial LR with the address of
60 * prvTaskExitError() in case it messes up unwinding of the stack in the
62 #ifdef configTASK_RETURN_ADDRESS
63 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
65 #define portTASK_RETURN_ADDRESS prvTaskExitError
68 /* For strict compliance with the Cortex-M spec the task start address should
69 * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
70 #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
72 /* The priority used by the kernel is assigned to a variable to make access
73 * from inline assembler easier. */
74 const uint32_t ulKernelPriority = portMIN_INTERRUPT_PRIORITY;
76 /* Each task maintains its own interrupt status in the critical nesting
78 static uint32_t ulCriticalNesting = 0xaaaaaaaaUL;
81 * Setup the timer to generate the tick interrupts.
83 static void prvSetupTimerInterrupt( void );
88 void SysTick_Handler( void );
91 * Functions defined in port_asm.asm.
93 extern void vPortEnableVFP( void );
94 extern void vPortStartFirstTask( void );
97 * Used to catch tasks that attempt to return from their implementing function.
99 static void prvTaskExitError( void );
101 /* This exists purely to allow the const to be used from within the
102 * port_asm.asm assembly file. */
103 const uint32_t ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;
105 /*-----------------------------------------------------------*/
108 * See header file for description.
110 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
111 TaskFunction_t pxCode,
112 void * pvParameters )
114 /* Simulate the stack frame as it would be created by a context switch
117 /* Offset added to account for the way the MCU uses the stack on entry/exit
118 * of interrupts, and to ensure alignment. */
121 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
123 *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
125 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */
127 /* Save code space by skipping register initialisation. */
128 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
129 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
131 /* A save method is being used that requires each task to maintain its
132 * own exec return value. */
134 *pxTopOfStack = portINITIAL_EXC_RETURN;
136 pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
140 /*-----------------------------------------------------------*/
142 static void prvTaskExitError( void )
144 /* A function that implements a task must not exit or attempt to return to
145 * its caller as there is nothing to return to. If a task wants to exit it
146 * should instead call vTaskDelete( NULL ).
148 * Artificially force an assert() to be triggered if configASSERT() is
149 * defined, then stop here so application writers can catch the error. */
150 configASSERT( ulCriticalNesting == ~0UL );
151 portDISABLE_INTERRUPTS();
157 /*-----------------------------------------------------------*/
160 * See header file for description.
162 BaseType_t xPortStartScheduler( void )
164 /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
165 * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
166 configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );
168 /* Make PendSV and SysTick the lowest priority interrupts. */
169 *( portNVIC_SHPR3_REG ) |= portNVIC_PENDSV_PRI;
170 *( portNVIC_SHPR3_REG ) |= portNVIC_SYSTICK_PRI;
172 /* Start the timer that generates the tick ISR. Interrupts are disabled
174 prvSetupTimerInterrupt();
176 /* Initialise the critical nesting count ready for the first task. */
177 ulCriticalNesting = 0;
179 /* Ensure the VFP is enabled - it should be anyway. */
182 /* Lazy save always. */
183 *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
185 /* Start the first task. */
186 vPortStartFirstTask();
188 /* Should not get here! */
191 /*-----------------------------------------------------------*/
193 void vPortEndScheduler( void )
195 /* Not implemented in ports where there is nothing to return to.
196 * Artificially force an assert. */
197 configASSERT( ulCriticalNesting == 1000UL );
199 /*-----------------------------------------------------------*/
201 void vPortYield( void )
203 /* Set a PendSV to request a context switch. */
204 *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
206 /* Barriers are normally not required but do ensure the code is completely
207 * within the specified behaviour for the architecture. */
211 /*-----------------------------------------------------------*/
213 void vPortEnterCritical( void )
215 portDISABLE_INTERRUPTS();
220 /* This is not the interrupt safe version of the enter critical function so
221 * assert() if it is being called from an interrupt context. Only API
222 * functions that end in "FromISR" can be used in an interrupt. Only assert if
223 * the critical nesting count is 1 to protect against recursive calls if the
224 * assert function also uses a critical section. */
225 if( ulCriticalNesting == 1 )
227 configASSERT( ( ( *( portNVIC_INT_CTRL ) ) & portVECTACTIVE_MASK ) == 0 );
230 /*-----------------------------------------------------------*/
232 void vPortExitCritical( void )
234 configASSERT( ulCriticalNesting );
237 if( ulCriticalNesting == 0 )
239 portENABLE_INTERRUPTS();
242 /*-----------------------------------------------------------*/
244 void SysTick_Handler( void )
248 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
251 if( xTaskIncrementTick() != pdFALSE )
253 traceISR_EXIT_TO_SCHEDULER();
254 /* Pend a context switch. */
255 *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
262 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
264 /*-----------------------------------------------------------*/
267 * Setup the systick timer to generate the tick interrupts at the required
270 void prvSetupTimerInterrupt( void )
272 /* Configure SysTick to interrupt at the requested rate. */
273 *( portNVIC_SYSTICK_LOAD ) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
274 *( portNVIC_SYSTICK_CTRL ) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;