OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_16/usb_fpga_2_16/trunk

Subversion Repositories usb_fpga_2_16

[/] [usb_fpga_2_16/] [trunk/] [examples/] [all/] [ucecho/] [c/] [UCEcho.c] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   UCEcho -- C host software for ucecho examples
3
   Copyright (C) 2009-2011 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <usb.h>
22
 
23
#define BUFSIZE  256
24
 
25
struct usb_device *device;
26
usb_dev_handle *handle;
27
char buf[BUFSIZE];
28
 
29
// find the first ucecho device
30
struct usb_device *find_device ()
31
{
32
    struct usb_bus *bus_search;
33
    struct usb_device *device_search;
34
 
35
    bus_search = usb_busses;
36
    while (bus_search != NULL)
37
    {
38
        device_search = bus_search->devices;
39
        while (device_search != NULL)
40
        {
41
            if ( (device_search->descriptor.idVendor == 0x221a) && (device_search->descriptor.idProduct == 0x100) )
42
            {
43
                handle = usb_open(device_search);
44
                usb_get_string_simple(handle, device_search->descriptor.iProduct, buf, BUFSIZE);
45
                if ( ! strncmp("ucecho", buf , 6 )  )
46
                    return device_search;
47
                usb_close(handle);
48
            }
49
            device_search = device_search->next;
50
        }
51
        bus_search = bus_search->next;
52
    }
53
 
54
    return NULL;
55
}
56
 
57
// main
58
int main(int argc, char *argv[])
59
{
60
    usb_init();                                         // initializing libusb
61
    usb_find_busses();                                  // ... finding busses
62
    usb_find_devices();                                 // ... and devices
63
 
64
    device = find_device();                             // find the device (hopefully the correct one)
65
 
66
    if ( device == NULL ) {                             // nothing found
67
        fprintf(stderr, "Cannot find ucecho device\n");
68
        return 1;
69
    }
70
 
71
    if (usb_claim_interface(handle, 0) < 0) {
72
        fprintf(stderr, "Error claiming interface 0: %s\n", usb_strerror());
73
        return 1;
74
    }
75
 
76
    while ( strcmp("QUIT", buf) ) {
77
        // read string from stdin
78
        printf("Enter a string or `quit' to exit the program: ");
79
        scanf("%s", buf);
80
 
81
        // write string to ucecho device 
82
        int i = usb_bulk_write(handle, 0x04, buf, strlen(buf)+1, 1000);
83
        if ( i < 0 ) {
84
            fprintf(stderr, "Error sending data: %s\n", usb_strerror());
85
            return 1;
86
        }
87
        printf("Send %d bytes: `%s'\n", i , buf);
88
 
89
        // read string back from ucecho device 
90
        i = usb_bulk_read(handle, 0x82, buf, BUFSIZE, 1000);
91
        if ( i < 0 ) {
92
            fprintf(stderr, "Error readin data: %s\n", usb_strerror());
93
            return 1;
94
        }
95
        printf("Read %d bytes: `%s'\n", i , buf);
96
 
97
    }
98
 
99
    usb_release_interface(handle, 0);
100
    usb_close(handle);
101
    return 0;
102
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.