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 Tern EE 186
|
57 |
|
|
* port.
|
58 |
|
|
*----------------------------------------------------------*/
|
59 |
|
|
|
60 |
|
|
/* Library includes. */
|
61 |
|
|
#include <embedded.h>
|
62 |
|
|
#include <ae.h>
|
63 |
|
|
|
64 |
|
|
/* Scheduler includes. */
|
65 |
|
|
#include "FreeRTOS.h"
|
66 |
|
|
#include "task.h"
|
67 |
|
|
#include "portasm.h"
|
68 |
|
|
|
69 |
|
|
/* The timer increments every four clocks, hence the divide by 4. */
|
70 |
|
|
#define portPRESCALE_VALUE ( 16 )
|
71 |
|
|
#define portTIMER_COMPARE ( configCPU_CLOCK_HZ / ( configTICK_RATE_HZ * 4UL ) )
|
72 |
|
|
|
73 |
|
|
/* From the RDC data sheet. */
|
74 |
|
|
#define portENABLE_TIMER_AND_INTERRUPT ( unsigned short ) 0xe00b
|
75 |
|
|
#define portENABLE_TIMER ( unsigned short ) 0xC001
|
76 |
|
|
|
77 |
|
|
/* Interrupt control. */
|
78 |
|
|
#define portEIO_REGISTER 0xff22
|
79 |
|
|
#define portCLEAR_INTERRUPT 0x0008
|
80 |
|
|
|
81 |
|
|
/* Setup the hardware to generate the required tick frequency. */
|
82 |
|
|
static void prvSetupTimerInterrupt( void );
|
83 |
|
|
|
84 |
|
|
/* The ISR used depends on whether the preemptive or cooperative scheduler
|
85 |
|
|
is being used. */
|
86 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
87 |
|
|
/* Tick service routine used by the scheduler when preemptive scheduling is
|
88 |
|
|
being used. */
|
89 |
|
|
static void __interrupt __far prvPreemptiveTick( void );
|
90 |
|
|
#else
|
91 |
|
|
/* Tick service routine used by the scheduler when cooperative scheduling is
|
92 |
|
|
being used. */
|
93 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void );
|
94 |
|
|
#endif
|
95 |
|
|
|
96 |
|
|
/* Trap routine used by taskYIELD() to manually cause a context switch. */
|
97 |
|
|
static void __interrupt __far prvYieldProcessor( void );
|
98 |
|
|
|
99 |
|
|
/*-----------------------------------------------------------*/
|
100 |
|
|
/* See header file for description. */
|
101 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
102 |
|
|
{
|
103 |
|
|
portSTACK_TYPE DS_Reg = 0;
|
104 |
|
|
|
105 |
|
|
/* We need the true data segment. */
|
106 |
|
|
__asm{ MOV DS_Reg, DS };
|
107 |
|
|
|
108 |
|
|
/* Place a few bytes of known values on the bottom of the stack.
|
109 |
|
|
This is just useful for debugging. */
|
110 |
|
|
|
111 |
|
|
*pxTopOfStack = 0x1111;
|
112 |
|
|
pxTopOfStack--;
|
113 |
|
|
*pxTopOfStack = 0x2222;
|
114 |
|
|
pxTopOfStack--;
|
115 |
|
|
*pxTopOfStack = 0x3333;
|
116 |
|
|
pxTopOfStack--;
|
117 |
|
|
|
118 |
|
|
/* We are going to start the scheduler using a return from interrupt
|
119 |
|
|
instruction to load the program counter, so first there would be the
|
120 |
|
|
function call with parameters preamble. */
|
121 |
|
|
|
122 |
|
|
*pxTopOfStack = FP_OFF( pvParameters );
|
123 |
|
|
pxTopOfStack--;
|
124 |
|
|
*pxTopOfStack = FP_OFF( pxCode );
|
125 |
|
|
pxTopOfStack--;
|
126 |
|
|
|
127 |
|
|
/* Next the status register and interrupt return address. */
|
128 |
|
|
*pxTopOfStack = portINITIAL_SW;
|
129 |
|
|
pxTopOfStack--;
|
130 |
|
|
*pxTopOfStack = FP_SEG( pxCode );
|
131 |
|
|
pxTopOfStack--;
|
132 |
|
|
*pxTopOfStack = FP_OFF( pxCode );
|
133 |
|
|
pxTopOfStack--;
|
134 |
|
|
|
135 |
|
|
/* The remaining registers would be pushed on the stack by our context
|
136 |
|
|
switch function. These are loaded with values simply to make debugging
|
137 |
|
|
easier. */
|
138 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xAAAA; /* AX */
|
139 |
|
|
pxTopOfStack--;
|
140 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xBBBB; /* BX */
|
141 |
|
|
pxTopOfStack--;
|
142 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xCCCC; /* CX */
|
143 |
|
|
pxTopOfStack--;
|
144 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xDDDD; /* DX */
|
145 |
|
|
pxTopOfStack--;
|
146 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xEEEE; /* ES */
|
147 |
|
|
pxTopOfStack--;
|
148 |
|
|
|
149 |
|
|
*pxTopOfStack = DS_Reg; /* DS */
|
150 |
|
|
pxTopOfStack--;
|
151 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x0123; /* SI */
|
152 |
|
|
pxTopOfStack--;
|
153 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xDDDD; /* DI */
|
154 |
|
|
pxTopOfStack--;
|
155 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xBBBB; /* BP */
|
156 |
|
|
|
157 |
|
|
return pxTopOfStack;
|
158 |
|
|
}
|
159 |
|
|
/*-----------------------------------------------------------*/
|
160 |
|
|
|
161 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
162 |
|
|
{
|
163 |
|
|
/* This is called with interrupts already disabled. */
|
164 |
|
|
|
165 |
|
|
/* Put our manual switch (yield) function on a known
|
166 |
|
|
vector. */
|
167 |
|
|
setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
|
168 |
|
|
|
169 |
|
|
/* Setup the tick interrupt. */
|
170 |
|
|
prvSetupTimerInterrupt();
|
171 |
|
|
|
172 |
|
|
/* Kick off the scheduler by setting up the context of the first task. */
|
173 |
|
|
portFIRST_CONTEXT();
|
174 |
|
|
|
175 |
|
|
/* Should not get here! */
|
176 |
|
|
return pdFALSE;
|
177 |
|
|
}
|
178 |
|
|
/*-----------------------------------------------------------*/
|
179 |
|
|
|
180 |
|
|
/* The ISR used depends on whether the preemptive or cooperative scheduler
|
181 |
|
|
is being used. */
|
182 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
183 |
|
|
static void __interrupt __far prvPreemptiveTick( void )
|
184 |
|
|
{
|
185 |
|
|
/* Get the scheduler to update the task states following the tick. */
|
186 |
|
|
vTaskIncrementTick();
|
187 |
|
|
|
188 |
|
|
/* Switch in the context of the next task to be run. */
|
189 |
|
|
portEND_SWITCHING_ISR();
|
190 |
|
|
|
191 |
|
|
/* Reset interrupt. */
|
192 |
|
|
outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
|
193 |
|
|
}
|
194 |
|
|
#else
|
195 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void )
|
196 |
|
|
{
|
197 |
|
|
/* Same as preemptive tick, but the cooperative scheduler is being used
|
198 |
|
|
so we don't have to switch in the context of the next task. */
|
199 |
|
|
vTaskIncrementTick();
|
200 |
|
|
/* Reset interrupt. */
|
201 |
|
|
outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
|
202 |
|
|
}
|
203 |
|
|
#endif
|
204 |
|
|
/*-----------------------------------------------------------*/
|
205 |
|
|
|
206 |
|
|
static void __interrupt __far prvYieldProcessor( void )
|
207 |
|
|
{
|
208 |
|
|
/* Switch in the context of the next task to be run. */
|
209 |
|
|
portEND_SWITCHING_ISR();
|
210 |
|
|
}
|
211 |
|
|
/*-----------------------------------------------------------*/
|
212 |
|
|
|
213 |
|
|
void vPortEndScheduler( void )
|
214 |
|
|
{
|
215 |
|
|
/* Not implemented. */
|
216 |
|
|
}
|
217 |
|
|
/*-----------------------------------------------------------*/
|
218 |
|
|
|
219 |
|
|
static void prvSetupTimerInterrupt( void )
|
220 |
|
|
{
|
221 |
|
|
const unsigned long ulCompareValue = portTIMER_COMPARE;
|
222 |
|
|
unsigned short usTimerCompare;
|
223 |
|
|
|
224 |
|
|
usTimerCompare = ( unsigned short ) ( ulCompareValue >> 4 );
|
225 |
|
|
t2_init( portENABLE_TIMER, portPRESCALE_VALUE, NULL );
|
226 |
|
|
|
227 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
228 |
|
|
/* Tick service routine used by the scheduler when preemptive scheduling is
|
229 |
|
|
being used. */
|
230 |
|
|
t1_init( portENABLE_TIMER_AND_INTERRUPT, usTimerCompare, usTimerCompare, prvPreemptiveTick );
|
231 |
|
|
#else
|
232 |
|
|
/* Tick service routine used by the scheduler when cooperative scheduling is
|
233 |
|
|
being used. */
|
234 |
|
|
t1_init( portENABLE_TIMER_AND_INTERRUPT, usTimerCompare, usTimerCompare, prvNonPreemptiveTick );
|
235 |
|
|
#endif
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|