Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / io / microtune_xxxx_eval_board.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2001,2004 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "microtune_xxxx_eval_board.h"
24 #include "microtune_eval_board_defs.h"
25 #include "microtune_xxxx.h"
26 #include "ppio.h"
27 #include "i2c_bitbang.h"
28 #include "i2c_bbio_pp.h"
29 #include <cmath>
30
31 static int AGC_DAC_I2C_ADDR =   0x2C;
32
33 microtune_xxxx_eval_board::microtune_xxxx_eval_board (int which_pp)
34 {
35   d_ppio = make_ppio (which_pp);
36   d_i2c = make_i2c_bitbang (make_i2c_bbio_pp (d_ppio));
37   d_tuner = 0;
38 }
39
40 microtune_xxxx_eval_board::~microtune_xxxx_eval_board ()
41 {
42   delete d_tuner;
43   d_tuner = 0;
44 }
45
46
47 //! is the eval board present?
48 bool 
49 microtune_xxxx_eval_board::board_present_p ()
50 {
51   bool result = true;
52   d_ppio->lock ();
53
54   int t = d_ppio->read_status ();
55   if ((t & UT_SP_SHOULD_BE_ZERO) != 0
56       || (t & UT_SP_SHOULD_BE_ONE) != UT_SP_SHOULD_BE_ONE)
57     result = false;
58
59   // could also see if SCL is looped back or not, but that seems like overkill
60
61   d_ppio->unlock ();
62   return result;
63 }
64
65 /*
66  * ----------------------------------------------------------------
67  *                          AGC stuff
68  *
69  * We're using a MAX518 8-bit 5V dual dac for setting the AGC's
70  * ----------------------------------------------------------------
71  */
72 void 
73 microtune_xxxx_eval_board::write_dac (int which, int value)
74 {
75   unsigned char cmd[2];
76   cmd[0] = which & 1;
77   cmd[1] = value;
78   d_i2c->write (AGC_DAC_I2C_ADDR, cmd, sizeof (cmd));
79 }
80
81 void 
82 microtune_xxxx_eval_board::write_both_dacs (int value0, int value1)
83 {
84   unsigned char cmd[4];
85   cmd[0] = 0;
86   cmd[1] = value0;
87   cmd[2] = 1;
88   cmd[3] = value1;
89   d_i2c->write (AGC_DAC_I2C_ADDR, cmd, sizeof (cmd));
90 }
91
92 static int scale_volts (float volts)
93 {
94   int   n;
95   n = (int) rint (volts * (256 / 5.0));
96   if (n < 0)
97     n = 0;
98   if (n > 255)
99     n = 255;
100
101   return n;
102 }
103
104 void
105 microtune_xxxx_eval_board::set_RF_AGC_voltage (float volts)
106 {
107   write_dac (0, scale_volts (volts));
108 }
109
110 void 
111 microtune_xxxx_eval_board::set_IF_AGC_voltage (float volts)
112 {
113   write_dac (1, scale_volts (volts));
114 }
115
116 // delegate to tuner
117
118 bool
119 microtune_xxxx_eval_board::set_RF_freq (double freq, double *actual_freq)
120 {
121   return d_tuner->set_RF_freq (freq, actual_freq);
122 }
123   
124 double
125 microtune_xxxx_eval_board::set_RF_freq (double freq)
126 {
127   return d_tuner->set_RF_freq (freq);
128 }
129
130 bool
131 microtune_xxxx_eval_board::pll_locked_p ()
132 {
133   return d_tuner->pll_locked_p ();
134 }
135
136 double
137 microtune_xxxx_eval_board::get_output_freq ()
138 {
139   return d_tuner->get_output_freq ();
140 }