GeezerVison is showing github.com — Samples: Fix timeout error on FX3 and logging improvements for fxload · Raghuvardhan-Reddy78/libusb@77a37cb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: examples/ezusb.c +61-11Lines changed: 61 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,7 +47,7 @@ extern void logerror(const char *format, ...) | 47 | 47 | * The Cypress FX parts are largely compatible with the Anchorhip ones. | 48 | 48 | */ | 49 | 49 |
| 50 | | -int verbose; | | 50 | +int verbose = 1; | 51 | 51 |
| 52 | 52 | /* | 53 | 53 | * return true if [addr,addr+len] includes external RAM | @@ -127,7 +127,7 @@ static int ezusb_write(libusb_device_handle *device, const char *label, | 127 | 127 | { | 128 | 128 | int status; | 129 | 129 |
| 130 | | - if (verbose) | | 130 | + if (verbose > 1) | 131 | 131 | logerror("%s, addr 0x%08x len %4u (0x%04x)\n", label, addr, (unsigned)len, (unsigned)len); | 132 | 132 | status = libusb_control_transfer(device, | 133 | 133 | LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, | @@ -150,7 +150,7 @@ static int ezusb_read(libusb_device_handle *device, const char *label, | 150 | 150 | { | 151 | 151 | int status; | 152 | 152 |
| 153 | | - if (verbose) | | 153 | + if (verbose > 1) | 154 | 154 | logerror("%s, addr 0x%08x len %4u (0x%04x)\n", label, addr, (unsigned)len, (unsigned)len); | 155 | 155 | status = libusb_control_transfer(device, | 156 | 156 | LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, | @@ -194,6 +194,33 @@ static bool ezusb_cpucs(libusb_device_handle *device, uint32_t addr, bool doRun) | 194 | 194 | return true; | 195 | 195 | } | 196 | 196 |
| | 197 | +/* | | 198 | + * Send an FX3 jumpt to address command | | 199 | + * Returns false on error. | | 200 | + */ | | 201 | +static bool ezusb_fx3_jump(libusb_device_handle *device, uint32_t addr) | | 202 | +{ | | 203 | + int status; | | 204 | + | | 205 | + if (verbose) | | 206 | + logerror("transfer execution to Program Entry at 0x%08x\n", addr); | | 207 | + status = libusb_control_transfer(device, | | 208 | + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, | | 209 | + RW_INTERNAL, addr & 0xFFFF, addr >> 16, | | 210 | + NULL, 0, 1000); | | 211 | + /* We may get an I/O error from libusbx as the device disappears */ | | 212 | + if ((status != 0) && (status != LIBUSB_ERROR_IO)) | | 213 | + { | | 214 | + const char *mesg = "failed to send jump command"; | | 215 | + if (status < 0) | | 216 | + logerror("%s: %s\n", mesg, libusb_error_name(status)); | | 217 | + else | | 218 | + logerror("%s\n", mesg); | | 219 | + return false; | | 220 | + } else | | 221 | + return true; | | 222 | +} | | 223 | + | 197 | 224 | /*****************************************************************************/ | 198 | 225 |
| 199 | 226 | /* | @@ -527,14 +554,14 @@ static int ram_poke(void *context, uint32_t addr, bool external, | 527 | 554 | } | 528 | 555 |
| 529 | 556 | /* | 530 | | - * Load an Cypress Image file into target RAM. | 531 | | - * The file is assumed to be in Cypress IMG format. | | 557 | + * Load a Cypress Image file into target RAM. | | 558 | + * See http://www.cypress.com/?docID=41351 (AN76405 PDF) for more info. | 532 | 559 | */ | 533 | 560 | static int fx3_load_ram(libusb_device_handle *device, const char *path) | 534 | 561 | { | 535 | 562 | uint32_t dCheckSum, dExpectedCheckSum, dAddress, i, dLen, dLength; | 536 | 563 | uint32_t* dImageBuf; | 537 | | - unsigned char *bBuf, hBuf[4], rBuf[4096]; | | 564 | + unsigned char *bBuf, hBuf[4], blBuf[4], rBuf[4096]; | 538 | 565 | FILE *image; | 539 | 566 |
| 540 | 567 | image = fopen(path, "rb"); | @@ -555,12 +582,36 @@ static int fx3_load_ram(libusb_device_handle *device, const char *path) | 555 | 582 | logerror("image doesn't have a CYpress signature\n"); | 556 | 583 | return -3; | 557 | 584 | } | 558 | | - if (hBuf[3] != 0xB0) { | 559 | | - logerror("invalid file format 0x%02X, expected 0xB0\n", hBuf[3]); | | 585 | + | | 586 | + // Check bImageType | | 587 | + switch(hBuf[3]) { | | 588 | + case 0xB0: | | 589 | + if (verbose) | | 590 | + logerror("normal FW binary %s image with checksum\n", (hBuf[2]&0x01)?"data":"executable"); | | 591 | + break; | | 592 | + case 0xB1: | | 593 | + logerror("security binary image is not currently supported\n"); | 560 | 594 | return -3; | | 595 | + case 0xB2: | | 596 | + logerror("VID:PID image is not currently supported\n"); | | 597 | + return -3; | | 598 | + default: | | 599 | + logerror("invalid image type 0x%02X\n", hBuf[3]); | | 600 | + return -3; | | 601 | + } | | 602 | + | | 603 | + // Read the bootloader version | | 604 | + if (verbose) { | | 605 | + if ((ezusb_read(device, "read bootloader version", RW_INTERNAL, 0xFFFF0020, blBuf, 4) < 0)) { | | 606 | + logerror("Could not read bootloader version\n"); | | 607 | + return -8; | | 608 | + } | | 609 | + logerror("FX3 bootloader version: 0x%02X%02X%02X%02X\n", blBuf[3], blBuf[2], blBuf[1], blBuf[0]); | 561 | 610 | } | 562 | 611 |
| 563 | 612 | dCheckSum = 0; | | 613 | + if (verbose) | | 614 | + logerror("writing image...\n"); | 564 | 615 | while (1) { | 565 | 616 | if ((fread(&dLength, sizeof(uint32_t), 1, image) != 1) || // read dLength | 566 | 617 | (fread(&dAddress, sizeof(uint32_t), 1, image) != 1)) { // read dAddress | @@ -621,8 +672,7 @@ static int fx3_load_ram(libusb_device_handle *device, const char *path) | 621 | 672 | } | 622 | 673 |
| 623 | 674 | // transfer execution to Program Entry | 624 | | - if (ezusb_write(device, "Jump command", RW_INTERNAL, dAddress, NULL, 0) < 0) { | 625 | | - logerror("failed to send jump command\n"); | | 675 | + if (!ezusb_fx3_jump(device, dAddress)) { | 626 | 676 | return -6; | 627 | 677 | } | 628 | 678 |
| @@ -659,7 +709,7 @@ int ezusb_load_ram(libusb_device_handle *device, const char *path, int fx_type, | 659 | 709 | if (image == NULL) { | 660 | 710 | logerror("%s: unable to open for input.\n", path); | 661 | 711 | return -2; | 662 | | - } else if (verbose) | | 712 | + } else if (verbose > 1) | 663 | 713 | logerror("open firmware image %s for RAM upload\n", path); | 664 | 714 |
| 665 | 715 | if (img_type == IMG_TYPE_IIC) { | |
Collapse file Copy file name to clipboardExpand all lines: examples/ezusb.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -110,7 +110,7 @@ extern int ezusb_load_ram(libusb_device_handle *device, | 110 | 110 | extern int ezusb_load_eeprom(libusb_device_handle *device, | 111 | 111 | const char *path, int fx_type, int img_type, int config); | 112 | 112 |
| 113 | | -/* boolean flag, says whether to write extra messages to stderr */ | | 113 | +/* Verbosity level (default 1). Can be increased or decreased with options v/q */ | 114 | 114 | extern int verbose; | 115 | 115 |
| 116 | 116 | #ifdef __cplusplus | |
Collapse file Copy file name to clipboardExpand all lines: examples/fxload.c +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,15 +64,16 @@ void logerror(const char *format, ...) | 64 | 64 | va_end(ap); | 65 | 65 | } | 66 | 66 |
| 67 | | -static int print_usage(int errcode) { | | 67 | +static int print_usage(int error_code) { | 68 | 68 | fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] -i firmware\n"); | 69 | 69 | fprintf(stderr, " -i <path> -- Firmware to upload\n"); | 70 | 70 | fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n"); | 71 | 71 | fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n"); | 72 | 72 | fprintf(stderr, " -p <bus,addr> -- Target device, as a libusbx bus number and device address path\n"); | 73 | 73 | fprintf(stderr, " -v -- Increase verbosity\n"); | | 74 | + fprintf(stderr, " -q -- Decrease verbosity (silent mode)\n"); | 74 | 75 | fprintf(stderr, " -V -- Print program version\n"); | 75 | | - return errcode; | | 76 | + return error_code; | 76 | 77 | } | 77 | 78 |
| 78 | 79 | #define FIRMWARE 0 | @@ -94,7 +95,7 @@ int main(int argc, char*argv[]) | 94 | 95 | libusb_device_handle *device = NULL; | 95 | 96 | struct libusb_device_descriptor desc; | 96 | 97 |
| 97 | | - while ((opt = getopt(argc, argv, "vV?hd:p:i:I:t:")) != EOF) | | 98 | + while ((opt = getopt(argc, argv, "qvV?hd:p:i:I:t:")) != EOF) | 98 | 99 | switch (opt) { | 99 | 100 |
| 100 | 101 | case 'd': | @@ -130,6 +131,10 @@ int main(int argc, char*argv[]) | 130 | 131 | verbose++; | 131 | 132 | break; | 132 | 133 |
| | 134 | + case 'q': | | 135 | + verbose--; | | 136 | + break; | | 137 | + | 133 | 138 | case '?': | 134 | 139 | case 'h': | 135 | 140 | default: | @@ -184,8 +189,8 @@ int main(int argc, char*argv[]) | 184 | 189 | } else { | 185 | 190 | status = libusb_get_device_descriptor(dev, &desc); | 186 | 191 | if (status >= 0) { | 187 | | - if (verbose >= 2) { | 188 | | - logerror("trying to match against %04x:%04x (%d,%d)\n", | | 192 | + if (verbose >= 3) { | | 193 | + logerror("examining %04x:%04x (%d,%d)\n", | 189 | 194 | desc.idVendor, desc.idProduct, _busnum, _devaddr); | 190 | 195 | } | 191 | 196 | for (j=0; j<ARRAYSIZE(known_device); j++) { | @@ -274,7 +279,7 @@ int main(int argc, char*argv[]) | 274 | 279 | } | 275 | 280 |
| 276 | 281 | /* single stage, put into internal memory */ | 277 | | - if (verbose) | | 282 | + if (verbose > 1) | 278 | 283 | logerror("single stage: load on-chip memory\n"); | 279 | 284 | status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 0); | 280 | 285 |
| |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -#define LIBUSB_NANO 10644 | | 1 | +#define LIBUSB_NANO 10645 |
|