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 OpenRISC port.
|
56 |
|
|
*----------------------------------------------------------*/
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* Scheduler includes. */
|
60 |
|
|
#include "FreeRTOS.h"
|
61 |
|
|
#include "task.h"
|
62 |
|
|
|
63 |
|
|
/* Processor constants. */
|
64 |
|
|
#include "port_spr_defs.h"
|
65 |
|
|
|
66 |
621 |
filepang |
/* Jump buffer */
|
67 |
|
|
#include <setjmp.h>
|
68 |
|
|
static jmp_buf jmpbuf;
|
69 |
|
|
|
70 |
572 |
jeremybenn |
/* Tick Timer Interrupt handler */
|
71 |
|
|
void vTickHandler( void );
|
72 |
|
|
|
73 |
|
|
/* Setup the timer to generate the tick interrupts. */
|
74 |
|
|
static void prvSetupTimerInterrupt( void );
|
75 |
|
|
|
76 |
|
|
/* For writing into SPR. */
|
77 |
|
|
static inline void mtspr(unsigned long spr, unsigned long value) {
|
78 |
|
|
asm("l.mtspr\t\t%0,%1,0": : "r" (spr), "r" (value));
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/* For reading SPR. */
|
82 |
|
|
static inline unsigned long mfspr(unsigned long spr) {
|
83 |
|
|
unsigned long value;
|
84 |
|
|
asm("l.mfspr\t\t%0,%1,0" : "=r" (value) : "r" (spr));
|
85 |
|
|
return value;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
/*
|
90 |
|
|
* naked attribute is ignored or32-elf-gcc 4.5.1-or32-1.0rc1
|
91 |
|
|
* use assemble routines in portasm.S
|
92 |
|
|
*/
|
93 |
|
|
#if 0
|
94 |
|
|
void vPortDisableInterrupts( void ) __attribute__ ((__naked__))
|
95 |
|
|
{
|
96 |
|
|
asm volatile ( \
|
97 |
|
|
" @ get current SR \n\t" \
|
98 |
|
|
" l.mfspr r3, r0, SPR_SR \n\t" \
|
99 |
|
|
" l.addi r4, r0, SPR_SR_TEE \n\t" \
|
100 |
|
|
" l.xori r4, r4, 0xffffffff \n\t" \
|
101 |
|
|
" l.and r3, r3, r4 \n\t" \
|
102 |
|
|
" l.addi r4, r0, SPR_SR_IEE \n\t" \
|
103 |
|
|
" l.xori r4, r4, 0xffffffff \n\t" \
|
104 |
|
|
" l.and r3, r3, r4 \n\t" \
|
105 |
|
|
" @ update SR \n\t" \
|
106 |
|
|
" l.mtspr r0, r3, SPR_SR \n\t" \
|
107 |
|
|
);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
void vPortEnableInterrupts( void ) __attribute__ ((__naked__))
|
111 |
|
|
{
|
112 |
|
|
asm volatile ( \
|
113 |
|
|
" @ get current SR \n\t" \
|
114 |
|
|
" l.mfspr r3, r0, SPR_SR \n\t" \
|
115 |
|
|
" @ enable Tick Timer Interrupt \n\t" \
|
116 |
|
|
" l.ori r3, r3, SPR_SR_TEE \n\t" \
|
117 |
|
|
" @ enable External Interrupt \n\t" \
|
118 |
|
|
" l.ori r3, r3, SPR_SR_IEE \n\t" \
|
119 |
|
|
" @ update SR \n\t" \
|
120 |
|
|
" l.mtspr r0, r3, SPR_SR \n\t" \
|
121 |
|
|
);
|
122 |
|
|
}
|
123 |
|
|
#endif
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
/*
|
127 |
|
|
* Initialise the stack of a task to look exactly as if a call to
|
128 |
|
|
* portSAVE_CONTEXT had been called.
|
129 |
|
|
*
|
130 |
|
|
* See header file for description.
|
131 |
|
|
*/
|
132 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
133 |
|
|
{
|
134 |
|
|
unsigned portLONG uTaskSR = mfspr(SPR_ESR_BASE);
|
135 |
|
|
uTaskSR &= ~SPR_SR_SM; // User mode
|
136 |
|
|
uTaskSR |= (SPR_SR_TEE | SPR_SR_IEE); // Tick interrupt enable, All External interupt enable
|
137 |
|
|
|
138 |
|
|
/* Setup the initial stack of the task. The stack is set exactly as
|
139 |
|
|
expected by the portRESTORE_CONTEXT() macro. */
|
140 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)pxCode; // SPR_EPCR_BASE(0)
|
141 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)uTaskSR; // SPR_ESR_BASE(0)
|
142 |
|
|
|
143 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000031; // r31
|
144 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000030; // r30
|
145 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000029; // r29
|
146 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000028; // r28
|
147 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000027; // r27
|
148 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000026; // r26
|
149 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000025; // r25
|
150 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000024; // r24
|
151 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000023; // r23
|
152 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000022; // r22
|
153 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000021; // r21
|
154 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000020; // r20
|
155 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000019; // r19
|
156 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000018; // r18
|
157 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000017; // r17
|
158 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000016; // r16
|
159 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000015; // r15
|
160 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000014; // r14
|
161 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000013; // r13
|
162 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000012; // r12
|
163 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000011; // r11
|
164 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000010; // r10
|
165 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000008; // r8
|
166 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000007; // r7
|
167 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000006; // r6
|
168 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000005; // r5
|
169 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000004; // r4
|
170 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)pvParameters; // task argument
|
171 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)0x00000002; // r2
|
172 |
|
|
*(--pxTopOfStack) = (portSTACK_TYPE)pxCode; // PC
|
173 |
|
|
|
174 |
|
|
return pxTopOfStack;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
178 |
|
|
{
|
179 |
621 |
filepang |
if(setjmp((void *)jmpbuf) == 0) {
|
180 |
|
|
/* Start the timer that generates the tick ISR. Interrupts are disabled
|
181 |
|
|
here already. */
|
182 |
|
|
prvSetupTimerInterrupt();
|
183 |
572 |
jeremybenn |
|
184 |
621 |
filepang |
/* Start the first task. */
|
185 |
|
|
portRESTORE_CONTEXT();
|
186 |
|
|
|
187 |
|
|
/* Should not get here! */
|
188 |
|
|
} else {
|
189 |
|
|
/* Retrun by vPortEndScheduler */
|
190 |
|
|
}
|
191 |
|
|
|
192 |
572 |
jeremybenn |
return 0;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
void vPortEndScheduler( void )
|
196 |
|
|
{
|
197 |
|
|
mtspr(SPR_SR, mfspr(SPR_SR) & (~SPR_SR_TEE)); // Tick stop
|
198 |
621 |
filepang |
longjmp((void *)jmpbuf, 1); // return to xPortStartScheduler
|
199 |
572 |
jeremybenn |
}
|
200 |
|
|
|
201 |
|
|
/*
|
202 |
|
|
* Setup the tick timer to generate the tick interrupts at the required frequency.
|
203 |
|
|
*/
|
204 |
|
|
static void prvSetupTimerInterrupt( void )
|
205 |
|
|
{
|
206 |
|
|
const unsigned portLONG ulTickPeriod = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
|
207 |
|
|
|
208 |
|
|
// Disable tick timer exception recognition
|
209 |
|
|
mtspr(SPR_SR, mfspr(SPR_SR) & ~SPR_SR_TEE);
|
210 |
|
|
|
211 |
|
|
// clears interrupt
|
212 |
|
|
mtspr(SPR_TTMR, mfspr(SPR_TTMR) & ~(SPR_TTMR_IP));
|
213 |
|
|
|
214 |
|
|
// Set period of one cycle, restartable mode
|
215 |
|
|
mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT | (ulTickPeriod & SPR_TTMR_PERIOD));
|
216 |
|
|
|
217 |
|
|
// Reset counter
|
218 |
|
|
mtspr(SPR_TTCR, 0);
|
219 |
|
|
|
220 |
|
|
// set OR1200 to accept exceptions
|
221 |
|
|
mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_TEE);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
621 |
filepang |
/*
|
225 |
|
|
* naked attribute is ignored or32-elf-gcc 4.5.1-or32-1.0rc1
|
226 |
|
|
* use assemble routines in portasm.S
|
227 |
|
|
*/
|
228 |
572 |
jeremybenn |
#if 0
|
229 |
|
|
void vTickHandler( void )
|
230 |
|
|
{
|
231 |
|
|
// clears interrupt
|
232 |
|
|
mtspr(SPR_TTMR, mfspr(SPR_TTMR) & ~(SPR_TTMR_IP));
|
233 |
|
|
|
234 |
|
|
/* Increment the RTOS tick count, then look for the highest priority
|
235 |
|
|
task that is ready to run. */
|
236 |
|
|
vTaskIncrementTick();
|
237 |
|
|
|
238 |
|
|
// The cooperative scheduler requires a normal simple Tick ISR to
|
239 |
|
|
// simply increment the system tick.
|
240 |
|
|
#if configUSE_PREEMPTION == 0
|
241 |
|
|
// nothing to do here
|
242 |
|
|
#else
|
243 |
|
|
/* Save the context of the current task. */
|
244 |
|
|
portSAVE_CONTEXT();
|
245 |
|
|
|
246 |
|
|
/* Find the highest priority task that is ready to run. */
|
247 |
|
|
vTaskSwitchContext();
|
248 |
|
|
|
249 |
|
|
portRESTORE_CONTEXT();
|
250 |
|
|
#endif
|
251 |
|
|
}
|
252 |
|
|
#endif
|