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

Revision 1629, 25.2 kB (checked in by jwoithe, 14 years ago)

RME: refine shared device configuration locking. Rename some data objects for clarity. Move frequency settings into the shared configuration object since ultimately these need to be accessible to all FFADO/RME processes. Other minor cleanups.

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