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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [watchdog/] [current/] [tests/] [watchdog.cxx] - Blame information for rev 842

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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 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
43
// Contributors:    nickg
44
// Date:          1998-07-20
45
// Description:   This test exercises the Watchdog class and checks that it
46
//                works as advertised.
47
//####DESCRIPTIONEND####
48
// -------------------------------------------------------------------------
49
 
50
#include <pkgconf/kernel.h>
51
 
52
#include <cyg/kernel/thread.hxx>
53
#include <cyg/kernel/thread.inl>
54
#include <cyg/kernel/sched.hxx>
55
#include <cyg/kernel/mutex.hxx>
56
#include <cyg/kernel/sema.hxx>
57
#include <cyg/kernel/clock.hxx>
58
 
59
#include <cyg/kernel/diag.h>
60
 
61
#include <cyg/io/watchdog.hxx>
62
 
63
#include <cyg/infra/testcase.h>
64
#include <cyg/infra/diag.h>
65
 
66
#if defined(CYGFUN_KERNEL_THREADS_TIMER) && \
67
    defined(CYGVAR_KERNEL_COUNTERS_CLOCK)
68
 
69
#include <cyg/kernel/sched.inl>
70
 
71
// -------------------------------------------------------------------------
72
// Data for the test
73
 
74
#ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
75
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
76
#else
77
#define STACKSIZE       (2*1024)        // size of thread stack
78
#endif
79
 
80
char thread_stack[STACKSIZE];
81
 
82
inline void *operator new(size_t size, void *ptr) { return ptr; };
83
 
84
// array of threads.
85
char thread[sizeof(Cyg_Thread)];
86
 
87
Cyg_Thread *th;
88
 
89
//cyg_tick_count one_sec;
90
cyg_tick_count watchdog_delay;
91
volatile bool watchdog_fired;
92
volatile cyg_tick_count watchdog_time;
93
 
94
#define WATCHDOG_LOOPS_HW               5
95
#define WATCHDOG_LOOPS_SIM              1
96
#define WATCHDOG_CYCLES_PER_LOOP_HW     10
97
#define WATCHDOG_CYCLES_PER_LOOP_SIM    2
98
#define WATCHDOG_TICKS_TILL_TIMEOUT     10
99
 
100
// -------------------------------------------------------------------------
101
// Action functions:
102
 
103
void watchdog_action1( CYG_ADDRWORD data )
104
{
105
    watchdog_fired = true;
106
    watchdog_time = Cyg_Clock::real_time_clock->current_value();
107
}
108
 
109
void watchdog_action2( CYG_ADDRWORD data )
110
{
111
    CYG_TEST_PASS_FINISH("Watchdog OK");
112
}
113
 
114
// -------------------------------------------------------------------------
115
// Thread body
116
 
117
 
118
void watchdog_thread( CYG_ADDRWORD id )
119
{
120
    cyg_tick_count watchdog_start_time;
121
    cyg_tick_count thread_start_time, thread_end_time;
122
    cyg_ucount8 watchdog_loops, watchdog_cycles_per_loop;
123
 
124
    if (cyg_test_is_simulator) {
125
        watchdog_loops = WATCHDOG_LOOPS_SIM;
126
        watchdog_cycles_per_loop = WATCHDOG_CYCLES_PER_LOOP_SIM;
127
    } else {
128
        watchdog_loops = WATCHDOG_LOOPS_HW;
129
        watchdog_cycles_per_loop = WATCHDOG_CYCLES_PER_LOOP_HW;
130
    }
131
 
132
    watchdog_fired = false;
133
    Cyg_Watchdog::watchdog.start();
134
 
135
    CYG_TEST_INFO("Testing that watchdog does not fire early");
136
 
137
    for (cyg_ucount8 tries = 0;  tries < watchdog_loops;  tries++) {
138
        diag_printf("Iteration #%d (testing over %d cycles)\n",
139
                    tries, watchdog_cycles_per_loop);
140
 
141
        // First test that the watchdog does not trigger when being reset.
142
        Cyg_Watchdog_Action wdaction( watchdog_action1, 0 );
143
 
144
        wdaction.install();
145
        Cyg_Watchdog::watchdog.reset();
146
 
147
        watchdog_start_time = Cyg_Clock::real_time_clock->current_value();
148
        watchdog_fired = false;
149
 
150
        for( cyg_ucount8 i = 0; i < watchdog_cycles_per_loop; i++ ) {
151
            thread_start_time = Cyg_Clock::real_time_clock->current_value();
152
            th->delay( watchdog_delay-2 );
153
            Cyg_Watchdog::watchdog.reset();
154
            if (watchdog_fired) {
155
                thread_end_time = Cyg_Clock::real_time_clock->current_value();
156
                diag_printf("Watchdog fired prematurely after %d ticks\n",
157
                            (int)(watchdog_time - watchdog_start_time));
158
                diag_printf("  Thread slept for %d ticks, loop #%d\n",
159
                            (int)(thread_end_time - thread_start_time), i);
160
                CYG_TEST_FAIL_FINISH("Watchdog triggered unexpectedly");
161
                break;
162
            } else {
163
                CYG_TEST_STILL_ALIVE(i, "Resetting watchdog...");
164
                Cyg_Watchdog::watchdog.reset();
165
                watchdog_fired = false;
166
                watchdog_start_time = Cyg_Clock::real_time_clock->current_value();
167
            }
168
        }
169
    }
170
 
171
    // Now check that it triggers when not reset
172
    CYG_TEST_INFO("Testing that watchdog fires");
173
    {
174
        Cyg_Watchdog_Action wdaction( watchdog_action2, 0 );
175
 
176
        wdaction.install();
177
 
178
        Cyg_Watchdog::watchdog.reset();
179
 
180
        th->delay( watchdog_delay*WATCHDOG_TICKS_TILL_TIMEOUT );
181
    }
182
 
183
    CYG_TEST_FAIL_FINISH("Watchdog failed to trigger");
184
}
185
 
186
// -------------------------------------------------------------------------
187
 
188
 
189
externC void
190
cyg_start( void )
191
{
192
    CYG_TEST_INIT();
193
 
194
#if !defined(CYGIMP_WATCHDOG_EMULATE) && defined(CYGPKG_HAL_MN10300_STDEVAL1)
195
    // Workaround for PR 17974
196
    if( cyg_test_is_simulator )
197
        CYG_TEST_NA("Watchdog device not implemented in MN10300 simulator.");
198
#endif
199
 
200
 
201
    Cyg_Clock::cyg_resolution res = Cyg_Clock::real_time_clock->get_resolution();
202
 
203
    cyg_uint64 wres = Cyg_Watchdog::watchdog.get_resolution();
204
 
205
    // Calculate how many clock ticks there are in a watchdog cycle.
206
 
207
    watchdog_delay = ((cyg_tick_count)wres * (cyg_tick_count)res.divisor );
208
    watchdog_delay /= res.dividend;
209
 
210
    th = new((void *)&thread) Cyg_Thread(CYG_SCHED_DEFAULT_INFO,
211
                                         watchdog_thread,
212
                                         0,
213
                                         "watchdog_thread",
214
                                         (CYG_ADDRESS)thread_stack,
215
                                         STACKSIZE
216
        );
217
 
218
    th->resume();
219
 
220
    // Get the world going
221
    Cyg_Scheduler::scheduler.start();
222
 
223
}
224
 
225
#else // if defined(CYGFUN_KERNEL_THREADS_TIMER) etc...
226
 
227
externC void
228
cyg_start( void )
229
{
230
    CYG_TEST_INIT();
231
    CYG_TEST_NA("Kernel real-time clock/threads timer disabled");
232
}
233
 
234
#endif // if defined(CYGFUN_KERNEL_THREADS_TIMER) etc...
235
 
236
// -------------------------------------------------------------------------
237
// EOF watchdog.cxx

powered by: WebSVN 2.1.0

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