Skip to content

Commit

Permalink
Add software controlled reset
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicgs committed Jan 27, 2017
1 parent 8efc85d commit b047dd0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
3 changes: 2 additions & 1 deletion firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ static const usb_request_handler_fn vendor_request_handler[] = {
usb_vendor_request_init_sweep,
usb_vendor_request_operacake_get_boards,
usb_vendor_request_operacake_set_ports,
usb_vendor_request_set_hw_sync_mode
usb_vendor_request_set_hw_sync_mode,
usb_vendor_request_reset
};

static const uint32_t vendor_request_handler_count =
Expand Down
11 changes: 11 additions & 0 deletions firmware/hackrf_usb/usb_api_board_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <hackrf_core.h>
#include <rom_iap.h>
#include <usb_queue.h>
#include <libopencm3/lpc43xx/wwdt.h>

#include <stddef.h>
#include <string.h>
Expand Down Expand Up @@ -91,3 +92,13 @@ usb_request_status_t usb_vendor_request_read_partid_serialno(
}
return USB_REQUEST_STATUS_OK;
}

usb_request_status_t usb_vendor_request_reset(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage)
{
if (stage == USB_TRANSFER_STAGE_SETUP) {
wwdt_reset(100000);
usb_transfer_schedule_ack(endpoint->in);
}
return USB_REQUEST_STATUS_OK;
}
2 changes: 2 additions & 0 deletions firmware/hackrf_usb/usb_api_board_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ usb_request_status_t usb_vendor_request_read_version_string(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
usb_request_status_t usb_vendor_request_read_partid_serialno(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
usb_request_status_t usb_vendor_request_reset(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);

#endif /* end of include guard: __USB_API_BOARD_INFO_H__ */
2 changes: 1 addition & 1 deletion firmware/libopencm3
50 changes: 34 additions & 16 deletions host/hackrf-tools/src/hackrf_spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ static void usage()
printf("\t-r <filename>: Read data into file.\n");
printf("\t-w <filename>: Write data from file.\n");
printf("\t-d <serialnumber>: Serial number of device, if multiple devices\n");
printf("\t-R: Reset HackRF after other operations.\n");
printf("\t-v: Verbose output.\n");
}

Expand All @@ -112,8 +113,9 @@ int main(int argc, char** argv)
bool read = false;
bool write = false;
bool verbose = false;
bool reset = false;

while ((opt = getopt_long(argc, argv, "a:l:r:w:d:v", long_options,
while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vR", long_options,
&option_index)) != EOF) {
switch (opt) {
case 'a':
Expand Down Expand Up @@ -142,6 +144,10 @@ int main(int argc, char** argv)
verbose = true;
break;

case 'R':
reset = true;
break;

default:
fprintf(stderr, "opt error: %d\n", opt);
usage();
Expand All @@ -156,17 +162,17 @@ int main(int argc, char** argv)
}
}

if (write == read) {
if (write == true) {
if((write == read) && (write == reset)) {
if(write && read) {
fprintf(stderr, "Read and write options are mutually exclusive.\n");
} else {
fprintf(stderr, "Specify either read or write option.\n");
fprintf(stderr, "Specify either read, write, or reset option.\n");
}
usage();
return EXIT_FAILURE;
}

if (path == NULL) {
if((read || write) && (path == NULL)) {
fprintf(stderr, "Specify a path to a file.\n");
usage();
return EXIT_FAILURE;
Expand Down Expand Up @@ -214,7 +220,7 @@ int main(int argc, char** argv)
}
}

if (fd == NULL) {
if((read || write) && (fd == NULL)) {
fprintf(stderr, "Failed to open file: %s\n", path);
return EXIT_FAILURE;
}
Expand All @@ -233,8 +239,7 @@ int main(int argc, char** argv)
return EXIT_FAILURE;
}

if (read)
{
if(read) {
ssize_t bytes_written;
tmp_length = length;
while (tmp_length)
Expand All @@ -261,7 +266,9 @@ int main(int argc, char** argv)
fd = NULL;
return EXIT_FAILURE;
}
} else {
}

if(write) {
ssize_t bytes_read = fread(data, 1, length, fd);
if (bytes_read != length) {
fprintf(stderr, "Failed read file (read %d bytes).\n",
Expand Down Expand Up @@ -297,13 +304,24 @@ int main(int argc, char** argv)
}
}

result = hackrf_close(device);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
return EXIT_FAILURE;
if(reset) {
result = hackrf_reset(device);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_reset() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
return EXIT_FAILURE;
}
} else {
result = hackrf_close(device);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
hackrf_error_name(result), result);
fclose(fd);
fd = NULL;
return EXIT_FAILURE;
}
}

hackrf_exit();
Expand Down
20 changes: 20 additions & 0 deletions host/libhackrf/src/hackrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef enum {
HACKRF_VENDOR_REQUEST_OPERACAKE_GET_BOARDS = 27,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_PORTS = 28,
HACKRF_VENDOR_REQUEST_SET_HW_SYNC_MODE = 29,
HACKRF_VENDOR_REQUEST_RESET = 30,
} hackrf_vendor_request;

typedef enum {
Expand Down Expand Up @@ -1829,6 +1830,25 @@ int ADDCALL hackrf_set_operacake_ports(hackrf_device* device,
}
}

int ADDCALL hackrf_reset(hackrf_device* device) {
int result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
HACKRF_VENDOR_REQUEST_RESET,
0,
0,
NULL,
0,
0
);

if( result != 0 ) {
return HACKRF_ERROR_LIBUSB;
} else {
return HACKRF_SUCCESS;
}
}

#ifdef __cplusplus
} // __cplusplus defined.
#endif
Expand Down
2 changes: 2 additions & 0 deletions host/libhackrf/src/hackrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ extern ADDAPI int ADDCALL hackrf_set_operacake_ports(hackrf_device* device,
uint8_t port_a,
uint8_t port_b);

extern ADDAPI int ADDCALL hackrf_reset(hackrf_device* device);

#ifdef __cplusplus
} // __cplusplus defined.
#endif
Expand Down

0 comments on commit b047dd0

Please sign in to comment.