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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Source/] [portable/] [SDCC/] [Cygnal/] [port.c] - Blame information for rev 857

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

Line No. Rev Author Line
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 Cygnal port.
56
 *----------------------------------------------------------*/
57
 
58
/* Standard includes. */
59
#include <string.h>
60
 
61
/* Scheduler includes. */
62
#include "FreeRTOS.h"
63
#include "task.h"
64
 
65
/* Constants required to setup timer 2 to produce the RTOS tick. */
66
#define portCLOCK_DIVISOR                               ( ( unsigned long ) 12 )
67
#define portMAX_TIMER_VALUE                             ( ( unsigned long ) 0xffff )
68
#define portENABLE_TIMER                                ( ( unsigned char ) 0x04 )
69
#define portTIMER_2_INTERRUPT_ENABLE    ( ( unsigned char ) 0x20 )
70
 
71
/* The value used in the IE register when a task first starts. */
72
#define portGLOBAL_INTERRUPT_BIT        ( ( portSTACK_TYPE ) 0x80 )
73
 
74
/* The value used in the PSW register when a task first starts. */
75
#define portINITIAL_PSW                         ( ( portSTACK_TYPE ) 0x00 )
76
 
77
/* Macro to clear the timer 2 interrupt flag. */
78
#define portCLEAR_INTERRUPT_FLAG()      TMR2CN &= ~0x80;
79
 
80
/* Used during a context switch to store the size of the stack being copied
81
to or from XRAM. */
82
data static unsigned char ucStackBytes;
83
 
84
/* Used during a context switch to point to the next byte in XRAM from/to which
85
a RAM byte is to be copied. */
86
xdata static portSTACK_TYPE * data pxXRAMStack;
87
 
88
/* Used during a context switch to point to the next byte in RAM from/to which
89
an XRAM byte is to be copied. */
90
data static portSTACK_TYPE * data pxRAMStack;
91
 
92
/* We require the address of the pxCurrentTCB variable, but don't want to know
93
any details of its type. */
94
typedef void tskTCB;
95
extern volatile tskTCB * volatile pxCurrentTCB;
96
 
97
/*
98
 * Setup the hardware to generate an interrupt off timer 2 at the required
99
 * frequency.
100
 */
101
static void prvSetupTimerInterrupt( void );
102
 
103
/*-----------------------------------------------------------*/
104
/*
105
 * Macro that copies the current stack from internal RAM to XRAM.  This is
106
 * required as the 8051 only contains enough internal RAM for a single stack,
107
 * but we have a stack for every task.
108
 */
109
#define portCOPY_STACK_TO_XRAM()                                                                                                                                \
110
{                                                                                                                                                                                               \
111
        /* pxCurrentTCB points to a TCB which itself points to the location into                                        \
112
        which the first stack byte should be copied.  Set pxXRAMStack to point                                          \
113
        to the location into which the first stack byte is to be copied. */                                                     \
114
        pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB );         \
115
                                                                                                                                                                                                \
116
        /* Set pxRAMStack to point to the first byte to be coped from the stack. */                                     \
117
        pxRAMStack = ( data portSTACK_TYPE * data ) configSTACK_START;                                                          \
118
                                                                                                                                                                                                \
119
        /* Calculate the size of the stack we are about to copy from the current                                        \
120
        stack pointer value. */                                                                                                                                         \
121
        ucStackBytes = SP - ( configSTACK_START - 1 );                                                                                          \
122
                                                                                                                                                                                                \
123
        /* Before starting to copy the stack, store the calculated stack size so                                        \
124
        the stack can be restored when the task is resumed. */                                                                          \
125
        *pxXRAMStack = ucStackBytes;                                                                                                                            \
126
                                                                                                                                                                                                \
127
        /* Copy each stack byte in turn.  pxXRAMStack is incremented first as we                                        \
128
        have already stored the stack size into XRAM. */                                                                                        \
129
        while( ucStackBytes )                                                                                                                                           \
130
        {                                                                                                                                                                                       \
131
                pxXRAMStack++;                                                                                                                                                  \
132
                *pxXRAMStack = *pxRAMStack;                                                                                                                             \
133
                pxRAMStack++;                                                                                                                                                   \
134
                ucStackBytes--;                                                                                                                                                 \
135
        }                                                                                                                                                                                       \
136
}
137
/*-----------------------------------------------------------*/
138
 
139
/*
140
 * Macro that copies the stack of the task being resumed from XRAM into
141
 * internal RAM.
142
 */
143
#define portCOPY_XRAM_TO_STACK()                                                                                                                                \
144
{                                                                                                                                                                                               \
145
        /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to                                         \
146
        copy the data back out of XRAM and into the stack. */                                                                           \
147
        pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB );         \
148
        pxRAMStack = ( data portSTACK_TYPE * data ) ( configSTACK_START - 1 );                                          \
149
                                                                                                                                                                                                \
150
        /* The first value stored in XRAM was the size of the stack - i.e. the                                          \
151
        number of bytes we need to copy back. */                                                                                                        \
152
        ucStackBytes = pxXRAMStack[ 0 ];                                                                                                                 \
153
                                                                                                                                                                                                \
154
        /* Copy the required number of bytes back into the stack. */                                                            \
155
        do                                                                                                                                                                                      \
156
        {                                                                                                                                                                                       \
157
                pxXRAMStack++;                                                                                                                                                  \
158
                pxRAMStack++;                                                                                                                                                   \
159
                *pxRAMStack = *pxXRAMStack;                                                                                                                             \
160
                ucStackBytes--;                                                                                                                                                 \
161
        } while( ucStackBytes );                                                                                                                                        \
162
                                                                                                                                                                                                \
163
        /* Restore the stack pointer ready to use the restored stack. */                                                        \
164
        SP = ( unsigned char ) pxRAMStack;                                                                                                              \
165
}
166
/*-----------------------------------------------------------*/
167
 
168
/*
169
 * Macro to push the current execution context onto the stack, before the stack
170
 * is moved to XRAM.
171
 */
172
#define portSAVE_CONTEXT()                                                                                                                                              \
173
{                                                                                                                                                                                               \
174
        _asm                                                                                                                                                                            \
175
                /* Push ACC first, as when restoring the context it must be restored                                    \
176
                last (it is used to set the IE register). */                                                                                    \
177
                push    ACC                                                                                                                                                             \
178
                /* Store the IE register then disable interrupts. */                                                                    \
179
                push    IE                                                                                                                                                              \
180
                clr             _EA                                                                                                                                                             \
181
                push    DPL                                                                                                                                                             \
182
                push    DPH                                                                                                                                                             \
183
                push    b                                                                                                                                                               \
184
                push    ar2                                                                                                                                                             \
185
                push    ar3                                                                                                                                                             \
186
                push    ar4                                                                                                                                                             \
187
                push    ar5                                                                                                                                                             \
188
                push    ar6                                                                                                                                                             \
189
                push    ar7                                                                                                                                                             \
190
                push    ar0                                                                                                                                                             \
191
                push    ar1                                                                                                                                                             \
192
                push    PSW                                                                                                                                                             \
193
        _endasm;                                                                                                                                                                        \
194
                PSW = 0;                                                                                                                                                         \
195
        _asm                                                                                                                                                                            \
196
                push    _bp                                                                                                                                                             \
197
        _endasm;                                                                                                                                                                        \
198
}
199
/*-----------------------------------------------------------*/
200
 
201
/*
202
 * Macro that restores the execution context from the stack.  The execution
203
 * context was saved into the stack before the stack was copied into XRAM.
204
 */
205
#define portRESTORE_CONTEXT()                                                                                                                                   \
206
{                                                                                                                                                                                               \
207
        _asm                                                                                                                                                                            \
208
                pop             _bp                                                                                                                                                             \
209
                pop             PSW                                                                                                                                                             \
210
                pop             ar1                                                                                                                                                             \
211
                pop             ar0                                                                                                                                                             \
212
                pop             ar7                                                                                                                                                             \
213
                pop             ar6                                                                                                                                                             \
214
                pop             ar5                                                                                                                                                             \
215
                pop             ar4                                                                                                                                                             \
216
                pop             ar3                                                                                                                                                             \
217
                pop             ar2                                                                                                                                                             \
218
                pop             b                                                                                                                                                               \
219
                pop             DPH                                                                                                                                                             \
220
                pop             DPL                                                                                                                                                             \
221
                /* The next byte of the stack is the IE register.  Only the global                                              \
222
                enable bit forms part of the task context.  Pop off the IE then set                                             \
223
                the global enable bit to match that of the stored IE register. */                                               \
224
                pop             ACC                                                                                                                                                             \
225
                JB              ACC.7,0098$                                                                                                                                             \
226
                CLR             IE.7                                                                                                                                                    \
227
                LJMP    0099$                                                                                                                                                   \
228
        0098$:                                                                                                                                                                          \
229
                SETB    IE.7                                                                                                                                                    \
230
        0099$:                                                                                                                                                                          \
231
                /* Finally pop off the ACC, which was the first register saved. */                                              \
232
                pop             ACC                                                                                                                                                             \
233
                reti                                                                                                                                                                    \
234
        _endasm;                                                                                                                                                                        \
235
}
236
/*-----------------------------------------------------------*/
237
 
238
/*
239
 * See header file for description.
240
 */
241
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
242
{
243
unsigned long ulAddress;
244
portSTACK_TYPE *pxStartOfStack;
245
 
246
        /* Leave space to write the size of the stack as the first byte. */
247
        pxStartOfStack = pxTopOfStack;
248
        pxTopOfStack++;
249
 
250
        /* Place a few bytes of known values on the bottom of the stack.
251
        This is just useful for debugging and can be uncommented if required.
252
        *pxTopOfStack = 0x11;
253
        pxTopOfStack++;
254
        *pxTopOfStack = 0x22;
255
        pxTopOfStack++;
256
        *pxTopOfStack = 0x33;
257
        pxTopOfStack++;
258
        */
259
 
260
        /* Simulate how the stack would look after a call to the scheduler tick
261
        ISR.
262
 
263
        The return address that would have been pushed by the MCU. */
264
        ulAddress = ( unsigned long ) pxCode;
265
        *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;
266
        ulAddress >>= 8;
267
        pxTopOfStack++;
268
        *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress );
269
        pxTopOfStack++;
270
 
271
        /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */
272
        *pxTopOfStack = 0xaa;   /* acc */
273
        pxTopOfStack++;
274
 
275
        /* We want tasks to start with interrupts enabled. */
276
        *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;
277
        pxTopOfStack++;
278
 
279
        /* The function parameters will be passed in the DPTR and B register as
280
        a three byte generic pointer is used. */
281
        ulAddress = ( unsigned long ) pvParameters;
282
        *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;   /* DPL */
283
        ulAddress >>= 8;
284
        *pxTopOfStack++;
285
        *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;   /* DPH */
286
        ulAddress >>= 8;
287
        pxTopOfStack++;
288
        *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;   /* b */
289
        pxTopOfStack++;
290
 
291
        /* The remaining registers are straight forward. */
292
        *pxTopOfStack = 0x02;   /* R2 */
293
        pxTopOfStack++;
294
        *pxTopOfStack = 0x03;   /* R3 */
295
        pxTopOfStack++;
296
        *pxTopOfStack = 0x04;   /* R4 */
297
        pxTopOfStack++;
298
        *pxTopOfStack = 0x05;   /* R5 */
299
        pxTopOfStack++;
300
        *pxTopOfStack = 0x06;   /* R6 */
301
        pxTopOfStack++;
302
        *pxTopOfStack = 0x07;   /* R7 */
303
        pxTopOfStack++;
304
        *pxTopOfStack = 0x00;   /* R0 */
305
        pxTopOfStack++;
306
        *pxTopOfStack = 0x01;   /* R1 */
307
        pxTopOfStack++;
308
        *pxTopOfStack = 0x00;   /* PSW */
309
        pxTopOfStack++;
310
        *pxTopOfStack = 0xbb;   /* BP */
311
 
312
        /* Dont increment the stack size here as we don't want to include
313
        the stack size byte as part of the stack size count.
314
 
315
        Finally we place the stack size at the beginning. */
316
        *pxStartOfStack = ( portSTACK_TYPE ) ( pxTopOfStack - pxStartOfStack );
317
 
318
        /* Unlike most ports, we return the start of the stack as this is where the
319
        size of the stack is stored. */
320
        return pxStartOfStack;
321
}
322
/*-----------------------------------------------------------*/
323
 
324
/*
325
 * See header file for description.
326
 */
327
portBASE_TYPE xPortStartScheduler( void )
328
{
329
        /* Setup timer 2 to generate the RTOS tick. */
330
        prvSetupTimerInterrupt();
331
 
332
        /* Make sure we start with the expected SFR page.  This line should not
333
        really be required. */
334
        SFRPAGE = 0;
335
 
336
        /* Copy the stack for the first task to execute from XRAM into the stack,
337
        restore the task context from the new stack, then start running the task. */
338
        portCOPY_XRAM_TO_STACK();
339
        portRESTORE_CONTEXT();
340
 
341
        /* Should never get here! */
342
        return pdTRUE;
343
}
344
/*-----------------------------------------------------------*/
345
 
346
void vPortEndScheduler( void )
347
{
348
        /* Not implemented for this port. */
349
}
350
/*-----------------------------------------------------------*/
351
 
352
/*
353
 * Manual context switch.  The first thing we do is save the registers so we
354
 * can use a naked attribute.
355
 */
356
void vPortYield( void ) _naked
357
{
358
        /* Save the execution context onto the stack, then copy the entire stack
359
        to XRAM.  This is necessary as the internal RAM is only large enough to
360
        hold one stack, and we want one per task.
361
 
362
        PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH
363
        IS REQUIRED. */
364
        portSAVE_CONTEXT();
365
        portCOPY_STACK_TO_XRAM();
366
 
367
        /* Call the standard scheduler context switch function. */
368
        vTaskSwitchContext();
369
 
370
        /* Copy the stack of the task about to execute from XRAM into RAM and
371
        restore it's context ready to run on exiting. */
372
        portCOPY_XRAM_TO_STACK();
373
        portRESTORE_CONTEXT();
374
}
375
/*-----------------------------------------------------------*/
376
 
377
#if configUSE_PREEMPTION == 1
378
        void vTimer2ISR( void ) interrupt 5 _naked
379
        {
380
                /* Preemptive context switch function triggered by the timer 2 ISR.
381
                This does the same as vPortYield() (see above) with the addition
382
                of incrementing the RTOS tick count. */
383
 
384
                portSAVE_CONTEXT();
385
                portCOPY_STACK_TO_XRAM();
386
 
387
                vTaskIncrementTick();
388
                vTaskSwitchContext();
389
 
390
                portCLEAR_INTERRUPT_FLAG();
391
                portCOPY_XRAM_TO_STACK();
392
                portRESTORE_CONTEXT();
393
        }
394
#else
395
        void vTimer2ISR( void ) interrupt 5
396
        {
397
                /* When using the cooperative scheduler the timer 2 ISR is only
398
                required to increment the RTOS tick count. */
399
 
400
                vTaskIncrementTick();
401
                portCLEAR_INTERRUPT_FLAG();
402
        }
403
#endif
404
/*-----------------------------------------------------------*/
405
 
406
static void prvSetupTimerInterrupt( void )
407
{
408
unsigned char ucOriginalSFRPage;
409
 
410
/* Constants calculated to give the required timer capture values. */
411
const unsigned long ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;
412
const unsigned long ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;
413
const unsigned long ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;
414
const unsigned char ucLowCaptureByte = ( unsigned char ) ( ulCaptureValue & ( unsigned long ) 0xff );
415
const unsigned char ucHighCaptureByte = ( unsigned char ) ( ulCaptureValue >> ( unsigned long ) 8 );
416
 
417
        /* NOTE:  This uses a timer only present on 8052 architecture. */
418
 
419
        /* Remember the current SFR page so we can restore it at the end of the
420
        function. */
421
        ucOriginalSFRPage = SFRPAGE;
422
        SFRPAGE = 0;
423
 
424
        /* TMR2CF can be left in its default state. */
425
        TMR2CF = ( unsigned char ) 0;
426
 
427
        /* Setup the overflow reload value. */
428
        RCAP2L = ucLowCaptureByte;
429
        RCAP2H = ucHighCaptureByte;
430
 
431
        /* The initial load is performed manually. */
432
        TMR2L = ucLowCaptureByte;
433
        TMR2H = ucHighCaptureByte;
434
 
435
        /* Enable the timer 2 interrupts. */
436
        IE |= portTIMER_2_INTERRUPT_ENABLE;
437
 
438
        /* Interrupts are disabled when this is called so the timer can be started
439
        here. */
440
        TMR2CN = portENABLE_TIMER;
441
 
442
        /* Restore the original SFR page. */
443
        SFRPAGE = ucOriginalSFRPage;
444
}
445
 
446
 
447
 
448
 

powered by: WebSVN 2.1.0

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