Skip to content

Commit 77a37cb

Browse files
committed
Samples: Fix timeout error on FX3 and logging improvements for fxload
* Add a specific ezusb_fx3_jump() that handles timeout as a non-error This is required as a successful jump call makes the device disconnect from the bus * Set default verbosity to 1 and adjust some messages' verbosity level * Add a new -q option to decrease verbosity * Add readout of the bootloader version for FX3 devices * Filter the image types actually supported for FX3 * Fix the "errcode shadows a global variable" warning on some systems
1 parent e68c666 commit 77a37cb

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

examples/ezusb.c

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern void logerror(const char *format, ...)
4747
* The Cypress FX parts are largely compatible with the Anchorhip ones.
4848
*/
4949

50-
int verbose;
50+
int verbose = 1;
5151

5252
/*
5353
* return true if [addr,addr+len] includes external RAM
@@ -127,7 +127,7 @@ static int ezusb_write(libusb_device_handle *device, const char *label,
127127
{
128128
int status;
129129

130-
if (verbose)
130+
if (verbose > 1)
131131
logerror("%s, addr 0x%08x len %4u (0x%04x)\n", label, addr, (unsigned)len, (unsigned)len);
132132
status = libusb_control_transfer(device,
133133
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,
150150
{
151151
int status;
152152

153-
if (verbose)
153+
if (verbose > 1)
154154
logerror("%s, addr 0x%08x len %4u (0x%04x)\n", label, addr, (unsigned)len, (unsigned)len);
155155
status = libusb_control_transfer(device,
156156
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)
194194
return true;
195195
}
196196

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+
197224
/*****************************************************************************/
198225

199226
/*
@@ -527,14 +554,14 @@ static int ram_poke(void *context, uint32_t addr, bool external,
527554
}
528555

529556
/*
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.
532559
*/
533560
static int fx3_load_ram(libusb_device_handle *device, const char *path)
534561
{
535562
uint32_t dCheckSum, dExpectedCheckSum, dAddress, i, dLen, dLength;
536563
uint32_t* dImageBuf;
537-
unsigned char *bBuf, hBuf[4], rBuf[4096];
564+
unsigned char *bBuf, hBuf[4], blBuf[4], rBuf[4096];
538565
FILE *image;
539566

540567
image = fopen(path, "rb");
@@ -555,12 +582,36 @@ static int fx3_load_ram(libusb_device_handle *device, const char *path)
555582
logerror("image doesn't have a CYpress signature\n");
556583
return -3;
557584
}
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");
560594
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]);
561610
}
562611

563612
dCheckSum = 0;
613+
if (verbose)
614+
logerror("writing image...\n");
564615
while (1) {
565616
if ((fread(&dLength, sizeof(uint32_t), 1, image) != 1) || // read dLength
566617
(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)
621672
}
622673

623674
// 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)) {
626676
return -6;
627677
}
628678

@@ -659,7 +709,7 @@ int ezusb_load_ram(libusb_device_handle *device, const char *path, int fx_type,
659709
if (image == NULL) {
660710
logerror("%s: unable to open for input.\n", path);
661711
return -2;
662-
} else if (verbose)
712+
} else if (verbose > 1)
663713
logerror("open firmware image %s for RAM upload\n", path);
664714

665715
if (img_type == IMG_TYPE_IIC) {

examples/ezusb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extern int ezusb_load_ram(libusb_device_handle *device,
110110
extern int ezusb_load_eeprom(libusb_device_handle *device,
111111
const char *path, int fx_type, int img_type, int config);
112112

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 */
114114
extern int verbose;
115115

116116
#ifdef __cplusplus

examples/fxload.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ void logerror(const char *format, ...)
6464
va_end(ap);
6565
}
6666

67-
static int print_usage(int errcode) {
67+
static int print_usage(int error_code) {
6868
fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] -i firmware\n");
6969
fprintf(stderr, " -i <path> -- Firmware to upload\n");
7070
fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n");
7171
fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n");
7272
fprintf(stderr, " -p <bus,addr> -- Target device, as a libusbx bus number and device address path\n");
7373
fprintf(stderr, " -v -- Increase verbosity\n");
74+
fprintf(stderr, " -q -- Decrease verbosity (silent mode)\n");
7475
fprintf(stderr, " -V -- Print program version\n");
75-
return errcode;
76+
return error_code;
7677
}
7778

7879
#define FIRMWARE 0
@@ -94,7 +95,7 @@ int main(int argc, char*argv[])
9495
libusb_device_handle *device = NULL;
9596
struct libusb_device_descriptor desc;
9697

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)
9899
switch (opt) {
99100

100101
case 'd':
@@ -130,6 +131,10 @@ int main(int argc, char*argv[])
130131
verbose++;
131132
break;
132133

134+
case 'q':
135+
verbose--;
136+
break;
137+
133138
case '?':
134139
case 'h':
135140
default:
@@ -184,8 +189,8 @@ int main(int argc, char*argv[])
184189
} else {
185190
status = libusb_get_device_descriptor(dev, &desc);
186191
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",
189194
desc.idVendor, desc.idProduct, _busnum, _devaddr);
190195
}
191196
for (j=0; j<ARRAYSIZE(known_device); j++) {
@@ -274,7 +279,7 @@ int main(int argc, char*argv[])
274279
}
275280

276281
/* single stage, put into internal memory */
277-
if (verbose)
282+
if (verbose > 1)
278283
logerror("single stage: load on-chip memory\n");
279284
status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 0);
280285

libusb/version_nano.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define LIBUSB_NANO 10644
1+
#define LIBUSB_NANO 10645

0 commit comments

Comments
 (0)