-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrtlsdr.pas
376 lines (327 loc) · 13.4 KB
/
rtlsdr.pas
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* Copyright (C) 2012-2013 by Steve Markgraf <[email protected]>
* Copyright (C) 2012 by Dimitri Stolnikov <[email protected]>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* Ported to Pascal by Li Zhengji (using Pascal naming convention)
}
unit RtlSdr;
{$mode objfpc}{$H+}
{$calling cdecl}
interface
uses
Classes, SysUtils;
const
RTLLIB = 'rtlsdr.dll';
type
TRtlSdrDev = record
end;
PRtlSdrDev = ^TRtlSdrDev;
TRtlSdrTuner = (
RTLSDR_TUNER_UNKNOWN,
RTLSDR_TUNER_E4000,
RTLSDR_TUNER_FC0012,
RTLSDR_TUNER_FC0013,
RTLSDR_TUNER_FC2580,
RTLSDR_TUNER_R820T,
RTLSDR_TUNER_R828D
);
TRtlSdrReadAsyncCB = procedure(Buf: PByte; const Len: Cardinal; Ctx: Pointer);
function RtlSdrGetDeviceCount(): Cardinal; external RTLLIB name 'rtlsdr_get_device_count';
function RtlSdrGetDeviceName(Index: Cardinal): PChar; external RTLLIB name 'rtlsdr_get_device_name';
{!
* Get USB device strings.
*
* NOTE: The string arguments must provide space for up to 256 bytes.
*
* \param index the device index
* \param manufact manufacturer name, may be NULL
* \param product product name, may be NULL
* \param serial serial number, may be NULL
* \return 0 on success
}
function RtlSdrGetDeviceUSBString(const Index: Cardinal; Manufacture, Product, Serial: PChar): Integer; external RTLLIB name 'rtlsdr_get_device_usb_strings';
{!
* Get device index by USB serial string descriptor.
*
* \param serial serial string of the device
* \return device index of first device where the name matched
* \return -1 if name is NULL
* \return -2 if no devices were found at all
* \return -3 if devices were found, but none with matching name
}
function RtlSdrGetIndexBySerial(const Serial: PChar): Integer; external RTLLIB name 'rtlsdr_get_index_by_serial';
function RtlSdrOpen(var Dev: PRtlSdrDev; const Index: Cardinal): Integer; external RTLLIB name 'rtlsdr_open';
function RtlSdrClose(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_close';
{ configuration functions }
{!
* Set crystal oscillator frequencies used for the RTL2832 and the tuner IC.
*
* Usually both ICs use the same clock. Changing the clock may make sense if
* you are applying an external clock to the tuner or to compensate the
* frequency (and samplerate) error caused by the original (cheap) crystal.
*
* NOTE: Call this function only if you fully understand the implications.
*
* \param dev the device handle given by rtlsdr_open()
* \param rtl_freq frequency value used to clock the RTL2832 in Hz
* \param tuner_freq frequency value used to clock the tuner IC in Hz
* \return 0 on success
}
function RtlSdrSetXtalFreq(Dev: PRtlSdrDev; RtlFreq, TunerFreq: Cardinal): Integer; external RTLLIB name 'rtlsdr_set_xtal_freq';
{!
* Get crystal oscillator frequencies used for the RTL2832 and the tuner IC.
*
* Usually both ICs use the same clock.
*
* \param dev the device handle given by rtlsdr_open()
* \param rtl_freq frequency value used to clock the RTL2832 in Hz
* \param tuner_freq frequency value used to clock the tuner IC in Hz
* \return 0 on success
}
function RtlSdrGetXtalFreq(Dev: PRtlSdrDev; var RtlFreq, TunerFreq: Cardinal): Integer; external RTLLIB name 'rtlsdr_get_xtal_freq';
{!
* Get USB device strings.
*
* NOTE: The string arguments must provide space for up to 256 bytes.
*
* \param dev the device handle given by rtlsdr_open()
* \param manufact manufacturer name, may be NULL
* \param product product name, may be NULL
* \param serial serial number, may be NULL
* \return 0 on success
}
function RtlSdrGetUSBStrings(Dev: PRtlSdrDev; Manufacture, Product, Serial: PChar): Integer; external RTLLIB name 'rtlsdr_get_usb_strings';
{!
* Write the device EEPROM
*
* \param dev the device handle given by rtlsdr_open()
* \param data buffer of data to be written
* \param offset address where the data should be written
* \param len length of the data
* \return 0 on success
* \return -1 if device handle is invalid
* \return -2 if EEPROM size is exceeded
* \return -3 if no EEPROM was found
}
function RtlSdrWriteEEPROM(Dev: PRtlSdrDev; Data: PByte; Offset: Byte; Len: Word): Integer; external RTLLIB name 'rtlsdr_write_eeprom';
{!
* Read the device EEPROM
*
* \param dev the device handle given by rtlsdr_open()
* \param data buffer where the data should be written
* \param offset address where the data should be read from
* \param len length of the data
* \return 0 on success
* \return -1 if device handle is invalid
* \return -2 if EEPROM size is exceeded
* \return -3 if no EEPROM was found
}
function RtlSdrReadEEPROM(Dev: PRtlSdrDev; Data: PByte; Offset: Byte; Len: Word): Integer; external RTLLIB name 'rtlsdr_read_eeprom';
function RtlSdrSetCenterFreq(Dev: PRtlSdrDev; const Freq: Cardinal): Integer; external RTLLIB name 'rtlsdr_set_center_freq';
{!
* Get actual frequency the device is tuned to.
*
* \param dev the device handle given by rtlsdr_open()
* \return 0 on error, frequency in Hz otherwise
}
function RtlSdrGetCenterFreq(Dev: PRtlSdrDev): Cardinal; external RTLLIB name 'rtlsdr_get_center_freq';
{!
* Set the frequency correction value for the device.
*
* \param dev the device handle given by rtlsdr_open()
* \param ppm correction value in parts per million (ppm)
* \return 0 on success
}
function RtlSdrSetFreqCorrection(Dev: PRtlSdrDev; const PPM: Integer): Integer; external RTLLIB name 'rtlsdr_set_freq_correction';
{!
* Get actual frequency correction value of the device.
*
* \param dev the device handle given by rtlsdr_open()
* \return correction value in parts per million (ppm)
}
function RtlSdrGetFreqCorrection(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_get_freq_correction';
{!
* Get the tuner type.
*
* \param dev the device handle given by rtlsdr_open()
* \return RTLSDR_TUNER_UNKNOWN on error, tuner type otherwise
}
function RtlSdrGetTunerType(const Index: Integer): TRtlSdrTuner; overload;
function RtlSdrGetTunerType(Dev: PRtlSdrDev): TRtlSdrTuner; external RTLLIB name 'rtlsdr_get_tuner_type';
function FormatTunerType(T: TRtlSdrTuner): string;
{!
* Get a list of gains supported by the tuner.
*
* NOTE: The gains argument must be preallocated by the caller. If NULL is
* being given instead, the number of available gain values will be returned.
*
* \param dev the device handle given by rtlsdr_open()
* \param gains array of gain values. In tenths of a dB, 115 means 11.5 dB.
* \return <= 0 on error, number of available (returned) gain values otherwise
}
function RtlSdrGetTunerGains(Dev: PRtlSdrDev; Gains: PInteger): Integer; external RTLLIB name 'rtlsdr_get_tuner_gains';
{!
* Set the gain for the device.
* Manual gain mode must be enabled for this to work.
*
* Valid gain values (in tenths of a dB) for the E4000 tuner:
* -10, 15, 40, 65, 90, 115, 140, 165, 190,
* 215, 240, 290, 340, 420, 430, 450, 470, 490
*
* Valid gain values may be queried with \ref rtlsdr_get_tuner_gains function.
*
* \param dev the device handle given by rtlsdr_open()
* \param gain in tenths of a dB, 115 means 11.5 dB.
* \return 0 on success
}
function RtlSdrSetTunerGain(Dev: PRtlSdrDev; Gain: Integer): Integer; external RTLLIB name 'rtlsdr_set_tuner_gain';
{!
* Get actual gain the device is configured to.
*
* \param dev the device handle given by rtlsdr_open()
* \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB.
}
function RtlSdrGetTunerGain(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_get_tuner_gain';
{!
* Set the intermediate frequency gain for the device.
*
* \param dev the device handle given by rtlsdr_open()
* \param stage intermediate frequency gain stage number (1 to 6 for E4000)
* \param gain in tenths of a dB, -30 means -3.0 dB.
* \return 0 on success
}
function RtlSdrSetTunerIfGain(Dev: PRtlSdrDev; const Stage, Gain: Integer): Integer; external RTLLIB name 'rtlsdr_set_tuner_if_gain';
{!
* Set the gain mode (automatic/manual) for the device.
* Manual gain mode must be enabled for the gain setter function to work.
*
* \param dev the device handle given by rtlsdr_open()
* \param manual gain mode, 1 means manual gain mode shall be enabled.
* \return 0 on success
}
function RtlSdrSetTunerGainMode(Dev: PRtlSdrDev; const Manual: Integer): Integer; external RTLLIB name 'rtlsdr_set_tuner_gain_mode';
{
(samp_rate <= 225000) ((samp_rate > 300000) && (samp_rate <= 900000)) (samp_rate > 3200000)
}
{ this will select the baseband filters according to the requested sample rate}
function RtlSdrSetSampleRate(Dev: PRtlSdrDev; const Rate: Cardinal): Integer; external RTLLIB name 'rtlsdr_set_sample_rate';
{!
* Get actual sample rate the device is configured to.
*
* \param dev the device handle given by rtlsdr_open()
* \return 0 on error, sample rate in Hz otherwise
}
function RtlSdrGetSampleRate(Dev: PRtlSdrDev): Cardinal; external RTLLIB name 'rtlsdr_get_sample_rate';
{!
* Enable test mode that returns an 8 bit counter instead of the samples.
* The counter is generated inside the RTL2832.
*
* \param dev the device handle given by rtlsdr_open()
* \param test mode, 1 means enabled, 0 disabled
* \return 0 on success
}
function RtlSdrSetTestMode(Dev: PRtlSdrDev; const On1: Integer): Integer; external RTLLIB name 'rtlsdr_set_testmode';
{!
* Enable or disable the internal digital AGC of the RTL2832.
*
* \param dev the device handle given by rtlsdr_open()
* \param digital AGC mode, 1 means enabled, 0 disabled
* \return 0 on success
}
function RtlSdrSetAgcMode(Dev: PRtlSdrDev; const On1: Integer): Integer; external RTLLIB name 'rtlsdr_set_agc_mode';
{!
* Enable or disable the direct sampling mode. When enabled, the IF mode
* of the RTL2832 is activated, and rtlsdr_set_center_freq() will control
* the IF-frequency of the DDC, which can be used to tune from 0 to 28.8 MHz
* (xtal frequency of the RTL2832).
*
* \param dev the device handle given by rtlsdr_open()
* \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled
* \return 0 on success
}
function RtlSdrSetDirectSampling(Dev: PRtlSdrDev; const On1: Integer): Integer; external RTLLIB name 'rtlsdr_set_direct_sampling';
{!
* Get state of the direct sampling mode
*
* \param dev the device handle given by rtlsdr_open()
* \return -1 on error, 0 means disabled, 1 I-ADC input enabled
* 2 Q-ADC input enabled
}
function RtlSdrGetDirectSampling(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_get_direct_sampling';
{!
* Enable or disable offset tuning for zero-IF tuners, which allows to avoid
* problems caused by the DC offset of the ADCs and 1/f noise.
*
* \param dev the device handle given by rtlsdr_open()
* \param on 0 means disabled, 1 enabled
* \return 0 on success
}
function RtlSdrSetOffsetTuning(Dev: PRtlSdrDev; const On1: Integer): Integer; external RTLLIB name 'rtlsdr_set_offset_tuning';
{!
* Get state of the offset tuning mode
*
* \param dev the device handle given by rtlsdr_open()
* \return -1 on error, 0 means disabled, 1 enabled
}
function RtlSdrGetOffsetTuning(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_get_offset_tuning';
{ streaming functions}
function RtlSdrResetBuffer(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_reset_buffer';
function RtlSdrReadSync(Dev: PRtlSdrDev; var Buf; const Len: Integer; var NRead: Integer): Integer; external RTLLIB name 'rtlsdr_read_sync';
{!
* Read samples from the device asynchronously. This function will block until
* it is being canceled using rtlsdr_cancel_async()
*
* \param dev the device handle given by rtlsdr_open()
* \param cb callback function to return received samples
* \param ctx user specific context to pass via the callback function
* \param buf_num optional buffer count, buf_num * buf_len = overall buffer size
* set to 0 for default buffer count (32)
* \param buf_len optional buffer length, must be multiple of 512,
* set to 0 for default buffer length (16 * 32 * 512)
* \return 0 on success
}
function RtlSdrReadASync(Dev: PRtlSdrDev; CB: TRtlSdrReadAsyncCB; Ctx: Pointer; BufNum: Cardinal; BufLen: Cardinal): Integer; external RTLLIB name 'rtlsdr_read_async';
{!
* Cancel all pending asynchronous operations on the device.
*
* \param dev the device handle given by rtlsdr_open()
* \return 0 on success
}
function RtlSdrCancelAsync(Dev: PRtlSdrDev): Integer; external RTLLIB name 'rtlsdr_cancel_async';
implementation
function RtlSdrGetTunerType(const Index: Integer): TRtlSdrTuner;
var
P: PRtlSdrDev = nil;
begin
Result := RTLSDR_TUNER_UNKNOWN;
if RtlSdrOpen(P, Index) <> 0 then Exit;
Result := RtlSdrGetTunerType(P);
RtlSdrClose(P);
end;
function FormatTunerType(T: TRtlSdrTuner): string;
begin
Result := 'UKNOWN';
case T of
RTLSDR_TUNER_E4000 : Result := 'E4000';
RTLSDR_TUNER_FC0012 : Result := 'FC0012';
RTLSDR_TUNER_FC0013 : Result := 'FC0013';
RTLSDR_TUNER_FC2580 : Result := 'FC2580';
RTLSDR_TUNER_R820T : Result := 'R820T';
RTLSDR_TUNER_R828D : Result := 'R828D';
end;
end;
end.