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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 572 jeremybenn
/*This file has been prepared for Doxygen automatic documentation generation.*/
2
/*! \file *********************************************************************
3
 *
4
 * \brief FreeRTOS port source for AVR32 UC3.
5
 *
6
 * - Compiler:           IAR EWAVR32
7
 * - Supported devices:  All AVR32 devices can be used.
8
 * - AppNote:
9
 *
10
 * \author               Atmel Corporation: http://www.atmel.com \n
11
 *                       Support and FAQ: http://support.atmel.no/
12
 *
13
 *****************************************************************************/
14
 
15
/*
16
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
17
 
18
    ***************************************************************************
19
    *                                                                         *
20
    * If you are:                                                             *
21
    *                                                                         *
22
    *    + New to FreeRTOS,                                                   *
23
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
24
    *    + Looking for basic training,                                        *
25
    *    + Wanting to improve your FreeRTOS skills and productivity           *
26
    *                                                                         *
27
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
28
    *                                                                         *
29
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
30
    *                  http://www.FreeRTOS.org/Documentation                  *
31
    *                                                                         *
32
    * A pdf reference manual is also available.  Both are usually delivered   *
33
    * to your inbox within 20 minutes to two hours when purchased between 8am *
34
    * and 8pm GMT (although please allow up to 24 hours in case of            *
35
    * exceptional circumstances).  Thank you for your support!                *
36
    *                                                                         *
37
    ***************************************************************************
38
 
39
    This file is part of the FreeRTOS distribution.
40
 
41
    FreeRTOS is free software; you can redistribute it and/or modify it under
42
    the terms of the GNU General Public License (version 2) as published by the
43
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
44
    ***NOTE*** The exception to the GPL is included to allow you to distribute
45
    a combined work that includes FreeRTOS without being obliged to provide the
46
    source code for proprietary components outside of the FreeRTOS kernel.
47
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
48
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
50
    more details. You should have received a copy of the GNU General Public
51
    License and the FreeRTOS license exception along with FreeRTOS; if not it
52
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
53
    by writing to Richard Barry, contact details for whom are available on the
54
    FreeRTOS WEB site.
55
 
56
    1 tab == 4 spaces!
57
 
58
    http://www.FreeRTOS.org - Documentation, latest information, license and
59
    contact details.
60
 
61
    http://www.SafeRTOS.com - A version that is certified for use in safety
62
    critical systems.
63
 
64
    http://www.OpenRTOS.com - Commercial support, development, porting,
65
    licensing and training services.
66
*/
67
 
68
 
69
/* Scheduler includes. */
70
#include "FreeRTOS.h"
71
#include "task.h"
72
 
73
/* AVR32 UC3 includes. */
74
#include <avr32/io.h>
75
#include <intrinsics.h>
76
#include "gpio.h"
77
 
78
#if configDBG
79
        #include "usart.h"
80
#endif
81
 
82
#if( configTICK_USE_TC==1 )
83
        #include "tc.h"
84
#endif
85
 
86
 
87
/* Constants required to setup the task context. */
88
#define portINITIAL_SR            ( ( portSTACK_TYPE ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */
89
#define portINSTRUCTION_SIZE      ( ( portSTACK_TYPE ) 0 )
90
 
91
/* Each task maintains its own critical nesting variable. */
92
#define portNO_CRITICAL_NESTING   ( ( unsigned long ) 0 )
93
volatile unsigned long ulCriticalNesting = 9999UL;
94
 
95
#if( configTICK_USE_TC==0 )
96
        static void prvScheduleNextTick( void );
97
#else
98
        static void prvClearTcInt( void );
99
#endif
100
 
101
/* Setup the timer to generate the tick interrupts. */
102
static void prvSetupTimerInterrupt( void );
103
 
104
/*-----------------------------------------------------------*/
105
 
106
/*
107
 * Low-level initialization routine called during startup, before the main
108
 * function.
109
 */
110
int __low_level_init(void)
111
{
112
        #if configHEAP_INIT
113
                #pragma segment = "HEAP"
114
                portBASE_TYPE *pxMem;
115
        #endif
116
 
117
        /* Enable exceptions. */
118
        ENABLE_ALL_EXCEPTIONS();
119
 
120
        /* Initialize interrupt handling. */
121
        INTC_init_interrupts();
122
 
123
        #if configHEAP_INIT
124
        {
125
                /* Initialize the heap used by malloc. */
126
                for( pxMem = __segment_begin( "HEAP" ); pxMem < ( portBASE_TYPE * ) __segment_end( "HEAP" ); )
127
                {
128
                        *pxMem++ = 0xA5A5A5A5;
129
                }
130
        }
131
        #endif
132
 
133
        /* Code section present if and only if the debug trace is activated. */
134
        #if configDBG
135
        {
136
                static const gpio_map_t DBG_USART_GPIO_MAP =
137
                {
138
                        { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },
139
                        { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }
140
                };
141
 
142
                static const usart_options_t DBG_USART_OPTIONS =
143
                {
144
                        .baudrate = configDBG_USART_BAUDRATE,
145
                        .charlength = 8,
146
                        .paritytype = USART_NO_PARITY,
147
                        .stopbits = USART_1_STOPBIT,
148
                        .channelmode = USART_NORMAL_CHMODE
149
                };
150
 
151
                /* Initialize the USART used for the debug trace with the configured parameters. */
152
                extern volatile avr32_usart_t *volatile stdio_usart_base;
153
                stdio_usart_base = configDBG_USART;
154
                gpio_enable_module( DBG_USART_GPIO_MAP,
155
                                    sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );
156
                usart_init_rs232(configDBG_USART, &DBG_USART_OPTIONS, configCPU_CLOCK_HZ);
157
        }
158
        #endif
159
 
160
        /* Request initialization of data segments. */
161
        return 1;
162
}
163
/*-----------------------------------------------------------*/
164
 
165
/* Added as there is no such function in FreeRTOS. */
166
void *pvPortRealloc( void *pv, size_t xWantedSize )
167
{
168
void *pvReturn;
169
 
170
        vTaskSuspendAll();
171
        {
172
                pvReturn = realloc( pv, xWantedSize );
173
        }
174
        xTaskResumeAll();
175
 
176
        return pvReturn;
177
}
178
/*-----------------------------------------------------------*/
179
 
180
/* The cooperative scheduler requires a normal IRQ service routine to
181
simply increment the system tick. */
182
/* The preemptive scheduler is defined as "naked" as the full context is saved
183
on entry as part of the context switch. */
184
#pragma shadow_registers = full   // Naked.
185
static void vTick( void )
186
{
187
        /* Save the context of the interrupted task. */
188
        portSAVE_CONTEXT_OS_INT();
189
 
190
        #if( configTICK_USE_TC==1 )
191
                /* Clear the interrupt flag. */
192
                prvClearTcInt();
193
        #else
194
                /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
195
                clock cycles from now. */
196
                prvScheduleNextTick();
197
        #endif
198
 
199
        /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
200
        calls in a critical section . */
201
        portENTER_CRITICAL();
202
                vTaskIncrementTick();
203
        portEXIT_CRITICAL();
204
 
205
        /* Restore the context of the "elected task". */
206
        portRESTORE_CONTEXT_OS_INT();
207
}
208
/*-----------------------------------------------------------*/
209
 
210
#pragma shadow_registers = full   // Naked.
211
void SCALLYield( void )
212
{
213
        /* Save the context of the interrupted task. */
214
        portSAVE_CONTEXT_SCALL();
215
        vTaskSwitchContext();
216
        portRESTORE_CONTEXT_SCALL();
217
}
218
/*-----------------------------------------------------------*/
219
 
220
/* The code generated by the GCC compiler uses the stack in different ways at
221
different optimisation levels.  The interrupt flags can therefore not always
222
be saved to the stack.  Instead the critical section nesting level is stored
223
in a variable, which is then saved as part of the stack context. */
224
#pragma optimize = no_inline
225
void vPortEnterCritical( void )
226
{
227
        /* Disable interrupts */
228
        portDISABLE_INTERRUPTS();
229
 
230
        /* Now interrupts are disabled ulCriticalNesting can be accessed
231
         directly.  Increment ulCriticalNesting to keep a count of how many times
232
         portENTER_CRITICAL() has been called. */
233
        ulCriticalNesting++;
234
}
235
/*-----------------------------------------------------------*/
236
 
237
#pragma optimize = no_inline
238
void vPortExitCritical( void )
239
{
240
        if(ulCriticalNesting > portNO_CRITICAL_NESTING)
241
        {
242
                ulCriticalNesting--;
243
                if( ulCriticalNesting == portNO_CRITICAL_NESTING )
244
                {
245
                        /* Enable all interrupt/exception. */
246
                        portENABLE_INTERRUPTS();
247
                }
248
        }
249
}
250
/*-----------------------------------------------------------*/
251
 
252
/*
253
 * Initialise the stack of a task to look exactly as if a call to
254
 * portSAVE_CONTEXT had been called.
255
 *
256
 * See header file for description.
257
 */
258
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
259
{
260
        /* Setup the initial stack of the task.  The stack is set exactly as
261
        expected by the portRESTORE_CONTEXT() macro. */
262
 
263
        /* When the task starts, it will expect to find the function parameter in R12. */
264
        pxTopOfStack--;
265
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x08080808;                                        /* R8 */
266
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x09090909;                                        /* R9 */
267
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0A0A0A0A;                                        /* R10 */
268
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0B0B0B0B;                                        /* R11 */
269
        *pxTopOfStack-- = ( portSTACK_TYPE ) pvParameters;                                      /* R12 */
270
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDEADBEEF;                                        /* R14/LR */
271
        *pxTopOfStack-- = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */
272
        *pxTopOfStack-- = ( portSTACK_TYPE ) portINITIAL_SR;                            /* SR */
273
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF0000FF;                                        /* R0 */
274
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x01010101;                                        /* R1 */
275
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x02020202;                                        /* R2 */
276
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x03030303;                                        /* R3 */
277
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x04040404;                                        /* R4 */
278
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x05050505;                                        /* R5 */
279
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x06060606;                                        /* R6 */
280
        *pxTopOfStack-- = ( portSTACK_TYPE ) 0x07070707;                                        /* R7 */
281
        *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_NESTING;                     /* ulCriticalNesting */
282
 
283
        return pxTopOfStack;
284
}
285
/*-----------------------------------------------------------*/
286
 
287
portBASE_TYPE xPortStartScheduler( void )
288
{
289
        /* Start the timer that generates the tick ISR.  Interrupts are disabled
290
        here already. */
291
        prvSetupTimerInterrupt();
292
 
293
        /* Start the first task. */
294
        portRESTORE_CONTEXT();
295
 
296
        /* Should not get here! */
297
        return 0;
298
}
299
/*-----------------------------------------------------------*/
300
 
301
void vPortEndScheduler( void )
302
{
303
        /* It is unlikely that the AVR32 port will require this function as there
304
        is nothing to return to.  */
305
}
306
/*-----------------------------------------------------------*/
307
 
308
/* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
309
clock cycles from now. */
310
#if( configTICK_USE_TC==0 )
311
        static void prvScheduleFirstTick(void)
312
        {
313
                unsigned long lCycles;
314
 
315
                lCycles = Get_system_register(AVR32_COUNT);
316
                lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
317
                // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception
318
                // generation feature does not get disabled.
319
                if(0 == lCycles)
320
                {
321
                        lCycles++;
322
                }
323
                Set_system_register(AVR32_COMPARE, lCycles);
324
        }
325
 
326
        #pragma optimize = no_inline
327
        static void prvScheduleNextTick(void)
328
        {
329
                unsigned long lCycles, lCount;
330
 
331
                lCycles = Get_system_register(AVR32_COMPARE);
332
                lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
333
                // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception
334
                // generation feature does not get disabled.
335
                if(0 == lCycles)
336
                {
337
                        lCycles++;
338
                }
339
                lCount = Get_system_register(AVR32_COUNT);
340
                if( lCycles < lCount )
341
                {               // We missed a tick, recover for the next.
342
                        lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
343
                }
344
                Set_system_register(AVR32_COMPARE, lCycles);
345
        }
346
#else
347
        #pragma optimize = no_inline
348
        static void prvClearTcInt(void)
349
        {
350
                AVR32_TC.channel[configTICK_TC_CHANNEL].sr;
351
        }
352
#endif
353
/*-----------------------------------------------------------*/
354
 
355
/* Setup the timer to generate the tick interrupts. */
356
static void prvSetupTimerInterrupt(void)
357
{
358
        #if( configTICK_USE_TC==1 )
359
 
360
                volatile avr32_tc_t *tc = &AVR32_TC;
361
 
362
                // Options for waveform genration.
363
                tc_waveform_opt_t waveform_opt =
364
                {
365
                .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */
366
 
367
                .bswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOB. */
368
                .beevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOB. */
369
                .bcpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOB. */
370
                .bcpb     = TC_EVT_EFFECT_NOOP,                /* RB compare effect on TIOB. */
371
 
372
                .aswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOA. */
373
                .aeevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOA. */
374
                .acpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOA: toggle. */
375
                .acpa     = TC_EVT_EFFECT_NOOP,                /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */
376
 
377
                .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */
378
                .enetrg   = FALSE,                             /* External event trigger enable. */
379
                .eevt     = 0,                                 /* External event selection. */
380
                .eevtedg  = TC_SEL_NO_EDGE,                    /* External event edge selection. */
381
                .cpcdis   = FALSE,                             /* Counter disable when RC compare. */
382
                .cpcstop  = FALSE,                             /* Counter clock stopped with RC compare. */
383
 
384
                .burst    = FALSE,                             /* Burst signal selection. */
385
                .clki     = FALSE,                             /* Clock inversion. */
386
                .tcclks   = TC_CLOCK_SOURCE_TC2                /* Internal source clock 2. */
387
                };
388
 
389
                tc_interrupt_t tc_interrupt =
390
                {
391
                        .etrgs=0,
392
                        .ldrbs=0,
393
                        .ldras=0,
394
                        .cpcs =1,
395
                        .cpbs =0,
396
                        .cpas =0,
397
                        .lovrs=0,
398
                        .covfs=0,
399
                };
400
 
401
        #endif
402
 
403
        /* Disable all interrupt/exception. */
404
        portDISABLE_INTERRUPTS();
405
 
406
        /* Register the compare interrupt handler to the interrupt controller and
407
        enable the compare interrupt. */
408
 
409
        #if( configTICK_USE_TC==1 )
410
        {
411
                INTC_register_interrupt((__int_handler)&vTick, configTICK_TC_IRQ, INT0);
412
 
413
                /* Initialize the timer/counter. */
414
                tc_init_waveform(tc, &waveform_opt);
415
 
416
                /* Set the compare triggers.
417
                Remember TC counter is 16-bits, so counting second is not possible!
418
                That's why we configure it to count ms. */
419
                tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );
420
 
421
                tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );
422
 
423
                /* Start the timer/counter. */
424
                tc_start(tc, configTICK_TC_CHANNEL);
425
        }
426
        #else
427
        {
428
                INTC_register_interrupt((__int_handler)&vTick, AVR32_CORE_COMPARE_IRQ, INT0);
429
                prvScheduleFirstTick();
430
        }
431
        #endif
432
}

powered by: WebSVN 2.1.0

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