2 * FreeRTOS Kernel V11.1.0
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 Philips ARM7 port.
31 *----------------------------------------------------------*/
36 + Bug fix - The prescale value for the timer setup is now written to T0PR
37 + instead of T0PC. This bug would have had no effect unless a prescale
38 + value was actually used.
41 /* Standard includes. */
43 #include <intrinsics.h>
45 /* Scheduler includes. */
49 /* Constants required to setup the tick ISR. */
50 #define portENABLE_TIMER ( ( uint8_t ) 0x01 )
51 #define portPRESCALE_VALUE 0x00
52 #define portINTERRUPT_ON_MATCH ( ( uint32_t ) 0x01 )
53 #define portRESET_COUNT_ON_MATCH ( ( uint32_t ) 0x02 )
55 /* Constants required to setup the initial stack. */
56 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
57 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
58 #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
60 /* Constants required to setup the PIT. */
61 #define portPIT_CLOCK_DIVISOR ( ( uint32_t ) 16 )
62 #define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_PERIOD_MS )
64 /* Constants required to handle interrupts. */
65 #define portTIMER_MATCH_ISR_BIT ( ( uint8_t ) 0x01 )
66 #define portCLEAR_VIC_INTERRUPT ( ( uint32_t ) 0 )
68 /* Constants required to handle critical sections. */
69 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
72 #define portINT_LEVEL_SENSITIVE 0
73 #define portPIT_ENABLE ( ( uint16_t ) 0x1 << 24 )
74 #define portPIT_INT_ENABLE ( ( uint16_t ) 0x1 << 25 )
76 /* Constants required to setup the VIC for the tick ISR. */
77 #define portTIMER_VIC_CHANNEL ( ( uint32_t ) 0x0004 )
78 #define portTIMER_VIC_CHANNEL_BIT ( ( uint32_t ) 0x0010 )
79 #define portTIMER_VIC_ENABLE ( ( uint32_t ) 0x0020 )
81 /*-----------------------------------------------------------*/
83 /* Setup the PIT to generate the tick interrupts. */
84 static void prvSetupTimerInterrupt( void );
86 /* ulCriticalNesting will get set to zero when the first task starts. It
87 * cannot be initialised to 0 as this will cause interrupts to be enabled
88 * during the kernel initialisation process. */
89 uint32_t ulCriticalNesting = ( uint32_t ) 9999;
91 /*-----------------------------------------------------------*/
94 * Initialise the stack of a task to look exactly as if a call to
95 * portSAVE_CONTEXT had been called.
97 * See header file for description.
99 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
100 TaskFunction_t pxCode,
101 void * pvParameters )
103 StackType_t * pxOriginalTOS;
105 pxOriginalTOS = pxTopOfStack;
107 /* Setup the initial stack of the task. The stack is set exactly as
108 * expected by the portRESTORE_CONTEXT() macro. */
110 /* First on the stack is the return address - which in this case is the
111 * start of the task. The offset is added to make the return address appear
112 * as it would within an IRQ ISR. */
113 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
116 *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
118 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
120 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
122 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
124 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
126 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
128 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
130 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
132 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
134 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
136 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
138 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
140 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
142 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
145 /* When the task starts is will expect to find the function parameter in
147 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
150 /* The status register is set for system mode, with interrupts enabled. */
151 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
153 if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
155 /* We want the task to start in thumb mode. */
156 *pxTopOfStack |= portTHUMB_MODE_BIT;
161 /* Interrupt flags cannot always be stored on the stack and will
162 * instead be stored in a variable, which is then saved as part of the
164 *pxTopOfStack = portNO_CRITICAL_NESTING;
168 /*-----------------------------------------------------------*/
170 BaseType_t xPortStartScheduler( void )
172 extern void vPortStartFirstTask( void );
174 /* Start the timer that generates the tick ISR. Interrupts are disabled
176 prvSetupTimerInterrupt();
178 /* Start the first task. */
179 vPortStartFirstTask();
181 /* Should not get here! */
184 /*-----------------------------------------------------------*/
186 void vPortEndScheduler( void )
188 /* It is unlikely that the ARM port will require this function as there
189 * is nothing to return to. */
191 /*-----------------------------------------------------------*/
193 #if configUSE_PREEMPTION == 0
195 /* The cooperative scheduler requires a normal IRQ service routine to
196 * simply increment the system tick. */
197 static __arm __irq void vPortNonPreemptiveTick( void );
198 static __arm __irq void vPortNonPreemptiveTick( void )
200 /* Increment the tick count - which may wake some tasks but as the
201 * preemptive scheduler is not being used any woken task is not given
202 * processor time no matter what its priority. */
203 xTaskIncrementTick();
205 /* Ready for the next interrupt. */
206 T0IR = portTIMER_MATCH_ISR_BIT;
207 VICVectAddr = portCLEAR_VIC_INTERRUPT;
210 #else /* if configUSE_PREEMPTION == 0 */
212 /* This function is called from an asm wrapper, so does not require the __irq
214 void vPortPreemptiveTick( void );
215 void vPortPreemptiveTick( void )
217 /* Increment the tick counter. */
218 if( xTaskIncrementTick() != pdFALSE )
220 /* The new tick value might unblock a task. Ensure the highest task that
221 * is ready to execute is the task that will execute when the tick ISR
223 vTaskSwitchContext();
226 /* Ready for the next interrupt. */
227 T0IR = portTIMER_MATCH_ISR_BIT;
228 VICVectAddr = portCLEAR_VIC_INTERRUPT;
231 #endif /* if configUSE_PREEMPTION == 0 */
233 /*-----------------------------------------------------------*/
235 static void prvSetupTimerInterrupt( void )
237 uint32_t ulCompareMatch;
239 /* A 1ms tick does not require the use of the timer prescale. This is
240 * defaulted to zero but can be used if necessary. */
241 T0PR = portPRESCALE_VALUE;
243 /* Calculate the match value required for our wanted tick rate. */
244 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
246 /* Protect against divide by zero. Using an if() statement still results
247 * in a warning - hence the #if. */
248 #if portPRESCALE_VALUE != 0
250 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
254 T0MR0 = ulCompareMatch;
256 /* Generate tick with timer 0 compare match. */
257 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
259 /* Setup the VIC for the timer. */
260 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
261 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
263 /* The ISR installed depends on whether the preemptive or cooperative
264 * scheduler is being used. */
265 #if configUSE_PREEMPTION == 1
267 extern void( vPortPreemptiveTickEntry )( void );
269 VICVectAddr0 = ( uint32_t ) vPortPreemptiveTickEntry;
273 extern void( vNonPreemptiveTick )( void );
275 VICVectAddr0 = ( int32_t ) vPortNonPreemptiveTick;
277 #endif /* if configUSE_PREEMPTION == 1 */
279 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
281 /* Start the timer - interrupts are disabled when this function is called
282 * so it is okay to do this here. */
283 T0TCR = portENABLE_TIMER;
285 /*-----------------------------------------------------------*/
287 void vPortEnterCritical( void )
289 /* Disable interrupts first! */
290 __disable_interrupt();
292 /* Now that interrupts are disabled, ulCriticalNesting can be accessed
293 * directly. Increment ulCriticalNesting to keep a count of how many times
294 * portENTER_CRITICAL() has been called. */
297 /*-----------------------------------------------------------*/
299 void vPortExitCritical( void )
301 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
303 /* Decrement the nesting count as we are leaving a critical section. */
306 /* If the nesting level has reached zero then interrupts should be
308 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
310 __enable_interrupt();
314 /*-----------------------------------------------------------*/