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