Merge pull request #264 from dodgymike/master
Added flags + code to hackrf_si5351c program, to open device by index or serial number
This commit is contained in:
@ -25,12 +25,20 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
|
#ifndef bool
|
||||||
|
typedef int bool;
|
||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
#endif
|
||||||
|
|
||||||
static void usage() {
|
static void usage() {
|
||||||
printf("\nUsage:\n");
|
printf("\nUsage:\n");
|
||||||
printf("\t-c, --config: print textual configuration information\n");
|
printf("\t-c, --config: print textual configuration information\n");
|
||||||
printf("\t-n, --register <n>: set register number for subsequent read/write operations\n");
|
printf("\t-n, --register <n>: set register number for subsequent read/write operations\n");
|
||||||
printf("\t-r, --read: read register specified by last -n argument, or all registers\n");
|
printf("\t-r, --read: read register specified by last -n argument, or all registers\n");
|
||||||
printf("\t-w, --write <v>: write register specified by last -n argument with value <v>\n");
|
printf("\t-w, --write <v>: write register specified by last -n argument with value <v>\n");
|
||||||
|
printf("\t-s, --device <s>: specify a particular device by serial number\n");
|
||||||
|
printf("\t-d, --device <n>: specify a particular device by number\n");
|
||||||
printf("\nExamples:\n");
|
printf("\nExamples:\n");
|
||||||
printf("\t<command> -n 12 -r # reads from register 12\n");
|
printf("\t<command> -n 12 -r # reads from register 12\n");
|
||||||
printf("\t<command> -r # reads all registers\n");
|
printf("\t<command> -r # reads all registers\n");
|
||||||
@ -42,6 +50,8 @@ static struct option long_options[] = {
|
|||||||
{ "register", required_argument, 0, 'n' },
|
{ "register", required_argument, 0, 'n' },
|
||||||
{ "write", required_argument, 0, 'w' },
|
{ "write", required_argument, 0, 'w' },
|
||||||
{ "read", no_argument, 0, 'r' },
|
{ "read", no_argument, 0, 'r' },
|
||||||
|
{ "device", no_argument, 0, 'd' },
|
||||||
|
{ "serial", no_argument, 0, 's' },
|
||||||
{ 0, 0, 0, 0 },
|
{ 0, 0, 0, 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -184,8 +194,15 @@ int dump_configuration(hackrf_device* device) {
|
|||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
uint16_t register_number = REGISTER_INVALID;
|
uint16_t register_number = REGISTER_INVALID;
|
||||||
uint16_t register_value;
|
uint16_t register_value;
|
||||||
|
bool read = false;
|
||||||
|
bool write = false;
|
||||||
|
bool dump_config = false;
|
||||||
|
const char* serial_number = NULL;
|
||||||
|
int device_index = 0;
|
||||||
|
|
||||||
hackrf_device* device = NULL;
|
hackrf_device* device = NULL;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
|
||||||
@ -194,36 +211,31 @@ int main(int argc, char** argv) {
|
|||||||
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
|
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = hackrf_open(&device);
|
|
||||||
if( result ) {
|
|
||||||
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while( (opt = getopt_long(argc, argv, "cn:rw:", long_options, &option_index)) != EOF ) {
|
while( (opt = getopt_long(argc, argv, "d:s:cn:rw:", long_options, &option_index)) != EOF ) {
|
||||||
switch( opt ) {
|
switch( opt ) {
|
||||||
case 'n':
|
case 'n':
|
||||||
result = parse_int(optarg, ®ister_number);
|
result = parse_int(optarg, ®ister_number);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'w':
|
case 'w':
|
||||||
result = parse_int(optarg, ®ister_value);
|
write = true;
|
||||||
if( result == HACKRF_SUCCESS ) {
|
|
||||||
result = write_register(device, register_number, register_value);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
if( register_number == REGISTER_INVALID ) {
|
read = true;
|
||||||
result = dump_registers(device);
|
|
||||||
} else {
|
|
||||||
result = dump_register(device, register_number);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
dump_configuration(device);
|
dump_config = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'd':
|
||||||
|
device_index = atoi(optarg);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 's':
|
||||||
|
serial_number = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -235,6 +247,37 @@ int main(int argc, char** argv) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(serial_number != NULL) {
|
||||||
|
result = hackrf_open_by_serial(serial_number, &device);
|
||||||
|
} else {
|
||||||
|
hackrf_device_list_t* device_list = hackrf_device_list();
|
||||||
|
if(device_list->devicecount <= 0) {
|
||||||
|
result = HACKRF_ERROR_NOT_FOUND;
|
||||||
|
} else {
|
||||||
|
result = hackrf_device_list_open(device_list, device_index, &device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( result ) {
|
||||||
|
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(write) {
|
||||||
|
result = parse_int(optarg, ®ister_value);
|
||||||
|
if( result == HACKRF_SUCCESS ) {
|
||||||
|
result = write_register(device, register_number, register_value);
|
||||||
|
}
|
||||||
|
} else if(read) {
|
||||||
|
if( register_number == REGISTER_INVALID ) {
|
||||||
|
result = dump_registers(device);
|
||||||
|
} else {
|
||||||
|
result = dump_register(device, register_number);
|
||||||
|
}
|
||||||
|
} else if(dump_config) {
|
||||||
|
dump_configuration(device);
|
||||||
|
}
|
||||||
|
|
||||||
result = hackrf_close(device);
|
result = hackrf_close(device);
|
||||||
if( result ) {
|
if( result ) {
|
||||||
|
Reference in New Issue
Block a user