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 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, TaskFunction_t pxCode, void *pvParameters )
101 StackType_t *pxOriginalTOS;
103 pxOriginalTOS = pxTopOfStack;
105 /* Setup the initial stack of the task. The stack is set exactly as
106 expected by the portRESTORE_CONTEXT() macro. */
108 /* First on the stack is the return address - which in this case is the
109 start of the task. The offset is added to make the return address appear
110 as it would within an IRQ ISR. */
111 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
114 *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
116 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
118 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
120 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
122 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
124 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
126 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
128 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
130 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
132 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
134 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
136 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
138 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
140 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
143 /* When the task starts is will expect to find the function parameter in
145 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
148 /* The status register is set for system mode, with interrupts enabled. */
149 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
151 if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
153 /* We want the task to start in thumb mode. */
154 *pxTopOfStack |= portTHUMB_MODE_BIT;
159 /* Interrupt flags cannot always be stored on the stack and will
160 instead be stored in a variable, which is then saved as part of the
162 *pxTopOfStack = portNO_CRITICAL_NESTING;
166 /*-----------------------------------------------------------*/
168 BaseType_t xPortStartScheduler( void )
170 extern void vPortStartFirstTask( void );
172 /* Start the timer that generates the tick ISR. Interrupts are disabled
174 prvSetupTimerInterrupt();
176 /* Start the first task. */
177 vPortStartFirstTask();
179 /* Should not get here! */
182 /*-----------------------------------------------------------*/
184 void vPortEndScheduler( void )
186 /* It is unlikely that the ARM port will require this function as there
187 is nothing to return to. */
189 /*-----------------------------------------------------------*/
191 #if configUSE_PREEMPTION == 0
193 /* The cooperative scheduler requires a normal IRQ service routine to
194 simply increment the system tick. */
195 static __arm __irq void vPortNonPreemptiveTick( void );
196 static __arm __irq void vPortNonPreemptiveTick( void )
198 /* Increment the tick count - which may wake some tasks but as the
199 preemptive scheduler is not being used any woken task is not given
200 processor time no matter what its priority. */
201 xTaskIncrementTick();
203 /* Ready for the next interrupt. */
204 T0IR = portTIMER_MATCH_ISR_BIT;
205 VICVectAddr = portCLEAR_VIC_INTERRUPT;
210 /* This function is called from an asm wrapper, so does not require the __irq
212 void vPortPreemptiveTick( void );
213 void vPortPreemptiveTick( void )
215 /* Increment the tick counter. */
216 if( xTaskIncrementTick() != pdFALSE )
218 /* The new tick value might unblock a task. Ensure the highest task that
219 is ready to execute is the task that will execute when the tick ISR
221 vTaskSwitchContext();
224 /* Ready for the next interrupt. */
225 T0IR = portTIMER_MATCH_ISR_BIT;
226 VICVectAddr = portCLEAR_VIC_INTERRUPT;
231 /*-----------------------------------------------------------*/
233 static void prvSetupTimerInterrupt( void )
235 uint32_t ulCompareMatch;
237 /* A 1ms tick does not require the use of the timer prescale. This is
238 defaulted to zero but can be used if necessary. */
239 T0PR = portPRESCALE_VALUE;
241 /* Calculate the match value required for our wanted tick rate. */
242 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
244 /* Protect against divide by zero. Using an if() statement still results
245 in a warning - hence the #if. */
246 #if portPRESCALE_VALUE != 0
248 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
252 T0MR0 = ulCompareMatch;
254 /* Generate tick with timer 0 compare match. */
255 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
257 /* Setup the VIC for the timer. */
258 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
259 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
261 /* The ISR installed depends on whether the preemptive or cooperative
262 scheduler is being used. */
263 #if configUSE_PREEMPTION == 1
265 extern void ( vPortPreemptiveTickEntry )( void );
267 VICVectAddr0 = ( uint32_t ) vPortPreemptiveTickEntry;
271 extern void ( vNonPreemptiveTick )( void );
273 VICVectAddr0 = ( int32_t ) vPortNonPreemptiveTick;
277 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
279 /* Start the timer - interrupts are disabled when this function is called
280 so it is okay to do this here. */
281 T0TCR = portENABLE_TIMER;
283 /*-----------------------------------------------------------*/
285 void vPortEnterCritical( void )
287 /* Disable interrupts first! */
288 __disable_interrupt();
290 /* Now interrupts are disabled ulCriticalNesting can be accessed
291 directly. Increment ulCriticalNesting to keep a count of how many times
292 portENTER_CRITICAL() has been called. */
295 /*-----------------------------------------------------------*/
297 void vPortExitCritical( void )
299 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
301 /* Decrement the nesting count as we are leaving a critical section. */
304 /* If the nesting level has reached zero then interrupts should be
306 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
308 __enable_interrupt();
312 /*-----------------------------------------------------------*/