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 |
|
|
* Implementation of functions defined in portable.h for the Atmel ARM7 port.
|
56 |
|
|
*----------------------------------------------------------*/
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* Standard includes. */
|
60 |
|
|
#include <stdlib.h>
|
61 |
|
|
|
62 |
|
|
/* Scheduler includes. */
|
63 |
|
|
#include "FreeRTOS.h"
|
64 |
|
|
#include "task.h"
|
65 |
|
|
|
66 |
|
|
/* Hardware includes. */
|
67 |
|
|
#include <board.h>
|
68 |
|
|
#include <pio/pio.h>
|
69 |
|
|
#include <pio/pio_it.h>
|
70 |
|
|
#include <pit/pit.h>
|
71 |
|
|
#include <aic/aic.h>
|
72 |
|
|
#include <tc/tc.h>
|
73 |
|
|
#include <utility/led.h>
|
74 |
|
|
#include <utility/trace.h>
|
75 |
|
|
|
76 |
|
|
/*-----------------------------------------------------------*/
|
77 |
|
|
|
78 |
|
|
/* Constants required to setup the initial stack. */
|
79 |
|
|
#define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
|
80 |
|
|
#define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
|
81 |
|
|
#define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
|
82 |
|
|
|
83 |
|
|
/* Constants required to setup the PIT. */
|
84 |
|
|
#define port1MHz_IN_Hz ( 1000000ul )
|
85 |
|
|
#define port1SECOND_IN_uS ( 1000000.0 )
|
86 |
|
|
|
87 |
|
|
/* Constants required to handle critical sections. */
|
88 |
|
|
#define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
#define portINT_LEVEL_SENSITIVE 0
|
92 |
|
|
#define portPIT_ENABLE ( ( unsigned short ) 0x1 << 24 )
|
93 |
|
|
#define portPIT_INT_ENABLE ( ( unsigned short ) 0x1 << 25 )
|
94 |
|
|
/*-----------------------------------------------------------*/
|
95 |
|
|
|
96 |
|
|
/* Setup the PIT to generate the tick interrupts. */
|
97 |
|
|
static void prvSetupTimerInterrupt( void );
|
98 |
|
|
|
99 |
|
|
/* The PIT interrupt handler - the RTOS tick. */
|
100 |
|
|
static void vPortTickISR( void );
|
101 |
|
|
|
102 |
|
|
/* ulCriticalNesting will get set to zero when the first task starts. It
|
103 |
|
|
cannot be initialised to 0 as this will cause interrupts to be enabled
|
104 |
|
|
during the kernel initialisation process. */
|
105 |
|
|
unsigned long ulCriticalNesting = ( unsigned long ) 9999;
|
106 |
|
|
|
107 |
|
|
/*-----------------------------------------------------------*/
|
108 |
|
|
|
109 |
|
|
/*
|
110 |
|
|
* Initialise the stack of a task to look exactly as if a call to
|
111 |
|
|
* portSAVE_CONTEXT had been called.
|
112 |
|
|
*
|
113 |
|
|
* See header file for description.
|
114 |
|
|
*/
|
115 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
116 |
|
|
{
|
117 |
|
|
portSTACK_TYPE *pxOriginalTOS;
|
118 |
|
|
|
119 |
|
|
pxOriginalTOS = pxTopOfStack;
|
120 |
|
|
|
121 |
|
|
/* Setup the initial stack of the task. The stack is set exactly as
|
122 |
|
|
expected by the portRESTORE_CONTEXT() macro. */
|
123 |
|
|
|
124 |
|
|
/* First on the stack is the return address - which in this case is the
|
125 |
|
|
start of the task. The offset is added to make the return address appear
|
126 |
|
|
as it would within an IRQ ISR. */
|
127 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
|
128 |
|
|
pxTopOfStack--;
|
129 |
|
|
|
130 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
|
131 |
|
|
pxTopOfStack--;
|
132 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
|
133 |
|
|
pxTopOfStack--;
|
134 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
|
135 |
|
|
pxTopOfStack--;
|
136 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
|
137 |
|
|
pxTopOfStack--;
|
138 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
|
139 |
|
|
pxTopOfStack--;
|
140 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
|
141 |
|
|
pxTopOfStack--;
|
142 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
|
143 |
|
|
pxTopOfStack--;
|
144 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
|
145 |
|
|
pxTopOfStack--;
|
146 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
|
147 |
|
|
pxTopOfStack--;
|
148 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
|
149 |
|
|
pxTopOfStack--;
|
150 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
|
151 |
|
|
pxTopOfStack--;
|
152 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
|
153 |
|
|
pxTopOfStack--;
|
154 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
|
155 |
|
|
pxTopOfStack--;
|
156 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
|
157 |
|
|
pxTopOfStack--;
|
158 |
|
|
|
159 |
|
|
/* When the task starts is will expect to find the function parameter in
|
160 |
|
|
R0. */
|
161 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
|
162 |
|
|
pxTopOfStack--;
|
163 |
|
|
|
164 |
|
|
/* The status register is set for system mode, with interrupts enabled. */
|
165 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
|
166 |
|
|
|
167 |
|
|
#ifdef THUMB_INTERWORK
|
168 |
|
|
{
|
169 |
|
|
/* We want the task to start in thumb mode. */
|
170 |
|
|
*pxTopOfStack |= portTHUMB_MODE_BIT;
|
171 |
|
|
}
|
172 |
|
|
#endif
|
173 |
|
|
|
174 |
|
|
pxTopOfStack--;
|
175 |
|
|
|
176 |
|
|
/* Interrupt flags cannot always be stored on the stack and will
|
177 |
|
|
instead be stored in a variable, which is then saved as part of the
|
178 |
|
|
tasks context. */
|
179 |
|
|
*pxTopOfStack = portNO_CRITICAL_NESTING;
|
180 |
|
|
|
181 |
|
|
return pxTopOfStack;
|
182 |
|
|
}
|
183 |
|
|
/*-----------------------------------------------------------*/
|
184 |
|
|
|
185 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
186 |
|
|
{
|
187 |
|
|
extern void vPortStartFirstTask( void );
|
188 |
|
|
|
189 |
|
|
/* Start the timer that generates the tick ISR. Interrupts are disabled
|
190 |
|
|
here already. */
|
191 |
|
|
prvSetupTimerInterrupt();
|
192 |
|
|
|
193 |
|
|
/* Start the first task. */
|
194 |
|
|
vPortStartFirstTask();
|
195 |
|
|
|
196 |
|
|
/* Should not get here! */
|
197 |
|
|
return 0;
|
198 |
|
|
}
|
199 |
|
|
/*-----------------------------------------------------------*/
|
200 |
|
|
|
201 |
|
|
void vPortEndScheduler( void )
|
202 |
|
|
{
|
203 |
|
|
/* It is unlikely that the ARM port will require this function as there
|
204 |
|
|
is nothing to return to. */
|
205 |
|
|
}
|
206 |
|
|
/*-----------------------------------------------------------*/
|
207 |
|
|
|
208 |
|
|
static __arm void vPortTickISR( void )
|
209 |
|
|
{
|
210 |
|
|
volatile unsigned long ulDummy;
|
211 |
|
|
|
212 |
|
|
/* Increment the tick count - which may wake some tasks but as the
|
213 |
|
|
preemptive scheduler is not being used any woken task is not given
|
214 |
|
|
processor time no matter what its priority. */
|
215 |
|
|
vTaskIncrementTick();
|
216 |
|
|
|
217 |
|
|
#if configUSE_PREEMPTION == 1
|
218 |
|
|
vTaskSwitchContext();
|
219 |
|
|
#endif
|
220 |
|
|
|
221 |
|
|
/* Clear the PIT interrupt. */
|
222 |
|
|
ulDummy = AT91C_BASE_PITC->PITC_PIVR;
|
223 |
|
|
|
224 |
|
|
/* To remove compiler warning. */
|
225 |
|
|
( void ) ulDummy;
|
226 |
|
|
|
227 |
|
|
/* The AIC is cleared in the asm wrapper, outside of this function. */
|
228 |
|
|
}
|
229 |
|
|
/*-----------------------------------------------------------*/
|
230 |
|
|
|
231 |
|
|
static void prvSetupTimerInterrupt( void )
|
232 |
|
|
{
|
233 |
|
|
const unsigned long ulPeriodIn_uS = ( 1.0 / ( double ) configTICK_RATE_HZ ) * port1SECOND_IN_uS;
|
234 |
|
|
|
235 |
|
|
/* Setup the PIT for the required frequency. */
|
236 |
|
|
PIT_Init( ulPeriodIn_uS, BOARD_MCK / port1MHz_IN_Hz );
|
237 |
|
|
|
238 |
|
|
/* Setup the PIT interrupt. */
|
239 |
|
|
AIC_DisableIT( AT91C_ID_SYS );
|
240 |
|
|
AIC_ConfigureIT( AT91C_ID_SYS, AT91C_AIC_PRIOR_LOWEST, vPortTickISR );
|
241 |
|
|
AIC_EnableIT( AT91C_ID_SYS );
|
242 |
|
|
PIT_EnableIT();
|
243 |
|
|
}
|
244 |
|
|
/*-----------------------------------------------------------*/
|
245 |
|
|
|
246 |
|
|
void vPortEnterCritical( void )
|
247 |
|
|
{
|
248 |
|
|
/* Disable interrupts first! */
|
249 |
|
|
__disable_irq();
|
250 |
|
|
|
251 |
|
|
/* Now interrupts are disabled ulCriticalNesting can be accessed
|
252 |
|
|
directly. Increment ulCriticalNesting to keep a count of how many times
|
253 |
|
|
portENTER_CRITICAL() has been called. */
|
254 |
|
|
ulCriticalNesting++;
|
255 |
|
|
}
|
256 |
|
|
/*-----------------------------------------------------------*/
|
257 |
|
|
|
258 |
|
|
void vPortExitCritical( void )
|
259 |
|
|
{
|
260 |
|
|
if( ulCriticalNesting > portNO_CRITICAL_NESTING )
|
261 |
|
|
{
|
262 |
|
|
/* Decrement the nesting count as we are leaving a critical section. */
|
263 |
|
|
ulCriticalNesting--;
|
264 |
|
|
|
265 |
|
|
/* If the nesting level has reached zero then interrupts should be
|
266 |
|
|
re-enabled. */
|
267 |
|
|
if( ulCriticalNesting == portNO_CRITICAL_NESTING )
|
268 |
|
|
{
|
269 |
|
|
__enable_irq();
|
270 |
|
|
}
|
271 |
|
|
}
|
272 |
|
|
}
|
273 |
|
|
/*-----------------------------------------------------------*/
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|