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/] [java/] [ztex/] [EzUsb.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   Java host software API of ZTEX SDK
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
package ztex;
27
 
28
import java.io.*;
29
import java.util.*;
30
import java.nio.*;
31
 
32
import org.usb4java.*;
33
 
34
/**
35
  * Provides methods for uploading firmware to Cypress EZ-USB devices.
36
  */
37
public class EzUsb {
38
// ******* reset **************************************************************
39
/**
40
  * Controls the reset state of a Cypress EZ-USB device.
41
  * @param handle The handle of the device.
42
  * @param r The reset state (true means reset).
43
  * @throws FirmwareUploadException if an error occurred while attempting to control the reset state.
44
  */
45
    public static void resetFx2 ( DeviceHandle handle, boolean r ) throws FirmwareUploadException {
46
        ByteBuffer buffer = BufferUtils.allocateByteBuffer(1);
47
        buffer.put(new byte[] { (byte) (r ? 1 : 0) });
48
 
49
        int k = LibUsb.controlTransfer(handle, (byte)0x40, (byte)(0xA0 & 255), (short)(0xE600 & 0xffff), (short)0, buffer, 1000);
50
        if ( k<0 )
51
            throw new FirmwareUploadException( LibUsb.strError(k) + ": unable to set reset="+r );
52
        else if ( k!=1 )
53
            throw new FirmwareUploadException( "Unable to set reset="+r );
54
        try {
55
            Thread.sleep( r ? 50 : 400 );       // give the firmware some time for initialization
56
        }
57
        catch ( InterruptedException e ) {
58
        }
59
    }
60
 
61
// ******* uploadFirmware ******************************************************
62
/**
63
  * Uploads the Firmware to a Cypress EZ-USB device.
64
  * @param handle The handle of the device.
65
  * @param imgFile The firmware image.
66
  * @return the upload time in ms.
67
  * @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
68
  */
69
    public static long uploadFirmware (DeviceHandle handle, ImgFile imgFile ) throws FirmwareUploadException {
70
        final int transactionBytes = 4096;
71
 
72
        ByteBuffer buffer = BufferUtils.allocateByteBuffer(transactionBytes).order(ByteOrder.LITTLE_ENDIAN);
73
 
74
        if ( !imgFile.isFx3 )
75
            resetFx2( handle, true );  // FX2 assumed, reset = 1
76
 
77
        long t0 = new Date().getTime();
78
        int j = 0;
79
        for ( int i=0; i<=imgFile.data.length; i++ ) {
80
            if ( i >= imgFile.data.length || imgFile.data[i] < 0 || j >=transactionBytes ) {
81
                if ( j > 0 ) {
82
                    long addr = ImgFile.uncompressAddr(i-j);
83
                    int k = LibUsb.controlTransfer(handle, (byte)0x40, (byte)(0xA0 & 255), (short)(addr & 0xffff), (short)(addr >> 16), BufferUtils.slice(buffer,0,j), 1000);   // upload j bytes
84
                    if ( k<0 )
85
                        throw new FirmwareUploadException(LibUsb.strError(k));
86
                    else if ( k!=j )
87
                        throw new FirmwareUploadException("sent "+k+" bytes, expected "+j);
88
                    try {
89
                        Thread.sleep( 1 );      // to avoid package loss
90
                    }
91
                        catch ( InterruptedException e ) {
92
                    }
93
                    buffer.clear();
94
                }
95
                j = 0;
96
            }
97
 
98
            if ( i < imgFile.data.length && imgFile.data[i] >= 0 && imgFile.data[i] <= 255 ) {
99
                buffer.put( (byte) imgFile.data[i] );
100
                j+=1;
101
            }
102
        }
103
        long t1 = new Date().getTime();
104
 
105
        if ( imgFile.startVector >=0 ) {
106
            LibUsb.controlTransfer(handle, (byte)0x40, (byte)(0xA0 & 255), (short)(imgFile.startVector & 0xffff), (short)(imgFile.startVector >> 16), ByteBuffer.allocateDirect(0), 1000);   // upload start vector
107
        }
108
 
109
        try {
110
            if ( !imgFile.isFx3 ) {
111
                resetFx2( handle, false );  // FX2 assumed, reset = 0, errors (due to renumeration) can be ignored
112
            }
113
        }
114
        catch ( FirmwareUploadException e ) {
115
        }
116
        return t1 - t0;
117
    }
118
}
119
 

powered by: WebSVN 2.1.0

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