Skip to content

Commit 3a413ca

Browse files
committed
Add CM3 support
1 parent 1169511 commit 3a413ca

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

usbboot/main.c

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <unistd.h>
77

88
int verbose = 0;
9+
int out_ep = 1;
10+
int in_ep = 2;
911

1012
void usage(int error)
1113
{
@@ -28,35 +30,105 @@ void usage(int error)
2830
exit(-1);
2931
}
3032

33+
libusb_device_handle * LIBUSB_CALL open_device_with_vid(
34+
libusb_context *ctx, uint16_t vendor_id)
35+
{
36+
struct libusb_device **devs;
37+
struct libusb_device *found = NULL;
38+
struct libusb_device *dev;
39+
struct libusb_device_handle *handle = NULL;
40+
size_t i = 0;
41+
int r;
42+
43+
if (libusb_get_device_list(ctx, &devs) < 0)
44+
return NULL;
45+
46+
while ((dev = devs[i++]) != NULL) {
47+
struct libusb_device_descriptor desc;
48+
r = libusb_get_device_descriptor(dev, &desc);
49+
if (r < 0)
50+
goto out;
51+
if(verbose)
52+
printf("Found device %d idVendor=0x%04x idProduct=0x%04x\n", i, desc.idVendor, desc.idProduct);
53+
if (desc.idVendor == vendor_id) {
54+
if(desc.idProduct == 0x2763 ||
55+
desc.idProduct == 0x2764)
56+
{
57+
if(verbose) printf("Device located successfully\n");
58+
found = dev;
59+
break;
60+
}
61+
}
62+
}
63+
64+
if (found) {
65+
r = libusb_open(found, &handle);
66+
if (r < 0)
67+
{
68+
if(verbose) printf("Failed to open the requested device\n");
69+
handle = NULL;
70+
}
71+
}
72+
73+
out:
74+
libusb_free_device_list(devs, 1);
75+
return handle;
76+
77+
}
3178

3279
int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device)
3380
{
3481
int ret = 0;
82+
int interface;
83+
struct libusb_config_descriptor *config;
3584

36-
*usb_device = libusb_open_device_with_vid_pid(*ctx, 0x0a5c, 0x2763);
85+
*usb_device = open_device_with_vid(*ctx, 0x0a5c);
3786
if (*usb_device == NULL)
3887
{
3988
return -1;
4089
}
4190

42-
ret = libusb_claim_interface(*usb_device, 0);
91+
libusb_get_active_config_descriptor(libusb_get_device(*usb_device), &config);
92+
93+
if(config->bNumInterfaces == 1)
94+
{
95+
interface = 0;
96+
out_ep = 1;
97+
in_ep = 2;
98+
}
99+
else
100+
{
101+
interface = 1;
102+
out_ep = 3;
103+
in_ep = 4;
104+
}
105+
106+
ret = libusb_claim_interface(*usb_device, interface);
43107
if (ret)
44108
{
45109
printf("Failed to claim interface\n");
46110
return ret;
47111
}
48112

113+
printf("Initialised device correctly\n");
114+
49115
return ret;
50116
}
51117

52118
int ep_write(unsigned char *buf, int len, libusb_device_handle * usb_device)
53119
{
120+
int a_len;
54121
int ret =
55122
libusb_control_transfer(usb_device, LIBUSB_REQUEST_TYPE_VENDOR, 0,
56123
len & 0xffff, len >> 16, NULL, 0, 1000);
57-
int a_len;
58124

59-
ret = libusb_bulk_transfer(usb_device, 0x01, buf, len, &a_len, 100000);
125+
if(ret != 0)
126+
{
127+
printf("Failed control transfer\n");
128+
return ret;
129+
}
130+
131+
ret = libusb_bulk_transfer(usb_device, out_ep, buf, len, &a_len, 100000);
60132
if(verbose)
61133
printf("libusb_bulk_transfer returned %d\n", ret);
62134

@@ -187,12 +259,13 @@ int main(int argc, char *argv[])
187259
exit(-1);
188260
}
189261

190-
libusb_set_debug(ctx, 0);
262+
libusb_set_debug(ctx, verbose ? LIBUSB_LOG_LEVEL_WARNING : 0);
191263

192264
do
193265
{
194266
FILE *fp_img = NULL;
195267
struct libusb_device_descriptor desc;
268+
struct libusb_config_descriptor *config;
196269

197270
printf("Waiting for BCM2835 ...\n");
198271

@@ -204,12 +277,15 @@ int main(int argc, char *argv[])
204277
{
205278
libusb_get_device_descriptor(libusb_get_device
206279
(usb_device), &desc);
280+
printf("Found serial number %d\n", desc.iSerialNumber);
207281
// Make sure we've re-enumerated since the last time
208282
if(desc.iSerialNumber == last_serial)
209283
{
210284
result = -1;
211285
libusb_close(usb_device);
212286
}
287+
288+
libusb_get_active_config_descriptor(libusb_get_device(usb_device), &config);
213289
}
214290

215291
if (result)
@@ -275,13 +351,14 @@ int main(int argc, char *argv[])
275351
exit(-1);
276352
}
277353

354+
sleep(1);
278355
size =
279356
ep_read((unsigned char *)&retcode, sizeof(retcode),
280357
usb_device);
281358

282359
if (retcode == 0)
283360
{
284-
if(verbose) printf("Successful\n");
361+
printf("Successful read %d bytes \n", size);
285362

286363
if(fp == fp2 && executable)
287364
{
@@ -292,6 +369,7 @@ int main(int argc, char *argv[])
292369
printf("Failed : 0x%x", retcode);
293370

294371
libusb_close(usb_device);
372+
sleep(1);
295373
}
296374
while(fp == fp1 || loop);
297375

usbboot/msd.elf

-134 KB
Binary file not shown.

usbboot/usbbootcode.bin

28 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)