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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [watchdog/] [v2_0/] [tests/] [watchdog.cxx] - Blame information for rev 631

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//        watchdog.cxx
4
//
5
//        Watchdog test
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, 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 version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):     nickg
44
// Contributors:    nickg
45
// Date:          1998-07-20
46
// Description:   This test exercises the Watchdog class and checks that it
47
//                works as advertised.
48
//####DESCRIPTIONEND####
49
// -------------------------------------------------------------------------
50
 
51
#include <pkgconf/kernel.h>
52
 
53
#include <cyg/kernel/thread.hxx>
54
#include <cyg/kernel/thread.inl>
55
#include <cyg/kernel/sched.hxx>
56
#include <cyg/kernel/mutex.hxx>
57
#include <cyg/kernel/sema.hxx>
58
#include <cyg/kernel/clock.hxx>
59
 
60
#include <cyg/kernel/diag.h>
61
 
62
#include <cyg/io/watchdog.hxx>
63
 
64
#include <cyg/infra/testcase.h>
65
#include <cyg/infra/diag.h>
66
 
67
#if defined(CYGFUN_KERNEL_THREADS_TIMER) && \
68
    defined(CYGVAR_KERNEL_COUNTERS_CLOCK)
69
 
70
#include <cyg/kernel/sched.inl>
71
 
72
// -------------------------------------------------------------------------
73
// Data for the test
74
 
75
#ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
76
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
77
#else
78
#define STACKSIZE       (2*1024)        // size of thread stack
79
#endif
80
 
81
char thread_stack[STACKSIZE];
82
 
83
inline void *operator new(size_t size, void *ptr) { return ptr; };
84
 
85
// array of threads.
86
char thread[sizeof(Cyg_Thread)];
87
 
88
Cyg_Thread *th;
89
 
90
//cyg_tick_count one_sec;
91
cyg_tick_count watchdog_delay;
92
volatile bool watchdog_fired;
93
volatile cyg_tick_count watchdog_time;
94
 
95
#define WATCHDOG_LOOPS_HW               5
96
#define WATCHDOG_LOOPS_SIM              1
97
#define WATCHDOG_CYCLES_PER_LOOP_HW     10
98
#define WATCHDOG_CYCLES_PER_LOOP_SIM    2
99
#define WATCHDOG_TICKS_TILL_TIMEOUT     10
100
 
101
// -------------------------------------------------------------------------
102
// Action functions:
103
 
104
void watchdog_action1( CYG_ADDRWORD data )
105
{
106
    watchdog_fired = true;
107
    watchdog_time = Cyg_Clock::real_time_clock->current_value();
108
}
109
 
110
void watchdog_action2( CYG_ADDRWORD data )
111
{
112
    CYG_TEST_PASS_FINISH("Watchdog OK");
113
}
114
 
115
// -------------------------------------------------------------------------
116
// Thread body
117
 
118
 
119
void watchdog_thread( CYG_ADDRWORD id )
120
{
121
    cyg_tick_count watchdog_start_time;
122
    cyg_tick_count thread_start_time, thread_end_time;
123
    cyg_ucount8 watchdog_loops, watchdog_cycles_per_loop;
124
 
125
    if (cyg_test_is_simulator) {
126
        watchdog_loops = WATCHDOG_LOOPS_SIM;
127
        watchdog_cycles_per_loop = WATCHDOG_CYCLES_PER_LOOP_SIM;
128
    } else {
129
        watchdog_loops = WATCHDOG_LOOPS_HW;
130
        watchdog_cycles_per_loop = WATCHDOG_CYCLES_PER_LOOP_HW;
131
    }
132
 
133
    watchdog_fired = false;
134
    Cyg_Watchdog::watchdog.start();
135
 
136
    CYG_TEST_INFO("Testing that watchdog does not fire early");
137
 
138
    for (cyg_ucount8 tries = 0;  tries < watchdog_loops;  tries++) {
139
        diag_printf("Iteration #%d (testing over %d cycles)\n",
140
                    tries, watchdog_cycles_per_loop);
141
 
142
        // First test that the watchdog does not trigger when being reset.
143
        Cyg_Watchdog_Action wdaction( watchdog_action1, 0 );
144
 
145
        wdaction.install();
146
        Cyg_Watchdog::watchdog.reset();
147
 
148
        watchdog_start_time = Cyg_Clock::real_time_clock->current_value();
149
        watchdog_fired = false;
150
 
151
        for( cyg_ucount8 i = 0; i < watchdog_cycles_per_loop; i++ ) {
152
            thread_start_time = Cyg_Clock::real_time_clock->current_value();
153
            th->delay( watchdog_delay-2 );
154
            Cyg_Watchdog::watchdog.reset();
155
            if (watchdog_fired) {
156
                thread_end_time = Cyg_Clock::real_time_clock->current_value();
157
                diag_printf("Watchdog fired prematurely after %d ticks\n",
158
                            (int)(watchdog_time - watchdog_start_time));
159
                diag_printf("  Thread slept for %d ticks, loop #%d\n",
160
                            (int)(thread_end_time - thread_start_time), i);
161
                CYG_TEST_FAIL_FINISH("Watchdog triggered unexpectedly");
162
                break;
163
            } else {
164
                CYG_TEST_STILL_ALIVE(i, "Resetting watchdog...");
165
                Cyg_Watchdog::watchdog.reset();
166
                watchdog_fired = false;
167
                watchdog_start_time = Cyg_Clock::real_time_clock->current_value();
168
            }
169
        }
170
    }
171
 
172
    // Now check that it triggers when not reset
173
    CYG_TEST_INFO("Testing that watchdog fires");
174
    {
175
        Cyg_Watchdog_Action wdaction( watchdog_action2, 0 );
176
 
177
        wdaction.install();
178
 
179
        Cyg_Watchdog::watchdog.reset();
180
 
181
        th->delay( watchdog_delay*WATCHDOG_TICKS_TILL_TIMEOUT );
182
    }
183
 
184
    CYG_TEST_FAIL_FINISH("Watchdog failed to trigger");
185
}
186
 
187
// -------------------------------------------------------------------------
188
 
189
 
190
externC void
191
cyg_start( void )
192
{
193
    CYG_TEST_INIT();
194
 
195
#if !defined(CYGIMP_WATCHDOG_EMULATE) && defined(CYGPKG_HAL_MN10300_STDEVAL1)
196
    // Workaround for PR 17974
197
    if( cyg_test_is_simulator )
198
        CYG_TEST_NA("Watchdog device not implemented in MN10300 simulator.");
199
#endif
200
 
201
 
202
    Cyg_Clock::cyg_resolution res = Cyg_Clock::real_time_clock->get_resolution();
203
 
204
    cyg_uint64 wres = Cyg_Watchdog::watchdog.get_resolution();
205
 
206
    // Calculate how many clock ticks there are in a watchdog cycle.
207
 
208
    watchdog_delay = ((cyg_tick_count)wres * (cyg_tick_count)res.divisor );
209
    watchdog_delay /= res.dividend;
210
 
211
    th = new((void *)&thread) Cyg_Thread(CYG_SCHED_DEFAULT_INFO,
212
                                         watchdog_thread,
213
                                         0,
214
                                         "watchdog_thread",
215
                                         (CYG_ADDRESS)thread_stack,
216
                                         STACKSIZE
217
        );
218
 
219
    th->resume();
220
 
221
    // Get the world going
222
    Cyg_Scheduler::scheduler.start();
223
 
224
}
225
 
226
#else // if defined(CYGFUN_KERNEL_THREADS_TIMER) etc...
227
 
228
externC void
229
cyg_start( void )
230
{
231
    CYG_TEST_INIT();
232
    CYG_TEST_NA("Kernel real-time clock/threads timer disabled");
233
}
234
 
235
#endif // if defined(CYGFUN_KERNEL_THREADS_TIMER) etc...
236
 
237
// -------------------------------------------------------------------------
238
// EOF watchdog.cxx

powered by: WebSVN 2.1.0

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