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 Atmel ARM7 port.
31 *----------------------------------------------------------*/
34 /* Standard includes. */
37 /* Scheduler includes. */
41 /* Constants required to setup the initial stack. */
42 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
43 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
44 #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
46 /* Constants required to setup the PIT. */
47 #define portPIT_CLOCK_DIVISOR ( ( uint32_t ) 16 )
48 #define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_PERIOD_MS )
50 /* Constants required to handle critical sections. */
51 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
54 #define portINT_LEVEL_SENSITIVE 0
55 #define portPIT_ENABLE ( ( uint16_t ) 0x1 << 24 )
56 #define portPIT_INT_ENABLE ( ( uint16_t ) 0x1 << 25 )
57 /*-----------------------------------------------------------*/
59 /* Setup the PIT to generate the tick interrupts. */
60 static void prvSetupTimerInterrupt( void );
62 /* ulCriticalNesting will get set to zero when the first task starts. It
63 cannot be initialised to 0 as this will cause interrupts to be enabled
64 during the kernel initialisation process. */
65 uint32_t ulCriticalNesting = ( uint32_t ) 9999;
67 /*-----------------------------------------------------------*/
70 * Initialise the stack of a task to look exactly as if a call to
71 * portSAVE_CONTEXT had been called.
73 * See header file for description.
75 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
77 StackType_t *pxOriginalTOS;
79 pxOriginalTOS = pxTopOfStack;
81 /* To ensure asserts in tasks.c don't fail, although in this case the assert
82 is not really required. */
85 /* Setup the initial stack of the task. The stack is set exactly as
86 expected by the portRESTORE_CONTEXT() macro. */
88 /* First on the stack is the return address - which in this case is the
89 start of the task. The offset is added to make the return address appear
90 as it would within an IRQ ISR. */
91 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
94 *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
96 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
98 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
100 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
102 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
104 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
106 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
108 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
110 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
112 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
114 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
116 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
118 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
120 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
123 /* When the task starts is will expect to find the function parameter in
125 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
128 /* The status register is set for system mode, with interrupts enabled. */
129 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
131 if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
133 /* We want the task to start in thumb mode. */
134 *pxTopOfStack |= portTHUMB_MODE_BIT;
139 /* Interrupt flags cannot always be stored on the stack and will
140 instead be stored in a variable, which is then saved as part of the
142 *pxTopOfStack = portNO_CRITICAL_NESTING;
146 /*-----------------------------------------------------------*/
148 BaseType_t xPortStartScheduler( void )
150 extern void vPortStartFirstTask( void );
152 /* Start the timer that generates the tick ISR. Interrupts are disabled
154 prvSetupTimerInterrupt();
156 /* Start the first task. */
157 vPortStartFirstTask();
159 /* Should not get here! */
162 /*-----------------------------------------------------------*/
164 void vPortEndScheduler( void )
166 /* It is unlikely that the ARM port will require this function as there
167 is nothing to return to. */
169 /*-----------------------------------------------------------*/
171 #if configUSE_PREEMPTION == 0
173 /* The cooperative scheduler requires a normal IRQ service routine to
174 simply increment the system tick. */
175 static __arm __irq void vPortNonPreemptiveTick( void );
176 static __arm __irq void vPortNonPreemptiveTick( void )
180 /* Increment the tick count - which may wake some tasks but as the
181 preemptive scheduler is not being used any woken task is not given
182 processor time no matter what its priority. */
183 xTaskIncrementTick();
185 /* Clear the PIT interrupt. */
186 ulDummy = AT91C_BASE_PITC->PITC_PIVR;
188 /* End the interrupt in the AIC. */
189 AT91C_BASE_AIC->AIC_EOICR = ulDummy;
194 /* Currently the IAR port requires the preemptive tick function to be
195 defined in an asm file. */
199 /*-----------------------------------------------------------*/
201 static void prvSetupTimerInterrupt( void )
203 AT91PS_PITC pxPIT = AT91C_BASE_PITC;
205 /* Setup the AIC for PIT interrupts. The interrupt routine chosen depends
206 on whether the preemptive or cooperative scheduler is being used. */
207 #if configUSE_PREEMPTION == 0
209 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_SYS, AT91C_AIC_PRIOR_HIGHEST, portINT_LEVEL_SENSITIVE, ( void (*)(void) ) vPortNonPreemptiveTick );
213 extern void ( vPortPreemptiveTick )( void );
214 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_SYS, AT91C_AIC_PRIOR_HIGHEST, portINT_LEVEL_SENSITIVE, ( void (*)(void) ) vPortPreemptiveTick );
218 /* Configure the PIT period. */
219 pxPIT->PITC_PIMR = portPIT_ENABLE | portPIT_INT_ENABLE | portPIT_COUNTER_VALUE;
221 /* Enable the interrupt. Global interrupts are disables at this point so
223 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_SYS );
225 /*-----------------------------------------------------------*/
227 void vPortEnterCritical( void )
229 /* Disable interrupts first! */
230 __disable_interrupt();
232 /* Now interrupts are disabled ulCriticalNesting can be accessed
233 directly. Increment ulCriticalNesting to keep a count of how many times
234 portENTER_CRITICAL() has been called. */
237 /*-----------------------------------------------------------*/
239 void vPortExitCritical( void )
241 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
243 /* Decrement the nesting count as we are leaving a critical section. */
246 /* If the nesting level has reached zero then interrupts should be
248 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
250 __enable_interrupt();
254 /*-----------------------------------------------------------*/