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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   fx3sdemo -- Demonstrates common features of the FX3S
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   Licensed under the Apache License, Version 2.0 (the "License");
7
   you may not use this file except in compliance with the License.
8
   You may obtain a copy of the License at
9
 
10
       http://www.apache.org/licenses/LICENSE-2.0
11
 
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
%*/
18
 
19
import java.io.*;
20
import java.util.*;
21
import java.nio.*;
22
 
23
import org.usb4java.*;
24
 
25
import ztex.*;
26
 
27
// *****************************************************************************
28
// ******* ParameterException **************************************************
29
// *****************************************************************************
30
// Exception the prints a help message
31
class ParameterException extends Exception {
32
    public final static String helpMsg = new String (
33
                "Parameters:\n"+
34
                "    -d <number>       Device number (default: 0)\n" +
35
                "    -f                Force uploads\n" +
36
                "    -p                Print bus info\n" +
37
                "    -h                This help" );
38
 
39
    public ParameterException (String msg) {
40
        super( msg + "\n" + helpMsg );
41
    }
42
}
43
 
44
// *****************************************************************************
45
// ******* FX3SDemo ************************************************************
46
// *****************************************************************************
47
class FX3SDemo extends Ztex1v1 {
48
 
49
// ******* Debug ***************************************************************
50
// constructor
51
    public FX3SDemo ( ZtexDevice1 pDev ) throws UsbException {
52
        super ( pDev );
53
    }
54
 
55
// ******* main ****************************************************************
56
    public static void main (String args[]) {
57
 
58
        int devNum = 0;
59
        boolean force = false;
60
        String bitStream = null;
61
 
62
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
63
            Runtime.getRuntime().addShutdownHook(new Thread() {
64
                public void run() {
65
                    Scanner s=new Scanner(System.in);
66
                    System.out.println("Press <enter> to continue ...");
67
                    s.nextLine();
68
                }
69
            });
70
        }
71
 
72
        try {
73
// init USB stuff
74
 
75
// Scan the USB. This also creates and initializes a new USB context.
76
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
77
 
78
// scan the command line arguments
79
            for (int i=0; i<args.length; i++ ) {
80
                if ( args[i].equals("-d") ) {
81
                    i++;
82
                    try {
83
                        if (i>=args.length) throw new Exception();
84
                        devNum = Integer.parseInt( args[i] );
85
                    }
86
                    catch (Exception e) {
87
                        throw new ParameterException("Device number expected after -d");
88
                    }
89
                }
90
                if ( args[i].equals("-b") ) {
91
                    i++;
92
                    try {
93
                        if (i>=args.length) throw new Exception();
94
                        bitStream =args[i];
95
                    }
96
                    catch (Exception e) {
97
                        throw new ParameterException("Bitstream file expected after -b");
98
                    }
99
                }
100
                else if ( args[i].equals("-f") ) {
101
                    force = true;
102
                }
103
                else if ( args[i].equals("-p") ) {
104
                    bus.printBus(System.out);
105
                    System.exit(0);
106
                }
107
                else if ( args[i].equals("-p") ) {
108
                    bus.printBus(System.out);
109
                    System.exit(0);
110
                }
111
                else if ( args[i].equals("-h") ) {
112
                    System.err.println(ParameterException.helpMsg);
113
                    System.exit(0);
114
                }
115
                else throw new ParameterException("Invalid Parameter: "+args[i]);
116
            }
117
 
118
 
119
// create the main class            
120
            if ( bus.numberOfDevices() <= 0) {
121
                System.err.println("No devices found");
122
                System.exit(0);
123
            }
124
            FX3SDemo ztex = new FX3SDemo ( bus.device(devNum) );
125
            bus.unref();
126
 
127
// upload the firmware if necessary
128
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("Demo for ZTEX FX3S Boards")  ) {
129
                if ( ztex.dev().valid() )
130
                System.out.println("Trying to overwrite firmware in RAM. This only works if a board specific firmware with reset support is running.\n"+
131
                                   "If uploading firmware fails please restart board with firmware disabled." );
132
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "fx3sdemo.img", force ) + " ms");
133
            }
134
 
135
// print log
136
            ztex.debug2PrintNextLogMessages(System.out);
137
 
138
// test flash2
139
            System.out.println("SD Flash Test:");
140
            if ( ztex.flash2Enabled() ) {
141
                System.out.println("Sector size: "+ztex.flash2SectorSize());
142
                System.out.println("Sector size: "+ztex.flash2Sectors());
143
                System.out.println(ztex.flash2Info());
144
                final int nsec = 65536 / ztex.flash2SectorSize();
145
                final int size = nsec * ztex.flash2SectorSize();
146
//              final int startSector = 131070;
147
                final int startSector = 711;
148
                byte[] buf0 = new byte[size];
149
                byte[] buf1 = new byte[size];
150
                byte[] buf2 = new byte[size];
151
                for (int i=0; i<size; i++ ) {
152
                    buf1[i] = (byte)((i % 251) & 255);
153
                    buf2[i] = (byte) 255;
154
                }
155
                try {
156
                    System.out.println("Reading backup of sectors " + startSector + ".." + (startSector+nsec-1));
157
                    ztex.flash2ReadSector(startSector, nsec, buf0);
158
                    System.out.println("Writing sectors " + startSector + ".." + (startSector+nsec-1));
159
                    ztex.flash2WriteSector(startSector, nsec, buf1);
160
                    System.out.println("Reading sectors " + startSector + ".." + (startSector+nsec-1));
161
                    ztex.flash2ReadSector(startSector, nsec, buf2);
162
                    int errors = 0;
163
                    for (int i=0; i<size; i++ ) {
164
                        if ( buf1[i] != buf2[i] ) {
165
                            if ( errors<10 ) System.out.println("Flash error at " + i + ":  " + (buf1[i] & 255) + " " + (buf2[i] & 255)+"        ");
166
                            else if (errors==10) System.out.println("...");
167
                            errors++;
168
                        }
169
                    }
170
                    if ( errors == 0 ) System.out.println("No Flash errors");
171
                    System.out.println("Restoring sectors " + startSector + ".." + (startSector+nsec-1));
172
                    ztex.flash2WriteSector(startSector, nsec, buf0);
173
 
174
                    System.out.println("Hint: Try out ../flashbench example for further tests");
175
                }
176
                catch (Exception e) {
177
                    System.out.println("Flash error: " + e.getLocalizedMessage() );
178
                }
179
            } else {
180
                System.out.println("No flash present");
181
            }
182
 
183
            ztex.debug2PrintNextLogMessages(System.out);
184
 
185
// release resources
186
            ztex.dispose();
187
 
188
        }
189
        catch (Exception e) {
190
            System.out.println("Error: "+e.getLocalizedMessage() );
191
        }
192
   }
193
 
194
}

powered by: WebSVN 2.1.0

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