Optional spi flash status read

This commit is contained in:
Dominic Spill
2017-09-12 18:04:15 -06:00
parent 8f544ee60d
commit f20763419f
2 changed files with 21 additions and 12 deletions

View File

@ -53,6 +53,7 @@ static struct option long_options[] = {
{ "write", required_argument, 0, 'w' }, { "write", required_argument, 0, 'w' },
{ "device", required_argument, 0, 'd' }, { "device", required_argument, 0, 'd' },
{ "reset", no_argument, 0, 'R' }, { "reset", no_argument, 0, 'R' },
{ "status", no_argument, 0, 's' },
{ "verbose", no_argument, 0, 'v' }, { "verbose", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
@ -95,6 +96,7 @@ static void usage()
printf("\t-r, --read <filename>: Read data into file.\n"); printf("\t-r, --read <filename>: Read data into file.\n");
printf("\t-w, --write <filename>: Write data from file.\n"); printf("\t-w, --write <filename>: Write data from file.\n");
printf("\t-d, --device <serialnumber>: Serial number of device, if multiple devices\n"); printf("\t-d, --device <serialnumber>: Serial number of device, if multiple devices\n");
printf("\t-s, --status: Read SPI flash status registers before other operations.\n");
printf("\t-R, --reset: Reset HackRF after other operations.\n"); printf("\t-R, --reset: Reset HackRF after other operations.\n");
printf("\t-v, --verbose: Verbose output.\n"); printf("\t-v, --verbose: Verbose output.\n");
} }
@ -119,9 +121,10 @@ int main(int argc, char** argv)
bool write = false; bool write = false;
bool verbose = false; bool verbose = false;
bool reset = false; bool reset = false;
bool read_status = false;
uint16_t usb_api; uint16_t usb_api;
while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vRh?", long_options, while ((opt = getopt_long(argc, argv, "a:l:r:w:d:svRh?", long_options,
&option_index)) != EOF) { &option_index)) != EOF) {
switch (opt) { switch (opt) {
case 'a': case 'a':
@ -146,6 +149,10 @@ int main(int argc, char** argv)
serial_number = optarg; serial_number = optarg;
break; break;
case 's':
read_status = true;
break;
case 'v': case 'v':
verbose = true; verbose = true;
break; break;
@ -179,7 +186,7 @@ int main(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(!(write || read || reset)) { if(!(write || read || reset || read_status)) {
fprintf(stderr, "Specify either read, write, or reset option.\n"); fprintf(stderr, "Specify either read, write, or reset option.\n");
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
@ -241,6 +248,11 @@ int main(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(read_status) {
hackrf_spiflash_status(device, status);
printf("Status: 0x%02x %02x\n", status[0], status[1]);
}
if(read) { if(read) {
ssize_t bytes_written; ssize_t bytes_written;
tmp_length = length; tmp_length = length;
@ -279,8 +291,6 @@ int main(int argc, char** argv)
fd = NULL; fd = NULL;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
hackrf_spiflash_status(device, status);
printf("Status: 0x%02x %02x\n", status[0], status[1]);
printf("Erasing SPI flash.\n"); printf("Erasing SPI flash.\n");
result = hackrf_spiflash_erase(device); result = hackrf_spiflash_erase(device);
if (result != HACKRF_SUCCESS) { if (result != HACKRF_SUCCESS) {
@ -292,8 +302,6 @@ int main(int argc, char** argv)
} }
if( !verbose ) printf("Writing %d bytes at 0x%06x.\n", length, address); if( !verbose ) printf("Writing %d bytes at 0x%06x.\n", length, address);
while (length) { while (length) {
hackrf_spiflash_status(device, status);
printf("Status: 0x%02x %02x\n", status[0], status[1]);
xfer_len = (length > 256) ? 256 : length; xfer_len = (length > 256) ? 256 : length;
if( verbose ) printf("Writing %d bytes at 0x%06x.\n", xfer_len, address); if( verbose ) printf("Writing %d bytes at 0x%06x.\n", xfer_len, address);
result = hackrf_spiflash_write(device, address, xfer_len, pdata); result = hackrf_spiflash_write(device, address, xfer_len, pdata);

View File

@ -136,6 +136,12 @@ static const max2837_ft_t max2837_ft[] = {
{ 0 } { 0 }
}; };
#define USB_API_REQUIRED(device, version) \
uint16_t usb_version = 0; \
hackrf_usb_api_version_read(device, &usb_version); \
if(usb_version < version) \
return HACKRF_ERROR_USB_API_VERSION;
static volatile bool do_exit = false; static volatile bool do_exit = false;
static const uint16_t hackrf_usb_vid = 0x1d50; static const uint16_t hackrf_usb_vid = 0x1d50;
@ -960,6 +966,7 @@ int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address,
int ADDCALL hackrf_spiflash_status(hackrf_device* device, uint8_t* data) int ADDCALL hackrf_spiflash_status(hackrf_device* device, uint8_t* data)
{ {
USB_API_REQUIRED(device, 0x0103)
int result; int result;
result = libusb_control_transfer( result = libusb_control_transfer(
@ -1078,12 +1085,6 @@ extern ADDAPI int ADDCALL hackrf_usb_api_version_read(hackrf_device* device,
return HACKRF_SUCCESS; return HACKRF_SUCCESS;
} }
#define USB_API_REQUIRED(device, version) \
uint16_t usb_version = 0; \
hackrf_usb_api_version_read(device, &usb_version); \
if(usb_version < version) \
return HACKRF_ERROR_USB_API_VERSION;
typedef struct { typedef struct {
uint32_t freq_mhz; /* From 0 to 6000+MHz */ uint32_t freq_mhz; /* From 0 to 6000+MHz */
uint32_t freq_hz; /* From 0 to 999999Hz */ uint32_t freq_hz; /* From 0 to 999999Hz */