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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [capi/] [c/] [ucecho.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   ZTEX Core API for C with examples
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
/** @file
27
ucecho example for C.
28
This example demonstrates the usage of the C API and the low speed interface of
29
default firmware.
30
 
31
The host software writes data to this interface, the FPGA converts it to
32
uppercase and stores it such that it can be read back from the host through the low speed interface.
33
 
34
The correct bitstream is detected and found automatically if the binary is executed from
35
the directory tree of the SDK. Otherwise it can be specified with parameter '-s'.
36
 
37
Full list of options can be obtained with '-h'
38
@include ucecho.c
39
@cond ucecho
40
*/
41
#include <stdio.h>
42
#include <stdarg.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <sys/types.h>
46
#include <fcntl.h>
47
#include <libusb-1.0/libusb.h>
48
 
49
#include "ztex.h"
50
 
51
static char* prog_name = NULL;          // name of the programm
52
 
53
static int paramerr(const char* format,...)
54
{
55
    fprintf(stderr, "Usage: %s options\n",prog_name);
56
    fprintf(stderr, "  -h                           Display this usage information\n"
57
                    "  -fu <vendor ID>:<product ID> Select device by USB IDs, default: 0x221A:0x100, <0:ignore ID\n"
58
                    "  -fd <bus>:<device>           Select device by bus number and device address\n"
59
                    "  -fs <string>                 Select device by serial number string\n"
60
                    "  -fp <string>                 Select device by product string\n"
61
                    "  -s <path>                    Addtional search path for bitstream, default '.."DIRSEP".."DIRSEP"examples"DIRSEP"ucecho'\n"
62
                    "  -r                           Reset device (default: reset configuration only)\n"
63
                    "  -i                           Print device info\n"
64
                    "  -p                           Print matching USB devices\n"
65
                    "  -pa                          Print all USB devices\n"
66
            );
67
    if ( format ) {
68
        va_list args;
69
        va_start(args,format);
70
        vfprintf(stderr, format, args);
71
        va_end(args);
72
        return 1;
73
    }
74
    return 0;
75
}
76
 
77
int main(int argc, char **argv)
78
{
79
    int id_vendor = 0x221A;     // ZTEX vendor ID
80
    int id_product = 0x100;     // default product ID for ZTEX firmware
81
    int status = 0;
82
    libusb_device **devs = NULL;
83
    int print_all=0, print=0, print_info=0, reset_dev=0;
84
    int busnum = -1, devnum = -1;
85
    char *sn_string = NULL, *product_string = NULL;
86
    libusb_device_handle *handle = NULL;
87
    ztex_device_info info;
88
    char *bitstream_fn = NULL, *bitstream_path = NULL;
89
    char sbuf[8192];
90
    uint8_t cbuf[1024];
91
    uint32_t vbuf[256];
92
    int vlen,slen;
93
 
94
    prog_name = argv[0];
95
    for (int i=1; i<argc; i++) {
96
        if ( !strcmp(argv[i],"-h") ) return paramerr(NULL);
97
        else if ( !strcmp(argv[i],"-p") ) print=1;
98
        else if ( !strcmp(argv[i],"-pa") ) print_all=1;
99
        else if ( !strcmp(argv[i],"-i") ) print_info=1;
100
        else if ( !strcmp(argv[i],"-r") ) reset_dev=1;
101
        else if ( !strcmp(argv[i],"-fu") ) {
102
            i++;
103
            if (i>=argc || sscanf(argv[i],"%i:%i", &id_vendor, &id_product)!=2 ) return paramerr("Error: <vendor ID>:<product ID> expected after -fu\n");
104
        }
105
        else if ( !strcmp(argv[i],"-fd") ) {
106
            i++;
107
            if (i>=argc || sscanf(argv[i],"%i:%i", &busnum, &devnum)!=2 ) return paramerr("Error: <bus>:<device> expected after -fd\n");
108
        }
109
        else if ( !strcmp(argv[i],"-fs") ) {
110
            i++;
111
            if (i>=argc ) return paramerr("Error: <string> expected after -fs\n");
112
            sn_string = argv[i];
113
        }
114
        else if ( !strcmp(argv[i],"-fp") ) {
115
            i++;
116
            if (i>=argc ) return paramerr("Error: <string> expected after -fp\n");
117
            product_string = argv[i];
118
        }
119
        else if ( !strcmp(argv[i],"-s") ) {
120
            i++;
121
            if (i>=argc ) return paramerr("Error: <path> expected after -s\n");
122
            bitstream_path = argv[i];
123
        }
124
        else return paramerr("Error: Invalid parameter %s\n", argv[i]);
125
    }
126
 
127
    // INIT libusb
128
    status = libusb_init(NULL);
129
    if (status < 0) {
130
        fprintf(stderr,"Error: Unable to init libusb: %s\n", libusb_error_name(status));
131
        return 1;
132
    }
133
 
134
    // find all USB devices
135
    status = libusb_get_device_list(NULL, &devs);
136
    if (status < 0) {
137
        fprintf(stderr,"Error: Unable to get device list: %s\n", libusb_error_name(status));
138
        goto err;
139
    }
140
 
141
    // print bus info or find device
142
    int dev_idx = ztex_scan_bus(sbuf, sizeof(sbuf), devs, print_all ? -1 : print ? 1 : 0, id_vendor, id_product, busnum, devnum, sn_string, product_string);
143
    printf(sbuf);
144
    if ( print || print_all ) {
145
        status = 0;
146
        goto noerr;
147
    } else if ( dev_idx<0 ) {
148
        if (dev_idx==-1) fprintf(stderr,"Error: No device found\n");
149
        goto err;
150
    }
151
 
152
    // open device
153
    status = libusb_open(devs[dev_idx], &handle);
154
    if (status < 0) {
155
        fprintf(stderr,"Error: Unable to open device: %s\n", libusb_error_name(status));
156
        goto err;
157
    }
158
    libusb_free_device_list(devs, 1);
159
    devs=NULL;
160
    libusb_set_configuration(handle,1);
161
 
162
    // reset configuration or device
163
   if ( ! reset_dev ) {
164
        status = libusb_set_configuration(handle,-1);
165
        if (status < 0) {
166
            fprintf(stderr,"Warning: Unable to unconfigure device: %s, trying to reset it\n", libusb_error_name(status));
167
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
168
            fprintf(stderr,"Due to limitations of Windows neither this nor device reset works. This may cause further errors ...\n");
169
#endif    
170
            reset_dev = 1;
171
        }
172
    }
173
    if ( reset_dev ) {
174
        status = libusb_reset_device(handle);
175
        if (status < 0) {
176
            fprintf(stderr,"Error: Unable to reset device: %s\n", libusb_error_name(status));
177
            goto err;
178
        }
179
    }
180
    status = libusb_set_configuration(handle,1);
181
    if (status < 0) fprintf(stderr,"Warning: Unable to set configuration 1: %s\n", libusb_error_name(status));
182
    fflush(stderr);
183
 
184
 
185
    // get and print device info
186
    status = ztex_get_device_info(handle, &info);
187
    if ( status < 0 ) {
188
        fprintf(stderr,"Error: Unable to get device info: %s\n", libusb_error_name(status));
189
        goto err;
190
    }
191
    if ( print_info ) {
192
        ztex_print_device_info( sbuf, sizeof(sbuf), &info );
193
        printf(sbuf);
194
        status = ztex_get_fpga_config(handle);
195
        if ( status < 0 ) {
196
            fprintf(stderr,"Error: Unable to get FPGA configuration state: %s\n", libusb_error_name(status));
197
            goto err;
198
        }
199
        printf("FPGA: %s\n", status==0 ? "unconfigured" : "configured");
200
        status = 0;
201
        goto noerr;
202
    }
203
    fflush(stderr);
204
 
205
    // find bitstream
206
    bitstream_fn = ztex_find_bitstream( &info, bitstream_path ? bitstream_path : ".."DIRSEP".."DIRSEP"examples"DIRSEP"ucecho" , "ucecho");
207
    if ( bitstream_fn )  {
208
        printf("Using bitstream '%s'\n", bitstream_fn);
209
        fflush(stdout);
210
    }
211
    else {
212
        fprintf(stderr,"Warning: Bitstream not found\n");
213
        goto nobitstream;
214
    }
215
 
216
    // read and upload bitstream
217
    FILE *fd = fopen(bitstream_fn, "rb");
218
    if ( fd == NULL ) {
219
        fprintf(stderr,"Warning: Error opening file '%s'\n", bitstream_fn);
220
        goto nobitstream;
221
    }
222
    status = ztex_upload_bitstream(sbuf,sizeof(sbuf),handle,&info,fd,-1);
223
    fclose(fd);
224
    fprintf(stderr,sbuf);
225
 
226
nobitstream:
227
    fflush(stderr);
228
    status = ztex_get_fpga_config(handle);
229
    if ( status < 0 ) {
230
        fprintf(stderr,"Error: Unable to get FPGA configuration state: %s\n", libusb_error_name(status));
231
        goto err;
232
    } else if ( status == 0 ) {
233
        fprintf(stderr,"Error: FPGA not configured\n");
234
        goto err;
235
    }
236
 
237
    // ucecho test
238
    sbuf[0]=0;
239
    while ( strcmp(sbuf,"quit") ) {
240
        printf("Enter a string or `quit' to exit the program: ");
241
        fflush(stdout);
242
        fgets(sbuf, sizeof(sbuf)-1, stdin);
243
        slen = strlen(sbuf);
244
        while ( (slen>0) && (sbuf[slen-1]<32) ) slen--;
245
        sbuf[slen]=0;
246
        if ( sbuf[0] ) {
247
            vlen = (slen+3)>>2;
248
            if ( vlen > 256 ) vlen = 256;
249
            memcpy(cbuf, sbuf, vlen*4);
250
            for (int i=0; i<vlen*4; i++)
251
                vbuf[i] = cbuf[i*4+0] | (cbuf[i*4+1]<<8) | (cbuf[i*4+2]<<16) | (cbuf[i*4+3]<<24);
252
            printf("Send %d words to address 10 ...\n",vlen);
253
            status = ztex_default_lsi_set2(handle,10,vbuf,vlen);
254
            if ( status<0 ) {
255
                fprintf(stderr,"Warning: Error writing to LSI: %s\n", strerror(status));
256
                goto err;
257
            }
258
 
259
            status = ztex_default_lsi_get2(handle,10,vbuf,vlen);
260
            if ( status<0 ) {
261
                fprintf(stderr,"Warning: Error reading from to LSI: %s\n", strerror(status));
262
                goto err;
263
            }
264
            for (int i=0; i<vlen; i++) {
265
                cbuf[i*4+0] = vbuf[i];
266
                cbuf[i*4+1] = vbuf[i]>>8;
267
                cbuf[i*4+2] = vbuf[i]>>16;
268
                cbuf[i*4+3] = vbuf[i]>>24;
269
            }
270
            cbuf[slen]=0;
271
            printf("Read %d words starting from 10: %s\n", vlen, cbuf );
272
 
273
            if ( vlen>1 ) {
274
                status = ztex_default_lsi_get2(handle,11,vbuf,vlen);
275
                if ( status<0 ) {
276
                    fprintf(stderr,"Warning: Error reading from to LSI: %s\n", strerror(status));
277
                    goto err;
278
                }
279
                for (int i=0; i<vlen; i++) {
280
                    cbuf[i*4+0] = vbuf[i];
281
                    cbuf[i*4+1] = vbuf[i]>>8;
282
                    cbuf[i*4+2] = vbuf[i]>>16;
283
                    cbuf[i*4+3] = vbuf[i]>>24;
284
                }
285
                cbuf[slen-4]=0;
286
                printf("Read %d words starting from 11: %s\n", vlen-1, cbuf );
287
            }
288
        }
289
        printf("\n");
290
    }
291
 
292
    // release resources
293
    status = 0;
294
    goto noerr;
295
err:
296
    status = 1;
297
noerr:
298
    if ( bitstream_fn ) free(bitstream_fn);
299
    if ( handle ) libusb_close(handle);
300
    if ( devs ) libusb_free_device_list(devs, 1);
301
    libusb_exit(NULL);
302
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
303
    if ( strcmp(sbuf,"quit") ) {
304
        printf("Press <return> to quit\n");
305
        fflush(NULL);
306
        fgetc(stdin);
307
    }
308
#endif    
309
    return status;
310
}
311
///@endcond

powered by: WebSVN 2.1.0

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