Skip to content

Commit b7e6642

Browse files
committed
libbladeRF: Switch back to IF_CONFIG or IF_RF_LINK after flash ops
We need to switch out of IF_SPI_FLASH after flash operations in order to continue using the FPGA. This, in conjunction with firmware v1.6.1, fixes #196.
1 parent e79b748 commit b7e6642

2 files changed

Lines changed: 55 additions & 14 deletions

File tree

host/libraries/libbladeRF/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ project(libbladeRF C)
77

88
set(VERSION_INFO_MAJOR 0)
99
set(VERSION_INFO_MINOR 11)
10-
set(VERSION_INFO_PATCH 1)
10+
set(VERSION_INFO_PATCH 2)
1111

1212
if(NOT DEFINED VERSION_INFO_EXTRA)
1313
set(VERSION_INFO_EXTRA "git")

host/libraries/libbladeRF/src/backend/libusb.c

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,31 @@ static int change_setting(struct bladerf *dev, uint8_t setting)
368368
}
369369
}
370370

371+
/* After performing a flash operation, switch back to either RF_LINK or the
372+
* FPGA loader.
373+
*/
374+
static int restore_post_flash_setting(struct bladerf *dev)
375+
{
376+
int fpga_loaded = lusb_is_fpga_configured(dev);
377+
int status;
378+
379+
if (fpga_loaded < 0) {
380+
log_debug("Not restoring alt interface setting - failed to check FPGA state\n");
381+
status = fpga_loaded;
382+
} else if (fpga_loaded) {
383+
status = change_setting(dev, USB_IF_RF_LINK);
384+
} else {
385+
/* Make sure we are using the configuration interface */
386+
if (dev->legacy & LEGACY_CONFIG_IF) {
387+
status = change_setting(dev, USB_IF_LEGACY_CONFIG);
388+
} else {
389+
status = change_setting(dev, USB_IF_CONFIG);
390+
}
391+
}
392+
393+
return status;
394+
}
395+
371396
int lusb_enable_module(struct bladerf *dev, bladerf_module m, bool enable) {
372397
int status;
373398
int32_t fx3_ret = -1;
@@ -595,7 +620,10 @@ static int enable_rf(struct bladerf *dev) {
595620
return status;
596621
}
597622
log_verbose( "Changed into RF link mode: %s\n", libusb_error_name(status) ) ;
598-
ret_status = lusb_populate_fpga_version(dev) ;
623+
624+
/* We can only read the FPGA version once we've switched over to the
625+
* RF_LINK mode */
626+
ret_status = lusb_populate_fpga_version(dev);
599627
return ret_status;
600628
}
601629

@@ -994,7 +1022,12 @@ static int lusb_erase_flash(struct bladerf *dev, uint32_t addr, uint32_t len)
9941022
return status;
9951023
}
9961024

997-
return len;
1025+
status = restore_post_flash_setting(dev);
1026+
if (status != 0) {
1027+
return status;
1028+
} else {
1029+
return len;
1030+
}
9981031
}
9991032

10001033
static int read_buffer(struct bladerf *dev, uint8_t request,
@@ -1115,6 +1148,7 @@ static int legacy_read_one_page(struct bladerf *dev,
11151148
return 0;
11161149
}
11171150

1151+
/* Assumes the device is already configured for USB_IF_SPI_FLASH */
11181152
static int read_one_page(struct bladerf *dev, uint16_t page, uint8_t *buf)
11191153
{
11201154
int32_t read_status = -1;
@@ -1173,7 +1207,12 @@ static int lusb_read_flash(struct bladerf *dev, uint32_t addr,
11731207
read += BLADERF_FLASH_PAGE_SIZE;
11741208
}
11751209

1176-
return read;
1210+
status = restore_post_flash_setting(dev);
1211+
if (status != 0) {
1212+
return status;
1213+
} else {
1214+
return read;
1215+
}
11771216
}
11781217

11791218
static int compare_page_buffers(uint8_t *page_buf, uint8_t *image_page)
@@ -1195,6 +1234,11 @@ static int verify_one_page(struct bladerf *dev,
11951234
uint8_t page_buf[BLADERF_FLASH_PAGE_SIZE];
11961235
unsigned int i;
11971236

1237+
status = change_setting(dev, USB_IF_SPI_FLASH);
1238+
if (status) {
1239+
log_error("Failed to set interface: %s\n", libusb_error_name(status));
1240+
return BLADERF_ERR_IO;
1241+
}
11981242

11991243
log_debug("Verifying page 0x%04x.\n", flash_from_pages(page));
12001244
status = read_one_page(dev, page, page_buf);
@@ -1214,7 +1258,7 @@ static int verify_one_page(struct bladerf *dev,
12141258
return BLADERF_ERR_IO;
12151259
}
12161260

1217-
return 0;
1261+
return restore_post_flash_setting(dev);
12181262
}
12191263

12201264
static int verify_flash(struct bladerf *dev, uint32_t addr,
@@ -1370,19 +1414,19 @@ static int lusb_write_flash(struct bladerf *dev, uint32_t addr,
13701414
written += BLADERF_FLASH_PAGE_SIZE;
13711415
}
13721416

1373-
return written;
1417+
status = restore_post_flash_setting(dev);
1418+
if (status != 0) {
1419+
return status;
1420+
} else {
1421+
return written;
1422+
}
13741423
}
13751424

13761425
static int lusb_flash_firmware(struct bladerf *dev,
13771426
uint8_t *image, size_t image_size)
13781427
{
13791428
int status;
13801429

1381-
if(dev->legacy & LEGACY_CONFIG_IF)
1382-
log_debug("LEGACY_CONFIG_IF\n");
1383-
if(dev->legacy & LEGACY_ALT_SETTING)
1384-
log_debug("LEGACY_ALT_SETTING");
1385-
13861430
assert(image_size <= UINT32_MAX);
13871431

13881432
status = lusb_erase_flash(dev, 0, (uint32_t)image_size);
@@ -1395,9 +1439,6 @@ static int lusb_flash_firmware(struct bladerf *dev,
13951439
status = verify_flash(dev, 0, image, (uint32_t)image_size);
13961440
}
13971441

1398-
/* A reset will be required at this point, so there's no sense in
1399-
* bothering to set the interface back to USB_IF_RF_LINK */
1400-
14011442
return status;
14021443
}
14031444

0 commit comments

Comments
 (0)