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
30 /* Standard includes. */
33 /* Scheduler includes. */
37 /* Constants required to setup the initial task context. */
38 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
39 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
40 #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
41 #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )
43 /* Constants required to setup the tick ISR. */
44 #define portENABLE_TIMER ( ( uint8_t ) 0x01 )
45 #define portPRESCALE_VALUE 0x00
46 #define portINTERRUPT_ON_MATCH ( ( uint32_t ) 0x01 )
47 #define portRESET_COUNT_ON_MATCH ( ( uint32_t ) 0x02 )
49 /* Constants required to setup the VIC for the tick ISR. */
50 #define portTIMER_VIC_CHANNEL ( ( uint32_t ) 0x0004 )
51 #define portTIMER_VIC_CHANNEL_BIT ( ( uint32_t ) 0x0010 )
52 #define portTIMER_VIC_ENABLE ( ( uint32_t ) 0x0020 )
54 /* Constants required to handle interrupts. */
55 #define portTIMER_MATCH_ISR_BIT ( ( uint8_t ) 0x01 )
56 #define portCLEAR_VIC_INTERRUPT ( ( uint32_t ) 0 )
58 /*-----------------------------------------------------------*/
60 /* The code generated by the Keil compiler does not maintain separate
61 * stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
62 * use the stack as per other ports. Instead a variable is used to keep
63 * track of the critical section nesting. This variable has to be stored
64 * as part of the task context and must be initialised to a non zero value. */
66 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
67 volatile uint32_t ulCriticalNesting = 9999UL;
69 /*-----------------------------------------------------------*/
71 /* Setup the timer to generate the tick interrupts. */
72 static void prvSetupTimerInterrupt( void );
75 * The scheduler can only be started from ARM mode, so
76 * vPortStartFirstSTask() is defined in portISR.c.
78 extern __asm void vPortStartFirstTask( void );
80 /*-----------------------------------------------------------*/
83 * See header file for description.
85 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
86 TaskFunction_t pxCode,
89 StackType_t * pxOriginalTOS;
91 /* Setup the initial stack of the task. The stack is set exactly as
92 * expected by the portRESTORE_CONTEXT() macro.
94 * Remember where the top of the (simulated) stack is before we place
96 pxOriginalTOS = pxTopOfStack;
98 /* To ensure asserts in tasks.c don't fail, although in this case the assert
99 * is not really required. */
102 /* First on the stack is the return address - which in this case is the
103 * start of the task. The offset is added to make the return address appear
104 * as it would within an IRQ ISR. */
105 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
108 *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
110 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
112 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
114 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
116 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
118 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
120 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
122 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
124 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
126 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
128 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
130 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
132 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
134 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
136 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
139 /* The last thing onto the stack is the status register, which is set for
140 * system mode, with interrupts enabled. */
141 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
143 if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
145 /* We want the task to start in thumb mode. */
146 *pxTopOfStack |= portTHUMB_MODE_BIT;
151 /* The code generated by the Keil compiler does not maintain separate
152 * stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
153 * use the stack as per other ports. Instead a variable is used to keep
154 * track of the critical section nesting. This variable has to be stored
155 * as part of the task context and is initially set to zero. */
156 *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
160 /*-----------------------------------------------------------*/
162 BaseType_t xPortStartScheduler( void )
164 /* Start the timer that generates the tick ISR. */
165 prvSetupTimerInterrupt();
167 /* Start the first task. This is done from portISR.c as ARM mode must be
169 vPortStartFirstTask();
171 /* Should not get here! */
174 /*-----------------------------------------------------------*/
176 void vPortEndScheduler( void )
178 /* It is unlikely that the ARM port will require this function as there
179 * is nothing to return to. If this is required - stop the tick ISR then
180 * return back to main. */
182 /*-----------------------------------------------------------*/
184 #if configUSE_PREEMPTION == 0
187 * The cooperative scheduler requires a normal IRQ service routine to
188 * simply increment the system tick.
190 void vNonPreemptiveTick( void ) __irq;
191 void vNonPreemptiveTick( void ) __irq
193 /* Increment the tick count - this may make a delaying task ready
194 * to run - but a context switch is not performed. */
195 xTaskIncrementTick();
197 T0IR = portTIMER_MATCH_ISR_BIT; /* Clear the timer event */
198 VICVectAddr = portCLEAR_VIC_INTERRUPT; /* Acknowledge the Interrupt */
201 #else /* if configUSE_PREEMPTION == 0 */
204 **************************************************************************
205 * The preemptive scheduler ISR is written in assembler and can be found
206 * in the portASM.s file. This will only get used if portUSE_PREEMPTION
207 * is set to 1 in portmacro.h
208 **************************************************************************
211 void vPreemptiveTick( void );
213 #endif /* if configUSE_PREEMPTION == 0 */
214 /*-----------------------------------------------------------*/
216 static void prvSetupTimerInterrupt( void )
218 uint32_t ulCompareMatch;
220 /* A 1ms tick does not require the use of the timer prescale. This is
221 * defaulted to zero but can be used if necessary. */
222 T0PR = portPRESCALE_VALUE;
224 /* Calculate the match value required for our wanted tick rate. */
225 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
227 /* Protect against divide by zero. Using an if() statement still results
228 * in a warning - hence the #if. */
229 #if portPRESCALE_VALUE != 0
231 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
235 T0MR0 = ulCompareMatch;
237 /* Generate tick with timer 0 compare match. */
238 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
240 /* Setup the VIC for the timer. */
241 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
242 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
244 /* The ISR installed depends on whether the preemptive or cooperative
245 * scheduler is being used. */
246 #if configUSE_PREEMPTION == 1
248 VICVectAddr0 = ( uint32_t ) vPreemptiveTick;
252 VICVectAddr0 = ( uint32_t ) vNonPreemptiveTick;
256 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
258 /* Start the timer - interrupts are disabled when this function is called
259 * so it is okay to do this here. */
260 T0TCR = portENABLE_TIMER;
262 /*-----------------------------------------------------------*/
264 void vPortEnterCritical( void )
266 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
269 /* Now that interrupts are disabled, ulCriticalNesting can be accessed
270 * directly. Increment ulCriticalNesting to keep a count of how many times
271 * portENTER_CRITICAL() has been called. */
274 /*-----------------------------------------------------------*/
276 void vPortExitCritical( void )
278 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
280 /* Decrement the nesting count as we are leaving a critical section. */
283 /* If the nesting level has reached zero then interrupts should be
285 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
287 /* Enable interrupts as per portEXIT_CRITICAL(). */
292 /*-----------------------------------------------------------*/