OpenCores
URL https://opencores.org/ocsvn/openfire2/openfire2/trunk

Subversion Repositories openfire2

[/] [openfire2/] [trunk/] [sw/] [freertos/] [port.c] - Blame information for rev 8

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 toni32
/*
2
        FreeRTOS.org V4.2.0 - Copyright (C) 2003-2007 Richard Barry.
3
 
4
        This file is part of the FreeRTOS.org distribution.
5
 
6
        FreeRTOS.org is free software; you can redistribute it and/or modify
7
        it under the terms of the GNU General Public License as published by
8
        the Free Software Foundation; either version 2 of the License, or
9
        (at your option) any later version.
10
 
11
        FreeRTOS.org is distributed in the hope that it will be useful,
12
        but WITHOUT ANY WARRANTY; without even the implied warranty of
13
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
        GNU General Public License for more details.
15
 
16
        You should have received a copy of the GNU General Public License
17
        along with FreeRTOS.org; if not, write to the Free Software
18
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
        A special exception to the GPL can be applied should you wish to distribute
21
        a combined work that includes FreeRTOS.org, without being obliged to provide
22
        the source code for any proprietary components.  See the licensing section
23
        of http://www.FreeRTOS.org for full details of how and when the exception
24
        can be applied.
25
 
26
        ***************************************************************************
27
        See http://www.FreeRTOS.org for documentation, latest information, license
28
        and contact details.  Please ensure to read the configuration and relevant
29
        port sections of the online documentation.
30
        ***************************************************************************
31
*/
32
 
33
/*-----------------------------------------------------------
34
 * Implementation of functions defined in portable.h for the MicroBlaze port.
35
 *----------------------------------------------------------*/
36
 
37
#include <stdio.h>
38
 
39
/* Scheduler includes. */
40
#include "FreeRTOS.h"
41
#include "task.h"
42
 
43
/* Standard includes. */
44
#include <string.h>
45
 
46
/* Hardware includes. */
47
#include "openfire.h"
48
 
49
/* Tasks are started with interrupts enabled. */
50
#define portINITIAL_MSR_STATE           ( ( portSTACK_TYPE ) 0x02 )
51
 
52
/* Tasks are started with a critical section nesting of 0 - however prior
53
to the scheduler being commenced we don't want the critical nesting level
54
to reach zero, so it is initialised to a high value. */
55
#define portINITIAL_NESTING_VALUE       ( 0xff )
56
 
57
/* The stack used by the ISR is filled with a known value to assist in
58
debugging. */
59
#define portISR_STACK_FILL_VALUE        0x55555555
60
 
61
/* Counts the nesting depth of calls to portENTER_CRITICAL().  Each task
62
maintains it's own count, so this variable is saved as part of the task
63
context. */
64
volatile unsigned portBASE_TYPE uxCriticalNesting = portINITIAL_NESTING_VALUE;
65
 
66
/* To limit the amount of stack required by each task, this port uses a
67
separate stack for interrupts. */
68
unsigned portLONG *pulISRStack;
69
 
70
/*-----------------------------------------------------------*/
71
 
72
/*
73
 * Sets up the periodic ISR used for the RTOS tick.  This uses timer 0, but
74
 * could have alternatively used the watchdog timer or timer 1.
75
 */
76
static void prvSetupTimerInterrupt( void );
77
/*-----------------------------------------------------------*/
78
 
79
/*
80
 * Initialise the stack of a task to look exactly as if a call to
81
 * portSAVE_CONTEXT had been made.
82
 *
83
 * See the header file portable.h.
84
 */
85
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
86
{
87
extern void *_SDA2_BASE_, *_SDA_BASE_;
88
const unsigned portLONG ulR2 = ( unsigned portLONG ) &_SDA2_BASE_;
89
const unsigned portLONG ulR13 = ( unsigned portLONG ) &_SDA_BASE_;
90
 
91
        /* Place a few bytes of known values on the bottom of the stack.
92
        This is essential for the Microblaze port and these lines must
93
        not be omitted.  The parameter value will overwrite the
94
        0x22222222 value during the function prologue. */
95
        *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;
96
        pxTopOfStack--;
97
        *pxTopOfStack = ( portSTACK_TYPE ) 0x22222222;
98
        pxTopOfStack--;
99
        *pxTopOfStack = ( portSTACK_TYPE ) 0x33333333;
100
        pxTopOfStack--;
101
 
102
        /* First stack an initial value for the critical section nesting.  This
103
        is initialised to zero as tasks are started with interrupts enabled. */
104
        *pxTopOfStack = ( portSTACK_TYPE ) 0x00;        /* R0. */
105
 
106
        /* Place an initial value for all the general purpose registers. */
107
        pxTopOfStack--;
108
        *pxTopOfStack = ( portSTACK_TYPE ) ulR2;        /* R2 - small data area. */
109
        pxTopOfStack--;
110
        *pxTopOfStack = ( portSTACK_TYPE ) 0x03;        /* R3. */
111
        pxTopOfStack--;
112
        *pxTopOfStack = ( portSTACK_TYPE ) 0x04;        /* R4. */
113
        pxTopOfStack--;
114
        *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;/* R5 contains the function call parameters. */
115
        pxTopOfStack--;
116
        *pxTopOfStack = ( portSTACK_TYPE ) 0x06;        /* R6. */
117
        pxTopOfStack--;
118
        *pxTopOfStack = ( portSTACK_TYPE ) 0x07;        /* R7. */
119
        pxTopOfStack--;
120
        *pxTopOfStack = ( portSTACK_TYPE ) 0x08;        /* R8. */
121
        pxTopOfStack--;
122
        *pxTopOfStack = ( portSTACK_TYPE ) 0x09;        /* R9. */
123
        pxTopOfStack--;
124
        *pxTopOfStack = ( portSTACK_TYPE ) 0x0a;        /* R10. */
125
        pxTopOfStack--;
126
        *pxTopOfStack = ( portSTACK_TYPE ) 0x0b;        /* R11. */
127
        pxTopOfStack--;
128
        *pxTopOfStack = ( portSTACK_TYPE ) 0x0c;        /* R12. */
129
        pxTopOfStack--;
130
        *pxTopOfStack = ( portSTACK_TYPE ) ulR13;       /* R13 - small data read write area. */
131
        pxTopOfStack--;
132
        *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* R14. */
133
        pxTopOfStack--;
134
        *pxTopOfStack = ( portSTACK_TYPE ) 0x0f;        /* R15. */
135
        pxTopOfStack--;
136
        *pxTopOfStack = ( portSTACK_TYPE ) 0x10;        /* R16. */
137
        pxTopOfStack--;
138
        *pxTopOfStack = ( portSTACK_TYPE ) 0x11;        /* R17. */
139
        pxTopOfStack--;
140
        *pxTopOfStack = ( portSTACK_TYPE ) 0x12;        /* R18. */
141
        pxTopOfStack--;
142
        *pxTopOfStack = ( portSTACK_TYPE ) 0x13;        /* R19. */
143
        pxTopOfStack--;
144
        *pxTopOfStack = ( portSTACK_TYPE ) 0x14;        /* R20. */
145
        pxTopOfStack--;
146
        *pxTopOfStack = ( portSTACK_TYPE ) 0x15;        /* R21. */
147
        pxTopOfStack--;
148
        *pxTopOfStack = ( portSTACK_TYPE ) 0x16;        /* R22. */
149
        pxTopOfStack--;
150
        *pxTopOfStack = ( portSTACK_TYPE ) 0x17;        /* R23. */
151
        pxTopOfStack--;
152
        *pxTopOfStack = ( portSTACK_TYPE ) 0x18;        /* R24. */
153
        pxTopOfStack--;
154
        *pxTopOfStack = ( portSTACK_TYPE ) 0x19;        /* R25. */
155
        pxTopOfStack--;
156
        *pxTopOfStack = ( portSTACK_TYPE ) 0x1a;        /* R26. */
157
        pxTopOfStack--;
158
        *pxTopOfStack = ( portSTACK_TYPE ) 0x1b;        /* R27. */
159
        pxTopOfStack--;
160
        *pxTopOfStack = ( portSTACK_TYPE ) 0x1c;        /* R28. */
161
        pxTopOfStack--;
162
        *pxTopOfStack = ( portSTACK_TYPE ) 0x1d;        /* R29. */
163
        pxTopOfStack--;
164
        *pxTopOfStack = ( portSTACK_TYPE ) 0x1e;        /* R30. */
165
        pxTopOfStack--;
166
 
167
        /* The MSR is stacked between R30 and R31. */
168
        *pxTopOfStack = portINITIAL_MSR_STATE;
169
        pxTopOfStack--;
170
 
171
        *pxTopOfStack = ( portSTACK_TYPE ) 0x1f;        /* R31. */
172
        pxTopOfStack--;
173
 
174
        /* Return a pointer to the top of the stack we have generated so this can
175
        be stored in the task control block for the task. */
176
        return pxTopOfStack;
177
}
178
/*-----------------------------------------------------------*/
179
 
180
portBASE_TYPE xPortStartScheduler( void )
181
{
182
extern void ( __FreeRTOS_interrupt_Handler )( void );
183
extern void ( vStartFirstTask )( void );
184
 
185
        /* Setup the FreeRTOS interrupt handler.  Code copied from crt0.s. */
186
        asm volatile (  "la  r6, r0, __FreeRTOS_interrupt_handler       \n\t" \
187
                        "sw  r6, r1, r0                                 \n\t" \
188
                        "lhu r7, r1, r0                                 \n\t" \
189
                        "shi r7, r0, 0x12                               \n\t" \
190
                        "shi r6, r0, 0x16                               \n\t" );
191
 
192
        /* Setup the hardware to generate the tick.  Interrupts are disabled when
193
        this function is called. */
194
        prvSetupTimerInterrupt();
195
 
196
        /* Allocate the stack to be used by the interrupt handler. */
197
        pulISRStack = ( unsigned portLONG * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ) );
198
 
199
        /* Restore the context of the first task that is going to run. */
200
        if( pulISRStack != NULL )
201
        {
202
                /* Fill the ISR stack with a known value to facilitate debugging. */
203
                memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ) );
204
                pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );
205
 
206
                /* Kick off the first task. */
207
                vStartFirstTask();
208
        }
209
 
210
        /* Should not get here as the tasks are now running! */
211
        return pdFALSE;
212
}
213
/*-----------------------------------------------------------*/
214
 
215
void vPortEndScheduler( void )
216
{
217
        /* Not implemented. */
218
}
219
/*-----------------------------------------------------------*/
220
 
221
/*
222
 * Manual context switch called by portYIELD or taskYIELD.
223
 */
224
void vPortYield( void )
225
{
226
extern void VPortYieldASM( void );
227
 
228
        /* Perform the context switch in a critical section to assure it is
229
        not interrupted by the tick ISR.  It is not a problem to do this as
230
        each task maintains it's own interrupt status. */
231
        portENTER_CRITICAL();
232
                /* Jump directly to the yield function to ensure there is no
233
                compiler generated prologue code. */
234
                asm volatile (  "bralid r14, VPortYieldASM              \n\t" \
235
                                "or r0, r0, r0                          \n\t" );
236
        portEXIT_CRITICAL();
237
}
238
/*-----------------------------------------------------------*/
239
 
240
/*
241
 * Hardware initialisation to generate the RTOS tick.
242
 */
243
static void prvSetupTimerInterrupt( void )
244
{
245
const unsigned portLONG ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
246
 
247
        /* configure Timer1 with ulCounterValue and enable timer */
248
        *(unsigned portLONG *) TIMER1_PORT = TIMER1_CONTROL | ulCounterValue;
249
 
250
        /* enable interrupts for timer1 */
251
        *(unsigned portLONG *) INTERRUPT_ENABLE = INTERRUPT_TIMER1;
252
}
253
/*-----------------------------------------------------------*/
254
 
255
/*
256
 * The interrupt handler placed in the interrupt vector when the scheduler is
257
 * started.  The task context has already been saved when this is called.
258
 * This handler determines the interrupt source and calls the relevant
259
 * peripheral handler.
260
 */
261
void vTaskISRHandler( void )
262
{
263
        /* todo : check which peripheral triggered the interrupt */
264
 
265
        /* Increment the RTOS tick - this might cause a task to unblock. */
266
        vTaskIncrementTick();
267
 
268
        /* Clear the timer interrupt */
269
        /* automatically cleread --> better design? */
270
 
271
        /* If we are using the preemptive scheduler then we also need to determine
272
        if this tick should cause a context switch. */
273
        #if configUSE_PREEMPTION == 1
274
                vTaskSwitchContext();
275
        #endif
276
}
277
/*-----------------------------------------------------------*/
278
 
279
void openfire_disable_interrupts(void)
280
{
281
  asm volatile ( "mfs r5, rmsr                  \n\t"   \
282
                 "andi r5, r0, ~0x2             \n\t"   \
283
                 "mts rmsr, r5                  \n\t");
284
}
285
 
286
void openfire_enable_interrupts(void)
287
{
288
  asm volatile ( "mfs r5, rmsr                  \n\t"   \
289
                 "ori r5, r0, 0x2               \n\t"   \
290
                 "mts rmsr, r5                  \n\t");
291
}
292
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.