From 0efbc6618e467e0bf9242f1851c3829da728ffe2 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Thu, 21 Feb 2013 14:25:30 -0700 Subject: [PATCH] board id request over USB (untested) --- firmware/usb_performance/usb_performance.c | 15 ++++- host/libhackrf/examples/CMakeLists.txt | 2 + host/libhackrf/examples/hackrf_info.c | 71 ++++++++++++++++++++++ host/libhackrf/src/hackrf.c | 38 +++++++++++- host/libhackrf/src/hackrf.h | 9 +++ 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 host/libhackrf/examples/hackrf_info.c diff --git a/firmware/usb_performance/usb_performance.c b/firmware/usb_performance/usb_performance.c index a1c3c9f3..36e9b971 100644 --- a/firmware/usb_performance/usb_performance.c +++ b/firmware/usb_performance/usb_performance.c @@ -430,6 +430,18 @@ usb_request_status_t usb_vendor_request_write_cpld( } } +usb_request_status_t usb_vendor_request_read_board_id( + usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) +{ + if (stage == USB_TRANSFER_STAGE_SETUP) { + endpoint->buffer[0] = BOARD_ID; + usb_endpoint_schedule(endpoint->in, &endpoint->buffer, 1); + usb_endpoint_schedule_ack(endpoint->out); + return USB_REQUEST_STATUS_OK; + } + return USB_REQUEST_STATUS_OK; +} + static const usb_request_handler_fn vendor_request_handler[] = { NULL, usb_vendor_request_set_transceiver_mode, @@ -443,7 +455,8 @@ static const usb_request_handler_fn vendor_request_handler[] = { usb_vendor_request_read_rffc5071, usb_vendor_request_write_spiflash, usb_vendor_request_read_spiflash, - usb_vendor_request_write_cpld + usb_vendor_request_write_cpld, + usb_vendor_request_read_board_id }; static const uint32_t vendor_request_handler_count = diff --git a/host/libhackrf/examples/CMakeLists.txt b/host/libhackrf/examples/CMakeLists.txt index 8e465482..09f0acd2 100644 --- a/host/libhackrf/examples/CMakeLists.txt +++ b/host/libhackrf/examples/CMakeLists.txt @@ -30,6 +30,7 @@ IF( EXAMPLES ) add_executable(hackrf_rffc5071 hackrf_rffc5071.c) add_executable(hackrf_spiflash hackrf_spiflash.c) add_executable(hackrf_cpldjtag hackrf_cpldjtag.c) + add_executable(hackrf_info hackrf_info.c) target_link_libraries(hackrf_max2837 hackrf) target_link_libraries(hackrf_si5351c hackrf) @@ -37,6 +38,7 @@ IF( EXAMPLES ) target_link_libraries(hackrf_rffc5071 hackrf) target_link_libraries(hackrf_spiflash hackrf) target_link_libraries(hackrf_cpldjtag hackrf) + target_link_libraries(hackrf_info hackrf) include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src) endif(EXAMPLES) diff --git a/host/libhackrf/examples/hackrf_info.c b/host/libhackrf/examples/hackrf_info.c new file mode 100644 index 00000000..7d7ca4fc --- /dev/null +++ b/host/libhackrf/examples/hackrf_info.c @@ -0,0 +1,71 @@ +/* + * Copyright 2012 Jared Boone + * Copyright 2013 Benjamin Vernoux + * Copyright 2013 Michael Ossmann + * + * 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. + */ + +#include + +#include +#include + +int main(int argc, char** argv) +{ + hackrf_device* device = NULL; + int result = HACKRF_SUCCESS; + uint8_t board_id = BOARD_ID_INVALID; + + result = hackrf_init(); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_init() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + result = hackrf_open(&device); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_open() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + printf("Found HackRF board.\n"); + + result = hackrf_board_id_read(device, &board_id); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_cpld_write() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + printf("Board ID Number: %d (%s)\n", board_id, + hackrf_board_id_name(board_id)); + + result = hackrf_close(device); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_close() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + hackrf_exit(); + + return EXIT_SUCCESS; +} diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 5510254b..70cff32d 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -41,7 +41,8 @@ typedef enum { HACKRF_VENDOR_REQUEST_RFFC5071_READ = 9, HACKRF_VENDOR_REQUEST_SPIFLASH_WRITE = 10, HACKRF_VENDOR_REQUEST_SPIFLASH_READ = 11, - HACKRF_VENDOR_REQUEST_CPLD_WRITE = 12 + HACKRF_VENDOR_REQUEST_CPLD_WRITE = 12, + HACKRF_VENDOR_REQUEST_BOARD_ID_READ = 13 } hackrf_vendor_request; typedef enum { @@ -505,6 +506,25 @@ int hackrf_cpld_write(hackrf_device* device, const uint16_t length, } } +int hackrf_board_id_read(hackrf_device* device, uint8_t* value) { + int result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_BOARD_ID_READ, + 0, + 0, + value, + 1, + 0 + ); + + if( result < 2 ) { + return HACKRF_ERROR_LIBUSB; + } else { + return HACKRF_SUCCESS; + } +} + static void* transfer_threadproc(void* arg) { hackrf_device* device = (hackrf_device*)arg; @@ -676,3 +696,19 @@ const char* hackrf_error_name(enum hackrf_error errcode) { return "HACKRF unknown error"; } } + +const char* hackrf_board_id_name(enum hackrf_board_id board_id) { + switch(board_id) { + case BOARD_ID_JELLYBEAN: + return "Jellybean"; + + case BOARD_ID_JAWBREAKER: + return "Jawbreaker"; + + case BOARD_ID_INVALID: + return "Invalid Board ID"; + + default: + return "Unknown Board ID"; + } +} diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 26386581..712b4fce 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -37,6 +37,12 @@ enum hackrf_error { HACKRF_ERROR_OTHER = -9999, }; +enum hackrf_board_id { + BOARD_ID_JELLYBEAN = 0, + BOARD_ID_JAWBREAKER = 1, + BOARD_ID_INVALID = 0xFF, +}; + typedef struct hackrf_device hackrf_device; typedef struct { @@ -82,6 +88,9 @@ int hackrf_spiflash_read(hackrf_device* device, const uint32_t address, int hackrf_cpld_write(hackrf_device* device, const uint16_t length, unsigned char* const data); +int hackrf_board_id_read(hackrf_device* device, uint8_t* value); + const char* hackrf_error_name(enum hackrf_error errcode); +const char* hackrf_board_id_name(enum hackrf_board_id board_id); #endif//__HACKRF_H__