1 |
2 |
ZTEX |
/*!
|
2 |
|
|
nvmtest -- ATxmega non volatile memory test on ZTEX USB-XMEGA Module 1.0
|
3 |
3 |
ZTEX |
Copyright (C) 2009-2014 ZTEX GmbH.
|
4 |
2 |
ZTEX |
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 |
|
|
" -w Enable certain workarounds\n"+
|
37 |
|
|
" -h This help" );
|
38 |
|
|
|
39 |
|
|
public ParameterException (String msg) {
|
40 |
|
|
super( msg + "\n" + helpMsg );
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
// *****************************************************************************
|
45 |
|
|
// ******* NVMTest *************************************************************
|
46 |
|
|
// *****************************************************************************
|
47 |
|
|
class NVMTest extends Ztex1v1 {
|
48 |
|
|
|
49 |
|
|
// ******* NVMTest *************************************************************
|
50 |
|
|
// constructor
|
51 |
|
|
public NVMTest ( 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 |
|
|
boolean workarounds = false;
|
61 |
|
|
|
62 |
|
|
try {
|
63 |
|
|
// init USB stuff
|
64 |
|
|
LibusbJava.usb_init();
|
65 |
|
|
|
66 |
|
|
// scan the USB bus
|
67 |
|
|
ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
|
68 |
|
|
if ( bus.numberOfDevices() <= 0) {
|
69 |
|
|
System.err.println("No devices found");
|
70 |
|
|
System.exit(0);
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
// scan the command line arguments
|
74 |
|
|
for (int i=0; i<args.length; i++ ) {
|
75 |
|
|
if ( args[i].equals("-d") ) {
|
76 |
|
|
i++;
|
77 |
|
|
try {
|
78 |
|
|
if (i>=args.length) throw new Exception();
|
79 |
|
|
devNum = Integer.parseInt( args[i] );
|
80 |
|
|
}
|
81 |
|
|
catch (Exception e) {
|
82 |
|
|
throw new ParameterException("Device number expected after -d");
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
else if ( args[i].equals("-f") ) {
|
86 |
|
|
force = true;
|
87 |
|
|
}
|
88 |
|
|
else if ( args[i].equals("-p") ) {
|
89 |
|
|
bus.printBus(System.out);
|
90 |
|
|
System.exit(0);
|
91 |
|
|
}
|
92 |
|
|
else if ( args[i].equals("-p") ) {
|
93 |
|
|
bus.printBus(System.out);
|
94 |
|
|
System.exit(0);
|
95 |
|
|
}
|
96 |
|
|
else if ( args[i].equals("-w") ) {
|
97 |
|
|
workarounds = true;
|
98 |
|
|
}
|
99 |
|
|
else if ( args[i].equals("-h") ) {
|
100 |
|
|
System.err.println(ParameterException.helpMsg);
|
101 |
|
|
System.exit(0);
|
102 |
|
|
}
|
103 |
|
|
else throw new ParameterException("Invalid Parameter: "+args[i]);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
// create the main class
|
108 |
|
|
NVMTest ztex = new NVMTest ( bus.device(devNum) );
|
109 |
|
|
ztex.certainWorkarounds = workarounds;
|
110 |
|
|
|
111 |
|
|
// upload the firmware if necessary
|
112 |
|
|
if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("nvmtest for USB-XMEGA modules") ) {
|
113 |
|
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( "nvmtest.ihx", force ) + " ms");
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
// print out some memory information
|
117 |
|
|
System.out.println("Flash size: " + ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize() + " EEPROM size: " + ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize() + " Error code: " + ztex.xmegaEC );
|
118 |
|
|
|
119 |
|
|
// read out device ID from production signature row
|
120 |
|
|
byte buf[] = new byte[ztex.xmegaFlashPageSize()];
|
121 |
|
|
|
122 |
|
|
ztex.xmegaNvmRead ( 0x01000090, buf, 4 );
|
123 |
|
|
System.out.println( "Device ID: " + Integer.toHexString(buf[0] & 255) + " " + Integer.toHexString(buf[1] & 255) + " " + Integer.toHexString(buf[2] & 255));
|
124 |
|
|
|
125 |
|
|
// test ATxmega Flash by reading / writing random data
|
126 |
|
|
// generate + write date
|
127 |
|
|
Random random = new Random();
|
128 |
|
|
for (int i=0; i<ztex.xmegaFlashPageSize(); i++ )
|
129 |
|
|
buf[i] = (byte) random.nextInt();
|
130 |
|
|
ztex.xmegaFlashPageWrite ( ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize()-1024, buf );
|
131 |
|
|
|
132 |
|
|
// read + verify data
|
133 |
|
|
byte buf2[] = new byte[ztex.xmegaFlashPageSize()];
|
134 |
|
|
ztex.xmegaFlashRead ( ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize()-1024, buf2, ztex.xmegaFlashPageSize());
|
135 |
|
|
int j = 0;
|
136 |
|
|
for (int i=0; i<ztex.xmegaFlashPageSize(); i++ ) {
|
137 |
|
|
if ( buf[i] != buf2[i] ) {
|
138 |
|
|
if ( j<10 )
|
139 |
|
|
System.out.println("Error at " + i +": " + (buf[i] & 255) + " != " + (buf2[i] & 255));
|
140 |
|
|
j+=1;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
System.out.println(j + " Flash Errors");
|
144 |
|
|
|
145 |
|
|
// test ATxmega EEPROM by reading / writing random data
|
146 |
|
|
// generate + write date
|
147 |
|
|
random = new Random();
|
148 |
|
|
for (int i=0; i<ztex.xmegaEepromPageSize(); i++ )
|
149 |
|
|
buf[i] = (byte) random.nextInt();
|
150 |
|
|
ztex.xmegaEepromPageWrite ( ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize()-64, buf );
|
151 |
|
|
|
152 |
|
|
// read + verify data
|
153 |
|
|
ztex.xmegaEepromRead ( ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize()-64, buf2, ztex.xmegaEepromPageSize());
|
154 |
|
|
j = 0;
|
155 |
|
|
for (int i=0; i<ztex.xmegaEepromPageSize(); i++ ) {
|
156 |
|
|
if ( buf[i] != buf2[i] ) {
|
157 |
|
|
if ( j<10 )
|
158 |
|
|
System.out.println("Error at " + i +": " + (buf[i] & 255) + " != " + (buf2[i] & 255));
|
159 |
|
|
j+=1;
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
System.out.println(j + " EEPROM Errors");
|
163 |
|
|
|
164 |
|
|
// test fuse by reading / writing JTAGUID
|
165 |
|
|
// read old JTAGUID
|
166 |
|
|
int i = ztex.xmegaFuseRead ( 0 );
|
167 |
|
|
System.out.print( "JTAGUID: 0x" + Integer.toHexString(i & 255) );
|
168 |
|
|
// write + read new JTAGUID
|
169 |
|
|
ztex.xmegaFuseWrite ( 0, 0x56 );
|
170 |
|
|
System.out.print( " -> 0x" + Integer.toHexString(ztex.xmegaFuseRead ( 0 ) ) );
|
171 |
|
|
// write + read old JTAGUID
|
172 |
|
|
ztex.xmegaFuseWrite ( 0, i );
|
173 |
|
|
System.out.println( " -> 0x" + Integer.toHexString(ztex.xmegaFuseRead ( 0 ) ) );
|
174 |
|
|
|
175 |
|
|
}
|
176 |
|
|
catch (Exception e) {
|
177 |
|
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
}
|