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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [cortexm/] [lm3s/] [lm3s8xx/] [current/] [tests/] [timers.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=============================================================================
2
//
3
//      timers.c
4
//
5
//      Test for Stellaris Cortex-M3 Device Timers
6
//
7
//=============================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 2008, 2011 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later
16
// version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
//
27
// As a special exception, if other files instantiate templates or use
28
// macros or inline functions from this file, or you compile this file
29
// and link it with other works to produce a work based on this file,
30
// this file does not by itself cause the resulting work to be covered by
31
// the GNU General Public License. However the source code for this file
32
// must still be made available in accordance with section (3) of the GNU
33
// General Public License v2.
34
//
35
// This exception does not invalidate any other reasons why a work based
36
// on this file might be covered by the GNU General Public License.
37
// -------------------------------------------
38
// ####ECOSGPLCOPYRIGHTEND####
39
//=============================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    nickg for STM32
43
//               ccoutand updated for Stellaris Cortex-M3 Devices
44
// Date:         2011-01-18
45
//
46
//####DESCRIPTIONEND####
47
//
48
//=============================================================================
49
 
50
#include <pkgconf/system.h>
51
#include <pkgconf/hal.h>
52
 
53
#if defined(CYGPKG_KERNEL)
54
#include <pkgconf/kernel.h>
55
#endif
56
 
57
#include <cyg/infra/testcase.h>
58
 
59
//=============================================================================
60
// Check all required packages and components are present
61
 
62
#if !defined(CYGPKG_KERNEL) || !defined(CYGPKG_KERNEL_API)
63
# define NA_MSG  "Configuration insufficient"
64
#endif
65
 
66
//=============================================================================
67
// If everything is present, compile the full test.
68
 
69
#ifndef NA_MSG
70
 
71
#include <cyg/hal/hal_arch.h>
72
#include <cyg/hal/hal_io.h>
73
#include <cyg/hal/hal_if.h>
74
 
75
#include <cyg/kernel/kapi.h>
76
#include <cyg/infra/diag.h>
77
#include <string.h>
78
 
79
//=============================================================================
80
 
81
#define LOOPS      24                  // 2 minutes
82
#define SINGLE_TIMER 0                 // Full / Single timer test
83
 
84
static int      test_stack[( CYGNUM_HAL_STACK_SIZE_MINIMUM / sizeof( int ) )];
85
static cyg_thread test_thread;
86
static cyg_handle_t main_thread;
87
 
88
//=============================================================================
89
 
90
struct timer {
91
    cyg_uint32      timer;
92
    cyg_uint32      base;
93
    cyg_uint32      periph;
94
    cyg_uint32      vector;
95
    cyg_uint32      priority;
96
    cyg_uint32      interval;
97
 
98
    cyg_uint32      ticks;
99
 
100
    cyg_uint32      preempt[10];
101
 
102
    cyg_uint32      preempt_dsr[10];
103
    cyg_uint32      dsr_count[10];
104
 
105
    cyg_interrupt   interrupt_object;
106
    cyg_handle_t    interrupt_handle;
107
};
108
 
109
struct timer    timers[] = {
110
#if SINGLE_TIMER
111
    {1, CYGHWR_HAL_LM3S_GPTIM0, CYGHWR_HAL_LM3S_P_TIMER0,
112
     CYGNUM_HAL_INTERRUPT_GTIM0_A, 0x20, 1000},
113
#else
114
    {1, CYGHWR_HAL_LM3S_GPTIM0, CYGHWR_HAL_LM3S_P_TIMER0,
115
     CYGNUM_HAL_INTERRUPT_GTIM0_A, 0x20, 127},
116
    {2, CYGHWR_HAL_LM3S_GPTIM1, CYGHWR_HAL_LM3S_P_TIMER1,
117
     CYGNUM_HAL_INTERRUPT_GTIM1_A, 0x60, 355},
118
    {3, CYGHWR_HAL_LM3S_GPTIM2, CYGHWR_HAL_LM3S_P_TIMER2,
119
     CYGNUM_HAL_INTERRUPT_GTIM2_A, 0x80, 731},
120
#endif
121
    {0, 0, 0, 0}
122
};
123
 
124
 
125
//=============================================================================
126
 
127
volatile cyg_uint32 ticks = 0;
128
volatile cyg_uint32 nesting = 0;
129
volatile cyg_uint32 max_nesting = 0;
130
volatile cyg_uint32 max_nesting_seen = 0;
131
volatile cyg_uint32 current = 0;
132
volatile cyg_uint32 in_dsr = 0;
133
 
134
 
135
//=============================================================================
136
 
137
void
138
init_timer( cyg_uint32 base, cyg_uint32 periph, cyg_uint32 interval )
139
{
140
    cyg_uint32      period = CYGHWR_HAL_CORTEXM_LM3S8XX_SYSCLK;
141
 
142
    CYGHWR_HAL_LM3S_PERIPH_SET( periph, 1 );
143
 
144
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_CTL, 0x0000 );
145
 
146
    period = period / 1000000;
147
 
148
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_TAPR, period );
149
 
150
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_CFG,
151
                      CYGHWR_HAL_LM3S_GPTIM_CFG_16BIT );
152
 
153
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_TAMR,
154
                      CYGHWR_HAL_LM3S_GPTIM_TAMR_PERIODIC );
155
 
156
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_TAILR, interval );
157
 
158
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_ICR,
159
                      CYGHWR_HAL_LM3S_GPTIM_ICR_TATOCINT );
160
 
161
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_IMR,
162
                      CYGHWR_HAL_LM3S_GPTIM_IMR_TATOIM );
163
 
164
    HAL_WRITE_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_CTL,
165
                      CYGHWR_HAL_LM3S_GPTIM_CTL_TAEN );
166
}
167
 
168
 
169
//=============================================================================
170
 
171
cyg_uint32
172
timer_isr( cyg_uint32 vector, CYG_ADDRWORD data )
173
{
174
    struct timer   *t = ( struct timer * )data;
175
    cyg_uint32      preempt = current;
176
    CYG_ADDRWORD    base = t->base;
177
    cyg_uint32      cnt;
178
 
179
    current = t->timer;
180
    t->ticks++;
181
    ticks++;
182
    t->preempt[preempt]++;
183
    nesting++;
184
 
185
    // Count only first ISR to preempt a DSR
186
    if( preempt == 0 )
187
        t->preempt_dsr[in_dsr]++;
188
 
189
    HAL_WRITE_UINT32( t->base + CYGHWR_HAL_LM3S_GPTIM_ICR,
190
                      CYGHWR_HAL_LM3S_GPTIM_ICR_TATOCINT );
191
 
192
    if( nesting > max_nesting )
193
        max_nesting = nesting;
194
 
195
    // Loiter here for a proportion of the timer interval to give
196
    // other timers the chance to preempt us.
197
    do {
198
        HAL_READ_UINT32( base + CYGHWR_HAL_LM3S_GPTIM_TAR, cnt );
199
    } while( cnt < t->interval / 10 );
200
 
201
    nesting--;
202
    current = preempt;
203
 
204
    if( ( t->ticks % 10 ) == 0 )
205
        return 3;
206
    else
207
        return 1;
208
}
209
 
210
 
211
//=============================================================================
212
 
213
void
214
timer_dsr( cyg_uint32 vector, cyg_uint32 count, CYG_ADDRWORD data )
215
{
216
    struct timer   *t = ( struct timer * )data;
217
    int             i;
218
 
219
    in_dsr = t->timer;
220
 
221
    if( count >= 8 )
222
        count = 8;
223
 
224
    t->dsr_count[count]++;
225
 
226
    // Loiter for a while
227
    for( i = 0; i < t->interval / 10; i++ )
228
        continue;
229
 
230
    in_dsr = 0;
231
}
232
 
233
 
234
//=============================================================================
235
 
236
void
237
timers_test( cyg_addrword_t data )
238
{
239
    int             loops = LOOPS;
240
    int             i;
241
    CYG_INTERRUPT_STATE istate;
242
 
243
    CYG_TEST_INIT(  );
244
 
245
    CYG_TEST_INFO( "Start Timers test" );
246
 
247
    for( i = 0; timers[i].timer != 0; i++ ) {
248
        struct timer   *t = &timers[i];
249
 
250
        init_timer( t->base, t->periph, t->interval );
251
 
252
        cyg_interrupt_create( t->vector,
253
                              t->priority,
254
                              ( cyg_addrword_t )t,
255
                              timer_isr,
256
                              timer_dsr,
257
                              &t->interrupt_handle, &t->interrupt_object );
258
 
259
        cyg_interrupt_attach( t->interrupt_handle );
260
        cyg_interrupt_unmask( t->vector );
261
 
262
    }
263
 
264
    while( loops-- ) {
265
        int             j;
266
 
267
        // 5 second delay
268
        cyg_thread_delay( 5 * 100 );
269
 
270
        // Disable interrupts while we print details, otherwise it
271
        // comes out very slowly.
272
        HAL_DISABLE_INTERRUPTS( istate );
273
 
274
        if( max_nesting > max_nesting_seen )
275
            max_nesting_seen = max_nesting;
276
 
277
        diag_printf( "\nISRs max_nesting %d max_nesting_seen %d\n",
278
                     max_nesting, max_nesting_seen );
279
        max_nesting = 0;
280
 
281
        diag_printf( " T      Ticks " );
282
 
283
        for( j = 0; j < 9; j++ )
284
            diag_printf( "%9d ", j );
285
        diag_printf( "\n" );
286
 
287
        for( i = 0; timers[i].timer != 0; i++ ) {
288
            struct timer   *t = &timers[i];
289
 
290
            diag_printf( "%2d: %9d ", t->timer, t->ticks );
291
 
292
            for( j = 0; j < 9; j++ )
293
                diag_printf( "%9d ", t->preempt[j] );
294
            diag_printf( "\n" );
295
 
296
        }
297
 
298
        diag_printf( "DSRs\n" );
299
 
300
        diag_printf( " T:           " );
301
 
302
        for( j = 0; j < 9; j++ )
303
            diag_printf( "%9d ", j );
304
        diag_printf( "\n" );
305
 
306
        for( i = 0; timers[i].timer != 0; i++ ) {
307
            struct timer   *t = &timers[i];
308
 
309
            diag_printf( "%2d:  preempt: ", t->timer );
310
 
311
            for( j = 0; j < 9; j++ )
312
                diag_printf( "%9d ", t->preempt_dsr[j] );
313
            diag_printf( "\n" );
314
 
315
            diag_printf( "       count: " );
316
 
317
            for( j = 0; j < 9; j++ )
318
                diag_printf( "%9d ", t->dsr_count[j] );
319
            diag_printf( "\n" );
320
        }
321
 
322
        HAL_RESTORE_INTERRUPTS( istate );
323
    }
324
 
325
    CYG_TEST_PASS_FINISH( "Timers test" );
326
}
327
 
328
 
329
//=============================================================================
330
 
331
void
332
cyg_user_start( void )
333
{
334
    cyg_thread_create( 0,              // Priority
335
                       timers_test,    //
336
                       0,              //
337
                       "timers test",  // Name
338
                       test_stack,     // Stack
339
                       CYGNUM_HAL_STACK_SIZE_MINIMUM,   // Stack size
340
                       &main_thread,   // Handle
341
                       &test_thread    // Thread data structure
342
         );
343
    cyg_thread_resume( main_thread );
344
}
345
 
346
//=============================================================================
347
 
348
#else // NA_MSG
349
 
350
void
351
cyg_user_start( void )
352
{
353
    CYG_TEST_NA( NA_MSG );
354
}
355
 
356
#endif // NA_MSG
357
 
358
//=============================================================================
359
// EOF timers.c

powered by: WebSVN 2.1.0

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