Skip to content

Commit

Permalink
Add flash status read
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicgs committed Sep 12, 2017
1 parent 97d3e65 commit 8f544ee
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
14 changes: 14 additions & 0 deletions firmware/common/w25q80bv.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
#define W25Q80BV_WRITE_ENABLE 0x06
#define W25Q80BV_CHIP_ERASE 0xC7
#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 */

Expand Down Expand Up @@ -74,6 +76,17 @@ uint8_t w25q80bv_get_status(w25q80bv_driver_t* const drv)
return data[1];
}

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];
}

/* Release power down / Device ID */
uint8_t w25q80bv_get_device_id(w25q80bv_driver_t* const drv)
{
Expand Down Expand Up @@ -110,6 +123,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)
Expand Down
1 change: 1 addition & 0 deletions firmware/common/w25q80bv.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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);
Expand Down
3 changes: 2 additions & 1 deletion firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ static const usb_request_handler_fn vendor_request_handler[] = {
usb_vendor_request_operacake_set_ports,
usb_vendor_request_set_hw_sync_mode,
usb_vendor_request_reset,
usb_vendor_request_operacake_set_ranges
usb_vendor_request_operacake_set_ranges,
usb_vendor_request_spiflash_status
};

static const uint32_t vendor_request_handler_count =
Expand Down
15 changes: 15 additions & 0 deletions firmware/hackrf_usb/usb_api_spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,18 @@ 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;
}
}
2 changes: 2 additions & 0 deletions firmware/hackrf_usb/usb_api_spiflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ 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);

#endif /* end of include guard: __USB_API_SPIFLASH_H__ */
2 changes: 1 addition & 1 deletion firmware/libopencm3
Submodule libopencm3 updated 0 files
5 changes: 5 additions & 0 deletions host/hackrf-tools/src/hackrf_spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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;
Expand Down Expand Up @@ -278,6 +279,8 @@ int main(int argc, char** argv)
fd = NULL;
return EXIT_FAILURE;
}
hackrf_spiflash_status(device, status);
printf("Status: 0x%02x %02x\n", status[0], status[1]);
printf("Erasing SPI flash.\n");
result = hackrf_spiflash_erase(device);
if (result != HACKRF_SUCCESS) {
Expand All @@ -289,6 +292,8 @@ int main(int argc, char** argv)
}
if( !verbose ) printf("Writing %d bytes at 0x%06x.\n", length, address);
while (length) {
hackrf_spiflash_status(device, status);
printf("Status: 0x%02x %02x\n", status[0], status[1]);
xfer_len = (length > 256) ? 256 : length;
if( verbose ) printf("Writing %d bytes at 0x%06x.\n", xfer_len, address);
result = hackrf_spiflash_write(device, address, xfer_len, pdata);
Expand Down
25 changes: 25 additions & 0 deletions host/libhackrf/src/hackrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef enum {
HACKRF_VENDOR_REQUEST_SET_HW_SYNC_MODE = 29,
HACKRF_VENDOR_REQUEST_RESET = 30,
HACKRF_VENDOR_REQUEST_OPERACAKE_SET_RANGES = 31,
HACKRF_VENDOR_REQUEST_SPIFLASH_STATUS = 32,
} hackrf_vendor_request;

#define USB_CONFIG_STANDARD 0x1
Expand Down Expand Up @@ -957,6 +958,30 @@ int ADDCALL hackrf_spiflash_read(hackrf_device* device, const uint32_t address,
}
}

int ADDCALL hackrf_spiflash_status(hackrf_device* device, uint8_t* data)
{
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_cpld_write(hackrf_device* device,
unsigned char* const data, const unsigned int total_length)
{
Expand Down
2 changes: 1 addition & 1 deletion host/libhackrf/src/hackrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ 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);
/* device will need to be reset after hackrf_cpld_write */
extern ADDAPI int ADDCALL hackrf_cpld_write(hackrf_device* device,
unsigned char* const data, const unsigned int total_length);
Expand Down

0 comments on commit 8f544ee

Please sign in to comment.