1 |
/* |
---|
2 |
* Copyright (C) 2005-2007 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* FFADO is free software; you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation; either version 2 of the License, or |
---|
12 |
* (at your option) any later version. |
---|
13 |
* FFADO is distributed in the hope that it will be useful, |
---|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 |
* GNU General Public License for more details. |
---|
17 |
* |
---|
18 |
* You should have received a copy of the GNU General Public License |
---|
19 |
* along with FFADO; if not, write to the Free Software |
---|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
21 |
* MA 02111-1307 USA. |
---|
22 |
* |
---|
23 |
*/ |
---|
24 |
|
---|
25 |
#ifdef HAVE_CONFIG_H |
---|
26 |
#include <config.h> |
---|
27 |
#endif |
---|
28 |
|
---|
29 |
#include <stdio.h> |
---|
30 |
#include <stdlib.h> |
---|
31 |
#include <string.h> |
---|
32 |
#include <endian.h> |
---|
33 |
|
---|
34 |
#include <signal.h> |
---|
35 |
#include "src/debugmodule/debugmodule.h" |
---|
36 |
|
---|
37 |
#include <netinet/in.h> |
---|
38 |
|
---|
39 |
#include "src/libstreaming/util/cycletimer.h" |
---|
40 |
|
---|
41 |
#include "src/libstreaming/util/IsoHandler.h" |
---|
42 |
#include "src/libstreaming/generic/IsoStream.h" |
---|
43 |
#include "src/libstreaming/util/IsoHandlerManager.h" |
---|
44 |
#include "src/libutil/PosixThread.h" |
---|
45 |
|
---|
46 |
#define TEST_PORT_0 |
---|
47 |
// #define TEST_PORT_1 |
---|
48 |
// #define TEST_PORT_2 |
---|
49 |
|
---|
50 |
using namespace Streaming; |
---|
51 |
using namespace Util; |
---|
52 |
|
---|
53 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
54 |
|
---|
55 |
int run; |
---|
56 |
|
---|
57 |
struct CYCLE_TIMER_REGISTER { |
---|
58 |
uint16_t seconds; |
---|
59 |
uint16_t cycles; |
---|
60 |
uint16_t offset; |
---|
61 |
}; |
---|
62 |
|
---|
63 |
uint64_t ctr_to_quadlet(struct CYCLE_TIMER_REGISTER x) { |
---|
64 |
uint64_t retval=0; |
---|
65 |
|
---|
66 |
x.seconds &= 0x7F; |
---|
67 |
|
---|
68 |
x.cycles &= 0x1FFF; |
---|
69 |
x.cycles %= 8000; |
---|
70 |
|
---|
71 |
x.offset &= 0xFFF; |
---|
72 |
x.offset %= 3072; |
---|
73 |
|
---|
74 |
retval = (x.seconds << 25) + (x.cycles << 12) + (x.offset); |
---|
75 |
return retval & 0xFFFFFFFF; |
---|
76 |
} |
---|
77 |
|
---|
78 |
static void sighandler (int sig) |
---|
79 |
{ |
---|
80 |
run = 0; |
---|
81 |
} |
---|
82 |
|
---|
83 |
int do_cycletimer_test() { |
---|
84 |
|
---|
85 |
struct CYCLE_TIMER_REGISTER cycle_timer; |
---|
86 |
uint32_t *cycle_timer_as_uint=(uint32_t *)&cycle_timer; |
---|
87 |
|
---|
88 |
uint32_t i=0; |
---|
89 |
uint32_t targetval=0; |
---|
90 |
|
---|
91 |
int failures=0; |
---|
92 |
|
---|
93 |
|
---|
94 |
// test 1 |
---|
95 |
// |
---|
96 |
|
---|
97 |
*cycle_timer_as_uint=0; |
---|
98 |
for (i=0;i<3072;i++) { |
---|
99 |
cycle_timer.offset=i; |
---|
100 |
targetval=CYCLE_TIMER_GET_OFFSET(ctr_to_quadlet(cycle_timer)); |
---|
101 |
|
---|
102 |
if(targetval != i) { |
---|
103 |
debugOutput(DEBUG_LEVEL_NORMAL, " test1 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval); |
---|
104 |
failures++; |
---|
105 |
} |
---|
106 |
} |
---|
107 |
|
---|
108 |
for (i=0;i<8000;i++) { |
---|
109 |
cycle_timer.cycles=i; |
---|
110 |
targetval=CYCLE_TIMER_GET_CYCLES(ctr_to_quadlet(cycle_timer)); |
---|
111 |
|
---|
112 |
if(targetval != i) { |
---|
113 |
debugOutput(DEBUG_LEVEL_NORMAL, " test2 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval); |
---|
114 |
failures++; |
---|
115 |
} |
---|
116 |
} |
---|
117 |
|
---|
118 |
for (i=0;i<128;i++) { |
---|
119 |
cycle_timer.seconds=i; |
---|
120 |
targetval=CYCLE_TIMER_GET_SECS(ctr_to_quadlet(cycle_timer)); |
---|
121 |
|
---|
122 |
if(targetval != i) { |
---|
123 |
debugOutput(DEBUG_LEVEL_NORMAL, " test3 failed on i=%d (%08X), returns %d (%08X)\n",i,i,targetval,targetval); |
---|
124 |
failures++; |
---|
125 |
} |
---|
126 |
} |
---|
127 |
|
---|
128 |
|
---|
129 |
// a value in ticks |
---|
130 |
// should be: 10sec, 1380cy, 640ticks |
---|
131 |
targetval=250000000L; |
---|
132 |
cycle_timer.seconds = TICKS_TO_SECS(targetval); |
---|
133 |
cycle_timer.cycles = TICKS_TO_CYCLES(targetval); |
---|
134 |
cycle_timer.offset = TICKS_TO_OFFSET(targetval); |
---|
135 |
|
---|
136 |
if((cycle_timer.seconds != 10) | |
---|
137 |
(cycle_timer.cycles != 1380) | |
---|
138 |
(cycle_timer.offset != 640)) |
---|
139 |
{ |
---|
140 |
debugOutput(DEBUG_LEVEL_NORMAL, " test4 failed: (%u,10)sec (%u,1380)cy (%u,640)ticks\n", |
---|
141 |
cycle_timer.seconds,cycle_timer.cycles,cycle_timer.offset); |
---|
142 |
failures++; |
---|
143 |
} else { |
---|
144 |
debugOutput(DEBUG_LEVEL_NORMAL, " test4 ok\n"); |
---|
145 |
} |
---|
146 |
|
---|
147 |
i=TICKS_TO_CYCLE_TIMER(targetval); |
---|
148 |
if (i != 0x14564280) { |
---|
149 |
debugOutput(DEBUG_LEVEL_NORMAL, " test5 failed: (0x%08X,0x14564280)\n", |
---|
150 |
i); |
---|
151 |
failures++; |
---|
152 |
} else { |
---|
153 |
debugOutput(DEBUG_LEVEL_NORMAL, " test5 ok\n"); |
---|
154 |
} |
---|
155 |
|
---|
156 |
targetval=CYCLE_TIMER_TO_TICKS(i); |
---|
157 |
if (targetval!=250000000L) { |
---|
158 |
debugOutput(DEBUG_LEVEL_NORMAL, " test6 failed: (%u,250000000)\n", |
---|
159 |
targetval); |
---|
160 |
failures++; |
---|
161 |
} else { |
---|
162 |
debugOutput(DEBUG_LEVEL_NORMAL, " test6 ok\n"); |
---|
163 |
} |
---|
164 |
|
---|
165 |
int32_t subs; |
---|
166 |
subs=diffTicks(10, 8); |
---|
167 |
if (subs != 2) { |
---|
168 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, 8) != 2 : %ld\n", |
---|
169 |
subs); |
---|
170 |
failures++; |
---|
171 |
} |
---|
172 |
|
---|
173 |
subs=diffTicks(10, 12); |
---|
174 |
if (subs != -2) { |
---|
175 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, 12) != -2 : %ld\n", |
---|
176 |
subs); |
---|
177 |
failures++; |
---|
178 |
} |
---|
179 |
|
---|
180 |
subs=diffTicks(TICKS_PER_SECOND*128L + 10, 8); |
---|
181 |
if (subs != 2) { |
---|
182 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(TICKS_PER_SECOND*128L + 10, 8) != 2 : %ld\n", |
---|
183 |
subs); |
---|
184 |
failures++; |
---|
185 |
} |
---|
186 |
|
---|
187 |
subs=diffTicks(TICKS_PER_SECOND*128L + 10, 12); |
---|
188 |
if (subs != -2) { |
---|
189 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(TICKS_PER_SECOND*128L + 10, 12) != -2 : %ld\n", |
---|
190 |
subs); |
---|
191 |
failures++; |
---|
192 |
} |
---|
193 |
|
---|
194 |
subs=diffTicks(10, TICKS_PER_SECOND*128L + 8); |
---|
195 |
if (subs != 2) { |
---|
196 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, TICKS_PER_SECOND*128L + 8) != 2 : %ld\n", |
---|
197 |
subs); |
---|
198 |
failures++; |
---|
199 |
} |
---|
200 |
|
---|
201 |
subs=diffTicks(10, TICKS_PER_SECOND*128L + 12); |
---|
202 |
if (subs != -2) { |
---|
203 |
debugOutput(DEBUG_LEVEL_NORMAL, " diffTicks(10, TICKS_PER_SECOND*128L + 12) != -2 : %l011llu\n", |
---|
204 |
subs); |
---|
205 |
failures++; |
---|
206 |
} |
---|
207 |
|
---|
208 |
//--------- |
---|
209 |
// now = 10sec, 1380cy, 640ticks |
---|
210 |
|
---|
211 |
uint32_t st=sytRecvToFullTicks(0x1234, 1000, 0x14564280); |
---|
212 |
if (st != 248860212LLU) { |
---|
213 |
debugOutput(DEBUG_LEVEL_NORMAL, " sytToRecvFullTicks(0x1234, 1000, 0x14564280) != 248860212 : %011lu\n", |
---|
214 |
st); |
---|
215 |
failures++; |
---|
216 |
} |
---|
217 |
|
---|
218 |
st=sytRecvToFullTicks(0xB2B6, 7000, TICKS_TO_CYCLE_TIMER(3118082282LU)); |
---|
219 |
if (st != 3118089910LLU) { |
---|
220 |
debugOutput(DEBUG_LEVEL_NORMAL, " sytToRecvFullTicks(0x1234, 1000, %08X) != 3118089910 : %011lu\n", |
---|
221 |
TICKS_TO_CYCLE_TIMER(3118082282LU), st); |
---|
222 |
failures++; |
---|
223 |
} |
---|
224 |
|
---|
225 |
st=sytXmitToFullTicks(0xC4EA, 3000, TICKS_TO_CYCLE_TIMER(2958285668LU)); |
---|
226 |
if (st != 2958349546LLU) { |
---|
227 |
debugOutput(DEBUG_LEVEL_NORMAL, " sytToXmitFullTicks(0x1234, 1000, %08X) != 2958349546 : %011lu\n", |
---|
228 |
TICKS_TO_CYCLE_TIMER(2958285668LU), st); |
---|
229 |
failures++; |
---|
230 |
} |
---|
231 |
|
---|
232 |
if (failures) { |
---|
233 |
debugOutput(DEBUG_LEVEL_NORMAL, " %d failures\n",failures); |
---|
234 |
return -1; |
---|
235 |
} else { |
---|
236 |
debugOutput(DEBUG_LEVEL_NORMAL, " no failures\n"); |
---|
237 |
} |
---|
238 |
return 0; |
---|
239 |
} |
---|
240 |
|
---|
241 |
int main(int argc, char *argv[]) |
---|
242 |
{ |
---|
243 |
|
---|
244 |
run=1; |
---|
245 |
|
---|
246 |
IsoHandlerManager *m_isoManager=NULL; |
---|
247 |
|
---|
248 |
#ifdef TEST_PORT_0 |
---|
249 |
IsoStream *s=NULL; |
---|
250 |
#endif |
---|
251 |
#ifdef TEST_PORT_1 |
---|
252 |
IsoStream *s2=NULL; |
---|
253 |
#endif |
---|
254 |
#ifdef TEST_PORT_2 |
---|
255 |
IsoStream *s3=NULL; |
---|
256 |
#endif |
---|
257 |
|
---|
258 |
signal (SIGINT, sighandler); |
---|
259 |
signal (SIGPIPE, sighandler); |
---|
260 |
|
---|
261 |
debugOutput(DEBUG_LEVEL_NORMAL, "FFADO Cycle timer test application\n"); |
---|
262 |
|
---|
263 |
debugOutput(DEBUG_LEVEL_NORMAL, "Testing cycle timer helper functions & macro's... \n"); |
---|
264 |
if(do_cycletimer_test()) { |
---|
265 |
debugOutput(DEBUG_LEVEL_NORMAL, " !!! FAILED !!!\n"); |
---|
266 |
exit(1); |
---|
267 |
} else { |
---|
268 |
debugOutput(DEBUG_LEVEL_NORMAL, " !!! PASSED !!!\n"); |
---|
269 |
} |
---|
270 |
|
---|
271 |
// exit(1); |
---|
272 |
|
---|
273 |
m_isoManager=new IsoHandlerManager(); |
---|
274 |
|
---|
275 |
if(!m_isoManager) { |
---|
276 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoHandlerManager\n"); |
---|
277 |
goto finish; |
---|
278 |
} |
---|
279 |
|
---|
280 |
m_isoManager->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
281 |
|
---|
282 |
if(!m_isoManager->init()) { |
---|
283 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init() IsoHandlerManager\n"); |
---|
284 |
goto finish; |
---|
285 |
} |
---|
286 |
|
---|
287 |
|
---|
288 |
#ifdef TEST_PORT_0 |
---|
289 |
// add a stream to the manager so that it has something to do |
---|
290 |
s=new IsoStream(IsoStream::eST_Receive, 0); |
---|
291 |
|
---|
292 |
if (!s) { |
---|
293 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n"); |
---|
294 |
goto finish; |
---|
295 |
} |
---|
296 |
|
---|
297 |
s->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
298 |
s->setChannel(0); |
---|
299 |
|
---|
300 |
if(!m_isoManager->registerStream(s)) { |
---|
301 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n"); |
---|
302 |
goto finish; |
---|
303 |
} |
---|
304 |
#endif |
---|
305 |
|
---|
306 |
#ifdef TEST_PORT_1 |
---|
307 |
// add a stream to the manager so that it has something to do |
---|
308 |
s2=new IsoStream(IsoStream::EST_Receive, 1); |
---|
309 |
|
---|
310 |
if (!s2) { |
---|
311 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n"); |
---|
312 |
goto finish; |
---|
313 |
} |
---|
314 |
|
---|
315 |
s2->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
316 |
|
---|
317 |
if (!s2->init()) { |
---|
318 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n"); |
---|
319 |
goto finish; |
---|
320 |
} |
---|
321 |
|
---|
322 |
s2->setChannel(0); |
---|
323 |
|
---|
324 |
if(!m_isoManager->registerStream(s2)) { |
---|
325 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n"); |
---|
326 |
goto finish; |
---|
327 |
} |
---|
328 |
#endif |
---|
329 |
|
---|
330 |
#ifdef TEST_PORT_2 |
---|
331 |
// add a stream to the manager so that it has something to do |
---|
332 |
s3=new IsoStream(IsoStream::EST_Receive,2); |
---|
333 |
|
---|
334 |
if (!s3) { |
---|
335 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not create IsoStream\n"); |
---|
336 |
goto finish; |
---|
337 |
} |
---|
338 |
|
---|
339 |
s3->setVerboseLevel(DEBUG_LEVEL_VERBOSE); |
---|
340 |
|
---|
341 |
if (!s3->init()) { |
---|
342 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not init IsoStream\n"); |
---|
343 |
goto finish; |
---|
344 |
} |
---|
345 |
|
---|
346 |
s3->setChannel(0); |
---|
347 |
|
---|
348 |
if(!m_isoManager->registerStream(s3)) { |
---|
349 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not register IsoStream\n"); |
---|
350 |
goto finish; |
---|
351 |
} |
---|
352 |
#endif |
---|
353 |
|
---|
354 |
debugOutput(DEBUG_LEVEL_NORMAL, "Preparing IsoHandlerManager...\n"); |
---|
355 |
if (!m_isoManager->prepare()) { |
---|
356 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not prepare isoManager\n"); |
---|
357 |
goto finish; |
---|
358 |
} |
---|
359 |
|
---|
360 |
debugOutput(DEBUG_LEVEL_NORMAL, "Starting IsoHandler...\n"); |
---|
361 |
if (!m_isoManager->startHandlers(0)) { |
---|
362 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not start handlers...\n"); |
---|
363 |
goto finish; |
---|
364 |
} |
---|
365 |
|
---|
366 |
while(run) { |
---|
367 |
sleep(1); |
---|
368 |
m_isoManager->dumpInfo(); |
---|
369 |
} |
---|
370 |
|
---|
371 |
debugOutput(DEBUG_LEVEL_NORMAL, "Stopping handlers...\n"); |
---|
372 |
if(!m_isoManager->stopHandlers()) { |
---|
373 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not stop ISO handlers\n"); |
---|
374 |
goto finish; |
---|
375 |
} |
---|
376 |
|
---|
377 |
#ifdef TEST_PORT_0 |
---|
378 |
if(!m_isoManager->unregisterStream(s)) { |
---|
379 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n"); |
---|
380 |
goto finish; |
---|
381 |
} |
---|
382 |
delete s; |
---|
383 |
#endif |
---|
384 |
|
---|
385 |
#ifdef TEST_PORT_1 |
---|
386 |
if(!m_isoManager->unregisterStream(s1)) { |
---|
387 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n"); |
---|
388 |
goto finish; |
---|
389 |
} |
---|
390 |
delete s1; |
---|
391 |
#endif |
---|
392 |
|
---|
393 |
#ifdef TEST_PORT_2 |
---|
394 |
if(!m_isoManager->unregisterStream(s2)) { |
---|
395 |
debugOutput(DEBUG_LEVEL_NORMAL, "Could not unregister IsoStream\n"); |
---|
396 |
goto finish; |
---|
397 |
} |
---|
398 |
delete s2; |
---|
399 |
#endif |
---|
400 |
|
---|
401 |
delete m_isoManager; |
---|
402 |
|
---|
403 |
finish: |
---|
404 |
debugOutput(DEBUG_LEVEL_NORMAL, "Bye...\n"); |
---|
405 |
|
---|
406 |
return EXIT_SUCCESS; |
---|
407 |
} |
---|