rtl-sdr.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index);
  30. RTLSDR_API int rtlsdr_close(rtlsdr_dev_t *dev);
  31. /* configuration functions */
  32. /*!
  33. * Set crystal oscillator frequencies used for the RTL2832 and the tuner IC.
  34. *
  35. * Usually both ICs use the same clock. Changing the clock may make sense if
  36. * you are applying an external clock to the tuner or to compensate the
  37. * frequency (and samplerate) error caused by the original cheap crystal.
  38. *
  39. * NOTE: Call this function only if you know what you are doing.
  40. *
  41. * \param dev the device handle given by rtlsdr_open()
  42. * \param rtl_freq frequency value used to clock the RTL2832 in Hz
  43. * \param tuner_freq frequency value used to clock the tuner IC in Hz
  44. * \return 0 on success
  45. */
  46. RTLSDR_API int rtlsdr_set_xtal_freq(rtlsdr_dev_t *dev, uint32_t rtl_freq,
  47. uint32_t tuner_freq);
  48. /*!
  49. * Get crystal oscillator frequencies used for the RTL2832 and the tuner IC.
  50. *
  51. * Usually both ICs use the same clock.
  52. *
  53. * \param dev the device handle given by rtlsdr_open()
  54. * \param rtl_freq frequency value used to clock the RTL2832 in Hz
  55. * \param tuner_freq frequency value used to clock the tuner IC in Hz
  56. * \return 0 on success
  57. */
  58. RTLSDR_API int rtlsdr_get_xtal_freq(rtlsdr_dev_t *dev, uint32_t *rtl_freq,
  59. uint32_t *tuner_freq);
  60. RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq);
  61. /*!
  62. * Get actual frequency the device is tuned to.
  63. *
  64. * \param dev the device handle given by rtlsdr_open()
  65. * \return 0 on error, frequency in Hz otherwise
  66. */
  67. RTLSDR_API uint32_t rtlsdr_get_center_freq(rtlsdr_dev_t *dev);
  68. RTLSDR_API int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm);
  69. RTLSDR_API int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev);
  70. /*!
  71. * Set the gain for the device.
  72. * Manual gain mode must be enabled for this to work.
  73. *
  74. * Valid gain values (in tenths of a dB) for the E4000 tuner:
  75. * -10, 15, 40, 65, 90, 115, 140, 165, 190,
  76. * 215, 240, 290, 340, 420, 430, 450, 470, 490
  77. *
  78. * \param dev the device handle given by rtlsdr_open()
  79. * \param gain in tenths of a dB, 115 means 11.5 dB.
  80. * \return 0 on success
  81. */
  82. RTLSDR_API int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain);
  83. /*!
  84. * Get actual gain the device is configured to.
  85. *
  86. * \param dev the device handle given by rtlsdr_open()
  87. * \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB.
  88. */
  89. RTLSDR_API int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev);
  90. /*!
  91. * Set the gain mode (automatic/manual) for the device.
  92. * Manual gain mode must be enabled for the gain setter function to work.
  93. *
  94. * \param dev the device handle given by rtlsdr_open()
  95. * \param manual gain mode, 1 means manual gain mode shall be enabled.
  96. * \return 0 on success
  97. */
  98. RTLSDR_API int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int manual);
  99. /* this will select the baseband filters according to the requested sample rate */
  100. RTLSDR_API int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate);
  101. /*!
  102. * Get actual sample rate the device is configured to.
  103. *
  104. * \param dev the device handle given by rtlsdr_open()
  105. * \return 0 on error, sample rate in Hz otherwise
  106. */
  107. RTLSDR_API uint32_t rtlsdr_get_sample_rate(rtlsdr_dev_t *dev);
  108. /*!
  109. * Enable test mode that returns an 8 bit counter instead of the samples.
  110. * The counter is generated inside the RTL2832.
  111. *
  112. * \param dev the device handle given by rtlsdr_open()
  113. * \param test mode, 1 means enabled, 0 disabled
  114. * \return 0 on success
  115. */
  116. RTLSDR_API int rtlsdr_set_testmode(rtlsdr_dev_t *dev, int on);
  117. /* streaming functions */
  118. RTLSDR_API int rtlsdr_reset_buffer(rtlsdr_dev_t *dev);
  119. RTLSDR_API int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read);
  120. typedef void(*rtlsdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx);
  121. /*!
  122. * Read samples from the device asynchronously. This function will block until
  123. * it is being canceled using rtlsdr_cancel_async()
  124. *
  125. * NOTE: This function is deprecated and is subject for removal.
  126. *
  127. * \param dev the device handle given by rtlsdr_open()
  128. * \param cb callback function to return received samples
  129. * \param ctx user specific context to pass via the callback function
  130. * \return 0 on success
  131. */
  132. RTLSDR_API int rtlsdr_wait_async(rtlsdr_dev_t *dev, rtlsdr_read_async_cb_t cb, void *ctx);
  133. /*!
  134. * Read samples from the device asynchronously. This function will block until
  135. * it is being canceled using rtlsdr_cancel_async()
  136. *
  137. * \param dev the device handle given by rtlsdr_open()
  138. * \param cb callback function to return received samples
  139. * \param ctx user specific context to pass via the callback function
  140. * \param buf_num optional buffer count, buf_num * buf_len = overall buffer size
  141. * set to 0 for default buffer count (32)
  142. * \param buf_len optional buffer length, must be multiple of 512,
  143. * set to 0 for default buffer length (16 * 32 * 512)
  144. * \return 0 on success
  145. */
  146. RTLSDR_API int rtlsdr_read_async(rtlsdr_dev_t *dev,
  147. rtlsdr_read_async_cb_t cb,
  148. void *ctx,
  149. uint32_t buf_num,
  150. uint32_t buf_len);
  151. /*!
  152. * Cancel all pending asynchronous operations on the device.
  153. *
  154. * \param dev the device handle given by rtlsdr_open()
  155. * \return 0 on success
  156. */
  157. RTLSDR_API int rtlsdr_cancel_async(rtlsdr_dev_t *dev);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif /* __RTL_SDR_H */