2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 /*-----------------------------------------------------------
\r
31 * Implementation of functions defined in portable.h for the ARM7 port.
\r
33 * Components that can be compiled to either ARM or THUMB mode are
\r
34 * contained in this file. The ISR routines, which can only be compiled
\r
35 * to ARM mode are contained in portISR.c.
\r
36 *----------------------------------------------------------*/
\r
39 /* Standard includes. */
\r
42 /* Scheduler includes. */
\r
43 #include "FreeRTOS.h"
\r
46 /* Constants required to setup the task context. */
\r
47 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
48 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
\r
49 #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
\r
50 #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )
\r
52 /* Constants required to setup the tick ISR. */
\r
53 #define portENABLE_TIMER ( ( uint8_t ) 0x01 )
\r
54 #define portPRESCALE_VALUE 0x00
\r
55 #define portINTERRUPT_ON_MATCH ( ( uint32_t ) 0x01 )
\r
56 #define portRESET_COUNT_ON_MATCH ( ( uint32_t ) 0x02 )
\r
58 /* Constants required to setup the VIC for the tick ISR. */
\r
59 #define portTIMER_VIC_CHANNEL ( ( uint32_t ) 0x0004 )
\r
60 #define portTIMER_VIC_CHANNEL_BIT ( ( uint32_t ) 0x0010 )
\r
61 #define portTIMER_VIC_ENABLE ( ( uint32_t ) 0x0020 )
\r
63 /*-----------------------------------------------------------*/
\r
65 /* Setup the timer to generate the tick interrupts. */
\r
66 static void prvSetupTimerInterrupt( void );
\r
69 * The scheduler can only be started from ARM mode, so
\r
70 * vPortISRStartFirstSTask() is defined in portISR.c.
\r
72 extern void vPortISRStartFirstTask( void );
\r
74 /*-----------------------------------------------------------*/
\r
77 * Initialise the stack of a task to look exactly as if a call to
\r
78 * portSAVE_CONTEXT had been called.
\r
80 * See header file for description.
\r
82 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
84 StackType_t *pxOriginalTOS;
\r
86 pxOriginalTOS = pxTopOfStack;
\r
88 /* To ensure asserts in tasks.c don't fail, although in this case the assert
\r
89 is not really required. */
\r
92 /* Setup the initial stack of the task. The stack is set exactly as
\r
93 expected by the portRESTORE_CONTEXT() macro. */
\r
95 /* First on the stack is the return address - which in this case is the
\r
96 start of the task. The offset is added to make the return address appear
\r
97 as it would within an IRQ ISR. */
\r
98 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
\r
101 *pxTopOfStack = ( StackType_t ) 0x00000000; /* R14 */
\r
103 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
105 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
\r
107 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
\r
109 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
\r
111 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
\r
113 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
\r
115 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
\r
117 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
\r
119 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
\r
121 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
\r
123 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
\r
125 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
\r
127 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
\r
130 /* When the task starts is will expect to find the function parameter in
\r
132 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
135 /* The last thing onto the stack is the status register, which is set for
\r
136 system mode, with interrupts enabled. */
\r
137 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
\r
139 if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00 )
\r
141 /* We want the task to start in thumb mode. */
\r
142 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
147 /* Some optimisation levels use the stack differently to others. This
\r
148 means the interrupt flags cannot always be stored on the stack and will
\r
149 instead be stored in a variable, which is then saved as part of the
\r
151 *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
\r
153 return pxTopOfStack;
\r
155 /*-----------------------------------------------------------*/
\r
157 BaseType_t xPortStartScheduler( void )
\r
159 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
161 prvSetupTimerInterrupt();
\r
163 /* Start the first task. */
\r
164 vPortISRStartFirstTask();
\r
166 /* Should not get here! */
\r
169 /*-----------------------------------------------------------*/
\r
171 void vPortEndScheduler( void )
\r
173 /* It is unlikely that the ARM port will require this function as there
\r
174 is nothing to return to. */
\r
176 /*-----------------------------------------------------------*/
\r
179 * Setup the timer 0 to generate the tick interrupts at the required frequency.
\r
181 static void prvSetupTimerInterrupt( void )
\r
183 uint32_t ulCompareMatch;
\r
185 PCLKSEL0 = (PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
\r
186 T0TCR = 2; /* Stop and reset the timer */
\r
187 T0CTCR = 0; /* Timer mode */
\r
189 /* A 1ms tick does not require the use of the timer prescale. This is
\r
190 defaulted to zero but can be used if necessary. */
\r
191 T0PR = portPRESCALE_VALUE;
\r
193 /* Calculate the match value required for our wanted tick rate. */
\r
194 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
196 /* Protect against divide by zero. Using an if() statement still results
\r
197 in a warning - hence the #if. */
\r
198 #if portPRESCALE_VALUE != 0
\r
200 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
\r
203 T0MR1 = ulCompareMatch;
\r
205 /* Generate tick with timer 0 compare match. */
\r
206 T0MCR = (3 << 3); /* Reset timer on match and generate interrupt */
\r
208 /* Setup the VIC for the timer. */
\r
209 VICIntEnable = 0x00000010;
\r
211 /* The ISR installed depends on whether the preemptive or cooperative
\r
212 scheduler is being used. */
\r
213 #if configUSE_PREEMPTION == 1
\r
215 extern void ( vPreemptiveTick )( void );
\r
216 VICVectAddr4 = ( int32_t ) vPreemptiveTick;
\r
220 extern void ( vNonPreemptiveTick )( void );
\r
221 VICVectAddr4 = ( int32_t ) vNonPreemptiveTick;
\r
227 /* Start the timer - interrupts are disabled when this function is called
\r
228 so it is okay to do this here. */
\r
229 T0TCR = portENABLE_TIMER;
\r
231 /*-----------------------------------------------------------*/
\r