rtl-sdr.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  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>
  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. RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index);
  45. RTLSDR_API int rtlsdr_close(rtlsdr_dev_t *dev);
  46. /* configuration functions */
  47. /*!
  48. * Set crystal oscillator frequencies used for the RTL2832 and the tuner IC.
  49. *
  50. * Usually both ICs use the same clock. Changing the clock may make sense if
  51. * you are applying an external clock to the tuner or to compensate the
  52. * frequency (and samplerate) error caused by the original (cheap) crystal.
  53. *
  54. * NOTE: Call this function only if you fully understand the implications.
  55. *
  56. * \param dev the device handle given by rtlsdr_open()
  57. * \param rtl_freq frequency value used to clock the RTL2832 in Hz
  58. * \param tuner_freq frequency value used to clock the tuner IC in Hz
  59. * \return 0 on success
  60. */
  61. RTLSDR_API int rtlsdr_set_xtal_freq(rtlsdr_dev_t *dev, uint32_t rtl_freq,
  62. uint32_t tuner_freq);
  63. /*!
  64. * Get crystal oscillator frequencies used for the RTL2832 and the tuner IC.
  65. *
  66. * Usually both ICs use the same clock.
  67. *
  68. * \param dev the device handle given by rtlsdr_open()
  69. * \param rtl_freq frequency value used to clock the RTL2832 in Hz
  70. * \param tuner_freq frequency value used to clock the tuner IC in Hz
  71. * \return 0 on success
  72. */
  73. RTLSDR_API int rtlsdr_get_xtal_freq(rtlsdr_dev_t *dev, uint32_t *rtl_freq,
  74. uint32_t *tuner_freq);
  75. /*!
  76. * Get USB device strings.
  77. *
  78. * NOTE: The string arguments must provide space for up to 256 bytes.
  79. *
  80. * \param dev the device handle given by rtlsdr_open()
  81. * \param manufact manufacturer name, may be NULL
  82. * \param product product name, may be NULL
  83. * \param serial serial number, may be NULL
  84. * \return 0 on success
  85. */
  86. RTLSDR_API int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact,
  87. char *product, char *serial);
  88. RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
  89. /*!
  90. * Get actual frequency the device is tuned to.
  91. *
  92. * \param dev the device handle given by rtlsdr_open()
  93. * \return 0 on error, frequency in Hz otherwise
  94. */
  95. RTLSDR_API uint32_t rtlsdr_get_center_freq(rtlsdr_dev_t *dev);
  96. /*!
  97. * Set the frequency correction value for the device.
  98. *
  99. * \param dev the device handle given by rtlsdr_open()
  100. * \param ppm correction value in parts per million (ppm)
  101. * \return 0 on success
  102. */
  103. RTLSDR_API int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm);
  104. /*!
  105. * Get actual frequency correction value of the device.
  106. *
  107. * \param dev the device handle given by rtlsdr_open()
  108. * \return correction value in parts per million (ppm)
  109. */
  110. RTLSDR_API int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev);
  111. enum rtlsdr_tuner {
  112. RTLSDR_TUNER_UNKNOWN = 0,
  113. RTLSDR_TUNER_E4000,
  114. RTLSDR_TUNER_FC0012,
  115. RTLSDR_TUNER_FC0013,
  116. RTLSDR_TUNER_FC2580
  117. };
  118. /*!
  119. * Get the tuner type.
  120. *
  121. * \param dev the device handle given by rtlsdr_open()
  122. * \return RTLSDR_TUNER_UNKNOWN on error, tuner type otherwise
  123. */
  124. RTLSDR_API enum rtlsdr_tuner rtlsdr_get_tuner_type(rtlsdr_dev_t *dev);
  125. /*!
  126. * Get a list of gains supported by the tuner.
  127. *
  128. * NOTE: The gains argument must be preallocated by the caller. If NULL is
  129. * being given instead, the number of available gain values will be returned.
  130. *
  131. * \param dev the device handle given by rtlsdr_open()
  132. * \param gains array of gain values. In tenths of a dB, 115 means 11.5 dB.
  133. * \return <= 0 on error, number of available (returned) gain values otherwise
  134. */
  135. RTLSDR_API int rtlsdr_get_tuner_gains(rtlsdr_dev_t *dev, int *gains);
  136. /*!
  137. * Set the gain for the device.
  138. * Manual gain mode must be enabled for this to work.
  139. *
  140. * Valid gain values (in tenths of a dB) for the E4000 tuner:
  141. * -10, 15, 40, 65, 90, 115, 140, 165, 190,
  142. * 215, 240, 290, 340, 420, 430, 450, 470, 490
  143. *
  144. * Valid gain values may be queried with \ref rtlsdr_get_tuner_gains function.
  145. *
  146. * \param dev the device handle given by rtlsdr_open()
  147. * \param gain in tenths of a dB, 115 means 11.5 dB.
  148. * \return 0 on success
  149. */
  150. RTLSDR_API int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain);
  151. /*!
  152. * Get actual gain the device is configured to.
  153. *
  154. * \param dev the device handle given by rtlsdr_open()
  155. * \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB.
  156. */
  157. RTLSDR_API int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev);
  158. /*!
  159. * Set the intermediate frequency gain for the device.
  160. *
  161. * \param dev the device handle given by rtlsdr_open()
  162. * \param stage intermediate frequency gain stage number (1 to 6 for E4000)
  163. * \param gain in tenths of a dB, -30 means -3.0 dB.
  164. * \return 0 on success
  165. */
  166. RTLSDR_API int rtlsdr_set_tuner_if_gain(rtlsdr_dev_t *dev, int stage, int gain);
  167. /*!
  168. * Set the gain mode (automatic/manual) for the device.
  169. * Manual gain mode must be enabled for the gain setter function to work.
  170. *
  171. * \param dev the device handle given by rtlsdr_open()
  172. * \param manual gain mode, 1 means manual gain mode shall be enabled.
  173. * \return 0 on success
  174. */
  175. RTLSDR_API int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int manual);
  176. /* this will select the baseband filters according to the requested sample rate */
  177. RTLSDR_API int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate);
  178. /*!
  179. * Get actual sample rate the device is configured to.
  180. *
  181. * \param dev the device handle given by rtlsdr_open()
  182. * \return 0 on error, sample rate in Hz otherwise
  183. */
  184. RTLSDR_API uint32_t rtlsdr_get_sample_rate(rtlsdr_dev_t *dev);
  185. /*!
  186. * Enable test mode that returns an 8 bit counter instead of the samples.
  187. * The counter is generated inside the RTL2832.
  188. *
  189. * \param dev the device handle given by rtlsdr_open()
  190. * \param test mode, 1 means enabled, 0 disabled
  191. * \return 0 on success
  192. */
  193. RTLSDR_API int rtlsdr_set_testmode(rtlsdr_dev_t *dev, int on);
  194. /*!
  195. * Enable or disable the internal digital AGC of the RTL2832.
  196. *
  197. * \param dev the device handle given by rtlsdr_open()
  198. * \param digital AGC mode, 1 means enabled, 0 disabled
  199. * \return 0 on success
  200. */
  201. RTLSDR_API int rtlsdr_set_agc_mode(rtlsdr_dev_t *dev, int on);
  202. /* streaming functions */
  203. RTLSDR_API int rtlsdr_reset_buffer(rtlsdr_dev_t *dev);
  204. RTLSDR_API int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read);
  205. typedef void(*rtlsdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx);
  206. /*!
  207. * Read samples from the device asynchronously. This function will block until
  208. * it is being canceled using rtlsdr_cancel_async()
  209. *
  210. * NOTE: This function is deprecated and is subject for removal.
  211. *
  212. * \param dev the device handle given by rtlsdr_open()
  213. * \param cb callback function to return received samples
  214. * \param ctx user specific context to pass via the callback function
  215. * \return 0 on success
  216. */
  217. RTLSDR_API int rtlsdr_wait_async(rtlsdr_dev_t *dev, rtlsdr_read_async_cb_t cb, void *ctx);
  218. /*!
  219. * Read samples from the device asynchronously. This function will block until
  220. * it is being canceled using rtlsdr_cancel_async()
  221. *
  222. * \param dev the device handle given by rtlsdr_open()
  223. * \param cb callback function to return received samples
  224. * \param ctx user specific context to pass via the callback function
  225. * \param buf_num optional buffer count, buf_num * buf_len = overall buffer size
  226. * set to 0 for default buffer count (32)
  227. * \param buf_len optional buffer length, must be multiple of 512,
  228. * set to 0 for default buffer length (16 * 32 * 512)
  229. * \return 0 on success
  230. */
  231. RTLSDR_API int rtlsdr_read_async(rtlsdr_dev_t *dev,
  232. rtlsdr_read_async_cb_t cb,
  233. void *ctx,
  234. uint32_t buf_num,
  235. uint32_t buf_len);
  236. /*!
  237. * Cancel all pending asynchronous operations on the device.
  238. *
  239. * \param dev the device handle given by rtlsdr_open()
  240. * \return 0 on success
  241. */
  242. RTLSDR_API int rtlsdr_cancel_async(rtlsdr_dev_t *dev);
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif /* __RTL_SDR_H */