comparison packages/io/watchdog/current/tests/watchdog.cxx @ 94:4642a6d35749 ecos-sw-2000-06-02

Merge from eCos master repository on 2000-06-02-07:47:04-BST
author jlarmour
date Fri, 02 Jun 2000 17:35:00 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
93:8c2901fd1dc1 94:4642a6d35749
1 //==========================================================================
2 //
3 // watchdog.cxx
4 //
5 // Watchdog test
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): nickg
35 // Contributors: nickg
36 // Date: 1998-07-20
37 // Description: This test exercises the Watchdog class and checks that it
38 // works as advertised.
39 //####DESCRIPTIONEND####
40 // -------------------------------------------------------------------------
41
42 #include <pkgconf/kernel.h>
43
44 #include <cyg/kernel/thread.hxx>
45 #include <cyg/kernel/thread.inl>
46 #include <cyg/kernel/sched.hxx>
47 #include <cyg/kernel/mutex.hxx>
48 #include <cyg/kernel/sema.hxx>
49 #include <cyg/kernel/clock.hxx>
50
51 #include <cyg/kernel/diag.h>
52
53 #include <cyg/io/watchdog.hxx>
54
55 #include <cyg/infra/testcase.h>
56 #include <cyg/infra/diag.h>
57
58 #if defined(CYGFUN_KERNEL_THREADS_TIMER) && \
59 defined(CYGVAR_KERNEL_COUNTERS_CLOCK)
60
61 #include <cyg/kernel/sched.inl>
62
63 // -------------------------------------------------------------------------
64 // Data for the test
65
66 #ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
67 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
68 #else
69 #define STACKSIZE (2*1024) // size of thread stack
70 #endif
71
72 char thread_stack[STACKSIZE];
73
74 inline void *operator new(size_t size, void *ptr) { return ptr; };
75
76 // array of threads.
77 char thread[sizeof(Cyg_Thread)];
78
79 Cyg_Thread *th;
80
81 //cyg_tick_count one_sec;
82 cyg_tick_count watchdog_delay;
83 volatile bool watchdog_fired;
84 volatile cyg_tick_count watchdog_time;
85
86 #define WATCHDOG_LOOPS_HW 5
87 #define WATCHDOG_LOOPS_SIM 1
88 #define WATCHDOG_CYCLES_PER_LOOP_HW 10
89 #define WATCHDOG_CYCLES_PER_LOOP_SIM 2
90 #define WATCHDOG_TICKS_TILL_TIMEOUT 10
91
92 // -------------------------------------------------------------------------
93 // Action functions:
94
95 void watchdog_action1( CYG_ADDRWORD data )
96 {
97 watchdog_fired = true;
98 watchdog_time = Cyg_Clock::real_time_clock->current_value();
99 }
100
101 void watchdog_action2( CYG_ADDRWORD data )
102 {
103 CYG_TEST_PASS_FINISH("Watchdog OK");
104 }
105
106 // -------------------------------------------------------------------------
107 // Thread body
108
109
110 void watchdog_thread( CYG_ADDRWORD id )
111 {
112 cyg_tick_count watchdog_start_time;
113 cyg_tick_count thread_start_time, thread_end_time;
114 cyg_ucount8 watchdog_loops, watchdog_cycles_per_loop;
115
116 if (cyg_test_is_simulator) {
117 watchdog_loops = WATCHDOG_LOOPS_SIM;
118 watchdog_cycles_per_loop = WATCHDOG_CYCLES_PER_LOOP_SIM;
119 } else {
120 watchdog_loops = WATCHDOG_LOOPS_HW;
121 watchdog_cycles_per_loop = WATCHDOG_CYCLES_PER_LOOP_HW;
122 }
123
124 watchdog_fired = false;
125 Cyg_Watchdog::watchdog.start();
126
127 CYG_TEST_INFO("Testing that watchdog does not fire early");
128
129 for (cyg_ucount8 tries = 0; tries < watchdog_loops; tries++) {
130 diag_printf("Iteration #%d (testing over %d cycles)\n",
131 tries, watchdog_cycles_per_loop);
132
133 // First test that the watchdog does not trigger when being reset.
134 Cyg_Watchdog_Action wdaction( watchdog_action1, 0 );
135
136 wdaction.install();
137 Cyg_Watchdog::watchdog.reset();
138
139 watchdog_start_time = Cyg_Clock::real_time_clock->current_value();
140 watchdog_fired = false;
141
142 for( cyg_ucount8 i = 0; i < watchdog_cycles_per_loop; i++ ) {
143 thread_start_time = Cyg_Clock::real_time_clock->current_value();
144 th->delay( watchdog_delay-2 );
145 Cyg_Watchdog::watchdog.reset();
146 if (watchdog_fired) {
147 thread_end_time = Cyg_Clock::real_time_clock->current_value();
148 diag_printf("Watchdog fired prematurely after %d ticks\n",
149 (int)(watchdog_time - watchdog_start_time));
150 diag_printf(" Thread slept for %d ticks, loop #%d\n",
151 (int)(thread_end_time - thread_start_time), i);
152 CYG_TEST_FAIL_FINISH("Watchdog triggered unexpectedly");
153 break;
154 } else {
155 CYG_TEST_STILL_ALIVE(i, "Resetting watchdog...");
156 Cyg_Watchdog::watchdog.reset();
157 watchdog_fired = false;
158 watchdog_start_time = Cyg_Clock::real_time_clock->current_value();
159 }
160 }
161 }
162
163 // Now check that it triggers when not reset
164 CYG_TEST_INFO("Testing that watchdog fires");
165 {
166 Cyg_Watchdog_Action wdaction( watchdog_action2, 0 );
167
168 wdaction.install();
169
170 Cyg_Watchdog::watchdog.reset();
171
172 th->delay( watchdog_delay*WATCHDOG_TICKS_TILL_TIMEOUT );
173 }
174
175 CYG_TEST_FAIL_FINISH("Watchdog failed to trigger");
176 }
177
178 // -------------------------------------------------------------------------
179
180
181 externC void
182 cyg_start( void )
183 {
184 CYG_TEST_INIT();
185
186 #if !defined(CYGIMP_WATCHDOG_EMULATE) && defined(CYGPKG_HAL_MN10300_STDEVAL1)
187 // Workaround for PR 17974
188 if( cyg_test_is_simulator )
189 CYG_TEST_NA("Watchdog device not implemented in MN10300 simulator.");
190 #endif
191
192
193 Cyg_Clock::cyg_resolution res = Cyg_Clock::real_time_clock->get_resolution();
194
195 cyg_uint64 wres = Cyg_Watchdog::watchdog.get_resolution();
196
197 // Calculate how many clock ticks there are in a watchdog cycle.
198
199 watchdog_delay = ((cyg_tick_count)wres * (cyg_tick_count)res.divisor );
200 watchdog_delay /= res.dividend;
201
202 th = new((void *)&thread) Cyg_Thread(CYG_SCHED_DEFAULT_INFO,
203 watchdog_thread,
204 0,
205 "watchdog_thread",
206 (CYG_ADDRESS)thread_stack,
207 STACKSIZE
208 );
209
210 th->resume();
211
212 // Get the world going
213 Cyg_Scheduler::scheduler.start();
214
215 }
216
217 #else // if defined(CYGFUN_KERNEL_THREADS_TIMER) etc...
218
219 externC void
220 cyg_start( void )
221 {
222 CYG_TEST_INIT();
223 CYG_TEST_NA("Kernel real-time clock/threads timer disabled");
224 }
225
226 #endif // if defined(CYGFUN_KERNEL_THREADS_TIMER) etc...
227
228 // -------------------------------------------------------------------------
229 // EOF watchdog.cxx