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

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [java/] [ztex/] [ZtexDevice1.java] - Diff between revs 4 and 8

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 4 Rev 8
Line 1... Line 1...
/*!
/*!
   Java Driver API for the ZTEX Firmware Kit
   Java host software API of ZTEX EZ-USB FX2 SDK
   Copyright (C) 2009-2010 ZTEX e.K.
   Copyright (C) 2009-2011 ZTEX GmbH.
   http://www.ztex.de
   http://www.ztex.de
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3 as
   it under the terms of the GNU General Public License version 3 as
   published by the Free Software Foundation.
   published by the Free Software Foundation.
Line 139... Line 139...
  * @see #ztexProductId
  * @see #ztexProductId
  */
  */
    public static final int ztexProductIdMax = 0x1ff;
    public static final int ztexProductIdMax = 0x1ff;
 
 
    private Usb_Device dev = null;
    private Usb_Device dev = null;
    private boolean isCypress = false;          // true if Cypress device
 
    private boolean valid = false;              // true if descriptor 1 is available
    private boolean valid = false;              // true if descriptor 1 is available
    private int usbVendorId = -1;
    private int usbVendorId = -1;
    private int usbProductId = -1;
    private int usbProductId = -1;
    private String manufacturerString = null;
    private String manufacturerString = null;
    private String productString = null;
    private String productString = null;
Line 178... Line 177...
  * To suppress this behavior (e.g. if the EZ-USB device is known to be unconfigured) the vendor and product id's
  * To suppress this behavior (e.g. if the EZ-USB device is known to be unconfigured) the vendor and product id's
  * can be set to -1.
  * can be set to -1.
  * @param p_dev The USB device.
  * @param p_dev The USB device.
  * @param pUsbVendorId The given vendor ID.
  * @param pUsbVendorId The given vendor ID.
  * @param pUsbProductId The given product ID.
  * @param pUsbProductId The given product ID.
 
  * @param allowUnconfigured If true, unconfigured devices are allowed.
  * @throws UsbException if an USB communication error occurs.
  * @throws UsbException if an USB communication error occurs.
  * @throws InvalidFirmwareException if no valid ZTEX descriptor 1 is found.
  * @throws InvalidFirmwareException if no valid ZTEX descriptor 1 is found.
 
  * @throws DeviceNotSupported if the device has the wrong USB ID's.
  */
  */
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId) throws UsbException, InvalidFirmwareException {
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId, boolean allowUnconfigured) throws UsbException, InvalidFirmwareException, DeviceNotSupportedException  {
        dev = p_dev;
        dev = p_dev;
 
 
        Usb_Device_Descriptor dd = dev().getDescriptor();
        Usb_Device_Descriptor dd = dev().getDescriptor();
        usbVendorId = dd.getIdVendor() & 65535;
        usbVendorId = dd.getIdVendor() & 65535;
        usbProductId = dd.getIdProduct() & 65535;
        usbProductId = dd.getIdProduct() & 65535;
 
 
        if ( usbVendorId == cypressVendorId  &&  usbProductId == cypressProductId ) {
        if ( ! ( (
            isCypress = true;
                   (usbVendorId == pUsbVendorId) &&
        }
                   (usbProductId == pUsbProductId || ( usbVendorId == ztexVendorId && pUsbProductId<0 && usbProductId>=ztexProductId && usbProductId<ztexProductIdMax ) )
 
                 ) ||
 
                 (
 
                   allowUnconfigured && (usbVendorId == cypressVendorId) && (usbProductId == cypressProductId)
 
                 ) )  )
 
            throw new DeviceNotSupportedException(p_dev);
 
 
        int handle = LibusbJava.usb_open(dev);
        long handle = LibusbJava.usb_open(dev);
 
 
//      if ( handle > 0 ) {
//      if ( handle > 0 ) {
            if ( dd.getIManufacturer() > 0 )
            if ( dd.getIManufacturer() > 0 )
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
            if ( dd.getIProduct() > 0 )
            if ( dd.getIProduct() > 0 )
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
            if ( dd.getISerialNumber() > 0 )
            if ( dd.getISerialNumber() > 0 )
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
 
 
            if ( usbVendorId == pUsbVendorId && (usbProductId == pUsbProductId || ( usbVendorId == ztexVendorId && pUsbProductId<0 && usbProductId>=ztexProductId && usbProductId<ztexProductIdMax ) ) ) {
 
                if ( snString == null ) {
                if ( snString == null ) {
                    LibusbJava.usb_close(handle);
                    LibusbJava.usb_close(handle);
 
                if ( allowUnconfigured )
 
                    return;
 
                else
                    throw new InvalidFirmwareException( dev, "Not a ZTEX device" );  // ZTEX devices always have a SN. See also the next comment a few lines below
                    throw new InvalidFirmwareException( dev, "Not a ZTEX device" );  // ZTEX devices always have a SN. See also the next comment a few lines below
                }
                }
 
 
                byte[] buf = new byte[42];
                byte[] buf = new byte[42];
                int i = LibusbJava.usb_control_msg(handle, 0xc0, 0x22, 0, 0, buf, 40, 500);       // Failing of this may cause problems under windows. Therefore we check for the SN above.
                int i = LibusbJava.usb_control_msg(handle, 0xc0, 0x22, 0, 0, buf, 40, 500);       // Failing of this may cause problems under windows. Therefore we check for the SN above.
                if ( i < 0 ) {
                if ( i < 0 ) {
                    LibusbJava.usb_close(handle);
                    LibusbJava.usb_close(handle);
 
                if ( allowUnconfigured )
 
                    return;
 
                else
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
                }
                }
                else if ( i != 40 ) {
                else if ( i != 40 ) {
                    LibusbJava.usb_close(handle);
                    LibusbJava.usb_close(handle);
 
                if ( allowUnconfigured )
 
                    return;
 
                else
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
                }
                }
                if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
                if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
                    LibusbJava.usb_close(handle);
                    LibusbJava.usb_close(handle);
 
                if ( allowUnconfigured )
 
                    return;
 
                else
                    throw new InvalidFirmwareException( dev, "Invalid ZTEX descriptor" );
                    throw new InvalidFirmwareException( dev, "Invalid ZTEX descriptor" );
                }
                }
                productId[0] = buf[6];
                productId[0] = buf[6];
                productId[1] = buf[7];
                productId[1] = buf[7];
                productId[2] = buf[8];
                productId[2] = buf[8];
Line 248... Line 265...
                moduleReserved[9] = buf[27];
                moduleReserved[9] = buf[27];
                moduleReserved[10] = buf[28];
                moduleReserved[10] = buf[28];
                moduleReserved[11] = buf[29];
                moduleReserved[11] = buf[29];
 
 
                valid = true;
                valid = true;
            }
 
//      }
//      }
//      else {
//      else {
//          throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
//          throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
//      }
//      }
 
 
Line 263... Line 280...
/**
/**
  * Returns a string representation if the device with a lot of useful information.
  * Returns a string representation if the device with a lot of useful information.
  * @return a string representation if the device with a lot of useful information.
  * @return a string representation if the device with a lot of useful information.
  */
  */
    public String toString () {
    public String toString () {
        return "bus=" + dev().getBus().getDirname() + "  device=" + dev().getFilename() + "  ID=" + Integer.toHexString(usbVendorId) + ":" + Integer.toHexString(usbProductId) +
 
            "\n   " + ( isCypress ? "Cypress" : "" ) +
        return "bus=" + dev().getBus().getDirname() + "  device=" + dev().getDevnum() + " (`" + dev().getFilename() + "')  ID=" + Integer.toHexString(usbVendorId) + ":" + Integer.toHexString(usbProductId) +"\n"  +
              ( manufacturerString == null ? "" : ("    Manufacturer=\""  + manufacturerString + "\"") ) +
              ( manufacturerString == null ? "" : ("    Manufacturer=\""  + manufacturerString + "\"") ) +
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
            ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
            ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
    }
    }
Line 301... Line 318...
  */
  */
    public final Usb_Device dev() {
    public final Usb_Device dev() {
        return dev;
        return dev;
    }
    }
 
 
// ******* isCypress ***********************************************************
 
/**
 
  * Returns true if the device has Cypress EZ-USB vendor and product ID's (0x4b4 and 0x8613).
 
  * @return true if the device has Cypress EZ-USB vendor and product ID's (0x4b4 and 0x8613).
 
  */
 
    public final boolean isCypress() {
 
        return isCypress;
 
    }
 
 
 
// ******* valid ***************************************************************
// ******* valid ***************************************************************
/**
/**
  * Returns true if ZTEX descriptor 1 is available.
  * Returns true if ZTEX descriptor 1 is available.
  * @return true if ZTEX descriptor 1 is available.
  * @return true if ZTEX descriptor 1 is available.
  */
  */

powered by: WebSVN 2.1.0

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