summaryrefslogtreecommitdiff
path: root/gnuradio-core/src/lib/io/microtune_4702.cc
blob: 3ec072d51bc41ab6ee46d8751fd73f30689e8514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- c++-*- */
/*
 * Copyright 2001,2003,2004 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include "microtune_4702.h"
#include <stdlib.h>
#include <stdio.h>
#include "i2c.h"

static const double FIRST_IF = 36.00e6;

// The tuner internally has 3 bands: VHF Low, VHF High & UHF.
// These are the recommened boundaries
static const double VHF_High_takeover = 174e6;
static const double UHF_takeover = 470e6;

static int PLL_I2C_ADDR	    = 	0x60;

static unsigned char
control_byte_1 (bool prescaler, int reference_divisor)
{
  int c = 0x80;
  //Note: Last two divider bits (bits 2 and 3 of this byte) determined later
  if (prescaler)
    c |= 0x10;

  switch (reference_divisor){
  case 2:
    c |= 0x00;	break;
  case 4:
    c |= 0x01;	break;
  case 8:
    c |= 0x02;	break;
  case 16:
    c |= 0x03;	break;
  case 32:
    c |= 0x04;	break;
  case 64:
    c |= 0x05;	break;
  case 128:
    c |= 0x06;	break;
  case 256:
    c |= 0x07;	break;
  case 24:
    c |= 0x08;	break;
  case 5:
    c |= 0x09;	break;
  case 10:
    c |= 0x0A;	break;
  case 20:
    c |= 0x0B;	break;
  case 40:
    c |= 0x0C;	break;
  case 80:
    c |= 0x0D;	break;
  case 160:
    c |= 0x0E;	break;
  case 320:
    c |= 0x0F;	break;
  default:
    abort ();
  }
  return c;
}

static unsigned char
control_byte_2 (double target_freq)
{
  int	c;

  if (target_freq < VHF_High_takeover)	// VHF low
    c = 0x8E;

  else if (target_freq < UHF_takeover){	// VHF high
    c = 0x05;
    if (target_freq < 390e6)
      c |= 0x40;
    else
      c |= 0x80;
  }
  else {				// UHF
    c = 0x03;
    if (target_freq < 750e6)
      c |= 0x80;
    else
      c |= 0xC0;
  }

  return c;
}


microtune_4702::microtune_4702 (i2c_sptr i2c, int i2c_addr)
{
  d_i2c = i2c;
  d_i2c_addr = i2c_addr;
  d_reference_divider = 320;
  d_prescaler  = false;
}

microtune_4702::~microtune_4702 ()
{
  // nop
}

/*!
 * \brief select RF frequency to be tuned to output frequency.
 * \p target_freq is the requested frequency in Hz, \p actual_freq
 * is set to the actual frequency tuned.  It takes about 100 ms
 * for the PLL to settle.
 *
 * \returns true iff sucessful.
 */
bool
microtune_4702::set_RF_freq (double target_freq, double *p_actual_freq)
{
  unsigned char buf[4];

  double target_f_osc = target_freq + FIRST_IF;

  double f_ref = 4e6 / d_reference_divider;

  //int divisor = (int) ((target_f_osc + (f_ref * 4)) / (f_ref * 8));

  long int divisor = (long int) (target_f_osc / f_ref);
  double actual_freq = (f_ref * divisor) - FIRST_IF;
  if (p_actual_freq != 0)
    *p_actual_freq = actual_freq;

  if ((divisor & ~0x1ffff) != 0)	// >17 bit divisor
    return false;

  buf[0] = ((divisor & 0x07f00) >> 8) & 0xff;	// DB1
  buf[1] = divisor & 0xff;		// DB2
  buf[2] = control_byte_1 (d_prescaler, d_reference_divider);
  buf[2] = buf[2] | (((divisor & 0x18000) >> 10) & 0xff);
  buf[3] = control_byte_2 (target_freq);

  printf ("%x\n", PLL_I2C_ADDR);
//#if 0
  printf ("set_RF_freq: target: %g MHz actual: %g MHz %02x %02x %02x %02x\n",
	  target_freq/1e6, actual_freq/1e6, buf[0], buf[1], buf[2], buf[3]);
//#endif

  return d_i2c->write (d_i2c_addr, buf, sizeof (buf));
}

/*!
 * \returns true iff PLL is locked
 */
bool
microtune_4702::pll_locked_p ()
{
  // FIXME
  return true;
}

/*!
 * \returns the output frequency of the tuner in Hz.
 */
double
microtune_4702::get_output_freq ()
{
  return FIRST_IF;
}