root/trunk/libfreebob/tests/test-cyclecounter.cpp

Revision 360, 8.2 kB (checked in by pieterpalmers, 16 years ago)

- temporary commit to backup some work

- Started a framework to synchronize IsoHandlers? to

any generic TimeSource?. The idea is to introduce
one overall time reference, and resynchronize all
other timed events to this time source.
This will, on the long run, allow:

  • combining devices on multiple FW busses together,
    as these are not synched by hardware.
  • synchronizing to the system clock
  • synchronizing to any other time source (e.g.
    when implementing a jackd client, i.e. using
    the freebob devices as jackd clients).

- Implemented a realtime safe way to read the cycle

timer for an IsoHandler?. (+ test application)

- Implemented tests/test-sytmonitor:

Monitors 2 or more channels and reports the average
SYT timestamp difference between both.

- Messed around with SYT timestamping for AMDTP. Doesn't

work (yet).

Line 
1 /***************************************************************************
2   Copyright (C) 2005 by Pieter Palmers   *
3                                                                         *
4   This program is free software; you can redistribute it and/or modify  *
5   it under the terms of the GNU General Public License as published by  *
6   the Free Software Foundation; either version 2 of the License, or     *
7   (at your option) any later version.                                   *
8                                                                         *
9   This program is distributed in the hope that it will be useful,       *
10   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12   GNU General Public License for more details.                          *
13                                                                         *
14   You should have received a copy of the GNU General Public License     *
15   along with this program; if not, write to the                         *
16   Free Software Foundation, Inc.,                                       *
17   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <endian.h>
28
29 #include <signal.h>
30 #include "src/debugmodule/debugmodule.h"
31
32 #include <netinet/in.h>
33
34 #include "src/libstreaming/cyclecounter.h"
35
36 #include "src/libstreaming/IsoHandler.h"
37 #include "src/libstreaming/IsoStream.h"
38 #include "src/libstreaming/IsoHandlerManager.h"
39 #include "src/libutil/PosixThread.h"
40
41 #define TEST_PORT_0
42 // #define TEST_PORT_1
43 // #define TEST_PORT_2
44
45 using namespace FreebobStreaming;
46 using namespace FreebobUtil;
47
48 DECLARE_GLOBAL_DEBUG_MODULE;
49 IMPL_GLOBAL_DEBUG_MODULE( FreeBoB, DEBUG_LEVEL_VERBOSE );
50
51 int run;
52
53 struct CYCLE_TIMER_REGISTER {
54         uint16_t seconds;
55         uint16_t cycles;
56         uint16_t offset;
57 };
58
59 uint64_t ctr_to_quadlet(struct CYCLE_TIMER_REGISTER x) {
60     uint64_t retval=0;
61    
62     x.seconds &= 0x7F;
63    
64     x.cycles &= 0x1FFF;
65     x.cycles %= 8000;
66    
67     x.offset &= 0xFFF;
68     x.offset %= 3072;
69    
70     retval = (x.seconds << 25) + (x.cycles << 12) + (x.offset);
71     return retval & 0xFFFFFFFF;
72 }
73
74 static void sighandler (int sig)
75 {
76         run = 0;
77 }
78
79 int do_cyclecounter_test() {
80    
81     struct CYCLE_TIMER_REGISTER cycle_counter;
82     uint32_t *cycle_counter_as_uint=(uint32_t *)&cycle_counter;
83    
84     uint32_t i=0;
85     uint32_t targetval=0;
86    
87     int failures=0;
88    
89    
90     // test 1
91     //
92    
93     *cycle_counter_as_uint=0;
94     for (i=0;i<3072;i++) {
95         cycle_counter.offset=i;
96         targetval=CYCLE_COUNTER_GET_OFFSET(ctr_to_quadlet(cycle_counter));
97        
98         if(targetval != i) {
99             debugOutput(DEBUG_LEVEL_NORMAL, "  test1 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
100             failures++;
101         }
102     }
103    
104     for (i=0;i<8000;i++) {
105         cycle_counter.cycles=i;
106         targetval=CYCLE_COUNTER_GET_CYCLES(ctr_to_quadlet(cycle_counter));
107        
108         if(targetval != i) {
109             debugOutput(DEBUG_LEVEL_NORMAL, "  test2 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
110             failures++;
111         }
112     }
113    
114     for (i=0;i<128;i++) {
115         cycle_counter.seconds=i;
116         targetval=CYCLE_COUNTER_GET_SECS(ctr_to_quadlet(cycle_counter));
117        
118         if(targetval != i) {
119             debugOutput(DEBUG_LEVEL_NORMAL, "  test3 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval);
120             failures++;
121         }
122     }
123    
124    
125    
126     if (failures) {
127         debugOutput(DEBUG_LEVEL_NORMAL, " %d failures\n",failures);
128         return -1;
129     } else {
130         debugOutput(DEBUG_LEVEL_NORMAL, " no failures\n");
131     }
132     return 0;
133 }
134
135 int main(int argc, char *argv[])
136 {
137
138         run=1;
139        
140         IsoHandlerManager *m_isoManager=NULL;
141     PosixThread * m_isoManagerThread=NULL;
142    
143 #ifdef TEST_PORT_0     
144     IsoStream *s=NULL;
145 #endif
146 #ifdef TEST_PORT_1     
147     IsoStream *s2=NULL;
148 #endif
149 #ifdef TEST_PORT_2
150     IsoStream *s3=NULL;
151 #endif
152
153         signal (SIGINT, sighandler);
154         signal (SIGPIPE, sighandler);
155
156         debugOutput(DEBUG_LEVEL_NORMAL, "Freebob Cycle counter test application\n");
157        
158         debugOutput(DEBUG_LEVEL_NORMAL, "Testing cycle counter helper functions & macro's... \n");
159         if(do_cyclecounter_test()) {
160            debugOutput(DEBUG_LEVEL_NORMAL, " !!! FAILED !!!\n");
161            exit(1);
162         } else {
163            debugOutput(DEBUG_LEVEL_NORMAL, " !!! PASSED !!!\n");
164         }
165        
166 //      exit(1);
167        
168         m_isoManager=new IsoHandlerManager();
169        
170         if(!m_isoManager) {
171                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoHandlerManager\n");
172                 goto finish;
173         }
174        
175         m_isoManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
176                
177     // the thread to iterate the manager
178         m_isoManagerThread=new PosixThread(
179               m_isoManager,
180               true, 80,
181               PTHREAD_CANCEL_DEFERRED);
182              
183         if(!m_isoManagerThread) {
184                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create iso manager thread\n");
185                 goto finish;
186         }
187
188 #ifdef TEST_PORT_0     
189         // add a stream to the manager so that it has something to do
190         s=new IsoStream(IsoStream::EST_Receive, 0);
191        
192         if (!s) {
193                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
194                 goto finish;
195         }       
196        
197         s->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
198        
199         if (!s->init()) {
200                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
201                 goto finish;
202         }
203        
204         s->setChannel(0);
205        
206         if(!m_isoManager->registerStream(s)) {
207                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
208                 goto finish;
209         }
210 #endif
211
212 #ifdef TEST_PORT_1     
213         // add a stream to the manager so that it has something to do
214         s2=new IsoStream(IsoStream::EST_Receive, 1);
215        
216         if (!s2) {
217                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
218                 goto finish;
219         }       
220        
221         s2->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
222        
223         if (!s2->init()) {
224                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
225                 goto finish;
226         }
227        
228         s2->setChannel(0);
229        
230         if(!m_isoManager->registerStream(s2)) {
231                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
232                 goto finish;
233         }
234 #endif
235
236 #ifdef TEST_PORT_2             
237         // add a stream to the manager so that it has something to do
238         s3=new IsoStream(IsoStream::EST_Receive,2);
239        
240         if (!s3) {
241                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n");
242                 goto finish;
243         }       
244        
245         s3->setVerboseLevel(DEBUG_LEVEL_VERBOSE);
246        
247         if (!s3->init()) {
248                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n");
249                 goto finish;
250         }
251        
252         s3->setChannel(0);
253        
254         if(!m_isoManager->registerStream(s3)) {
255                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n");
256                 goto finish;
257         }
258 #endif
259
260         debugOutput(DEBUG_LEVEL_NORMAL,   "Preparing IsoHandlerManager...\n");
261         if (!m_isoManager->prepare()) {
262                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not prepare isoManager\n");
263                 goto finish;
264         }
265
266         debugOutput(DEBUG_LEVEL_NORMAL,   "Starting ISO manager sync update thread...\n");
267         // start the runner thread
268         m_isoManagerThread->Start();
269        
270         debugOutput(DEBUG_LEVEL_NORMAL,   "Starting IsoHandler...\n");
271         if (!m_isoManager->startHandlers(0)) {
272                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not start handlers...\n");
273                 goto finish;
274         }
275        
276         while(run) {
277         sleep(1);
278         m_isoManager->dumpInfo();
279         }
280
281         debugOutput(DEBUG_LEVEL_NORMAL,   "Stopping handlers...\n");
282         if(!m_isoManager->stopHandlers()) {
283            debugOutput(DEBUG_LEVEL_NORMAL, "Could not stop ISO handlers\n");
284            goto finish;
285         }
286        
287         // stop the sync thread
288         debugOutput(DEBUG_LEVEL_NORMAL,   "Stopping ISO manager sync update thread...\n");
289         m_isoManagerThread->Stop();
290        
291 #ifdef TEST_PORT_0     
292         if(!m_isoManager->unregisterStream(s)) {
293                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
294                 goto finish;
295         }
296         delete s;
297 #endif
298
299 #ifdef TEST_PORT_1     
300         if(!m_isoManager->unregisterStream(s1)) {
301                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
302                 goto finish;
303         }
304         delete s1;
305 #endif
306
307 #ifdef TEST_PORT_2     
308         if(!m_isoManager->unregisterStream(s2)) {
309                 debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n");
310                 goto finish;
311         }
312         delete s2;
313 #endif
314        
315         delete m_isoManagerThread;
316     delete m_isoManager;
317
318 finish:
319         debugOutput(DEBUG_LEVEL_NORMAL, "Bye...\n");
320
321     delete DebugModuleManager::instance();
322        
323   return EXIT_SUCCESS;
324 }
Note: See TracBrowser for help on using the browser.