2 * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
3 * Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
4 * Copyright (C) 2012 by Dimitri Stolnikov <horiz0n@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <rtl-sdr_export.h>
30 typedef struct rtlsdr_dev rtlsdr_dev_t;
32 RTLSDR_API uint32_t rtlsdr_get_device_count(void);
34 RTLSDR_API const char* rtlsdr_get_device_name(uint32_t index);
37 * Get USB device strings.
39 * NOTE: The string arguments must provide space for up to 256 bytes.
41 * \param index the device index
42 * \param manufact manufacturer name, may be NULL
43 * \param product product name, may be NULL
44 * \param serial serial number, may be NULL
45 * \return 0 on success
47 RTLSDR_API int rtlsdr_get_device_usb_strings(uint32_t index,
53 * Get device index by USB serial string descriptor.
55 * \param serial serial string of the device
56 * \return device index of first device where the name matched
57 * \return -1 if name is NULL
58 * \return -2 if no devices were found at all
59 * \return -3 if devices were found, but none with matching name
61 RTLSDR_API int rtlsdr_get_index_by_serial(const char *serial);
63 RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index);
65 RTLSDR_API int rtlsdr_close(rtlsdr_dev_t *dev);
67 /* configuration functions */
70 * Set crystal oscillator frequencies used for the RTL2832 and the tuner IC.
72 * Usually both ICs use the same clock. Changing the clock may make sense if
73 * you are applying an external clock to the tuner or to compensate the
74 * frequency (and samplerate) error caused by the original (cheap) crystal.
76 * NOTE: Call this function only if you fully understand the implications.
78 * \param dev the device handle given by rtlsdr_open()
79 * \param rtl_freq frequency value used to clock the RTL2832 in Hz
80 * \param tuner_freq frequency value used to clock the tuner IC in Hz
81 * \return 0 on success
83 RTLSDR_API int rtlsdr_set_xtal_freq(rtlsdr_dev_t *dev, uint32_t rtl_freq,
87 * Get crystal oscillator frequencies used for the RTL2832 and the tuner IC.
89 * Usually both ICs use the same clock.
91 * \param dev the device handle given by rtlsdr_open()
92 * \param rtl_freq frequency value used to clock the RTL2832 in Hz
93 * \param tuner_freq frequency value used to clock the tuner IC in Hz
94 * \return 0 on success
96 RTLSDR_API int rtlsdr_get_xtal_freq(rtlsdr_dev_t *dev, uint32_t *rtl_freq,
97 uint32_t *tuner_freq);
100 * Get USB device strings.
102 * NOTE: The string arguments must provide space for up to 256 bytes.
104 * \param dev the device handle given by rtlsdr_open()
105 * \param manufact manufacturer name, may be NULL
106 * \param product product name, may be NULL
107 * \param serial serial number, may be NULL
108 * \return 0 on success
110 RTLSDR_API int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact,
111 char *product, char *serial);
114 * Write the device EEPROM
116 * \param dev the device handle given by rtlsdr_open()
117 * \param data buffer of data to be written
118 * \param offset address where the data should be written
119 * \param len length of the data
120 * \return 0 on success
121 * \return -1 if device handle is invalid
122 * \return -2 if EEPROM size is exceeded
123 * \return -3 if no EEPROM was found
126 RTLSDR_API int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data,
127 uint8_t offset, uint16_t len);
130 * Read the device EEPROM
132 * \param dev the device handle given by rtlsdr_open()
133 * \param data buffer where the data should be written
134 * \param offset address where the data should be read from
135 * \param len length of the data
136 * \return 0 on success
137 * \return -1 if device handle is invalid
138 * \return -2 if EEPROM size is exceeded
139 * \return -3 if no EEPROM was found
142 RTLSDR_API int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data,
143 uint8_t offset, uint16_t len);
145 RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
148 * Get actual frequency the device is tuned to.
150 * \param dev the device handle given by rtlsdr_open()
151 * \return 0 on error, frequency in Hz otherwise
153 RTLSDR_API uint32_t rtlsdr_get_center_freq(rtlsdr_dev_t *dev);
156 * Set the frequency correction value for the device.
158 * \param dev the device handle given by rtlsdr_open()
159 * \param ppm correction value in parts per million (ppm)
160 * \return 0 on success
162 RTLSDR_API int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm);
165 * Get actual frequency correction value of the device.
167 * \param dev the device handle given by rtlsdr_open()
168 * \return correction value in parts per million (ppm)
170 RTLSDR_API int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev);
173 RTLSDR_TUNER_UNKNOWN = 0,
182 * Get the tuner type.
184 * \param dev the device handle given by rtlsdr_open()
185 * \return RTLSDR_TUNER_UNKNOWN on error, tuner type otherwise
187 RTLSDR_API enum rtlsdr_tuner rtlsdr_get_tuner_type(rtlsdr_dev_t *dev);
190 * Get a list of gains supported by the tuner.
192 * NOTE: The gains argument must be preallocated by the caller. If NULL is
193 * being given instead, the number of available gain values will be returned.
195 * \param dev the device handle given by rtlsdr_open()
196 * \param gains array of gain values. In tenths of a dB, 115 means 11.5 dB.
197 * \return <= 0 on error, number of available (returned) gain values otherwise
199 RTLSDR_API int rtlsdr_get_tuner_gains(rtlsdr_dev_t *dev, int *gains);
202 * Set the gain for the device.
203 * Manual gain mode must be enabled for this to work.
205 * Valid gain values (in tenths of a dB) for the E4000 tuner:
206 * -10, 15, 40, 65, 90, 115, 140, 165, 190,
207 * 215, 240, 290, 340, 420, 430, 450, 470, 490
209 * Valid gain values may be queried with \ref rtlsdr_get_tuner_gains function.
211 * \param dev the device handle given by rtlsdr_open()
212 * \param gain in tenths of a dB, 115 means 11.5 dB.
213 * \return 0 on success
215 RTLSDR_API int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain);
218 * Get actual gain the device is configured to.
220 * \param dev the device handle given by rtlsdr_open()
221 * \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB.
223 RTLSDR_API int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev);
226 * Set the intermediate frequency gain for the device.
228 * \param dev the device handle given by rtlsdr_open()
229 * \param stage intermediate frequency gain stage number (1 to 6 for E4000)
230 * \param gain in tenths of a dB, -30 means -3.0 dB.
231 * \return 0 on success
233 RTLSDR_API int rtlsdr_set_tuner_if_gain(rtlsdr_dev_t *dev, int stage, int gain);
236 * Set the gain mode (automatic/manual) for the device.
237 * Manual gain mode must be enabled for the gain setter function to work.
239 * \param dev the device handle given by rtlsdr_open()
240 * \param manual gain mode, 1 means manual gain mode shall be enabled.
241 * \return 0 on success
243 RTLSDR_API int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int manual);
245 /* this will select the baseband filters according to the requested sample rate */
246 RTLSDR_API int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate);
249 * Get actual sample rate the device is configured to.
251 * \param dev the device handle given by rtlsdr_open()
252 * \return 0 on error, sample rate in Hz otherwise
254 RTLSDR_API uint32_t rtlsdr_get_sample_rate(rtlsdr_dev_t *dev);
257 * Enable test mode that returns an 8 bit counter instead of the samples.
258 * The counter is generated inside the RTL2832.
260 * \param dev the device handle given by rtlsdr_open()
261 * \param test mode, 1 means enabled, 0 disabled
262 * \return 0 on success
264 RTLSDR_API int rtlsdr_set_testmode(rtlsdr_dev_t *dev, int on);
267 * Enable or disable the internal digital AGC of the RTL2832.
269 * \param dev the device handle given by rtlsdr_open()
270 * \param digital AGC mode, 1 means enabled, 0 disabled
271 * \return 0 on success
273 RTLSDR_API int rtlsdr_set_agc_mode(rtlsdr_dev_t *dev, int on);
276 * Enable or disable the direct sampling mode. When enabled, the IF mode
277 * of the RTL2832 is activated, and rtlsdr_set_center_freq() will control
278 * the IF-frequency of the DDC, which can be used to tune from 0 to 28.8 MHz
279 * (xtal frequency of the RTL2832).
281 * \param dev the device handle given by rtlsdr_open()
282 * \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled
283 * \return 0 on success
285 RTLSDR_API int rtlsdr_set_direct_sampling(rtlsdr_dev_t *dev, int on);
288 * Get state of the direct sampling mode
290 * \param dev the device handle given by rtlsdr_open()
291 * \return -1 on error, 0 means disabled, 1 I-ADC input enabled
292 * 2 Q-ADC input enabled
294 RTLSDR_API int rtlsdr_get_direct_sampling(rtlsdr_dev_t *dev);
297 * Enable or disable offset tuning for zero-IF tuners, which allows to avoid
298 * problems caused by the DC offset of the ADCs and 1/f noise.
300 * \param dev the device handle given by rtlsdr_open()
301 * \param on 0 means disabled, 1 enabled
302 * \return 0 on success
304 RTLSDR_API int rtlsdr_set_offset_tuning(rtlsdr_dev_t *dev, int on);
307 * Get state of the offset tuning mode
309 * \param dev the device handle given by rtlsdr_open()
310 * \return -1 on error, 0 means disabled, 1 enabled
312 RTLSDR_API int rtlsdr_get_offset_tuning(rtlsdr_dev_t *dev);
314 /* streaming functions */
316 RTLSDR_API int rtlsdr_reset_buffer(rtlsdr_dev_t *dev);
318 RTLSDR_API int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read);
320 typedef void(*rtlsdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx);
323 * Read samples from the device asynchronously. This function will block until
324 * it is being canceled using rtlsdr_cancel_async()
326 * NOTE: This function is deprecated and is subject for removal.
328 * \param dev the device handle given by rtlsdr_open()
329 * \param cb callback function to return received samples
330 * \param ctx user specific context to pass via the callback function
331 * \return 0 on success
333 RTLSDR_API int rtlsdr_wait_async(rtlsdr_dev_t *dev, rtlsdr_read_async_cb_t cb, void *ctx);
336 * Read samples from the device asynchronously. This function will block until
337 * it is being canceled using rtlsdr_cancel_async()
339 * \param dev the device handle given by rtlsdr_open()
340 * \param cb callback function to return received samples
341 * \param ctx user specific context to pass via the callback function
342 * \param buf_num optional buffer count, buf_num * buf_len = overall buffer size
343 * set to 0 for default buffer count (32)
344 * \param buf_len optional buffer length, must be multiple of 512,
345 * set to 0 for default buffer length (16 * 32 * 512)
346 * \return 0 on success
348 RTLSDR_API int rtlsdr_read_async(rtlsdr_dev_t *dev,
349 rtlsdr_read_async_cb_t cb,
355 * Cancel all pending asynchronous operations on the device.
357 * \param dev the device handle given by rtlsdr_open()
358 * \return 0 on success
360 RTLSDR_API int rtlsdr_cancel_async(rtlsdr_dev_t *dev);
366 #endif /* __RTL_SDR_H */