├── .gitignore ├── .travis.yml ├── AUTHORS ├── CMakeLists.txt ├── COPYING ├── Doxyfile.in ├── Makefile.am ├── README ├── cmake ├── Modules │ ├── FindLibUSB.cmake │ ├── FindThreads.cmake │ └── Version.cmake └── cmake_uninstall.cmake.in ├── configure.ac ├── git-version-gen ├── include ├── CMakeLists.txt ├── Makefile.am ├── reg_field.h ├── rtl-sdr.h ├── rtl-sdr_export.h ├── rtlsdr_i2c.h ├── tuner_e4k.h ├── tuner_fc0012.h ├── tuner_fc0013.h ├── tuner_fc2580.h └── tuner_r82xx.h ├── librtlsdr.pc.in ├── m4 └── .gitignore ├── rtl-sdr.rules ├── rtlsdr.conf └── src ├── CMakeLists.txt ├── Makefile.am ├── convenience ├── convenience.c └── convenience.h ├── getopt ├── getopt.c └── getopt.h ├── librtlsdr.c ├── rtl_adsb.c ├── rtl_eeprom.c ├── rtl_fm.c ├── rtl_power.c ├── rtl_sdr.c ├── rtl_tcp.c ├── rtl_test.c ├── tuner_e4k.c ├── tuner_fc0012.c ├── tuner_fc0013.c ├── tuner_fc2580.c └── tuner_r82xx.c /.gitignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | Makefile.in 3 | .deps 4 | .libs 5 | *.o 6 | *.lo 7 | *.la 8 | *.pc 9 | aclocal.m4 10 | acinclude.m4 11 | aminclude.am 12 | m4/*.m4 13 | autom4te.cache 14 | config.h* 15 | config.sub 16 | config.log 17 | config.status 18 | config.guess 19 | configure 20 | depcomp 21 | missing 22 | ltmain.sh 23 | install-sh 24 | stamp-h1 25 | libtool 26 | Doxyfile 27 | 28 | .tarball-version 29 | .version 30 | 31 | .*.swp 32 | 33 | doc/ 34 | 35 | src/rtl_sdr 36 | src/rtl_tcp 37 | 38 | CMakeCache.txt 39 | */CMakeFiles 40 | CMakeFiles 41 | *.cmake 42 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | compiler: 3 | - gcc 4 | - clang 5 | 6 | before_install: 7 | - sudo apt-get update -qq 8 | - sudo apt-get install -qq libusb-1.0-0-dev 9 | 10 | script: cmake . && make && sudo make install 11 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Steve Markgraf 2 | Dimitri Stolnikov 3 | Hoernchen 4 | Kyle Keen 5 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2012 OSMOCOM Project 2 | # 3 | # This file is part of rtl-sdr 4 | # 5 | # GNU Radio is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3, or (at your option) 8 | # any later version. 9 | # 10 | # GNU Radio is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with GNU Radio; see the file COPYING. If not, write to 17 | # the Free Software Foundation, Inc., 51 Franklin Street, 18 | # Boston, MA 02110-1301, USA. 19 | 20 | 21 | ######################################################################## 22 | # Project setup 23 | ######################################################################## 24 | cmake_minimum_required(VERSION 2.6) 25 | set(CMAKE_LEGACY_CYGWIN_WIN32 0) 26 | project(rtlsdr C) 27 | 28 | #select the release build type by default to get optimization flags 29 | if(NOT CMAKE_BUILD_TYPE) 30 | set(CMAKE_BUILD_TYPE "Release") 31 | message(STATUS "Build type not specified: defaulting to release.") 32 | endif(NOT CMAKE_BUILD_TYPE) 33 | set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "") 34 | 35 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules) 36 | 37 | if(NOT LIB_INSTALL_DIR) 38 | set(LIB_INSTALL_DIR lib) 39 | endif() 40 | 41 | # Set the version information here 42 | set(VERSION_INFO_MAJOR_VERSION 0) # increment major on api compatibility changes 43 | set(VERSION_INFO_MINOR_VERSION 5) # increment minor on feature-level changes 44 | set(VERSION_INFO_PATCH_VERSION git) # increment patch for bug fixes and docs 45 | include(Version) # setup version info 46 | 47 | ######################################################################## 48 | # Compiler specific setup 49 | ######################################################################## 50 | if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32) 51 | ADD_DEFINITIONS(-Wall) 52 | ADD_DEFINITIONS(-Wextra) 53 | ADD_DEFINITIONS(-Wno-unused-parameter) 54 | ADD_DEFINITIONS(-Wno-unused) 55 | ADD_DEFINITIONS(-Wsign-compare) 56 | ADD_DEFINITIONS(-Wdeclaration-after-statement) 57 | #http://gcc.gnu.org/wiki/Visibility 58 | add_definitions(-fvisibility=hidden) 59 | endif() 60 | 61 | ######################################################################## 62 | # Find build dependencies 63 | ######################################################################## 64 | find_package(PkgConfig) 65 | find_package(LibUSB) 66 | set(THREADS_USE_PTHREADS_WIN32 true) 67 | find_package(Threads) 68 | 69 | if(NOT LIBUSB_FOUND) 70 | message(FATAL_ERROR "LibUSB 1.0 required to compile rtl-sdr") 71 | endif() 72 | if(NOT THREADS_FOUND) 73 | message(FATAL_ERROR "pthreads(-win32) required to compile rtl-sdr") 74 | endif() 75 | ######################################################################## 76 | # Setup the include and linker paths 77 | ######################################################################## 78 | include_directories( 79 | ${CMAKE_SOURCE_DIR}/include 80 | ${LIBUSB_INCLUDE_DIR} 81 | ${THREADS_PTHREADS_INCLUDE_DIR} 82 | ) 83 | 84 | #link_directories( 85 | # ... 86 | #) 87 | 88 | # Set component parameters 89 | #set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE INTERNAL "" FORCE) 90 | 91 | ######################################################################## 92 | # Create uninstall target 93 | ######################################################################## 94 | configure_file( 95 | ${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in 96 | ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake 97 | @ONLY) 98 | 99 | add_custom_target(uninstall 100 | ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake 101 | ) 102 | 103 | ######################################################################## 104 | # Install udev rules 105 | ######################################################################## 106 | option(INSTALL_UDEV_RULES "Install udev rules for RTL-SDR" OFF) 107 | if (INSTALL_UDEV_RULES) 108 | install ( 109 | FILES rtl-sdr.rules 110 | DESTINATION "/etc/udev/rules.d" 111 | COMPONENT "udev" 112 | ) 113 | else (INSTALL_UDEV_RULES) 114 | message (STATUS "Udev rules not being installed, install them with -DINSTALL_UDEV_RULES=ON") 115 | endif (INSTALL_UDEV_RULES) 116 | 117 | option(DETACH_KERNEL_DRIVER "Detach kernel driver if loaded" OFF) 118 | if (DETACH_KERNEL_DRIVER) 119 | message (STATUS "Building with kernel driver detaching enabled") 120 | add_definitions(-DDETACH_KERNEL_DRIVER=1) 121 | else (DETACH_KERNEL_DRIVER) 122 | message (STATUS "Building with kernel driver detaching disabled, use -DDETACH_KERNEL_DRIVER=ON to enable") 123 | endif (DETACH_KERNEL_DRIVER) 124 | 125 | ######################################################################## 126 | # Add subdirectories 127 | ######################################################################## 128 | add_subdirectory(include) 129 | add_subdirectory(src) 130 | 131 | ######################################################################## 132 | # Create Pkg Config File 133 | ######################################################################## 134 | FOREACH(inc ${LIBUSB_INCLUDE_DIR}) 135 | LIST(APPEND RTLSDR_PC_CFLAGS "-I${inc}") 136 | ENDFOREACH(inc) 137 | 138 | FOREACH(lib ${LIBUSB_LIBRARY_DIRS}) 139 | LIST(APPEND RTLSDR_PC_LIBS "-L${lib}") 140 | ENDFOREACH(lib) 141 | 142 | # use space-separation format for the pc file 143 | STRING(REPLACE ";" " " RTLSDR_PC_CFLAGS "${RTLSDR_PC_CFLAGS}") 144 | STRING(REPLACE ";" " " RTLSDR_PC_LIBS "${RTLSDR_PC_LIBS}") 145 | 146 | # unset these vars to avoid hard-coded paths to cross environment 147 | IF(CMAKE_CROSSCOMPILING) 148 | UNSET(RTLSDR_PC_CFLAGS) 149 | UNSET(RTLSDR_PC_LIBS) 150 | ENDIF(CMAKE_CROSSCOMPILING) 151 | 152 | set(prefix ${CMAKE_INSTALL_PREFIX}) 153 | set(exec_prefix \${prefix}) 154 | set(libdir \${exec_prefix}/${LIB_INSTALL_DIR}) 155 | set(includedir \${prefix}/include) 156 | 157 | CONFIGURE_FILE( 158 | ${CMAKE_CURRENT_SOURCE_DIR}/librtlsdr.pc.in 159 | ${CMAKE_CURRENT_BINARY_DIR}/librtlsdr.pc 160 | @ONLY) 161 | 162 | INSTALL( 163 | FILES ${CMAKE_CURRENT_BINARY_DIR}/librtlsdr.pc 164 | DESTINATION ${LIB_INSTALL_DIR}/pkgconfig 165 | ) 166 | 167 | ######################################################################## 168 | # Print Summary 169 | ######################################################################## 170 | MESSAGE(STATUS "Building for version: ${VERSION} / ${LIBVER}") 171 | MESSAGE(STATUS "Using install prefix: ${CMAKE_INSTALL_PREFIX}") 172 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6 2 | ACLOCAL_AMFLAGS = -I m4 3 | 4 | INCLUDES = $(all_includes) -I$(top_srcdir)/include 5 | SUBDIRS = include src 6 | 7 | pkgconfigdir = $(libdir)/pkgconfig 8 | pkgconfig_DATA = librtlsdr.pc 9 | 10 | BUILT_SOURCES = $(top_srcdir)/.version 11 | $(top_srcdir)/.version: 12 | echo $(VERSION) > $@-t && mv $@-t $@ 13 | dist-hook: 14 | echo $(VERSION) > $(distdir)/.tarball-version 15 | 16 | install-udev-rules: 17 | $(INSTALL_DATA) rtl-sdr.rules /etc/udev/rules.d 18 | 19 | uninstall-udev-rules: 20 | rm -rf /etc/udev/rules.d/rtl-sdr.rules 21 | 22 | EXTRA_DIST = git-version-gen 23 | 24 | if HAVE_DOXYGEN 25 | 26 | pkgdocdir=$(docdir)/$(PACKAGE)-$(VERSION) 27 | doc_htmldir=$(pkgdocdir)/html 28 | 29 | doc_html_DATA = $(top_builddir)/doc/html.tar 30 | 31 | $(doc_html_DATA): $(top_builddir)/doc/html/index.html 32 | cd $(top_builddir)/doc && tar cf html.tar html 33 | 34 | $(top_builddir)/doc/html/index.html: $(SOURCES) Doxyfile 35 | @rm -rf doc 36 | mkdir -p doc 37 | $(DOXYGEN) Doxyfile 38 | 39 | install-data-hook: 40 | cd $(DESTDIR)$(doc_htmldir) && tar xf html.tar --strip-components 1 && rm -f html.tar 41 | 42 | uninstall-hook: 43 | cd $(DESTDIR) && rm -rf $(doc_htmldir) 44 | 45 | DX_CLEAN = doc/{html,latex}/* doc/html.tar 46 | 47 | endif 48 | 49 | MOSTLYCLEANFILES = $(DX_CLEAN) 50 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | rtl-sdr 2 | turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | ====================================================================== 4 | 5 | For more information see: 6 | http://sdr.osmocom.org/trac/wiki/rtl-sdr 7 | -------------------------------------------------------------------------------- /cmake/Modules/FindLibUSB.cmake: -------------------------------------------------------------------------------- 1 | if(NOT LIBUSB_FOUND) 2 | pkg_check_modules (LIBUSB_PKG libusb-1.0) 3 | find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h 4 | PATHS 5 | ${LIBUSB_PKG_INCLUDE_DIRS} 6 | /usr/include/libusb-1.0 7 | /usr/include 8 | /usr/local/include 9 | ) 10 | 11 | #standard library name for libusb-1.0 12 | set(libusb1_library_names usb-1.0) 13 | 14 | #libusb-1.0 compatible library on freebsd 15 | if((CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") OR (CMAKE_SYSTEM_NAME STREQUAL "kFreeBSD")) 16 | list(APPEND libusb1_library_names usb) 17 | endif() 18 | 19 | find_library(LIBUSB_LIBRARIES 20 | NAMES ${libusb1_library_names} 21 | PATHS 22 | ${LIBUSB_PKG_LIBRARY_DIRS} 23 | /usr/lib 24 | /usr/local/lib 25 | ) 26 | 27 | include(CheckFunctionExists) 28 | if(LIBUSB_INCLUDE_DIRS) 29 | set(CMAKE_REQUIRED_INCLUDES ${LIBUSB_INCLUDE_DIRS}) 30 | endif() 31 | if(LIBUSB_LIBRARIES) 32 | set(CMAKE_REQUIRED_LIBRARIES ${LIBUSB_LIBRARIES}) 33 | endif() 34 | 35 | CHECK_FUNCTION_EXISTS("libusb_handle_events_timeout_completed" HAVE_LIBUSB_HANDLE_EVENTS_TIMEOUT_COMPLETED) 36 | if(HAVE_LIBUSB_HANDLE_EVENTS_TIMEOUT_COMPLETED) 37 | add_definitions(-DHAVE_LIBUSB_HANDLE_EVENTS_TIMEOUT_COMPLETED=1) 38 | endif(HAVE_LIBUSB_HANDLE_EVENTS_TIMEOUT_COMPLETED) 39 | 40 | CHECK_FUNCTION_EXISTS("libusb_error_name" HAVE_LIBUSB_ERROR_NAME) 41 | if(HAVE_LIBUSB_ERROR_NAME) 42 | add_definitions(-DHAVE_LIBUSB_ERROR_NAME=1) 43 | endif(HAVE_LIBUSB_ERROR_NAME) 44 | 45 | if(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES) 46 | set(LIBUSB_FOUND TRUE CACHE INTERNAL "libusb-1.0 found") 47 | message(STATUS "Found libusb-1.0: ${LIBUSB_INCLUDE_DIR}, ${LIBUSB_LIBRARIES}") 48 | else(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES) 49 | set(LIBUSB_FOUND FALSE CACHE INTERNAL "libusb-1.0 found") 50 | message(STATUS "libusb-1.0 not found.") 51 | endif(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES) 52 | 53 | mark_as_advanced(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARIES) 54 | 55 | endif(NOT LIBUSB_FOUND) 56 | -------------------------------------------------------------------------------- /cmake/Modules/FindThreads.cmake: -------------------------------------------------------------------------------- 1 | # Updated FindThreads.cmake that supports pthread-win32 2 | # Downloaded from http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=6399 3 | 4 | # - This module determines the thread library of the system. 5 | # 6 | # The following variables are set 7 | # CMAKE_THREAD_LIBS_INIT - the thread library 8 | # CMAKE_USE_SPROC_INIT - are we using sproc? 9 | # CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads? 10 | # CMAKE_USE_PTHREADS_INIT - are we using pthreads 11 | # CMAKE_HP_PTHREADS_INIT - are we using hp pthreads 12 | # 13 | # If use of pthreads-win32 is desired, the following variables 14 | # can be set. 15 | # 16 | # THREADS_USE_PTHREADS_WIN32 - 17 | # Setting this to true searches for the pthreads-win32 18 | # port (since CMake 2.8.0) 19 | # 20 | # THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME 21 | # C = no exceptions (default) 22 | # (NOTE: This is the default scheme on most POSIX thread 23 | # implementations and what you should probably be using) 24 | # CE = C++ Exception Handling 25 | # SE = Structure Exception Handling (MSVC only) 26 | # (NOTE: Changing this option from the default may affect 27 | # the portability of your application. See pthreads-win32 28 | # documentation for more details.) 29 | # 30 | #====================================================== 31 | # Example usage where threading library 32 | # is provided by the system: 33 | # 34 | # find_package(Threads REQUIRED) 35 | # add_executable(foo foo.cc) 36 | # target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT}) 37 | # 38 | # Example usage if pthreads-win32 is desired on Windows 39 | # or a system provided thread library: 40 | # 41 | # set(THREADS_USE_PTHREADS_WIN32 true) 42 | # find_package(Threads REQUIRED) 43 | # include_directories(${THREADS_PTHREADS_INCLUDE_DIR}) 44 | # 45 | # add_executable(foo foo.cc) 46 | # target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT}) 47 | # 48 | 49 | INCLUDE (CheckIncludeFiles) 50 | INCLUDE (CheckLibraryExists) 51 | SET(Threads_FOUND FALSE) 52 | 53 | IF(WIN32 AND NOT CYGWIN AND THREADS_USE_PTHREADS_WIN32) 54 | SET(_Threads_ptwin32 true) 55 | ENDIF() 56 | 57 | # Do we have sproc? 58 | IF(CMAKE_SYSTEM MATCHES IRIX) 59 | CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H) 60 | ENDIF() 61 | 62 | IF(CMAKE_HAVE_SPROC_H) 63 | # We have sproc 64 | SET(CMAKE_USE_SPROC_INIT 1) 65 | 66 | ELSEIF(_Threads_ptwin32) 67 | 68 | IF(NOT DEFINED THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME) 69 | # Assign the default scheme 70 | SET(THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME "C") 71 | ELSE() 72 | # Validate the scheme specified by the user 73 | IF(NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "C" AND 74 | NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "CE" AND 75 | NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE") 76 | MESSAGE(FATAL_ERROR "See documentation for FindPthreads.cmake, only C, CE, and SE modes are allowed") 77 | ENDIF() 78 | IF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE") 79 | MESSAGE(FATAL_ERROR "Structured Exception Handling is only allowed for MSVC") 80 | ENDIF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE") 81 | ENDIF() 82 | 83 | FIND_PATH(THREADS_PTHREADS_INCLUDE_DIR pthread.h) 84 | 85 | # Determine the library filename 86 | IF(MSVC) 87 | SET(_Threads_pthreads_libname 88 | pthreadV${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2) 89 | ELSEIF(MINGW) 90 | SET(_Threads_pthreads_libname 91 | pthreadG${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2) 92 | ELSE() 93 | MESSAGE(FATAL_ERROR "This should never happen") 94 | ENDIF() 95 | 96 | # Use the include path to help find the library if possible 97 | SET(_Threads_lib_paths "") 98 | IF(THREADS_PTHREADS_INCLUDE_DIR) 99 | GET_FILENAME_COMPONENT(_Threads_root_dir 100 | ${THREADS_PTHREADS_INCLUDE_DIR} PATH) 101 | SET(_Threads_lib_paths ${_Threads_root_dir}/lib) 102 | ENDIF() 103 | FIND_LIBRARY(THREADS_PTHREADS_WIN32_LIBRARY 104 | NAMES ${_Threads_pthreads_libname} 105 | PATHS ${_Threads_lib_paths} 106 | DOC "The Portable Threads Library for Win32" 107 | NO_SYSTEM_PATH 108 | ) 109 | 110 | IF(THREADS_PTHREADS_INCLUDE_DIR AND THREADS_PTHREADS_WIN32_LIBRARY) 111 | MARK_AS_ADVANCED(THREADS_PTHREADS_INCLUDE_DIR) 112 | SET(CMAKE_THREAD_LIBS_INIT ${THREADS_PTHREADS_WIN32_LIBRARY}) 113 | SET(CMAKE_HAVE_THREADS_LIBRARY 1) 114 | SET(Threads_FOUND TRUE) 115 | ENDIF() 116 | 117 | MARK_AS_ADVANCED(THREADS_PTHREADS_WIN32_LIBRARY) 118 | 119 | ELSE() 120 | # Do we have pthreads? 121 | CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H) 122 | IF(CMAKE_HAVE_PTHREAD_H) 123 | 124 | # 125 | # We have pthread.h 126 | # Let's check for the library now. 127 | # 128 | SET(CMAKE_HAVE_THREADS_LIBRARY) 129 | IF(NOT THREADS_HAVE_PTHREAD_ARG) 130 | 131 | # Do we have -lpthreads 132 | CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE) 133 | IF(CMAKE_HAVE_PTHREADS_CREATE) 134 | SET(CMAKE_THREAD_LIBS_INIT "-lpthreads") 135 | SET(CMAKE_HAVE_THREADS_LIBRARY 1) 136 | SET(Threads_FOUND TRUE) 137 | ENDIF() 138 | 139 | # Ok, how about -lpthread 140 | CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE) 141 | IF(CMAKE_HAVE_PTHREAD_CREATE) 142 | SET(CMAKE_THREAD_LIBS_INIT "-lpthread") 143 | SET(Threads_FOUND TRUE) 144 | SET(CMAKE_HAVE_THREADS_LIBRARY 1) 145 | ENDIF() 146 | 147 | IF(CMAKE_SYSTEM MATCHES "SunOS.*") 148 | # On sun also check for -lthread 149 | CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE) 150 | IF(CMAKE_HAVE_THR_CREATE) 151 | SET(CMAKE_THREAD_LIBS_INIT "-lthread") 152 | SET(CMAKE_HAVE_THREADS_LIBRARY 1) 153 | SET(Threads_FOUND TRUE) 154 | ENDIF() 155 | ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*") 156 | 157 | ENDIF(NOT THREADS_HAVE_PTHREAD_ARG) 158 | 159 | IF(NOT CMAKE_HAVE_THREADS_LIBRARY) 160 | # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread 161 | IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG") 162 | MESSAGE(STATUS "Check if compiler accepts -pthread") 163 | TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG 164 | ${CMAKE_BINARY_DIR} 165 | ${CMAKE_ROOT}/Modules/CheckForPthreads.c 166 | CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread 167 | COMPILE_OUTPUT_VARIABLE OUTPUT) 168 | 169 | IF(THREADS_HAVE_PTHREAD_ARG) 170 | IF(THREADS_PTHREAD_ARG MATCHES "^2$") 171 | SET(Threads_FOUND TRUE) 172 | MESSAGE(STATUS "Check if compiler accepts -pthread - yes") 173 | ELSE() 174 | MESSAGE(STATUS "Check if compiler accepts -pthread - no") 175 | FILE(APPEND 176 | ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 177 | "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n") 178 | ENDIF() 179 | ELSE() 180 | MESSAGE(STATUS "Check if compiler accepts -pthread - no") 181 | FILE(APPEND 182 | ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 183 | "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n") 184 | ENDIF() 185 | 186 | ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG") 187 | 188 | IF(THREADS_HAVE_PTHREAD_ARG) 189 | SET(Threads_FOUND TRUE) 190 | SET(CMAKE_THREAD_LIBS_INIT "-pthread") 191 | ENDIF() 192 | 193 | ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY) 194 | ENDIF(CMAKE_HAVE_PTHREAD_H) 195 | ENDIF() 196 | 197 | IF(CMAKE_THREAD_LIBS_INIT) 198 | SET(CMAKE_USE_PTHREADS_INIT 1) 199 | SET(Threads_FOUND TRUE) 200 | ENDIF() 201 | 202 | IF(CMAKE_SYSTEM MATCHES "Windows" 203 | AND NOT THREADS_USE_PTHREADS_WIN32) 204 | SET(CMAKE_USE_WIN32_THREADS_INIT 1) 205 | SET(Threads_FOUND TRUE) 206 | ENDIF() 207 | 208 | IF(CMAKE_USE_PTHREADS_INIT) 209 | IF(CMAKE_SYSTEM MATCHES "HP-UX-*") 210 | # Use libcma if it exists and can be used. It provides more 211 | # symbols than the plain pthread library. CMA threads 212 | # have actually been deprecated: 213 | # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395 214 | # http://docs.hp.com/en/947/d8.html 215 | # but we need to maintain compatibility here. 216 | # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads 217 | # are available. 218 | CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA) 219 | IF(CMAKE_HAVE_HP_CMA) 220 | SET(CMAKE_THREAD_LIBS_INIT "-lcma") 221 | SET(CMAKE_HP_PTHREADS_INIT 1) 222 | SET(Threads_FOUND TRUE) 223 | ENDIF(CMAKE_HAVE_HP_CMA) 224 | SET(CMAKE_USE_PTHREADS_INIT 1) 225 | ENDIF() 226 | 227 | IF(CMAKE_SYSTEM MATCHES "OSF1-V*") 228 | SET(CMAKE_USE_PTHREADS_INIT 0) 229 | SET(CMAKE_THREAD_LIBS_INIT ) 230 | ENDIF() 231 | 232 | IF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*") 233 | SET(CMAKE_USE_PTHREADS_INIT 1) 234 | SET(Threads_FOUND TRUE) 235 | SET(CMAKE_THREAD_LIBS_INIT ) 236 | SET(CMAKE_USE_WIN32_THREADS_INIT 0) 237 | ENDIF() 238 | ENDIF(CMAKE_USE_PTHREADS_INIT) 239 | 240 | INCLUDE(FindPackageHandleStandardArgs) 241 | IF(_Threads_ptwin32) 242 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG 243 | THREADS_PTHREADS_WIN32_LIBRARY THREADS_PTHREADS_INCLUDE_DIR) 244 | ELSE() 245 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND) 246 | ENDIF() 247 | -------------------------------------------------------------------------------- /cmake/Modules/Version.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2013 OSMOCOM Project 2 | # 3 | # This file is part of rtl-sdr 4 | # 5 | # rtl-sdr is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3, or (at your option) 8 | # any later version. 9 | # 10 | # rtl-sdr is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with rtl-sdr; see the file COPYING. If not, write to 17 | # the Free Software Foundation, Inc., 51 Franklin Street, 18 | # Boston, MA 02110-1301, USA. 19 | 20 | if(DEFINED __INCLUDED_VERSION_CMAKE) 21 | return() 22 | endif() 23 | set(__INCLUDED_VERSION_CMAKE TRUE) 24 | 25 | # VERSION_INFO_* variables must be provided by user 26 | set(MAJOR_VERSION ${VERSION_INFO_MAJOR_VERSION}) 27 | set(MINOR_VERSION ${VERSION_INFO_MINOR_VERSION}) 28 | set(PATCH_VERSION ${VERSION_INFO_PATCH_VERSION}) 29 | 30 | ######################################################################## 31 | # Extract the version string from git describe. 32 | ######################################################################## 33 | find_package(Git QUIET) 34 | 35 | if(GIT_FOUND) 36 | message(STATUS "Extracting version information from git describe...") 37 | execute_process( 38 | COMMAND ${GIT_EXECUTABLE} describe --always --abbrev=4 --long 39 | OUTPUT_VARIABLE GIT_DESCRIBE OUTPUT_STRIP_TRAILING_WHITESPACE 40 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 41 | ) 42 | else() 43 | set(GIT_DESCRIBE "v${MAJOR_VERSION}.${MINOR_VERSION}.x-xxx-xunknown") 44 | endif() 45 | 46 | ######################################################################## 47 | # Use the logic below to set the version constants 48 | ######################################################################## 49 | if("${PATCH_VERSION}" STREQUAL "git") 50 | # VERSION: 3.6git-xxx-gxxxxxxxx 51 | # LIBVER: 3.6git 52 | set(VERSION "${GIT_DESCRIBE}") 53 | set(LIBVER "${MAJOR_VERSION}.${MINOR_VERSION}${PATCH_VERSION}") 54 | else() 55 | # This is a numbered release. 56 | # VERSION: 3.6.1 57 | # LIBVER: 3.6.1 58 | set(VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}") 59 | set(LIBVER "${VERSION}") 60 | endif() 61 | -------------------------------------------------------------------------------- /cmake/cmake_uninstall.cmake.in: -------------------------------------------------------------------------------- 1 | # http://www.vtk.org/Wiki/CMake_FAQ#Can_I_do_.22make_uninstall.22_with_CMake.3F 2 | 3 | IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") 4 | MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"") 5 | ENDIF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") 6 | 7 | FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) 8 | STRING(REGEX REPLACE "\n" ";" files "${files}") 9 | FOREACH(file ${files}) 10 | MESSAGE(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") 11 | IF(EXISTS "$ENV{DESTDIR}${file}") 12 | EXEC_PROGRAM( 13 | "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" 14 | OUTPUT_VARIABLE rm_out 15 | RETURN_VALUE rm_retval 16 | ) 17 | IF(NOT "${rm_retval}" STREQUAL 0) 18 | MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") 19 | ENDIF(NOT "${rm_retval}" STREQUAL 0) 20 | ELSEIF(IS_SYMLINK "$ENV{DESTDIR}${file}") 21 | EXEC_PROGRAM( 22 | "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" 23 | OUTPUT_VARIABLE rm_out 24 | RETURN_VALUE rm_retval 25 | ) 26 | IF(NOT "${rm_retval}" STREQUAL 0) 27 | MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") 28 | ENDIF(NOT "${rm_retval}" STREQUAL 0) 29 | ELSE(EXISTS "$ENV{DESTDIR}${file}") 30 | MESSAGE(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.") 31 | ENDIF(EXISTS "$ENV{DESTDIR}${file}") 32 | ENDFOREACH(file) 33 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([librtlsdr], 2 | m4_esyscmd([./git-version-gen .tarball-version]), 3 | [osmocom-sdr@lists.osmocom.org]) 4 | 5 | AM_INIT_AUTOMAKE([dist-bzip2]) 6 | 7 | dnl kernel style compile messages 8 | m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) 9 | 10 | dnl checks for programs 11 | AC_PROG_MAKE_SET 12 | AC_PROG_CC 13 | AC_PROG_INSTALL 14 | AM_PROG_CC_C_O 15 | LT_INIT 16 | AC_PROG_LIBTOOL 17 | 18 | PKG_CHECK_MODULES(LIBUSB, libusb-1.0 >= 1.0) 19 | LIBS="$LIBS $LIBUSB_LIBS" 20 | CFLAGS="$CFLAGS $LIBUSB_CFLAGS" 21 | 22 | AC_PATH_PROG(DOXYGEN,doxygen,false) 23 | AM_CONDITIONAL(HAVE_DOXYGEN, test $DOXYGEN != false) 24 | 25 | AC_CONFIG_MACRO_DIR([m4]) 26 | 27 | dnl checks for header files 28 | AC_HEADER_STDC 29 | AC_CHECK_HEADERS(sys/types.h) 30 | AC_CHECK_HEADERS(pthread.h,, [AC_MSG_ERROR([pthread.h required])]) 31 | 32 | # pc variables 33 | AC_SUBST(RTLSDR_PC_LIBS,["$LIBS"]) 34 | AC_SUBST(RTLSDR_PC_CFLAGS,["$CFLAGS"]) 35 | 36 | dnl checks for required libraries 37 | dnl pthreads 38 | AC_CHECK_LIB(pthread, pthread_create, [LIBS="$LIBS -lpthread"]) 39 | 40 | dnl libmath (for rtl_fm) 41 | AC_CHECK_LIB(m, atan2, [LIBS="$LIBS -lm"]) 42 | 43 | dnl libmath (for rtl_adsb) 44 | AC_CHECK_LIB(m, sqrt, [LIBS="$LIBS -lm"]) 45 | 46 | dnl libmath (for rtl_power) 47 | AC_CHECK_LIB(m, atan2, [LIBS="$LIBS -lm"]) 48 | 49 | dnl librealtime (for rtl_test, rtl_fm) 50 | AC_CHECK_LIB(rt, clock_gettime, [LIBS="$LIBS -lrt"]) 51 | 52 | # The following test is taken from WebKit's webkit.m4 53 | saved_CFLAGS="$CFLAGS" 54 | CFLAGS="$CFLAGS -fvisibility=hidden " 55 | AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden]) 56 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([char foo;])], 57 | [ AC_MSG_RESULT([yes]) 58 | SYMBOL_VISIBILITY="-fvisibility=hidden"], 59 | AC_MSG_RESULT([no])) 60 | CFLAGS="$saved_CFLAGS" 61 | AC_SUBST(SYMBOL_VISIBILITY) 62 | 63 | AC_MSG_CHECKING(whether compiler understands -Wall) 64 | old_CFLAGS="$CFLAGS" 65 | CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-parameter -Wno-unused -Wsign-compare -Wdeclaration-after-statement" 66 | AC_TRY_COMPILE([],[], 67 | AC_MSG_RESULT(yes), 68 | AC_MSG_RESULT(no) 69 | CFLAGS="$old_CFLAGS") 70 | 71 | AC_ARG_ENABLE(driver-detach, 72 | [ --enable-driver-detach Enable detaching of kernel driver (disabled by default)], 73 | [if test x$enableval = xyes; then 74 | CFLAGS="$CFLAGS -DDETACH_KERNEL_DRIVER" 75 | fi]) 76 | 77 | dnl Generate the output 78 | AC_CONFIG_HEADER(config.h) 79 | 80 | AC_OUTPUT( 81 | librtlsdr.pc 82 | include/Makefile 83 | src/Makefile 84 | Makefile 85 | Doxyfile 86 | ) 87 | -------------------------------------------------------------------------------- /git-version-gen: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Print a version string. 3 | scriptversion=2010-01-28.01 4 | 5 | # Copyright (C) 2007-2010 Free Software Foundation, Inc. 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU General Public License as published by 9 | # the Free Software Foundation; either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | 20 | # This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/. 21 | # It may be run two ways: 22 | # - from a git repository in which the "git describe" command below 23 | # produces useful output (thus requiring at least one signed tag) 24 | # - from a non-git-repo directory containing a .tarball-version file, which 25 | # presumes this script is invoked like "./git-version-gen .tarball-version". 26 | 27 | # In order to use intra-version strings in your project, you will need two 28 | # separate generated version string files: 29 | # 30 | # .tarball-version - present only in a distribution tarball, and not in 31 | # a checked-out repository. Created with contents that were learned at 32 | # the last time autoconf was run, and used by git-version-gen. Must not 33 | # be present in either $(srcdir) or $(builddir) for git-version-gen to 34 | # give accurate answers during normal development with a checked out tree, 35 | # but must be present in a tarball when there is no version control system. 36 | # Therefore, it cannot be used in any dependencies. GNUmakefile has 37 | # hooks to force a reconfigure at distribution time to get the value 38 | # correct, without penalizing normal development with extra reconfigures. 39 | # 40 | # .version - present in a checked-out repository and in a distribution 41 | # tarball. Usable in dependencies, particularly for files that don't 42 | # want to depend on config.h but do want to track version changes. 43 | # Delete this file prior to any autoconf run where you want to rebuild 44 | # files to pick up a version string change; and leave it stale to 45 | # minimize rebuild time after unrelated changes to configure sources. 46 | # 47 | # It is probably wise to add these two files to .gitignore, so that you 48 | # don't accidentally commit either generated file. 49 | # 50 | # Use the following line in your configure.ac, so that $(VERSION) will 51 | # automatically be up-to-date each time configure is run (and note that 52 | # since configure.ac no longer includes a version string, Makefile rules 53 | # should not depend on configure.ac for version updates). 54 | # 55 | # AC_INIT([GNU project], 56 | # m4_esyscmd([build-aux/git-version-gen .tarball-version]), 57 | # [bug-project@example]) 58 | # 59 | # Then use the following lines in your Makefile.am, so that .version 60 | # will be present for dependencies, and so that .tarball-version will 61 | # exist in distribution tarballs. 62 | # 63 | # BUILT_SOURCES = $(top_srcdir)/.version 64 | # $(top_srcdir)/.version: 65 | # echo $(VERSION) > $@-t && mv $@-t $@ 66 | # dist-hook: 67 | # echo $(VERSION) > $(distdir)/.tarball-version 68 | 69 | case $# in 70 | 1) ;; 71 | *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;; 72 | esac 73 | 74 | tarball_version_file=$1 75 | nl=' 76 | ' 77 | 78 | # First see if there is a tarball-only version file. 79 | # then try "git describe", then default. 80 | if test -f $tarball_version_file 81 | then 82 | v=`cat $tarball_version_file` || exit 1 83 | case $v in 84 | *$nl*) v= ;; # reject multi-line output 85 | [0-9]*) ;; 86 | *) v= ;; 87 | esac 88 | test -z "$v" \ 89 | && echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2 90 | fi 91 | 92 | if test -n "$v" 93 | then 94 | : # use $v 95 | elif 96 | v=`git describe --abbrev=4 --match='v*' HEAD 2>/dev/null \ 97 | || git describe --abbrev=4 HEAD 2>/dev/null` \ 98 | && case $v in 99 | [0-9]*) ;; 100 | v[0-9]*) ;; 101 | *) (exit 1) ;; 102 | esac 103 | then 104 | # Is this a new git that lists number of commits since the last 105 | # tag or the previous older version that did not? 106 | # Newer: v6.10-77-g0f8faeb 107 | # Older: v6.10-g0f8faeb 108 | case $v in 109 | *-*-*) : git describe is okay three part flavor ;; 110 | *-*) 111 | : git describe is older two part flavor 112 | # Recreate the number of commits and rewrite such that the 113 | # result is the same as if we were using the newer version 114 | # of git describe. 115 | vtag=`echo "$v" | sed 's/-.*//'` 116 | numcommits=`git rev-list "$vtag"..HEAD | wc -l` 117 | v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`; 118 | ;; 119 | esac 120 | 121 | # Change the first '-' to a '.', so version-comparing tools work properly. 122 | # Remove the "g" in git describe's output string, to save a byte. 123 | v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`; 124 | else 125 | v=UNKNOWN 126 | fi 127 | 128 | v=`echo "$v" |sed 's/^v//'` 129 | 130 | # Don't declare a version "dirty" merely because a time stamp has changed. 131 | git status > /dev/null 2>&1 132 | 133 | dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty= 134 | case "$dirty" in 135 | '') ;; 136 | *) # Append the suffix only if there isn't one already. 137 | case $v in 138 | *-dirty) ;; 139 | *) v="$v-dirty" ;; 140 | esac ;; 141 | esac 142 | 143 | # Omit the trailing newline, so that m4_esyscmd can use the result directly. 144 | echo "$v" | tr -d '\012' 145 | 146 | # Local variables: 147 | # eval: (add-hook 'write-file-hooks 'time-stamp) 148 | # time-stamp-start: "scriptversion=" 149 | # time-stamp-format: "%:y-%02m-%02d.%02H" 150 | # time-stamp-end: "$" 151 | # End: 152 | -------------------------------------------------------------------------------- /include/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2012 OSMOCOM Project 2 | # 3 | # This file is part of rtl-sdr 4 | # 5 | # GNU Radio is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3, or (at your option) 8 | # any later version. 9 | # 10 | # GNU Radio is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with GNU Radio; see the file COPYING. If not, write to 17 | # the Free Software Foundation, Inc., 51 Franklin Street, 18 | # Boston, MA 02110-1301, USA. 19 | 20 | ######################################################################## 21 | # Install public header files 22 | ######################################################################## 23 | install(FILES 24 | rtl-sdr.h 25 | rtl-sdr_export.h 26 | DESTINATION include 27 | ) 28 | -------------------------------------------------------------------------------- /include/Makefile.am: -------------------------------------------------------------------------------- 1 | rtlsdr_HEADERS = rtl-sdr.h rtl-sdr_export.h 2 | 3 | noinst_HEADERS = reg_field.h rtlsdr_i2c.h tuner_e4k.h tuner_fc0012.h tuner_fc0013.h tuner_fc2580.h tuner_r82xx.h 4 | 5 | rtlsdrdir = $(includedir) 6 | -------------------------------------------------------------------------------- /include/reg_field.h: -------------------------------------------------------------------------------- 1 | #ifndef _REG_FIELD_H 2 | #define _REG_FIELD_H 3 | 4 | #include 5 | #include 6 | 7 | enum cmd_op { 8 | CMD_OP_GET = (1 << 0), 9 | CMD_OP_SET = (1 << 1), 10 | CMD_OP_EXEC = (1 << 2), 11 | }; 12 | 13 | enum pstate { 14 | ST_IN_CMD, 15 | ST_IN_ARG, 16 | }; 17 | 18 | struct strbuf { 19 | uint8_t idx; 20 | char buf[32]; 21 | }; 22 | 23 | struct cmd_state { 24 | struct strbuf cmd; 25 | struct strbuf arg; 26 | enum pstate state; 27 | void (*out)(const char *format, va_list ap); 28 | }; 29 | 30 | struct cmd { 31 | const char *cmd; 32 | uint32_t ops; 33 | int (*cb)(struct cmd_state *cs, enum cmd_op op, const char *cmd, 34 | int argc, char **argv); 35 | const char *help; 36 | }; 37 | 38 | /* structure describing a field in a register */ 39 | struct reg_field { 40 | uint8_t reg; 41 | uint8_t shift; 42 | uint8_t width; 43 | }; 44 | 45 | struct reg_field_ops { 46 | const struct reg_field *fields; 47 | const char **field_names; 48 | uint32_t num_fields; 49 | void *data; 50 | int (*write_cb)(void *data, uint32_t reg, uint32_t val); 51 | uint32_t (*read_cb)(void *data, uint32_t reg); 52 | }; 53 | 54 | uint32_t reg_field_read(struct reg_field_ops *ops, struct reg_field *field); 55 | int reg_field_write(struct reg_field_ops *ops, struct reg_field *field, uint32_t val); 56 | int reg_field_cmd(struct cmd_state *cs, enum cmd_op op, 57 | const char *cmd, int argc, char **argv, 58 | struct reg_field_ops *ops); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/rtl-sdr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * Copyright (C) 2012-2013 by Steve Markgraf 4 | * Copyright (C) 2012 by Dimitri Stolnikov 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 . 18 | */ 19 | 20 | #ifndef __RTL_SDR_H 21 | #define __RTL_SDR_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #include 28 | #include 29 | 30 | typedef struct rtlsdr_dev rtlsdr_dev_t; 31 | 32 | RTLSDR_API uint32_t rtlsdr_get_device_count(void); 33 | 34 | RTLSDR_API const char* rtlsdr_get_device_name(uint32_t index); 35 | 36 | /*! 37 | * Get USB device strings. 38 | * 39 | * NOTE: The string arguments must provide space for up to 256 bytes. 40 | * 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 46 | */ 47 | RTLSDR_API int rtlsdr_get_device_usb_strings(uint32_t index, 48 | char *manufact, 49 | char *product, 50 | char *serial); 51 | 52 | /*! 53 | * Get device index by USB serial string descriptor. 54 | * 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 60 | */ 61 | RTLSDR_API int rtlsdr_get_index_by_serial(const char *serial); 62 | 63 | RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index); 64 | 65 | RTLSDR_API int rtlsdr_close(rtlsdr_dev_t *dev); 66 | 67 | /* configuration functions */ 68 | 69 | /*! 70 | * Set crystal oscillator frequencies used for the RTL2832 and the tuner IC. 71 | * 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. 75 | * 76 | * NOTE: Call this function only if you fully understand the implications. 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_set_xtal_freq(rtlsdr_dev_t *dev, uint32_t rtl_freq, 84 | uint32_t tuner_freq); 85 | 86 | /*! 87 | * Get crystal oscillator frequencies used for the RTL2832 and the tuner IC. 88 | * 89 | * Usually both ICs use the same clock. 90 | * 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 95 | */ 96 | RTLSDR_API int rtlsdr_get_xtal_freq(rtlsdr_dev_t *dev, uint32_t *rtl_freq, 97 | uint32_t *tuner_freq); 98 | 99 | /*! 100 | * Get USB device strings. 101 | * 102 | * NOTE: The string arguments must provide space for up to 256 bytes. 103 | * 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 109 | */ 110 | RTLSDR_API int rtlsdr_get_usb_strings(rtlsdr_dev_t *dev, char *manufact, 111 | char *product, char *serial); 112 | 113 | /*! 114 | * Write the device EEPROM 115 | * 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 124 | */ 125 | 126 | RTLSDR_API int rtlsdr_write_eeprom(rtlsdr_dev_t *dev, uint8_t *data, 127 | uint8_t offset, uint16_t len); 128 | 129 | /*! 130 | * Read the device EEPROM 131 | * 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 140 | */ 141 | 142 | RTLSDR_API int rtlsdr_read_eeprom(rtlsdr_dev_t *dev, uint8_t *data, 143 | uint8_t offset, uint16_t len); 144 | 145 | RTLSDR_API int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq); 146 | 147 | RTLSDR_API int rtlsdr_set_if_freq(rtlsdr_dev_t *dev, uint32_t freq); 148 | 149 | RTLSDR_API int rtlsdr_set_if_bandwidth(rtlsdr_dev_t *dev, int bw); 150 | 151 | /*! 152 | * Get actual frequency the device is tuned to. 153 | * 154 | * \param dev the device handle given by rtlsdr_open() 155 | * \return 0 on error, frequency in Hz otherwise 156 | */ 157 | RTLSDR_API uint32_t rtlsdr_get_center_freq(rtlsdr_dev_t *dev); 158 | 159 | /*! 160 | * Set the frequency correction value for the device. 161 | * 162 | * \param dev the device handle given by rtlsdr_open() 163 | * \param ppm correction value in parts per million (ppm) 164 | * \return 0 on success 165 | */ 166 | RTLSDR_API int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm); 167 | 168 | /*! 169 | * Get actual frequency correction value of the device. 170 | * 171 | * \param dev the device handle given by rtlsdr_open() 172 | * \return correction value in parts per million (ppm) 173 | */ 174 | RTLSDR_API int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev); 175 | 176 | enum rtlsdr_tuner { 177 | RTLSDR_TUNER_UNKNOWN = 0, 178 | RTLSDR_TUNER_E4000, 179 | RTLSDR_TUNER_FC0012, 180 | RTLSDR_TUNER_FC0013, 181 | RTLSDR_TUNER_FC2580, 182 | RTLSDR_TUNER_R820T, 183 | RTLSDR_TUNER_R828D 184 | }; 185 | 186 | /*! 187 | * Get the tuner type. 188 | * 189 | * \param dev the device handle given by rtlsdr_open() 190 | * \return RTLSDR_TUNER_UNKNOWN on error, tuner type otherwise 191 | */ 192 | RTLSDR_API enum rtlsdr_tuner rtlsdr_get_tuner_type(rtlsdr_dev_t *dev); 193 | 194 | /*! 195 | * Get a list of gains supported by the tuner. 196 | * 197 | * NOTE: The gains argument must be preallocated by the caller. If NULL is 198 | * being given instead, the number of available gain values will be returned. 199 | * 200 | * \param dev the device handle given by rtlsdr_open() 201 | * \param gains array of gain values. In tenths of a dB, 115 means 11.5 dB. 202 | * \return <= 0 on error, number of available (returned) gain values otherwise 203 | */ 204 | RTLSDR_API int rtlsdr_get_tuner_gains(rtlsdr_dev_t *dev, int *gains); 205 | 206 | /*! 207 | * Set the gain for the device. 208 | * Manual gain mode must be enabled for this to work. 209 | * 210 | * Valid gain values (in tenths of a dB) for the E4000 tuner: 211 | * -10, 15, 40, 65, 90, 115, 140, 165, 190, 212 | * 215, 240, 290, 340, 420, 430, 450, 470, 490 213 | * 214 | * Valid gain values may be queried with \ref rtlsdr_get_tuner_gains function. 215 | * 216 | * \param dev the device handle given by rtlsdr_open() 217 | * \param gain in tenths of a dB, 115 means 11.5 dB. 218 | * \return 0 on success 219 | */ 220 | RTLSDR_API int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain); 221 | 222 | /*! 223 | * Get actual gain the device is configured to. 224 | * 225 | * \param dev the device handle given by rtlsdr_open() 226 | * \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB. 227 | */ 228 | RTLSDR_API int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev); 229 | 230 | /*! 231 | * Set the intermediate frequency gain for the device. 232 | * 233 | * \param dev the device handle given by rtlsdr_open() 234 | * \param stage intermediate frequency gain stage number (1 to 6 for E4000) 235 | * \param gain in tenths of a dB, -30 means -3.0 dB. 236 | * \return 0 on success 237 | */ 238 | RTLSDR_API int rtlsdr_set_tuner_if_gain(rtlsdr_dev_t *dev, int stage, int gain); 239 | 240 | /*! 241 | * Set the gain mode (automatic/manual) for the device. 242 | * Manual gain mode must be enabled for the gain setter function to work. 243 | * 244 | * \param dev the device handle given by rtlsdr_open() 245 | * \param manual gain mode, 1 means manual gain mode shall be enabled. 246 | * \return 0 on success 247 | */ 248 | RTLSDR_API int rtlsdr_set_tuner_gain_mode(rtlsdr_dev_t *dev, int manual); 249 | 250 | /*! 251 | * Set the sample rate for the device, also selects the baseband filters 252 | * according to the requested sample rate for tuners where this is possible. 253 | * 254 | * \param dev the device handle given by rtlsdr_open() 255 | * \param samp_rate the sample rate to be set, possible values are: 256 | * 225001 - 300000 Hz 257 | * 900001 - 3200000 Hz 258 | * sample loss is to be expected for rates > 2400000 259 | * \return 0 on success, -EINVAL on invalid rate 260 | */ 261 | RTLSDR_API int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate); 262 | 263 | /*! 264 | * Get actual sample rate the device is configured to. 265 | * 266 | * \param dev the device handle given by rtlsdr_open() 267 | * \return 0 on error, sample rate in Hz otherwise 268 | */ 269 | RTLSDR_API uint32_t rtlsdr_get_sample_rate(rtlsdr_dev_t *dev); 270 | 271 | /*! 272 | * Enable test mode that returns an 8 bit counter instead of the samples. 273 | * The counter is generated inside the RTL2832. 274 | * 275 | * \param dev the device handle given by rtlsdr_open() 276 | * \param test mode, 1 means enabled, 0 disabled 277 | * \return 0 on success 278 | */ 279 | RTLSDR_API int rtlsdr_set_testmode(rtlsdr_dev_t *dev, int on); 280 | 281 | /*! 282 | * Enable or disable the internal digital AGC of the RTL2832. 283 | * 284 | * \param dev the device handle given by rtlsdr_open() 285 | * \param digital AGC mode, 1 means enabled, 0 disabled 286 | * \return 0 on success 287 | */ 288 | RTLSDR_API int rtlsdr_set_agc_mode(rtlsdr_dev_t *dev, int on); 289 | 290 | /*! 291 | * Enable or disable the direct sampling mode. When enabled, the IF mode 292 | * of the RTL2832 is activated, and rtlsdr_set_center_freq() will control 293 | * the IF-frequency of the DDC, which can be used to tune from 0 to 28.8 MHz 294 | * (xtal frequency of the RTL2832). 295 | * 296 | * \param dev the device handle given by rtlsdr_open() 297 | * \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled 298 | * \return 0 on success 299 | */ 300 | RTLSDR_API int rtlsdr_set_direct_sampling(rtlsdr_dev_t *dev, int on); 301 | 302 | /*! 303 | * Get state of the direct sampling mode 304 | * 305 | * \param dev the device handle given by rtlsdr_open() 306 | * \return -1 on error, 0 means disabled, 1 I-ADC input enabled 307 | * 2 Q-ADC input enabled 308 | */ 309 | RTLSDR_API int rtlsdr_get_direct_sampling(rtlsdr_dev_t *dev); 310 | 311 | /*! 312 | * Enable or disable offset tuning for zero-IF tuners, which allows to avoid 313 | * problems caused by the DC offset of the ADCs and 1/f noise. 314 | * 315 | * \param dev the device handle given by rtlsdr_open() 316 | * \param on 0 means disabled, 1 enabled 317 | * \return 0 on success 318 | */ 319 | RTLSDR_API int rtlsdr_set_offset_tuning(rtlsdr_dev_t *dev, int on); 320 | 321 | /*! 322 | * Get state of the offset tuning mode 323 | * 324 | * \param dev the device handle given by rtlsdr_open() 325 | * \return -1 on error, 0 means disabled, 1 enabled 326 | */ 327 | RTLSDR_API int rtlsdr_get_offset_tuning(rtlsdr_dev_t *dev); 328 | 329 | /*! 330 | * Enable or disable frequency dithering for r820t tuners. 331 | * Must be performed before freq_set(). 332 | * Fails for other tuners. 333 | * 334 | * \param dev the device handle given by rtlsdr_open() 335 | * \param on 0 means disabled, 1 enabled 336 | * \return 0 on success 337 | */ 338 | RTLSDR_API int rtlsdr_set_dithering(rtlsdr_dev_t *dev, int dither); 339 | 340 | /* streaming functions */ 341 | 342 | RTLSDR_API int rtlsdr_reset_buffer(rtlsdr_dev_t *dev); 343 | 344 | RTLSDR_API int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read); 345 | 346 | typedef void(*rtlsdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx); 347 | 348 | /*! 349 | * Read samples from the device asynchronously. This function will block until 350 | * it is being canceled using rtlsdr_cancel_async() 351 | * 352 | * NOTE: This function is deprecated and is subject for removal. 353 | * 354 | * \param dev the device handle given by rtlsdr_open() 355 | * \param cb callback function to return received samples 356 | * \param ctx user specific context to pass via the callback function 357 | * \return 0 on success 358 | */ 359 | RTLSDR_API int rtlsdr_wait_async(rtlsdr_dev_t *dev, rtlsdr_read_async_cb_t cb, void *ctx); 360 | 361 | /*! 362 | * Read samples from the device asynchronously. This function will block until 363 | * it is being canceled using rtlsdr_cancel_async() 364 | * 365 | * \param dev the device handle given by rtlsdr_open() 366 | * \param cb callback function to return received samples 367 | * \param ctx user specific context to pass via the callback function 368 | * \param buf_num optional buffer count, buf_num * buf_len = overall buffer size 369 | * set to 0 for default buffer count (15) 370 | * \param buf_len optional buffer length, must be multiple of 512, 371 | * should be a multiple of 16384 (URB size), set to 0 372 | * for default buffer length (16 * 32 * 512) 373 | * \return 0 on success 374 | */ 375 | RTLSDR_API int rtlsdr_read_async(rtlsdr_dev_t *dev, 376 | rtlsdr_read_async_cb_t cb, 377 | void *ctx, 378 | uint32_t buf_num, 379 | uint32_t buf_len); 380 | 381 | /*! 382 | * Cancel all pending asynchronous operations on the device. 383 | * 384 | * \param dev the device handle given by rtlsdr_open() 385 | * \return 0 on success 386 | */ 387 | RTLSDR_API int rtlsdr_cancel_async(rtlsdr_dev_t *dev); 388 | 389 | #ifdef __cplusplus 390 | } 391 | #endif 392 | 393 | #endif /* __RTL_SDR_H */ 394 | -------------------------------------------------------------------------------- /include/rtl-sdr_export.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * Copyright (C) 2012 by Hoernchen 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef RTLSDR_EXPORT_H 20 | #define RTLSDR_EXPORT_H 21 | 22 | #if defined __GNUC__ 23 | # if __GNUC__ >= 4 24 | # define __SDR_EXPORT __attribute__((visibility("default"))) 25 | # define __SDR_IMPORT __attribute__((visibility("default"))) 26 | # else 27 | # define __SDR_EXPORT 28 | # define __SDR_IMPORT 29 | # endif 30 | #elif _MSC_VER 31 | # define __SDR_EXPORT __declspec(dllexport) 32 | # define __SDR_IMPORT __declspec(dllimport) 33 | #else 34 | # define __SDR_EXPORT 35 | # define __SDR_IMPORT 36 | #endif 37 | 38 | #ifndef rtlsdr_STATIC 39 | # ifdef rtlsdr_EXPORTS 40 | # define RTLSDR_API __SDR_EXPORT 41 | # else 42 | # define RTLSDR_API __SDR_IMPORT 43 | # endif 44 | #else 45 | #define RTLSDR_API 46 | #endif 47 | #endif /* RTLSDR_EXPORT_H */ 48 | -------------------------------------------------------------------------------- /include/rtlsdr_i2c.h: -------------------------------------------------------------------------------- 1 | #ifndef __I2C_H 2 | #define __I2C_H 3 | 4 | uint32_t rtlsdr_get_tuner_clock(void *dev); 5 | int rtlsdr_i2c_write_fn(void *dev, uint8_t addr, uint8_t *buf, int len); 6 | int rtlsdr_i2c_read_fn(void *dev, uint8_t addr, uint8_t *buf, int len); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /include/tuner_e4k.h: -------------------------------------------------------------------------------- 1 | #ifndef _E4K_TUNER_H 2 | #define _E4K_TUNER_H 3 | 4 | /* 5 | * Elonics E4000 tuner driver 6 | * 7 | * (C) 2011-2012 by Harald Welte 8 | * (C) 2012 by Sylvain Munaut 9 | * (C) 2012 by Hoernchen 10 | * 11 | * All Rights Reserved 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License, or 16 | * (at your option) any later version. 17 | * 18 | * This program is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | * GNU General Public License for more details. 22 | * 23 | * You should have received a copy of the GNU General Public License 24 | * along with this program. If not, see . 25 | */ 26 | 27 | #define E4K_I2C_ADDR 0xc8 28 | #define E4K_CHECK_ADDR 0x02 29 | #define E4K_CHECK_VAL 0x40 30 | 31 | enum e4k_reg { 32 | E4K_REG_MASTER1 = 0x00, 33 | E4K_REG_MASTER2 = 0x01, 34 | E4K_REG_MASTER3 = 0x02, 35 | E4K_REG_MASTER4 = 0x03, 36 | E4K_REG_MASTER5 = 0x04, 37 | E4K_REG_CLK_INP = 0x05, 38 | E4K_REG_REF_CLK = 0x06, 39 | E4K_REG_SYNTH1 = 0x07, 40 | E4K_REG_SYNTH2 = 0x08, 41 | E4K_REG_SYNTH3 = 0x09, 42 | E4K_REG_SYNTH4 = 0x0a, 43 | E4K_REG_SYNTH5 = 0x0b, 44 | E4K_REG_SYNTH6 = 0x0c, 45 | E4K_REG_SYNTH7 = 0x0d, 46 | E4K_REG_SYNTH8 = 0x0e, 47 | E4K_REG_SYNTH9 = 0x0f, 48 | E4K_REG_FILT1 = 0x10, 49 | E4K_REG_FILT2 = 0x11, 50 | E4K_REG_FILT3 = 0x12, 51 | // gap 52 | E4K_REG_GAIN1 = 0x14, 53 | E4K_REG_GAIN2 = 0x15, 54 | E4K_REG_GAIN3 = 0x16, 55 | E4K_REG_GAIN4 = 0x17, 56 | // gap 57 | E4K_REG_AGC1 = 0x1a, 58 | E4K_REG_AGC2 = 0x1b, 59 | E4K_REG_AGC3 = 0x1c, 60 | E4K_REG_AGC4 = 0x1d, 61 | E4K_REG_AGC5 = 0x1e, 62 | E4K_REG_AGC6 = 0x1f, 63 | E4K_REG_AGC7 = 0x20, 64 | E4K_REG_AGC8 = 0x21, 65 | // gap 66 | E4K_REG_AGC11 = 0x24, 67 | E4K_REG_AGC12 = 0x25, 68 | // gap 69 | E4K_REG_DC1 = 0x29, 70 | E4K_REG_DC2 = 0x2a, 71 | E4K_REG_DC3 = 0x2b, 72 | E4K_REG_DC4 = 0x2c, 73 | E4K_REG_DC5 = 0x2d, 74 | E4K_REG_DC6 = 0x2e, 75 | E4K_REG_DC7 = 0x2f, 76 | E4K_REG_DC8 = 0x30, 77 | // gap 78 | E4K_REG_QLUT0 = 0x50, 79 | E4K_REG_QLUT1 = 0x51, 80 | E4K_REG_QLUT2 = 0x52, 81 | E4K_REG_QLUT3 = 0x53, 82 | // gap 83 | E4K_REG_ILUT0 = 0x60, 84 | E4K_REG_ILUT1 = 0x61, 85 | E4K_REG_ILUT2 = 0x62, 86 | E4K_REG_ILUT3 = 0x63, 87 | // gap 88 | E4K_REG_DCTIME1 = 0x70, 89 | E4K_REG_DCTIME2 = 0x71, 90 | E4K_REG_DCTIME3 = 0x72, 91 | E4K_REG_DCTIME4 = 0x73, 92 | E4K_REG_PWM1 = 0x74, 93 | E4K_REG_PWM2 = 0x75, 94 | E4K_REG_PWM3 = 0x76, 95 | E4K_REG_PWM4 = 0x77, 96 | E4K_REG_BIAS = 0x78, 97 | E4K_REG_CLKOUT_PWDN = 0x7a, 98 | E4K_REG_CHFILT_CALIB = 0x7b, 99 | E4K_REG_I2C_REG_ADDR = 0x7d, 100 | // FIXME 101 | }; 102 | 103 | #define E4K_MASTER1_RESET (1 << 0) 104 | #define E4K_MASTER1_NORM_STBY (1 << 1) 105 | #define E4K_MASTER1_POR_DET (1 << 2) 106 | 107 | #define E4K_SYNTH1_PLL_LOCK (1 << 0) 108 | #define E4K_SYNTH1_BAND_SHIF 1 109 | 110 | #define E4K_SYNTH7_3PHASE_EN (1 << 3) 111 | 112 | #define E4K_SYNTH8_VCOCAL_UPD (1 << 2) 113 | 114 | #define E4K_FILT3_DISABLE (1 << 5) 115 | 116 | #define E4K_AGC1_LIN_MODE (1 << 4) 117 | #define E4K_AGC1_LNA_UPDATE (1 << 5) 118 | #define E4K_AGC1_LNA_G_LOW (1 << 6) 119 | #define E4K_AGC1_LNA_G_HIGH (1 << 7) 120 | 121 | #define E4K_AGC6_LNA_CAL_REQ (1 << 4) 122 | 123 | #define E4K_AGC7_MIX_GAIN_AUTO (1 << 0) 124 | #define E4K_AGC7_GAIN_STEP_5dB (1 << 5) 125 | 126 | #define E4K_AGC8_SENS_LIN_AUTO (1 << 0) 127 | 128 | #define E4K_AGC11_LNA_GAIN_ENH (1 << 0) 129 | 130 | #define E4K_DC1_CAL_REQ (1 << 0) 131 | 132 | #define E4K_DC5_I_LUT_EN (1 << 0) 133 | #define E4K_DC5_Q_LUT_EN (1 << 1) 134 | #define E4K_DC5_RANGE_DET_EN (1 << 2) 135 | #define E4K_DC5_RANGE_EN (1 << 3) 136 | #define E4K_DC5_TIMEVAR_EN (1 << 4) 137 | 138 | #define E4K_CLKOUT_DISABLE 0x96 139 | 140 | #define E4K_CHFCALIB_CMD (1 << 0) 141 | 142 | #define E4K_AGC1_MOD_MASK 0xF 143 | 144 | enum e4k_agc_mode { 145 | E4K_AGC_MOD_SERIAL = 0x0, 146 | E4K_AGC_MOD_IF_PWM_LNA_SERIAL = 0x1, 147 | E4K_AGC_MOD_IF_PWM_LNA_AUTONL = 0x2, 148 | E4K_AGC_MOD_IF_PWM_LNA_SUPERV = 0x3, 149 | E4K_AGC_MOD_IF_SERIAL_LNA_PWM = 0x4, 150 | E4K_AGC_MOD_IF_PWM_LNA_PWM = 0x5, 151 | E4K_AGC_MOD_IF_DIG_LNA_SERIAL = 0x6, 152 | E4K_AGC_MOD_IF_DIG_LNA_AUTON = 0x7, 153 | E4K_AGC_MOD_IF_DIG_LNA_SUPERV = 0x8, 154 | E4K_AGC_MOD_IF_SERIAL_LNA_AUTON = 0x9, 155 | E4K_AGC_MOD_IF_SERIAL_LNA_SUPERV = 0xa, 156 | }; 157 | 158 | enum e4k_band { 159 | E4K_BAND_VHF2 = 0, 160 | E4K_BAND_VHF3 = 1, 161 | E4K_BAND_UHF = 2, 162 | E4K_BAND_L = 3, 163 | }; 164 | 165 | enum e4k_mixer_filter_bw { 166 | E4K_F_MIX_BW_27M = 0, 167 | E4K_F_MIX_BW_4M6 = 8, 168 | E4K_F_MIX_BW_4M2 = 9, 169 | E4K_F_MIX_BW_3M8 = 10, 170 | E4K_F_MIX_BW_3M4 = 11, 171 | E4K_F_MIX_BW_3M = 12, 172 | E4K_F_MIX_BW_2M7 = 13, 173 | E4K_F_MIX_BW_2M3 = 14, 174 | E4K_F_MIX_BW_1M9 = 15, 175 | }; 176 | 177 | enum e4k_if_filter { 178 | E4K_IF_FILTER_MIX, 179 | E4K_IF_FILTER_CHAN, 180 | E4K_IF_FILTER_RC 181 | }; 182 | struct e4k_pll_params { 183 | uint32_t fosc; 184 | uint32_t intended_flo; 185 | uint32_t flo; 186 | uint16_t x; 187 | uint8_t z; 188 | uint8_t r; 189 | uint8_t r_idx; 190 | uint8_t threephase; 191 | }; 192 | 193 | struct e4k_state { 194 | void *i2c_dev; 195 | uint8_t i2c_addr; 196 | enum e4k_band band; 197 | struct e4k_pll_params vco; 198 | void *rtl_dev; 199 | }; 200 | 201 | int e4k_init(struct e4k_state *e4k); 202 | int e4k_standby(struct e4k_state *e4k, int enable); 203 | int e4k_if_gain_set(struct e4k_state *e4k, uint8_t stage, int8_t value); 204 | int e4k_mixer_gain_set(struct e4k_state *e4k, int8_t value); 205 | int e4k_commonmode_set(struct e4k_state *e4k, int8_t value); 206 | int e4k_tune_freq(struct e4k_state *e4k, uint32_t freq); 207 | int e4k_tune_params(struct e4k_state *e4k, struct e4k_pll_params *p); 208 | uint32_t e4k_compute_pll_params(struct e4k_pll_params *oscp, uint32_t fosc, uint32_t intended_flo); 209 | int e4k_if_filter_bw_get(struct e4k_state *e4k, enum e4k_if_filter filter); 210 | int e4k_if_filter_bw_set(struct e4k_state *e4k, enum e4k_if_filter filter, 211 | uint32_t bandwidth); 212 | int e4k_if_filter_chan_enable(struct e4k_state *e4k, int on); 213 | int e4k_rf_filter_set(struct e4k_state *e4k); 214 | 215 | int e4k_manual_dc_offset(struct e4k_state *e4k, int8_t iofs, int8_t irange, int8_t qofs, int8_t qrange); 216 | int e4k_dc_offset_calibrate(struct e4k_state *e4k); 217 | int e4k_dc_offset_gen_table(struct e4k_state *e4k); 218 | 219 | int e4k_set_lna_gain(struct e4k_state *e4k, int32_t gain); 220 | int e4k_enable_manual_gain(struct e4k_state *e4k, uint8_t manual); 221 | int e4k_set_enh_gain(struct e4k_state *e4k, int32_t gain); 222 | #endif /* _E4K_TUNER_H */ 223 | -------------------------------------------------------------------------------- /include/tuner_fc0012.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Fitipower FC0012 tuner driver 3 | * 4 | * Copyright (C) 2012 Hans-Frieder Vogt 5 | * 6 | * modified for use in librtlsdr 7 | * Copyright (C) 2012 Steve Markgraf 8 | * 9 | * This program is free software; you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation; either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 | * 23 | */ 24 | 25 | #ifndef _FC0012_H_ 26 | #define _FC0012_H_ 27 | 28 | #define FC0012_I2C_ADDR 0xc6 29 | #define FC0012_CHECK_ADDR 0x00 30 | #define FC0012_CHECK_VAL 0xa1 31 | 32 | int fc0012_init(void *dev); 33 | int fc0012_set_params(void *dev, uint32_t freq, uint32_t bandwidth); 34 | int fc0012_set_gain(void *dev, int gain); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/tuner_fc0013.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Fitipower FC0013 tuner driver 3 | * 4 | * Copyright (C) 2012 Hans-Frieder Vogt 5 | * 6 | * modified for use in librtlsdr 7 | * Copyright (C) 2012 Steve Markgraf 8 | * 9 | * This program is free software; you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation; either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 | * 23 | */ 24 | 25 | #ifndef _FC0013_H_ 26 | #define _FC0013_H_ 27 | 28 | #define FC0013_I2C_ADDR 0xc6 29 | #define FC0013_CHECK_ADDR 0x00 30 | #define FC0013_CHECK_VAL 0xa3 31 | 32 | int fc0013_init(void *dev); 33 | int fc0013_set_params(void *dev, uint32_t freq, uint32_t bandwidth); 34 | int fc0013_set_gain_mode(void *dev, int manual); 35 | int fc0013_set_lna_gain(void *dev, int gain); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/tuner_fc2580.h: -------------------------------------------------------------------------------- 1 | #ifndef __TUNER_FC2580_H 2 | #define __TUNER_FC2580_H 3 | 4 | #define BORDER_FREQ 2600000 //2.6GHz : The border frequency which determines whether Low VCO or High VCO is used 5 | #define USE_EXT_CLK 0 //0 : Use internal XTAL Oscillator / 1 : Use External Clock input 6 | #define OFS_RSSI 57 7 | 8 | #define FC2580_I2C_ADDR 0xac 9 | #define FC2580_CHECK_ADDR 0x01 10 | #define FC2580_CHECK_VAL 0x56 11 | 12 | typedef enum { 13 | FC2580_UHF_BAND, 14 | FC2580_L_BAND, 15 | FC2580_VHF_BAND, 16 | FC2580_NO_BAND 17 | } fc2580_band_type; 18 | 19 | typedef enum { 20 | FC2580_FCI_FAIL, 21 | FC2580_FCI_SUCCESS 22 | } fc2580_fci_result_type; 23 | 24 | enum FUNCTION_STATUS 25 | { 26 | FUNCTION_SUCCESS, 27 | FUNCTION_ERROR, 28 | }; 29 | 30 | extern void fc2580_wait_msec(void *pTuner, int a); 31 | 32 | fc2580_fci_result_type fc2580_i2c_write(void *pTuner, unsigned char reg, unsigned char val); 33 | fc2580_fci_result_type fc2580_i2c_read(void *pTuner, unsigned char reg, unsigned char *read_data); 34 | 35 | /*============================================================================== 36 | fc2580 initial setting 37 | 38 | This function is a generic function which gets called to initialize 39 | 40 | fc2580 in DVB-H mode or L-Band TDMB mode 41 | 42 | 43 | 44 | ifagc_mode 45 | type : integer 46 | 1 : Internal AGC 47 | 2 : Voltage Control Mode 48 | 49 | ==============================================================================*/ 50 | fc2580_fci_result_type fc2580_set_init(void *pTuner, int ifagc_mode, unsigned int freq_xtal ); 51 | 52 | /*============================================================================== 53 | fc2580 frequency setting 54 | 55 | This function is a generic function which gets called to change LO Frequency 56 | 57 | of fc2580 in DVB-H mode or L-Band TDMB mode 58 | 59 | 60 | 61 | f_lo 62 | Value of target LO Frequency in 'kHz' unit 63 | ex) 2.6GHz = 2600000 64 | 65 | ==============================================================================*/ 66 | fc2580_fci_result_type fc2580_set_freq(void *pTuner, unsigned int f_lo, unsigned int freq_xtal ); 67 | 68 | 69 | /*============================================================================== 70 | fc2580 filter BW setting 71 | 72 | This function is a generic function which gets called to change Bandwidth 73 | 74 | frequency of fc2580's channel selection filter 75 | 76 | 77 | 78 | filter_bw 79 | 1 : 1.53MHz(TDMB) 80 | 6 : 6MHz 81 | 7 : 7MHz 82 | 8 : 7.8MHz 83 | 84 | 85 | ==============================================================================*/ 86 | fc2580_fci_result_type fc2580_set_filter( void *pTuner, unsigned char filter_bw, unsigned int freq_xtal ); 87 | 88 | // The following context is FC2580 tuner API source code 89 | // Definitions 90 | 91 | // AGC mode 92 | enum FC2580_AGC_MODE 93 | { 94 | FC2580_AGC_INTERNAL = 1, 95 | FC2580_AGC_EXTERNAL = 2, 96 | }; 97 | 98 | 99 | // Bandwidth mode 100 | enum FC2580_BANDWIDTH_MODE 101 | { 102 | FC2580_BANDWIDTH_1530000HZ = 1, 103 | FC2580_BANDWIDTH_6000000HZ = 6, 104 | FC2580_BANDWIDTH_7000000HZ = 7, 105 | FC2580_BANDWIDTH_8000000HZ = 8, 106 | }; 107 | 108 | // Manipulaing functions 109 | int 110 | fc2580_Initialize( 111 | void *pTuner 112 | ); 113 | 114 | int 115 | fc2580_SetRfFreqHz( 116 | void *pTuner, 117 | unsigned long RfFreqHz 118 | ); 119 | 120 | // Extra manipulaing functions 121 | int 122 | fc2580_SetBandwidthMode( 123 | void *pTuner, 124 | int BandwidthMode 125 | ); 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /include/tuner_r82xx.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Rafael Micro R820T/R828D driver 3 | * 4 | * Copyright (C) 2013 Mauro Carvalho Chehab 5 | * Copyright (C) 2013 Steve Markgraf 6 | * 7 | * This driver is a heavily modified version of the driver found in the 8 | * Linux kernel: 9 | * http://git.linuxtv.org/linux-2.6.git/history/HEAD:/drivers/media/tuners/r820t.c 10 | * 11 | * This program is free software: you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation, either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see . 23 | */ 24 | 25 | #ifndef R82XX_H 26 | #define R82XX_H 27 | 28 | #define R820T_I2C_ADDR 0x34 29 | #define R828D_I2C_ADDR 0x74 30 | #define R828D_XTAL_FREQ 16000000 31 | 32 | #define R82XX_CHECK_ADDR 0x00 33 | #define R82XX_CHECK_VAL 0x69 34 | 35 | #define R82XX_DEFAULT_IF_FREQ 6000000 36 | #define R82XX_DEFAULT_IF_BW 2000000 37 | 38 | #define REG_SHADOW_START 5 39 | #define NUM_REGS 30 40 | #define NUM_IMR 5 41 | #define IMR_TRIAL 9 42 | 43 | #define VER_NUM 49 44 | 45 | enum r82xx_chip { 46 | CHIP_R820T, 47 | CHIP_R620D, 48 | CHIP_R828D, 49 | CHIP_R828, 50 | CHIP_R828S, 51 | CHIP_R820C, 52 | }; 53 | 54 | enum r82xx_tuner_type { 55 | TUNER_RADIO = 1, 56 | TUNER_ANALOG_TV, 57 | TUNER_DIGITAL_TV 58 | }; 59 | 60 | enum r82xx_xtal_cap_value { 61 | XTAL_LOW_CAP_30P = 0, 62 | XTAL_LOW_CAP_20P, 63 | XTAL_LOW_CAP_10P, 64 | XTAL_LOW_CAP_0P, 65 | XTAL_HIGH_CAP_0P 66 | }; 67 | 68 | struct r82xx_config { 69 | uint8_t i2c_addr; 70 | uint32_t xtal; 71 | enum r82xx_chip rafael_chip; 72 | unsigned int max_i2c_msg_len; 73 | int use_predetect; 74 | }; 75 | 76 | struct r82xx_priv { 77 | struct r82xx_config *cfg; 78 | 79 | uint8_t regs[NUM_REGS]; 80 | uint8_t buf[NUM_REGS + 1]; 81 | enum r82xx_xtal_cap_value xtal_cap_sel; 82 | uint16_t pll; /* kHz */ 83 | uint32_t int_freq; 84 | uint8_t fil_cal_code; 85 | uint8_t input; 86 | int init_done; 87 | int disable_dither; 88 | int reg_cache; 89 | int reg_batch, reg_low, reg_high; 90 | 91 | /* Store current mode */ 92 | uint32_t delsys; 93 | enum r82xx_tuner_type type; 94 | 95 | uint32_t bw; /* in MHz */ 96 | 97 | void *rtl_dev; 98 | }; 99 | 100 | struct r82xx_freq_range { 101 | uint32_t freq; 102 | uint8_t open_d; 103 | uint8_t rf_mux_ploy; 104 | uint8_t tf_c; 105 | uint8_t xtal_cap20p; 106 | uint8_t xtal_cap10p; 107 | uint8_t xtal_cap0p; 108 | }; 109 | 110 | enum r82xx_delivery_system { 111 | SYS_UNDEFINED, 112 | SYS_DVBT, 113 | SYS_DVBT2, 114 | SYS_ISDBT, 115 | }; 116 | 117 | int r82xx_standby(struct r82xx_priv *priv); 118 | int r82xx_init(struct r82xx_priv *priv); 119 | int r82xx_set_freq(struct r82xx_priv *priv, uint32_t freq); 120 | int r82xx_set_gain(struct r82xx_priv *priv, int set_manual_gain, int gain); 121 | int r82xx_set_nomod(struct r82xx_priv *priv); 122 | int r82xx_set_dither(struct r82xx_priv *priv, int dither); 123 | int r82xx_set_bw(struct r82xx_priv *priv, uint32_t bw); 124 | int r82xx_set_if_freq(struct r82xx_priv *priv, uint32_t freq); 125 | 126 | #endif 127 | -------------------------------------------------------------------------------- /librtlsdr.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: RTL-SDR Library 7 | Description: C Utility Library 8 | Version: @VERSION@ 9 | Cflags: -I${includedir}/ @RTLSDR_PC_CFLAGS@ 10 | Libs: -L${libdir} -lrtlsdr 11 | Libs.private: @RTLSDR_PC_LIBS@ 12 | Requires: libusb-1.0 13 | -------------------------------------------------------------------------------- /m4/.gitignore: -------------------------------------------------------------------------------- 1 | /libtool.m4 2 | /lt*.m4 3 | -------------------------------------------------------------------------------- /rtl-sdr.rules: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2012-2013 Osmocom rtl-sdr project 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | # 17 | 18 | # original RTL2832U vid/pid (hama nano, for example) 19 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2832", MODE:="0666" 20 | 21 | # RTL2832U OEM vid/pid, e.g. ezcap EzTV668 (E4000), Newsky TV28T (E4000/R820T) etc. 22 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838", MODE:="0666" 23 | 24 | # DigitalNow Quad DVB-T PCI-E card (4x FC0012?) 25 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0413", ATTRS{idProduct}=="6680", MODE:="0666" 26 | 27 | # Leadtek WinFast DTV Dongle mini D (FC0012) 28 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0413", ATTRS{idProduct}=="6f0f", MODE:="0666" 29 | 30 | # Genius TVGo DVB-T03 USB dongle (Ver. B) 31 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0458", ATTRS{idProduct}=="707f", MODE:="0666" 32 | 33 | # Terratec Cinergy T Stick Black (rev 1) (FC0012) 34 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00a9", MODE:="0666" 35 | 36 | # Terratec NOXON rev 1 (FC0013) 37 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00b3", MODE:="0666" 38 | 39 | # Terratec Deutschlandradio DAB Stick (FC0013) 40 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00b4", MODE:="0666" 41 | 42 | # Terratec NOXON DAB Stick - Radio Energy (FC0013) 43 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00b5", MODE:="0666" 44 | 45 | # Terratec Media Broadcast DAB Stick (FC0013) 46 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00b7", MODE:="0666" 47 | 48 | # Terratec BR DAB Stick (FC0013) 49 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00b8", MODE:="0666" 50 | 51 | # Terratec WDR DAB Stick (FC0013) 52 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00b9", MODE:="0666" 53 | 54 | # Terratec MuellerVerlag DAB Stick (FC0013) 55 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00c0", MODE:="0666" 56 | 57 | # Terratec Fraunhofer DAB Stick (FC0013) 58 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00c6", MODE:="0666" 59 | 60 | # Terratec Cinergy T Stick RC (Rev.3) (E4000) 61 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00d3", MODE:="0666" 62 | 63 | # Terratec T Stick PLUS (E4000) 64 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00d7", MODE:="0666" 65 | 66 | # Terratec NOXON rev 2 (E4000) 67 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="0ccd", ATTRS{idProduct}=="00e0", MODE:="0666" 68 | 69 | # PixelView PV-DT235U(RN) (FC0012) 70 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1554", ATTRS{idProduct}=="5020", MODE:="0666" 71 | 72 | # Astrometa DVB-T/DVB-T2 (R828D) 73 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="15f4", ATTRS{idProduct}=="0131", MODE:="0666" 74 | 75 | # Compro Videomate U620F (E4000) 76 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="185b", ATTRS{idProduct}=="0620", MODE:="0666" 77 | 78 | # Compro Videomate U650F (E4000) 79 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="185b", ATTRS{idProduct}=="0650", MODE:="0666" 80 | 81 | # Compro Videomate U680F (E4000) 82 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="185b", ATTRS{idProduct}=="0680", MODE:="0666" 83 | 84 | # GIGABYTE GT-U7300 (FC0012) 85 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d393", MODE:="0666" 86 | 87 | # DIKOM USB-DVBT HD 88 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d394", MODE:="0666" 89 | 90 | # Peak 102569AGPK (FC0012) 91 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d395", MODE:="0666" 92 | 93 | # KWorld KW-UB450-T USB DVB-T Pico TV (TUA9001) 94 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d397", MODE:="0666" 95 | 96 | # Zaapa ZT-MINDVBZP (FC0012) 97 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d398", MODE:="0666" 98 | 99 | # SVEON STV20 DVB-T USB & FM (FC0012) 100 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d39d", MODE:="0666" 101 | 102 | # Twintech UT-40 (FC0013) 103 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d3a4", MODE:="0666" 104 | 105 | # ASUS U3100MINI_PLUS_V2 (FC0013) 106 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d3a8", MODE:="0666" 107 | 108 | # SVEON STV27 DVB-T USB & FM (FC0013) 109 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d3af", MODE:="0666" 110 | 111 | # SVEON STV21 DVB-T USB & FM 112 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b80", ATTRS{idProduct}=="d3b0", MODE:="0666" 113 | 114 | # Dexatek DK DVB-T Dongle (Logilink VG0002A) (FC2580) 115 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1d19", ATTRS{idProduct}=="1101", MODE:="0666" 116 | 117 | # Dexatek DK DVB-T Dongle (MSI DigiVox mini II V3.0) 118 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1d19", ATTRS{idProduct}=="1102", MODE:="0666" 119 | 120 | # Dexatek DK 5217 DVB-T Dongle (FC2580) 121 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1d19", ATTRS{idProduct}=="1103", MODE:="0666" 122 | 123 | # MSI DigiVox Micro HD (FC2580) 124 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1d19", ATTRS{idProduct}=="1104", MODE:="0666" 125 | 126 | # Sweex DVB-T USB (FC0012) 127 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1f4d", ATTRS{idProduct}=="a803", MODE:="0666" 128 | 129 | # GTek T803 (FC0012) 130 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1f4d", ATTRS{idProduct}=="b803", MODE:="0666" 131 | 132 | # Lifeview LV5TDeluxe (FC0012) 133 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1f4d", ATTRS{idProduct}=="c803", MODE:="0666" 134 | 135 | # MyGica TD312 (FC0012) 136 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1f4d", ATTRS{idProduct}=="d286", MODE:="0666" 137 | 138 | # PROlectrix DV107669 (FC0012) 139 | SUBSYSTEMS=="usb", ATTRS{idVendor}=="1f4d", ATTRS{idProduct}=="d803", MODE:="0666" 140 | -------------------------------------------------------------------------------- /rtlsdr.conf: -------------------------------------------------------------------------------- 1 | # disable DVB drivers 2 | blacklist dvb_usb_rtl28xxu 3 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2012 OSMOCOM Project 2 | # 3 | # This file is part of rtl-sdr 4 | # 5 | # GNU Radio is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3, or (at your option) 8 | # any later version. 9 | # 10 | # GNU Radio is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with GNU Radio; see the file COPYING. If not, write to 17 | # the Free Software Foundation, Inc., 51 Franklin Street, 18 | # Boston, MA 02110-1301, USA. 19 | 20 | ######################################################################## 21 | # Setup library 22 | ######################################################################## 23 | add_library(rtlsdr_shared SHARED 24 | librtlsdr.c 25 | tuner_e4k.c 26 | tuner_fc0012.c 27 | tuner_fc0013.c 28 | tuner_fc2580.c 29 | tuner_r82xx.c 30 | ) 31 | target_link_libraries(rtlsdr_shared 32 | ${LIBUSB_LIBRARIES} 33 | ) 34 | set_target_properties(rtlsdr_shared PROPERTIES DEFINE_SYMBOL "rtlsdr_EXPORTS") 35 | set_target_properties(rtlsdr_shared PROPERTIES OUTPUT_NAME rtlsdr) 36 | set_target_properties(rtlsdr_shared PROPERTIES SOVERSION ${MAJOR_VERSION}) 37 | set_target_properties(rtlsdr_shared PROPERTIES VERSION ${LIBVER}) 38 | 39 | add_library(rtlsdr_static STATIC 40 | librtlsdr.c 41 | tuner_e4k.c 42 | tuner_fc0012.c 43 | tuner_fc0013.c 44 | tuner_fc2580.c 45 | tuner_r82xx.c 46 | ) 47 | target_link_libraries(rtlsdr_static 48 | ${LIBUSB_LIBRARIES} 49 | ) 50 | set_property(TARGET rtlsdr_static APPEND PROPERTY COMPILE_DEFINITIONS "rtlsdr_STATIC" ) 51 | 52 | add_library(convenience_static STATIC 53 | convenience/convenience.c 54 | ) 55 | target_link_libraries(convenience_static 56 | rtlsdr_shared 57 | ) 58 | 59 | if(NOT WIN32) 60 | # Force same library filename for static and shared variants of the library 61 | set_target_properties(rtlsdr_static PROPERTIES OUTPUT_NAME rtlsdr) 62 | endif() 63 | 64 | ######################################################################## 65 | # Build utility 66 | ######################################################################## 67 | add_executable(rtl_sdr rtl_sdr.c) 68 | add_executable(rtl_tcp rtl_tcp.c) 69 | add_executable(rtl_test rtl_test.c) 70 | add_executable(rtl_fm rtl_fm.c) 71 | add_executable(rtl_eeprom rtl_eeprom.c) 72 | add_executable(rtl_adsb rtl_adsb.c) 73 | add_executable(rtl_power rtl_power.c) 74 | set(INSTALL_TARGETS rtlsdr_shared rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb rtl_power) 75 | 76 | target_link_libraries(rtl_sdr convenience_static rtlsdr_shared 77 | ${LIBUSB_LIBRARIES} 78 | ${CMAKE_THREAD_LIBS_INIT} 79 | ) 80 | target_link_libraries(rtl_tcp convenience_static rtlsdr_shared 81 | ${LIBUSB_LIBRARIES} 82 | ${CMAKE_THREAD_LIBS_INIT} 83 | ) 84 | target_link_libraries(rtl_test convenience_static rtlsdr_shared 85 | ${LIBUSB_LIBRARIES} 86 | ${CMAKE_THREAD_LIBS_INIT} 87 | ) 88 | target_link_libraries(rtl_fm convenience_static rtlsdr_shared 89 | ${LIBUSB_LIBRARIES} 90 | ${CMAKE_THREAD_LIBS_INIT} 91 | ) 92 | target_link_libraries(rtl_eeprom convenience_static rtlsdr_shared 93 | ${LIBUSB_LIBRARIES} 94 | ${CMAKE_THREAD_LIBS_INIT} 95 | ) 96 | target_link_libraries(rtl_adsb convenience_static rtlsdr_shared 97 | ${LIBUSB_LIBRARIES} 98 | ${CMAKE_THREAD_LIBS_INIT} 99 | ) 100 | target_link_libraries(rtl_power convenience_static rtlsdr_shared 101 | ${LIBUSB_LIBRARIES} 102 | ${CMAKE_THREAD_LIBS_INIT} 103 | ) 104 | if(UNIX) 105 | target_link_libraries(rtl_adsb m) 106 | target_link_libraries(rtl_power m) 107 | if(APPLE) 108 | target_link_libraries(rtl_test m) 109 | target_link_libraries(rtl_fm m) 110 | else() 111 | target_link_libraries(rtl_test m rt) 112 | target_link_libraries(rtl_fm m rt) 113 | endif() 114 | endif() 115 | 116 | if(WIN32) 117 | add_library(libgetopt_static STATIC 118 | getopt/getopt.c 119 | ) 120 | target_link_libraries(rtl_sdr libgetopt_static) 121 | target_link_libraries(rtl_tcp ws2_32 libgetopt_static) 122 | target_link_libraries(rtl_test libgetopt_static) 123 | target_link_libraries(rtl_fm libgetopt_static) 124 | target_link_libraries(rtl_eeprom libgetopt_static) 125 | target_link_libraries(rtl_adsb libgetopt_static) 126 | target_link_libraries(rtl_power libgetopt_static) 127 | endif() 128 | ######################################################################## 129 | # Install built library files & utilities 130 | ######################################################################## 131 | install(TARGETS ${INSTALL_TARGETS} 132 | LIBRARY DESTINATION ${LIB_INSTALL_DIR} # .so/.dylib file 133 | ARCHIVE DESTINATION ${LIB_INSTALL_DIR} # .lib file 134 | RUNTIME DESTINATION bin # .dll file 135 | ) 136 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | # This is _NOT_ the library release version, it's an API version. 2 | # Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification 3 | LIBVERSION=0:5:0 4 | 5 | AUTOMAKE_OPTIONS = subdir-objects 6 | INCLUDES = $(all_includes) -I$(top_srcdir)/include 7 | noinst_HEADERS = convenience/convenience.h 8 | AM_CFLAGS = ${CFLAGS} -fPIC ${SYMBOL_VISIBILITY} 9 | 10 | lib_LTLIBRARIES = librtlsdr.la 11 | 12 | librtlsdr_la_SOURCES = librtlsdr.c tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner_fc2580.c tuner_r82xx.c 13 | librtlsdr_la_LDFLAGS = -version-info $(LIBVERSION) 14 | 15 | bin_PROGRAMS = rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb rtl_power 16 | 17 | rtl_sdr_SOURCES = rtl_sdr.c convenience/convenience.c 18 | rtl_sdr_LDADD = librtlsdr.la 19 | 20 | rtl_tcp_SOURCES = rtl_tcp.c convenience/convenience.c 21 | rtl_tcp_LDADD = librtlsdr.la 22 | 23 | rtl_test_SOURCES = rtl_test.c convenience/convenience.c 24 | rtl_test_LDADD = librtlsdr.la $(LIBM) 25 | 26 | rtl_fm_SOURCES = rtl_fm.c convenience/convenience.c 27 | rtl_fm_LDADD = librtlsdr.la $(LIBM) 28 | 29 | rtl_eeprom_SOURCES = rtl_eeprom.c convenience/convenience.c 30 | rtl_eeprom_LDADD = librtlsdr.la $(LIBM) 31 | 32 | rtl_adsb_SOURCES = rtl_adsb.c convenience/convenience.c 33 | rtl_adsb_LDADD = librtlsdr.la $(LIBM) 34 | 35 | rtl_power_SOURCES = rtl_power.c convenience/convenience.c 36 | rtl_power_LDADD = librtlsdr.la $(LIBM) 37 | -------------------------------------------------------------------------------- /src/convenience/convenience.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013-2014 by Kyle Keen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* a collection of user friendly tools 19 | * todo: use strtol for more flexible int parsing 20 | * */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef _WIN32 27 | #include 28 | #else 29 | #include 30 | #include 31 | #include 32 | #define _USE_MATH_DEFINES 33 | #endif 34 | 35 | #include 36 | 37 | #include "rtl-sdr.h" 38 | 39 | double atofs(char *s) 40 | /* standard suffixes */ 41 | { 42 | char last; 43 | int len; 44 | double suff = 1.0; 45 | len = strlen(s); 46 | last = s[len-1]; 47 | s[len-1] = '\0'; 48 | switch (last) { 49 | case 'g': 50 | case 'G': 51 | suff *= 1e3; 52 | case 'm': 53 | case 'M': 54 | suff *= 1e3; 55 | case 'k': 56 | case 'K': 57 | suff *= 1e3; 58 | suff *= atof(s); 59 | s[len-1] = last; 60 | return suff; 61 | } 62 | s[len-1] = last; 63 | return atof(s); 64 | } 65 | 66 | double atoft(char *s) 67 | /* time suffixes, returns seconds */ 68 | { 69 | char last; 70 | int len; 71 | double suff = 1.0; 72 | len = strlen(s); 73 | last = s[len-1]; 74 | s[len-1] = '\0'; 75 | switch (last) { 76 | case 'h': 77 | case 'H': 78 | suff *= 60; 79 | case 'm': 80 | case 'M': 81 | suff *= 60; 82 | case 's': 83 | case 'S': 84 | suff *= atof(s); 85 | s[len-1] = last; 86 | return suff; 87 | } 88 | s[len-1] = last; 89 | return atof(s); 90 | } 91 | 92 | double atofp(char *s) 93 | /* percent suffixes */ 94 | { 95 | char last; 96 | int len; 97 | double suff = 1.0; 98 | len = strlen(s); 99 | last = s[len-1]; 100 | s[len-1] = '\0'; 101 | switch (last) { 102 | case '%': 103 | suff *= 0.01; 104 | suff *= atof(s); 105 | s[len-1] = last; 106 | return suff; 107 | } 108 | s[len-1] = last; 109 | return atof(s); 110 | } 111 | 112 | int nearest_gain(rtlsdr_dev_t *dev, int target_gain) 113 | { 114 | int i, r, err1, err2, count, nearest; 115 | int* gains; 116 | r = rtlsdr_set_tuner_gain_mode(dev, 1); 117 | if (r < 0) { 118 | fprintf(stderr, "WARNING: Failed to enable manual gain.\n"); 119 | return r; 120 | } 121 | count = rtlsdr_get_tuner_gains(dev, NULL); 122 | if (count <= 0) { 123 | return 0; 124 | } 125 | gains = malloc(sizeof(int) * count); 126 | count = rtlsdr_get_tuner_gains(dev, gains); 127 | nearest = gains[0]; 128 | for (i=0; i=0; i--) { 254 | if (serial[i] != start_char) { 255 | continue;} 256 | fprintf(stderr, "PPM calibration found in eeprom.\n"); 257 | status = 0; 258 | *ppm_error = atoi(serial + i + 1); 259 | break; 260 | } 261 | serial[len-1] = stop_char; 262 | return status; 263 | } 264 | 265 | int verbose_reset_buffer(rtlsdr_dev_t *dev) 266 | { 267 | int r; 268 | r = rtlsdr_reset_buffer(dev); 269 | if (r < 0) { 270 | fprintf(stderr, "WARNING: Failed to reset buffers.\n");} 271 | return r; 272 | } 273 | 274 | int verbose_device_search(char *s) 275 | { 276 | int i, device_count, device, offset; 277 | char *s2; 278 | char vendor[256] = {0}, product[256] = {0}, serial[256] = {0}; 279 | device_count = rtlsdr_get_device_count(); 280 | if (!device_count) { 281 | fprintf(stderr, "No supported devices found.\n"); 282 | return -1; 283 | } 284 | fprintf(stderr, "Found %d device(s):\n", device_count); 285 | for (i = 0; i < device_count; i++) { 286 | if (rtlsdr_get_device_usb_strings(i, vendor, product, serial) == 0) { 287 | fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial); 288 | } else { 289 | fprintf(stderr, " %d: %s\n", i, "Failed to query data"); 290 | } 291 | } 292 | fprintf(stderr, "\n"); 293 | /* does string look like raw id number */ 294 | device = (int)strtol(s, &s2, 0); 295 | if (s2[0] == '\0' && device >= 0 && device < device_count) { 296 | fprintf(stderr, "Using device %d: %s\n", 297 | device, rtlsdr_get_device_name((uint32_t)device)); 298 | return device; 299 | } 300 | /* does string exact match a serial */ 301 | for (i = 0; i < device_count; i++) { 302 | rtlsdr_get_device_usb_strings(i, vendor, product, serial); 303 | if (strcmp(s, serial) != 0) { 304 | continue;} 305 | device = i; 306 | fprintf(stderr, "Using device %d: %s\n", 307 | device, rtlsdr_get_device_name((uint32_t)device)); 308 | return device; 309 | } 310 | /* does string prefix match a serial */ 311 | for (i = 0; i < device_count; i++) { 312 | rtlsdr_get_device_usb_strings(i, vendor, product, serial); 313 | if (strncmp(s, serial, strlen(s)) != 0) { 314 | continue;} 315 | device = i; 316 | fprintf(stderr, "Using device %d: %s\n", 317 | device, rtlsdr_get_device_name((uint32_t)device)); 318 | return device; 319 | } 320 | /* does string suffix match a serial */ 321 | for (i = 0; i < device_count; i++) { 322 | rtlsdr_get_device_usb_strings(i, vendor, product, serial); 323 | offset = strlen(serial) - strlen(s); 324 | if (offset < 0) { 325 | continue;} 326 | if (strncmp(s, serial+offset, strlen(s)) != 0) { 327 | continue;} 328 | device = i; 329 | fprintf(stderr, "Using device %d: %s\n", 330 | device, rtlsdr_get_device_name((uint32_t)device)); 331 | return device; 332 | } 333 | fprintf(stderr, "No matching devices found.\n"); 334 | return -1; 335 | } 336 | 337 | // vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab 338 | -------------------------------------------------------------------------------- /src/convenience/convenience.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013-2014 by Kyle Keen 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 2 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | /* a collection of user friendly tools */ 19 | 20 | /*! 21 | * Convert standard suffixes (k, M, G) to double 22 | * 23 | * \param s a string to be parsed 24 | * \return double 25 | */ 26 | 27 | double atofs(char *s); 28 | 29 | /*! 30 | * Convert time suffixes (s, m, h) to double 31 | * 32 | * \param s a string to be parsed 33 | * \return seconds as double 34 | */ 35 | 36 | double atoft(char *s); 37 | 38 | /*! 39 | * Convert percent suffixe (%) to double 40 | * 41 | * \param s a string to be parsed 42 | * \return double 43 | */ 44 | 45 | double atofp(char *s); 46 | 47 | /*! 48 | * Find nearest supported gain 49 | * 50 | * \param dev the device handle given by rtlsdr_open() 51 | * \param target_gain in tenths of a dB 52 | * \return 0 on success 53 | */ 54 | 55 | int nearest_gain(rtlsdr_dev_t *dev, int target_gain); 56 | 57 | /*! 58 | * Set device frequency and report status on stderr 59 | * 60 | * \param dev the device handle given by rtlsdr_open() 61 | * \param frequency in Hz 62 | * \return 0 on success 63 | */ 64 | 65 | int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency); 66 | 67 | /*! 68 | * Set device sample rate and report status on stderr 69 | * 70 | * \param dev the device handle given by rtlsdr_open() 71 | * \param samp_rate in samples/second 72 | * \return 0 on success 73 | */ 74 | 75 | int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate); 76 | 77 | /*! 78 | * Enable or disable the direct sampling mode and report status on stderr 79 | * 80 | * \param dev the device handle given by rtlsdr_open() 81 | * \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled 82 | * \return 0 on success 83 | */ 84 | 85 | int verbose_direct_sampling(rtlsdr_dev_t *dev, int on); 86 | 87 | /*! 88 | * Enable offset tuning and report status on stderr 89 | * 90 | * \param dev the device handle given by rtlsdr_open() 91 | * \return 0 on success 92 | */ 93 | 94 | int verbose_offset_tuning(rtlsdr_dev_t *dev); 95 | 96 | /*! 97 | * Enable auto gain and report status on stderr 98 | * 99 | * \param dev the device handle given by rtlsdr_open() 100 | * \return 0 on success 101 | */ 102 | 103 | int verbose_auto_gain(rtlsdr_dev_t *dev); 104 | 105 | /*! 106 | * Set tuner gain and report status on stderr 107 | * 108 | * \param dev the device handle given by rtlsdr_open() 109 | * \param gain in tenths of a dB 110 | * \return 0 on success 111 | */ 112 | 113 | int verbose_gain_set(rtlsdr_dev_t *dev, int gain); 114 | 115 | /*! 116 | * Set the frequency correction value for the device and report status on stderr. 117 | * 118 | * \param dev the device handle given by rtlsdr_open() 119 | * \param ppm_error correction value in parts per million (ppm) 120 | * \return 0 on success 121 | */ 122 | 123 | int verbose_ppm_set(rtlsdr_dev_t *dev, int ppm_error); 124 | 125 | /*! 126 | * Attempts to extract a correction value from eeprom and store it to an int. 127 | * 128 | * \param dev the device handle given by rtlsdr_open() 129 | * \param ppm_error correction value in parts per million (ppm) 130 | * \return 0 on success 131 | */ 132 | int verbose_ppm_eeprom(rtlsdr_dev_t *dev, int *ppm_error); 133 | 134 | /*! 135 | * Reset buffer 136 | * 137 | * \param dev the device handle given by rtlsdr_open() 138 | * \return 0 on success 139 | */ 140 | 141 | int verbose_reset_buffer(rtlsdr_dev_t *dev); 142 | 143 | /*! 144 | * Find the closest matching device. 145 | * 146 | * \param s a string to be parsed 147 | * \return dev_index int, -1 on error 148 | */ 149 | 150 | int verbose_device_search(char *s); 151 | 152 | -------------------------------------------------------------------------------- /src/getopt/getopt.h: -------------------------------------------------------------------------------- 1 | /* Declarations for getopt. 2 | Copyright (C) 1989-1994, 1996-1999, 2001 Free Software Foundation, Inc. 3 | This file is part of the GNU C Library. 4 | 5 | The GNU C Library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | The GNU C Library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with the GNU C Library; if not, write to the Free 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 18 | 02111-1307 USA. */ 19 | 20 | #ifndef _GETOPT_H 21 | 22 | #ifndef __need_getopt 23 | # define _GETOPT_H 1 24 | #endif 25 | 26 | /* If __GNU_LIBRARY__ is not already defined, either we are being used 27 | standalone, or this is the first header included in the source file. 28 | If we are being used with glibc, we need to include , but 29 | that does not exist if we are standalone. So: if __GNU_LIBRARY__ is 30 | not defined, include , which will pull in for us 31 | if it's from glibc. (Why ctype.h? It's guaranteed to exist and it 32 | doesn't flood the namespace with stuff the way some other headers do.) */ 33 | #if !defined __GNU_LIBRARY__ 34 | # include 35 | #endif 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /* For communication from `getopt' to the caller. 42 | When `getopt' finds an option that takes an argument, 43 | the argument value is returned here. 44 | Also, when `ordering' is RETURN_IN_ORDER, 45 | each non-option ARGV-element is returned here. */ 46 | 47 | extern char *optarg; 48 | 49 | /* Index in ARGV of the next element to be scanned. 50 | This is used for communication to and from the caller 51 | and for communication between successive calls to `getopt'. 52 | 53 | On entry to `getopt', zero means this is the first call; initialize. 54 | 55 | When `getopt' returns -1, this is the index of the first of the 56 | non-option elements that the caller should itself scan. 57 | 58 | Otherwise, `optind' communicates from one call to the next 59 | how much of ARGV has been scanned so far. */ 60 | 61 | extern int optind; 62 | 63 | /* Callers store zero here to inhibit the error message `getopt' prints 64 | for unrecognized options. */ 65 | 66 | extern int opterr; 67 | 68 | /* Set to an option character which was unrecognized. */ 69 | 70 | extern int optopt; 71 | 72 | #ifndef __need_getopt 73 | /* Describe the long-named options requested by the application. 74 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector 75 | of `struct option' terminated by an element containing a name which is 76 | zero. 77 | 78 | The field `has_arg' is: 79 | no_argument (or 0) if the option does not take an argument, 80 | required_argument (or 1) if the option requires an argument, 81 | optional_argument (or 2) if the option takes an optional argument. 82 | 83 | If the field `flag' is not NULL, it points to a variable that is set 84 | to the value given in the field `val' when the option is found, but 85 | left unchanged if the option is not found. 86 | 87 | To have a long-named option do something other than set an `int' to 88 | a compiled-in constant, such as set a value from `optarg', set the 89 | option's `flag' field to zero and its `val' field to a nonzero 90 | value (the equivalent single-letter option character, if there is 91 | one). For long options that have a zero `flag' field, `getopt' 92 | returns the contents of the `val' field. */ 93 | 94 | struct option 95 | { 96 | # if (defined __STDC__ && __STDC__) || defined __cplusplus 97 | const char *name; 98 | # else 99 | char *name; 100 | # endif 101 | /* has_arg can't be an enum because some compilers complain about 102 | type mismatches in all the code that assumes it is an int. */ 103 | int has_arg; 104 | int *flag; 105 | int val; 106 | }; 107 | 108 | /* Names for the values of the `has_arg' field of `struct option'. */ 109 | 110 | # define no_argument 0 111 | # define required_argument 1 112 | # define optional_argument 2 113 | #endif /* need getopt */ 114 | 115 | 116 | /* Get definitions and prototypes for functions to process the 117 | arguments in ARGV (ARGC of them, minus the program name) for 118 | options given in OPTS. 119 | 120 | Return the option character from OPTS just read. Return -1 when 121 | there are no more options. For unrecognized options, or options 122 | missing arguments, `optopt' is set to the option letter, and '?' is 123 | returned. 124 | 125 | The OPTS string is a list of characters which are recognized option 126 | letters, optionally followed by colons, specifying that that letter 127 | takes an argument, to be placed in `optarg'. 128 | 129 | If a letter in OPTS is followed by two colons, its argument is 130 | optional. This behavior is specific to the GNU `getopt'. 131 | 132 | The argument `--' causes premature termination of argument 133 | scanning, explicitly telling `getopt' that there are no more 134 | options. 135 | 136 | If OPTS begins with `--', then non-option arguments are treated as 137 | arguments to the option '\0'. This behavior is specific to the GNU 138 | `getopt'. */ 139 | 140 | #if (defined __STDC__ && __STDC__) || defined __cplusplus 141 | # ifdef __GNU_LIBRARY__ 142 | /* Many other libraries have conflicting prototypes for getopt, with 143 | differences in the consts, in stdlib.h. To avoid compilation 144 | errors, only prototype getopt for the GNU C library. */ 145 | extern int getopt (int __argc, char *const *__argv, const char *__shortopts); 146 | # else /* not __GNU_LIBRARY__ */ 147 | extern int getopt (); 148 | # endif /* __GNU_LIBRARY__ */ 149 | 150 | # ifndef __need_getopt 151 | extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts, 152 | const struct option *__longopts, int *__longind); 153 | extern int getopt_long_only (int __argc, char *const *__argv, 154 | const char *__shortopts, 155 | const struct option *__longopts, int *__longind); 156 | 157 | /* Internal only. Users should not call this directly. */ 158 | extern int _getopt_internal (int __argc, char *const *__argv, 159 | const char *__shortopts, 160 | const struct option *__longopts, int *__longind, 161 | int __long_only); 162 | # endif 163 | #else /* not __STDC__ */ 164 | extern int getopt (); 165 | # ifndef __need_getopt 166 | extern int getopt_long (); 167 | extern int getopt_long_only (); 168 | 169 | extern int _getopt_internal (); 170 | # endif 171 | #endif /* __STDC__ */ 172 | 173 | #ifdef __cplusplus 174 | } 175 | #endif 176 | 177 | /* Make sure we later can get all the definitions and declarations. */ 178 | #undef __need_getopt 179 | 180 | #endif /* getopt.h */ 181 | -------------------------------------------------------------------------------- /src/rtl_adsb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * Copyright (C) 2012 by Steve Markgraf 4 | * Copyright (C) 2012 by Hoernchen 5 | * Copyright (C) 2012 by Kyle Keen 6 | * Copyright (C) 2012 by Youssef Touil 7 | * Copyright (C) 2012 by Ian Gilmour 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #ifndef _WIN32 32 | #include 33 | #else 34 | #include 35 | #include 36 | #include 37 | #include "getopt/getopt.h" 38 | #endif 39 | 40 | #include 41 | #include 42 | 43 | #include "rtl-sdr.h" 44 | #include "convenience/convenience.h" 45 | 46 | #if defined(_MSC_VER) && _MSC_VER < 1800 47 | #define round(x) (x > 0.0 ? floor(x + 0.5): ceil(x - 0.5)) 48 | #endif 49 | 50 | #define ADSB_RATE 2000000 51 | #define ADSB_FREQ 1090000000 52 | #define DEFAULT_ASYNC_BUF_NUMBER 12 53 | #define DEFAULT_BUF_LENGTH (16 * 16384) 54 | #define AUTO_GAIN -100 55 | 56 | #define MESSAGEGO 253 57 | #define OVERWRITE 254 58 | #define BADSAMPLE 255 59 | 60 | static pthread_t demod_thread; 61 | static pthread_cond_t ready; 62 | static pthread_mutex_t ready_m; 63 | static volatile int do_exit = 0; 64 | static rtlsdr_dev_t *dev = NULL; 65 | 66 | uint16_t squares[256]; 67 | 68 | /* todo, bundle these up in a struct */ 69 | uint8_t *buffer; /* also abused for uint16_t */ 70 | int verbose_output = 0; 71 | int short_output = 0; 72 | int quality = 10; 73 | int allowed_errors = 5; 74 | FILE *file; 75 | int adsb_frame[14]; 76 | #define preamble_len 16 77 | #define long_frame 112 78 | #define short_frame 56 79 | 80 | /* signals are not threadsafe by default */ 81 | #define safe_cond_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m) 82 | #define safe_cond_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m) 83 | 84 | void usage(void) 85 | { 86 | fprintf(stderr, 87 | "rtl_adsb, a simple ADS-B decoder\n\n" 88 | "Use:\trtl_adsb [-R] [-g gain] [-p ppm] [output file]\n" 89 | "\t[-d device_index (default: 0)]\n" 90 | "\t[-V verbove output (default: off)]\n" 91 | "\t[-S show short frames (default: off)]\n" 92 | "\t[-Q quality (0: no sanity checks, 0.5: half bit, 1: one bit (default), 2: two bits)]\n" 93 | "\t[-e allowed_errors (default: 5)]\n" 94 | "\t[-g tuner_gain (default: automatic)]\n" 95 | "\t[-p ppm_error (default: 0)]\n" 96 | "\tfilename (a '-' dumps samples to stdout)\n" 97 | "\t (omitting the filename also uses stdout)\n\n" 98 | "Streaming with netcat:\n" 99 | "\trtl_adsb | netcat -lp 8080\n" 100 | "\twhile true; do rtl_adsb | nc -lp 8080; done\n" 101 | "Streaming with socat:\n" 102 | "\trtl_adsb | socat -u - TCP4:sdrsharp.com:47806\n" 103 | "\n"); 104 | exit(1); 105 | } 106 | 107 | #ifdef _WIN32 108 | BOOL WINAPI 109 | sighandler(int signum) 110 | { 111 | if (CTRL_C_EVENT == signum) { 112 | fprintf(stderr, "Signal caught, exiting!\n"); 113 | do_exit = 1; 114 | rtlsdr_cancel_async(dev); 115 | return TRUE; 116 | } 117 | return FALSE; 118 | } 119 | #else 120 | static void sighandler(int signum) 121 | { 122 | fprintf(stderr, "Signal caught, exiting!\n"); 123 | do_exit = 1; 124 | rtlsdr_cancel_async(dev); 125 | } 126 | #endif 127 | 128 | void display(int *frame, int len) 129 | { 130 | int i, df; 131 | if (!short_output && len <= short_frame) { 132 | return;} 133 | df = (frame[0] >> 3) & 0x1f; 134 | if (quality == 0 && !(df==11 || df==17 || df==18 || df==19)) { 135 | return;} 136 | fprintf(file, "*"); 137 | for (i=0; i<((len+7)/8); i++) { 138 | fprintf(file, "%02x", frame[i]);} 139 | fprintf(file, ";\r\n"); 140 | if (!verbose_output) { 141 | return;} 142 | fprintf(file, "DF=%i CA=%i\n", df, frame[0] & 0x07); 143 | fprintf(file, "ICAO Address=%06x\n", frame[1] << 16 | frame[2] << 8 | frame[3]); 144 | if (len <= short_frame) { 145 | return;} 146 | fprintf(file, "PI=0x%06x\n", frame[11] << 16 | frame[12] << 8 | frame[13]); 147 | fprintf(file, "Type Code=%i S.Type/Ant.=%x\n", (frame[4] >> 3) & 0x1f, frame[4] & 0x07); 148 | fprintf(file, "--------------\n"); 149 | } 150 | 151 | int abs8(int x) 152 | /* do not subtract 127 from the raw iq, this handles it */ 153 | { 154 | if (x >= 127) { 155 | return x - 127;} 156 | return 127 - x; 157 | } 158 | 159 | void squares_precompute(void) 160 | /* equiv to abs(x-128) ^ 2 */ 161 | { 162 | int i, j; 163 | // todo, check if this LUT is actually any faster 164 | for (i=0; i<256; i++) { 165 | j = abs8(i); 166 | squares[i] = (uint16_t)(j*j); 167 | } 168 | } 169 | 170 | int magnitute(uint8_t *buf, int len) 171 | /* takes i/q, changes buf in place (16 bit), returns new len (16 bit) */ 172 | { 173 | int i; 174 | uint16_t *m; 175 | for (i=0; i b; 187 | bit = c > d; 188 | 189 | if (quality == 0) { 190 | return bit;} 191 | 192 | if (quality == 5) { 193 | if ( bit && bit_p && b > c) { 194 | return BADSAMPLE;} 195 | if (!bit && !bit_p && b < c) { 196 | return BADSAMPLE;} 197 | return bit; 198 | } 199 | 200 | if (quality == 10) { 201 | if ( bit && bit_p && c > b) { 202 | return 1;} 203 | if ( bit && !bit_p && d < b) { 204 | return 1;} 205 | if (!bit && bit_p && d > b) { 206 | return 0;} 207 | if (!bit && !bit_p && c < b) { 208 | return 0;} 209 | return BADSAMPLE; 210 | } 211 | 212 | if ( bit && bit_p && c > b && d < a) { 213 | return 1;} 214 | if ( bit && !bit_p && c > a && d < b) { 215 | return 1;} 216 | if (!bit && bit_p && c < a && d > b) { 217 | return 0;} 218 | if (!bit && !bit_p && c < b && d > a) { 219 | return 0;} 220 | return BADSAMPLE; 221 | } 222 | 223 | inline uint16_t min16(uint16_t a, uint16_t b) 224 | { 225 | return ab ? a : b; 231 | } 232 | 233 | inline int preamble(uint16_t *buf, int i) 234 | /* returns 0/1 for preamble at index i */ 235 | { 236 | int i2; 237 | uint16_t low = 0; 238 | uint16_t high = 65535; 239 | for (i2=0; i2 allowed_errors) { 291 | buf[i2] = BADSAMPLE; 292 | break; 293 | } else { 294 | bit = a > b; 295 | /* these don't have to match the bit */ 296 | a = 0; 297 | b = 65535; 298 | } 299 | } 300 | buf[i] = buf[i+1] = OVERWRITE; 301 | buf[i2] = bit; 302 | } 303 | } 304 | } 305 | 306 | void messages(uint16_t *buf, int len) 307 | { 308 | int i, data_i, index, shift, frame_len; 309 | // todo, allow wrap across buffers 310 | for (i=0; i 1) { 312 | continue;} 313 | frame_len = long_frame; 314 | data_i = 0; 315 | for (index=0; index<14; index++) { 316 | adsb_frame[index] = 0;} 317 | for(; i= 0 ? r : -r; 492 | } 493 | 494 | -------------------------------------------------------------------------------- /src/rtl_eeprom.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * rtl_eeprom, EEPROM modification tool 4 | * Copyright (C) 2012 by Steve Markgraf 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 . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #ifndef _WIN32 25 | #include 26 | #else 27 | #include 28 | #include "getopt/getopt.h" 29 | #endif 30 | 31 | #include "rtl-sdr.h" 32 | 33 | #define EEPROM_SIZE 256 34 | #define MAX_STR_SIZE 256 35 | #define STR_OFFSET 0x09 36 | 37 | static rtlsdr_dev_t *dev = NULL; 38 | 39 | typedef struct rtlsdr_config { 40 | uint16_t vendor_id; 41 | uint16_t product_id; 42 | char manufacturer[MAX_STR_SIZE]; 43 | char product[MAX_STR_SIZE]; 44 | char serial[MAX_STR_SIZE]; 45 | int have_serial; 46 | int enable_ir; 47 | int remote_wakeup; 48 | } rtlsdr_config_t; 49 | 50 | void dump_config(rtlsdr_config_t *conf) 51 | { 52 | fprintf(stderr, "__________________________________________\n"); 53 | fprintf(stderr, "Vendor ID:\t\t0x%04x\n", conf->vendor_id); 54 | fprintf(stderr, "Product ID:\t\t0x%04x\n", conf->product_id); 55 | fprintf(stderr, "Manufacturer:\t\t%s\n", conf->manufacturer); 56 | fprintf(stderr, "Product:\t\t%s\n", conf->product); 57 | fprintf(stderr, "Serial number:\t\t%s\n", conf->serial); 58 | fprintf(stderr, "Serial number enabled:\t"); 59 | fprintf(stderr, conf->have_serial ? "yes\n": "no\n"); 60 | fprintf(stderr, "IR endpoint enabled:\t"); 61 | fprintf(stderr, conf->enable_ir ? "yes\n": "no\n"); 62 | fprintf(stderr, "Remote wakeup enabled:\t"); 63 | fprintf(stderr, conf->remote_wakeup ? "yes\n": "no\n"); 64 | fprintf(stderr, "__________________________________________\n"); 65 | } 66 | 67 | void usage(void) 68 | { 69 | fprintf(stderr, 70 | "rtl_eeprom, an EEPROM programming tool for " 71 | "RTL2832 based DVB-T receivers\n\n" 72 | "Usage:\n" 73 | "\t[-d device_index (default: 0)]\n" 74 | "\t[-m set manufacturer string]\n" 75 | "\t[-p set product string]\n" 76 | "\t[-s set serial number string]\n" 77 | "\t[-i <0,1> disable/enable IR-endpoint]\n" 78 | "\t[-g generate default config and write to device]\n" 79 | "\t[ can be one of:]\n" 80 | "\t[ realtek\t\tRealtek default (as without EEPROM)]\n" 81 | "\t[ realtek_oem\t\tRealtek default OEM with EEPROM]\n" 82 | "\t[ noxon\t\tTerratec NOXON DAB Stick]\n" 83 | "\t[ terratec_black\tTerratec T Stick Black]\n" 84 | "\t[ terratec_plus\tTerratec T Stick+ (DVB-T/DAB)]\n" 85 | "\t[-w write dumped file to device]\n" 86 | "\t[-r dump EEPROM to file]\n" 87 | "\t[-h display this help text]\n" 88 | "\nUse on your own risk, especially -w!\n"); 89 | exit(1); 90 | } 91 | 92 | int get_string_descriptor(int pos, uint8_t *data, char *str) 93 | { 94 | int len, i, j = 0; 95 | 96 | len = data[pos]; 97 | 98 | if (data[pos + 1] != 0x03) 99 | fprintf(stderr, "Error: invalid string descriptor!\n"); 100 | 101 | for (i = 2; i < len; i += 2) 102 | str[j++] = data[pos + i]; 103 | 104 | str[j] = 0x00; 105 | 106 | return pos + i; 107 | } 108 | 109 | int set_string_descriptor(int pos, uint8_t *data, char *str) 110 | { 111 | int i = 0, j = 2; 112 | 113 | if (pos < 0) 114 | return -1; 115 | 116 | data[pos + 1] = 0x03; 117 | 118 | while (str[i] != 0x00) { 119 | if ((pos + j) >= 78) { 120 | fprintf(stderr, "Error: string too long, truncated!\n"); 121 | return -1; 122 | } 123 | data[pos + j++] = str[i++]; 124 | data[pos + j++] = 0x00; 125 | } 126 | 127 | data[pos] = j; 128 | 129 | return pos + j; 130 | } 131 | 132 | int parse_eeprom_to_conf(rtlsdr_config_t *conf, uint8_t *dat) 133 | { 134 | int pos; 135 | 136 | if ((dat[0] != 0x28) || (dat[1] != 0x32)) 137 | fprintf(stderr, "Error: invalid RTL2832 EEPROM header!\n"); 138 | 139 | conf->vendor_id = dat[2] | (dat[3] << 8); 140 | conf->product_id = dat[4] | (dat[5] << 8); 141 | conf->have_serial = (dat[6] == 0xa5) ? 1 : 0; 142 | conf->remote_wakeup = (dat[7] & 0x01) ? 1 : 0; 143 | conf->enable_ir = (dat[7] & 0x02) ? 1 : 0; 144 | 145 | pos = get_string_descriptor(STR_OFFSET, dat, conf->manufacturer); 146 | pos = get_string_descriptor(pos, dat, conf->product); 147 | get_string_descriptor(pos, dat, conf->serial); 148 | 149 | return 0; 150 | } 151 | 152 | int gen_eeprom_from_conf(rtlsdr_config_t *conf, uint8_t *dat) 153 | { 154 | int pos; 155 | 156 | dat[0] = 0x28; 157 | dat[1] = 0x32; 158 | dat[2] = conf->vendor_id & 0xff; 159 | dat[3] = (conf->vendor_id >> 8) & 0xff ; 160 | dat[4] = conf->product_id & 0xff; 161 | dat[5] = (conf->product_id >> 8) & 0xff; 162 | dat[6] = conf->have_serial ? 0xa5 : 0x00; 163 | dat[7] = 0x14; 164 | dat[7] |= conf->remote_wakeup ? 0x01 : 0x00; 165 | dat[7] |= conf->enable_ir ? 0x02 : 0x00; 166 | dat[8] = 0x02; 167 | 168 | pos = set_string_descriptor(STR_OFFSET, dat, conf->manufacturer); 169 | pos = set_string_descriptor(pos, dat, conf->product); 170 | pos = set_string_descriptor(pos, dat, conf->serial); 171 | 172 | dat[78] = 0x00; /* length of IR config */ 173 | 174 | return pos; 175 | } 176 | 177 | enum configs { 178 | CONF_NONE = 0, 179 | REALTEK, 180 | REALTEK_EEPROM, 181 | TERRATEC_NOXON, 182 | TERRATEC_T_BLACK, 183 | TERRATEC_T_PLUS, 184 | }; 185 | 186 | void gen_default_conf(rtlsdr_config_t *conf, int config) 187 | { 188 | switch (config) { 189 | case REALTEK: 190 | fprintf(stderr, "Realtek default (as without EEPROM)\n"); 191 | conf->vendor_id = 0x0bda; 192 | conf->product_id = 0x2832; 193 | strcpy(conf->manufacturer, "Generic"); 194 | strcpy(conf->product, "RTL2832U DVB-T"); 195 | strcpy(conf->serial, "0"); 196 | conf->have_serial = 1; 197 | conf->enable_ir = 0; 198 | conf->remote_wakeup = 1; 199 | break; 200 | case REALTEK_EEPROM: 201 | fprintf(stderr, "Realtek default OEM with EEPROM\n"); 202 | conf->vendor_id = 0x0bda; 203 | conf->product_id = 0x2838; 204 | strcpy(conf->manufacturer, "Realtek"); 205 | strcpy(conf->product, "RTL2838UHIDIR"); 206 | strcpy(conf->serial, "00000001"); 207 | conf->have_serial = 1; 208 | conf->enable_ir = 1; 209 | conf->remote_wakeup = 0; 210 | break; 211 | case TERRATEC_NOXON: 212 | fprintf(stderr, "Terratec NOXON DAB Stick\n"); 213 | conf->vendor_id = 0x0ccd; 214 | conf->product_id = 0x00b3; 215 | strcpy(conf->manufacturer, "NOXON"); 216 | strcpy(conf->product, "DAB Stick"); 217 | strcpy(conf->serial, "0"); 218 | conf->have_serial = 1; 219 | conf->enable_ir = 0; 220 | conf->remote_wakeup = 1; 221 | break; 222 | case TERRATEC_T_BLACK: 223 | fprintf(stderr, "Terratec T Stick Black\n"); 224 | conf->vendor_id = 0x0ccd; 225 | conf->product_id = 0x00a9; 226 | strcpy(conf->manufacturer, "Realtek"); 227 | strcpy(conf->product, "RTL2838UHIDIR"); 228 | strcpy(conf->serial, "00000001"); 229 | conf->have_serial = 1; 230 | conf->enable_ir = 1; 231 | conf->remote_wakeup = 0; 232 | break; 233 | case TERRATEC_T_PLUS: 234 | fprintf(stderr, "Terratec ran T Stick+\n"); 235 | conf->vendor_id = 0x0ccd; 236 | conf->product_id = 0x00d7; 237 | strcpy(conf->manufacturer, "Realtek"); 238 | strcpy(conf->product, "RTL2838UHIDIR"); 239 | strcpy(conf->serial, "00000001"); 240 | conf->have_serial = 1; 241 | conf->enable_ir = 1; 242 | conf->remote_wakeup = 0; 243 | break; 244 | default: 245 | break; 246 | }; 247 | } 248 | 249 | int main(int argc, char **argv) 250 | { 251 | int i, r, opt; 252 | uint32_t dev_index = 0; 253 | int device_count; 254 | char *filename = NULL; 255 | FILE *file = NULL; 256 | char *manuf_str = NULL; 257 | char *product_str = NULL; 258 | char *serial_str = NULL; 259 | uint8_t buf[EEPROM_SIZE]; 260 | rtlsdr_config_t conf; 261 | int flash_file = 0; 262 | int default_config = 0; 263 | int change = 0; 264 | int ir_endpoint = 0; 265 | char ch; 266 | 267 | while ((opt = getopt(argc, argv, "d:m:p:s:i:g:w:r:h?")) != -1) { 268 | switch (opt) { 269 | case 'd': 270 | dev_index = atoi(optarg); 271 | break; 272 | case 'm': 273 | manuf_str = optarg; 274 | change = 1; 275 | break; 276 | case 'p': 277 | product_str = optarg; 278 | change = 1; 279 | break; 280 | case 's': 281 | serial_str = optarg; 282 | change = 1; 283 | break; 284 | case 'i': 285 | ir_endpoint = (atoi(optarg) > 0) ? 1 : -1; 286 | change = 1; 287 | break; 288 | case 'g': 289 | if (!strcmp(optarg, "realtek")) 290 | default_config = REALTEK; 291 | else if (!strcmp(optarg, "realtek_oem")) 292 | default_config = REALTEK_EEPROM; 293 | else if (!strcmp(optarg, "noxon")) 294 | default_config = TERRATEC_NOXON; 295 | else if (!strcmp(optarg, "terratec_black")) 296 | default_config = TERRATEC_T_BLACK; 297 | else if (!strcmp(optarg, "terratec_plus")) 298 | default_config = TERRATEC_T_PLUS; 299 | 300 | if (default_config != CONF_NONE) 301 | change = 1; 302 | break; 303 | case 'w': 304 | flash_file = 1; 305 | change = 1; 306 | case 'r': 307 | filename = optarg; 308 | break; 309 | default: 310 | usage(); 311 | break; 312 | } 313 | } 314 | 315 | device_count = rtlsdr_get_device_count(); 316 | if (!device_count) { 317 | fprintf(stderr, "No supported devices found.\n"); 318 | exit(1); 319 | } 320 | 321 | fprintf(stderr, "Found %d device(s):\n", device_count); 322 | for (i = 0; i < device_count; i++) 323 | fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i)); 324 | fprintf(stderr, "\n"); 325 | 326 | fprintf(stderr, "Using device %d: %s\n", 327 | dev_index, 328 | rtlsdr_get_device_name(dev_index)); 329 | 330 | r = rtlsdr_open(&dev, dev_index); 331 | if (r < 0) { 332 | fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); 333 | exit(1); 334 | } 335 | 336 | fprintf(stderr, "\n"); 337 | 338 | r = rtlsdr_read_eeprom(dev, buf, 0, EEPROM_SIZE); 339 | if (r < 0) { 340 | if (r == -3) 341 | fprintf(stderr, "No EEPROM has been found.\n"); 342 | else 343 | fprintf(stderr, "Failed to read EEPROM, err %i.\n", r); 344 | goto exit; 345 | } 346 | 347 | if (r < 0) 348 | return -1; 349 | 350 | fprintf(stderr, "Current configuration:\n"); 351 | parse_eeprom_to_conf(&conf, buf); 352 | dump_config(&conf); 353 | 354 | if (filename) { 355 | file = fopen(filename, flash_file ? "rb" : "wb"); 356 | if (!file) { 357 | fprintf(stderr, "Error opening file!\n"); 358 | goto exit; 359 | } 360 | if (flash_file) { 361 | if (fread(buf, 1, sizeof(buf), file) != sizeof(buf)) 362 | fprintf(stderr, "Error reading file!\n"); 363 | } else { 364 | if (fwrite(buf, 1, sizeof(buf), file) != sizeof(buf)) 365 | fprintf(stderr, "Short write, exiting!\n"); 366 | else 367 | fprintf(stderr, "\nDump to %s successful.\n", filename); 368 | } 369 | } 370 | 371 | if (manuf_str) 372 | strncpy((char*)&conf.manufacturer, manuf_str, MAX_STR_SIZE); 373 | 374 | if (product_str) 375 | strncpy((char*)&conf.product, product_str, MAX_STR_SIZE); 376 | 377 | if (serial_str) { 378 | conf.have_serial = 1; 379 | strncpy((char*)&conf.serial, serial_str, MAX_STR_SIZE); 380 | } 381 | 382 | if (ir_endpoint != 0) 383 | conf.enable_ir = (ir_endpoint > 0) ? 1 : 0; 384 | 385 | if (!change) 386 | goto exit; 387 | 388 | fprintf(stderr, "\nNew configuration:\n"); 389 | 390 | if (default_config != CONF_NONE) 391 | gen_default_conf(&conf, default_config); 392 | 393 | if (!flash_file) { 394 | if (gen_eeprom_from_conf(&conf, buf) < 0) 395 | goto exit; 396 | } 397 | 398 | parse_eeprom_to_conf(&conf, buf); 399 | dump_config(&conf); 400 | 401 | fprintf(stderr, "Write new configuration to device [y/n]? "); 402 | 403 | while ((ch = getchar())) { 404 | if (ch != 'y') 405 | goto exit; 406 | else 407 | break; 408 | } 409 | 410 | r = rtlsdr_write_eeprom(dev, buf, 0, flash_file ? EEPROM_SIZE : 128); 411 | if (r < 0) 412 | fprintf(stderr, "Error while writing EEPROM: %i\n", r); 413 | else 414 | fprintf(stderr, "\nConfiguration successfully written.\n" 415 | "Please replug the device for changes" 416 | " to take effect.\n"); 417 | 418 | exit: 419 | if (file) 420 | fclose(file); 421 | 422 | rtlsdr_close(dev); 423 | 424 | return r >= 0 ? r : -r; 425 | } 426 | -------------------------------------------------------------------------------- /src/rtl_sdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * Copyright (C) 2012 by Steve Markgraf 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifndef _WIN32 26 | #include 27 | #else 28 | #include 29 | #include 30 | #include 31 | #include "getopt/getopt.h" 32 | #endif 33 | 34 | #include "rtl-sdr.h" 35 | #include "convenience/convenience.h" 36 | 37 | #define DEFAULT_SAMPLE_RATE 2048000 38 | #define DEFAULT_BUF_LENGTH (16 * 16384) 39 | #define MINIMAL_BUF_LENGTH 512 40 | #define MAXIMAL_BUF_LENGTH (256 * 16384) 41 | 42 | static int do_exit = 0; 43 | static uint32_t bytes_to_read = 0; 44 | static rtlsdr_dev_t *dev = NULL; 45 | 46 | void usage(void) 47 | { 48 | fprintf(stderr, 49 | "rtl_sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n" 50 | "Usage:\t -f frequency_to_tune_to [Hz]\n" 51 | "\t[-s samplerate (default: 2048000 Hz)]\n" 52 | "\t[-d device_index (default: 0)]\n" 53 | "\t[-g gain (default: 0 for auto)]\n" 54 | "\t[-p ppm_error (default: 0)]\n" 55 | "\t[-b output_block_size (default: 16 * 16384)]\n" 56 | "\t[-n number of samples to read (default: 0, infinite)]\n" 57 | "\t[-S force sync output (default: async)]\n" 58 | "\t[-D direct_sampling_mode, 0 (default/off), 1 (I), 2 (Q), 3 (no-mod)]\n" 59 | "\t[-N no dithering (default: use dithering)]\n" 60 | "\tfilename (a '-' dumps samples to stdout)\n\n"); 61 | exit(1); 62 | } 63 | 64 | #ifdef _WIN32 65 | BOOL WINAPI 66 | sighandler(int signum) 67 | { 68 | if (CTRL_C_EVENT == signum) { 69 | fprintf(stderr, "Signal caught, exiting!\n"); 70 | do_exit = 1; 71 | rtlsdr_cancel_async(dev); 72 | return TRUE; 73 | } 74 | return FALSE; 75 | } 76 | #else 77 | static void sighandler(int signum) 78 | { 79 | fprintf(stderr, "Signal caught, exiting!\n"); 80 | do_exit = 1; 81 | rtlsdr_cancel_async(dev); 82 | } 83 | #endif 84 | 85 | static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) 86 | { 87 | if (ctx) { 88 | if (do_exit) 89 | return; 90 | 91 | if ((bytes_to_read > 0) && (bytes_to_read < len)) { 92 | len = bytes_to_read; 93 | do_exit = 1; 94 | rtlsdr_cancel_async(dev); 95 | } 96 | 97 | if (fwrite(buf, 1, len, (FILE*)ctx) != len) { 98 | fprintf(stderr, "Short write, samples lost, exiting!\n"); 99 | rtlsdr_cancel_async(dev); 100 | } 101 | 102 | if (bytes_to_read > 0) 103 | bytes_to_read -= len; 104 | } 105 | } 106 | 107 | int main(int argc, char **argv) 108 | { 109 | #ifndef _WIN32 110 | struct sigaction sigact; 111 | #endif 112 | char *filename = NULL; 113 | int n_read; 114 | int r, opt; 115 | int gain = 0; 116 | int ppm_error = 0; 117 | int sync_mode = 0; 118 | int direct_sampling = 0; 119 | int dithering = 1; 120 | FILE *file; 121 | uint8_t *buffer; 122 | int dev_index = 0; 123 | int dev_given = 0; 124 | uint32_t frequency = 100000000; 125 | uint32_t samp_rate = DEFAULT_SAMPLE_RATE; 126 | uint32_t out_block_size = DEFAULT_BUF_LENGTH; 127 | 128 | while ((opt = getopt(argc, argv, "d:f:g:s:b:n:p:D:SN")) != -1) { 129 | switch (opt) { 130 | case 'd': 131 | dev_index = verbose_device_search(optarg); 132 | dev_given = 1; 133 | break; 134 | case 'f': 135 | frequency = (uint32_t)atofs(optarg); 136 | break; 137 | case 'g': 138 | gain = (int)(atof(optarg) * 10); /* tenths of a dB */ 139 | break; 140 | case 's': 141 | samp_rate = (uint32_t)atofs(optarg); 142 | break; 143 | case 'p': 144 | ppm_error = atoi(optarg); 145 | break; 146 | case 'b': 147 | out_block_size = (uint32_t)atof(optarg); 148 | break; 149 | case 'n': 150 | bytes_to_read = (uint32_t)atofs(optarg) * 2; 151 | break; 152 | case 'S': 153 | sync_mode = 1; 154 | break; 155 | case 'D': 156 | direct_sampling = atoi(optarg); 157 | break; 158 | case 'N': 159 | dithering = 0; 160 | break; 161 | default: 162 | usage(); 163 | break; 164 | } 165 | } 166 | 167 | if (argc <= optind) { 168 | usage(); 169 | } else { 170 | filename = argv[optind]; 171 | } 172 | 173 | if(out_block_size < MINIMAL_BUF_LENGTH || 174 | out_block_size > MAXIMAL_BUF_LENGTH ){ 175 | fprintf(stderr, 176 | "Output block size wrong value, falling back to default\n"); 177 | fprintf(stderr, 178 | "Minimal length: %u\n", MINIMAL_BUF_LENGTH); 179 | fprintf(stderr, 180 | "Maximal length: %u\n", MAXIMAL_BUF_LENGTH); 181 | out_block_size = DEFAULT_BUF_LENGTH; 182 | } 183 | 184 | buffer = malloc(out_block_size * sizeof(uint8_t)); 185 | 186 | if (!dev_given) { 187 | dev_index = verbose_device_search("0"); 188 | } 189 | 190 | if (dev_index < 0) { 191 | exit(1); 192 | } 193 | 194 | r = rtlsdr_open(&dev, (uint32_t)dev_index); 195 | if (r < 0) { 196 | fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); 197 | exit(1); 198 | } 199 | #ifndef _WIN32 200 | sigact.sa_handler = sighandler; 201 | sigemptyset(&sigact.sa_mask); 202 | sigact.sa_flags = 0; 203 | sigaction(SIGINT, &sigact, NULL); 204 | sigaction(SIGTERM, &sigact, NULL); 205 | sigaction(SIGQUIT, &sigact, NULL); 206 | sigaction(SIGPIPE, &sigact, NULL); 207 | #else 208 | SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); 209 | #endif 210 | 211 | if (!dithering) { 212 | fprintf(stderr, "Disabling dithering... "); 213 | r = rtlsdr_set_dithering(dev, dithering); 214 | if (r) { 215 | fprintf(stderr, "failure\n"); 216 | } else { 217 | fprintf(stderr, "success\n"); 218 | } 219 | } 220 | 221 | if (direct_sampling) { 222 | verbose_direct_sampling(dev, direct_sampling); 223 | } 224 | 225 | /* Set the sample rate */ 226 | verbose_set_sample_rate(dev, samp_rate); 227 | 228 | /* Set the frequency */ 229 | verbose_set_frequency(dev, frequency); 230 | 231 | if (0 == gain) { 232 | /* Enable automatic gain */ 233 | verbose_auto_gain(dev); 234 | } else { 235 | /* Enable manual gain */ 236 | gain = nearest_gain(dev, gain); 237 | verbose_gain_set(dev, gain); 238 | } 239 | 240 | verbose_ppm_set(dev, ppm_error); 241 | 242 | if(strcmp(filename, "-") == 0) { /* Write samples to stdout */ 243 | file = stdout; 244 | #ifdef _WIN32 245 | _setmode(_fileno(stdin), _O_BINARY); 246 | #endif 247 | } else { 248 | file = fopen(filename, "wb"); 249 | if (!file) { 250 | fprintf(stderr, "Failed to open %s\n", filename); 251 | goto out; 252 | } 253 | } 254 | 255 | /* Reset endpoint before we start reading from it (mandatory) */ 256 | verbose_reset_buffer(dev); 257 | 258 | if (sync_mode) { 259 | fprintf(stderr, "Reading samples in sync mode...\n"); 260 | while (!do_exit) { 261 | r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read); 262 | if (r < 0) { 263 | fprintf(stderr, "WARNING: sync read failed.\n"); 264 | break; 265 | } 266 | 267 | if ((bytes_to_read > 0) && (bytes_to_read < (uint32_t)n_read)) { 268 | n_read = bytes_to_read; 269 | do_exit = 1; 270 | } 271 | 272 | if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) { 273 | fprintf(stderr, "Short write, samples lost, exiting!\n"); 274 | break; 275 | } 276 | 277 | if ((uint32_t)n_read < out_block_size) { 278 | fprintf(stderr, "Short read, samples lost, exiting!\n"); 279 | break; 280 | } 281 | 282 | if (bytes_to_read > 0) 283 | bytes_to_read -= n_read; 284 | } 285 | } else { 286 | fprintf(stderr, "Reading samples in async mode...\n"); 287 | r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file, 288 | 0, out_block_size); 289 | } 290 | 291 | if (do_exit) 292 | fprintf(stderr, "\nUser cancel, exiting...\n"); 293 | else 294 | fprintf(stderr, "\nLibrary error %d, exiting...\n", r); 295 | 296 | if (file != stdout) 297 | fclose(file); 298 | 299 | rtlsdr_close(dev); 300 | free (buffer); 301 | out: 302 | return r >= 0 ? r : -r; 303 | } 304 | -------------------------------------------------------------------------------- /src/rtl_tcp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * Copyright (C) 2012 by Steve Markgraf 4 | * Copyright (C) 2012-2013 by Hoernchen 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 . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef _WIN32 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #else 36 | #include 37 | #include "getopt/getopt.h" 38 | #endif 39 | 40 | #include 41 | 42 | #include "rtl-sdr.h" 43 | #include "convenience/convenience.h" 44 | 45 | #ifdef _WIN32 46 | #pragma comment(lib, "ws2_32.lib") 47 | 48 | typedef int socklen_t; 49 | 50 | #else 51 | #define closesocket close 52 | #define SOCKADDR struct sockaddr 53 | #define SOCKET int 54 | #define SOCKET_ERROR -1 55 | #endif 56 | 57 | static SOCKET s; 58 | 59 | static pthread_t tcp_worker_thread; 60 | static pthread_t command_thread; 61 | static pthread_cond_t exit_cond; 62 | static pthread_mutex_t exit_cond_lock; 63 | 64 | static pthread_mutex_t ll_mutex; 65 | static pthread_cond_t cond; 66 | 67 | struct llist { 68 | char *data; 69 | size_t len; 70 | struct llist *next; 71 | }; 72 | 73 | typedef struct { /* structure size must be multiple of 2 bytes */ 74 | char magic[4]; 75 | uint32_t tuner_type; 76 | uint32_t tuner_gain_count; 77 | } dongle_info_t; 78 | 79 | static rtlsdr_dev_t *dev = NULL; 80 | 81 | static int global_numq = 0; 82 | static struct llist *ll_buffers = 0; 83 | static int llbuf_num = 500; 84 | 85 | static volatile int do_exit = 0; 86 | 87 | void usage(void) 88 | { 89 | printf("rtl_tcp, an I/Q spectrum server for RTL2832 based DVB-T receivers\n\n" 90 | "Usage:\t[-a listen address]\n" 91 | "\t[-p listen port (default: 1234)]\n" 92 | "\t[-f frequency to tune to [Hz]]\n" 93 | "\t[-g gain (default: 0 for auto)]\n" 94 | "\t[-s samplerate in Hz (default: 2048000 Hz)]\n" 95 | "\t[-b number of buffers (default: 15, set by library)]\n" 96 | "\t[-n max number of linked list buffers to keep (default: 500)]\n" 97 | "\t[-d device index (default: 0)]\n" 98 | "\t[-P ppm_error (default: 0)]\n"); 99 | exit(1); 100 | } 101 | 102 | #ifdef _WIN32 103 | int gettimeofday(struct timeval *tv, void* ignored) 104 | { 105 | FILETIME ft; 106 | unsigned __int64 tmp = 0; 107 | if (NULL != tv) { 108 | GetSystemTimeAsFileTime(&ft); 109 | tmp |= ft.dwHighDateTime; 110 | tmp <<= 32; 111 | tmp |= ft.dwLowDateTime; 112 | tmp /= 10; 113 | tmp -= 11644473600000000ULL; 114 | tv->tv_sec = (long)(tmp / 1000000UL); 115 | tv->tv_usec = (long)(tmp % 1000000UL); 116 | } 117 | return 0; 118 | } 119 | 120 | BOOL WINAPI 121 | sighandler(int signum) 122 | { 123 | if (CTRL_C_EVENT == signum) { 124 | fprintf(stderr, "Signal caught, exiting!\n"); 125 | do_exit = 1; 126 | rtlsdr_cancel_async(dev); 127 | return TRUE; 128 | } 129 | return FALSE; 130 | } 131 | #else 132 | static void sighandler(int signum) 133 | { 134 | fprintf(stderr, "Signal caught, exiting!\n"); 135 | rtlsdr_cancel_async(dev); 136 | do_exit = 1; 137 | } 138 | #endif 139 | 140 | void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) 141 | { 142 | if(!do_exit) { 143 | struct llist *rpt = (struct llist*)malloc(sizeof(struct llist)); 144 | rpt->data = (char*)malloc(len); 145 | memcpy(rpt->data, buf, len); 146 | rpt->len = len; 147 | rpt->next = NULL; 148 | 149 | pthread_mutex_lock(&ll_mutex); 150 | 151 | if (ll_buffers == NULL) { 152 | ll_buffers = rpt; 153 | } else { 154 | struct llist *cur = ll_buffers; 155 | int num_queued = 0; 156 | 157 | while (cur->next != NULL) { 158 | cur = cur->next; 159 | num_queued++; 160 | } 161 | 162 | if(llbuf_num && llbuf_num == num_queued-2){ 163 | struct llist *curelem; 164 | 165 | free(ll_buffers->data); 166 | curelem = ll_buffers->next; 167 | free(ll_buffers); 168 | ll_buffers = curelem; 169 | } 170 | 171 | cur->next = rpt; 172 | 173 | if (num_queued > global_numq) 174 | printf("ll+, now %d\n", num_queued); 175 | else if (num_queued < global_numq) 176 | printf("ll-, now %d\n", num_queued); 177 | 178 | global_numq = num_queued; 179 | } 180 | pthread_cond_signal(&cond); 181 | pthread_mutex_unlock(&ll_mutex); 182 | } 183 | } 184 | 185 | static void *tcp_worker(void *arg) 186 | { 187 | struct llist *curelem,*prev; 188 | int bytesleft,bytessent, index; 189 | struct timeval tv= {1,0}; 190 | struct timespec ts; 191 | struct timeval tp; 192 | fd_set writefds; 193 | int r = 0; 194 | 195 | while(1) { 196 | if(do_exit) 197 | pthread_exit(0); 198 | 199 | pthread_mutex_lock(&ll_mutex); 200 | gettimeofday(&tp, NULL); 201 | ts.tv_sec = tp.tv_sec+5; 202 | ts.tv_nsec = tp.tv_usec * 1000; 203 | r = pthread_cond_timedwait(&cond, &ll_mutex, &ts); 204 | if(r == ETIMEDOUT) { 205 | pthread_mutex_unlock(&ll_mutex); 206 | printf("worker cond timeout\n"); 207 | sighandler(0); 208 | pthread_exit(NULL); 209 | } 210 | 211 | curelem = ll_buffers; 212 | ll_buffers = 0; 213 | pthread_mutex_unlock(&ll_mutex); 214 | 215 | while(curelem != 0) { 216 | bytesleft = curelem->len; 217 | index = 0; 218 | bytessent = 0; 219 | while(bytesleft > 0) { 220 | FD_ZERO(&writefds); 221 | FD_SET(s, &writefds); 222 | tv.tv_sec = 1; 223 | tv.tv_usec = 0; 224 | r = select(s+1, NULL, &writefds, NULL, &tv); 225 | if(r) { 226 | bytessent = send(s, &curelem->data[index], bytesleft, 0); 227 | bytesleft -= bytessent; 228 | index += bytessent; 229 | } 230 | if(bytessent == SOCKET_ERROR || do_exit) { 231 | printf("worker socket bye\n"); 232 | sighandler(0); 233 | pthread_exit(NULL); 234 | } 235 | } 236 | prev = curelem; 237 | curelem = curelem->next; 238 | free(prev->data); 239 | free(prev); 240 | } 241 | } 242 | } 243 | 244 | static int set_gain_by_index(rtlsdr_dev_t *_dev, unsigned int index) 245 | { 246 | int res = 0; 247 | int* gains; 248 | int count = rtlsdr_get_tuner_gains(_dev, NULL); 249 | 250 | if (count > 0 && (unsigned int)count > index) { 251 | gains = malloc(sizeof(int) * count); 252 | count = rtlsdr_get_tuner_gains(_dev, gains); 253 | 254 | res = rtlsdr_set_tuner_gain(_dev, gains[index]); 255 | 256 | free(gains); 257 | } 258 | 259 | return res; 260 | } 261 | 262 | #ifdef _WIN32 263 | #define __attribute__(x) 264 | #pragma pack(push, 1) 265 | #endif 266 | struct command{ 267 | unsigned char cmd; 268 | unsigned int param; 269 | }__attribute__((packed)); 270 | #ifdef _WIN32 271 | #pragma pack(pop) 272 | #endif 273 | static void *command_worker(void *arg) 274 | { 275 | int left, received = 0; 276 | fd_set readfds; 277 | struct command cmd={0, 0}; 278 | struct timeval tv= {1, 0}; 279 | int r = 0; 280 | uint32_t tmp; 281 | 282 | while(1) { 283 | left=sizeof(cmd); 284 | while(left >0) { 285 | FD_ZERO(&readfds); 286 | FD_SET(s, &readfds); 287 | tv.tv_sec = 1; 288 | tv.tv_usec = 0; 289 | r = select(s+1, &readfds, NULL, NULL, &tv); 290 | if(r) { 291 | received = recv(s, (char*)&cmd+(sizeof(cmd)-left), left, 0); 292 | left -= received; 293 | } 294 | if(received == SOCKET_ERROR || do_exit) { 295 | printf("comm recv bye\n"); 296 | sighandler(0); 297 | pthread_exit(NULL); 298 | } 299 | } 300 | switch(cmd.cmd) { 301 | case 0x01: 302 | printf("set freq %u\n", ntohl(cmd.param)); 303 | rtlsdr_set_center_freq(dev,ntohl(cmd.param)); 304 | break; 305 | case 0x02: 306 | printf("set sample rate %u\n", ntohl(cmd.param)); 307 | rtlsdr_set_sample_rate(dev, ntohl(cmd.param)); 308 | break; 309 | case 0x03: 310 | printf("set gain mode %u\n", ntohl(cmd.param)); 311 | rtlsdr_set_tuner_gain_mode(dev, ntohl(cmd.param)); 312 | break; 313 | case 0x04: 314 | printf("set gain %u\n", ntohl(cmd.param)); 315 | rtlsdr_set_tuner_gain(dev, ntohl(cmd.param)); 316 | break; 317 | case 0x05: 318 | printf("set freq correction %u\n", ntohl(cmd.param)); 319 | rtlsdr_set_freq_correction(dev, ntohl(cmd.param)); 320 | break; 321 | case 0x06: 322 | tmp = ntohl(cmd.param); 323 | printf("set if stage %d gain %d\n", tmp >> 16, (short)(tmp & 0xffff)); 324 | rtlsdr_set_tuner_if_gain(dev, tmp >> 16, (short)(tmp & 0xffff)); 325 | break; 326 | case 0x07: 327 | printf("set test mode %u\n", ntohl(cmd.param)); 328 | rtlsdr_set_testmode(dev, ntohl(cmd.param)); 329 | break; 330 | case 0x08: 331 | printf("set agc mode %u\n", ntohl(cmd.param)); 332 | rtlsdr_set_agc_mode(dev, ntohl(cmd.param)); 333 | break; 334 | case 0x09: 335 | printf("set direct sampling %u\n", ntohl(cmd.param)); 336 | rtlsdr_set_direct_sampling(dev, ntohl(cmd.param)); 337 | break; 338 | case 0x0a: 339 | printf("set offset tuning %u\n", ntohl(cmd.param)); 340 | rtlsdr_set_offset_tuning(dev, ntohl(cmd.param)); 341 | break; 342 | case 0x0b: 343 | printf("set rtl xtal %u\n", ntohl(cmd.param)); 344 | rtlsdr_set_xtal_freq(dev, ntohl(cmd.param), 0); 345 | break; 346 | case 0x0c: 347 | printf("set tuner xtal %u\n", ntohl(cmd.param)); 348 | rtlsdr_set_xtal_freq(dev, 0, ntohl(cmd.param)); 349 | break; 350 | case 0x0d: 351 | printf("set tuner gain by index %u\n", ntohl(cmd.param)); 352 | set_gain_by_index(dev, ntohl(cmd.param)); 353 | break; 354 | default: 355 | break; 356 | } 357 | cmd.cmd = 0xff; 358 | } 359 | } 360 | 361 | int main(int argc, char **argv) 362 | { 363 | int r, opt, i; 364 | char* addr = "127.0.0.1"; 365 | int port = 1234; 366 | uint32_t frequency = 100000000, samp_rate = 2048000; 367 | struct sockaddr_in local, remote; 368 | uint32_t buf_num = 0; 369 | int dev_index = 0; 370 | int dev_given = 0; 371 | int gain = 0; 372 | int ppm_error = 0; 373 | int custom_ppm = 0; 374 | struct llist *curelem,*prev; 375 | pthread_attr_t attr; 376 | void *status; 377 | struct timeval tv = {1,0}; 378 | struct linger ling = {1,0}; 379 | SOCKET listensocket; 380 | socklen_t rlen; 381 | fd_set readfds; 382 | u_long blockmode = 1; 383 | dongle_info_t dongle_info; 384 | #ifdef _WIN32 385 | WSADATA wsd; 386 | i = WSAStartup(MAKEWORD(2,2), &wsd); 387 | #else 388 | struct sigaction sigact, sigign; 389 | #endif 390 | 391 | while ((opt = getopt(argc, argv, "a:p:f:g:s:b:n:d:P:")) != -1) { 392 | switch (opt) { 393 | case 'd': 394 | dev_index = verbose_device_search(optarg); 395 | dev_given = 1; 396 | break; 397 | case 'f': 398 | frequency = (uint32_t)atofs(optarg); 399 | break; 400 | case 'g': 401 | gain = (int)(atof(optarg) * 10); /* tenths of a dB */ 402 | break; 403 | case 's': 404 | samp_rate = (uint32_t)atofs(optarg); 405 | break; 406 | case 'a': 407 | addr = optarg; 408 | break; 409 | case 'p': 410 | port = atoi(optarg); 411 | break; 412 | case 'b': 413 | buf_num = atoi(optarg); 414 | break; 415 | case 'n': 416 | llbuf_num = atoi(optarg); 417 | break; 418 | case 'P': 419 | ppm_error = atoi(optarg); 420 | custom_ppm = 1; 421 | break; 422 | default: 423 | usage(); 424 | break; 425 | } 426 | } 427 | 428 | if (argc < optind) 429 | usage(); 430 | 431 | if (!dev_given) { 432 | dev_index = verbose_device_search("0"); 433 | } 434 | 435 | if (dev_index < 0) { 436 | exit(1); 437 | } 438 | 439 | rtlsdr_open(&dev, (uint32_t)dev_index); 440 | if (NULL == dev) { 441 | fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); 442 | exit(1); 443 | } 444 | 445 | #ifndef _WIN32 446 | sigact.sa_handler = sighandler; 447 | sigemptyset(&sigact.sa_mask); 448 | sigact.sa_flags = 0; 449 | sigign.sa_handler = SIG_IGN; 450 | sigaction(SIGINT, &sigact, NULL); 451 | sigaction(SIGTERM, &sigact, NULL); 452 | sigaction(SIGQUIT, &sigact, NULL); 453 | sigaction(SIGPIPE, &sigign, NULL); 454 | #else 455 | SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); 456 | #endif 457 | 458 | /* Set the tuner error */ 459 | if (!custom_ppm) { 460 | verbose_ppm_eeprom(dev, &ppm_error); 461 | } 462 | verbose_ppm_set(dev, ppm_error); 463 | 464 | /* Set the sample rate */ 465 | r = rtlsdr_set_sample_rate(dev, samp_rate); 466 | if (r < 0) 467 | fprintf(stderr, "WARNING: Failed to set sample rate.\n"); 468 | 469 | /* Set the frequency */ 470 | r = rtlsdr_set_center_freq(dev, frequency); 471 | if (r < 0) 472 | fprintf(stderr, "WARNING: Failed to set center freq.\n"); 473 | else 474 | fprintf(stderr, "Tuned to %i Hz.\n", frequency); 475 | 476 | if (0 == gain) { 477 | /* Enable automatic gain */ 478 | r = rtlsdr_set_tuner_gain_mode(dev, 0); 479 | if (r < 0) 480 | fprintf(stderr, "WARNING: Failed to enable automatic gain.\n"); 481 | } else { 482 | /* Enable manual gain */ 483 | r = rtlsdr_set_tuner_gain_mode(dev, 1); 484 | if (r < 0) 485 | fprintf(stderr, "WARNING: Failed to enable manual gain.\n"); 486 | 487 | /* Set the tuner gain */ 488 | r = rtlsdr_set_tuner_gain(dev, gain); 489 | if (r < 0) 490 | fprintf(stderr, "WARNING: Failed to set tuner gain.\n"); 491 | else 492 | fprintf(stderr, "Tuner gain set to %f dB.\n", gain/10.0); 493 | } 494 | 495 | /* Reset endpoint before we start reading from it (mandatory) */ 496 | r = rtlsdr_reset_buffer(dev); 497 | if (r < 0) 498 | fprintf(stderr, "WARNING: Failed to reset buffers.\n"); 499 | 500 | pthread_mutex_init(&exit_cond_lock, NULL); 501 | pthread_mutex_init(&ll_mutex, NULL); 502 | pthread_mutex_init(&exit_cond_lock, NULL); 503 | pthread_cond_init(&cond, NULL); 504 | pthread_cond_init(&exit_cond, NULL); 505 | 506 | memset(&local,0,sizeof(local)); 507 | local.sin_family = AF_INET; 508 | local.sin_port = htons(port); 509 | local.sin_addr.s_addr = inet_addr(addr); 510 | 511 | listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 512 | r = 1; 513 | setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, (char *)&r, sizeof(int)); 514 | setsockopt(listensocket, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling)); 515 | bind(listensocket,(struct sockaddr *)&local,sizeof(local)); 516 | 517 | #ifdef _WIN32 518 | ioctlsocket(listensocket, FIONBIO, &blockmode); 519 | #else 520 | r = fcntl(listensocket, F_GETFL, 0); 521 | r = fcntl(listensocket, F_SETFL, r | O_NONBLOCK); 522 | #endif 523 | 524 | while(1) { 525 | printf("listening...\n"); 526 | printf("Use the device argument 'rtl_tcp=%s:%d' in OsmoSDR " 527 | "(gr-osmosdr) source\n" 528 | "to receive samples in GRC and control " 529 | "rtl_tcp parameters (frequency, gain, ...).\n", 530 | addr, port); 531 | listen(listensocket,1); 532 | 533 | while(1) { 534 | FD_ZERO(&readfds); 535 | FD_SET(listensocket, &readfds); 536 | tv.tv_sec = 1; 537 | tv.tv_usec = 0; 538 | r = select(listensocket+1, &readfds, NULL, NULL, &tv); 539 | if(do_exit) { 540 | goto out; 541 | } else if(r) { 542 | rlen = sizeof(remote); 543 | s = accept(listensocket,(struct sockaddr *)&remote, &rlen); 544 | break; 545 | } 546 | } 547 | 548 | setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling)); 549 | 550 | printf("client accepted!\n"); 551 | 552 | memset(&dongle_info, 0, sizeof(dongle_info)); 553 | memcpy(&dongle_info.magic, "RTL0", 4); 554 | 555 | r = rtlsdr_get_tuner_type(dev); 556 | if (r >= 0) 557 | dongle_info.tuner_type = htonl(r); 558 | 559 | r = rtlsdr_get_tuner_gains(dev, NULL); 560 | if (r >= 0) 561 | dongle_info.tuner_gain_count = htonl(r); 562 | 563 | r = send(s, (const char *)&dongle_info, sizeof(dongle_info), 0); 564 | if (sizeof(dongle_info) != r) 565 | printf("failed to send dongle information\n"); 566 | 567 | pthread_attr_init(&attr); 568 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 569 | r = pthread_create(&tcp_worker_thread, &attr, tcp_worker, NULL); 570 | r = pthread_create(&command_thread, &attr, command_worker, NULL); 571 | pthread_attr_destroy(&attr); 572 | 573 | r = rtlsdr_read_async(dev, rtlsdr_callback, NULL, buf_num, 0); 574 | 575 | pthread_join(tcp_worker_thread, &status); 576 | pthread_join(command_thread, &status); 577 | 578 | closesocket(s); 579 | 580 | printf("all threads dead..\n"); 581 | curelem = ll_buffers; 582 | ll_buffers = 0; 583 | 584 | while(curelem != 0) { 585 | prev = curelem; 586 | curelem = curelem->next; 587 | free(prev->data); 588 | free(prev); 589 | } 590 | 591 | do_exit = 0; 592 | global_numq = 0; 593 | } 594 | 595 | out: 596 | rtlsdr_close(dev); 597 | closesocket(listensocket); 598 | closesocket(s); 599 | #ifdef _WIN32 600 | WSACleanup(); 601 | #endif 602 | printf("bye!\n"); 603 | return r >= 0 ? r : -r; 604 | } 605 | -------------------------------------------------------------------------------- /src/rtl_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver 3 | * rtl_test, test and benchmark tool 4 | * 5 | * Copyright (C) 2012-2014 by Steve Markgraf 6 | * Copyright (C) 2012-2014 by Kyle Keen 7 | * Copyright (C) 2014 by Michael Tatarinov 8 | * 9 | * This program is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef __APPLE__ 31 | #include 32 | #else 33 | #include 34 | #endif 35 | 36 | #ifndef _WIN32 37 | #include 38 | #else 39 | #include 40 | #include "getopt/getopt.h" 41 | #endif 42 | 43 | #include "rtl-sdr.h" 44 | #include "convenience/convenience.h" 45 | 46 | #define DEFAULT_SAMPLE_RATE 2048000 47 | #define DEFAULT_BUF_LENGTH (16 * 16384) 48 | #define MINIMAL_BUF_LENGTH 512 49 | #define MAXIMAL_BUF_LENGTH (256 * 16384) 50 | 51 | #define MHZ(x) ((x)*1000*1000) 52 | 53 | #define PPM_DURATION 10 54 | #define PPM_DUMP_TIME 5 55 | 56 | #define SCAN_LIMIT 2500000000 57 | 58 | struct time_generic 59 | /* holds all the platform specific values */ 60 | { 61 | #ifndef _WIN32 62 | time_t tv_sec; 63 | long tv_nsec; 64 | #else 65 | long tv_sec; 66 | long tv_nsec; 67 | int init; 68 | LARGE_INTEGER frequency; 69 | LARGE_INTEGER ticks; 70 | #endif 71 | }; 72 | 73 | static enum { 74 | NO_BENCHMARK, 75 | TUNER_BENCHMARK, 76 | PPM_BENCHMARK 77 | } test_mode = NO_BENCHMARK; 78 | 79 | static int do_exit = 0; 80 | static rtlsdr_dev_t *dev = NULL; 81 | 82 | static uint32_t samp_rate = DEFAULT_SAMPLE_RATE; 83 | 84 | static uint32_t total_samples = 0; 85 | static uint32_t dropped_samples = 0; 86 | 87 | static unsigned int ppm_duration = PPM_DURATION; 88 | 89 | void usage(void) 90 | { 91 | fprintf(stderr, 92 | "rtl_test, a benchmark tool for RTL2832 based DVB-T receivers\n\n" 93 | "Usage:\n" 94 | "\t[-s samplerate (default: 2048000 Hz)]\n" 95 | "\t[-d device_index (default: 0)]\n" 96 | "\t[-t enable tuner range benchmark]\n" 97 | "\t[-p[seconds] enable PPM error measurement (default: 10 seconds)]\n" 98 | "\t[-b output_block_size (default: 16 * 16384)]\n" 99 | "\t[-S force sync output (default: async)]\n"); 100 | exit(1); 101 | } 102 | 103 | #ifdef _WIN32 104 | BOOL WINAPI 105 | sighandler(int signum) 106 | { 107 | if (CTRL_C_EVENT == signum) { 108 | fprintf(stderr, "Signal caught, exiting!\n"); 109 | do_exit = 1; 110 | rtlsdr_cancel_async(dev); 111 | return TRUE; 112 | } 113 | return FALSE; 114 | } 115 | #else 116 | static void sighandler(int signum) 117 | { 118 | fprintf(stderr, "Signal caught, exiting!\n"); 119 | do_exit = 1; 120 | rtlsdr_cancel_async(dev); 121 | } 122 | #endif 123 | 124 | static void underrun_test(unsigned char *buf, uint32_t len, int mute) 125 | { 126 | uint32_t i, lost = 0; 127 | static uint8_t bcnt, uninit = 1; 128 | 129 | if (uninit) { 130 | bcnt = buf[0]; 131 | uninit = 0; 132 | } 133 | for (i = 0; i < len; i++) { 134 | if(bcnt != buf[i]) { 135 | lost += (buf[i] > bcnt) ? (buf[i] - bcnt) : (bcnt - buf[i]); 136 | bcnt = buf[i]; 137 | } 138 | 139 | bcnt++; 140 | } 141 | 142 | total_samples += len; 143 | dropped_samples += lost; 144 | if (mute) 145 | return; 146 | if (lost) 147 | printf("lost at least %d bytes\n", lost); 148 | 149 | } 150 | 151 | #ifndef _WIN32 152 | static int ppm_gettime(struct time_generic *tg) 153 | { 154 | int rv = ENOSYS; 155 | struct timespec ts; 156 | 157 | #ifdef __unix__ 158 | rv = clock_gettime(CLOCK_MONOTONIC, &ts); 159 | tg->tv_sec = ts.tv_sec; 160 | tg->tv_nsec = ts.tv_nsec; 161 | #elif __APPLE__ 162 | struct timeval tv; 163 | 164 | rv = gettimeofday(&tv, NULL); 165 | ts->tv_sec = tv.tv_sec; 166 | ts->tv_nsec = tv.tv_usec * 1000; 167 | #endif 168 | return rv; 169 | } 170 | #endif 171 | 172 | #ifdef _WIN32 173 | static int ppm_gettime(struct time_generic *tg) 174 | { 175 | int rv; 176 | int64_t frac; 177 | if (!tg->init) { 178 | QueryPerformanceFrequency(&tg->frequency); 179 | tg->init = 1; 180 | } 181 | rv = QueryPerformanceCounter(&tg->ticks); 182 | tg->tv_sec = tg->ticks.QuadPart / tg->frequency.QuadPart; 183 | frac = (int64_t)(tg->ticks.QuadPart - (tg->tv_sec * tg->frequency.QuadPart)); 184 | tg->tv_nsec = (long)(frac * 1000000000L / (int64_t)tg->frequency.QuadPart); 185 | return !rv; 186 | } 187 | #endif 188 | 189 | static int ppm_report(uint64_t nsamples, uint64_t interval) 190 | { 191 | double real_rate, ppm; 192 | 193 | real_rate = nsamples * 1e9 / interval; 194 | ppm = 1e6 * (real_rate / (double)samp_rate - 1.); 195 | return (int)round(ppm); 196 | } 197 | 198 | static void ppm_test(uint32_t len) 199 | { 200 | static uint64_t nsamples = 0; 201 | static uint64_t interval = 0; 202 | static uint64_t nsamples_total = 0; 203 | static uint64_t interval_total = 0; 204 | static struct time_generic ppm_now; 205 | static struct time_generic ppm_recent; 206 | 207 | static enum { 208 | PPM_INIT_NO, 209 | PPM_INIT_DUMP, 210 | PPM_INIT_RUN 211 | } ppm_init = PPM_INIT_NO; 212 | 213 | ppm_gettime(&ppm_now); 214 | 215 | if (ppm_init != PPM_INIT_RUN) { 216 | /* 217 | * Kyle Keen wrote: 218 | * PPM_DUMP_TIME throws out the first N seconds of data. 219 | * The dongle's PPM is usually very bad when first starting up, 220 | * typically incorrect by more than twice the final value. 221 | * Discarding the first few seconds allows the value to stabilize much faster. 222 | */ 223 | if (ppm_init == PPM_INIT_NO) { 224 | ppm_recent.tv_sec = ppm_now.tv_sec + PPM_DUMP_TIME; 225 | ppm_init = PPM_INIT_DUMP; 226 | return; 227 | } 228 | if (ppm_init == PPM_INIT_DUMP && ppm_recent.tv_sec < ppm_now.tv_sec) 229 | return; 230 | ppm_recent = ppm_now; 231 | ppm_init = PPM_INIT_RUN; 232 | return; 233 | } 234 | nsamples += (uint64_t)(len / 2UL); 235 | interval = (uint64_t)(ppm_now.tv_sec - ppm_recent.tv_sec); 236 | if (interval < ppm_duration) 237 | return; 238 | interval *= 1000000000UL; 239 | interval += (int64_t)(ppm_now.tv_nsec - ppm_recent.tv_nsec); 240 | 241 | nsamples_total += nsamples; 242 | interval_total += interval; 243 | printf("real sample rate: %i current PPM: %i cumulative PPM: %i\n", 244 | (int)((1000000000UL * nsamples) / interval), 245 | ppm_report(nsamples, interval), 246 | ppm_report(nsamples_total, interval_total)); 247 | ppm_recent = ppm_now; 248 | nsamples = 0; 249 | } 250 | 251 | static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) 252 | { 253 | underrun_test(buf, len, 0); 254 | if (test_mode == PPM_BENCHMARK) 255 | ppm_test(len); 256 | } 257 | 258 | /* smallest band or band gap that tuner_benchmark() will notice */ 259 | static uint32_t max_step(uint32_t freq) { 260 | if (freq < 1e6) 261 | return 1e4; 262 | if (freq > 1e8) 263 | return 1e6; 264 | return freq / 1e2; 265 | } 266 | 267 | /* precision with which tuner_benchmark() will measure the edges of bands */ 268 | static uint32_t min_step(uint32_t freq) { 269 | return 100; 270 | } 271 | 272 | int confirm_pll_lock(uint32_t f) 273 | { 274 | int i; 275 | for (i=0; i<20; i++) { 276 | if (rtlsdr_set_center_freq(dev, f) >= 0) 277 | return 1; 278 | } 279 | return 0; 280 | } 281 | 282 | /* returns last frequency before achieving status of 'lock' */ 283 | uint32_t coarse_search(uint32_t start, int lock) 284 | { 285 | uint32_t f = start, f2; 286 | int status; 287 | while (f < SCAN_LIMIT) { 288 | if (do_exit) 289 | break; 290 | f2 = f + max_step(f); 291 | status = rtlsdr_set_center_freq(dev, f2) >= 0; 292 | if (!lock && !status) 293 | status = confirm_pll_lock(f2); 294 | if (status == lock) 295 | return f; 296 | f = f2; 297 | } 298 | return SCAN_LIMIT + 1; 299 | } 300 | 301 | /* returns frequency of a transition 302 | * must have one transition between start and start+step */ 303 | uint32_t fine_search(uint32_t start, uint32_t step) 304 | { 305 | int low_status, mid_status, high_status; 306 | uint32_t f, stop; 307 | stop = start + step; 308 | f = start + step / 2; 309 | low_status = rtlsdr_set_center_freq(dev, start) >= 0; 310 | high_status = rtlsdr_set_center_freq(dev, stop) >= 0; 311 | if (low_status == high_status) 312 | return start; 313 | while (step > min_step(start)) { 314 | if (do_exit) 315 | break; 316 | mid_status = rtlsdr_set_center_freq(dev, f) >= 0; 317 | if (low_status == mid_status) 318 | start = f; 319 | else 320 | stop = f; 321 | step = stop - start; 322 | f = start + step / 2; 323 | } 324 | return f; 325 | } 326 | 327 | void tuner_benchmark(void) 328 | { 329 | uint32_t f = 0, low_bound, high_bound; 330 | fprintf(stderr, "Testing tuner range. This may take a couple of minutes...\n"); 331 | while (!do_exit) { 332 | /* find start of a band */ 333 | f = coarse_search(f, 1); 334 | low_bound = fine_search(f, max_step(f)); 335 | f += max_step(f); 336 | /* find stop of a band */ 337 | f = coarse_search(f, 0); 338 | high_bound = fine_search(f, max_step(f)); 339 | f += max_step(f); 340 | if (f > SCAN_LIMIT) 341 | break; 342 | fprintf(stderr, "Band: %u - %u Hz\n", low_bound, high_bound); 343 | } 344 | } 345 | 346 | int main(int argc, char **argv) 347 | { 348 | #ifndef _WIN32 349 | struct sigaction sigact; 350 | #endif 351 | int n_read, r, opt, i; 352 | int sync_mode = 0; 353 | uint8_t *buffer; 354 | int dev_index = 0; 355 | int dev_given = 0; 356 | uint32_t out_block_size = DEFAULT_BUF_LENGTH; 357 | int count; 358 | int gains[100]; 359 | 360 | while ((opt = getopt(argc, argv, "d:s:b:tp::Sh")) != -1) { 361 | switch (opt) { 362 | case 'd': 363 | dev_index = verbose_device_search(optarg); 364 | dev_given = 1; 365 | break; 366 | case 's': 367 | samp_rate = (uint32_t)atof(optarg); 368 | break; 369 | case 'b': 370 | out_block_size = (uint32_t)atof(optarg); 371 | break; 372 | case 't': 373 | test_mode = TUNER_BENCHMARK; 374 | break; 375 | case 'p': 376 | test_mode = PPM_BENCHMARK; 377 | if (optarg) 378 | ppm_duration = atoi(optarg); 379 | break; 380 | case 'S': 381 | sync_mode = 1; 382 | break; 383 | case 'h': 384 | default: 385 | usage(); 386 | break; 387 | } 388 | } 389 | 390 | if(out_block_size < MINIMAL_BUF_LENGTH || 391 | out_block_size > MAXIMAL_BUF_LENGTH ){ 392 | fprintf(stderr, 393 | "Output block size wrong value, falling back to default\n"); 394 | fprintf(stderr, 395 | "Minimal length: %u\n", MINIMAL_BUF_LENGTH); 396 | fprintf(stderr, 397 | "Maximal length: %u\n", MAXIMAL_BUF_LENGTH); 398 | out_block_size = DEFAULT_BUF_LENGTH; 399 | } 400 | 401 | buffer = malloc(out_block_size * sizeof(uint8_t)); 402 | 403 | if (!dev_given) { 404 | dev_index = verbose_device_search("0"); 405 | } 406 | 407 | if (dev_index < 0) { 408 | exit(1); 409 | } 410 | 411 | r = rtlsdr_open(&dev, (uint32_t)dev_index); 412 | if (r < 0) { 413 | fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); 414 | exit(1); 415 | } 416 | #ifndef _WIN32 417 | sigact.sa_handler = sighandler; 418 | sigemptyset(&sigact.sa_mask); 419 | sigact.sa_flags = 0; 420 | sigaction(SIGINT, &sigact, NULL); 421 | sigaction(SIGTERM, &sigact, NULL); 422 | sigaction(SIGQUIT, &sigact, NULL); 423 | sigaction(SIGPIPE, &sigact, NULL); 424 | #else 425 | SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); 426 | #endif 427 | count = rtlsdr_get_tuner_gains(dev, NULL); 428 | fprintf(stderr, "Supported gain values (%d): ", count); 429 | 430 | count = rtlsdr_get_tuner_gains(dev, gains); 431 | for (i = 0; i < count; i++) 432 | fprintf(stderr, "%.1f ", gains[i] / 10.0); 433 | fprintf(stderr, "\n"); 434 | 435 | /* Set the sample rate */ 436 | verbose_set_sample_rate(dev, samp_rate); 437 | 438 | if (test_mode == TUNER_BENCHMARK) { 439 | tuner_benchmark(); 440 | goto exit; 441 | } 442 | 443 | /* Enable test mode */ 444 | r = rtlsdr_set_testmode(dev, 1); 445 | 446 | /* Reset endpoint before we start reading from it (mandatory) */ 447 | verbose_reset_buffer(dev); 448 | 449 | if ((test_mode == PPM_BENCHMARK) && !sync_mode) { 450 | fprintf(stderr, "Reporting PPM error measurement every %u seconds...\n", ppm_duration); 451 | fprintf(stderr, "Press ^C after a few minutes.\n"); 452 | } 453 | 454 | if (test_mode == NO_BENCHMARK) { 455 | fprintf(stderr, "\nInfo: This tool will continuously" 456 | " read from the device, and report if\n" 457 | "samples get lost. If you observe no " 458 | "further output, everything is fine.\n\n"); 459 | } 460 | 461 | if (sync_mode) { 462 | fprintf(stderr, "Reading samples in sync mode...\n"); 463 | fprintf(stderr, "(Samples are being lost but not reported.)\n"); 464 | while (!do_exit) { 465 | r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read); 466 | if (r < 0) { 467 | fprintf(stderr, "WARNING: sync read failed.\n"); 468 | break; 469 | } 470 | 471 | if ((uint32_t)n_read < out_block_size) { 472 | fprintf(stderr, "Short read, samples lost, exiting!\n"); 473 | break; 474 | } 475 | underrun_test(buffer, n_read, 1); 476 | } 477 | } else { 478 | fprintf(stderr, "Reading samples in async mode...\n"); 479 | r = rtlsdr_read_async(dev, rtlsdr_callback, NULL, 480 | 0, out_block_size); 481 | } 482 | 483 | if (do_exit) { 484 | fprintf(stderr, "\nUser cancel, exiting...\n"); 485 | fprintf(stderr, "Samples per million lost (minimum): %i\n", (int)(1000000L * dropped_samples / total_samples)); 486 | } 487 | else 488 | fprintf(stderr, "\nLibrary error %d, exiting...\n", r); 489 | 490 | exit: 491 | rtlsdr_close(dev); 492 | free (buffer); 493 | 494 | return r >= 0 ? r : -r; 495 | } 496 | -------------------------------------------------------------------------------- /src/tuner_fc0012.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Fitipower FC0012 tuner driver 3 | * 4 | * Copyright (C) 2012 Hans-Frieder Vogt 5 | * 6 | * modified for use in librtlsdr 7 | * Copyright (C) 2012 Steve Markgraf 8 | * 9 | * This program is free software; you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation; either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | #include "rtlsdr_i2c.h" 28 | #include "tuner_fc0012.h" 29 | 30 | static int fc0012_writereg(void *dev, uint8_t reg, uint8_t val) 31 | { 32 | uint8_t data[2]; 33 | data[0] = reg; 34 | data[1] = val; 35 | 36 | if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, data, 2) < 0) 37 | return -1; 38 | 39 | return 0; 40 | } 41 | 42 | static int fc0012_readreg(void *dev, uint8_t reg, uint8_t *val) 43 | { 44 | uint8_t data = reg; 45 | 46 | if (rtlsdr_i2c_write_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0) 47 | return -1; 48 | 49 | if (rtlsdr_i2c_read_fn(dev, FC0012_I2C_ADDR, &data, 1) < 0) 50 | return -1; 51 | 52 | *val = data; 53 | 54 | return 0; 55 | } 56 | 57 | /* Incomplete list of register settings: 58 | * 59 | * Name Reg Bits Desc 60 | * CHIP_ID 0x00 0-7 Chip ID (constant 0xA1) 61 | * RF_A 0x01 0-3 Number of count-to-9 cycles in RF 62 | * divider (suggested: 2..9) 63 | * RF_M 0x02 0-7 Total number of cycles (to-8 and to-9) 64 | * in RF divider 65 | * RF_K_HIGH 0x03 0-6 Bits 8..14 of fractional divider 66 | * RF_K_LOW 0x04 0-7 Bits 0..7 of fractional RF divider 67 | * RF_OUTDIV_A 0x05 3-7 Power of two required? 68 | * LNA_POWER_DOWN 0x06 0 Set to 1 to switch off low noise amp 69 | * RF_OUTDIV_B 0x06 1 Set to select 3 instead of 2 for the 70 | * RF output divider 71 | * VCO_SPEED 0x06 3 Select tuning range of VCO: 72 | * 0 = Low range, (ca. 1.1 - 1.5GHz) 73 | * 1 = High range (ca. 1.4 - 1.8GHz) 74 | * BANDWIDTH 0x06 6-7 Set bandwidth. 6MHz = 0x80, 7MHz=0x40 75 | * 8MHz=0x00 76 | * XTAL_SPEED 0x07 5 Set to 1 for 28.8MHz Crystal input 77 | * or 0 for 36MHz 78 | * 0x08 0-7 79 | * EN_CAL_RSSI 0x09 4 Enable calibrate RSSI 80 | * (Receive Signal Strength Indicator) 81 | * LNA_FORCE 0x0d 0 82 | * AGC_FORCE 0x0d ? 83 | * LNA_GAIN 0x13 3-4 Low noise amp gain 84 | * LNA_COMPS 0x15 3 ? 85 | * VCO_CALIB 0x0e 7 Set high then low to calibrate VCO 86 | * (fast lock?) 87 | * VCO_VOLTAGE 0x0e 0-6 Read Control voltage of VCO 88 | * (big value -> low freq) 89 | */ 90 | 91 | int fc0012_init(void *dev) 92 | { 93 | int ret = 0; 94 | unsigned int i; 95 | uint8_t reg[] = { 96 | 0x00, /* dummy reg. 0 */ 97 | 0x05, /* reg. 0x01 */ 98 | 0x10, /* reg. 0x02 */ 99 | 0x00, /* reg. 0x03 */ 100 | 0x00, /* reg. 0x04 */ 101 | 0x0f, /* reg. 0x05: may also be 0x0a */ 102 | 0x00, /* reg. 0x06: divider 2, VCO slow */ 103 | 0x00, /* reg. 0x07: may also be 0x0f */ 104 | 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256, 105 | Loop Bw 1/8 */ 106 | 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */ 107 | 0xb8, /* reg. 0x0a: Disable LO Test Buffer */ 108 | 0x82, /* reg. 0x0b: Output Clock is same as clock frequency, 109 | may also be 0x83 */ 110 | 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */ 111 | 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */ 112 | 0x00, /* reg. 0x0e */ 113 | 0x00, /* reg. 0x0f */ 114 | 0x00, /* reg. 0x10: may also be 0x0d */ 115 | 0x00, /* reg. 0x11 */ 116 | 0x1f, /* reg. 0x12: Set to maximum gain */ 117 | 0x08, /* reg. 0x13: Set to Middle Gain: 0x08, 118 | Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */ 119 | 0x00, /* reg. 0x14 */ 120 | 0x04, /* reg. 0x15: Enable LNA COMPS */ 121 | }; 122 | 123 | #if 0 124 | switch (rtlsdr_get_tuner_clock(dev)) { 125 | case FC_XTAL_27_MHZ: 126 | case FC_XTAL_28_8_MHZ: 127 | reg[0x07] |= 0x20; 128 | break; 129 | case FC_XTAL_36_MHZ: 130 | default: 131 | break; 132 | } 133 | #endif 134 | reg[0x07] |= 0x20; 135 | 136 | // if (priv->dual_master) 137 | reg[0x0c] |= 0x02; 138 | 139 | for (i = 1; i < sizeof(reg); i++) { 140 | ret = fc0012_writereg(dev, i, reg[i]); 141 | if (ret) 142 | break; 143 | } 144 | 145 | return ret; 146 | } 147 | 148 | int fc0012_set_params(void *dev, uint32_t freq, uint32_t bandwidth) 149 | { 150 | int i, ret = 0; 151 | uint8_t reg[7], am, pm, multi, tmp; 152 | uint64_t f_vco; 153 | uint32_t xtal_freq_div_2; 154 | uint16_t xin, xdiv; 155 | int vco_select = 0; 156 | 157 | xtal_freq_div_2 = rtlsdr_get_tuner_clock(dev) / 2; 158 | 159 | /* select frequency divider and the frequency of VCO */ 160 | if (freq < 37084000) { /* freq * 96 < 3560000000 */ 161 | multi = 96; 162 | reg[5] = 0x82; 163 | reg[6] = 0x00; 164 | } else if (freq < 55625000) { /* freq * 64 < 3560000000 */ 165 | multi = 64; 166 | reg[5] = 0x82; 167 | reg[6] = 0x02; 168 | } else if (freq < 74167000) { /* freq * 48 < 3560000000 */ 169 | multi = 48; 170 | reg[5] = 0x42; 171 | reg[6] = 0x00; 172 | } else if (freq < 111250000) { /* freq * 32 < 3560000000 */ 173 | multi = 32; 174 | reg[5] = 0x42; 175 | reg[6] = 0x02; 176 | } else if (freq < 148334000) { /* freq * 24 < 3560000000 */ 177 | multi = 24; 178 | reg[5] = 0x22; 179 | reg[6] = 0x00; 180 | } else if (freq < 222500000) { /* freq * 16 < 3560000000 */ 181 | multi = 16; 182 | reg[5] = 0x22; 183 | reg[6] = 0x02; 184 | } else if (freq < 296667000) { /* freq * 12 < 3560000000 */ 185 | multi = 12; 186 | reg[5] = 0x12; 187 | reg[6] = 0x00; 188 | } else if (freq < 445000000) { /* freq * 8 < 3560000000 */ 189 | multi = 8; 190 | reg[5] = 0x12; 191 | reg[6] = 0x02; 192 | } else if (freq < 593334000) { /* freq * 6 < 3560000000 */ 193 | multi = 6; 194 | reg[5] = 0x0a; 195 | reg[6] = 0x00; 196 | } else { 197 | multi = 4; 198 | reg[5] = 0x0a; 199 | reg[6] = 0x02; 200 | } 201 | 202 | f_vco = freq * multi; 203 | 204 | if (f_vco >= 3060000000U) { 205 | reg[6] |= 0x08; 206 | vco_select = 1; 207 | } 208 | 209 | /* From divided value (XDIV) determined the FA and FP value */ 210 | xdiv = (uint16_t)(f_vco / xtal_freq_div_2); 211 | if ((f_vco - xdiv * xtal_freq_div_2) >= (xtal_freq_div_2 / 2)) 212 | xdiv++; 213 | 214 | pm = (uint8_t)(xdiv / 8); 215 | am = (uint8_t)(xdiv - (8 * pm)); 216 | 217 | if (am < 2) { 218 | am += 8; 219 | pm--; 220 | } 221 | 222 | if (pm > 31) { 223 | reg[1] = am + (8 * (pm - 31)); 224 | reg[2] = 31; 225 | } else { 226 | reg[1] = am; 227 | reg[2] = pm; 228 | } 229 | 230 | if ((reg[1] > 15) || (reg[2] < 0x0b)) { 231 | fprintf(stderr, "[FC0012] no valid PLL combination " 232 | "found for %u Hz!\n", freq); 233 | return -1; 234 | } 235 | 236 | /* fix clock out */ 237 | reg[6] |= 0x20; 238 | 239 | /* From VCO frequency determines the XIN ( fractional part of Delta 240 | Sigma PLL) and divided value (XDIV) */ 241 | xin = (uint16_t)((f_vco - (f_vco / xtal_freq_div_2) * xtal_freq_div_2) / 1000); 242 | xin = (xin << 15) / (xtal_freq_div_2 / 1000); 243 | if (xin >= 16384) 244 | xin += 32768; 245 | 246 | reg[3] = xin >> 8; /* xin with 9 bit resolution */ 247 | reg[4] = xin & 0xff; 248 | 249 | reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */ 250 | switch (bandwidth) { 251 | case 6000000: 252 | reg[6] |= 0x80; 253 | break; 254 | case 7000000: 255 | reg[6] |= 0x40; 256 | break; 257 | case 8000000: 258 | default: 259 | break; 260 | } 261 | 262 | /* modified for Realtek demod */ 263 | reg[5] |= 0x07; 264 | 265 | for (i = 1; i <= 6; i++) { 266 | ret = fc0012_writereg(dev, i, reg[i]); 267 | if (ret) 268 | goto exit; 269 | } 270 | 271 | /* VCO Calibration */ 272 | ret = fc0012_writereg(dev, 0x0e, 0x80); 273 | if (!ret) 274 | ret = fc0012_writereg(dev, 0x0e, 0x00); 275 | 276 | /* VCO Re-Calibration if needed */ 277 | if (!ret) 278 | ret = fc0012_writereg(dev, 0x0e, 0x00); 279 | 280 | if (!ret) { 281 | // msleep(10); 282 | ret = fc0012_readreg(dev, 0x0e, &tmp); 283 | } 284 | if (ret) 285 | goto exit; 286 | 287 | /* vco selection */ 288 | tmp &= 0x3f; 289 | 290 | if (vco_select) { 291 | if (tmp > 0x3c) { 292 | reg[6] &= ~0x08; 293 | ret = fc0012_writereg(dev, 0x06, reg[6]); 294 | if (!ret) 295 | ret = fc0012_writereg(dev, 0x0e, 0x80); 296 | if (!ret) 297 | ret = fc0012_writereg(dev, 0x0e, 0x00); 298 | } 299 | } else { 300 | if (tmp < 0x02) { 301 | reg[6] |= 0x08; 302 | ret = fc0012_writereg(dev, 0x06, reg[6]); 303 | if (!ret) 304 | ret = fc0012_writereg(dev, 0x0e, 0x80); 305 | if (!ret) 306 | ret = fc0012_writereg(dev, 0x0e, 0x00); 307 | } 308 | } 309 | 310 | exit: 311 | return ret; 312 | } 313 | 314 | int fc0012_set_gain(void *dev, int gain) 315 | { 316 | int ret; 317 | uint8_t tmp = 0; 318 | 319 | ret = fc0012_readreg(dev, 0x13, &tmp); 320 | 321 | /* mask bits off */ 322 | tmp &= 0xe0; 323 | 324 | switch (gain) { 325 | case -99: /* -9.9 dB */ 326 | tmp |= 0x02; 327 | break; 328 | case -40: /* -4 dB */ 329 | break; 330 | case 71: 331 | tmp |= 0x08; /* 7.1 dB */ 332 | break; 333 | case 179: 334 | tmp |= 0x17; /* 17.9 dB */ 335 | break; 336 | case 192: 337 | default: 338 | tmp |= 0x10; /* 19.2 dB */ 339 | break; 340 | } 341 | 342 | ret = fc0012_writereg(dev, 0x13, tmp); 343 | 344 | return ret; 345 | } 346 | -------------------------------------------------------------------------------- /src/tuner_fc0013.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Fitipower FC0013 tuner driver 3 | * 4 | * Copyright (C) 2012 Hans-Frieder Vogt 5 | * partially based on driver code from Fitipower 6 | * Copyright (C) 2010 Fitipower Integrated Technology Inc 7 | * 8 | * modified for use in librtlsdr 9 | * Copyright (C) 2012 Steve Markgraf 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation; either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 | * 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "rtlsdr_i2c.h" 31 | #include "tuner_fc0013.h" 32 | 33 | static int fc0013_writereg(void *dev, uint8_t reg, uint8_t val) 34 | { 35 | uint8_t data[2]; 36 | data[0] = reg; 37 | data[1] = val; 38 | 39 | if (rtlsdr_i2c_write_fn(dev, FC0013_I2C_ADDR, data, 2) < 0) 40 | return -1; 41 | 42 | return 0; 43 | } 44 | 45 | static int fc0013_readreg(void *dev, uint8_t reg, uint8_t *val) 46 | { 47 | uint8_t data = reg; 48 | 49 | if (rtlsdr_i2c_write_fn(dev, FC0013_I2C_ADDR, &data, 1) < 0) 50 | return -1; 51 | 52 | if (rtlsdr_i2c_read_fn(dev, FC0013_I2C_ADDR, &data, 1) < 0) 53 | return -1; 54 | 55 | *val = data; 56 | 57 | return 0; 58 | } 59 | 60 | int fc0013_init(void *dev) 61 | { 62 | int ret = 0; 63 | unsigned int i; 64 | uint8_t reg[] = { 65 | 0x00, /* reg. 0x00: dummy */ 66 | 0x09, /* reg. 0x01 */ 67 | 0x16, /* reg. 0x02 */ 68 | 0x00, /* reg. 0x03 */ 69 | 0x00, /* reg. 0x04 */ 70 | 0x17, /* reg. 0x05 */ 71 | 0x02, /* reg. 0x06: LPF bandwidth */ 72 | 0x0a, /* reg. 0x07: CHECK */ 73 | 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256, 74 | Loop Bw 1/8 */ 75 | 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */ 76 | 0xb8, /* reg. 0x0a: Disable LO Test Buffer */ 77 | 0x82, /* reg. 0x0b: CHECK */ 78 | 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */ 79 | 0x01, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, may need 0x02 */ 80 | 0x00, /* reg. 0x0e */ 81 | 0x00, /* reg. 0x0f */ 82 | 0x00, /* reg. 0x10 */ 83 | 0x00, /* reg. 0x11 */ 84 | 0x00, /* reg. 0x12 */ 85 | 0x00, /* reg. 0x13 */ 86 | 0x50, /* reg. 0x14: DVB-t High Gain, UHF. 87 | Middle Gain: 0x48, Low Gain: 0x40 */ 88 | 0x01, /* reg. 0x15 */ 89 | }; 90 | #if 0 91 | switch (rtlsdr_get_tuner_clock(dev)) { 92 | case FC_XTAL_27_MHZ: 93 | case FC_XTAL_28_8_MHZ: 94 | reg[0x07] |= 0x20; 95 | break; 96 | case FC_XTAL_36_MHZ: 97 | default: 98 | break; 99 | } 100 | #endif 101 | reg[0x07] |= 0x20; 102 | 103 | // if (dev->dual_master) 104 | reg[0x0c] |= 0x02; 105 | 106 | for (i = 1; i < sizeof(reg); i++) { 107 | ret = fc0013_writereg(dev, i, reg[i]); 108 | if (ret < 0) 109 | break; 110 | } 111 | 112 | return ret; 113 | } 114 | 115 | int fc0013_rc_cal_add(void *dev, int rc_val) 116 | { 117 | int ret; 118 | uint8_t rc_cal; 119 | int val; 120 | 121 | /* push rc_cal value, get rc_cal value */ 122 | ret = fc0013_writereg(dev, 0x10, 0x00); 123 | if (ret) 124 | goto error_out; 125 | 126 | /* get rc_cal value */ 127 | ret = fc0013_readreg(dev, 0x10, &rc_cal); 128 | if (ret) 129 | goto error_out; 130 | 131 | rc_cal &= 0x0f; 132 | 133 | val = (int)rc_cal + rc_val; 134 | 135 | /* forcing rc_cal */ 136 | ret = fc0013_writereg(dev, 0x0d, 0x11); 137 | if (ret) 138 | goto error_out; 139 | 140 | /* modify rc_cal value */ 141 | if (val > 15) 142 | ret = fc0013_writereg(dev, 0x10, 0x0f); 143 | else if (val < 0) 144 | ret = fc0013_writereg(dev, 0x10, 0x00); 145 | else 146 | ret = fc0013_writereg(dev, 0x10, (uint8_t)val); 147 | 148 | error_out: 149 | return ret; 150 | } 151 | 152 | int fc0013_rc_cal_reset(void *dev) 153 | { 154 | int ret; 155 | 156 | ret = fc0013_writereg(dev, 0x0d, 0x01); 157 | if (!ret) 158 | ret = fc0013_writereg(dev, 0x10, 0x00); 159 | 160 | return ret; 161 | } 162 | 163 | static int fc0013_set_vhf_track(void *dev, uint32_t freq) 164 | { 165 | int ret; 166 | uint8_t tmp; 167 | 168 | ret = fc0013_readreg(dev, 0x1d, &tmp); 169 | if (ret) 170 | goto error_out; 171 | tmp &= 0xe3; 172 | if (freq <= 177500000) { /* VHF Track: 7 */ 173 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x1c); 174 | } else if (freq <= 184500000) { /* VHF Track: 6 */ 175 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x18); 176 | } else if (freq <= 191500000) { /* VHF Track: 5 */ 177 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x14); 178 | } else if (freq <= 198500000) { /* VHF Track: 4 */ 179 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x10); 180 | } else if (freq <= 205500000) { /* VHF Track: 3 */ 181 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x0c); 182 | } else if (freq <= 219500000) { /* VHF Track: 2 */ 183 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x08); 184 | } else if (freq < 300000000) { /* VHF Track: 1 */ 185 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x04); 186 | } else { /* UHF and GPS */ 187 | ret = fc0013_writereg(dev, 0x1d, tmp | 0x1c); 188 | } 189 | 190 | error_out: 191 | return ret; 192 | } 193 | 194 | int fc0013_set_params(void *dev, uint32_t freq, uint32_t bandwidth) 195 | { 196 | int i, ret = 0; 197 | uint8_t reg[7], am, pm, multi, tmp; 198 | uint64_t f_vco; 199 | uint32_t xtal_freq_div_2; 200 | uint16_t xin, xdiv; 201 | int vco_select = 0; 202 | 203 | xtal_freq_div_2 = rtlsdr_get_tuner_clock(dev) / 2; 204 | 205 | /* set VHF track */ 206 | ret = fc0013_set_vhf_track(dev, freq); 207 | if (ret) 208 | goto exit; 209 | 210 | if (freq < 300000000) { 211 | /* enable VHF filter */ 212 | ret = fc0013_readreg(dev, 0x07, &tmp); 213 | if (ret) 214 | goto exit; 215 | ret = fc0013_writereg(dev, 0x07, tmp | 0x10); 216 | if (ret) 217 | goto exit; 218 | 219 | /* disable UHF & disable GPS */ 220 | ret = fc0013_readreg(dev, 0x14, &tmp); 221 | if (ret) 222 | goto exit; 223 | ret = fc0013_writereg(dev, 0x14, tmp & 0x1f); 224 | if (ret) 225 | goto exit; 226 | } else if (freq <= 862000000) { 227 | /* disable VHF filter */ 228 | ret = fc0013_readreg(dev, 0x07, &tmp); 229 | if (ret) 230 | goto exit; 231 | ret = fc0013_writereg(dev, 0x07, tmp & 0xef); 232 | if (ret) 233 | goto exit; 234 | 235 | /* enable UHF & disable GPS */ 236 | ret = fc0013_readreg(dev, 0x14, &tmp); 237 | if (ret) 238 | goto exit; 239 | ret = fc0013_writereg(dev, 0x14, (tmp & 0x1f) | 0x40); 240 | if (ret) 241 | goto exit; 242 | } else { 243 | /* disable VHF filter */ 244 | ret = fc0013_readreg(dev, 0x07, &tmp); 245 | if (ret) 246 | goto exit; 247 | ret = fc0013_writereg(dev, 0x07, tmp & 0xef); 248 | if (ret) 249 | goto exit; 250 | 251 | /* disable UHF & enable GPS */ 252 | ret = fc0013_readreg(dev, 0x14, &tmp); 253 | if (ret) 254 | goto exit; 255 | ret = fc0013_writereg(dev, 0x14, (tmp & 0x1f) | 0x20); 256 | if (ret) 257 | goto exit; 258 | } 259 | 260 | /* select frequency divider and the frequency of VCO */ 261 | if (freq < 37084000) { /* freq * 96 < 3560000000 */ 262 | multi = 96; 263 | reg[5] = 0x82; 264 | reg[6] = 0x00; 265 | } else if (freq < 55625000) { /* freq * 64 < 3560000000 */ 266 | multi = 64; 267 | reg[5] = 0x02; 268 | reg[6] = 0x02; 269 | } else if (freq < 74167000) { /* freq * 48 < 3560000000 */ 270 | multi = 48; 271 | reg[5] = 0x42; 272 | reg[6] = 0x00; 273 | } else if (freq < 111250000) { /* freq * 32 < 3560000000 */ 274 | multi = 32; 275 | reg[5] = 0x82; 276 | reg[6] = 0x02; 277 | } else if (freq < 148334000) { /* freq * 24 < 3560000000 */ 278 | multi = 24; 279 | reg[5] = 0x22; 280 | reg[6] = 0x00; 281 | } else if (freq < 222500000) { /* freq * 16 < 3560000000 */ 282 | multi = 16; 283 | reg[5] = 0x42; 284 | reg[6] = 0x02; 285 | } else if (freq < 296667000) { /* freq * 12 < 3560000000 */ 286 | multi = 12; 287 | reg[5] = 0x12; 288 | reg[6] = 0x00; 289 | } else if (freq < 445000000) { /* freq * 8 < 3560000000 */ 290 | multi = 8; 291 | reg[5] = 0x22; 292 | reg[6] = 0x02; 293 | } else if (freq < 593334000) { /* freq * 6 < 3560000000 */ 294 | multi = 6; 295 | reg[5] = 0x0a; 296 | reg[6] = 0x00; 297 | } else if (freq < 950000000) { /* freq * 4 < 3800000000 */ 298 | multi = 4; 299 | reg[5] = 0x12; 300 | reg[6] = 0x02; 301 | } else { 302 | multi = 2; 303 | reg[5] = 0x0a; 304 | reg[6] = 0x02; 305 | } 306 | 307 | f_vco = freq * multi; 308 | 309 | if (f_vco >= 3060000000U) { 310 | reg[6] |= 0x08; 311 | vco_select = 1; 312 | } 313 | 314 | /* From divided value (XDIV) determined the FA and FP value */ 315 | xdiv = (uint16_t)(f_vco / xtal_freq_div_2); 316 | if ((f_vco - xdiv * xtal_freq_div_2) >= (xtal_freq_div_2 / 2)) 317 | xdiv++; 318 | 319 | pm = (uint8_t)(xdiv / 8); 320 | am = (uint8_t)(xdiv - (8 * pm)); 321 | 322 | if (am < 2) { 323 | am += 8; 324 | pm--; 325 | } 326 | 327 | if (pm > 31) { 328 | reg[1] = am + (8 * (pm - 31)); 329 | reg[2] = 31; 330 | } else { 331 | reg[1] = am; 332 | reg[2] = pm; 333 | } 334 | 335 | if ((reg[1] > 15) || (reg[2] < 0x0b)) { 336 | fprintf(stderr, "[FC0013] no valid PLL combination " 337 | "found for %u Hz!\n", freq); 338 | return -1; 339 | } 340 | 341 | /* fix clock out */ 342 | reg[6] |= 0x20; 343 | 344 | /* From VCO frequency determines the XIN ( fractional part of Delta 345 | Sigma PLL) and divided value (XDIV) */ 346 | xin = (uint16_t)((f_vco - (f_vco / xtal_freq_div_2) * xtal_freq_div_2) / 1000); 347 | xin = (xin << 15) / (xtal_freq_div_2 / 1000); 348 | if (xin >= 16384) 349 | xin += 32768; 350 | 351 | reg[3] = xin >> 8; 352 | reg[4] = xin & 0xff; 353 | 354 | reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */ 355 | switch (bandwidth) { 356 | case 6000000: 357 | reg[6] |= 0x80; 358 | break; 359 | case 7000000: 360 | reg[6] |= 0x40; 361 | break; 362 | case 8000000: 363 | default: 364 | break; 365 | } 366 | 367 | /* modified for Realtek demod */ 368 | reg[5] |= 0x07; 369 | 370 | for (i = 1; i <= 6; i++) { 371 | ret = fc0013_writereg(dev, i, reg[i]); 372 | if (ret) 373 | goto exit; 374 | } 375 | 376 | ret = fc0013_readreg(dev, 0x11, &tmp); 377 | if (ret) 378 | goto exit; 379 | if (multi == 64) 380 | ret = fc0013_writereg(dev, 0x11, tmp | 0x04); 381 | else 382 | ret = fc0013_writereg(dev, 0x11, tmp & 0xfb); 383 | if (ret) 384 | goto exit; 385 | 386 | /* VCO Calibration */ 387 | ret = fc0013_writereg(dev, 0x0e, 0x80); 388 | if (!ret) 389 | ret = fc0013_writereg(dev, 0x0e, 0x00); 390 | 391 | /* VCO Re-Calibration if needed */ 392 | if (!ret) 393 | ret = fc0013_writereg(dev, 0x0e, 0x00); 394 | 395 | if (!ret) { 396 | // msleep(10); 397 | ret = fc0013_readreg(dev, 0x0e, &tmp); 398 | } 399 | if (ret) 400 | goto exit; 401 | 402 | /* vco selection */ 403 | tmp &= 0x3f; 404 | 405 | if (vco_select) { 406 | if (tmp > 0x3c) { 407 | reg[6] &= ~0x08; 408 | ret = fc0013_writereg(dev, 0x06, reg[6]); 409 | if (!ret) 410 | ret = fc0013_writereg(dev, 0x0e, 0x80); 411 | if (!ret) 412 | ret = fc0013_writereg(dev, 0x0e, 0x00); 413 | } 414 | } else { 415 | if (tmp < 0x02) { 416 | reg[6] |= 0x08; 417 | ret = fc0013_writereg(dev, 0x06, reg[6]); 418 | if (!ret) 419 | ret = fc0013_writereg(dev, 0x0e, 0x80); 420 | if (!ret) 421 | ret = fc0013_writereg(dev, 0x0e, 0x00); 422 | } 423 | } 424 | 425 | exit: 426 | return ret; 427 | } 428 | 429 | int fc0013_set_gain_mode(void *dev, int manual) 430 | { 431 | int ret = 0; 432 | uint8_t tmp = 0; 433 | 434 | ret |= fc0013_readreg(dev, 0x0d, &tmp); 435 | 436 | if (manual) 437 | tmp |= (1 << 3); 438 | else 439 | tmp &= ~(1 << 3); 440 | 441 | ret |= fc0013_writereg(dev, 0x0d, tmp); 442 | 443 | /* set a fixed IF-gain for now */ 444 | ret |= fc0013_writereg(dev, 0x13, 0x0a); 445 | 446 | return ret; 447 | } 448 | 449 | int fc0013_lna_gains[] ={ 450 | -99, 0x02, 451 | -73, 0x03, 452 | -65, 0x05, 453 | -63, 0x04, 454 | -63, 0x00, 455 | -60, 0x07, 456 | -58, 0x01, 457 | -54, 0x06, 458 | 58, 0x0f, 459 | 61, 0x0e, 460 | 63, 0x0d, 461 | 65, 0x0c, 462 | 67, 0x0b, 463 | 68, 0x0a, 464 | 70, 0x09, 465 | 71, 0x08, 466 | 179, 0x17, 467 | 181, 0x16, 468 | 182, 0x15, 469 | 184, 0x14, 470 | 186, 0x13, 471 | 188, 0x12, 472 | 191, 0x11, 473 | 197, 0x10 474 | }; 475 | 476 | #define GAIN_CNT (sizeof(fc0013_lna_gains) / sizeof(int) / 2) 477 | 478 | int fc0013_set_lna_gain(void *dev, int gain) 479 | { 480 | int ret = 0; 481 | unsigned int i; 482 | uint8_t tmp = 0; 483 | 484 | ret |= fc0013_readreg(dev, 0x14, &tmp); 485 | 486 | /* mask bits off */ 487 | tmp &= 0xe0; 488 | 489 | for (i = 0; i < GAIN_CNT; i++) { 490 | if ((fc0013_lna_gains[i*2] >= gain) || (i+1 == GAIN_CNT)) { 491 | tmp |= fc0013_lna_gains[i*2 + 1]; 492 | break; 493 | } 494 | } 495 | 496 | /* set gain */ 497 | ret |= fc0013_writereg(dev, 0x14, tmp); 498 | 499 | return ret; 500 | } 501 | -------------------------------------------------------------------------------- /src/tuner_fc2580.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FCI FC2580 tuner driver, taken from the kernel driver that can be found 3 | * on http://linux.terratec.de/tv_en.html 4 | * 5 | * This driver is a mess, and should be cleaned up/rewritten. 6 | * 7 | */ 8 | 9 | #include 10 | 11 | #include "rtlsdr_i2c.h" 12 | #include "tuner_fc2580.h" 13 | 14 | /* 16.384 MHz (at least on the Logilink VG0002A) */ 15 | #define CRYSTAL_FREQ 16384000 16 | 17 | /* glue functions to rtl-sdr code */ 18 | 19 | fc2580_fci_result_type fc2580_i2c_write(void *pTuner, unsigned char reg, unsigned char val) 20 | { 21 | uint8_t data[2]; 22 | 23 | data[0] = reg; 24 | data[1] = val; 25 | 26 | if (rtlsdr_i2c_write_fn(pTuner, FC2580_I2C_ADDR, data, 2) < 0) 27 | return FC2580_FCI_FAIL; 28 | 29 | return FC2580_FCI_SUCCESS; 30 | } 31 | 32 | fc2580_fci_result_type fc2580_i2c_read(void *pTuner, unsigned char reg, unsigned char *read_data) 33 | { 34 | uint8_t data = reg; 35 | 36 | if (rtlsdr_i2c_write_fn(pTuner, FC2580_I2C_ADDR, &data, 1) < 0) 37 | return FC2580_FCI_FAIL; 38 | 39 | if (rtlsdr_i2c_read_fn(pTuner, FC2580_I2C_ADDR, &data, 1) < 0) 40 | return FC2580_FCI_FAIL; 41 | 42 | *read_data = data; 43 | 44 | return FC2580_FCI_SUCCESS; 45 | } 46 | 47 | int 48 | fc2580_Initialize( 49 | void *pTuner 50 | ) 51 | { 52 | int AgcMode; 53 | unsigned int CrystalFreqKhz; 54 | 55 | //TODO set AGC mode 56 | AgcMode = FC2580_AGC_EXTERNAL; 57 | 58 | // Initialize tuner with AGC mode. 59 | // Note: CrystalFreqKhz = round(CrystalFreqHz / 1000) 60 | CrystalFreqKhz = (unsigned int)((CRYSTAL_FREQ + 500) / 1000); 61 | 62 | if(fc2580_set_init(pTuner, AgcMode, CrystalFreqKhz) != FC2580_FCI_SUCCESS) 63 | goto error_status_initialize_tuner; 64 | 65 | 66 | return FUNCTION_SUCCESS; 67 | 68 | 69 | error_status_initialize_tuner: 70 | return FUNCTION_ERROR; 71 | } 72 | 73 | int 74 | fc2580_SetRfFreqHz( 75 | void *pTuner, 76 | unsigned long RfFreqHz 77 | ) 78 | { 79 | unsigned int RfFreqKhz; 80 | unsigned int CrystalFreqKhz; 81 | 82 | // Set tuner RF frequency in KHz. 83 | // Note: RfFreqKhz = round(RfFreqHz / 1000) 84 | // CrystalFreqKhz = round(CrystalFreqHz / 1000) 85 | RfFreqKhz = (unsigned int)((RfFreqHz + 500) / 1000); 86 | CrystalFreqKhz = (unsigned int)((CRYSTAL_FREQ + 500) / 1000); 87 | 88 | if(fc2580_set_freq(pTuner, RfFreqKhz, CrystalFreqKhz) != FC2580_FCI_SUCCESS) 89 | goto error_status_set_tuner_rf_frequency; 90 | 91 | return FUNCTION_SUCCESS; 92 | 93 | error_status_set_tuner_rf_frequency: 94 | return FUNCTION_ERROR; 95 | } 96 | 97 | /** 98 | 99 | @brief Set FC2580 tuner bandwidth mode. 100 | 101 | */ 102 | int 103 | fc2580_SetBandwidthMode( 104 | void *pTuner, 105 | int BandwidthMode 106 | ) 107 | { 108 | unsigned int CrystalFreqKhz; 109 | 110 | // Set tuner bandwidth mode. 111 | // Note: CrystalFreqKhz = round(CrystalFreqHz / 1000) 112 | CrystalFreqKhz = (unsigned int)((CRYSTAL_FREQ + 500) / 1000); 113 | 114 | if(fc2580_set_filter(pTuner, (unsigned char)BandwidthMode, CrystalFreqKhz) != FC2580_FCI_SUCCESS) 115 | goto error_status_set_tuner_bandwidth_mode; 116 | 117 | return FUNCTION_SUCCESS; 118 | 119 | 120 | error_status_set_tuner_bandwidth_mode: 121 | return FUNCTION_ERROR; 122 | } 123 | 124 | void fc2580_wait_msec(void *pTuner, int a) 125 | { 126 | /* USB latency is enough for now ;) */ 127 | // usleep(a * 1000); 128 | return; 129 | } 130 | 131 | /*============================================================================== 132 | fc2580 initial setting 133 | 134 | This function is a generic function which gets called to initialize 135 | 136 | fc2580 in DVB-H mode or L-Band TDMB mode 137 | 138 | 139 | 140 | ifagc_mode 141 | type : integer 142 | 1 : Internal AGC 143 | 2 : Voltage Control Mode 144 | 145 | ==============================================================================*/ 146 | fc2580_fci_result_type fc2580_set_init( void *pTuner, int ifagc_mode, unsigned int freq_xtal ) 147 | { 148 | fc2580_fci_result_type result = FC2580_FCI_SUCCESS; 149 | 150 | result &= fc2580_i2c_write(pTuner, 0x00, 0x00); /*** Confidential ***/ 151 | result &= fc2580_i2c_write(pTuner, 0x12, 0x86); 152 | result &= fc2580_i2c_write(pTuner, 0x14, 0x5C); 153 | result &= fc2580_i2c_write(pTuner, 0x16, 0x3C); 154 | result &= fc2580_i2c_write(pTuner, 0x1F, 0xD2); 155 | result &= fc2580_i2c_write(pTuner, 0x09, 0xD7); 156 | result &= fc2580_i2c_write(pTuner, 0x0B, 0xD5); 157 | result &= fc2580_i2c_write(pTuner, 0x0C, 0x32); 158 | result &= fc2580_i2c_write(pTuner, 0x0E, 0x43); 159 | result &= fc2580_i2c_write(pTuner, 0x21, 0x0A); 160 | result &= fc2580_i2c_write(pTuner, 0x22, 0x82); 161 | if( ifagc_mode == 1 ) 162 | { 163 | result &= fc2580_i2c_write(pTuner, 0x45, 0x10); //internal AGC 164 | result &= fc2580_i2c_write(pTuner, 0x4C, 0x00); //HOLD_AGC polarity 165 | } 166 | else if( ifagc_mode == 2 ) 167 | { 168 | result &= fc2580_i2c_write(pTuner, 0x45, 0x20); //Voltage Control Mode 169 | result &= fc2580_i2c_write(pTuner, 0x4C, 0x02); //HOLD_AGC polarity 170 | } 171 | result &= fc2580_i2c_write(pTuner, 0x3F, 0x88); 172 | result &= fc2580_i2c_write(pTuner, 0x02, 0x0E); 173 | result &= fc2580_i2c_write(pTuner, 0x58, 0x14); 174 | result &= fc2580_set_filter(pTuner, 8, freq_xtal); //BW = 7.8MHz 175 | 176 | return result; 177 | } 178 | 179 | 180 | /*============================================================================== 181 | fc2580 frequency setting 182 | 183 | This function is a generic function which gets called to change LO Frequency 184 | 185 | of fc2580 in DVB-H mode or L-Band TDMB mode 186 | 187 | 188 | freq_xtal: kHz 189 | 190 | f_lo 191 | Value of target LO Frequency in 'kHz' unit 192 | ex) 2.6GHz = 2600000 193 | 194 | ==============================================================================*/ 195 | fc2580_fci_result_type fc2580_set_freq( void *pTuner, unsigned int f_lo, unsigned int freq_xtal ) 196 | { 197 | unsigned int f_diff, f_diff_shifted, n_val, k_val; 198 | unsigned int f_vco, r_val, f_comp; 199 | unsigned char pre_shift_bits = 4;// number of preshift to prevent overflow in shifting f_diff to f_diff_shifted 200 | unsigned char data_0x18; 201 | unsigned char data_0x02 = (USE_EXT_CLK<<5)|0x0E; 202 | 203 | fc2580_band_type band = ( f_lo > 1000000 )? FC2580_L_BAND : ( f_lo > 400000 )? FC2580_UHF_BAND : FC2580_VHF_BAND; 204 | 205 | fc2580_fci_result_type result = FC2580_FCI_SUCCESS; 206 | 207 | f_vco = ( band == FC2580_UHF_BAND )? f_lo * 4 : (( band == FC2580_L_BAND )? f_lo * 2 : f_lo * 12); 208 | r_val = ( f_vco >= 2*76*freq_xtal )? 1 : ( f_vco >= 76*freq_xtal )? 2 : 4; 209 | f_comp = freq_xtal/r_val; 210 | n_val = ( f_vco / 2 ) / f_comp; 211 | 212 | f_diff = f_vco - 2* f_comp * n_val; 213 | f_diff_shifted = f_diff << ( 20 - pre_shift_bits ); 214 | k_val = f_diff_shifted / ( ( 2* f_comp ) >> pre_shift_bits ); 215 | 216 | if( f_diff_shifted - k_val * ( ( 2* f_comp ) >> pre_shift_bits ) >= ( f_comp >> pre_shift_bits ) ) 217 | k_val = k_val + 1; 218 | 219 | if( f_vco >= BORDER_FREQ ) //Select VCO Band 220 | data_0x02 = data_0x02 | 0x08; //0x02[3] = 1; 221 | else 222 | data_0x02 = data_0x02 & 0xF7; //0x02[3] = 0; 223 | 224 | // if( band != curr_band ) { 225 | switch(band) 226 | { 227 | case FC2580_UHF_BAND: 228 | data_0x02 = (data_0x02 & 0x3F); 229 | 230 | result &= fc2580_i2c_write(pTuner, 0x25, 0xF0); 231 | result &= fc2580_i2c_write(pTuner, 0x27, 0x77); 232 | result &= fc2580_i2c_write(pTuner, 0x28, 0x53); 233 | result &= fc2580_i2c_write(pTuner, 0x29, 0x60); 234 | result &= fc2580_i2c_write(pTuner, 0x30, 0x09); 235 | result &= fc2580_i2c_write(pTuner, 0x50, 0x8C); 236 | result &= fc2580_i2c_write(pTuner, 0x53, 0x50); 237 | 238 | if( f_lo < 538000 ) 239 | result &= fc2580_i2c_write(pTuner, 0x5F, 0x13); 240 | else 241 | result &= fc2580_i2c_write(pTuner, 0x5F, 0x15); 242 | 243 | if( f_lo < 538000 ) 244 | { 245 | result &= fc2580_i2c_write(pTuner, 0x61, 0x07); 246 | result &= fc2580_i2c_write(pTuner, 0x62, 0x06); 247 | result &= fc2580_i2c_write(pTuner, 0x67, 0x06); 248 | result &= fc2580_i2c_write(pTuner, 0x68, 0x08); 249 | result &= fc2580_i2c_write(pTuner, 0x69, 0x10); 250 | result &= fc2580_i2c_write(pTuner, 0x6A, 0x12); 251 | } 252 | else if( f_lo < 794000 ) 253 | { 254 | result &= fc2580_i2c_write(pTuner, 0x61, 0x03); 255 | result &= fc2580_i2c_write(pTuner, 0x62, 0x03); 256 | result &= fc2580_i2c_write(pTuner, 0x67, 0x03); //ACI improve 257 | result &= fc2580_i2c_write(pTuner, 0x68, 0x05); //ACI improve 258 | result &= fc2580_i2c_write(pTuner, 0x69, 0x0C); 259 | result &= fc2580_i2c_write(pTuner, 0x6A, 0x0E); 260 | } 261 | else 262 | { 263 | result &= fc2580_i2c_write(pTuner, 0x61, 0x07); 264 | result &= fc2580_i2c_write(pTuner, 0x62, 0x06); 265 | result &= fc2580_i2c_write(pTuner, 0x67, 0x07); 266 | result &= fc2580_i2c_write(pTuner, 0x68, 0x09); 267 | result &= fc2580_i2c_write(pTuner, 0x69, 0x10); 268 | result &= fc2580_i2c_write(pTuner, 0x6A, 0x12); 269 | } 270 | 271 | result &= fc2580_i2c_write(pTuner, 0x63, 0x15); 272 | 273 | result &= fc2580_i2c_write(pTuner, 0x6B, 0x0B); 274 | result &= fc2580_i2c_write(pTuner, 0x6C, 0x0C); 275 | result &= fc2580_i2c_write(pTuner, 0x6D, 0x78); 276 | result &= fc2580_i2c_write(pTuner, 0x6E, 0x32); 277 | result &= fc2580_i2c_write(pTuner, 0x6F, 0x14); 278 | result &= fc2580_set_filter(pTuner, 8, freq_xtal); //BW = 7.8MHz 279 | break; 280 | case FC2580_VHF_BAND: 281 | data_0x02 = (data_0x02 & 0x3F) | 0x80; 282 | result &= fc2580_i2c_write(pTuner, 0x27, 0x77); 283 | result &= fc2580_i2c_write(pTuner, 0x28, 0x33); 284 | result &= fc2580_i2c_write(pTuner, 0x29, 0x40); 285 | result &= fc2580_i2c_write(pTuner, 0x30, 0x09); 286 | result &= fc2580_i2c_write(pTuner, 0x50, 0x8C); 287 | result &= fc2580_i2c_write(pTuner, 0x53, 0x50); 288 | result &= fc2580_i2c_write(pTuner, 0x5F, 0x0F); 289 | result &= fc2580_i2c_write(pTuner, 0x61, 0x07); 290 | result &= fc2580_i2c_write(pTuner, 0x62, 0x00); 291 | result &= fc2580_i2c_write(pTuner, 0x63, 0x15); 292 | result &= fc2580_i2c_write(pTuner, 0x67, 0x03); 293 | result &= fc2580_i2c_write(pTuner, 0x68, 0x05); 294 | result &= fc2580_i2c_write(pTuner, 0x69, 0x10); 295 | result &= fc2580_i2c_write(pTuner, 0x6A, 0x12); 296 | result &= fc2580_i2c_write(pTuner, 0x6B, 0x08); 297 | result &= fc2580_i2c_write(pTuner, 0x6C, 0x0A); 298 | result &= fc2580_i2c_write(pTuner, 0x6D, 0x78); 299 | result &= fc2580_i2c_write(pTuner, 0x6E, 0x32); 300 | result &= fc2580_i2c_write(pTuner, 0x6F, 0x54); 301 | result &= fc2580_set_filter(pTuner, 7, freq_xtal); //BW = 6.8MHz 302 | break; 303 | case FC2580_L_BAND: 304 | data_0x02 = (data_0x02 & 0x3F) | 0x40; 305 | result &= fc2580_i2c_write(pTuner, 0x2B, 0x70); 306 | result &= fc2580_i2c_write(pTuner, 0x2C, 0x37); 307 | result &= fc2580_i2c_write(pTuner, 0x2D, 0xE7); 308 | result &= fc2580_i2c_write(pTuner, 0x30, 0x09); 309 | result &= fc2580_i2c_write(pTuner, 0x44, 0x20); 310 | result &= fc2580_i2c_write(pTuner, 0x50, 0x8C); 311 | result &= fc2580_i2c_write(pTuner, 0x53, 0x50); 312 | result &= fc2580_i2c_write(pTuner, 0x5F, 0x0F); 313 | result &= fc2580_i2c_write(pTuner, 0x61, 0x0F); 314 | result &= fc2580_i2c_write(pTuner, 0x62, 0x00); 315 | result &= fc2580_i2c_write(pTuner, 0x63, 0x13); 316 | result &= fc2580_i2c_write(pTuner, 0x67, 0x00); 317 | result &= fc2580_i2c_write(pTuner, 0x68, 0x02); 318 | result &= fc2580_i2c_write(pTuner, 0x69, 0x0C); 319 | result &= fc2580_i2c_write(pTuner, 0x6A, 0x0E); 320 | result &= fc2580_i2c_write(pTuner, 0x6B, 0x08); 321 | result &= fc2580_i2c_write(pTuner, 0x6C, 0x0A); 322 | result &= fc2580_i2c_write(pTuner, 0x6D, 0xA0); 323 | result &= fc2580_i2c_write(pTuner, 0x6E, 0x50); 324 | result &= fc2580_i2c_write(pTuner, 0x6F, 0x14); 325 | result &= fc2580_set_filter(pTuner, 1, freq_xtal); //BW = 1.53MHz 326 | break; 327 | default: 328 | break; 329 | } 330 | // curr_band = band; 331 | // } 332 | 333 | //A command about AGC clock's pre-divide ratio 334 | if( freq_xtal >= 28000 ) 335 | result &= fc2580_i2c_write(pTuner, 0x4B, 0x22 ); 336 | 337 | //Commands about VCO Band and PLL setting. 338 | result &= fc2580_i2c_write(pTuner, 0x02, data_0x02); 339 | data_0x18 = ( ( r_val == 1 )? 0x00 : ( ( r_val == 2 )? 0x10 : 0x20 ) ) + (unsigned char)(k_val >> 16); 340 | result &= fc2580_i2c_write(pTuner, 0x18, data_0x18); //Load 'R' value and high part of 'K' values 341 | result &= fc2580_i2c_write(pTuner, 0x1A, (unsigned char)( k_val >> 8 ) ); //Load middle part of 'K' value 342 | result &= fc2580_i2c_write(pTuner, 0x1B, (unsigned char)( k_val ) ); //Load lower part of 'K' value 343 | result &= fc2580_i2c_write(pTuner, 0x1C, (unsigned char)( n_val ) ); //Load 'N' value 344 | 345 | //A command about UHF LNA Load Cap 346 | if( band == FC2580_UHF_BAND ) 347 | result &= fc2580_i2c_write(pTuner, 0x2D, ( f_lo <= (unsigned int)794000 )? 0x9F : 0x8F ); //LNA_OUT_CAP 348 | 349 | 350 | return result; 351 | } 352 | 353 | 354 | /*============================================================================== 355 | fc2580 filter BW setting 356 | 357 | This function is a generic function which gets called to change Bandwidth 358 | 359 | frequency of fc2580's channel selection filter 360 | 361 | 362 | freq_xtal: kHz 363 | 364 | filter_bw 365 | 1 : 1.53MHz(TDMB) 366 | 6 : 6MHz (Bandwidth 6MHz) 367 | 7 : 6.8MHz (Bandwidth 7MHz) 368 | 8 : 7.8MHz (Bandwidth 8MHz) 369 | 370 | 371 | ==============================================================================*/ 372 | fc2580_fci_result_type fc2580_set_filter( void *pTuner, unsigned char filter_bw, unsigned int freq_xtal ) 373 | { 374 | unsigned char cal_mon = 0, i; 375 | fc2580_fci_result_type result = FC2580_FCI_SUCCESS; 376 | 377 | if(filter_bw == 1) 378 | { 379 | result &= fc2580_i2c_write(pTuner, 0x36, 0x1C); 380 | result &= fc2580_i2c_write(pTuner, 0x37, (unsigned char)(4151*freq_xtal/1000000) ); 381 | result &= fc2580_i2c_write(pTuner, 0x39, 0x00); 382 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x09); 383 | } 384 | if(filter_bw == 6) 385 | { 386 | result &= fc2580_i2c_write(pTuner, 0x36, 0x18); 387 | result &= fc2580_i2c_write(pTuner, 0x37, (unsigned char)(4400*freq_xtal/1000000) ); 388 | result &= fc2580_i2c_write(pTuner, 0x39, 0x00); 389 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x09); 390 | } 391 | else if(filter_bw == 7) 392 | { 393 | result &= fc2580_i2c_write(pTuner, 0x36, 0x18); 394 | result &= fc2580_i2c_write(pTuner, 0x37, (unsigned char)(3910*freq_xtal/1000000) ); 395 | result &= fc2580_i2c_write(pTuner, 0x39, 0x80); 396 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x09); 397 | } 398 | else if(filter_bw == 8) 399 | { 400 | result &= fc2580_i2c_write(pTuner, 0x36, 0x18); 401 | result &= fc2580_i2c_write(pTuner, 0x37, (unsigned char)(3300*freq_xtal/1000000) ); 402 | result &= fc2580_i2c_write(pTuner, 0x39, 0x80); 403 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x09); 404 | } 405 | 406 | 407 | for(i=0; i<5; i++) 408 | { 409 | fc2580_wait_msec(pTuner, 5);//wait 5ms 410 | result &= fc2580_i2c_read(pTuner, 0x2F, &cal_mon); 411 | if( (cal_mon & 0xC0) != 0xC0) 412 | { 413 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x01); 414 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x09); 415 | } 416 | else 417 | break; 418 | } 419 | 420 | result &= fc2580_i2c_write(pTuner, 0x2E, 0x01); 421 | 422 | return result; 423 | } 424 | 425 | /*============================================================================== 426 | fc2580 RSSI function 427 | 428 | This function is a generic function which returns fc2580's 429 | 430 | current RSSI value. 431 | 432 | 433 | none 434 | 435 | 436 | int 437 | rssi : estimated input power. 438 | 439 | ==============================================================================*/ 440 | //int fc2580_get_rssi(void) { 441 | // 442 | // unsigned char s_lna, s_rfvga, s_cfs, s_ifvga; 443 | // int ofs_lna, ofs_rfvga, ofs_csf, ofs_ifvga, rssi; 444 | // 445 | // fc2580_i2c_read(0x71, &s_lna ); 446 | // fc2580_i2c_read(0x72, &s_rfvga ); 447 | // fc2580_i2c_read(0x73, &s_cfs ); 448 | // fc2580_i2c_read(0x74, &s_ifvga ); 449 | // 450 | // 451 | // ofs_lna = 452 | // (curr_band==FC2580_UHF_BAND)? 453 | // (s_lna==0)? 0 : 454 | // (s_lna==1)? -6 : 455 | // (s_lna==2)? -17 : 456 | // (s_lna==3)? -22 : -30 : 457 | // (curr_band==FC2580_VHF_BAND)? 458 | // (s_lna==0)? 0 : 459 | // (s_lna==1)? -6 : 460 | // (s_lna==2)? -19 : 461 | // (s_lna==3)? -24 : -32 : 462 | // (curr_band==FC2580_L_BAND)? 463 | // (s_lna==0)? 0 : 464 | // (s_lna==1)? -6 : 465 | // (s_lna==2)? -11 : 466 | // (s_lna==3)? -16 : -34 : 467 | // 0;//FC2580_NO_BAND 468 | // ofs_rfvga = -s_rfvga+((s_rfvga>=11)? 1 : 0) + ((s_rfvga>=18)? 1 : 0); 469 | // ofs_csf = -6*s_cfs; 470 | // ofs_ifvga = s_ifvga/4; 471 | // 472 | // return rssi = ofs_lna+ofs_rfvga+ofs_csf+ofs_ifvga+OFS_RSSI; 473 | // 474 | //} 475 | 476 | /*============================================================================== 477 | fc2580 Xtal frequency Setting 478 | 479 | This function is a generic function which sets 480 | 481 | the frequency of xtal. 482 | 483 | 484 | 485 | frequency 486 | frequency value of internal(external) Xtal(clock) in kHz unit. 487 | 488 | ==============================================================================*/ 489 | //void fc2580_set_freq_xtal(unsigned int frequency) { 490 | // 491 | // freq_xtal = frequency; 492 | // 493 | //} 494 | 495 | --------------------------------------------------------------------------------