root/trunk/libffado/src/rme/fireface_hw.cpp

Revision 1606, 23.1 kB (checked in by jwoithe, 14 years ago)

RME: phantom switches in mixer GUI now control the respective Fireface hardware. Tested only on FF400 to date.
RME: add some more TCO glue.

Line 
1 /*
2  * Copyright (C) 2009 by Jonathan Woithe
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 2 of the License, or
12  * (at your option) version 3 of the License.
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 /* This file implements miscellaneous lower-level hardware functions for the Fireface */
25
26 #include "rme/rme_avdevice.h"
27 #include "rme/fireface_def.h"
28
29 #include "debugmodule/debugmodule.h"
30
31 namespace Rme {
32
33 unsigned int
34 Device::multiplier_of_freq(unsigned int freq)
35 {
36   if (freq > MIN_QUAD_SPEED)
37     return 4;
38   if (freq > MIN_DOUBLE_SPEED)
39     return 2;
40   return 1;
41 }
42
43 signed int
44 Device::init_hardware(void)
45 {
46     // Initialises the device's settings structure to a known state and then
47     // sets the hardware to reflect this state.
48     //
49     // In time this function may read a cached device setup and initialise
50     // based on that.  It may also read the device configuration from the
51     // device flash and adopt that.  For now (for initial testing purposes)
52     // we'll go with a static state.
53     memset(&settings, 0, sizeof(settings));
54     settings.spdif_input_mode = FF_SWPARAM_SPDIF_INPUT_COAX;
55     settings.spdif_output_mode = FF_SWPARAM_SPDIF_OUTPUT_COAX;
56     settings.clock_mode = FF_SWPARAM_CLOCK_MODE_MASTER;
57     settings.sync_ref = FF_SWPARAM_SYNCREF_WORDCLOCK;
58     settings.input_level = FF_SWPARAM_ILEVEL_LOGAIN;
59     settings.output_level = FF_SWPARAM_OLEVEL_HIGAIN;
60
61     // A default sampling rate.  An explicit DDS frequency is not enabled
62     // by default.
63     m_software_freq = 44100;
64     m_dds_freq = 0;
65
66     if (set_hardware_params(&settings) != 0)
67         return -1;
68
69     // Also configure the TCO (Time Code Option) settings for those devices
70     // which have a TCO.
71     if (tco_present) {
72         memset(&tco_settings, 0, sizeof(tco_settings));
73         return write_tco_settings(&tco_settings);
74     }
75
76     return 0;
77 }
78
79 signed int
80 Device::get_hardware_status(unsigned int *stat0, unsigned int *stat1)
81 {
82     unsigned int buf[2];
83     if (readBlock(RME_FF_STATUS_REG0, buf, 2) != 0)
84         return -1;
85     *stat0 = buf[0];
86     *stat1 = buf[1];
87     return 0;
88 }
89
90 signed int
91 Device::get_hardware_streaming_status(unsigned int *stat, unsigned int n)
92 {
93     // Get the hardware status as it applies to the streaming system.  This
94     // involves a request of 4 quadlets from the status register.  It
95     // appears that the first register's definition is slightly different in
96     // this situation compared to when only 2 quadlets are requested as is
97     // done in get_hardware_status().
98     //
99     // "n" is the size of the passed-in stat array.  It must be >= 4.
100     if (n < 4)
101         return -1;
102     if (readBlock(RME_FF_STATUS_REG0, stat, 4) != 0)
103         return -1;
104     return 0;
105 }
106
107 signed int
108 Device::get_hardware_state(FF_state_t *state)
109 {
110     // Retrieve the hardware status and deduce the device state.  Return
111     // -1 on error, 0 on success.  The given state structure will be
112     // cleared by this call.
113     unsigned int stat0, stat1;
114     memset(state, 0, sizeof(*state));
115     if (get_hardware_status(&stat0, &stat1) != 0)
116         return -1;
117
118     state->is_streaming = (stat0 & SR0_IS_STREAMING) != 0;
119
120     state->clock_mode = (settings.clock_mode == FF_SWPARAM_CLOCK_MODE_MASTER)?FF_STATE_CLOCKMODE_MASTER:FF_STATE_CLOCKMODE_AUTOSYNC;
121
122     switch (stat0 & SR0_AUTOSYNC_SRC_MASK) {
123         case SR0_AUTOSYNC_SRC_ADAT1:
124             state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT1;
125             break;
126         case SR0_AUTOSYNC_SRC_ADAT2:
127             state->autosync_source = FF_STATE_AUTOSYNC_SRC_ADAT2;
128             break;
129         case SR0_AUTOSYNC_SRC_SPDIF:
130             state->autosync_source = FF_STATE_AUTOSYNC_SRC_SPDIF;
131             break;
132         case SR0_AUTOSYNC_SRC_WCLK:
133             state->autosync_source = FF_STATE_AUTOSYNC_SRC_WCLK;
134             break;
135         case SR0_AUTOSYNC_SRC_TCO:
136             state->autosync_source = FF_STATE_AUTOSYNC_SRC_TCO;
137             break;
138         default: state->autosync_source = FF_STATE_AUTOSYNC_SRC_NOLOCK;
139     }
140
141     switch (stat0 & SR0_AUTOSYNC_FREQ_MASK) {
142         case SR0_AUTOSYNC_FREQ_32k:  state->autosync_freq = 32000; break;
143         case SR0_AUTOSYNC_FREQ_44k1: state->autosync_freq = 44100; break;
144         case SR0_AUTOSYNC_FREQ_48k:  state->autosync_freq = 48000; break;
145         case SR0_AUTOSYNC_FREQ_64k:  state->autosync_freq = 64000; break;
146         case SR0_AUTOSYNC_FREQ_88k2: state->autosync_freq = 88200; break;
147         case SR0_AUTOSYNC_FREQ_96k:  state->autosync_freq = 96000; break;
148         case SR0_AUTOSYNC_FREQ_128k: state->autosync_freq = 128000; break;
149         case SR0_AUTOSYNC_FREQ_176k4:state->autosync_freq = 176400; break;
150         case SR0_AUTOSYNC_FREQ_192k: state->autosync_freq = 192000; break;
151     }
152
153     switch (stat0 & SR0_SPDIF_FREQ_MASK) {
154         case SR0_SPDIF_FREQ_32k:  state->spdif_freq = 32000; break;
155         case SR0_SPDIF_FREQ_44k1: state->spdif_freq = 41000; break;
156         case SR0_SPDIF_FREQ_48k:  state->spdif_freq = 48000; break;
157         case SR0_SPDIF_FREQ_64k:  state->spdif_freq = 64000; break;
158         case SR0_SPDIF_FREQ_88k2: state->spdif_freq = 88200; break;
159         case SR0_SPDIF_FREQ_96k:  state->spdif_freq = 96000; break;
160         case SR0_SPDIF_FREQ_128k: state->spdif_freq = 128000; break;
161         case SR0_SPDIF_FREQ_176k4:state->spdif_freq = 176400; break;
162         case SR0_SPDIF_FREQ_192k: state->spdif_freq = 192000; break;
163     }
164
165     switch (stat0 & SR0_ADAT1_STATUS_MASK) {
166         case SR0_ADAT1_STATUS_NOLOCK:
167             state->adat1_sync_status = FF_STATE_SYNC_NOLOCK; break;
168         case SR0_ADAT1_STATUS_LOCK:
169             state->adat1_sync_status = FF_STATE_SYNC_LOCKED; break;
170         case SR0_ADAT1_STATUS_SYNC:
171             state->adat1_sync_status = FF_STATE_SYNC_SYNCED; break;
172     }
173     switch (stat0 & SR0_ADAT2_STATUS_MASK) {
174         case SR0_ADAT2_STATUS_NOLOCK:
175             state->adat2_sync_status = FF_STATE_SYNC_NOLOCK; break;
176         case SR0_ADAT2_STATUS_LOCK:
177             state->adat2_sync_status = FF_STATE_SYNC_LOCKED; break;
178         case SR0_ADAT2_STATUS_SYNC:
179             state->adat2_sync_status = FF_STATE_SYNC_SYNCED; break;
180     }
181     switch (stat0 & SR0_SPDIF_STATUS_MASK) {
182         case SR0_SPDIF_STATUS_NOLOCK:
183             state->spdif_sync_status = FF_STATE_SYNC_NOLOCK; break;
184         case SR0_SPDIF_STATUS_LOCK:
185             state->spdif_sync_status = FF_STATE_SYNC_LOCKED; break;
186         case SR0_SPDIF_STATUS_SYNC:
187             state->spdif_sync_status = FF_STATE_SYNC_SYNCED; break;
188     }
189     switch (stat0 & SR0_WCLK_STATUS_MASK) {
190         case SR0_WCLK_STATUS_NOLOCK:
191             state->wclk_sync_status = FF_STATE_SYNC_NOLOCK; break;
192         case SR0_WCLK_STATUS_LOCK:
193             state->wclk_sync_status = FF_STATE_SYNC_LOCKED; break;
194         case SR0_WCLK_STATUS_SYNC:
195             state->wclk_sync_status = FF_STATE_SYNC_SYNCED; break;
196     }
197     switch (stat1 & SR1_TCO_STATUS_MASK) {
198        case SR1_TCO_STATUS_NOLOCK:
199            state->tco_sync_status = FF_STATE_SYNC_NOLOCK; break;
200        case SR1_TCO_STATUS_LOCK:
201            state->tco_sync_status = FF_STATE_SYNC_LOCKED; break;
202        case SR1_TCO_STATUS_SYNC:
203            state->tco_sync_status = FF_STATE_SYNC_SYNCED; break;
204     }
205
206     return 0;
207 }
208
209 signed int
210 Device::set_hardware_params(FF_software_settings_t *use_settings)
211 {
212     // Initialises the hardware to the state defined by the supplied
213     // software settings structure (which will usually be the device's
214     // "settings" structure).  This has the side effect of extinguishing the
215     // "Host" LED on the FF400 when done for the first time after the
216     // interface has been powered up.
217     //
218     // If use_settings is NULL, the device's current settings structure will
219     // be used to source the configuration information.
220
221     FF_software_settings_t *sw_settings;
222     quadlet_t data[3] = {0, 0, 0};
223     unsigned int conf_reg;
224
225     if (use_settings == NULL)
226       sw_settings = &settings;
227     else
228       sw_settings = use_settings;
229
230     if (sw_settings->mic_phantom[0])
231       data[0] |= CR0_PHANTOM_MIC0;
232     if (sw_settings->mic_phantom[1])
233       data[0] |= CR0_PHANTOM_MIC1;
234     if (sw_settings->mic_phantom[2])
235       data[0] |= CR0_PHANTOM_MIC2;
236     if (sw_settings->mic_phantom[3])
237       data[0] |= CR0_PHANTOM_MIC3;
238
239     /* Phones level */
240     switch (sw_settings->phones_level) {
241         case FF_SWPARAM_PHONESLEVEL_HIGAIN:
242             data[0] |= CRO_PHLEVEL_HIGAIN;
243             break;
244         case FF_SWPARAM_PHONESLEVEL_4dBU:
245             data[0] |= CR0_PHLEVEL_4dBU;
246             break;
247         case FF_SWPARAM_PHONESLEVEL_m10dBV:
248             data[0] |= CRO_PHLEVEL_m10dBV;
249             break;
250     }
251
252     /* Input level */
253     switch (sw_settings->input_level) {
254         case FF_SWPARAM_ILEVEL_LOGAIN: // Low gain
255             data[1] |= CR1_ILEVEL_CPLD_LOGAIN;    // CPLD
256             data[0] |= CR0_ILEVEL_FPGA_LOGAIN;    // LED control (used on FF800 only)
257             break;
258         case FF_SWPARAM_ILEVEL_4dBU:   // +4 dBu
259             data[1] |= CR1_ILEVEL_CPLD_4dBU;
260             data[0] |= CR0_ILEVEL_FPGA_4dBU;
261             break;
262         case FF_SWPARAM_ILEVEL_m10dBV: // -10 dBV
263             data[1] |= CR1_ILEVEL_CPLD_m10dBV;
264             data[0] |= CR0_ILEVEL_FPGA_m10dBV;
265             break;
266     }
267
268     /* Output level */
269     switch (sw_settings->output_level) {
270         case FF_SWPARAM_OLEVEL_HIGAIN: // High gain
271             data[1] |= CR1_OLEVEL_CPLD_HIGAIN;   // CPLD
272             data[0] |= CR0_OLEVEL_FPGA_HIGAIN;   // LED control (used on FF800 only)
273             break;
274         case FF_SWPARAM_OLEVEL_4dBU:   // +4 dBu
275             data[1] |= CR1_OLEVEL_CPLD_4dBU;
276             data[0] |= CR0_OLEVEL_FPGA_4dBU;
277             break;
278         case FF_SWPARAM_OLEVEL_m10dBV: // -10 dBV
279             data[1] |= CR1_OLEVEL_CPLD_m10dBV;
280             data[0] |= CR0_OLEVEL_FPGA_m10dBV;
281             break;
282     }
283
284     /* Set input options.  The meaning of the options differs between
285      * devices, so we use the generic identifiers here.
286      */
287     data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT1_A : 0;
288     data[1] |= (sw_settings->input_opt[1] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT1_B : 0;
289     data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_A) ? CR1_INPUT_OPT2_A : 0;
290     data[1] |= (sw_settings->input_opt[2] & FF_SWPARAM_INPUT_OPT_B) ? CR1_INPUT_OPT2_B : 0;
291
292     // Drive the speaker emulation / filter LED via FPGA
293     data[0] |= (sw_settings->filter) ? CR0_FILTER_FPGA : 0;
294
295     // Set the "rear" option for input 0 if selected
296     data[1] |= (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_REAR) ? CR1_FF800_INPUT1_REAR : 0;
297
298     // The input 0 "front" option is activated using one of two bits
299     // depending on whether the filter (aka "speaker emulation") setting is
300     // active.
301     if (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_FRONT) {
302         data[1] |= (sw_settings->filter) ? CR1_FF800_INPUT1_FRONT_WITH_FILTER : CR1_FF800_INPUT1_FRONT;
303     }
304
305     data[2] |= (sw_settings->spdif_output_emphasis==FF_SWPARAM_SPDIF_OUTPUT_EMPHASIS_ON) ? CR2_SPDIF_OUT_EMP : 0;
306     data[2] |= (sw_settings->spdif_output_pro==FF_SWPARAM_SPDIF_OUTPUT_PRO_ON) ? CR2_SPDIF_OUT_PRO : 0;
307     data[2] |= (sw_settings->spdif_output_nonaudio==FF_SWPARAM_SPDIF_OUTPUT_NONAUDIO_ON) ? CR2_SPDIF_OUT_NONAUDIO : 0;
308     data[2] |= (sw_settings->spdif_output_mode==FF_SWPARAM_SPDIF_OUTPUT_OPTICAL) ? CR2_SPDIF_OUT_ADAT2 : 0;
309     data[2] |= (sw_settings->clock_mode==FF_SWPARAM_CLOCK_MODE_AUTOSYNC) ? CR2_CLOCKMODE_AUTOSYNC : CR2_CLOCKMODE_MASTER;
310     data[2] |= (sw_settings->spdif_input_mode==FF_SWPARAM_SPDIF_INPUT_COAX) ? CR2_SPDIF_IN_COAX : CR2_SPDIF_IN_ADAT2;
311     data[2] |= (sw_settings->word_clock_single_speed=FF_SWPARAM_WORD_CLOCK_1x) ? CR2_WORD_CLOCK_1x : 0;
312
313     /* TMS / TCO toggle bits in CR2 are not set by other drivers */
314
315     /* Drive / fuzz */
316     if (sw_settings->fuzz)
317       data[0] |= CR0_INSTR_DRIVE_FPGA; // FPGA LED control
318     else
319       data[1] |= CR1_INSTR_DRIVE;      // CPLD
320
321     /* Drop-and-stop is hardwired on in other drivers */
322     data[2] |= CR2_DROP_AND_STOP;
323
324     if (m_rme_model == RME_MODEL_FIREFACE400) {
325         data[2] |= CR2_FF400_BIT;
326     }
327
328     switch (sw_settings->sync_ref) {
329         case FF_SWPARAM_SYNCREF_WORDCLOCK:
330             data[2] |= CR2_SYNC_WORDCLOCK;
331             break;
332         case FF_SWPARAM_SYNCREF_ADAT1:
333             data[2] |= CR2_SYNC_ADAT1;
334             break;
335         case FF_SWPARAM_SYNCREF_ADAT2:
336             data[2] |= CR2_SYNC_ADAT2;
337             break;
338         case FF_SWPARAM_SYNCREF_SPDIF:
339             data[2] |= CR2_SYNC_SPDIF;
340             break;
341         case FF_SWPARAM_SYNCREC_TCO:
342             data[2] |= CR2_SYNC_TCO;
343             break;
344     }
345
346     // This is hardwired in other drivers
347     data[2] |= (CR2_FREQ0 + CR2_FREQ1 + CR2_DSPEED + CR2_QSSPEED);
348
349     // The FF800 limiter applies to the front panel instrument input, so it
350     // only makes sense that it is disabled when that input is in use.
351     data[2] |= (sw_settings->limiter_disable &&
352                 (sw_settings->input_opt[0] & FF_SWPARAM_FF800_INPUT_OPT_FRONT)) ?
353                 CR2_DISABLE_LIMITER : 0;
354
355 //This is just for testing - it's a known consistent configuration
356 //data[0] = 0x00020811;      // Phantom off
357 //data[0] = 0x00020811;      // Phantom on
358 //data[1] = 0x0000031e;
359 //data[2] = 0xc400101f;
360     debugOutput(DEBUG_LEVEL_VERBOSE, "set hardware registers: 0x%08x 0x%08x 0x%08x\n",
361       data[0], data[1], data[2]);
362
363     conf_reg = (m_rme_model==RME_MODEL_FIREFACE800)?RME_FF800_CONF_REG:RME_FF400_CONF_REG;
364     if (writeBlock(conf_reg, data, 3) != 0)
365         return -1;
366
367     return -0;
368 }
369
370 signed int
371 Device::read_tco(quadlet_t *tco_data, signed int size)
372 {
373     // Read the TCO registers and return the respective values in *tco_data.
374     // Return value is 0 on success, or -1 if there is no TCO present.
375     // "size" is the size (in quadlets) of the array pointed to by tco_data.
376     // To obtain all TCO data "size" should be at least 4.  If the caller
377     // doesn't care about the data returned by the TCO, tco_data can be
378     // NULL.
379     quadlet_t buf[4];
380     signed int i;
381
382     // The Fireface 400 can't have the TCO fitted
383     if (m_rme_model==RME_MODEL_FIREFACE400)
384         return -1;
385
386     if (readBlock(RME_FF_TCO_READ_REG, buf, 4) != 0)
387         return -1;
388
389     if (tco_data != NULL) {
390         for (i=0; i<(size<4)?size:4; i++)
391             tco_data[i] = buf[i];
392     }
393
394     if ( (buf[0] & 0x80808080) == 0x80808080 &&
395          (buf[1] & 0x80808080) == 0x80808080 &&
396          (buf[2] & 0x80808080) == 0x80808080 &&
397          (buf[3] & 0x8000FFFF) == 0x80008000) {
398         // A TCO is present
399         return 0;
400     }
401
402     return -1;
403 }
404
405 signed int
406 Device::write_tco(quadlet_t *tco_data, signed int size)
407 {
408     // Writes data to the TCO.  No check is made as to whether a TCO is
409     // present in the current device.  Return value is 0 on success or -1 on
410     // error.  "size" is the size (in quadlets) of the data pointed to by
411     // "tco_data".  The first 4 quadlets of tco_data are significant; all
412     // others are ignored.  If fewer than 4 quadlets are supplied (as
413     // indicated by the "size" parameter, -1 will be returned.
414     if (size < 4)
415         return -1;
416
417     // Don't bother trying to write if the device is a FF400 since the TCO
418     // can't be fitted to this device.
419     if (m_rme_model==RME_MODEL_FIREFACE400)
420         return -1;
421
422     if (writeBlock(RME_FF_TCO_WRITE_REG, tco_data, 4) != 0)
423         return -1;
424
425     return 0;
426 }
427
428 signed int
429 Device::hardware_is_streaming(void)
430 {
431     // Return 1 if the hardware is streaming, 0 if not.
432     unsigned int s1, s2;
433     if (get_hardware_status(&s1, &s2) != 0)
434         return 0;
435     return (s1 & SR0_IS_STREAMING) != 0;
436 }
437
438 signed int
439 Device::read_tco_state(FF_TCO_state_t *tco_state)
440 {
441     // Reads the current TCO state into the supplied state structure
442
443     quadlet_t tc[4];
444     unsigned int PLL_phase;
445
446     if (read_tco(tc, 4) != 0)
447       return -1;
448
449     // The timecode is stored in BCD (binary coded decimal) in register 0.
450     tco_state->frames = (tc[0] & 0xf) + ((tc[0] & 0x30) >> 4)*10;
451     tco_state->seconds = ((tc[0] & 0xf00) >> 8) + ((tc[0] & 0x7000) >> 12)*10;
452     tco_state->minutes = ((tc[0] & 0xf0000) >> 16) + ((tc[0] & 0x700000) >> 20)*10;
453     tco_state->hours = ((tc[0] & 0xf000000) >> 24) + ((tc[0] & 0x30000000) >> 28)*10;
454
455     tco_state->locked = (tc[1] & FF_TCO1_TCO_lock) != 0;
456     tco_state->ltc_valid = (tc[1] & FF_TCO1_LTC_INPUT_VALID) != 0;
457
458     switch (tc[1] & FF_TCO1_LTC_FORMAT_MASK) {
459         case FF_TC01_LTC_FORMAT_24fps:
460           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_24fps; break;
461         case FF_TCO1_LTC_FORMAT_25fps:
462           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_25fps; break;
463         case FF_TC01_LTC_FORMAT_29_97fps:
464           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_29_97fps; break;
465         case FF_TCO1_LTC_FORMAT_30fps:
466           tco_state->frame_rate = FF_TCOSTATE_FRAMERATE_30fps; break;
467     }
468
469     tco_state->drop_frame = (tc[1] & FF_TCO1_SET_DROPFRAME) != 0;
470
471     switch (tc[1] & FF_TCO1_VIDEO_INPUT_MASK) {
472         case FF_TCO1_VIDEO_INPUT_NTSC:
473             tco_state->video_input = FF_TCOSTATE_VIDEO_NTSC; break;
474         case FF_TCO1_VIDEO_INPUT_PAL:
475             tco_state->video_input = FF_TCOSTATE_VIDEO_PAL; break;
476         default:
477             tco_state->video_input = FF_TCOSTATE_VIDEO_NONE;
478     }
479
480     if ((tc[1] & FF_TCO1_WORD_CLOCK_INPUT_VALID) == 0) {
481         tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_NONE;
482     } else {
483         switch (tc[1] & FF_TCO1_WORD_CLOCK_INPUT_MASK) {
484             case FF_TCO1_WORD_CLOCK_INPUT_1x:
485                 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_1x; break;
486             case FF_TCO1_WORD_CLOCK_INPUT_2x:
487                 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_2x; break;
488             case FF_TCO1_WORD_CLOCK_INPUT_4x:
489                 tco_state->word_clock_state = FF_TCOSTATE_WORDCLOCK_4x; break;
490         }
491     }
492
493     PLL_phase = (tc[2] & 0x7f) + ((tc[2] & 0x7f00) >> 1);
494     tco_state->sample_rate = (25000000.0 * 16.0)/PLL_phase;
495
496     return 0;
497 }
498
499 signed int
500 Device::write_tco_settings(FF_TCO_settings_t *tco_settings)
501 {
502     // Writes the supplied application-level settings to the device's TCO
503     // (Time Code Option).  Don't bother doing anything if the device doesn't
504     // have a TCO fitted.  Returns 0 on success, -1 on error.
505
506     quadlet_t tc[4] = {0, 0, 0, 0};
507
508     if (!tco_present) {
509         return -1;
510     }
511
512     if (tco_settings->MTC)
513         tc[0] |= FF_TCO0_MTC;
514
515     switch (tco_settings->input) {
516         case FF_TCOPARAM_INPUT_LTC:
517             tc[2] |= FF_TCO2_INPUT_LTC; break;
518         case FF_TCOPARAM_INPUT_VIDEO:
519             tc[2] |= FF_TCO2_INPUT_VIDEO; break;
520         case FF_TCOPARAM_INPUT_WCK:
521             tc[2] |= FF_TCO2_INPUT_WORD_CLOCK; break;
522     }
523
524     switch (tco_settings->frame_rate) {
525         case FF_TCOPARAM_FRAMERATE_24fps:
526             tc[1] |= FF_TC01_LTC_FORMAT_24fps; break;
527         case FF_TCOPARAM_FRAMERATE_25fps:
528             tc[1] |= FF_TCO1_LTC_FORMAT_25fps; break;
529         case FF_TCOPARAM_FRAMERATE_29_97fps:
530             tc[1] |= FF_TC01_LTC_FORMAT_29_97fps; break;
531         case FF_TCOPARAM_FRAMERATE_29_97dfps:
532             tc[1] |= FF_TCO1_LTC_FORMAT_29_97dpfs; break;
533         case FF_TCOPARAM_FRAMERATE_30fps:
534             tc[1] |= FF_TCO1_LTC_FORMAT_30fps; break;
535         case FF_TCOPARAM_FRAMERATE_30dfps:
536             tc[1] |= FF_TCO1_LTC_FORMAT_30dfps; break;
537     }
538
539     switch (tco_settings->word_clock) {
540         case FF_TCOPARAM_WORD_CLOCK_CONV_1_1:
541             tc[2] |= FF_TCO2_WORD_CLOCK_CONV_1_1; break;
542         case FF_TCOPARAM_WORD_CLOCK_CONV_44_48:
543             tc[2] |= FF_TCO2_WORD_CLOCK_CONV_44_48; break;
544         case FF_TCOPARAM_WORD_CLOCK_CONV_48_44:
545             tc[2] |= FF_TCO2_WORD_CLOCK_CONV_48_44; break;
546     }
547
548     switch (tco_settings->sample_rate) {
549         case FF_TCOPARAM_SRATE_44_1:
550             tc[2] |= FF_TCO2_SRATE_44_1; break;
551         case FF_TCOPARAM_SRATE_48:
552             tc[2] |= FF_TCO2_SRATE_48; break;
553         case FF_TCOPARAM_SRATE_FROM_APP:
554             tc[2] |= FF_TCO2_SRATE_FROM_APP; break;
555     }
556
557     switch (tco_settings->pull) {
558         case FF_TCPPARAM_PULL_NONE:
559             tc[2] |= FF_TCO2_PULL_0; break;
560         case FF_TCOPARAM_PULL_UP_01:
561             tc[2] |= FF_TCO2_PULL_UP_01; break;
562         case FF_TCOPARAM_PULL_DOWN_01:
563             tc[2] |= FF_TCO2_PULL_DOWN_01; break;
564         case FF_TCOPARAM_PULL_UP_40:
565             tc[2] |= FF_TCO2_PULL_UP_40; break;
566         case FF_TCOPARAM_PULL_DOWN_40:
567             tc[2] |= FF_TCO2_PULL_DOWN_40; break;
568     }
569
570     if (tco_settings->termination == FF_TCOPARAM_TERMINATION_ON)
571         tc[2] |= FF_TCO2_SET_TERMINATION;
572
573     return write_tco(tc, 4);
574
575     return 0;
576 }
577
578 signed int
579 Device::set_hardware_dds_freq(signed int freq)
580 {
581     // Set the device's DDS to the given frequency (which in turn determines
582     // the sampling frequency).  Returns 0 on success, -1 on error.
583
584     unsigned int ret = 0;
585
586     if (freq < MIN_SPEED || freq > MAX_SPEED)
587         return -1;
588
589     if (m_rme_model == RME_MODEL_FIREFACE400)
590         ret = writeRegister(RME_FF400_STREAM_SRATE, freq);
591     else
592         ret = writeRegister(RME_FF800_STREAM_SRATE, freq);
593
594     return ret;
595 }
596
597 signed int
598 Device::hardware_init_streaming(unsigned int sample_rate,
599     unsigned int tx_channel)
600 {
601     // tx_channel is the ISO channel the PC will transmit on.
602     quadlet_t buf[4];
603     fb_nodeaddr_t addr;
604     unsigned int size;
605
606     buf[0] = sample_rate;
607     buf[1] = (num_channels << 11) + tx_channel;
608     buf[2] = num_channels;
609     buf[3] = 0;
610     buf[4] = 0;
611     if (speed800) {
612         buf[2] |= RME_FF800_STREAMING_SPEED_800;
613     }
614
615     if (m_rme_model == RME_MODEL_FIREFACE400) {
616         addr = RME_FF400_STREAM_INIT_REG;
617         size = RME_FF400_STREAM_INIT_SIZE;
618     } else {
619         addr = RME_FF800_STREAM_INIT_REG;
620         size = RME_FF800_STREAM_INIT_SIZE;
621     }
622
623     return writeBlock(addr, buf, size);
624 }
625
626 signed int
627 Device::hardware_start_streaming(unsigned int listen_channel)
628 {
629     // Listen_channel is the ISO channel the PC will listen on for data sent
630     // by the Fireface.
631     fb_nodeaddr_t addr;
632     quadlet_t data = num_channels;
633
634     if (m_rme_model == RME_MODEL_FIREFACE400) {
635         addr = RME_FF400_STREAM_START_REG;
636         data |= (listen_channel << 5);
637     } else {
638         addr = RME_FF800_STREAM_START_REG;
639         if (speed800)
640             data |= RME_FF800_STREAMING_SPEED_800; // Flag 800 Mbps speed
641     }
642
643     return writeRegister(addr, data);
644 }
645
646 signed int
647 Device::hardware_stop_streaming(void)
648 {
649     fb_nodeaddr_t addr;
650     quadlet_t buf[4] = {0, 0, 0, 1};
651     unsigned int size;
652
653     if (m_rme_model == RME_MODEL_FIREFACE400) {
654       addr = RME_FF400_STREAM_END_REG;
655       size = RME_FF400_STREAM_END_SIZE;
656     } else {
657       addr = RME_FF800_STREAM_END_REG;
658       size = RME_FF800_STREAM_END_SIZE;
659     }
660
661     return writeBlock(addr, buf, size);
662 }
663
664 }
Note: See TracBrowser for help on using the browser.