initial commit

Signed-off-by: Steve Markgraf <steve@steve-m.de>
This commit is contained in:
Steve Markgraf 2012-10-14 18:03:51 +02:00
commit 4259747118
25 changed files with 4467 additions and 0 deletions

33
.gitignore vendored Normal file
View File

@ -0,0 +1,33 @@
Makefile
Makefile.in
.deps
.libs
*.o
*.lo
*.la
*.pc
aclocal.m4
acinclude.m4
aminclude.am
m4/*.m4
autom4te.cache
config.h*
config.sub
config.log
config.status
config.guess
configure
depcomp
missing
ltmain.sh
install-sh
stamp-h1
libtool
Doxyfile
.tarball-version
.version
.*.swp
doc/

3
AUTHORS Normal file
View File

@ -0,0 +1,3 @@
Steve Markgraf <steve@steve-m.de>
Eric Wild <la@tfc-server.de>
Dimitri Stolnikov <horiz0n@gmx.net>

124
CMakeLists.txt Normal file
View File

@ -0,0 +1,124 @@
# Copyright 2012 OSMOCOM Project
#
# This file is part of MiriSDR
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
########################################################################
# Project setup
########################################################################
cmake_minimum_required(VERSION 2.6)
project(mirisdr C)
#select the release build type by default to get optimization flags
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
message(STATUS "Build type not specified: defaulting to release.")
endif(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
########################################################################
# Compiler specific setup
########################################################################
if(CMAKE_COMPILER_IS_GNUCC AND NOT WIN32)
ADD_DEFINITIONS(-Wall)
ADD_DEFINITIONS(-Wextra)
ADD_DEFINITIONS(-Wno-unused)
ADD_DEFINITIONS(-Wsign-compare)
#http://gcc.gnu.org/wiki/Visibility
add_definitions(-fvisibility=hidden)
endif()
########################################################################
# Find build dependencies
########################################################################
find_package(PkgConfig)
find_package(LibUSB)
if(NOT LIBUSB_FOUND)
message(FATAL_ERROR "LibUSB 1.0 required to compile MiriSDR")
endif()
########################################################################
# Setup the include and linker paths
########################################################################
include_directories(
${CMAKE_SOURCE_DIR}/include
${LIBUSB_INCLUDE_DIR}
)
#link_directories(
# ...
#)
# Set component parameters
#set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE INTERNAL "" FORCE)
########################################################################
# Create uninstall target
########################################################################
configure_file(
${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
@ONLY)
add_custom_target(uninstall
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
########################################################################
# Add subdirectories
########################################################################
add_subdirectory(include)
add_subdirectory(src)
########################################################################
# Create Pkg Config File
########################################################################
FOREACH(inc ${LIBUSB_INCLUDE_DIR})
LIST(APPEND MIRISDR_PC_CFLAGS "-I${inc}")
ENDFOREACH(inc)
FOREACH(lib ${LIBUSB_LIBRARY_DIRS})
LIST(APPEND MIRISDR_PC_LIBS "-L${lib}")
ENDFOREACH(lib)
# use space-separation format for the pc file
STRING(REPLACE ";" " " MIRISDR_PC_CFLAGS "${MIRISDR_PC_CFLAGS}")
STRING(REPLACE ";" " " MIRISDR_PC_LIBS "${MIRISDR_PC_LIBS}")
# unset these vars to avoid hard-coded paths to cross environment
IF(CMAKE_CROSSCOMPILING)
UNSET(MIRISDR_PC_CFLAGS)
UNSET(MIRISDR_PC_LIBS)
ENDIF(CMAKE_CROSSCOMPILING)
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix \${prefix})
set(libdir \${exec_prefix}/lib)
set(includedir \${prefix}/include)
CONFIGURE_FILE(
${CMAKE_CURRENT_SOURCE_DIR}/libmirisdr.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libmirisdr.pc
@ONLY)
INSTALL(
FILES ${CMAKE_CURRENT_BINARY_DIR}/libmirisdr.pc
DESTINATION lib/pkgconfig
)

339
COPYING Normal file
View File

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

1716
Doxyfile.in Normal file

File diff suppressed because it is too large Load Diff

43
Makefile.am Normal file
View File

@ -0,0 +1,43 @@
AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6
ACLOCAL_AMFLAGS = -I m4
INCLUDES = $(all_includes) -I$(top_srcdir)/include
SUBDIRS = include src
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libosmosdr.pc
BUILT_SOURCES = $(top_srcdir)/.version
$(top_srcdir)/.version:
echo $(VERSION) > $@-t && mv $@-t $@
dist-hook:
echo $(VERSION) > $(distdir)/.tarball-version
EXTRA_DIST = git-version-gen
if HAVE_DOXYGEN
pkgdocdir=$(docdir)/$(PACKAGE)-$(VERSION)
doc_htmldir=$(pkgdocdir)/html
doc_html_DATA = $(top_builddir)/doc/html.tar
$(doc_html_DATA): $(top_builddir)/doc/html/index.html
cd $(top_builddir)/doc && tar cf html.tar html
$(top_builddir)/doc/html/index.html: $(SOURCES) Doxyfile
@rm -rf doc
mkdir -p doc
$(DOXYGEN) Doxyfile
install-data-hook:
cd $(DESTDIR)$(doc_htmldir) && tar xf html.tar --strip-components 1 && rm -f html.tar
uninstall-hook:
cd $(DESTDIR) && rm -rf $(doc_htmldir)
DX_CLEAN = doc/{html,latex}/* doc/html.tar
endif
MOSTLYCLEANFILES = $(DX_CLEAN)

2
README Normal file
View File

@ -0,0 +1,2 @@
For more information see:
http://sdr.osmocom.org

View File

@ -0,0 +1,28 @@
if(NOT LIBUSB_FOUND)
pkg_check_modules (LIBUSB_PKG libusb-1.0)
find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h
PATHS
${LIBUSB_PKG_INCLUDE_DIRS}
/usr/include/libusb-1.0
/usr/include
/usr/local/include
)
find_library(LIBUSB_LIBRARIES NAMES usb-1.0
PATHS
${LIBUSB_PKG_LIBRARY_DIRS}
/usr/lib
/usr/local/lib
)
if(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
set(LIBUSB_FOUND TRUE CACHE INTERNAL "libusb-1.0 found")
message(STATUS "Found libusb-1.0: ${LIBUSB_INCLUDE_DIR}, ${LIBUSB_LIBRARIES}")
else(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
set(LIBUSB_FOUND FALSE CACHE INTERNAL "libusb-1.0 found")
message(STATUS "libusb-1.0 not found.")
endif(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
mark_as_advanced(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARIES)
endif(NOT LIBUSB_FOUND)

View File

@ -0,0 +1,32 @@
# http://www.vtk.org/Wiki/CMake_FAQ#Can_I_do_.22make_uninstall.22_with_CMake.3F
IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"")
ENDIF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
STRING(REGEX REPLACE "\n" ";" files "${files}")
FOREACH(file ${files})
MESSAGE(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
IF(EXISTS "$ENV{DESTDIR}${file}")
EXEC_PROGRAM(
"@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
OUTPUT_VARIABLE rm_out
RETURN_VALUE rm_retval
)
IF(NOT "${rm_retval}" STREQUAL 0)
MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
ENDIF(NOT "${rm_retval}" STREQUAL 0)
ELSEIF(IS_SYMLINK "$ENV{DESTDIR}${file}")
EXEC_PROGRAM(
"@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
OUTPUT_VARIABLE rm_out
RETURN_VALUE rm_retval
)
IF(NOT "${rm_retval}" STREQUAL 0)
MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
ENDIF(NOT "${rm_retval}" STREQUAL 0)
ELSE(EXISTS "$ENV{DESTDIR}${file}")
MESSAGE(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
ENDIF(EXISTS "$ENV{DESTDIR}${file}")
ENDFOREACH(file)

62
configure.ac Normal file
View File

@ -0,0 +1,62 @@
AC_INIT([libmirisdr],
m4_esyscmd([./git-version-gen .tarball-version]),
[osmocom-sdr@lists.osmocom.org])
AM_INIT_AUTOMAKE([dist-bzip2])
dnl kernel style compile messages
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
dnl checks for programs
AC_PROG_MAKE_SET
AC_PROG_CC
AC_PROG_INSTALL
LT_INIT
AC_PROG_LIBTOOL
PKG_CHECK_MODULES(LIBUSB, libusb-1.0 >= 1.0)
LIBS="$LIBS $LIBUSB_LIBS"
CFLAGS="$CFLAGS $LIBUSB_CFLAGS"
AC_PATH_PROG(DOXYGEN,doxygen,false)
AM_CONDITIONAL(HAVE_DOXYGEN, test $DOXYGEN != false)
AC_CONFIG_MACRO_DIR([m4])
dnl checks for header files
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/types.h)
# pc variables
AC_SUBST(MIRISDR_PC_LIBS,["$LIBS"])
AC_SUBST(MIRISDR_PC_CFLAGS,["$CFLAGS"])
# The following test is taken from WebKit's webkit.m4
saved_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -fvisibility=hidden "
AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([char foo;])],
[ AC_MSG_RESULT([yes])
SYMBOL_VISIBILITY="-fvisibility=hidden"],
AC_MSG_RESULT([no]))
CFLAGS="$saved_CFLAGS"
AC_SUBST(SYMBOL_VISIBILITY)
AC_MSG_CHECKING(whether compiler understands -Wall)
old_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused -Wsign-compare"
AC_TRY_COMPILE([],[],
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no)
CFLAGS="$old_CFLAGS")
dnl Generate the output
AM_CONFIG_HEADER(config.h)
AC_OUTPUT(
libmirisdr.pc
include/Makefile
src/Makefile
Makefile
Doxyfile
)

151
git-version-gen Executable file
View File

@ -0,0 +1,151 @@
#!/bin/sh
# Print a version string.
scriptversion=2010-01-28.01
# Copyright (C) 2007-2010 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
# It may be run two ways:
# - from a git repository in which the "git describe" command below
# produces useful output (thus requiring at least one signed tag)
# - from a non-git-repo directory containing a .tarball-version file, which
# presumes this script is invoked like "./git-version-gen .tarball-version".
# In order to use intra-version strings in your project, you will need two
# separate generated version string files:
#
# .tarball-version - present only in a distribution tarball, and not in
# a checked-out repository. Created with contents that were learned at
# the last time autoconf was run, and used by git-version-gen. Must not
# be present in either $(srcdir) or $(builddir) for git-version-gen to
# give accurate answers during normal development with a checked out tree,
# but must be present in a tarball when there is no version control system.
# Therefore, it cannot be used in any dependencies. GNUmakefile has
# hooks to force a reconfigure at distribution time to get the value
# correct, without penalizing normal development with extra reconfigures.
#
# .version - present in a checked-out repository and in a distribution
# tarball. Usable in dependencies, particularly for files that don't
# want to depend on config.h but do want to track version changes.
# Delete this file prior to any autoconf run where you want to rebuild
# files to pick up a version string change; and leave it stale to
# minimize rebuild time after unrelated changes to configure sources.
#
# It is probably wise to add these two files to .gitignore, so that you
# don't accidentally commit either generated file.
#
# Use the following line in your configure.ac, so that $(VERSION) will
# automatically be up-to-date each time configure is run (and note that
# since configure.ac no longer includes a version string, Makefile rules
# should not depend on configure.ac for version updates).
#
# AC_INIT([GNU project],
# m4_esyscmd([build-aux/git-version-gen .tarball-version]),
# [bug-project@example])
#
# Then use the following lines in your Makefile.am, so that .version
# will be present for dependencies, and so that .tarball-version will
# exist in distribution tarballs.
#
# BUILT_SOURCES = $(top_srcdir)/.version
# $(top_srcdir)/.version:
# echo $(VERSION) > $@-t && mv $@-t $@
# dist-hook:
# echo $(VERSION) > $(distdir)/.tarball-version
case $# in
1) ;;
*) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
esac
tarball_version_file=$1
nl='
'
# First see if there is a tarball-only version file.
# then try "git describe", then default.
if test -f $tarball_version_file
then
v=`cat $tarball_version_file` || exit 1
case $v in
*$nl*) v= ;; # reject multi-line output
[0-9]*) ;;
*) v= ;;
esac
test -z "$v" \
&& echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
fi
if test -n "$v"
then
: # use $v
elif
v=`git describe --abbrev=4 --match='v*' HEAD 2>/dev/null \
|| git describe --abbrev=4 HEAD 2>/dev/null` \
&& case $v in
[0-9]*) ;;
v[0-9]*) ;;
*) (exit 1) ;;
esac
then
# Is this a new git that lists number of commits since the last
# tag or the previous older version that did not?
# Newer: v6.10-77-g0f8faeb
# Older: v6.10-g0f8faeb
case $v in
*-*-*) : git describe is okay three part flavor ;;
*-*)
: git describe is older two part flavor
# Recreate the number of commits and rewrite such that the
# result is the same as if we were using the newer version
# of git describe.
vtag=`echo "$v" | sed 's/-.*//'`
numcommits=`git rev-list "$vtag"..HEAD | wc -l`
v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
;;
esac
# Change the first '-' to a '.', so version-comparing tools work properly.
# Remove the "g" in git describe's output string, to save a byte.
v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
else
v=UNKNOWN
fi
v=`echo "$v" |sed 's/^v//'`
# Don't declare a version "dirty" merely because a time stamp has changed.
git status > /dev/null 2>&1
dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
case "$dirty" in
'') ;;
*) # Append the suffix only if there isn't one already.
case $v in
*-dirty) ;;
*) v="$v-dirty" ;;
esac ;;
esac
# Omit the trailing newline, so that m4_esyscmd can use the result directly.
echo "$v" | tr -d '\012'
# Local variables:
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-end: "$"
# End:

27
include/CMakeLists.txt Normal file
View File

@ -0,0 +1,27 @@
# Copyright 2012 OSMOCOM Project
#
# This file is part of MiriSDR
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
########################################################################
# Install public header files
########################################################################
install(FILES
mirisdr.h
mirisdr_export.h
DESTINATION include
)

5
include/Makefile.am Normal file
View File

@ -0,0 +1,5 @@
mirisdr_HEADERS = mirisdr.h mirisdr_export.h
noinst_HEADERS = mirisdr_reg.h tuner_msi001.h
mirisdrdir = $(includedir)

186
include/mirisdr.h Normal file
View File

@ -0,0 +1,186 @@
/*
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
* Copyright (C) 2012 by Dimitri Stolnikov <horiz0n@gmx.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MIRISDR_H
#define __MIRISDR_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <mirisdr_export.h>
typedef struct mirisdr_dev mirisdr_dev_t;
MIRISDR_API uint32_t mirisdr_get_device_count(void);
MIRISDR_API const char* mirisdr_get_device_name(uint32_t index);
/*!
* Get USB device strings.
*
* NOTE: The string arguments must provide space for up to 256 bytes.
*
* \param index the device index
* \param manufact manufacturer name, may be NULL
* \param product product name, may be NULL
* \param serial serial number, may be NULL
* \return 0 on success
*/
MIRISDR_API int mirisdr_get_device_usb_strings(uint32_t index,
char *manufact,
char *product,
char *serial);
MIRISDR_API int mirisdr_open(mirisdr_dev_t **dev, uint32_t index);
MIRISDR_API int mirisdr_close(mirisdr_dev_t *dev);
/* configuration functions */
/*!
* Get USB device strings.
*
* NOTE: The string arguments must provide space for up to 256 bytes.
*
* \param dev the device handle given by mirisdr_open()
* \param manufact manufacturer name, may be NULL
* \param product product name, may be NULL
* \param serial serial number, may be NULL
* \return 0 on success
*/
MIRISDR_API int mirisdr_get_usb_strings(mirisdr_dev_t *dev, char *manufact,
char *product, char *serial);
/*!
* Set the frequency the device is tuned to.
*
* \param dev the device handle given by mirisdr_open()
* \param freq frequency in Hz the device should be tuned to
* \return 0 on error, frequency in Hz otherwise
*/
MIRISDR_API int mirisdr_set_center_freq(mirisdr_dev_t *dev, uint32_t freq);
/*!
* Get the actual frequency the device is tuned to.
*
* \param dev the device handle given by mirisdr_open()
* \return 0 on error, frequency in Hz otherwise
*/
MIRISDR_API uint32_t mirisdr_get_center_freq(mirisdr_dev_t *dev);
/*!
* Get a list of gains supported by the tuner.
*
* NOTE: The gains argument must be preallocated by the caller. If NULL is
* being given instead, the number of available gain values will be returned.
*
* \param dev the device handle given by mirisdr_open()
* \param gains array of gain values. In tenths of a dB, 115 means 11.5 dB.
* \return <= 0 on error, number of available (returned) gain values otherwise
*/
MIRISDR_API int mirisdr_get_tuner_gains(mirisdr_dev_t *dev, int *gains);
/*!
* Set the gain for the device.
* Manual gain mode must be enabled for this to work.
*
* Valid gain values may be queried with \ref mirisdr_get_tuner_gains function.
*
* \param dev the device handle given by mirisdr_open()
* \param gain in tenths of a dB, 115 means 11.5 dB.
* \return 0 on success
*/
MIRISDR_API int mirisdr_set_tuner_gain(mirisdr_dev_t *dev, int gain);
/*!
* Get actual gain the device is configured to.
*
* \param dev the device handle given by mirisdr_open()
* \return 0 on error, gain in tenths of a dB, 115 means 11.5 dB.
*/
MIRISDR_API int mirisdr_get_tuner_gain(mirisdr_dev_t *dev);
/*!
* Set the gain mode (automatic/manual) for the device.
* Manual gain mode must be enabled for the gain setter function to work.
*
* \param dev the device handle given by mirisdr_open()
* \param manual gain mode, 1 means manual gain mode shall be enabled.
* \return 0 on success
*/
MIRISDR_API int mirisdr_set_tuner_gain_mode(mirisdr_dev_t *dev, int manual);
/*!
* Set the sample rate for the device.
*
* \param dev the device handle given by mirisdr_open()
* \param rate the sample rate in Hz
* \return 0 on success
*/
MIRISDR_API int mirisdr_set_sample_rate(mirisdr_dev_t *dev, uint32_t rate);
/*!
* Get the sample rate the device is configured to.
*
* \param dev the device handle given by mirisdr_open()
* \return 0 on error, sample rate in Hz otherwise
*/
MIRISDR_API uint32_t mirisdr_get_sample_rate(mirisdr_dev_t *dev);
/* streaming functions */
MIRISDR_API int mirisdr_reset_buffer(mirisdr_dev_t *dev);
MIRISDR_API int mirisdr_read_sync(mirisdr_dev_t *dev, void *buf, int len, int *n_read);
typedef void(*mirisdr_read_async_cb_t)(unsigned char *buf, uint32_t len, void *ctx);
/*!
* Read samples from the device asynchronously. This function will block until
* it is being canceled using mirisdr_cancel_async()
*
* \param dev the device handle given by mirisdr_open()
* \param cb callback function to return received samples
* \param ctx user specific context to pass via the callback function
* \param buf_num optional buffer count, buf_num * buf_len = overall buffer size
* set to 0 for default buffer count (32)
* \param buf_len optional buffer length, must be multiple of 512,
* set to 0 for default buffer length (16 * 32 * 512)
* \return 0 on success
*/
MIRISDR_API int mirisdr_read_async(mirisdr_dev_t *dev,
mirisdr_read_async_cb_t cb,
void *ctx,
uint32_t buf_num,
uint32_t buf_len);
/*!
* Cancel all pending asynchronous operations on the device.
*
* \param dev the device handle given by mirisdr_open()
* \return 0 on success
*/
MIRISDR_API int mirisdr_cancel_async(mirisdr_dev_t *dev);
#ifdef __cplusplus
}
#endif
#endif /* __MIRISDR_H */

46
include/mirisdr_export.h Normal file
View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2012 by Hoernchen <la@tfc-server.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MIRISDR_EXPORT_H
#define __MIRISDR_EXPORT_H
#if defined __GNUC__
# if __GNUC__ >= 4
# define __SDR_EXPORT __attribute__((visibility("default")))
# define __SDR_IMPORT __attribute__((visibility("default")))
# else
# define __SDR_EXPORT
# define __SDR_IMPORT
# endif
#elif _MSC_VER
# define __SDR_EXPORT __declspec(dllexport)
# define __SDR_IMPORT __declspec(dllimport)
#else
# define __SDR_EXPORT
# define __SDR_IMPORT
#endif
#ifndef mirisdr_STATIC
# ifdef mirisdr_EXPORTS
# define MIRISDR_API __SDR_EXPORT
# else
# define MIRISDR_API __SDR_IMPORT
# endif
#else
#define MIRISDR_API
#endif
#endif /* __MIRISDR_EXPORT_H */

6
include/mirisdr_reg.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __REG_H
#define __REG_H
int mirisdr_reg_write_fn(void *dev, uint8_t reg, uint32_t val);
#endif

97
include/tuner_msi001.h Normal file
View File

@ -0,0 +1,97 @@
/*
* Mirics MSi001 tuner driver
*
* Copyright (C) 2012 by Eric Wild <la@tfc-server.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#define c(v) ((v<0)?printf("err! %s %s:%i\n", #v, __FILE__, __LINE__):0)
#define MHZ(x) ((x)*1000*1000)
#define KHZ(x) ((x)*1000)
enum mode{
AM_MODE1=0,
AM_MODE2=1,
VHF_MODE=2,
B3_MODE=3,
B45_MODE=4,
BL_MODE=5
};
enum xtal{
XTAL19_2Mz=0,
XTAL22M=1,
XTAL24_576M=2,
XTAL26M=3,
XTAL38_4M=4
};
enum am_mixgainred{
r1_mixbu_p0_0 = 0,
r1_mixbu_p0_6 = 1,
r1_mixbu_p0_12 = 2,
r1_mixbu_p0_18 = 3,
r1_mixbu_p1_24 = 3
};
struct state{
enum mode m;
enum xtal x;
double freq_hz;
uint32_t minus_bbgain;
enum am_mixgainred am_mixgainred;
uint32_t mixl;
uint32_t lnagr;
uint32_t reg[6];
};
struct r0_modes_{
char* bits;
unsigned char value;
char* name;
unsigned char lodiv;
};
struct iffreqs_{
uint32_t xtalfreq;
uint32_t fref1;
uint32_t fif1;
};
int msi001_init(void *dev, uint32_t freq);
//######
#define R0_FIL_MODE_SH 12
#define R0_FIL_BW_SH 14
#define R0_XTAL_SEL_SH 17
#define R0_IF_LPMODE_SH 20
#define R0_VCO_LPMODE_SH 23
#define FIL_MODE_450K_IF 0x2
#define FIL_MODE_ZERO_IF 0x3
//######
#define R2_INT_SH 16
//######
#define R1_MIXBU_SH 10
#define R1_MIXL_SH 12
#define R1_LNAGR_SH 13
#define R1_DCCAL_SH 14
#define R1_DCCAL 0x05 // continuous, no speedup
//######

11
libmirisdr.pc.in Normal file
View File

@ -0,0 +1,11 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: MiriSDR Library
Description: C Utility Library
Version: @VERSION@
Cflags: -I${includedir}/ @MIRISDR_PC_CFLAGS@
Libs: -L${libdir} -lmirisdr -lusb-1.0
Libs.private: @MIRISDR_PC_LIBS@

2
m4/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/libtool.m4
/lt*.m4

22
mirisdr.rules Normal file
View File

@ -0,0 +1,22 @@
#
# Copyright 2012 Osmocom MiriSDR project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Mirics MSi2500 default (e.g. VTX3D card)
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1df7", ATTRS{idProduct}=="2500", MODE:="0666"
# IO-DATA GV-TV100 stick
SUBSYSTEMS=="usb", ATTRS{idVendor}=="04bb", ATTRS{idProduct}=="0537", MODE:="0666"

71
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,71 @@
# Copyright 2012 OSMOCOM Project
#
# This file is part of MiriSDR
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
########################################################################
# Setup library
########################################################################
add_library(mirisdr_shared SHARED
libmirisdr.c
tuner_msi001.c
)
target_link_libraries(mirisdr_shared
${LIBUSB_LIBRARIES}
)
set_target_properties(mirisdr_shared PROPERTIES DEFINE_SYMBOL "mirisdr_EXPORTS")
set_target_properties(mirisdr_shared PROPERTIES OUTPUT_NAME mirisdr)
set_target_properties(mirisdr_shared PROPERTIES SOVERSION 0 VERSION 0.0.0)
add_library(mirisdr_static STATIC
libmirisdr.c
tuner_msi001.c
)
target_link_libraries(mirisdr_static
${LIBUSB_LIBRARIES}
)
set_property(TARGET mirisdr_static APPEND PROPERTY COMPILE_DEFINITIONS "mirisdr_STATIC" )
if(NOT WIN32)
# Force same library filename for static and shared variants of the library
set_target_properties(mirisdr_static PROPERTIES OUTPUT_NAME mirisdr)
endif()
########################################################################
# Build utility
########################################################################
add_executable(miri_sdr miri_sdr.c)
target_link_libraries(miri_sdr mirisdr_static
${LIBUSB_LIBRARIES}
)
if(WIN32)
set_property(TARGET miri_sdr APPEND PROPERTY COMPILE_DEFINITIONS "mirisdr_STATIC" )
endif()
########################################################################
# Install built library files & utilities
########################################################################
install(TARGETS mirisdr_shared mirisdr_static miri_sdr
LIBRARY DESTINATION lib${LIB_SUFFIX} # .so/.dylib file
ARCHIVE DESTINATION lib${LIB_SUFFIX} # .lib file
RUNTIME DESTINATION bin # .dll file
)

16
src/Makefile.am Normal file
View File

@ -0,0 +1,16 @@
# This is _NOT_ the library release version, it's an API version.
# Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification
LIBVERSION=0:0:0
INCLUDES = $(all_includes) -I$(top_srcdir)/include
AM_CFLAGS = ${CFLAGS} -fPIC ${SYMBOL_VISIBILITY}
lib_LTLIBRARIES = libmirisdr.la
libmirisdr_la_SOURCES = libmirisdr.c
libmirisdr_la_LDFLAGS = -version-info $(LIBVERSION)
bin_PROGRAMS = miri_sdr
miri_sdr_SOURCES = miri_sdr.c
miri_sdr_LDADD = libmirisdr.la

959
src/libmirisdr.c Normal file
View File

@ -0,0 +1,959 @@
/*
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
* Copyright (C) 2012 by Dimitri Stolnikov <horiz0n@gmx.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef _WIN32
#include <unistd.h>
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
//#define USE_SSE
#ifdef USE_SSE
#include <immintrin.h>
#endif
#include <libusb.h>
/*
* All libusb callback functions should be marked with the LIBUSB_CALL macro
* to ensure that they are compiled with the same calling convention as libusb.
*
* If the macro isn't available in older libusb versions, we simply define it.
*/
#ifndef LIBUSB_CALL
#define LIBUSB_CALL
#endif
#include "mirisdr.h"
#include "mirisdr_reg.h"
#include "tuner_msi001.h"
typedef struct mirisdr_tuner {
/* tuner interface */
int (*init)(void *);
int (*exit)(void *);
int (*set_freq)(void *, uint32_t freq /* Hz */);
int (*set_bw)(void *, int bw /* Hz */);
int (*set_gain)(void *, int gain /* dB */);
int (*set_gain_mode)(void *, int manual);
} mirisdr_tuner_t;
enum mirisdr_async_status {
mirisdr_INACTIVE = 0,
mirisdr_CANCELING,
mirisdr_RUNNING
};
struct mirisdr_dev {
libusb_context *ctx;
struct libusb_device_handle *devh;
uint32_t xfer_buf_num;
uint32_t xfer_iso_pack;
uint32_t xfer_buf_len;
struct libusb_transfer **xfer;
unsigned char **xfer_buf;
mirisdr_read_async_cb_t cb;
void *cb_ctx;
enum mirisdr_async_status async_status;
/* adc context */
uint32_t rate; /* Hz */
uint32_t adc_clock; /* Hz */
/* tuner context */
mirisdr_tuner_t *tuner;
uint32_t freq; /* Hz */
int gain; /* dB */
/* samples context */
int headerflag;
uint32_t addr;
};
typedef struct mirisdr_dongle {
uint16_t vid;
uint16_t pid;
const char *name;
} mirisdr_dongle_t;
static mirisdr_dongle_t known_devices[] = {
{ 0x1df7, 0x2500, "Mirics MSi2500 default (e.g. VTX3D card)" },
{ 0x04bb, 0x0537, "IO-DATA GV-TV100 stick" }
};
#define DEFAULT_BUF_NUMBER 32
#define DEFAULT_ISO_PACKETS 8
#define DEFAULT_BUF_LENGTH (3072 * DEFAULT_ISO_PACKETS)
#define DEF_ADC_FREQ 4000000
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
#define FUNC(group, function) ((group << 8) | function)
#define CTRL_TIMEOUT 300
#define ISO_TIMEOUT 0
int _msi001_init(void *dev) {
return 0;
}
int msi001_exit(void *dev) {
return 0;
}
int msi001_set_freq(void *dev, uint32_t freq) {
return msi001_init(dev, freq);
}
int msi001_set_bw(void *dev, int bw) {
return 0;
}
int msi001_set_lna_gain(void *dev, int32_t gain) {
return 0;
}
int msi001_mixer_gain_set(void *dev, int8_t gain) {
return 0;
}
int msi001_set_enh_gain(void *dev, int32_t gain) {
return 0;
}
int msi001_set_gain(void *dev, int gain) {
return 0;
}
int msi001_set_gain_mode(void *dev, int manual) {
return 0;
}
static mirisdr_tuner_t tuner = {
_msi001_init, msi001_exit,
msi001_set_freq,
msi001_set_bw, msi001_set_gain, msi001_set_gain_mode
};
int msi2500_write_reg(mirisdr_dev_t *dev, uint8_t reg, uint32_t val)
{
uint16_t wValue = (val & 0xff) << 8 | reg;
uint16_t wIndex = (val >> 8) & 0xffff;
int r = libusb_control_transfer(dev->devh, 0x42, 0x41, wValue, wIndex, NULL, 0, CTRL_TIMEOUT);
// fprintf(stderr, "%s: reg %02x -> %06x\n", __FUNCTION__, reg, val);
return r;
}
void mirisdr_init_baseband(mirisdr_dev_t *dev)
{
/* TODO figure out what that does and why it's needed */
libusb_control_transfer(dev->devh, 0x42, 0x43, 0x0, 0x0, NULL, 0, CTRL_TIMEOUT);
/* initialisation */
msi2500_write_reg(dev, 0x05, 0x00000c);
msi2500_write_reg(dev, 0x00, 0x000200);
msi2500_write_reg(dev, 0x02, 0x004801);
/* IF filter bw + compression scheme? */
msi2500_write_reg(dev, 0x07, 0x0000a5);
/* sample rate = 9.14 MS/s */
msi2500_write_reg(dev, 0x04, 0x04923d);
msi2500_write_reg(dev, 0x03, 0x01c907);
//6M sample rate
// msi2500_write_reg(dev, 0x04, 0x9220b);
// msi2500_write_reg(dev, 0x03, 0x14a0b);
// doesn't work yet
// fprintf(stderr, "setting fs\n");
// mirisdr_set_samp_rate(dev, 8000000);
msi2500_write_reg(dev, 0x13, 0x006b46);
msi2500_write_reg(dev, 0x14, 0x0000f5);
msi2500_write_reg(dev, 0x12, 0x802800);
msi2500_write_reg(dev, 0x29, 0x032201);
/* enable lock led, deselect eeprom CS */
msi2500_write_reg(dev, 0x08, 0x006680);
/* tuner init */
msi2500_write_reg(dev, 0x09, 0x0094b3);
msi2500_write_reg(dev, 0x09, 0x00800e);
msi2500_write_reg(dev, 0x09, 0x200256);
/* set gains, TODO remove, use tuner driver */
msi2500_write_reg(dev, 0x09, 0x014281);
}
int mirisdr_deinit_baseband(mirisdr_dev_t *dev)
{
int r = 0;
if (!dev)
return -1;
if (dev->tuner && dev->tuner->exit) {
// r = dev->tuner->exit(dev); /* deinitialize tuner */
}
/* disable lock led */
msi2500_write_reg(dev, 0x08, 0x006600);
return r;
}
int mirisdr_get_usb_strings(mirisdr_dev_t *dev, char *manufact, char *product,
char *serial)
{
struct libusb_device_descriptor dd;
libusb_device *device = NULL;
const int buf_max = 256;
int r = 0;
if (!dev || !dev->devh)
return -1;
device = libusb_get_device(dev->devh);
r = libusb_get_device_descriptor(device, &dd);
if (r < 0)
return -1;
if (manufact) {
memset(manufact, 0, buf_max);
libusb_get_string_descriptor_ascii(dev->devh, dd.iManufacturer,
(unsigned char *)manufact,
buf_max);
}
if (product) {
memset(product, 0, buf_max);
libusb_get_string_descriptor_ascii(dev->devh, dd.iProduct,
(unsigned char *)product,
buf_max);
}
if (serial) {
memset(serial, 0, buf_max);
libusb_get_string_descriptor_ascii(dev->devh, dd.iSerialNumber,
(unsigned char *)serial,
buf_max);
}
return 0;
}
int mirisdr_set_center_freq(mirisdr_dev_t *dev, uint32_t freq)
{
int r = -2;
if (!dev || !dev->tuner)
return -1;
if (dev->tuner->set_freq)
r = dev->tuner->set_freq(dev, freq);
if (!r)
dev->freq = freq;
else
dev->freq = 0;
return r;
}
uint32_t mirisdr_get_center_freq(mirisdr_dev_t *dev)
{
if (!dev || !dev->tuner)
return 0;
return dev->freq;
}
int mirisdr_get_tuner_gains(mirisdr_dev_t *dev, int *gains)
{
const int msi001_gains[] = { -10, 15, 40, 65, 90, 115, 140, 165, 190, 215,
240, 290, 340, 420, 430, 450, 470, 490 };
int len = sizeof(msi001_gains);
if (!dev)
return -1;
if (!gains) { /* no buffer provided, just return the count */
return len / sizeof(int);
} else {
if (len)
memcpy(gains, msi001_gains, len);
return len / sizeof(int);
}
}
int mirisdr_set_tuner_gain(mirisdr_dev_t *dev, int gain)
{
int r = -2;
if (!dev || !dev->tuner)
return -1;
if (dev->tuner->set_gain)
r = dev->tuner->set_gain((void *)dev, gain);
if (!r)
dev->gain = gain;
else
dev->gain = 0;
return r;
}
int mirisdr_get_tuner_gain(mirisdr_dev_t *dev)
{
if (!dev || !dev->tuner)
return 0;
return dev->gain;
}
int mirisdr_set_tuner_gain_mode(mirisdr_dev_t *dev, int mode)
{
int r = -2;
if (!dev || !dev->tuner)
return -1;
if (dev->tuner->set_gain_mode)
r = dev->tuner->set_gain_mode((void *)dev, mode);
return r;
}
int mirisdr_set_tuner_lna_gain(mirisdr_dev_t *dev, int gain)
{
return 0;
}
int mirisdr_set_tuner_mixer_gain(mirisdr_dev_t *dev, int gain)
{
return 0;
}
int mirisdr_set_tuner_mixer_enh(mirisdr_dev_t *dev, int enh)
{
return 0;
}
int mirisdr_set_tuner_if_gain(mirisdr_dev_t *dev, int stage, int gain)
{
return 0;
}
int mirisdr_set_sample_rate(mirisdr_dev_t *dev, uint32_t samp_rate)
{
int n;
int r = 0;
if (!dev)
return -1;
/* TODO */
// mirisdr_set_samp_rate(dev, samp_rate);
if (r >= 0) {
if (dev->tuner && dev->tuner->set_bw)
dev->tuner->set_bw(dev, samp_rate);
dev->rate = samp_rate;
} else {
dev->rate = 0;
}
return r;
}
uint32_t mirisdr_get_sample_rate(mirisdr_dev_t *dev)
{
if (!dev)
return 0;
return dev->rate;
}
static mirisdr_dongle_t *find_known_device(uint16_t vid, uint16_t pid)
{
unsigned int i;
mirisdr_dongle_t *device = NULL;
for (i = 0; i < sizeof(known_devices)/sizeof(mirisdr_dongle_t); i++ ) {
if (known_devices[i].vid == vid && known_devices[i].pid == pid) {
device = &known_devices[i];
break;
}
}
return device;
}
uint32_t mirisdr_get_device_count(void)
{
int i;
libusb_context *ctx;
libusb_device **list;
struct libusb_device_descriptor dd;
uint32_t device_count = 0;
ssize_t cnt;
libusb_init(&ctx);
cnt = libusb_get_device_list(ctx, &list);
for (i = 0; i < cnt; i++) {
libusb_get_device_descriptor(list[i], &dd);
if (find_known_device(dd.idVendor, dd.idProduct))
device_count++;
}
libusb_free_device_list(list, 1);
libusb_exit(ctx);
return device_count;
}
const char *mirisdr_get_device_name(uint32_t index)
{
int i;
libusb_context *ctx;
libusb_device **list;
struct libusb_device_descriptor dd;
mirisdr_dongle_t *device = NULL;
uint32_t device_count = 0;
ssize_t cnt;
libusb_init(&ctx);
cnt = libusb_get_device_list(ctx, &list);
for (i = 0; i < cnt; i++) {
libusb_get_device_descriptor(list[i], &dd);
device = find_known_device(dd.idVendor, dd.idProduct);
if (device) {
device_count++;
if (index == device_count - 1)
break;
device = NULL;
}
}
libusb_free_device_list(list, 1);
libusb_exit(ctx);
if (device)
return device->name;
else
return "";
}
int mirisdr_get_device_usb_strings(uint32_t index, char *manufact,
char *product, char *serial)
{
int r = -2;
int i;
libusb_context *ctx;
libusb_device **list;
struct libusb_device_descriptor dd;
mirisdr_dev_t devt;
uint32_t device_count = 0;
ssize_t cnt;
libusb_init(&ctx);
cnt = libusb_get_device_list(ctx, &list);
for (i = 0; i < cnt; i++) {
libusb_get_device_descriptor(list[i], &dd);
if (find_known_device(dd.idVendor, dd.idProduct))
device_count++;
if (index == device_count - 1) {
r = libusb_open(list[i], &devt.devh);
if (!r) {
r = mirisdr_get_usb_strings(&devt,
manufact,
product,
serial);
libusb_close(devt.devh);
}
break;
}
}
libusb_free_device_list(list, 1);
libusb_exit(ctx);
return r;
}
int mirisdr_open(mirisdr_dev_t **out_dev, uint32_t index)
{
int r;
int i;
libusb_device **list;
mirisdr_dev_t *dev = NULL;
libusb_device *device = NULL;
uint32_t device_count = 0;
struct libusb_device_descriptor dd;
ssize_t cnt;
dev = malloc(sizeof(mirisdr_dev_t));
if (NULL == dev)
return -ENOMEM;
memset(dev, 0, sizeof(mirisdr_dev_t));
libusb_init(&dev->ctx);
cnt = libusb_get_device_list(dev->ctx, &list);
for (i = 0; i < cnt; i++) {
device = list[i];
libusb_get_device_descriptor(list[i], &dd);
if (find_known_device(dd.idVendor, dd.idProduct))
device_count++;
if (index == device_count - 1)
break;
device = NULL;
}
if (!device) {
r = -1;
goto err;
}
r = libusb_open(device, &dev->devh);
if (r < 0) {
libusb_free_device_list(list, 1);
fprintf(stderr, "usb_open error %d\n", r);
goto err;
}
libusb_free_device_list(list, 1);
r = libusb_claim_interface(dev->devh, 0);
if (r < 0) {
fprintf(stderr, "usb_claim_interface error %d\n", r);
goto err;
}
dev->adc_clock = DEF_ADC_FREQ;
mirisdr_init_baseband(dev);
dev->tuner = &tuner; /* so far we have only one tuner */
if (dev->tuner->init) {
r = dev->tuner->init(dev);
}
r = libusb_set_interface_alt_setting(dev->devh, 0, 1);
*out_dev = dev;
return 0;
err:
if (dev) {
if (dev->ctx)
libusb_exit(dev->ctx);
free(dev);
}
return r;
}
int mirisdr_close(mirisdr_dev_t *dev)
{
if (!dev)
return -1;
mirisdr_deinit_baseband(dev);
libusb_release_interface(dev->devh, 0);
libusb_close(dev->devh);
libusb_exit(dev->ctx);
free(dev);
return 0;
}
int mirisdr_reset_buffer(mirisdr_dev_t *dev)
{
if (!dev)
return -1;
/* TODO: implement */
return 0;
}
int mirisdr_read_sync(mirisdr_dev_t *dev, void *buf, int len, int *n_read)
{
if (!dev)
return -1;
// return libusb_bulk_transfer(dev->devh, 0x86, buf, len, n_read, BULK_TIMEOUT);
return -1;
}
void hexdump(uint8_t *inbuf, int cnt)
{
int i;
for (i = 0; i < cnt; i++) {
printf("%02x ", inbuf[i]);
}
printf("\n");
}
int mirisdr_convert_samples(mirisdr_dev_t *dev, unsigned char* inbuf, int16_t *outsamples, int length)
{
int i, j, k, l, block;
uint32_t offs;
uint8_t *ip;
uint32_t address;
int op = 0;
ip = inbuf;
block = length / 1024;
while (block--) {
/* parse header */
#if 1
address = ip[1] + (ip[2] << 8) + (ip[3] << 16);
if (address != dev->addr)
fprintf(stderr, "Lost samples!\n");
dev->addr = address + (ip[0] >> 7) + 1;
if (((ip[5] & 0x40) && dev->headerflag)) {
hexdump(ip, 16);
dev->headerflag = 0;
} else if ((!(ip[5] & 0x40) && !dev->headerflag)) {
hexdump(ip, 16);
dev->headerflag = 1;
}
#endif
/* skip header */
ip += 16;
/* 16-sample-ips per block */
k = 6;
while (k--) {
uint32_t flag;
for (j = 0; j < 16; j++) {
for (i = 0; i < 10; i += 5) {
outsamples[op++] = (ip[i+0] << 6) | ((ip[i+1] & 0x03) << 14);
outsamples[op++] = ((ip[i+1] & 0xfc) << 4) | ((ip[i+2] & 0x0f) << 12);
outsamples[op++] = ((ip[i+2] & 0xf0) << 2) | ((ip[i+3] & 0x3f) << 10);
outsamples[op++] = (ip[i+3] & 0xc0) | (ip[i+4] << 8);
}
/* 10 bytes per 8 samples */
ip += 10;
}
flag = *(uint32_t*)ip;
flag = 0;
for (j = 0; j < 16; j++) {
#ifdef USE_SSE
/* Hoernchen's SSE accelerated version */
__m128i* addr = (__m128i*)&(outsamples[op-128+j*8+0]);
__m128i v;
switch(flag & 0x3) {
case 0:
v = _mm_loadu_si128(addr);
v = _mm_srai_epi16(v, 2);
_mm_storeu_si128(addr, v);
break;
case 1:
v = _mm_loadu_si128(addr);
v = _mm_srai_epi16(v, 1);
_mm_storeu_si128(addr, v);
break;
case 2:
case 3:
break;
}
#else
switch (flag & 0x03) {
case 0:
outsamples[op-128+j*8+0] >>= 2;
outsamples[op-128+j*8+1] >>= 2;
outsamples[op-128+j*8+2] >>= 2;
outsamples[op-128+j*8+3] >>= 2;
outsamples[op-128+j*8+4] >>= 2;
outsamples[op-128+j*8+5] >>= 2;
outsamples[op-128+j*8+6] >>= 2;
outsamples[op-128+j*8+7] >>= 2;
break;
case 1:
outsamples[op-128+j*8+0] >>= 1;
outsamples[op-128+j*8+1] >>= 1;
outsamples[op-128+j*8+2] >>= 1;
outsamples[op-128+j*8+3] >>= 1;
outsamples[op-128+j*8+4] >>= 1;
outsamples[op-128+j*8+5] >>= 1;
outsamples[op-128+j*8+6] >>= 1;
outsamples[op-128+j*8+7] >>= 1;
break;
case 2:
case 3:
break;
}
#endif
flag >>= 2;
}
/* flagbytes */
ip += 4;
}
ip += 24;
}
return op;
// fwrite(outsamples, op * sizeof(uint16_t) , 1, file);
}
static void LIBUSB_CALL _libusb_callback(struct libusb_transfer *xfer)
{
int i, len, total_len = 0;
static unsigned char* iso_packet_buf;
mirisdr_dev_t *dev = (mirisdr_dev_t *)xfer->user_data;
int16_t outsamples[768 * 3 * 8];
// if (xfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
for (i = 0; i < xfer->num_iso_packets; i++) {
struct libusb_iso_packet_descriptor *pack = &xfer->iso_packet_desc[i];
if (pack->status != LIBUSB_TRANSFER_COMPLETED) {
// fprintf(stderr, "transfer status: %d\n", xfer->status);
// mirisdr_cancel_async(dev); /* abort async loop */
}
if (pack->actual_length > 0) {
iso_packet_buf = libusb_get_iso_packet_buffer_simple(xfer, i);
if (iso_packet_buf) {
len = mirisdr_convert_samples(dev, iso_packet_buf, outsamples + total_len, pack->actual_length);
total_len += len;
}
// if (pack->actual_length != 3072)
// fprintf(stderr, "pack%u length:%u, actual_length:%u\n", i, pack->length, pack->actual_length);
}
}
if (dev->cb)
dev->cb((uint8_t*)outsamples, total_len * sizeof(int16_t), dev->cb_ctx);
/* resubmit transfer */
if (libusb_submit_transfer(xfer) < 0) {
fprintf(stderr, "error re-submitting URB\n");
exit(1);
}
}
static int _mirisdr_alloc_async_buffers(mirisdr_dev_t *dev)
{
unsigned int i;
if (!dev)
return -1;
if (!dev->xfer) {
dev->xfer = malloc(dev->xfer_buf_num *
sizeof(struct libusb_transfer *));
for(i = 0; i < dev->xfer_buf_num; ++i)
dev->xfer[i] = libusb_alloc_transfer(dev->xfer_iso_pack);
}
if (!dev->xfer_buf) {
dev->xfer_buf = malloc(dev->xfer_buf_num *
sizeof(unsigned char *));
for(i = 0; i < dev->xfer_buf_num; ++i)
dev->xfer_buf[i] = malloc(dev->xfer_buf_len);
}
printf("%s\n", __FUNCTION__);
return 0;
}
static int _mirisdr_free_async_buffers(mirisdr_dev_t *dev)
{
unsigned int i;
if (!dev)
return -1;
if (dev->xfer) {
for(i = 0; i < dev->xfer_buf_num; ++i) {
if (dev->xfer[i]) {
libusb_free_transfer(dev->xfer[i]);
}
}
free(dev->xfer);
dev->xfer = NULL;
}
if (dev->xfer_buf) {
for(i = 0; i < dev->xfer_buf_num; ++i) {
if (dev->xfer_buf[i])
free(dev->xfer_buf[i]);
}
free(dev->xfer_buf);
dev->xfer_buf = NULL;
}
return 0;
}
int mirisdr_read_async(mirisdr_dev_t *dev, mirisdr_read_async_cb_t cb, void *ctx,
uint32_t buf_num, uint32_t buf_len)
{
unsigned int i;
int r, num_iso_pack = 8;
struct timeval tv = { 1, 0 };
if (!dev)
return -1;
dev->cb = cb;
dev->cb_ctx = ctx;
// if (buf_num > 0)
// dev->xfer_buf_num = buf_num;
// else
dev->xfer_buf_num = DEFAULT_BUF_NUMBER;
dev->xfer_iso_pack = DEFAULT_ISO_PACKETS;
// if (buf_len > 0 && buf_len % 512 == 0) /* len must be multiple of 512 */
// dev->xfer_buf_len = buf_len;
// else
dev->xfer_buf_len = DEFAULT_BUF_LENGTH;
_mirisdr_alloc_async_buffers(dev);
for(i = 0; i < dev->xfer_buf_num; ++i) {
libusb_fill_iso_transfer(dev->xfer[i],
dev->devh,
0x81,
dev->xfer_buf[i],
dev->xfer_buf_len,
num_iso_pack,
_libusb_callback,
(void *)dev,
ISO_TIMEOUT);
libusb_set_iso_packet_lengths(dev->xfer[i],
dev->xfer_buf_len/dev->xfer_iso_pack);
libusb_submit_transfer(dev->xfer[i]);
}
dev->async_status = mirisdr_RUNNING;
while (mirisdr_INACTIVE != dev->async_status) {
r = libusb_handle_events_timeout(dev->ctx, &tv);
if (r < 0) {
fprintf(stderr, "handle_events returned: %d\n", r);
if (r == LIBUSB_ERROR_INTERRUPTED) /* stray signal */
continue;
break;
}
if (mirisdr_CANCELING == dev->async_status) {
dev->async_status = mirisdr_INACTIVE;
if (!dev->xfer)
break;
for(i = 0; i < dev->xfer_buf_num; ++i) {
if (!dev->xfer[i])
continue;
if (dev->xfer[i]->status == LIBUSB_TRANSFER_COMPLETED) {
libusb_cancel_transfer(dev->xfer[i]);
dev->async_status = mirisdr_CANCELING;
}
}
if (mirisdr_INACTIVE == dev->async_status)
break;
}
}
_mirisdr_free_async_buffers(dev);
return r;
}
int mirisdr_cancel_async(mirisdr_dev_t *dev)
{
if (!dev)
return -1;
if (mirisdr_RUNNING == dev->async_status) {
dev->async_status = mirisdr_CANCELING;
return 0;
}
return -2;
}
int mirisdr_reg_write_fn(void *dev, uint8_t reg, uint32_t val)
{
if (dev)
return msi2500_write_reg(((mirisdr_dev_t *)dev), reg, val);
return -1;
}

302
src/miri_sdr.c Normal file
View File

@ -0,0 +1,302 @@
/*
* MiriSDR
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
* Copyright (C) 2012 by Dimitri Stolnikov <horiz0n@gmx.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <Windows.h>
#endif
#include "mirisdr.h"
#define DEFAULT_SAMPLE_RATE 500000
#define DEFAULT_ASYNC_BUF_NUMBER 32
#define DEFAULT_BUF_LENGTH (16 * 16384)
#define MINIMAL_BUF_LENGTH 512
#define MAXIMAL_BUF_LENGTH (256 * 16384)
static int do_exit = 0;
static mirisdr_dev_t *dev = NULL;
void usage(void)
{
#ifdef _WIN32
fprintf(stderr,
"Usage:\t miri_sdr.exe [device_index] [samplerate in kHz] "
"[gain] [frequency in Hz] [filename]\n");
#else
fprintf(stderr,
"Usage:\t -f frequency_to_tune_to [Hz]\n"
"\t[-s samplerate (default: 2048000 Hz)]\n"
"\t[-d device_index (default: 0)]\n"
"\t[-g gain (default: 0 for auto)]\n"
"\t[-b output_block_size (default: 16 * 16384)]\n"
"\t[-S force sync output (default: async)]\n"
"\tfilename (a '-' dumps samples to stdout)\n\n");
#endif
exit(1);
}
#ifdef _WIN32
BOOL WINAPI
sighandler(int signum)
{
if (CTRL_C_EVENT == signum) {
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
mirisdr_cancel_async(dev);
return TRUE;
}
return FALSE;
}
#else
static void sighandler(int signum)
{
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
mirisdr_cancel_async(dev);
}
#endif
static void mirisdr_callback(unsigned char *buf, uint32_t len, void *ctx)
{
if (ctx) {
if (fwrite(buf, 1, len, (FILE*)ctx) != len) {
fprintf(stderr, "Short write, samples lost, exiting!\n");
mirisdr_cancel_async(dev);
}
}
}
int main(int argc, char **argv)
{
#ifndef _WIN32
struct sigaction sigact;
#endif
char *filename = NULL;
int n_read;
int r, opt;
int i, gain = 0;
int sync_mode = 0;
FILE *file;
uint8_t *buffer;
uint32_t dev_index = 0;
uint32_t frequency = 100000000;
uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
int device_count;
char vendor[256] = { 0 }, product[256] = { 0 }, serial[256] = { 0 };
int count;
int gains[100];
uint32_t rates[100];
#ifndef _WIN32
while ((opt = getopt(argc, argv, "d:f:g:s:b:S::")) != -1) {
switch (opt) {
case 'd':
dev_index = atoi(optarg);
break;
case 'f':
frequency = (uint32_t)atof(optarg);
break;
case 'g':
gain = (int)(atof(optarg) * 10); /* tenths of a dB */
break;
case 's':
samp_rate = (uint32_t)atof(optarg);
break;
case 'b':
out_block_size = (uint32_t)atof(optarg);
break;
case 'S':
sync_mode = 1;
break;
default:
usage();
break;
}
}
if (argc <= optind) {
usage();
} else {
filename = argv[optind];
}
#else
if(argc <6)
usage();
dev_index = atoi(argv[1]);
samp_rate = atoi(argv[2])*1000;
gain=(int)(atof(argv[3]) * 10); /* tenths of a dB */
frequency = atoi(argv[4]);
filename = argv[5];
#endif
if(out_block_size < MINIMAL_BUF_LENGTH ||
out_block_size > MAXIMAL_BUF_LENGTH ){
fprintf(stderr,
"Output block size wrong value, falling back to default\n");
fprintf(stderr,
"Minimal length: %u\n", MINIMAL_BUF_LENGTH);
fprintf(stderr,
"Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
out_block_size = DEFAULT_BUF_LENGTH;
}
buffer = malloc(out_block_size * sizeof(uint8_t));
device_count = mirisdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
exit(1);
}
fprintf(stderr, "Found %d device(s):\n", device_count);
for (i = 0; i < device_count; i++) {
mirisdr_get_device_usb_strings(i, vendor, product, serial);
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
}
fprintf(stderr, "\n");
fprintf(stderr, "Using device %d: %s\n",
dev_index, mirisdr_get_device_name(dev_index));
r = mirisdr_open(&dev, dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open mirisdr device #%d.\n", dev_index);
exit(1);
}
#ifndef _WIN32
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
sigaction(SIGPIPE, &sigact, NULL);
#else
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif
count = mirisdr_get_tuner_gains(dev, NULL);
fprintf(stderr, "Supported gain values (%d): ", count);
count = mirisdr_get_tuner_gains(dev, gains);
for (i = 0; i < count; i++)
fprintf(stderr, "%.1f ", gains[i] / 10.0);
fprintf(stderr, "\n");
r = mirisdr_get_usb_strings(dev, vendor, product, serial);
if (r < 0)
fprintf(stderr, "WARNING: Failed to read usb strings.\n");
else
fprintf(stderr, "%s, %s: SN: %s\n", vendor, product, serial);
/* Set the sample rate */
r = mirisdr_set_sample_rate(dev, samp_rate);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
else {
samp_rate = mirisdr_get_sample_rate(dev);
fprintf(stderr, "Sample rate is set to %u Hz.\n", samp_rate);
}
/* Set the frequency */
r = mirisdr_set_center_freq(dev, frequency);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set center freq.\n");
else
fprintf(stderr, "Tuned to %u Hz.\n", frequency);
if (0 == gain) {
/* Enable automatic gain */
r = mirisdr_set_tuner_gain_mode(dev, 0);
if (r < 0)
fprintf(stderr, "WARNING: Failed to enable automatic gain.\n");
} else {
/* Enable manual gain */
r = mirisdr_set_tuner_gain_mode(dev, 1);
if (r < 0)
fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
/* Set the tuner gain */
r = mirisdr_set_tuner_gain(dev, gain);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
else
fprintf(stderr, "Tuner gain set to %f dB.\n", gain/10.0);
}
if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
file = stdout;
} else {
file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Failed to open %s\n", filename);
goto out;
}
}
/* Reset endpoint before we start reading from it (mandatory) */
r = mirisdr_reset_buffer(dev);
if (r < 0)
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
if (sync_mode) {
fprintf(stderr, "Reading samples in sync mode...\n");
while (!do_exit) {
r = mirisdr_read_sync(dev, buffer, out_block_size, &n_read);
if (r < 0) {
fprintf(stderr, "WARNING: sync read failed.\n");
break;
}
if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) {
fprintf(stderr, "Short write, samples lost, exiting!\n");
break;
}
if ((uint32_t)n_read < out_block_size) {
fprintf(stderr, "Short read, samples lost, exiting!\n");
break;
}
}
} else {
fprintf(stderr, "Reading samples in async mode...\n");
r = mirisdr_read_async(dev, mirisdr_callback, (void *)file,
DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
}
if (do_exit)
fprintf(stderr, "\nUser cancel, exiting...\n");
else
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
if (file != stdout)
fclose(file);
mirisdr_close(dev);
free (buffer);
out:
return r >= 0 ? r : -r;
}

184
src/tuner_msi001.c Normal file
View File

@ -0,0 +1,184 @@
/*
* Mirics MSi001 tuner driver
*
* Copyright (C) 2012 by Eric Wild <la@tfc-server.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tuner_msi001.h"
#include "mirisdr_reg.h"
#include <stdint.h>
#include <stdio.h>
#include <memory.h>
static const struct r0_modes_ r0_modes[] = {
{"01100001", 0x61, "AM_MODE1", 16},
{"11100001", 0xe1, "AM_MODE2", 16},
{"01000010", 0x42, "VHF_MODE", 32},
{"01000100", 0x44, "B3_MODE", 16},
{"01001000", 0x48, "B45_MODE", 4},
{"01010000", 0x50, "BL_MODE", 2}
};
static const struct iffreqs_ iffreqs[] = {
{MHZ(19.2), MHZ(19.2), MHZ(19.2) * 7},
{MHZ(22.0), MHZ(22.0), MHZ(22.0) * 6},
// {MHZ(24.576), MHZ(24.576), MHZ(24.576) *5},
{MHZ(24.000), MHZ(24.000), MHZ(24.000) *5},
{MHZ(26), MHZ(26), MHZ(26) * 5},
{MHZ(38.4), MHZ(38.4)/2, MHZ(38.4) * 3.5}
};
static void writereg(void *dev, uint8_t reg, uint32_t val) {
fprintf(stderr, "%u 0x%08x\n", reg, val);
mirisdr_reg_write_fn(dev, 0x09, val);
}
static int set_reg0(void *dev, struct state* s) {
uint32_t reg0=0;
reg0 = r0_modes[s->m].value << 4;
if (s->m == AM_MODE1 || s->m == AM_MODE1) {
reg0 |= FIL_MODE_450K_IF << R0_FIL_MODE_SH;
reg0 |= 0x1 << R0_FIL_BW_SH;//hack filter bw
} else {
reg0 |= FIL_MODE_ZERO_IF << R0_FIL_MODE_SH;
reg0 |= 0x7 << R0_FIL_BW_SH;//hack filter bw
}
reg0 |= s->x << R0_XTAL_SEL_SH;
writereg(dev, 0, reg0);
s->reg[0] = reg0;
return 0;
}
static int set_reg52 (void *dev, struct state* s) {
uint32_t reg5=5;
uint32_t reg2=2;
float i_want_a_fracstep = 1000e3;
float f_if=0, fsynth,synthstep;
uint32_t thresh,int_,frac;
if (s->m == AM_MODE1 || s->m == AM_MODE1) {
f_if = 450e3;
fsynth = (iffreqs[s->x].fif1 - s->freq_hz + f_if) * r0_modes[s->m].lodiv;
fprintf(stderr, "wtf is if2?");
return -1;
} else {
fsynth = (s->freq_hz + f_if) * r0_modes[s->m].lodiv;
}
synthstep = i_want_a_fracstep * r0_modes[s->m].lodiv;
thresh = (iffreqs[s->x].fref1*4)/synthstep;
reg5 |= 0x28 << 16;
reg5 |= (uint32_t)thresh << 4;
int_ = fsynth/(iffreqs[s->x].fref1*4);//ROUNDDOWN?
frac = ((fsynth/(iffreqs[s->x].fref1*4)) - int_)*thresh;
reg2 |= frac << 4;
reg2 |= int_ << R2_INT_SH;
fprintf(stderr, "fsynth %fmhz synthstep %e thresh %u int_ %u frac %u // frac step value: %f\n", fsynth/1e6, synthstep, thresh, int_, frac, i_want_a_fracstep);
writereg(dev, 5, reg5);
writereg(dev, 2, reg2);
/* bit 19 and 21 must be set */
s->reg[5] = reg5;
s->reg[2] = reg2;
return 0;
}
static void setfreqs(void *dev, struct state* s) {
c(set_reg0(dev, s));
c(set_reg52(dev, s));
}
static int setgains(void *dev, struct state* s) {
uint32_t reg1=1;
s->minus_bbgain &= (1<<6)-1;
reg1 |= s->minus_bbgain << 4;
s->mixl &= 0x1;
s->lnagr &= 0x1;
if(s->m == AM_MODE1 || s->m == AM_MODE1){
reg1 |= s->am_mixgainred << R1_MIXBU_SH;
}
else {
s->am_mixgainred = r1_mixbu_p0_0;//not am = reset to 0
reg1 |= s->lnagr << R1_LNAGR_SH;
}
reg1 |= s->mixl << R1_MIXL_SH;
reg1 |= R1_DCCAL << R1_DCCAL_SH;//hack
writereg(dev, 1, reg1);
s->reg[1] = reg1;
return 0;
}
static void checkfreq(void *dev, struct state* s) {
uint32_t freq = (iffreqs[s->x].fref1*4)/r0_modes[s->m].lodiv;
uint32_t frac = (s->reg[2] >> 4) & ((1<<12)-1);
uint32_t int_ = (s->reg[2] >> R2_INT_SH) & ((1<<6)-1);
uint32_t thresh = (s->reg[5] >> 4) & ((1<<12)-1);
double res = freq * (float)((float)int_ + ((float)((frac << 12) +0)/(float)(thresh << 12) ));
float fstep = (float)(iffreqs[s->x].fref1 * 4)/ r0_modes[s->m].lodiv * thresh;
fprintf(stderr, "freq %u thresh %u int %u frac %u res: %fmhz\n", freq, thresh, int_, frac, res/1e6);
fprintf(stderr, "+1 frac res: %fmhz\n", freq * (float)((float)int_ + ((float)(((frac+1) << 12) +0)/(float)(thresh << 12) )) /1e6);
fprintf(stderr, "-1 frac res: %fmhz\n", freq * (float)((float)int_ + ((float)(((frac-1) << 12) +0)/(float)(thresh << 12) )) /1e6);
}
int msi001_init(void *dev, uint32_t freq)
{
struct state curstate;
memset(&curstate,0,sizeof(curstate));
//band sel
if (freq < MHZ(30))
curstate.m = AM_MODE1;
else if (freq < MHZ(140))
curstate.m = VHF_MODE;
else if (freq < MHZ(300))
curstate.m = B3_MODE;
else if (freq < MHZ(970))
curstate.m = B45_MODE;
else
curstate.m = BL_MODE;
curstate.x = XTAL24_576M;//xtal freq
curstate.freq_hz= freq;
setfreqs(dev, &curstate);
curstate.minus_bbgain = 20;//gain reduction: 0-59dB
curstate.am_mixgainred = r1_mixbu_p0_12;// ignored & reset except in am mode
curstate.mixl = 0;// bool, table 6-11
curstate.lnagr = 0;// bool, table 6-11
// c(setgains(dev, &curstate));
//no dc track timing
//no aux features
// AFC?
checkfreq(dev, &curstate);
return 0;
}