Skip to content

Commit

Permalink
add hackrf_tx in hackrf-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
tanwubin committed Oct 4, 2023
1 parent e84cb98 commit aa585b1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 66 deletions.
133 changes: 67 additions & 66 deletions host/hackrf-tools/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
# Copyright 2012 Jared Boone
# Copyright 2013 Benjamin Vernoux
#
# This file is part of HackRF.
#
# 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, 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; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

# Based heavily upon the libftdi cmake setup.

set(INSTALL_DEFAULT_BINDIR "bin" CACHE STRING "Appended to CMAKE_INSTALL_PREFIX")

find_package(FFTW REQUIRED)
include_directories(${FFTW_INCLUDES})
get_filename_component(FFTW_LIBRARY_DIRS ${FFTW_LIBRARIES} DIRECTORY)
link_directories(${FFTW_LIBRARY_DIRS})

SET(TOOLS
hackrf_transfer
hackrf_spiflash
hackrf_cpldjtag
hackrf_info
hackrf_debug
hackrf_clock
hackrf_sweep
hackrf_operacake
)

if(MSVC)
add_library(libgetopt_static STATIC
../getopt/getopt.c
)
LIST(APPEND TOOLS_LINK_LIBS ${FFTW_LIBRARIES})
else()
LIST(APPEND TOOLS_LINK_LIBS m fftw3f)
endif()

if(NOT libhackrf_SOURCE_DIR)
include_directories(${LIBHACKRF_INCLUDE_DIR})
LIST(APPEND TOOLS_LINK_LIBS ${LIBHACKRF_LIBRARIES})
else()
LIST(APPEND TOOLS_LINK_LIBS hackrf)
endif()

if(MSVC)
LIST(APPEND TOOLS_LINK_LIBS libgetopt_static)
endif()

foreach(tool ${TOOLS})
add_executable(${tool} ${tool}.c)
target_link_libraries(${tool} ${TOOLS_LINK_LIBS})
install(TARGETS ${tool} RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
endforeach(tool)
# Copyright 2012 Jared Boone
# Copyright 2013 Benjamin Vernoux
#
# This file is part of HackRF.
#
# 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, 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; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

# Based heavily upon the libftdi cmake setup.

set(INSTALL_DEFAULT_BINDIR "bin" CACHE STRING "Appended to CMAKE_INSTALL_PREFIX")

find_package(FFTW REQUIRED)
include_directories(${FFTW_INCLUDES})
get_filename_component(FFTW_LIBRARY_DIRS ${FFTW_LIBRARIES} DIRECTORY)
link_directories(${FFTW_LIBRARY_DIRS})

SET(TOOLS
hackrf_transfer
hackrf_spiflash
hackrf_cpldjtag
hackrf_info
hackrf_debug
hackrf_clock
hackrf_sweep
hackrf_operacake
hackrf_tx
)

if(MSVC)
add_library(libgetopt_static STATIC
../getopt/getopt.c
)
LIST(APPEND TOOLS_LINK_LIBS ${FFTW_LIBRARIES})
else()
LIST(APPEND TOOLS_LINK_LIBS m fftw3f)
endif()

if(NOT libhackrf_SOURCE_DIR)
include_directories(${LIBHACKRF_INCLUDE_DIR})
LIST(APPEND TOOLS_LINK_LIBS ${LIBHACKRF_LIBRARIES})
else()
LIST(APPEND TOOLS_LINK_LIBS hackrf)
endif()

if(MSVC)
LIST(APPEND TOOLS_LINK_LIBS libgetopt_static)
endif()

foreach(tool ${TOOLS})
add_executable(${tool} ${tool}.c)
target_link_libraries(${tool} ${TOOLS_LINK_LIBS})
install(TARGETS ${tool} RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
endforeach(tool)
50 changes: 50 additions & 0 deletions host/hackrf-tools/src/hackrf_tx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <hackrf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define FREQ 900000000


hackrf_device* device = NULL;

unsigned char* mbuffer = "012345678901234567890123456789";

void init_hackrf()
{
hackrf_init();
hackrf_open(&device);

hackrf_set_sample_rate(device, 8000000);
hackrf_set_baseband_filter_bandwidth(device, 8000000*0.75);
hackrf_set_freq(device, FREQ);
hackrf_set_txvga_gain(device, 40);
hackrf_set_lna_gain(device, 40);
hackrf_set_amp_enable(device, 1);

printf("HACKRF init done.\n");
}

int _hackrf_tx_callback(hackrf_transfer *transfer) {
printf("transfer->valid_length is %d\n", transfer->valid_length);
// memcpy(transfer->buffer,mbuffer,transfer->valid_length);
if (strlen(mbuffer) >= transfer->valid_length) {
printf("send txt len overflow!\n");
}
memcpy(transfer->buffer,mbuffer,strlen(mbuffer));
printf("HACKRF send finish.%s\n", transfer->buffer);
return 0;
}

int main(void)
{
for (int i = 0; i < 100; i++) {
init_hackrf();
hackrf_start_tx(device, _hackrf_tx_callback, NULL);
hackrf_stop_tx(device);
hackrf_close(device);
// _sleep(1000);
}
return EXIT_SUCCESS;
}

0 comments on commit aa585b1

Please sign in to comment.