1 |
572 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/*-----------------------------------------------------------
|
56 |
|
|
* Implementation of functions defined in portable.h for the Atmel AT91R40008
|
57 |
|
|
* port.
|
58 |
|
|
*
|
59 |
|
|
* Components that can be compiled to either ARM or THUMB mode are
|
60 |
|
|
* contained in this file. The ISR routines, which can only be compiled
|
61 |
|
|
* to ARM mode are contained in portISR.c.
|
62 |
|
|
*----------------------------------------------------------*/
|
63 |
|
|
|
64 |
|
|
/* Standard includes. */
|
65 |
|
|
#include <stdlib.h>
|
66 |
|
|
|
67 |
|
|
/* Scheduler includes. */
|
68 |
|
|
#include "FreeRTOS.h"
|
69 |
|
|
#include "task.h"
|
70 |
|
|
|
71 |
|
|
/* Hardware specific definitions. */
|
72 |
|
|
#include "AT91R40008.h"
|
73 |
|
|
#include "pio.h"
|
74 |
|
|
#include "aic.h"
|
75 |
|
|
#include "tc.h"
|
76 |
|
|
|
77 |
|
|
/* Constants required to setup the task context. */
|
78 |
|
|
#define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
|
79 |
|
|
#define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
|
80 |
|
|
#define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
|
81 |
|
|
#define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )
|
82 |
|
|
#define portTICK_PRIORITY_6 ( 6 )
|
83 |
|
|
/*-----------------------------------------------------------*/
|
84 |
|
|
|
85 |
|
|
/* Setup the timer to generate the tick interrupts. */
|
86 |
|
|
static void prvSetupTimerInterrupt( void );
|
87 |
|
|
|
88 |
|
|
/*
|
89 |
|
|
* The scheduler can only be started from ARM mode, so
|
90 |
|
|
* vPortISRStartFirstSTask() is defined in portISR.c.
|
91 |
|
|
*/
|
92 |
|
|
extern void vPortISRStartFirstTask( void );
|
93 |
|
|
|
94 |
|
|
/*-----------------------------------------------------------*/
|
95 |
|
|
|
96 |
|
|
/*
|
97 |
|
|
* Initialise the stack of a task to look exactly as if a call to
|
98 |
|
|
* portSAVE_CONTEXT had been called.
|
99 |
|
|
*
|
100 |
|
|
* See header file for description.
|
101 |
|
|
*/
|
102 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
103 |
|
|
{
|
104 |
|
|
portSTACK_TYPE *pxOriginalTOS;
|
105 |
|
|
|
106 |
|
|
pxOriginalTOS = pxTopOfStack;
|
107 |
|
|
|
108 |
|
|
/* Setup the initial stack of the task. The stack is set exactly as
|
109 |
|
|
expected by the portRESTORE_CONTEXT() macro. */
|
110 |
|
|
|
111 |
|
|
/* First on the stack is the return address - which in this case is the
|
112 |
|
|
start of the task. The offset is added to make the return address appear
|
113 |
|
|
as it would within an IRQ ISR. */
|
114 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
|
115 |
|
|
pxTopOfStack--;
|
116 |
|
|
|
117 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
|
118 |
|
|
pxTopOfStack--;
|
119 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
|
120 |
|
|
pxTopOfStack--;
|
121 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
|
122 |
|
|
pxTopOfStack--;
|
123 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
|
124 |
|
|
pxTopOfStack--;
|
125 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
|
126 |
|
|
pxTopOfStack--;
|
127 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
|
128 |
|
|
pxTopOfStack--;
|
129 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
|
130 |
|
|
pxTopOfStack--;
|
131 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
|
132 |
|
|
pxTopOfStack--;
|
133 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
|
134 |
|
|
pxTopOfStack--;
|
135 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
|
136 |
|
|
pxTopOfStack--;
|
137 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
|
138 |
|
|
pxTopOfStack--;
|
139 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
|
140 |
|
|
pxTopOfStack--;
|
141 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
|
142 |
|
|
pxTopOfStack--;
|
143 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
|
144 |
|
|
pxTopOfStack--;
|
145 |
|
|
|
146 |
|
|
/* When the task starts is will expect to find the function parameter in
|
147 |
|
|
R0. */
|
148 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
|
149 |
|
|
pxTopOfStack--;
|
150 |
|
|
|
151 |
|
|
/* The last thing onto the stack is the status register, which is set for
|
152 |
|
|
system mode, with interrupts enabled. */
|
153 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
|
154 |
|
|
|
155 |
|
|
#ifdef THUMB_INTERWORK
|
156 |
|
|
{
|
157 |
|
|
/* We want the task to start in thumb mode. */
|
158 |
|
|
*pxTopOfStack |= portTHUMB_MODE_BIT;
|
159 |
|
|
}
|
160 |
|
|
#endif
|
161 |
|
|
|
162 |
|
|
pxTopOfStack--;
|
163 |
|
|
|
164 |
|
|
/* Some optimisation levels use the stack differently to others. This
|
165 |
|
|
means the interrupt flags cannot always be stored on the stack and will
|
166 |
|
|
instead be stored in a variable, which is then saved as part of the
|
167 |
|
|
tasks context. */
|
168 |
|
|
*pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
|
169 |
|
|
|
170 |
|
|
return pxTopOfStack;
|
171 |
|
|
}
|
172 |
|
|
/*-----------------------------------------------------------*/
|
173 |
|
|
|
174 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
175 |
|
|
{
|
176 |
|
|
/* Start the timer that generates the tick ISR. Interrupts are disabled
|
177 |
|
|
here already. */
|
178 |
|
|
prvSetupTimerInterrupt();
|
179 |
|
|
|
180 |
|
|
/* Start the first task. */
|
181 |
|
|
vPortISRStartFirstTask();
|
182 |
|
|
|
183 |
|
|
/* Should not get here! */
|
184 |
|
|
return 0;
|
185 |
|
|
}
|
186 |
|
|
/*-----------------------------------------------------------*/
|
187 |
|
|
|
188 |
|
|
void vPortEndScheduler( void )
|
189 |
|
|
{
|
190 |
|
|
/* It is unlikely that the ARM port will require this function as there
|
191 |
|
|
is nothing to return to. */
|
192 |
|
|
}
|
193 |
|
|
/*-----------------------------------------------------------*/
|
194 |
|
|
|
195 |
|
|
/*
|
196 |
|
|
* Setup the tick timer to generate the tick interrupts at the required frequency.
|
197 |
|
|
*/
|
198 |
|
|
static void prvSetupTimerInterrupt( void )
|
199 |
|
|
{
|
200 |
|
|
volatile unsigned long ulDummy;
|
201 |
|
|
|
202 |
|
|
/* Enable clock to the tick timer... */
|
203 |
|
|
AT91C_BASE_PS->PS_PCER = portTIMER_CLK_ENABLE_BIT;
|
204 |
|
|
|
205 |
|
|
/* Stop the tick timer... */
|
206 |
|
|
portTIMER_REG_BASE_PTR->TC_CCR = TC_CLKDIS;
|
207 |
|
|
|
208 |
|
|
/* Start with tick timer interrupts disabled... */
|
209 |
|
|
portTIMER_REG_BASE_PTR->TC_IDR = 0xFFFFFFFF;
|
210 |
|
|
|
211 |
|
|
/* Clear any pending tick timer interrupts... */
|
212 |
|
|
ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
|
213 |
|
|
|
214 |
|
|
/* Store interrupt handler function address in tick timer vector register...
|
215 |
|
|
The ISR installed depends on whether the preemptive or cooperative
|
216 |
|
|
scheduler is being used. */
|
217 |
|
|
#if configUSE_PREEMPTION == 1
|
218 |
|
|
{
|
219 |
|
|
extern void ( vPreemptiveTick )( void );
|
220 |
|
|
AT91C_BASE_AIC->AIC_SVR[portTIMER_AIC_CHANNEL] = ( unsigned long ) vPreemptiveTick;
|
221 |
|
|
}
|
222 |
|
|
#else // else use cooperative scheduler
|
223 |
|
|
{
|
224 |
|
|
extern void ( vNonPreemptiveTick )( void );
|
225 |
|
|
AT91C_BASE_AIC->AIC_SVR[portTIMER_AIC_CHANNEL] = ( unsigned long ) vNonPreemptiveTick;
|
226 |
|
|
}
|
227 |
|
|
#endif
|
228 |
|
|
|
229 |
|
|
/* Tick timer interrupt level-sensitive, priority 6... */
|
230 |
|
|
AT91C_BASE_AIC->AIC_SMR[ portTIMER_AIC_CHANNEL ] = AIC_SRCTYPE_INT_LEVEL_SENSITIVE | portTICK_PRIORITY_6;
|
231 |
|
|
|
232 |
|
|
/* Enable the tick timer interrupt...
|
233 |
|
|
|
234 |
|
|
First at timer level */
|
235 |
|
|
portTIMER_REG_BASE_PTR->TC_IER = TC_CPCS;
|
236 |
|
|
|
237 |
|
|
/* Then at the AIC level. */
|
238 |
|
|
AT91C_BASE_AIC->AIC_IECR = (1 << portTIMER_AIC_CHANNEL);
|
239 |
|
|
|
240 |
|
|
/* Calculate timer compare value to achieve the desired tick rate... */
|
241 |
|
|
if( (configCPU_CLOCK_HZ / (configTICK_RATE_HZ * 2) ) <= 0xFFFF )
|
242 |
|
|
{
|
243 |
|
|
/* The tick rate is fast enough for us to use the faster timer input
|
244 |
|
|
clock (main clock / 2). */
|
245 |
|
|
portTIMER_REG_BASE_PTR->TC_CMR = TC_WAVE | TC_CLKS_MCK2 | TC_BURST_NONE | TC_CPCTRG;
|
246 |
|
|
portTIMER_REG_BASE_PTR->TC_RC = configCPU_CLOCK_HZ / (configTICK_RATE_HZ * 2);
|
247 |
|
|
}
|
248 |
|
|
else
|
249 |
|
|
{
|
250 |
|
|
/* We must use a slower timer input clock (main clock / 8) because the
|
251 |
|
|
tick rate is too slow for the faster input clock. */
|
252 |
|
|
portTIMER_REG_BASE_PTR->TC_CMR = TC_WAVE | TC_CLKS_MCK8 | TC_BURST_NONE | TC_CPCTRG;
|
253 |
|
|
portTIMER_REG_BASE_PTR->TC_RC = configCPU_CLOCK_HZ / (configTICK_RATE_HZ * 8);
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
/* Start tick timer... */
|
257 |
|
|
portTIMER_REG_BASE_PTR->TC_CCR = TC_SWTRG | TC_CLKEN;
|
258 |
|
|
}
|
259 |
|
|
/*-----------------------------------------------------------*/
|
260 |
|
|
|