diff --git a/firmware/common/w25q80bv.c b/firmware/common/w25q80bv.c index 73dd0224..5397354c 100644 --- a/firmware/common/w25q80bv.c +++ b/firmware/common/w25q80bv.c @@ -38,12 +38,15 @@ #define W25Q80BV_FAST_READ 0x0b #define W25Q80BV_WRITE_ENABLE 0x06 #define W25Q80BV_CHIP_ERASE 0xC7 +#define W25Q80BV_WRITE_STATUS 0x01 #define W25Q80BV_READ_STATUS1 0x05 +#define W25Q80BV_READ_STATUS2 0x35 #define W25Q80BV_PAGE_PROGRAM 0x02 #define W25Q80BV_DEVICE_ID 0xAB #define W25Q80BV_UNIQUE_ID 0x4B #define W25Q80BV_STATUS_BUSY 0x01 +#define W25Q80BV_STATUS_WEL 0x02 #define W25Q80BV_DEVICE_ID_RES 0x13 /* Expected device_id for W25Q80BV */ @@ -110,6 +113,7 @@ void w25q80bv_write_enable(w25q80bv_driver_t* const drv) uint8_t data[] = { W25Q80BV_WRITE_ENABLE }; spi_bus_transfer(drv->bus, data, ARRAY_SIZE(data)); + while (w25q80bv_get_status(drv) ^ W25Q80BV_STATUS_WEL); } void w25q80bv_chip_erase(w25q80bv_driver_t* const drv) @@ -223,3 +227,22 @@ void w25q80bv_read(w25q80bv_driver_t* const drv, uint32_t addr, uint32_t len, ui spi_bus_transfer_gather(drv->bus, transfers, ARRAY_SIZE(transfers)); } + +void w25q80bv_clear_status(w25q80bv_driver_t* const drv) +{ + w25q80bv_write_enable(drv); + w25q80bv_wait_while_busy(drv); + uint8_t data[] = { W25Q80BV_WRITE_STATUS, 0x00, 0x00 }; + spi_bus_transfer(drv->bus, data, ARRAY_SIZE(data)); +} + +void w25q80bv_get_full_status(w25q80bv_driver_t* const drv, uint8_t* data) +{ + uint8_t cmd[] = { W25Q80BV_READ_STATUS1, 0xFF }; + spi_bus_transfer(drv->bus, cmd, ARRAY_SIZE(cmd)); + data[0] = cmd[1]; + cmd[0] =W25Q80BV_READ_STATUS2; + cmd[1] = 0xFF; + spi_bus_transfer(drv->bus, cmd, ARRAY_SIZE(cmd)); + data[1] = cmd[1]; +} diff --git a/firmware/common/w25q80bv.h b/firmware/common/w25q80bv.h index 494332a1..d98dd302 100644 --- a/firmware/common/w25q80bv.h +++ b/firmware/common/w25q80bv.h @@ -53,10 +53,12 @@ struct w25q80bv_driver_t { }; void w25q80bv_setup(w25q80bv_driver_t* const drv); +void w25q80bv_get_full_status(w25q80bv_driver_t* const drv, uint8_t* data); void w25q80bv_chip_erase(w25q80bv_driver_t* const drv); void w25q80bv_program(w25q80bv_driver_t* const drv, uint32_t addr, uint32_t len, uint8_t* data); uint8_t w25q80bv_get_device_id(w25q80bv_driver_t* const drv); void w25q80bv_get_unique_id(w25q80bv_driver_t* const drv, w25q80bv_unique_id_t* unique_id); void w25q80bv_read(w25q80bv_driver_t* const drv, uint32_t addr, uint32_t len, uint8_t* const data); +void w25q80bv_clear_status(w25q80bv_driver_t* const drv); #endif//__W25Q80BV_H__ diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index d99b3529..7c5047b8 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -90,7 +90,9 @@ static const usb_request_handler_fn vendor_request_handler[] = { usb_vendor_request_set_hw_sync_mode, usb_vendor_request_reset, usb_vendor_request_operacake_set_ranges, - usb_vendor_request_set_clkout_enable + usb_vendor_request_set_clkout_enable, + usb_vendor_request_spiflash_status, + usb_vendor_request_spiflash_clear_status }; static const uint32_t vendor_request_handler_count = diff --git a/firmware/hackrf_usb/usb_api_spiflash.c b/firmware/hackrf_usb/usb_api_spiflash.c index 6b3f8502..9663f676 100644 --- a/firmware/hackrf_usb/usb_api_spiflash.c +++ b/firmware/hackrf_usb/usb_api_spiflash.c @@ -121,3 +121,28 @@ usb_request_status_t usb_vendor_request_read_spiflash( } } +usb_request_status_t usb_vendor_request_spiflash_status( + usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) +{ + uint8_t data[2]; + if (stage == USB_TRANSFER_STAGE_SETUP) { + w25q80bv_get_full_status(&spi_flash, data); + usb_transfer_schedule_block(endpoint->in, &data, 2, NULL, NULL); + return USB_REQUEST_STATUS_OK; + } else if (stage == USB_TRANSFER_STAGE_DATA) { + usb_transfer_schedule_ack(endpoint->out); + return USB_REQUEST_STATUS_OK; + } else { + return USB_REQUEST_STATUS_OK; + } +} + +usb_request_status_t usb_vendor_request_spiflash_clear_status( + usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) +{ + if (stage == USB_TRANSFER_STAGE_SETUP) { + w25q80bv_clear_status(&spi_flash); + usb_transfer_schedule_ack(endpoint->in); + } +return USB_REQUEST_STATUS_OK; +} \ No newline at end of file diff --git a/firmware/hackrf_usb/usb_api_spiflash.h b/firmware/hackrf_usb/usb_api_spiflash.h index f8f9f52e..3d17203d 100644 --- a/firmware/hackrf_usb/usb_api_spiflash.h +++ b/firmware/hackrf_usb/usb_api_spiflash.h @@ -32,5 +32,9 @@ usb_request_status_t usb_vendor_request_write_spiflash( usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage); usb_request_status_t usb_vendor_request_read_spiflash( usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage); +usb_request_status_t usb_vendor_request_spiflash_status( + usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage); +usb_request_status_t usb_vendor_request_spiflash_clear_status( + usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage); #endif /* end of include guard: __USB_API_SPIFLASH_H__ */ diff --git a/firmware/libopencm3 b/firmware/libopencm3 index d3d6f3e7..c9f40aae 160000 --- a/firmware/libopencm3 +++ b/firmware/libopencm3 @@ -1 +1 @@ -Subproject commit d3d6f3e74b34593d1b56175abe5b4cb72da4ec9d +Subproject commit c9f40aae0ba25f1ffb7f2a49e050fd50714d0632 diff --git a/host/hackrf-tools/src/hackrf_spiflash.c b/host/hackrf-tools/src/hackrf_spiflash.c index ffed119a..216b5eb7 100644 --- a/host/hackrf-tools/src/hackrf_spiflash.c +++ b/host/hackrf-tools/src/hackrf_spiflash.c @@ -54,6 +54,8 @@ static struct option long_options[] = { { "compatibility", no_argument, 0, 'c' }, { "device", required_argument, 0, 'd' }, { "reset", no_argument, 0, 'R' }, + { "status", no_argument, 0, 's' }, + { "clear", no_argument, 0, 'c' }, { "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, @@ -145,6 +147,8 @@ static void usage() printf("\t-w, --write : Write data from file.\n"); printf("\t-i, --no-check: Skip check for firmware compatibility with target device.\n"); printf("\t-d, --device : Serial number of device, if multiple devices\n"); + printf("\t-s, --status: Read SPI flash status registers before other operations.\n"); + printf("\t-c, --clear: Clear SPI flash status registers before other operations.\n"); printf("\t-R, --reset: Reset HackRF after other operations.\n"); printf("\t-v, --verbose: Verbose output.\n"); } @@ -152,6 +156,7 @@ static void usage() int main(int argc, char** argv) { int opt; + uint8_t status[2]; uint32_t address = 0; uint32_t length = MAX_LENGTH; uint32_t tmp_length; @@ -169,9 +174,11 @@ int main(int argc, char** argv) bool ignore_compat_check = false; bool verbose = false; bool reset = false; + bool read_status = false; + bool clear_status = false; uint16_t usb_api; - while ((opt = getopt_long(argc, argv, "a:l:r:w:id:vRh?", long_options, + while ((opt = getopt_long(argc, argv, "a:l:r:w:id:scvRh?", long_options, &option_index)) != EOF) { switch (opt) { case 'a': @@ -200,6 +207,14 @@ int main(int argc, char** argv) serial_number = optarg; break; + case 's': + read_status = true; + break; + + case 'c': + clear_status = true; + break; + case 'v': verbose = true; break; @@ -233,7 +248,7 @@ int main(int argc, char** argv) return EXIT_FAILURE; } - if(!(write || read || reset)) { + if(!(write || read || reset || read_status || clear_status)) { fprintf(stderr, "Specify either read, write, or reset option.\n"); usage(); return EXIT_FAILURE; @@ -295,6 +310,42 @@ int main(int argc, char** argv) return EXIT_FAILURE; } + if(read_status) { + result = hackrf_spiflash_status(device, status); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_spiflash_status() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + if(!verbose) { + printf("Status: 0x%02x %02x\n", status[0], status[1]); + } else { + printf("SRP0\t%x\nSEC\t%x\nTB\t%x\nBP\t%x\nWEL\t%x\nBusy\t%x\n", + (status[0] & 0x80) >> 7, + (status[0] & 0x40) >> 6, + (status[0] & 0x20) >> 5, + (status[0] & 0x1C) >> 2, + (status[0] & 0x02) >> 1, + status[0] & 0x01); + printf("SUS\t%x\nCMP\t%x\nLB\t%x\nRes\t%x\nQE\t%x\nSRP1\t%x\n", + (status[1] & 0x80) >> 7, + (status[1] & 0x40) >> 6, + (status[1] & 0x38) >> 3, + (status[1] & 0x04) >> 2, + (status[1] & 0x02) >> 1, + status[1] & 0x01); + } + } + + if(clear_status) { + result = hackrf_spiflash_clear_status(device); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_spiflash_clear_status() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + } + if(read) { ssize_t bytes_written; tmp_length = length; diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index e0a9f527..51efae8c 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -80,6 +80,8 @@ typedef enum { HACKRF_VENDOR_REQUEST_RESET = 30, HACKRF_VENDOR_REQUEST_OPERACAKE_SET_RANGES = 31, HACKRF_VENDOR_REQUEST_CLKOUT_ENABLE = 32, + HACKRF_VENDOR_REQUEST_SPIFLASH_STATUS = 33, + HACKRF_VENDOR_REQUEST_SPIFLASH_CLEAR_STATUS = 34, } hackrf_vendor_request; #define USB_CONFIG_STANDARD 0x1 @@ -136,6 +138,12 @@ static const max2837_ft_t max2837_ft[] = { { 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 const uint16_t hackrf_usb_vid = 0x1d50; @@ -958,6 +966,55 @@ int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, } } +int ADDCALL hackrf_spiflash_status(hackrf_device* device, uint8_t* data) +{ + USB_API_REQUIRED(device, 0x0103) + int result; + + result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_SPIFLASH_STATUS, + 0, + 0, + data, + 2, + 0 + ); + + if (result < 1) + { + last_libusb_error = result; + return HACKRF_ERROR_LIBUSB; + } else { + return HACKRF_SUCCESS; + } +} + +int ADDCALL hackrf_spiflash_clear_status(hackrf_device* device) +{ + USB_API_REQUIRED(device, 0x0103) + int result; + result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_SPIFLASH_CLEAR_STATUS, + 0, + 0, + NULL, + 0, + 0 + ); + + if( result != 0 ) + { + last_libusb_error = result; + return HACKRF_ERROR_LIBUSB; + } else { + return HACKRF_SUCCESS; + } +} + int ADDCALL hackrf_cpld_write(hackrf_device* device, unsigned char* const data, const unsigned int total_length) { @@ -1054,12 +1111,6 @@ extern ADDAPI int ADDCALL hackrf_usb_api_version_read(hackrf_device* device, 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 { uint32_t freq_mhz; /* From 0 to 6000+MHz */ uint32_t freq_hz; /* From 0 to 999999Hz */ diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 2e335c01..4c43bffd 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -176,6 +176,8 @@ extern ADDAPI int ADDCALL hackrf_rffc5071_write(hackrf_device* device, uint8_t r extern ADDAPI int ADDCALL hackrf_spiflash_erase(hackrf_device* device); extern ADDAPI int ADDCALL hackrf_spiflash_write(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* const data); extern ADDAPI int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address, const uint16_t length, unsigned char* data); +extern ADDAPI int ADDCALL hackrf_spiflash_status(hackrf_device* device, uint8_t* data); +extern ADDAPI int ADDCALL hackrf_spiflash_clear_status(hackrf_device* device); /* device will need to be reset after hackrf_cpld_write */ extern ADDAPI int ADDCALL hackrf_cpld_write(hackrf_device* device,