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 ST STR75x ARM7
|
56 |
|
|
* port.
|
57 |
|
|
*----------------------------------------------------------*/
|
58 |
|
|
|
59 |
|
|
/* Library includes. */
|
60 |
|
|
#include "75x_tb.h"
|
61 |
|
|
#include "75x_eic.h"
|
62 |
|
|
|
63 |
|
|
/* Scheduler includes. */
|
64 |
|
|
#include "FreeRTOS.h"
|
65 |
|
|
#include "task.h"
|
66 |
|
|
|
67 |
|
|
/* Constants required to setup the initial stack. */
|
68 |
|
|
#define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
|
69 |
|
|
#define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
|
70 |
|
|
|
71 |
|
|
/* Constants required to handle critical sections. */
|
72 |
|
|
#define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
|
73 |
|
|
|
74 |
|
|
/* Prescale used on the timer clock when calculating the tick period. */
|
75 |
|
|
#define portPRESCALE 20
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
/*-----------------------------------------------------------*/
|
79 |
|
|
|
80 |
|
|
/* Setup the TB to generate the tick interrupts. */
|
81 |
|
|
static void prvSetupTimerInterrupt( void );
|
82 |
|
|
|
83 |
|
|
/* ulCriticalNesting will get set to zero when the first task starts. It
|
84 |
|
|
cannot be initialised to 0 as this will cause interrupts to be enabled
|
85 |
|
|
during the kernel initialisation process. */
|
86 |
|
|
unsigned long ulCriticalNesting = ( unsigned long ) 9999;
|
87 |
|
|
|
88 |
|
|
/* Tick interrupt routines for preemptive operation. */
|
89 |
|
|
__arm void vPortPreemptiveTick( void );
|
90 |
|
|
|
91 |
|
|
/*-----------------------------------------------------------*/
|
92 |
|
|
|
93 |
|
|
/*
|
94 |
|
|
* Initialise the stack of a task to look exactly as if a call to
|
95 |
|
|
* portSAVE_CONTEXT had been called.
|
96 |
|
|
*
|
97 |
|
|
* See header file for description.
|
98 |
|
|
*/
|
99 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
100 |
|
|
{
|
101 |
|
|
portSTACK_TYPE *pxOriginalTOS;
|
102 |
|
|
|
103 |
|
|
pxOriginalTOS = pxTopOfStack;
|
104 |
|
|
|
105 |
|
|
/* Setup the initial stack of the task. The stack is set exactly as
|
106 |
|
|
expected by the portRESTORE_CONTEXT() macro. */
|
107 |
|
|
|
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 = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
|
112 |
|
|
pxTopOfStack--;
|
113 |
|
|
|
114 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
|
115 |
|
|
pxTopOfStack--;
|
116 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
|
117 |
|
|
pxTopOfStack--;
|
118 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
|
119 |
|
|
pxTopOfStack--;
|
120 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
|
121 |
|
|
pxTopOfStack--;
|
122 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
|
123 |
|
|
pxTopOfStack--;
|
124 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
|
125 |
|
|
pxTopOfStack--;
|
126 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
|
127 |
|
|
pxTopOfStack--;
|
128 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
|
129 |
|
|
pxTopOfStack--;
|
130 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
|
131 |
|
|
pxTopOfStack--;
|
132 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
|
133 |
|
|
pxTopOfStack--;
|
134 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
|
135 |
|
|
pxTopOfStack--;
|
136 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
|
137 |
|
|
pxTopOfStack--;
|
138 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
|
139 |
|
|
pxTopOfStack--;
|
140 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
|
141 |
|
|
pxTopOfStack--;
|
142 |
|
|
|
143 |
|
|
/* When the task starts is will expect to find the function parameter in
|
144 |
|
|
R0. */
|
145 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
|
146 |
|
|
pxTopOfStack--;
|
147 |
|
|
|
148 |
|
|
/* The status register is set for system mode, with interrupts enabled. */
|
149 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
|
150 |
|
|
pxTopOfStack--;
|
151 |
|
|
|
152 |
|
|
/* Interrupt flags cannot always be stored on the stack and will
|
153 |
|
|
instead be stored in a variable, which is then saved as part of the
|
154 |
|
|
tasks context. */
|
155 |
|
|
*pxTopOfStack = portNO_CRITICAL_NESTING;
|
156 |
|
|
|
157 |
|
|
return pxTopOfStack;
|
158 |
|
|
}
|
159 |
|
|
/*-----------------------------------------------------------*/
|
160 |
|
|
|
161 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
162 |
|
|
{
|
163 |
|
|
extern void vPortStartFirstTask( void );
|
164 |
|
|
|
165 |
|
|
/* Start the timer that generates the tick ISR. Interrupts are disabled
|
166 |
|
|
here already. */
|
167 |
|
|
prvSetupTimerInterrupt();
|
168 |
|
|
|
169 |
|
|
/* Start the first task. */
|
170 |
|
|
vPortStartFirstTask();
|
171 |
|
|
|
172 |
|
|
/* Should not get here! */
|
173 |
|
|
return 0;
|
174 |
|
|
}
|
175 |
|
|
/*-----------------------------------------------------------*/
|
176 |
|
|
|
177 |
|
|
void vPortEndScheduler( void )
|
178 |
|
|
{
|
179 |
|
|
/* It is unlikely that the ARM port will require this function as there
|
180 |
|
|
is nothing to return to. */
|
181 |
|
|
}
|
182 |
|
|
/*-----------------------------------------------------------*/
|
183 |
|
|
|
184 |
|
|
__arm void vPortPreemptiveTick( void )
|
185 |
|
|
{
|
186 |
|
|
/* Increment the tick counter. */
|
187 |
|
|
vTaskIncrementTick();
|
188 |
|
|
|
189 |
|
|
/* The new tick value might unblock a task. Ensure the highest task that
|
190 |
|
|
is ready to execute is the task that will execute when the tick ISR
|
191 |
|
|
exits. */
|
192 |
|
|
#if configUSE_PREEMPTION == 1
|
193 |
|
|
vTaskSwitchContext();
|
194 |
|
|
#endif
|
195 |
|
|
|
196 |
|
|
TB_ClearITPendingBit( TB_IT_Update );
|
197 |
|
|
}
|
198 |
|
|
/*-----------------------------------------------------------*/
|
199 |
|
|
|
200 |
|
|
static void prvSetupTimerInterrupt( void )
|
201 |
|
|
{
|
202 |
|
|
EIC_IRQInitTypeDef EIC_IRQInitStructure;
|
203 |
|
|
TB_InitTypeDef TB_InitStructure;
|
204 |
|
|
|
205 |
|
|
/* Setup the EIC for the TB. */
|
206 |
|
|
EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
|
207 |
|
|
EIC_IRQInitStructure.EIC_IRQChannel = TB_IRQChannel;
|
208 |
|
|
EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
|
209 |
|
|
EIC_IRQInit(&EIC_IRQInitStructure);
|
210 |
|
|
|
211 |
|
|
/* Setup the TB for the generation of the tick interrupt. */
|
212 |
|
|
TB_InitStructure.TB_Mode = TB_Mode_Timing;
|
213 |
|
|
TB_InitStructure.TB_CounterMode = TB_CounterMode_Down;
|
214 |
|
|
TB_InitStructure.TB_Prescaler = portPRESCALE - 1;
|
215 |
|
|
TB_InitStructure.TB_AutoReload = ( ( configCPU_CLOCK_HZ / portPRESCALE ) / configTICK_RATE_HZ );
|
216 |
|
|
TB_Init(&TB_InitStructure);
|
217 |
|
|
|
218 |
|
|
/* Enable TB Update interrupt */
|
219 |
|
|
TB_ITConfig(TB_IT_Update, ENABLE);
|
220 |
|
|
|
221 |
|
|
/* Clear TB Update interrupt pending bit */
|
222 |
|
|
TB_ClearITPendingBit(TB_IT_Update);
|
223 |
|
|
|
224 |
|
|
/* Enable TB */
|
225 |
|
|
TB_Cmd(ENABLE);
|
226 |
|
|
}
|
227 |
|
|
/*-----------------------------------------------------------*/
|
228 |
|
|
|
229 |
|
|
__arm __interwork void vPortEnterCritical( void )
|
230 |
|
|
{
|
231 |
|
|
/* Disable interrupts first! */
|
232 |
|
|
__disable_interrupt();
|
233 |
|
|
|
234 |
|
|
/* Now interrupts are disabled ulCriticalNesting can be accessed
|
235 |
|
|
directly. Increment ulCriticalNesting to keep a count of how many times
|
236 |
|
|
portENTER_CRITICAL() has been called. */
|
237 |
|
|
ulCriticalNesting++;
|
238 |
|
|
}
|
239 |
|
|
/*-----------------------------------------------------------*/
|
240 |
|
|
|
241 |
|
|
__arm __interwork void vPortExitCritical( void )
|
242 |
|
|
{
|
243 |
|
|
if( ulCriticalNesting > portNO_CRITICAL_NESTING )
|
244 |
|
|
{
|
245 |
|
|
/* Decrement the nesting count as we are leaving a critical section. */
|
246 |
|
|
ulCriticalNesting--;
|
247 |
|
|
|
248 |
|
|
/* If the nesting level has reached zero then interrupts should be
|
249 |
|
|
re-enabled. */
|
250 |
|
|
if( ulCriticalNesting == portNO_CRITICAL_NESTING )
|
251 |
|
|
{
|
252 |
|
|
__enable_interrupt();
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
/*-----------------------------------------------------------*/
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
|