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/] [Ztex1v1.java] - Diff between revs 3 and 4

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

Rev 3 Rev 4
Line 1... Line 1...
/*!
/*!
   Java Driver API for the ZTEX Firmware Kit
   Java Driver API for the ZTEX Firmware Kit
   Copyright (C) 2008-2009 ZTEX e.K.
   Copyright (C) 2009-2010 ZTEX e.K.
   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 224... Line 224...
 
 
    private boolean fpgaConfigured = false;
    private boolean fpgaConfigured = false;
    private int fpgaChecksum = 0;
    private int fpgaChecksum = 0;
    private int fpgaBytes = 0;
    private int fpgaBytes = 0;
    private int fpgaInitB = 0;
    private int fpgaInitB = 0;
 
    private int fpgaFlashResult = 255;
 
    private boolean fpgaFlashBitSwap = false;
 
 
    /** * Number of bytes written to EEPROM. (Obtained by {@link #eepromState()}.) */
    /** * Number of bytes written to EEPROM. (Obtained by {@link #eepromState()}.) */
    public int eepromBytes = 0;
    public int eepromBytes = 0;
    /** * Checksum of the last EEPROM transfer. (Obtained by {@link #eepromState()}.) */
    /** * Checksum of the last EEPROM transfer. (Obtained by {@link #eepromState()}.) */
    public int eepromChecksum = 0;
    public int eepromChecksum = 0;
Line 250... Line 252...
    public static final int FLASH_EC_PENDING = 4;
    public static final int FLASH_EC_PENDING = 4;
    /** * Signals an error while attempting to read from Flash. */
    /** * Signals an error while attempting to read from Flash. */
    public static final int FLASH_EC_READ_ERROR = 5;
    public static final int FLASH_EC_READ_ERROR = 5;
    /** * Signals an error while attempting to write to Flash. */
    /** * Signals an error while attempting to write to Flash. */
    public static final int FLASH_EC_WRITE_ERROR = 6;
    public static final int FLASH_EC_WRITE_ERROR = 6;
 
    /** * Signals the the installed Flash memeory is not supported. */
 
    public static final int FLASH_EC_NOTSUPPORTED = 7;
 
 
// ******* Ztex1v1 *************************************************************
// ******* Ztex1v1 *************************************************************
/**
/**
  * Constructs an instance from a given device.
  * Constructs an instance from a given device.
  * @param pDev The given device.
  * @param pDev The given device.
Line 344... Line 348...
            throw new InvalidFirmwareException(this, "Incompatible Product ID");
            throw new InvalidFirmwareException(this, "Incompatible Product ID");
    }
    }
 
 
// ******* getFpgaState ********************************************************
// ******* getFpgaState ********************************************************
    private void getFpgaState () throws UsbException, InvalidFirmwareException, CapabilityException {
    private void getFpgaState () throws UsbException, InvalidFirmwareException, CapabilityException {
        byte[] buffer = new byte[7];
        byte[] buffer = new byte[9];
        checkCapability(0,1);
        checkCapability(0,1);
        vendorRequest2(0x30, "getFpgaState", buffer, 7);
        vendorRequest2(0x30, "getFpgaState", buffer, 9);
        fpgaConfigured = buffer[0] == 0;
        fpgaConfigured = buffer[0] == 0;
        fpgaChecksum = buffer[1] & 0xff;
        fpgaChecksum = buffer[1] & 0xff;
        fpgaBytes = ((buffer[5] & 0xff)<<24) | ((buffer[4] & 0xff)<<16) | ((buffer[3] & 0xff)<<8) | (buffer[2] & 0xff);
        fpgaBytes = ((buffer[5] & 0xff)<<24) | ((buffer[4] & 0xff)<<16) | ((buffer[3] & 0xff)<<8) | (buffer[2] & 0xff);
        fpgaInitB = buffer[6] & 0xff;
        fpgaInitB = buffer[6] & 0xff;
 
        fpgaFlashResult = buffer[7];
 
        fpgaFlashBitSwap = buffer[8] != 0;
    }
    }
 
 
// ******* getFpgaState ********************************************************
// ******* printFpgaState ******************************************************
/**
/**
  * Prints out the FPGA state.
  * Prints out the FPGA state.
  * @throws InvalidFirmwareException if interface 1 is not supported.
  * @throws InvalidFirmwareException if interface 1 is not supported.
  * @throws UsbException if a communication error occurs.
  * @throws UsbException if a communication error occurs.
  * @throws CapabilityException if FPGA configuration is not supported by the firmware.
  * @throws CapabilityException if FPGA configuration is not supported by the firmware.
  */
  */
    public void printFpgaState () throws UsbException, InvalidFirmwareException, CapabilityException {
    public void printFpgaState () throws UsbException, InvalidFirmwareException, CapabilityException {
        getFpgaState();
        getFpgaState();
        System.out.println( "size=" + fpgaBytes + " ;  checksum=" + fpgaChecksum + "; INIT_B_HIST=" + fpgaInitB +" (should be 222)" );
        System.out.println( "size=" + fpgaBytes + ";  checksum=" + fpgaChecksum + "; INIT_B_HIST=" + fpgaInitB +" (should be 222); flash_configuration_result=" + fpgaFlashResult );
    }
    }
 
 
// ******* getFpgaConfiguration ************************************************
// ******* getFpgaConfiguration ************************************************
/**
/**
  * Returns true if the FPGA is configured.
  * Returns true if the FPGA is configured.
Line 482... Line 488...
                if ( ! fpgaConfigured ) {
                if ( ! fpgaConfigured ) {
                    throw new BitstreamUploadException( "FPGA configuration failed: DONE pin does not go high (size=" + fpgaBytes + " ,  " + (bs - fpgaBytes) + " bytes went lost;  checksum="
                    throw new BitstreamUploadException( "FPGA configuration failed: DONE pin does not go high (size=" + fpgaBytes + " ,  " + (bs - fpgaBytes) + " bytes went lost;  checksum="
                        + fpgaChecksum + " , should be " + cs + ";  INIT_B_HIST=" + fpgaInitB +", should be 222)" );
                        + fpgaChecksum + " , should be " + cs + ";  INIT_B_HIST=" + fpgaInitB +", should be 222)" );
                }
                }
//              System.out.println( "FPGA configuration: size=" + fpgaBytes + " ,  " + (bs - fpgaBytes) + " bytes went lost;  checksum=" + fpgaChecksum + " , should be " + cs + ";  INIT_B_HIST=" + fpgaInitB +", should be 222" );
//              System.out.println( "FPGA configuration: size=" + fpgaBytes + " ,  " + (bs - fpgaBytes) + " bytes went lost;  checksum=" + fpgaChecksum + " , should be " + cs + ";  INIT_B_HIST=" + fpgaInitB +", should be 222" );
                if ( fpgaInitB != 222 )
//              if ( fpgaInitB != 222 )
                    System.err.println ( "Warning: FPGA configuration may have failed: DONE pin has gone high but INIT_B states are wrong: " + fpgaInitB +", should be 222");
//                  System.err.println ( "Warning: FPGA configuration may have failed: DONE pin has gone high but INIT_B states are wrong: " + fpgaInitB +", should be 222");
 
 
                tries = 0;
                tries = 0;
                t0 += new Date().getTime();
                t0 += new Date().getTime();
 
 
            }
            }
Line 753... Line 759...
                return "Another operation is pending";
                return "Another operation is pending";
            case FLASH_EC_READ_ERROR:
            case FLASH_EC_READ_ERROR:
                return "Read error";
                return "Read error";
            case FLASH_EC_WRITE_ERROR:
            case FLASH_EC_WRITE_ERROR:
                return "Write error";
                return "Write error";
 
            case FLASH_EC_NOTSUPPORTED:
 
                return "Not supported";
        }
        }
        return "Error " + errNum;
        return "Error " + errNum;
    }
    }
 
 
/**
/**
Line 836... Line 844...
        checkCapability(0,2);
        checkCapability(0,2);
        if ( ! flashEnabled() )
        if ( ! flashEnabled() )
            throw new CapabilityException(this, "No Flash memory installed or");
            throw new CapabilityException(this, "No Flash memory installed or");
 
 
        try {
        try {
            vendorRequest2( 0x41, "Flash Read", sector, 0, buf, flashSectorSize );
            vendorRequest2( 0x41, "Flash Read", sector, sector >> 16, buf, flashSectorSize );
        }
        }
        catch ( UsbException e ) {
        catch ( UsbException e ) {
            throw new UsbException( dev().dev(), "Flash Read: " + flashStrError() );
            throw new UsbException( dev().dev(), "Flash Read: " + flashStrError() );
        }
        }
    }
    }
Line 859... Line 867...
        checkCapability(0,2);
        checkCapability(0,2);
        if ( ! flashEnabled() )
        if ( ! flashEnabled() )
            throw new CapabilityException(this, "No Flash memory installed or");
            throw new CapabilityException(this, "No Flash memory installed or");
 
 
        try {
        try {
            vendorCommand2( 0x42, "Flash Write", sector, 0, buf, flashSectorSize );
            vendorCommand2( 0x42, "Flash Write", sector, sector >> 16, buf, flashSectorSize );
        }
        }
        catch ( UsbException e ) {
        catch ( UsbException e ) {
            throw new UsbException( dev().dev(), "Flash Write: " + flashStrError() );
            throw new UsbException( dev().dev(), "Flash Write: " + flashStrError() );
        }
        }
    }
    }
Line 924... Line 932...
  * @throws InvalidFirmwareException if interface 1 is not supported.
  * @throws InvalidFirmwareException if interface 1 is not supported.
  * @throws UsbException if a communication error occurs.
  * @throws UsbException if a communication error occurs.
  * @throws CapabilityException if Flash memory access is not supported by the firmware.
  * @throws CapabilityException if Flash memory access is not supported by the firmware.
  */
  */
    public long flashSize () throws UsbException, InvalidFirmwareException, CapabilityException {
    public long flashSize () throws UsbException, InvalidFirmwareException, CapabilityException {
        return flashSectorSize() * flashSectors();
        return flashSectorSize() * (long)flashSectors();
    }
    }
 
 
// ******* printMmcState *******************************************************
// ******* printMmcState *******************************************************
// returns true if Flash is available
// returns true if Flash is available
/**
/**
Line 937... Line 945...
  * @throws InvalidFirmwareException if interface 1 is not supported.
  * @throws InvalidFirmwareException if interface 1 is not supported.
  * @throws UsbException if a communication error occurs.
  * @throws UsbException if a communication error occurs.
  * @throws CapabilityException if Flash memory access is not supported by the firmware.
  * @throws CapabilityException if Flash memory access is not supported by the firmware.
  */
  */
    public boolean printMmcState ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
    public boolean printMmcState ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
        byte[] buf = new byte[22];
        byte[] buf = new byte[23];
        checkCapability(0,2);
        checkCapability(0,2);
        vendorRequest2(0x43, "MMC State", 0, 0, buf, 22);
        vendorRequest2(0x43, "MMC State", 0, 0, buf, 23);
        System.out.println("status=" + Integer.toBinaryString(256+(buf[0] & 255)).substring(1) + "." + Integer.toBinaryString(256+(buf[1] & 255)).substring(1) +
        System.out.println("status=" + Integer.toBinaryString(256+(buf[0] & 255)).substring(1) + "." + Integer.toBinaryString(256+(buf[1] & 255)).substring(1) +
                "   lastCmd=" + buf[3] +
                "   lastCmd=" + buf[3] +
                "   lastCmdResponse=" + Integer.toBinaryString(256+(buf[4] & 255)).substring(1) +
                "   lastCmdResponse=" + Integer.toBinaryString(256+(buf[4] & 255)).substring(1) +
                "   ec=" + buf[2] +
                "   ec=" + buf[2] +
                "   BUSY=" + buf[21] +
                "   BUSY=" + buf[22] +
                "   buf=" + (buf[5] & 255)+" "+(buf[6] & 255)+" "+(buf[7] & 255)+" "+(buf[8] & 255)+" "+(buf[9] & 255)+" "+(buf[10] & 255)+"  "+(buf[11] & 255)); // +" "+(buf[12] & 255)+" "+(buf[13] & 255)+" "+(buf[14] & 255)+" "+(buf[15] & 255)+" "+(buf[16] & 255));
                "   SDHC=" + buf[5] +
 
                "   buf=" + (buf[6] & 255)+" "+(buf[7] & 255)+" "+(buf[8] & 255)+" "+(buf[9] & 255)+" "+(buf[10] & 255)+" "+(buf[11] & 255)+"  "+(buf[12] & 255)); // +" "+(buf[13] & 255)+" "+(buf[14] & 255)+" "+(buf[15] & 255)+" "+(buf[16] & 255)+" "+(buf[17] & 255));
 
 
        return flashEnabled == 1;
        return flashEnabled == 1;
    }
    }
 
 
// ******* flashUploadBitstream ************************************************
// ******* flashUploadBitstream ************************************************
Line 999... Line 1008...
  * @throws UsbException if a communication error occurs.
  * @throws UsbException if a communication error occurs.
  * @throws CapabilityException if Flash memory access is not possible.
  * @throws CapabilityException if Flash memory access is not possible.
  * @throws BitstreamReadException if an error occurred while attempting to read the Bitstream.
  * @throws BitstreamReadException if an error occurred while attempting to read the Bitstream.
  */
  */
    public long flashUploadBitstream ( String fwFileName ) throws BitstreamReadException, UsbException, InvalidFirmwareException, CapabilityException {
    public long flashUploadBitstream ( String fwFileName ) throws BitstreamReadException, UsbException, InvalidFirmwareException, CapabilityException {
 
        checkCapability(0,1);
        checkCapability(0,2);
        checkCapability(0,2);
        if ( ! flashEnabled() )
        if ( ! flashEnabled() )
            throw new CapabilityException(this, "No Flash memory installed or");
            throw new CapabilityException(this, "No Flash memory installed or");
 
        getFpgaState();
 
 
// read the Bitstream file      
// read the Bitstream file      
        byte[][] buffer = new byte[2048][];
        byte[][] buffer = new byte[2048][];
        int i,j;
        int i,j;
        try {
        try {
            InputStream inputStream = JInputStream.getInputStream( fwFileName );
            InputStream inputStream = JInputStream.getInputStream( fwFileName );
            j = flashSectorSize;
            j = flashSectorSize;
            for ( i=0; i<buffer.length && j==flashSectorSize; i++ ) {
            for ( i=0; i<buffer.length && j==flashSectorSize; i++ ) {
                buffer[i] = new byte[flashSectorSize];
                buffer[i] = new byte[flashSectorSize];
                j = inputStream.read( buffer[i] );
                j = inputStream.read( buffer[i] );
 
                if ( fpgaFlashBitSwap ) {
 
                    for (int k=0; k<flashSectorSize; k++ ) {
 
                        byte b = buffer[i][k];
 
                        buffer[i][k] = (byte) ( ((b & 128) >> 7) |
 
                                                ((b &  64) >> 5) |
 
                                                ((b &  32) >> 3) |
 
                                                ((b &  16) >> 1) |
 
                                                ((b &   8) << 1) |
 
                                                ((b &   4) << 3) |
 
                                                ((b &   2) << 5) |
 
                                                ((b &   1) << 7));
 
                    }
 
                }
                if ( j < 0 )
                if ( j < 0 )
                    j = 0;
                    j = 0;
            }
            }
 
 
            try {
            try {

powered by: WebSVN 2.1.0

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