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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Source/] [portable/] [IAR/] [STR91x/] [port.c] - Blame information for rev 572

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 ST STR91x ARM9
56
 * port.
57
 *----------------------------------------------------------*/
58
 
59
/* Library includes. */
60
#include "91x_lib.h"
61
 
62
/* Standard includes. */
63
#include <stdlib.h>
64
#include <assert.h>
65
 
66
/* Scheduler includes. */
67
#include "FreeRTOS.h"
68
#include "task.h"
69
 
70
#ifndef configUSE_WATCHDOG_TICK
71
        #error configUSE_WATCHDOG_TICK must be set to either 1 or 0 in FreeRTOSConfig.h to use either the Watchdog or timer 2 to generate the tick interrupt respectively.
72
#endif
73
 
74
/* Constants required to setup the initial stack. */
75
#ifndef _RUN_TASK_IN_ARM_MODE_
76
        #define portINITIAL_SPSR                        ( ( portSTACK_TYPE ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
77
#else
78
        #define portINITIAL_SPSR                        ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
79
#endif
80
 
81
#define portINSTRUCTION_SIZE                    ( ( portSTACK_TYPE ) 4 )
82
 
83
/* Constants required to handle critical sections. */
84
#define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )
85
 
86
#ifndef abs
87
        #define abs(x) ((x)>0 ? (x) : -(x))
88
#endif
89
 
90
/**
91
 * Toggle a led using the following algorithm:
92
 * if ( GPIO_ReadBit(GPIO9, GPIO_Pin_2) )
93
 * {
94
 *   GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );
95
 * }
96
 * else
97
 * {
98
 *   GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );
99
 * }
100
 *
101
 */
102
#define TOGGLE_LED(port,pin)                                                                    \
103
        if ( ((((port)->DR[(pin)<<2])) & (pin)) != Bit_RESET )          \
104
        {                                                                                                                       \
105
        (port)->DR[(pin) <<2] = 0x00;                                                   \
106
        }                                                                                                                       \
107
        else                                                                                                            \
108
        {                                                                                                                       \
109
        (port)->DR[(pin) <<2] = (pin);                                                  \
110
        }
111
 
112
 
113
/*-----------------------------------------------------------*/
114
 
115
/* Setup the watchdog to generate the tick interrupts. */
116
static void prvSetupTimerInterrupt( void );
117
 
118
/* ulCriticalNesting will get set to zero when the first task starts.  It
119
cannot be initialised to 0 as this will cause interrupts to be enabled
120
during the kernel initialisation process. */
121
unsigned long ulCriticalNesting = ( unsigned long ) 9999;
122
 
123
/* Tick interrupt routines for cooperative and preemptive operation
124
respectively.  The preemptive version is not defined as __irq as it is called
125
from an asm wrapper function. */
126
void WDG_IRQHandler( void );
127
 
128
/* VIC interrupt default handler. */
129
static void prvDefaultHandler( void );
130
 
131
#if configUSE_WATCHDOG_TICK == 0
132
        /* Used to update the OCR timer register */
133
        static u16 s_nPulseLength;
134
#endif
135
 
136
/*-----------------------------------------------------------*/
137
 
138
/*
139
 * Initialise the stack of a task to look exactly as if a call to
140
 * portSAVE_CONTEXT had been called.
141
 *
142
 * See header file for description.
143
 */
144
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
145
{
146
        portSTACK_TYPE *pxOriginalTOS;
147
 
148
        pxOriginalTOS = pxTopOfStack;
149
 
150
        /* Setup the initial stack of the task.  The stack is set exactly as
151
        expected by the portRESTORE_CONTEXT() macro. */
152
 
153
        /* First on the stack is the return address - which in this case is the
154
        start of the task.  The offset is added to make the return address appear
155
        as it would within an IRQ ISR. */
156
        *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
157
        pxTopOfStack--;
158
 
159
        *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa;  /* R14 */
160
        pxTopOfStack--;
161
        *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
162
        pxTopOfStack--;
163
        *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */
164
        pxTopOfStack--;
165
        *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */
166
        pxTopOfStack--;
167
        *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */
168
        pxTopOfStack--;
169
        *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */
170
        pxTopOfStack--;
171
        *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */
172
        pxTopOfStack--;
173
        *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */
174
        pxTopOfStack--;
175
        *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */
176
        pxTopOfStack--;
177
        *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */
178
        pxTopOfStack--;
179
        *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */
180
        pxTopOfStack--;
181
        *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */
182
        pxTopOfStack--;
183
        *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */
184
        pxTopOfStack--;
185
        *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */
186
        pxTopOfStack--;
187
 
188
        /* When the task starts is will expect to find the function parameter in
189
        R0. */
190
        *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
191
        pxTopOfStack--;
192
 
193
        /* The status register is set for system mode, with interrupts enabled. */
194
        *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
195
        pxTopOfStack--;
196
 
197
        /* Interrupt flags cannot always be stored on the stack and will
198
        instead be stored in a variable, which is then saved as part of the
199
        tasks context. */
200
        *pxTopOfStack = portNO_CRITICAL_NESTING;
201
 
202
        return pxTopOfStack;
203
}
204
/*-----------------------------------------------------------*/
205
 
206
portBASE_TYPE xPortStartScheduler( void )
207
{
208
extern void vPortStartFirstTask( void );
209
 
210
        /* Start the timer that generates the tick ISR.  Interrupts are disabled
211
        here already. */
212
        prvSetupTimerInterrupt();
213
 
214
        /* Start the first task. */
215
        vPortStartFirstTask();
216
 
217
        /* Should not get here! */
218
        return 0;
219
}
220
/*-----------------------------------------------------------*/
221
 
222
void vPortEndScheduler( void )
223
{
224
        /* It is unlikely that the ARM port will require this function as there
225
        is nothing to return to.  */
226
}
227
/*-----------------------------------------------------------*/
228
 
229
/* This function is called from an asm wrapper, so does not require the __irq
230
keyword. */
231
#if configUSE_WATCHDOG_TICK == 1
232
 
233
        static void prvFindFactors(u32 n, u16 *a, u32 *b)
234
        {
235
                /* This function is copied from the ST STR7 library and is
236
                copyright STMicroelectronics.  Reproduced with permission. */
237
 
238
                u32 b0;
239
                u16 a0;
240
                long err, err_min=n;
241
 
242
                *a = a0 = ((n-1)/65536ul) + 1;
243
                *b = b0 = n / *a;
244
 
245
                for (; *a <= 256; (*a)++)
246
                {
247
                        *b = n / *a;
248
                        err = (long)*a * (long)*b - (long)n;
249
                        if (abs(err) > (*a / 2))
250
                        {
251
                                (*b)++;
252
                                err = (long)*a * (long)*b - (long)n;
253
                        }
254
                        if (abs(err) < abs(err_min))
255
                        {
256
                                err_min = err;
257
                                a0 = *a;
258
                                b0 = *b;
259
                                if (err == 0) break;
260
                        }
261
                }
262
 
263
                *a = a0;
264
                *b = b0;
265
        }
266
        /*-----------------------------------------------------------*/
267
 
268
        static void prvSetupTimerInterrupt( void )
269
        {
270
        WDG_InitTypeDef xWdg;
271
        unsigned short a;
272
        unsigned long n = configCPU_PERIPH_HZ / configTICK_RATE_HZ, b;
273
 
274
                /* Configure the watchdog as a free running timer that generates a
275
                periodic interrupt. */
276
 
277
                SCU_APBPeriphClockConfig( __WDG, ENABLE );
278
                WDG_DeInit();
279
                WDG_StructInit(&xWdg);
280
                prvFindFactors( n, &a, &b );
281
                xWdg.WDG_Prescaler = a - 1;
282
                xWdg.WDG_Preload = b - 1;
283
                WDG_Init( &xWdg );
284
                WDG_ITConfig(ENABLE);
285
 
286
                /* Configure the VIC for the WDG interrupt. */
287
                VIC_Config( WDG_ITLine, VIC_IRQ, 10 );
288
                VIC_ITCmd( WDG_ITLine, ENABLE );
289
 
290
                /* Install the default handlers for both VIC's. */
291
                VIC0->DVAR = ( unsigned long ) prvDefaultHandler;
292
                VIC1->DVAR = ( unsigned long ) prvDefaultHandler;
293
 
294
                WDG_Cmd(ENABLE);
295
        }
296
        /*-----------------------------------------------------------*/
297
 
298
        void WDG_IRQHandler( void )
299
        {
300
                {
301
                        /* Increment the tick counter. */
302
                        vTaskIncrementTick();
303
 
304
                        #if configUSE_PREEMPTION == 1
305
                        {
306
                                /* The new tick value might unblock a task.  Ensure the highest task that
307
                                is ready to execute is the task that will execute when the tick ISR
308
                                exits. */
309
                                vTaskSwitchContext();
310
                        }
311
                        #endif /* configUSE_PREEMPTION. */
312
 
313
                        /* Clear the interrupt in the watchdog. */
314
                        WDG->SR &= ~0x0001;
315
                }
316
        }
317
 
318
#else
319
 
320
        static void prvFindFactors(u32 n, u8 *a, u16 *b)
321
        {
322
                /* This function is copied from the ST STR7 library and is
323
                copyright STMicroelectronics.  Reproduced with permission. */
324
 
325
                u16 b0;
326
                u8 a0;
327
                long err, err_min=n;
328
 
329
 
330
                *a = a0 = ((n-1)/256) + 1;
331
                *b = b0 = n / *a;
332
 
333
                for (; *a <= 256; (*a)++)
334
                {
335
                        *b = n / *a;
336
                        err = (long)*a * (long)*b - (long)n;
337
                        if (abs(err) > (*a / 2))
338
                        {
339
                                (*b)++;
340
                                err = (long)*a * (long)*b - (long)n;
341
                        }
342
                        if (abs(err) < abs(err_min))
343
                        {
344
                                err_min = err;
345
                                a0 = *a;
346
                                b0 = *b;
347
                                if (err == 0) break;
348
                        }
349
                }
350
 
351
                *a = a0;
352
                *b = b0;
353
        }
354
        /*-----------------------------------------------------------*/
355
 
356
        static void prvSetupTimerInterrupt( void )
357
        {
358
                unsigned char a;
359
                unsigned short b;
360
                unsigned long n = configCPU_PERIPH_HZ / configTICK_RATE_HZ;
361
 
362
                TIM_InitTypeDef timer;
363
 
364
                SCU_APBPeriphClockConfig( __TIM23, ENABLE );
365
                TIM_DeInit(TIM2);
366
                TIM_StructInit(&timer);
367
                prvFindFactors( n, &a, &b );
368
 
369
                timer.TIM_Mode           = TIM_OCM_CHANNEL_1;
370
                timer.TIM_OC1_Modes      = TIM_TIMING;
371
                timer.TIM_Clock_Source   = TIM_CLK_APB;
372
                timer.TIM_Clock_Edge     = TIM_CLK_EDGE_RISING;
373
                timer.TIM_Prescaler      = a-1;
374
                timer.TIM_Pulse_Level_1  = TIM_HIGH;
375
                timer.TIM_Pulse_Length_1 = s_nPulseLength  = b-1;
376
 
377
                TIM_Init (TIM2, &timer);
378
                TIM_ITConfig(TIM2, TIM_IT_OC1, ENABLE);
379
                /* Configure the VIC for the WDG interrupt. */
380
                VIC_Config( TIM2_ITLine, VIC_IRQ, 10 );
381
                VIC_ITCmd( TIM2_ITLine, ENABLE );
382
 
383
                /* Install the default handlers for both VIC's. */
384
                VIC0->DVAR = ( unsigned long ) prvDefaultHandler;
385
                VIC1->DVAR = ( unsigned long ) prvDefaultHandler;
386
 
387
                TIM_CounterCmd(TIM2, TIM_CLEAR);
388
                TIM_CounterCmd(TIM2, TIM_START);
389
        }
390
        /*-----------------------------------------------------------*/
391
 
392
        void TIM2_IRQHandler( void )
393
        {
394
                /* Reset the timer counter to avioid overflow. */
395
                TIM2->OC1R += s_nPulseLength;
396
 
397
                /* Increment the tick counter. */
398
                vTaskIncrementTick();
399
 
400
                #if configUSE_PREEMPTION == 1
401
                {
402
                        /* The new tick value might unblock a task.  Ensure the highest task that
403
                        is ready to execute is the task that will execute when the tick ISR
404
                        exits. */
405
                        vTaskSwitchContext();
406
                }
407
                #endif
408
 
409
                /* Clear the interrupt in the watchdog. */
410
                TIM2->SR &= ~TIM_FLAG_OC1;
411
        }
412
 
413
#endif /* USE_WATCHDOG_TICK */
414
 
415
/*-----------------------------------------------------------*/
416
 
417
__arm __interwork void vPortEnterCritical( void )
418
{
419
        /* Disable interrupts first! */
420
        portDISABLE_INTERRUPTS();
421
 
422
        /* Now interrupts are disabled ulCriticalNesting can be accessed
423
        directly.  Increment ulCriticalNesting to keep a count of how many times
424
        portENTER_CRITICAL() has been called. */
425
        ulCriticalNesting++;
426
}
427
/*-----------------------------------------------------------*/
428
 
429
__arm __interwork void vPortExitCritical( void )
430
{
431
        if( ulCriticalNesting > portNO_CRITICAL_NESTING )
432
        {
433
                /* Decrement the nesting count as we are leaving a critical section. */
434
                ulCriticalNesting--;
435
 
436
                /* If the nesting level has reached zero then interrupts should be
437
                re-enabled. */
438
                if( ulCriticalNesting == portNO_CRITICAL_NESTING )
439
                {
440
                        portENABLE_INTERRUPTS();
441
                }
442
        }
443
}
444
/*-----------------------------------------------------------*/
445
 
446
static void prvDefaultHandler( void )
447
{
448
}
449
 
450
 
451
 
452
 
453
 

powered by: WebSVN 2.1.0

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