rtl-sdr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
  3. * Copyright (C) 2012-2013 by Steve Markgraf <steve@steve-m.de>
  4. * Copyright (C) 2012 by Dimitri Stolnikov <horiz0n@gmx.net>
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #ifndef __RTL_SDR_H
  20. #define __RTL_SDR_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include <stdint.h>
  25. #include <rtl-sdr_export.h>
  26. typedef struct rtlsdr_dev rtlsdr_dev_t;
  27. RTLSDR_API uint32_t rtlsdr_get_device_count(void);
  28. RTLSDR_API const char* rtlsdr_get_device_name(uint32_t index);
  29. /*!
  30. * Get USB device strings.
  31. *
  32. * NOTE: The string arguments must provide space for up to 256 bytes.
  33. *
  34. * \param index the device index
  35. * \param manufact manufacturer name, may be NULL
  36. * \param product product name, may be NULL
  37. * \param serial serial number, may be NULL
  38. * \return 0 on success
  39. */
  40. RTLSDR_API int rtlsdr_get_device_usb_strings(uint32_t index,
  41. char *manufact,
  42. char *product,
  43. char *serial);
  44. /*!
  45. * Get device index by USB serial string descriptor.
  46. *
  47. * \param serial serial string of the device
  48. * \return device index of first device where the name matched
  49. * \return -1 if name is NULL
  50. * \return -2 if no devices were found at all
  51. * \return -3 if devices were found, but none with matching name
  52. */
  53. RTLSDR_API int rtlsdr_get_index_by_serial(const char *serial);
  54. RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index);
  55. RTLSDR_API int rtlsdr_close(rtlsdr_dev_t *dev);
  56. /* configuration functions */
  57. /*!
  58. * Set crystal oscillator frequencies used for the RTL2832 and the tuner IC.
  59. *
  60. * Usually both ICs use the same clock. Changing the clock may make sense if
  61. * you are applying an external clock to the tuner or to compensate the
  62. * frequency (and samplerate) error caused by the original (cheap) crystal.
  63. *
  64. * NOTE: Call this function only if you fully understand the implications.
  65. *
  66. * \param dev the device handle given by rtlsdr_open()
  67. * \param rtl_freq frequency value used to clock the RTL2832 in Hz
  68. * \param tuner_freq frequency value used to clock the tuner IC in Hz
  69. * \return 0 on success
  70. */
  71. RTLSDR_API int rtlsdr_set_xtal_freq(rtlsdr_dev_t *dev, uint32_t rtl_freq,
  72. uint32_t tuner_freq);
  73. /*!
  74. * Get crystal oscillator frequencies used for the RTL2832 and the tuner IC.
  75. *
  76. * Usually both ICs use the same clock.
  77. *
  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
  82. */
  83. RTLSDR_API int rtlsdr_get_xtal_freq(rtlsdr_dev_t *dev, uint32_t *rtl_freq,
  84. uint32_t *tuner_freq);
  85. /*!
  86. * Get USB device strings.
  87. *
  88. * NOTE: The string arguments must provide space for up to 256 bytes.
  89. *
  90. * \param dev the device handle given by rtlsdr_open()
  91. * \param manufact manufacturer name, may be NULL
  92. * \param product product name, may be NULL
  93. * \param serial serial number, may be NULL
  94. * \return 0 on success
  95. */
  96. RTLSDR_API int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact,
  97. char *product, char *serial);
  98. /*!
  99. * Write the device EEPROM
  100. *
  101. * \param dev the device handle given by rtlsdr_open()
  102. * \param data buffer of data to be written
  103. * \param offset address where the data should be written
  104. * \param len length of the data
  105. * \return 0 on success
  106. * \return -1 if device handle is invalid
  107. * \return -2 if EEPROM size is exceeded
  108. * \return -3 if no EEPROM was found
  109. */
  110. RTLSDR_API int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data,
  111. uint8_t offset, uint16_t len);
  112. /*!
  113. * Read the device EEPROM
  114. *
  115. * \param dev the device handle given by rtlsdr_open()
  116. * \param data buffer where the data should be written
  117. * \param offset address where the data should be read from
  118. * \param len length of the data
  119. * \return 0 on success
  120. * \return -1 if device handle is invalid
  121. * \return -2 if EEPROM size is exceeded
  122. * \return -3 if no EEPROM was found
  123. */
  124. RTLSDR_API int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data,
  125. uint8_t offset, uint16_t len);
  126. RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
  127. /*!
  128. * Get actual frequency the device is tuned to.
  129. *
  130. * \param dev the device handle given by rtlsdr_open()
  131. * \return 0 on error, frequency in Hz otherwise
  132. */
  133. RTLSDR_API uint32_t rtlsdr_get_center_freq(rtlsdr_dev_t *dev);
  134. /*!
  135. * Set the frequency correction value for the device.
  136. *
  137. * \param dev the device handle given by rtlsdr_open()
  138. * \param ppm correction value in parts per million (ppm)
  139. * \return 0 on success
  140. */
  141. RTLSDR_API int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm);
  142. /*!
  143. * Get actual frequency correction value of the device.
  144. *
  145. * \param dev the device handle given by rtlsdr_open()
  146. * \return correction value in parts per million (ppm)
  147. */
  148. RTLSDR_API int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev);
  149. enum rtlsdr_tuner {
  150. RTLSDR_TUNER_UNKNOWN = 0,
  151. RTLSDR_TUNER_E4000,
  152. RTLSDR_TUNER_FC0012,
  153. RTLSDR_TUNER_FC0013,
  154. RTLSDR_TUNER_FC2580,
  155. RTLSDR_TUNER_R820T,
  156. RTLSDR_TUNER_R828D
  157. };
  158. /*!
  159. * Get the tuner type.
  160. *
  161. * \param dev the device handle given by rtlsdr_open()
  162. * \return RTLSDR_TUNER_UNKNOWN on error, tuner type otherwise
  163. */
  164. RTLSDR_API enum rtlsdr_tuner rtlsdr_get_tuner_type(rtlsdr_dev_t *dev);
  165. /*!
  166. * Get a list of gains supported by the tuner.
  167. *
  168. * NOTE: The gains argument must be preallocated by the caller. If NULL is
  169. * being given instead, the number of available gain values will be returned.
  170. *
  171. * \param dev the device handle given by rtlsdr_open()
  172. * \param gains array of gain values. In tenths of a dB, 115 means 11.5 dB.
  173. * \return <= 0 on error, number of available (returned) gain values otherwise
  174. */
  175. RTLSDR_API int rtlsdr_get_tuner_gains(rtlsdr_dev_t *dev, int *gains);
  176. /*!
  177. * Set the gain for the device.
  178. * Manual gain mode must be enabled for this to work.
  179. *
  180. * Valid gain values (in tenths of a dB) for the E4000 tuner:
  181. * -10, 15, 40, 65, 90, 115, 140, 165, 190,
  182. * 215, 240, 290, 340, 420, 430, 450, 470, 490
  183. *
  184. * Valid gain values may be queried with \ref rtlsdr_get_tuner_gains function.
  185. *
  186. * \param dev the device handle given by rtlsdr_open()
  187. * \param gain in tenths of a dB, 115 means 11.5 dB.
  188. * \return 0 on success
  189. */
  190. RTLSDR_API int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain);
  191. /*!
  192. * Set the bandwidth for the device.
  193. *
  194. * \param dev the device handle given by rtlsdr_open()
  195. * \param bw bandwidth in Hz. Zero means automatic BW selection.
  196. * \return 0 on success
  197. */
  198. RTLSDR_API int rtlsdr_set_tuner_bandwidth(rtlsdr_dev_t *dev, uint32_t bw);
  199. /*!
  200. * Get actual gain the device is configured to.
  201. *
  202. * \param dev the device handle given by rtlsdr_open()
  203. * \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB.
  204. */
  205. RTLSDR_API int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev);
  206. /*!
  207. * Set the intermediate frequency gain for the device.
  208. *
  209. * \param dev the device handle given by rtlsdr_open()
  210. * \param stage intermediate frequency gain stage number (1 to 6 for E4000)
  211. * \param gain in tenths of a dB, -30 means -3.0 dB.
  212. * \return 0 on success
  213. */
  214. RTLSDR_API int rtlsdr_set_tuner_if_gain(rtlsdr_dev_t *dev, int stage, int gain);
  215. /*!
  216. * Set the gain mode (automatic/manual) for the device.
  217. * Manual gain mode must be enabled for the gain setter function to work.
  218. *
  219. * \param dev the device handle given by rtlsdr_open()
  220. * \param manual gain mode, 1 means manual gain mode shall be enabled.
  221. * \return 0 on success
  222. */
  223. RTLSDR_API int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int manual);
  224. /*!
  225. * Set the sample rate for the device, also selects the baseband filters
  226. * according to the requested sample rate for tuners where this is possible.
  227. *
  228. * \param dev the device handle given by rtlsdr_open()
  229. * \param samp_rate the sample rate to be set, possible values are:
  230. * 225001 - 300000 Hz
  231. * 900001 - 3200000 Hz
  232. * sample loss is to be expected for rates > 2400000
  233. * \return 0 on success, -EINVAL on invalid rate
  234. */
  235. RTLSDR_API int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate);
  236. /*!
  237. * Get actual sample rate the device is configured to.
  238. *
  239. * \param dev the device handle given by rtlsdr_open()
  240. * \return 0 on error, sample rate in Hz otherwise
  241. */
  242. RTLSDR_API uint32_t rtlsdr_get_sample_rate(rtlsdr_dev_t *dev);
  243. /*!
  244. * Enable test mode that returns an 8 bit counter instead of the samples.
  245. * The counter is generated inside the RTL2832.
  246. *
  247. * \param dev the device handle given by rtlsdr_open()
  248. * \param test mode, 1 means enabled, 0 disabled
  249. * \return 0 on success
  250. */
  251. RTLSDR_API int rtlsdr_set_testmode(rtlsdr_dev_t *dev, int on);
  252. /*!
  253. * Enable or disable the internal digital AGC of the RTL2832.
  254. *
  255. * \param dev the device handle given by rtlsdr_open()
  256. * \param digital AGC mode, 1 means enabled, 0 disabled
  257. * \return 0 on success
  258. */
  259. RTLSDR_API int rtlsdr_set_agc_mode(rtlsdr_dev_t *dev, int on);
  260. /*!
  261. * Enable or disable the direct sampling mode. When enabled, the IF mode
  262. * of the RTL2832 is activated, and rtlsdr_set_center_freq() will control
  263. * the IF-frequency of the DDC, which can be used to tune from 0 to 28.8 MHz
  264. * (xtal frequency of the RTL2832).
  265. *
  266. * \param dev the device handle given by rtlsdr_open()
  267. * \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled
  268. * \return 0 on success
  269. */
  270. RTLSDR_API int rtlsdr_set_direct_sampling(rtlsdr_dev_t *dev, int on);
  271. /*!
  272. * Get state of the direct sampling mode
  273. *
  274. * \param dev the device handle given by rtlsdr_open()
  275. * \return -1 on error, 0 means disabled, 1 I-ADC input enabled
  276. * 2 Q-ADC input enabled
  277. */
  278. RTLSDR_API int rtlsdr_get_direct_sampling(rtlsdr_dev_t *dev);
  279. /*!
  280. * Enable or disable offset tuning for zero-IF tuners, which allows to avoid
  281. * problems caused by the DC offset of the ADCs and 1/f noise.
  282. *
  283. * \param dev the device handle given by rtlsdr_open()
  284. * \param on 0 means disabled, 1 enabled
  285. * \return 0 on success
  286. */
  287. RTLSDR_API int rtlsdr_set_offset_tuning(rtlsdr_dev_t *dev, int on);
  288. /*!
  289. * Get state of the offset tuning mode
  290. *
  291. * \param dev the device handle given by rtlsdr_open()
  292. * \return -1 on error, 0 means disabled, 1 enabled
  293. */
  294. RTLSDR_API int rtlsdr_get_offset_tuning(rtlsdr_dev_t *dev);
  295. /* streaming functions */
  296. RTLSDR_API int rtlsdr_reset_buffer(rtlsdr_dev_t *dev);
  297. RTLSDR_API int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read);
  298. typedef void(*rtlsdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx);
  299. /*!
  300. * Read samples from the device asynchronously. This function will block until
  301. * it is being canceled using rtlsdr_cancel_async()
  302. *
  303. * NOTE: This function is deprecated and is subject for removal.
  304. *
  305. * \param dev the device handle given by rtlsdr_open()
  306. * \param cb callback function to return received samples
  307. * \param ctx user specific context to pass via the callback function
  308. * \return 0 on success
  309. */
  310. RTLSDR_API int rtlsdr_wait_async(rtlsdr_dev_t *dev, rtlsdr_read_async_cb_t cb, void *ctx);
  311. /*!
  312. * Read samples from the device asynchronously. This function will block until
  313. * it is being canceled using rtlsdr_cancel_async()
  314. *
  315. * \param dev the device handle given by rtlsdr_open()
  316. * \param cb callback function to return received samples
  317. * \param ctx user specific context to pass via the callback function
  318. * \param buf_num optional buffer count, buf_num * buf_len = overall buffer size
  319. * set to 0 for default buffer count (15)
  320. * \param buf_len optional buffer length, must be multiple of 512,
  321. * should be a multiple of 16384 (URB size), set to 0
  322. * for default buffer length (16 * 32 * 512)
  323. * \return 0 on success
  324. */
  325. RTLSDR_API int rtlsdr_read_async(rtlsdr_dev_t *dev,
  326. rtlsdr_read_async_cb_t cb,
  327. void *ctx,
  328. uint32_t buf_num,
  329. uint32_t buf_len);
  330. /*!
  331. * Cancel all pending asynchronous operations on the device.
  332. *
  333. * \param dev the device handle given by rtlsdr_open()
  334. * \return 0 on success
  335. */
  336. RTLSDR_API int rtlsdr_cancel_async(rtlsdr_dev_t *dev);
  337. #ifdef __cplusplus
  338. }
  339. #endif
  340. #endif /* __RTL_SDR_H */