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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [examples/] [usb-fpga-1.11/] [flashdemo/] [FlashDemo.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   flashdemo -- demo for Flash memory access from firmware and host software for ZTEX USB-FPGA Modules 1.11
3
   Copyright (C) 2009-2014 ZTEX GmbH.
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
                "    -ue          Upload Firmware to EEPROM\n" +
37
                "    -re          Reset EEPROM Firmware\n" +
38
                "    -w           Enable certain workarounds\n" +
39
                "    -h           This help" );
40
 
41
    public ParameterException (String msg) {
42
        super( msg + "\n" + helpMsg );
43
    }
44
}
45
 
46
// *****************************************************************************
47
// ******* Test0 ***************************************************************
48
// *****************************************************************************
49
class FlashDemo extends Ztex1v1 {
50
 
51
// ******* FlashDemo ***********************************************************
52
// constructor
53
    public FlashDemo ( ZtexDevice1 pDev ) throws UsbException {
54
        super ( pDev );
55
    }
56
 
57
// ******* main ****************************************************************
58
    public static void main (String args[]) {
59
 
60
        int devNum = 0;
61
        boolean force = false;
62
        boolean workarounds = false;
63
 
64
        try {
65
// init USB stuff
66
            LibusbJava.usb_init();
67
 
68
// scan the USB bus
69
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
70
            if ( bus.numberOfDevices() <= 0) {
71
                System.err.println("No devices found");
72
                System.exit(0);
73
            }
74
 
75
// scan the command line arguments
76
            for (int i=0; i<args.length; i++ ) {
77
                if ( args[i].equals("-d") ) {
78
                    i++;
79
                    try {
80
                        if (i>=args.length) throw new Exception();
81
                        devNum = Integer.parseInt( args[i] );
82
                    }
83
                    catch (Exception e) {
84
                        throw new ParameterException("Device number expected after -d");
85
                    }
86
                }
87
                else if ( args[i].equals("-f") ) {
88
                    force = true;
89
                }
90
                else if ( args[i].equals("-p") ) {
91
                    bus.printBus(System.out);
92
                    System.exit(0);
93
                }
94
                else if ( args[i].equals("-w") ) {
95
                    workarounds = true;
96
                }
97
                else if ( args[i].equals("-h") ) {
98
                        System.err.println(ParameterException.helpMsg);
99
                        System.exit(0);
100
                }
101
                else if ( !args[i].equals("-re") && !args[i].equals("-ue") )
102
                    throw new ParameterException("Invalid Parameter: "+args[i]);
103
            }
104
 
105
 
106
// create the main class            
107
            FlashDemo ztex = new FlashDemo ( bus.device(devNum) );
108
            ztex.certainWorkarounds = workarounds;
109
 
110
// upload the firmware if necessary
111
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("Flash demo for UFM 1.11")  ) {
112
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "flashdemo.ihx", force ) + " ms");
113
            }
114
 
115
            for (int i=0; i<args.length; i++ ) {
116
                if ( args[i].equals("-re") ) {
117
                    ztex.eepromDisable();
118
                }
119
                else if ( args[i].equals("-ue") ) {
120
                    System.out.println("Firmware to EEPROM upload time: " + ztex.eepromUpload( "flashdemo.ihx", force ) + " ms");
121
                }
122
            }
123
 
124
// print some information
125
            System.out.println("Capabilities: " + ztex.capabilityInfo(", "));
126
            System.out.println("Enabled: " + ztex.flashEnabled());
127
            System.out.println("Size: " + ztex.flashSize());
128
 
129
            byte[] buf = new byte[ztex.flashSectorSize()];
130
            ztex.flashReadSector(0,buf);         // read out the las sector;
131
            int sector = (buf[0] & 255) | ((buf[1] & 255) << 8) | ((buf[1] & 255) << 16) | ((buf[1] & 255) << 24);
132
            System.out.println("Last sector: "+sector);
133
 
134
            ztex.flashReadSector(sector,buf);           // read out the string
135
            int i=0;
136
            while ( buf[i] != '\0'&& i < ztex.flashSectorSize() )
137
                i++;
138
            System.out.println("The string: `" + new String(buf,0,i)+ "'");
139
 
140
        }
141
        catch (Exception e) {
142
            System.out.println("Error: "+e.getLocalizedMessage() );
143
        }
144
   }
145
 
146
}

powered by: WebSVN 2.1.0

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