/*!
|
/*!
|
nvmtest -- ATxmega non volatile memory test on ZTEX USB-XMEGA Module 1.0
|
nvmtest -- ATxmega non volatile memory test on ZTEX USB-XMEGA Module 1.0
|
Copyright (C) 2009-2011 ZTEX GmbH.
|
Copyright (C) 2009-2014 ZTEX GmbH.
|
http://www.ztex.de
|
http://www.ztex.de
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License version 3 as
|
it under the terms of the GNU General Public License version 3 as
|
published by the Free Software Foundation.
|
published by the Free Software Foundation.
|
|
|
This program is distributed in the hope that it will be useful, but
|
This program is distributed in the hope that it will be useful, but
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
General Public License for more details.
|
General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
!*/
|
!*/
|
|
|
import java.io.*;
|
import java.io.*;
|
import java.util.*;
|
import java.util.*;
|
|
|
import ch.ntb.usb.*;
|
import ch.ntb.usb.*;
|
|
|
import ztex.*;
|
import ztex.*;
|
|
|
// *****************************************************************************
|
// *****************************************************************************
|
// ******* ParameterException **************************************************
|
// ******* ParameterException **************************************************
|
// *****************************************************************************
|
// *****************************************************************************
|
// Exception the prints a help message
|
// Exception the prints a help message
|
class ParameterException extends Exception {
|
class ParameterException extends Exception {
|
public final static String helpMsg = new String (
|
public final static String helpMsg = new String (
|
"Parameters:\n"+
|
"Parameters:\n"+
|
" -d <number> Device Number (default: 0)\n" +
|
" -d <number> Device Number (default: 0)\n" +
|
" -f Force uploads\n" +
|
" -f Force uploads\n" +
|
" -p Print bus info\n" +
|
" -p Print bus info\n" +
|
" -w Enable certain workarounds\n"+
|
" -w Enable certain workarounds\n"+
|
" -h This help" );
|
" -h This help" );
|
|
|
public ParameterException (String msg) {
|
public ParameterException (String msg) {
|
super( msg + "\n" + helpMsg );
|
super( msg + "\n" + helpMsg );
|
}
|
}
|
}
|
}
|
|
|
// *****************************************************************************
|
// *****************************************************************************
|
// ******* NVMTest *************************************************************
|
// ******* NVMTest *************************************************************
|
// *****************************************************************************
|
// *****************************************************************************
|
class NVMTest extends Ztex1v1 {
|
class NVMTest extends Ztex1v1 {
|
|
|
// ******* NVMTest *************************************************************
|
// ******* NVMTest *************************************************************
|
// constructor
|
// constructor
|
public NVMTest ( ZtexDevice1 pDev ) throws UsbException {
|
public NVMTest ( ZtexDevice1 pDev ) throws UsbException {
|
super ( pDev );
|
super ( pDev );
|
}
|
}
|
|
|
// ******* main ****************************************************************
|
// ******* main ****************************************************************
|
public static void main (String args[]) {
|
public static void main (String args[]) {
|
|
|
int devNum = 0;
|
int devNum = 0;
|
boolean force = false;
|
boolean force = false;
|
boolean workarounds = false;
|
boolean workarounds = false;
|
|
|
try {
|
try {
|
// init USB stuff
|
// init USB stuff
|
LibusbJava.usb_init();
|
LibusbJava.usb_init();
|
|
|
// scan the USB bus
|
// scan the USB bus
|
ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
|
ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
|
if ( bus.numberOfDevices() <= 0) {
|
if ( bus.numberOfDevices() <= 0) {
|
System.err.println("No devices found");
|
System.err.println("No devices found");
|
System.exit(0);
|
System.exit(0);
|
}
|
}
|
|
|
// scan the command line arguments
|
// scan the command line arguments
|
for (int i=0; i<args.length; i++ ) {
|
for (int i=0; i<args.length; i++ ) {
|
if ( args[i].equals("-d") ) {
|
if ( args[i].equals("-d") ) {
|
i++;
|
i++;
|
try {
|
try {
|
if (i>=args.length) throw new Exception();
|
if (i>=args.length) throw new Exception();
|
devNum = Integer.parseInt( args[i] );
|
devNum = Integer.parseInt( args[i] );
|
}
|
}
|
catch (Exception e) {
|
catch (Exception e) {
|
throw new ParameterException("Device number expected after -d");
|
throw new ParameterException("Device number expected after -d");
|
}
|
}
|
}
|
}
|
else if ( args[i].equals("-f") ) {
|
else if ( args[i].equals("-f") ) {
|
force = true;
|
force = true;
|
}
|
}
|
else if ( args[i].equals("-p") ) {
|
else if ( args[i].equals("-p") ) {
|
bus.printBus(System.out);
|
bus.printBus(System.out);
|
System.exit(0);
|
System.exit(0);
|
}
|
}
|
else if ( args[i].equals("-p") ) {
|
else if ( args[i].equals("-p") ) {
|
bus.printBus(System.out);
|
bus.printBus(System.out);
|
System.exit(0);
|
System.exit(0);
|
}
|
}
|
else if ( args[i].equals("-w") ) {
|
else if ( args[i].equals("-w") ) {
|
workarounds = true;
|
workarounds = true;
|
}
|
}
|
else if ( args[i].equals("-h") ) {
|
else if ( args[i].equals("-h") ) {
|
System.err.println(ParameterException.helpMsg);
|
System.err.println(ParameterException.helpMsg);
|
System.exit(0);
|
System.exit(0);
|
}
|
}
|
else throw new ParameterException("Invalid Parameter: "+args[i]);
|
else throw new ParameterException("Invalid Parameter: "+args[i]);
|
}
|
}
|
|
|
|
|
// create the main class
|
// create the main class
|
NVMTest ztex = new NVMTest ( bus.device(devNum) );
|
NVMTest ztex = new NVMTest ( bus.device(devNum) );
|
ztex.certainWorkarounds = workarounds;
|
ztex.certainWorkarounds = workarounds;
|
|
|
// upload the firmware if necessary
|
// upload the firmware if necessary
|
if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("nvmtest for USB-XMEGA modules") ) {
|
if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("nvmtest for USB-XMEGA modules") ) {
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( "nvmtest.ihx", force ) + " ms");
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( "nvmtest.ihx", force ) + " ms");
|
}
|
}
|
|
|
// print out some memory information
|
// print out some memory information
|
System.out.println("Flash size: " + ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize() + " EEPROM size: " + ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize() + " Error code: " + ztex.xmegaEC );
|
System.out.println("Flash size: " + ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize() + " EEPROM size: " + ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize() + " Error code: " + ztex.xmegaEC );
|
|
|
// read out device ID from production signature row
|
// read out device ID from production signature row
|
byte buf[] = new byte[ztex.xmegaFlashPageSize()];
|
byte buf[] = new byte[ztex.xmegaFlashPageSize()];
|
|
|
ztex.xmegaNvmRead ( 0x01000090, buf, 4 );
|
ztex.xmegaNvmRead ( 0x01000090, buf, 4 );
|
System.out.println( "Device ID: " + Integer.toHexString(buf[0] & 255) + " " + Integer.toHexString(buf[1] & 255) + " " + Integer.toHexString(buf[2] & 255));
|
System.out.println( "Device ID: " + Integer.toHexString(buf[0] & 255) + " " + Integer.toHexString(buf[1] & 255) + " " + Integer.toHexString(buf[2] & 255));
|
|
|
// test ATxmega Flash by reading / writing random data
|
// test ATxmega Flash by reading / writing random data
|
// generate + write date
|
// generate + write date
|
Random random = new Random();
|
Random random = new Random();
|
for (int i=0; i<ztex.xmegaFlashPageSize(); i++ )
|
for (int i=0; i<ztex.xmegaFlashPageSize(); i++ )
|
buf[i] = (byte) random.nextInt();
|
buf[i] = (byte) random.nextInt();
|
ztex.xmegaFlashPageWrite ( ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize()-1024, buf );
|
ztex.xmegaFlashPageWrite ( ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize()-1024, buf );
|
|
|
// read + verify data
|
// read + verify data
|
byte buf2[] = new byte[ztex.xmegaFlashPageSize()];
|
byte buf2[] = new byte[ztex.xmegaFlashPageSize()];
|
ztex.xmegaFlashRead ( ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize()-1024, buf2, ztex.xmegaFlashPageSize());
|
ztex.xmegaFlashRead ( ztex.xmegaFlashPages()*ztex.xmegaFlashPageSize()-1024, buf2, ztex.xmegaFlashPageSize());
|
int j = 0;
|
int j = 0;
|
for (int i=0; i<ztex.xmegaFlashPageSize(); i++ ) {
|
for (int i=0; i<ztex.xmegaFlashPageSize(); i++ ) {
|
if ( buf[i] != buf2[i] ) {
|
if ( buf[i] != buf2[i] ) {
|
if ( j<10 )
|
if ( j<10 )
|
System.out.println("Error at " + i +": " + (buf[i] & 255) + " != " + (buf2[i] & 255));
|
System.out.println("Error at " + i +": " + (buf[i] & 255) + " != " + (buf2[i] & 255));
|
j+=1;
|
j+=1;
|
}
|
}
|
}
|
}
|
System.out.println(j + " Flash Errors");
|
System.out.println(j + " Flash Errors");
|
|
|
// test ATxmega EEPROM by reading / writing random data
|
// test ATxmega EEPROM by reading / writing random data
|
// generate + write date
|
// generate + write date
|
random = new Random();
|
random = new Random();
|
for (int i=0; i<ztex.xmegaEepromPageSize(); i++ )
|
for (int i=0; i<ztex.xmegaEepromPageSize(); i++ )
|
buf[i] = (byte) random.nextInt();
|
buf[i] = (byte) random.nextInt();
|
ztex.xmegaEepromPageWrite ( ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize()-64, buf );
|
ztex.xmegaEepromPageWrite ( ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize()-64, buf );
|
|
|
// read + verify data
|
// read + verify data
|
ztex.xmegaEepromRead ( ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize()-64, buf2, ztex.xmegaEepromPageSize());
|
ztex.xmegaEepromRead ( ztex.xmegaEepromPages()*ztex.xmegaEepromPageSize()-64, buf2, ztex.xmegaEepromPageSize());
|
j = 0;
|
j = 0;
|
for (int i=0; i<ztex.xmegaEepromPageSize(); i++ ) {
|
for (int i=0; i<ztex.xmegaEepromPageSize(); i++ ) {
|
if ( buf[i] != buf2[i] ) {
|
if ( buf[i] != buf2[i] ) {
|
if ( j<10 )
|
if ( j<10 )
|
System.out.println("Error at " + i +": " + (buf[i] & 255) + " != " + (buf2[i] & 255));
|
System.out.println("Error at " + i +": " + (buf[i] & 255) + " != " + (buf2[i] & 255));
|
j+=1;
|
j+=1;
|
}
|
}
|
}
|
}
|
System.out.println(j + " EEPROM Errors");
|
System.out.println(j + " EEPROM Errors");
|
|
|
// test fuse by reading / writing JTAGUID
|
// test fuse by reading / writing JTAGUID
|
// read old JTAGUID
|
// read old JTAGUID
|
int i = ztex.xmegaFuseRead ( 0 );
|
int i = ztex.xmegaFuseRead ( 0 );
|
System.out.print( "JTAGUID: 0x" + Integer.toHexString(i & 255) );
|
System.out.print( "JTAGUID: 0x" + Integer.toHexString(i & 255) );
|
// write + read new JTAGUID
|
// write + read new JTAGUID
|
ztex.xmegaFuseWrite ( 0, 0x56 );
|
ztex.xmegaFuseWrite ( 0, 0x56 );
|
System.out.print( " -> 0x" + Integer.toHexString(ztex.xmegaFuseRead ( 0 ) ) );
|
System.out.print( " -> 0x" + Integer.toHexString(ztex.xmegaFuseRead ( 0 ) ) );
|
// write + read old JTAGUID
|
// write + read old JTAGUID
|
ztex.xmegaFuseWrite ( 0, i );
|
ztex.xmegaFuseWrite ( 0, i );
|
System.out.println( " -> 0x" + Integer.toHexString(ztex.xmegaFuseRead ( 0 ) ) );
|
System.out.println( " -> 0x" + Integer.toHexString(ztex.xmegaFuseRead ( 0 ) ) );
|
|
|
}
|
}
|
catch (Exception e) {
|
catch (Exception e) {
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
}
|
}
|
}
|
}
|
|
|
}
|
}
|
|
|