| 1 |
2 |
ZTEX |
/*!
|
| 2 |
|
|
ucecho -- example for ZTEX USB FPGA Module 1.2
|
| 3 |
|
|
Copyright (C) 2008-2009 ZTEX e.K.
|
| 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 |
|
|
|
| 19 |
|
|
import java.io.*;
|
| 20 |
|
|
import java.util.*;
|
| 21 |
|
|
|
| 22 |
|
|
import ch.ntb.usb.*;
|
| 23 |
|
|
|
| 24 |
|
|
import ztex.*;
|
| 25 |
|
|
|
| 26 |
|
|
// *****************************************************************************
|
| 27 |
|
|
// ******* ParameterException **************************************************
|
| 28 |
|
|
// *****************************************************************************
|
| 29 |
|
|
// Exception the prints a help message
|
| 30 |
|
|
class ParameterException extends Exception {
|
| 31 |
|
|
public final static String helpMsg = new String (
|
| 32 |
|
|
"Parameters:\n"+
|
| 33 |
|
|
" -d <number> Device Number (default: 0)\n" +
|
| 34 |
|
|
" -f Force uploads\n" +
|
| 35 |
|
|
" -p Print bus info\n" +
|
| 36 |
|
|
" -w Enable certain workarounds which may be required for vmware + windows\n"+
|
| 37 |
|
|
" -h This help" );
|
| 38 |
|
|
|
| 39 |
|
|
public ParameterException (String msg) {
|
| 40 |
|
|
super( msg + "\n" + helpMsg );
|
| 41 |
|
|
}
|
| 42 |
|
|
}
|
| 43 |
|
|
|
| 44 |
|
|
// *****************************************************************************
|
| 45 |
|
|
// ******* Test0 ***************************************************************
|
| 46 |
|
|
// *****************************************************************************
|
| 47 |
|
|
class UCEcho extends Ztex1v1 {
|
| 48 |
|
|
|
| 49 |
|
|
// ******* UCEcho **************************************************************
|
| 50 |
|
|
// constructor
|
| 51 |
|
|
public UCEcho ( ZtexDevice1 pDev ) throws UsbException, ZtexDescriptorException {
|
| 52 |
|
|
super ( pDev );
|
| 53 |
|
|
}
|
| 54 |
|
|
|
| 55 |
|
|
// ******* claimInterface ******************************************************
|
| 56 |
|
|
// claims interface 0
|
| 57 |
|
|
public void claimInterface ( ) throws UsbException{
|
| 58 |
|
|
if ( LibusbJava.usb_set_configuration(handle(), 1) < 0 )
|
| 59 |
|
|
throw new UsbException("Setting configuration to 1 failed: " + LibusbJava.usb_strerror());
|
| 60 |
|
|
if ( LibusbJava.usb_claim_interface(handle(), 0) < 0 )
|
| 61 |
|
|
throw new UsbException("Claiming interface 0 failed: " + LibusbJava.usb_strerror());
|
| 62 |
|
|
}
|
| 63 |
|
|
|
| 64 |
|
|
// ******* releaseInterface ****************************************************
|
| 65 |
|
|
// releases interface 0
|
| 66 |
|
|
public void releaseInterface ( ) {
|
| 67 |
|
|
LibusbJava.usb_release_interface(handle(), 0);
|
| 68 |
|
|
}
|
| 69 |
|
|
|
| 70 |
|
|
// ******* echo ****************************************************************
|
| 71 |
|
|
// writes a string to Endpoint 4, reads it back from Endpoint 2 and writes the output to System.out
|
| 72 |
|
|
public void echo ( String input ) throws UsbException {
|
| 73 |
|
|
byte buf[] = input.getBytes();
|
| 74 |
|
|
int i = LibusbJava.usb_bulk_write(handle, 0x04, buf, buf.length, 1000);
|
| 75 |
|
|
if ( i<0 )
|
| 76 |
|
|
throw new UsbException("Error sending data: " + LibusbJava.usb_strerror());
|
| 77 |
|
|
System.out.println("Send "+i+" bytes: `"+input+"'" );
|
| 78 |
|
|
|
| 79 |
|
|
try {
|
| 80 |
|
|
Thread.sleep( 10 );
|
| 81 |
|
|
}
|
| 82 |
|
|
catch ( InterruptedException e ) {
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
buf = new byte[1024];
|
| 86 |
|
|
i = LibusbJava.usb_bulk_read(handle, 0x82, buf, 1024, 1000);
|
| 87 |
|
|
if ( i<0 )
|
| 88 |
|
|
throw new UsbException("Error receiving data: " + LibusbJava.usb_strerror());
|
| 89 |
|
|
System.out.println("Read "+i+" bytes: `"+new String(buf,0,i)+"'" );
|
| 90 |
|
|
}
|
| 91 |
|
|
|
| 92 |
|
|
// ******* main ****************************************************************
|
| 93 |
|
|
public static void main (String args[]) {
|
| 94 |
|
|
|
| 95 |
|
|
int devNum = 0;
|
| 96 |
|
|
boolean force = false;
|
| 97 |
|
|
boolean workarounds = false;
|
| 98 |
|
|
|
| 99 |
|
|
try {
|
| 100 |
|
|
// init USB stuff
|
| 101 |
|
|
LibusbJava.usb_init();
|
| 102 |
|
|
|
| 103 |
|
|
// scan the USB bus
|
| 104 |
|
|
ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.cypressVendorId, ZtexDevice1.cypressProductId, true, false, 1);
|
| 105 |
|
|
if ( bus.numberOfDevices() <= 0) {
|
| 106 |
|
|
System.err.println("No devices found");
|
| 107 |
|
|
System.exit(0);
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
// scan the command line arguments
|
| 111 |
|
|
for (int i=0; i<args.length; i++ ) {
|
| 112 |
|
|
if ( args[i].equals("-d") ) {
|
| 113 |
|
|
i++;
|
| 114 |
|
|
try {
|
| 115 |
|
|
if (i>=args.length) throw new Exception();
|
| 116 |
|
|
devNum = Integer.parseInt( args[i] );
|
| 117 |
|
|
}
|
| 118 |
|
|
catch (Exception e) {
|
| 119 |
|
|
throw new ParameterException("Device number expected after -d");
|
| 120 |
|
|
}
|
| 121 |
|
|
}
|
| 122 |
|
|
else if ( args[i].equals("-f") ) {
|
| 123 |
|
|
force = true;
|
| 124 |
|
|
}
|
| 125 |
|
|
else if ( args[i].equals("-p") ) {
|
| 126 |
|
|
bus.printBus(System.out);
|
| 127 |
|
|
System.exit(0);
|
| 128 |
|
|
}
|
| 129 |
|
|
else if ( args[i].equals("-p") ) {
|
| 130 |
|
|
bus.printBus(System.out);
|
| 131 |
|
|
System.exit(0);
|
| 132 |
|
|
}
|
| 133 |
|
|
else if ( args[i].equals("-w") ) {
|
| 134 |
|
|
workarounds = true;
|
| 135 |
|
|
}
|
| 136 |
|
|
else if ( args[i].equals("-h") ) {
|
| 137 |
|
|
System.err.println(ParameterException.helpMsg);
|
| 138 |
|
|
System.exit(0);
|
| 139 |
|
|
}
|
| 140 |
|
|
else throw new ParameterException("Invalid Parameter: "+args[i]);
|
| 141 |
|
|
}
|
| 142 |
|
|
|
| 143 |
|
|
|
| 144 |
|
|
// create the main class
|
| 145 |
|
|
UCEcho ztex = new UCEcho ( bus.device(devNum) );
|
| 146 |
|
|
ztex.certainWorkarounds = workarounds;
|
| 147 |
|
|
|
| 148 |
|
|
// upload the firmware if necessary
|
| 149 |
|
|
if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("ucecho for EZ-USB devices") ) {
|
| 150 |
|
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( "ucecho.ihx", force ) + " ms");
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
// claim interface 0
|
| 154 |
|
|
ztex.claimInterface();
|
| 155 |
|
|
|
| 156 |
|
|
// read string from stdin and write it to USB device
|
| 157 |
|
|
String str = "";
|
| 158 |
|
|
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
|
| 159 |
|
|
while ( ! str.equals("quit") ) {
|
| 160 |
|
|
System.out.print("Enter a string or `quit' to exit the program: ");
|
| 161 |
|
|
str = reader.readLine();
|
| 162 |
|
|
if ( ! str.equals("") )
|
| 163 |
|
|
ztex.echo(str);
|
| 164 |
|
|
System.out.println("");
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
// release interface 0
|
| 168 |
|
|
ztex.releaseInterface();
|
| 169 |
|
|
|
| 170 |
|
|
}
|
| 171 |
|
|
catch (Exception e) {
|
| 172 |
|
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
| 173 |
|
|
}
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
}
|