Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / io / microtune_4937.cc
1 /* -*- c++-*- */
2 /*
3  * Copyright 2001,2003 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_4937.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <i2c.h>
27
28 static const double first_IF = 43.75e6;
29
30 // The tuner internally has 3 bands: VHF Low, VHF High & UHF.
31 // These are the recommened boundaries
32 static const double VHF_High_takeover = 158e6;
33 static const double UHF_takeover = 464e6;
34
35
36 static unsigned char
37 control_byte_1 (bool fast_tuning_p, int reference_divisor)
38 {
39   int c = 0x88;
40
41   if (fast_tuning_p)
42     c |= 0x40;
43
44   switch (reference_divisor){
45   case 512:
46     c |= 0x3 << 1;      break;
47   case 640:
48     c |= 0x0 << 1;      break;
49   case 1024:
50     c |= 0x1 << 1;      break;
51   default:
52     abort ();
53   }
54   return c;
55 }
56
57 static unsigned char
58 control_byte_2 (double target_freq, bool shutdown_tx_PGA)
59 {
60   int   c;
61
62   if (target_freq < VHF_High_takeover)  // VHF low
63     c = 0xa0;
64   else if (target_freq < UHF_takeover)  // VHF high
65     c = 0x90;
66   else                                  // UHF
67     c = 0x30;
68
69   if (shutdown_tx_PGA)
70     c |= 0x08;
71
72   return c;
73 }
74
75 microtune_4937::microtune_4937 (i2c_sptr i2c, int i2c_addr)
76 {
77   d_i2c = i2c;
78   d_i2c_addr = i2c_addr;
79   d_reference_divider = 640;
80   d_fast_tuning_p  = false;
81 }
82
83 microtune_4937::~microtune_4937 ()
84 {
85   // nop
86 }
87
88 /*!
89  * \brief select RF frequency to be tuned to output frequency.
90  * \p target_freq is the requested frequency in Hz, \p actual_freq
91  * is set to the actual frequency tuned.  It takes about 100 ms
92  * for the PLL to settle.
93  *
94  * \returns true iff sucessful.
95  */
96 bool
97 microtune_4937::set_RF_freq (double target_freq, double *p_actual_freq)
98 {
99   unsigned char buf[4];
100
101   double target_f_osc = target_freq + first_IF;
102   
103   double f_ref = 4e6 / d_reference_divider;
104
105   // f_osc = f_ref * 8 * divisor
106   // divisor = f_osc / (f_ref * 8)
107
108   int divisor = (int) ((target_f_osc + (f_ref * 4)) / (f_ref * 8));
109   double actual_freq = (f_ref * 8 * divisor) - first_IF;
110   if (p_actual_freq != 0)
111     *p_actual_freq = actual_freq;
112
113   if ((divisor & ~0x7fff) != 0) // 15 bit divisor
114     return false;
115
116   buf[0] = (divisor >> 8) & 0xff;       // DB1
117   buf[1] = divisor & 0xff;              // DB2
118   buf[2] = control_byte_1 (d_fast_tuning_p, d_reference_divider);
119   buf[3] = control_byte_2 (target_freq, true);
120
121 #if 0
122   printf ("set_RF_freq: target: %g MHz actual: %g MHz %02x %02x %02x %02x\n",
123           target_freq/1e6, actual_freq/1e6, buf[0], buf[1], buf[2], buf[3]);
124 #endif
125
126   return d_i2c->write (d_i2c_addr, buf, 4);
127 }
128
129 /*!
130  * \returns true iff PLL is locked
131  */
132 bool 
133 microtune_4937::pll_locked_p ()
134 {
135   // FIXME
136   return true;
137 }
138
139 /*!
140  * \returns the output frequency of the tuner in Hz.
141  */
142 double
143 microtune_4937::get_output_freq ()
144 {
145   return 5.75e6;        // 3x7702
146 }