198 lines
5.2 KiB
C
198 lines
5.2 KiB
C
/* testlibusb.c */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/times.h>
|
|
#include <sys/time.h>
|
|
#include <usb.h>
|
|
|
|
|
|
void print_endpoint(struct usb_endpoint_descriptor *endpoint)
|
|
{
|
|
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
|
|
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
|
|
printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
|
|
printf(" bInterval: %d\n", endpoint->bInterval);
|
|
printf(" bRefresh: %d\n", endpoint->bRefresh);
|
|
printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
|
|
}
|
|
|
|
|
|
void print_altsetting(struct usb_interface_descriptor *interface)
|
|
{
|
|
int i;
|
|
|
|
printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
|
|
printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
|
|
printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
|
|
printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
|
|
printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
|
|
printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
|
|
printf(" iInterface: %d\n", interface->iInterface);
|
|
|
|
for (i = 0; i < interface->bNumEndpoints; i++)
|
|
print_endpoint(&interface->endpoint[i]);
|
|
}
|
|
|
|
|
|
void print_interface(struct usb_interface *interface)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < interface->num_altsetting; i++)
|
|
print_altsetting(&interface->altsetting[i]);
|
|
}
|
|
|
|
|
|
void print_configuration(struct usb_config_descriptor *config)
|
|
{
|
|
int i;
|
|
|
|
printf(" wTotalLength: %d\n", config->wTotalLength);
|
|
printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
|
|
printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
|
|
printf(" iConfiguration: %d\n", config->iConfiguration);
|
|
printf(" bmAttributes: %02xh\n", config->bmAttributes);
|
|
printf(" MaxPower: %d\n", config->MaxPower);
|
|
|
|
for (i = 0; i < config->bNumInterfaces; i++)
|
|
print_interface(&config->interface[i]);
|
|
}
|
|
#define BUFLEN (4*65536)
|
|
#define FLASHSIZE (8*1024*1024)
|
|
int main(void)
|
|
{
|
|
int nbusses, ndevs;
|
|
struct usb_bus *bus;
|
|
struct usb_device *dev;
|
|
char *pBuf;
|
|
int ret, i;
|
|
char string[256];
|
|
usb_dev_handle *udev;
|
|
int found;
|
|
double start, end;
|
|
struct timeval t;
|
|
FILE *pFile;
|
|
|
|
usb_init();
|
|
nbusses = usb_find_busses();
|
|
ndevs = usb_find_devices();
|
|
bus = usb_get_busses();
|
|
|
|
printf("bus/device idVendor/idProduct\n");
|
|
|
|
pBuf = (char*)malloc(BUFLEN);
|
|
memset(pBuf, 0, BUFLEN);
|
|
found = 0;
|
|
for (bus = usb_busses; bus; bus = bus->next)
|
|
{
|
|
for (dev = bus->devices; dev; dev = dev->next)
|
|
{
|
|
printf("%s/%s %04X/%04X\n", bus->dirname, dev->filename,
|
|
dev->descriptor.idVendor, dev->descriptor.idProduct);
|
|
|
|
udev = usb_open(dev);
|
|
if (udev)
|
|
{
|
|
if ((dev->descriptor.idVendor == 0x04B4) && (dev->descriptor.idProduct == 0x7200))
|
|
{
|
|
found = 1;
|
|
for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
|
|
print_configuration(&dev->config[i]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (found)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
typedef struct _scmd_t
|
|
{
|
|
unsigned sync;
|
|
unsigned code;
|
|
unsigned len;
|
|
} cmd_t;
|
|
|
|
cmd_t cmd_read = {0xC24755AA, 2, BUFLEN};
|
|
cmd_t cmd_write = {0xC24755AA, 1, BUFLEN};
|
|
cmd_t cmd_readflash = {0xC24755AA, 3, FLASHSIZE};
|
|
|
|
ret = usb_set_configuration(udev, 2);
|
|
if (ret < 0)
|
|
printf("- Unable to set configuration\n");
|
|
ret = usb_claim_interface(udev, 0);
|
|
if (ret < 0)
|
|
printf("- Unable to claim interface\n");
|
|
|
|
printf("Send command 1\n");
|
|
ret = usb_bulk_write(udev, 0x01, (char*)&cmd_write, sizeof(cmd_t), 5000);
|
|
if (ret < 0)
|
|
printf("- Unable to send\n");
|
|
gettimeofday(&t, NULL);
|
|
start = t.tv_sec + 1E-6*t.tv_usec;
|
|
ret = usb_bulk_write(udev, 0x01, (char*)pBuf, cmd_write.len, 5000);
|
|
gettimeofday(&t, NULL);
|
|
end = t.tv_sec + 1E-6*t.tv_usec;
|
|
if (ret < 0)
|
|
printf("- Unable to send\n");
|
|
|
|
printf("Data rate: %.2f kbyte/s\n", (double)BUFLEN/(end-start)/1024);
|
|
|
|
printf("Send command 2\n");
|
|
ret = usb_bulk_write(udev, 0x01, (char*)&cmd_read, sizeof(cmd_t), 5000);
|
|
if (ret < 0)
|
|
printf("- Unable to send\n");
|
|
|
|
printf("Read command 2\n");
|
|
gettimeofday(&t, NULL);
|
|
start = t.tv_sec + 1E-6*t.tv_usec;
|
|
ret = usb_bulk_read(udev, 0x02, (char*)pBuf, cmd_read.len, 5000);
|
|
gettimeofday(&t, NULL);
|
|
end = t.tv_sec + 1E-6*t.tv_usec;
|
|
if (ret < 0)
|
|
printf("- Unable to receive\n");
|
|
else
|
|
{
|
|
printf("Received %d bytes\n", ret);
|
|
printf("Data rate: %.2f kbyte/s\n", (double)BUFLEN/(end-start)/1024);
|
|
}
|
|
free(pBuf);
|
|
|
|
printf("Read Flash\n");
|
|
pBuf = (char*)malloc(FLASHSIZE);
|
|
memset(pBuf, 0, FLASHSIZE);
|
|
ret = usb_bulk_write(udev, 0x01, (char*)&cmd_readflash, sizeof(cmd_t), 5000);
|
|
if (ret < 0)
|
|
printf("- Unable to send\n");
|
|
|
|
gettimeofday(&t, NULL);
|
|
start = t.tv_sec + 1E-6*t.tv_usec;
|
|
ret = usb_bulk_read(udev, 0x02, (char*)pBuf, cmd_readflash.len, 500000);
|
|
gettimeofday(&t, NULL);
|
|
end = t.tv_sec + 1E-6*t.tv_usec;
|
|
if (ret < 0)
|
|
printf("- Unable to receive\n");
|
|
else
|
|
{
|
|
printf("Received %d bytes\n", ret);
|
|
printf("Data rate: %.2f kbyte/s\n", (double)FLASHSIZE/(end-start)/1024);
|
|
}
|
|
pFile = fopen("./flash.bin", "wb");
|
|
fwrite(pBuf, 1, FLASHSIZE, pFile);
|
|
fclose(pFile);
|
|
free(pBuf);
|
|
|
|
ret = usb_release_interface(udev, 0);
|
|
if (ret < 0)
|
|
printf("- Unable to release interface\n");
|
|
|
|
printf("Close\n");
|
|
usb_close (udev);
|
|
|
|
// sleep(1);
|
|
return 0;
|
|
}
|