root/trunk/libffado/tests/test-ieee1394service.cpp

Revision 752, 5.4 kB (checked in by ppalmers, 14 years ago)

- Implement a DLL based mechanism to read the cycle timer. This can potentially be more lightweight for the reader threads since it avoids a the CTR read kernel call. It also has the
side effect that FFADO now works on older kernels that don't implement the cycle timer read call.

Line 
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  * This program 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 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <endian.h>
32
33 #include <signal.h>
34 #include "src/debugmodule/debugmodule.h"
35
36 #include <netinet/in.h>
37
38 #include "src/threads.h"
39
40 #include "src/libieee1394/cycletimer.h"
41 #include "src/libieee1394/configrom.h"
42 #include "src/libieee1394/ieee1394service.h"
43 #include "src/libieee1394/ARMHandler.h"
44
45 #include <libraw1394/raw1394.h>
46
47 DECLARE_GLOBAL_DEBUG_MODULE;
48
49 int run=1;
50 static void sighandler (int sig)
51 {
52     run = 0;
53 }
54
55 class MyFunctor : public Functor
56 {
57 public:
58     MyFunctor() {}
59     virtual ~MyFunctor() {}
60
61     void operator() () {
62         printf("hello from the functor (%p)\n", this);
63     };
64 };
65
66 int main(int argc, char *argv[])
67 {
68     setDebugLevel(DEBUG_LEVEL_VERBOSE);
69     signal (SIGINT, sighandler);
70     signal (SIGPIPE, sighandler);
71
72     const int PORT_TO_USE = 0;
73
74     printf("FFADO Ieee1394Service test application\n");
75
76     Ieee1394Service *m_service=NULL;
77
78     m_service=new Ieee1394Service();
79     m_service->setVerboseLevel(DEBUG_LEVEL_VERBOSE );
80     m_service->initialize(PORT_TO_USE);
81     m_service->setThreadParameters(true, 20);
82
83     MyFunctor *test_busreset=new MyFunctor();
84
85     printf(" adding (%p) as busreset handler\n", test_busreset);
86
87     m_service->addBusResetHandler(test_busreset);
88
89     nodeaddr_t addr =  m_service->findFreeARMBlock(0x0000FFFFE0000000ULL, 4, 4 );
90
91     ARMHandler *test_arm=new ARMHandler(addr,
92                          4,
93                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
94                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
95                          0);
96
97     printf(" adding (%p) as arm handler\n", test_arm);
98
99     if (!m_service->registerARMHandler(test_arm)) {
100         printf("  failed\n");
101     }
102
103     addr =  m_service->findFreeARMBlock(0x0000FFFFE0000000ULL, 4, 4 );
104
105     ARMHandler *test_arm2=new ARMHandler(addr,
106                          4,
107                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
108                          RAW1394_ARM_READ | RAW1394_ARM_WRITE | RAW1394_ARM_LOCK,
109                          0);
110
111     printf(" adding (%p) as arm handler\n", test_arm2);
112
113     if (!m_service->registerARMHandler(test_arm2)) {
114         printf("  failed\n");
115     }
116
117     raw1394handle_t m_handle = raw1394_new_handle_on_port( PORT_TO_USE );
118     if ( !m_handle ) {
119         if ( !errno ) {
120             debugFatal("libraw1394 not compatible\n");
121         } else {
122             debugFatal("Ieee1394Service::initialize: Could not get 1394 handle: %s\n",
123                 strerror(errno) );
124             debugFatal("Is ieee1394 and raw1394 driver loaded?\n");
125         }
126         exit(-1);
127     }
128
129     while(run) {
130         fflush(stderr);
131         fflush(stdout);
132         usleep(900*1000);
133
134         uint32_t cycle_timer;
135         uint64_t local_time;
136         // read the CTR 'raw' from a handle
137         // and read it from the 1394 service, which uses a DLL
138         int err = raw1394_read_cycle_timer(m_handle, &cycle_timer, &local_time);
139         uint64_t ctr = CYCLE_TIMER_TO_TICKS( cycle_timer );
140         uint64_t ctr_dll = m_service->getCycleTimerTicks();
141
142         if(err) {
143             debugError("CTR read error\n");
144         }
145         debugOutput ( DEBUG_LEVEL_VERBOSE,
146                     "Cycle timer: %011llu (%03us %04ucy %04uticks)\n",
147                     ctr, (unsigned int)TICKS_TO_SECS( ctr ),
148                     (unsigned int)TICKS_TO_CYCLES( ctr ), (unsigned int)TICKS_TO_OFFSET( ctr ) );
149         debugOutput ( DEBUG_LEVEL_VERBOSE,
150                     "   from DLL: %011llu (%03us %04ucy %04uticks)\n",
151                     ctr_dll, (unsigned int)TICKS_TO_SECS( ctr_dll ),
152                     (unsigned int)TICKS_TO_CYCLES( ctr_dll ), (unsigned int)TICKS_TO_OFFSET( ctr_dll ) );
153         int64_t diff = diffTicks(ctr, ctr_dll);
154         uint64_t abs_diff;
155         if (diff < 0) {
156             abs_diff = -diff;
157         } else {
158             abs_diff = diff;
159         }
160         debugOutput ( DEBUG_LEVEL_VERBOSE,
161                     "      diff: %s%011llu (%03us %04ucy %04uticks)\n",
162                     ((int64_t)abs_diff==diff?" ":"-"), abs_diff, (unsigned int)TICKS_TO_SECS( abs_diff ),
163                     (unsigned int)TICKS_TO_CYCLES( abs_diff ), (unsigned int)TICKS_TO_OFFSET( abs_diff ) );
164         if (abs_diff > 1000) {
165             debugWarning("Alert, large diff: %lld\n", diff);
166         }
167         debugOutput ( DEBUG_LEVEL_VERBOSE,
168                     " wait...\n");
169     }
170
171     delete m_service;
172     delete test_busreset;
173     delete test_arm;
174     delete test_arm2;
175
176     printf("Bye...\n");
177
178     return EXIT_SUCCESS;
179 }
Note: See TracBrowser for help on using the browser.