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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   Java Driver API for the ZTEX Firmware Kit
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
/*
20
    Upload firmware to Cypress EZ-USB device
21
*/
22
package ztex;
23
 
24
import java.io.*;
25
import java.util.*;
26
 
27
import ch.ntb.usb.*;
28
 
29
public class EzUsb {
30
// ******* reset **************************************************************
31
    public static void reset ( int handle, boolean r ) throws FirmwareUploadException {
32
        byte buffer[] = { (byte) (r ? 1 : 0) };
33
        int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, 0xE600, 0, buffer, 1, 1000);   // upload j bytes
34
        if ( k<0 )
35
            throw new FirmwareUploadException( LibusbJava.usb_strerror() + ": unable to set reset="+buffer[0] );
36
        else if ( k!=1 )
37
            throw new FirmwareUploadException( "Unable to set reset="+buffer[0] );
38
        try {
39
            Thread.sleep( 50 );
40
        }
41
            catch ( InterruptedException e ) {
42
        }
43
    }
44
 
45
// ******* uploadFirmware ******************************************************
46
//  returns upload time in ms
47
    public static long uploadFirmware (int handle, IhxFile ihxFile ) throws FirmwareUploadException {
48
        final int transactionBytes = 256;
49
        byte[] buffer = new byte[transactionBytes];
50
        long t0 = new Date().getTime();
51
 
52
        reset( handle, true );  // reset = 1
53
 
54
        int j = 0;
55
        for ( int i=0; i<=ihxFile.ihxData.length; i++ ) {
56
            if ( i >= ihxFile.ihxData.length || ihxFile.ihxData[i] < 0 || j >=transactionBytes ) {
57
                if ( j > 0 ) {
58
                    int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, i-j, 0, buffer, j, 1000);   // upload j bytes
59
                    if ( k<0 )
60
                        throw new FirmwareUploadException(LibusbJava.usb_strerror());
61
                    else if ( k!=j )
62
                        throw new FirmwareUploadException();
63
                    try {
64
                        Thread.sleep( 1 );      // to avoid package loss
65
                    }
66
                        catch ( InterruptedException e ) {
67
                    }
68
                }
69
                j = 0;
70
            }
71
 
72
            if ( i < ihxFile.ihxData.length && ihxFile.ihxData[i] >= 0 && ihxFile.ihxData[i] <= 255 ) {
73
                buffer[j] = (byte) ihxFile.ihxData[i];
74
                j+=1;
75
            }
76
        }
77
 
78
        try {
79
            EzUsb.reset(handle,false);          // error (may caused re-numeration) can be ignored
80
        }
81
        catch ( FirmwareUploadException e ) {
82
        }
83
        return new Date().getTime() - t0;
84
    }
85
}
86
 

powered by: WebSVN 2.1.0

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