-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrtl2832u.js
261 lines (241 loc) · 7.8 KB
/
rtl2832u.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* Operations on the RTL2832U demodulator.
* @param {ConnectionHandle} conn The USB connection handle.
* @param {number} ppm The frequency correction factor, in parts per million.
* @param {number=} opt_gain The optional gain in dB. If unspecified or null, sets auto gain.
* @constructor
*/
function RTL2832U(conn, ppm, opt_gain) {
/**
* Frequency of the oscillator crystal.
*/
var XTAL_FREQ = 28800000;
/**
* Tuner intermediate frequency.
*/
var IF_FREQ = 3570000;
/**
* The number of bytes for each sample.
*/
var BYTES_PER_SAMPLE = 2;
/**
* Communications with the demodulator via USB.
*/
var com = new RtlCom(conn);
/**
* A handler for errors. It's a function that receives the error message.
*/
var errorHandler;
/**
* The tuner used by the dongle.
*/
var tuner;
/**
* Initialize the demodulator.
* @param {Function} kont The continuation for this function.
*/
function open(kont) {
com.writeEach([
[CMD.REG, BLOCK.USB, REG.SYSCTL, 0x09, 1],
[CMD.REG, BLOCK.USB, REG.EPA_MAXPKT, 0x0200, 2],
[CMD.REG, BLOCK.USB, REG.EPA_CTL, 0x0210, 2]
], function() {
com.iface.claim(function() {
com.writeEach([
[CMD.REG, BLOCK.SYS, REG.DEMOD_CTL_1, 0x22, 1],
[CMD.REG, BLOCK.SYS, REG.DEMOD_CTL, 0xe8, 1],
[CMD.DEMODREG, 1, 0x01, 0x14, 1],
[CMD.DEMODREG, 1, 0x01, 0x10, 1],
[CMD.DEMODREG, 1, 0x15, 0x00, 1],
[CMD.DEMODREG, 1, 0x16, 0x0000, 2],
[CMD.DEMODREG, 1, 0x16, 0x00, 1],
[CMD.DEMODREG, 1, 0x17, 0x00, 1],
[CMD.DEMODREG, 1, 0x18, 0x00, 1],
[CMD.DEMODREG, 1, 0x19, 0x00, 1],
[CMD.DEMODREG, 1, 0x1a, 0x00, 1],
[CMD.DEMODREG, 1, 0x1b, 0x00, 1],
[CMD.DEMODREG, 1, 0x1c, 0xca, 1],
[CMD.DEMODREG, 1, 0x1d, 0xdc, 1],
[CMD.DEMODREG, 1, 0x1e, 0xd7, 1],
[CMD.DEMODREG, 1, 0x1f, 0xd8, 1],
[CMD.DEMODREG, 1, 0x20, 0xe0, 1],
[CMD.DEMODREG, 1, 0x21, 0xf2, 1],
[CMD.DEMODREG, 1, 0x22, 0x0e, 1],
[CMD.DEMODREG, 1, 0x23, 0x35, 1],
[CMD.DEMODREG, 1, 0x24, 0x06, 1],
[CMD.DEMODREG, 1, 0x25, 0x50, 1],
[CMD.DEMODREG, 1, 0x26, 0x9c, 1],
[CMD.DEMODREG, 1, 0x27, 0x0d, 1],
[CMD.DEMODREG, 1, 0x28, 0x71, 1],
[CMD.DEMODREG, 1, 0x29, 0x11, 1],
[CMD.DEMODREG, 1, 0x2a, 0x14, 1],
[CMD.DEMODREG, 1, 0x2b, 0x71, 1],
[CMD.DEMODREG, 1, 0x2c, 0x74, 1],
[CMD.DEMODREG, 1, 0x2d, 0x19, 1],
[CMD.DEMODREG, 1, 0x2e, 0x41, 1],
[CMD.DEMODREG, 1, 0x2f, 0xa5, 1],
[CMD.DEMODREG, 0, 0x19, 0x05, 1],
[CMD.DEMODREG, 1, 0x93, 0xf0, 1],
[CMD.DEMODREG, 1, 0x94, 0x0f, 1],
[CMD.DEMODREG, 1, 0x11, 0x00, 1],
[CMD.DEMODREG, 1, 0x04, 0x00, 1],
[CMD.DEMODREG, 0, 0x61, 0x60, 1],
[CMD.DEMODREG, 0, 0x06, 0x80, 1],
[CMD.DEMODREG, 1, 0xb1, 0x1b, 1],
[CMD.DEMODREG, 0, 0x0d, 0x83, 1]
], function() {
var xtalFreq = Math.floor(XTAL_FREQ * (1 + ppm / 1000000));
com.i2c.open(function() {
R820T.check(com, function(found) {
if (found) {
tuner = new R820T(com, xtalFreq, throwError);
}
if (!tuner) {
throwError('Sorry, your USB dongle has an unsupported tuner chip. ' +
'Only the R820T chip is supported.');
return;
}
var multiplier = -1 * Math.floor(IF_FREQ * (1<<22) / xtalFreq);
com.writeEach([
[CMD.DEMODREG, 1, 0xb1, 0x1a, 1],
[CMD.DEMODREG, 0, 0x08, 0x4d, 1],
[CMD.DEMODREG, 1, 0x19, (multiplier >> 16) & 0x3f, 1],
[CMD.DEMODREG, 1, 0x1a, (multiplier >> 8) & 0xff, 1],
[CMD.DEMODREG, 1, 0x1b, multiplier & 0xff, 1],
[CMD.DEMODREG, 1, 0x15, 0x01, 1]
], function() {
tuner.init(function() {
setGain(opt_gain, function() {
com.i2c.close(kont);
})})})})})})})});
}
/**
* Sets the requested gain.
* @param {number|null|undefined} gain The gain in dB, or null/undefined
* for automatic gain.
* @param {Function} kont The continuation for this function.
*/
function setGain(gain, kont) {
if (gain == null) {
tuner.setAutoGain(kont);
} else {
tuner.setManualGain(gain, kont);
}
}
/**
* Set the sample rate.
* @param {number} rate The sample rate, in samples/sec.
* @param {Function} kont The continuation for this function. Receives the
* sample rate that was actually set as its first parameter.
*/
function setSampleRate(rate, kont) {
var ratio = Math.floor(XTAL_FREQ * (1 << 22) / rate);
ratio &= 0x0ffffffc;
var realRate = Math.floor(XTAL_FREQ * (1 << 22) / ratio);
var ppmOffset = -1 * Math.floor(ppm * (1 << 24) / 1000000);
com.writeEach([
[CMD.DEMODREG, 1, 0x9f, (ratio >> 16) & 0xffff, 2],
[CMD.DEMODREG, 1, 0xa1, ratio & 0xffff, 2],
[CMD.DEMODREG, 1, 0x3e, (ppmOffset >> 8) & 0x3f, 1],
[CMD.DEMODREG, 1, 0x3f, ppmOffset & 0xff, 1]
], function() {
resetDemodulator(function() {
kont(realRate);
})});
}
/**
* Resets the demodulator.
* @param {Function} kont The continuation for this function.
*/
function resetDemodulator(kont) {
com.writeEach([
[CMD.DEMODREG, 1, 0x01, 0x14, 1],
[CMD.DEMODREG, 1, 0x01, 0x10, 1]
], kont);
}
/**
* Tunes the device to the given frequency.
* @param {number} freq The frequency to tune to, in Hertz.
* @param {Function} kont The continuation for this function.
*/
function setCenterFrequency(freq, kont) {
com.i2c.open(function() {
tuner.setFrequency(freq + IF_FREQ, function() {
com.i2c.close(kont);
})});
}
/**
* Resets the sample buffer. Call this before starting to read samples.
* @param {Function} kont The continuation for this function.
*/
function resetBuffer(kont) {
com.writeEach([
[CMD.REG, BLOCK.USB, REG.EPA_CTL, 0x0210, 2],
[CMD.REG, BLOCK.USB, REG.EPA_CTL, 0x0000, 2]
], kont);
}
/**
* Reads a block of samples off the device.
* @param {number} length The number of samples to read.
* @param {Function} kont The continuation for this function. It will receive
* as its argument an ArrayBuffer containing the read samples, which you
* can interpret as pairs of unsigned 8-bit integers; the first one is
* the sample's I value, and the second one is its Q value.
*/
function readSamples(length, kont) {
com.bulk.readBuffer(length * BYTES_PER_SAMPLE, kont);
}
/**
* Stops the demodulator.
* @param {Function} kont The continuation for this function.
*/
function close(kont) {
com.i2c.open(function() {
tuner.close(function() {
com.i2c.close(function() {
com.iface.release(kont);
})})});
}
/**
* Handles an error.
* @param {string} msg The error message.
*/
function throwError(msg) {
if (errorHandler) {
errorHandler(msg);
} else {
throw msg;
}
}
/**
* Sets a function to call if there's an error communicating with the
* demodulator.
* @param {Function} func The function to call on error.
*/
function setOnError(func) {
errorHandler = func;
com.setOnError(throwError);
}
return {
open: open,
setSampleRate: setSampleRate,
setCenterFrequency: setCenterFrequency,
resetBuffer: resetBuffer,
readSamples: readSamples,
close: close,
setOnError: setOnError
};
}