Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
/* USB Host Shield Board test routine. Runs after assembly to check board functionality */
2
 
3
/* USB related */
4
//#include <Spi.h>
5
#include <Max3421e.h>
6
#include <Max3421e_constants.h>
7
#include <Usb.h>
8
 
9
#include "board_test.h" /* Board test messages */
10
 
11
//#define MAX_SS 10
12
 
13
void setup();
14
void loop();
15
 
16
MAX3421E Max;
17
USB Usb;
18
 
19
void setup()
20
{
21
  Serial.begin( 115200 );
22
  //Serial.println("Start");
23
  //Serial.println( SCK_PIN, DEC );
24
  Max.powerOn();
25
  printProgStr( startBanner );
26
  printProgStr( anykey_msg );
27
  //Serial.print( Max.getvar(), DEC);
28
}
29
 
30
void loop()
31
{
32
  while( Serial.available() == 0 );  //wait for input
33
  Serial.read();                     //empty input buffer
34
  /* start tests */
35
  /* SPI short test */
36
  if (!revregcheck()) test_halted();
37
  /* GPIO test */
38
  if (!gpiocheck()) printProgStr(PSTR("\r\nGPIO check failed. Make sure GPIO loopback adapter is installed"));
39
  /* SPI long test */
40
  if (!spitest()) test_halted();      //test SPI for transmission errors
41
  if (!osctest()) printProgStr(PSTR("OSCOK test failed. Check the oscillator"));
42
  if (!usbtest()) printProgStr(PSTR("USB connection test failed. Check traces from USB connector to MAX3421E, as well as VBUS"));  //never gets here
43
    /* All tests passed */
44
  printProgStr( anykey_msg );
45
}
46
 
47
/* SPI short test. Checks connectivity to MAX3421E by reading REVISION register. */
48
/* Die rev.1 returns 0x01, rev.2 0x12, rev.3 0x13. Any other value is considered communication error */
49
bool revregcheck()
50
{
51
  byte tmpbyte;
52
  printProgStr(PSTR("\r\nReading REVISION register...Die revision "));
53
  tmpbyte = Max.regRd( rREVISION );
54
  switch( tmpbyte ) {
55
    case( 0x01 ):  //rev.01
56
      printProgStr(PSTR("01"));
57
      break;
58
    case( 0x12 ):  //rev.02
59
      printProgStr(PSTR("02"));
60
      break;
61
    case( 0x13 ):  //rev.03
62
      printProgStr(PSTR("03"));
63
      break;
64
    default:
65
      printProgStr(PSTR("invalid. Value returned: "));
66
      print_hex( tmpbyte, 8 );
67
      printProgStr( testfailed_msg );
68
      return( false );
69
      break;
70
  }//switch( tmpbyte )...
71
  printProgStr( testpassed_msg );
72
  return( true );
73
}
74
/* SPI long test */
75
bool spitest()
76
{
77
  byte l = 0;
78
  byte k = 0;
79
  byte gpinpol_copy = Max.regRd( rGPINPOL );
80
  printProgStr(PSTR("\r\nSPI test. Each  '.' indicates 64K transferred. Stops after transferring 1MB (16 dots)\r\n"));
81
  /**/
82
  for( byte j = 0; j < 16; j++ ) {
83
    for( word i = 0; i < 65535; i++ ) {
84
      Max.regWr( rGPINPOL, k );
85
      l = Max.regRd( rGPINPOL);
86
      if( l != k ) {
87
        printProgStr( spitest_fail_msg );
88
        print_hex( k, 8);
89
        printProgStr(PSTR("Value read: "));
90
        print_hex( l, 8 );
91
        return( false );                  //test failed
92
      }
93
      k++;
94
    }//for( i = 0; i < 65535; i++
95
    Serial.print(".");
96
  }//for j = 0; j < 16...
97
  Max.regWr( rGPINPOL, gpinpol_copy );
98
  printProgStr(testpassed_msg);
99
  return( true );
100
}
101
/* Oscillator test */
102
bool osctest()
103
{
104
  printProgStr(PSTR("\r\nOscillator start/stop test."));
105
  printProgStr( osctest_oscstate_msg );
106
  check_OSCOKIRQ();                          //print OSCOK state
107
  printProgStr(PSTR("\r\nSetting CHIP RESET."));
108
  Max.regWr( rUSBCTL, bmCHIPRES );              //Chip reset. This stops the oscillator
109
  printProgStr( osctest_oscstate_msg );
110
  check_OSCOKIRQ();  //print OSCOK state
111
  printProgStr(PSTR("\r\nClearing CHIP RESET. "));
112
  Max.regWr( rUSBCTL, 0x00 );                //Chip reset release
113
  for( word i = 0; i < 65535; i++) {
114
    if( Max.regRd( rUSBIRQ ) & bmOSCOKIRQ ) {
115
      printProgStr(PSTR("PLL is stable. Time to stabilize - "));
116
      Serial.print( i, DEC );
117
      printProgStr(PSTR(" cycles"));
118
      printProgStr( testpassed_msg );
119
      return( true );
120
    }
121
  }//for i =
122
  return(false);
123
}
124
/* Stop/start oscillator */
125
void check_OSCOKIRQ()
126
{
127
  if( Max.regRd( rUSBIRQ ) & bmOSCOKIRQ ) {  //checking oscillator state
128
    printProgStr(PSTR("ON"));
129
  }
130
  else {
131
    printProgStr(PSTR("OFF"));
132
  }
133
}
134
/* Test USB connectivity */
135
bool usbtest()
136
{
137
  byte rcode;
138
  byte usbstate;
139
    Max.powerOn();
140
    delay( 200 );
141
    printProgStr(PSTR("\r\nUSB Connectivity test. Waiting for device connection... "));
142
    while( 1 ) {
143
      delay( 200 );
144
      Max.Task();
145
      Usb.Task();
146
      usbstate = Usb.getUsbTaskState();
147
      switch( usbstate ) {
148
        case( USB_ATTACHED_SUBSTATE_RESET_DEVICE ):
149
          printProgStr(PSTR("\r\nDevice connected. Resetting"));
150
          break;
151
        case( USB_ATTACHED_SUBSTATE_WAIT_SOF ):
152
          printProgStr(PSTR("\r\nReset complete. Waiting for the first SOF..."));
153
          //delay( 1000 );
154
          break;
155
        case( USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE ):
156
          printProgStr(PSTR("\r\nSOF generation started. Enumerating device."));
157
          break;
158
        case( USB_STATE_ADDRESSING ):
159
          printProgStr(PSTR("\r\nSetting device address"));
160
          //delay( 100 );
161
          break;
162
        case( USB_STATE_CONFIGURING ):
163
          //delay( 1000 );
164
          printProgStr(PSTR("\r\nGetting device descriptor"));
165
          rcode = getdevdescr( 1 );
166
            if( rcode ) {
167
              printProgStr(PSTR("\r\nError reading device descriptor. Error code "));
168
              print_hex( rcode, 8 );
169
            }
170
            else {
171
              printProgStr(PSTR("\r\n\nAll tests passed. Press RESET to restart test"));
172
              while(1);
173
            }
174
          break;
175
        case( USB_STATE_ERROR ):
176
          printProgStr(PSTR("\r\nUSB state machine reached error state"));
177
          break;
178
        default:
179
          break;
180
    }//switch
181
  }//while(1)
182
}
183
/* Get device descriptor */
184
byte getdevdescr( byte addr )
185
{
186
  USB_DEVICE_DESCRIPTOR buf;
187
  byte rcode;
188
  rcode = Usb.getDevDescr( addr, 0, 0x12, ( char *)&buf );
189
  if( rcode ) {
190
    return( rcode );
191
  }
192
  printProgStr(PSTR("\r\nDevice descriptor: "));
193
  printProgStr(PSTR("\r\nDescriptor Length:\t"));
194
  print_hex( buf.bLength, 8 );
195
  printProgStr(PSTR("\r\nDescriptor type:\t"));
196
  print_hex( buf.bDescriptorType, 8 );
197
  printProgStr(PSTR("\r\nUSB version:\t"));
198
  print_hex( buf.bcdUSB, 16 );
199
  printProgStr(PSTR("\r\nDevice class:\t"));
200
  print_hex( buf.bDeviceClass, 8 );
201
  printProgStr(PSTR("\r\nDevice Subclass:\t"));
202
  print_hex( buf.bDeviceSubClass, 8 );
203
  printProgStr(PSTR("\r\nDevice Protocol:\t"));
204
  print_hex( buf.bDeviceProtocol, 8 );
205
  printProgStr(PSTR("\r\nMax.packet size:\t"));
206
  print_hex( buf.bMaxPacketSize0, 8 );
207
  printProgStr(PSTR("\r\nVendor ID:\t"));
208
  print_hex( buf.idVendor, 16 );
209
  printProgStr(PSTR("\r\nProduct ID:\t"));
210
  print_hex( buf.idProduct, 16 );
211
  printProgStr(PSTR("\r\nRevision ID:\t"));
212
  print_hex( buf.bcdDevice, 16 );
213
  printProgStr(PSTR("\r\nMfg.string index:\t"));
214
  print_hex( buf.iManufacturer, 8 );
215
  printProgStr(PSTR("\r\nProd.string index:\t"));
216
  print_hex( buf.iProduct, 8 );
217
  printProgStr(PSTR("\r\nSerial number index:\t"));
218
  print_hex( buf.iSerialNumber, 8 );
219
  printProgStr(PSTR("\r\nNumber of conf.:\t"));
220
  print_hex( buf.bNumConfigurations, 8 );
221
  return( 0 );
222
}
223
 
224
/* GPIO lines check. A loopback adapter connecting GPIN to GPOUT is assumed */
225
bool gpiocheck()
226
{
227
 byte tmpbyte = 0;
228
  printProgStr(PSTR("\r\nChecking GPIO lines. Install GPIO loopback adapter and press any key to continue..."));
229
  while( Serial.available() == 0 );  //wait for input
230
  Serial.read();                     //empty input buffer
231
    for( byte i = 0; i < 255; i++ ) {
232
      Max.gpioWr( i );
233
      tmpbyte = Max.gpioRd();
234
      if( tmpbyte != i ) {
235
        printProgStr(PSTR("GPIO read/write mismatch. Write: "));
236
        Serial.print(i, HEX);
237
        printProgStr(PSTR(" Read: "));
238
        Serial.println( tmpbyte, HEX );
239
        return( false );
240
      }//if( tmpbyte != i )
241
    }//for( i= 0...
242
    printProgStr( testpassed_msg );
243
    return( true );
244
}
245
/* Test halted state. Generates 0x55 to aid in SPI troubleshooting */
246
void test_halted()
247
{
248
  printProgStr( test_halted_msg );
249
  printProgStr(PSTR("\r\nPress RESET to restart test"));
250
  while( 1 )  {            //System Stop. Generating pattern to keep SCLK, MISO, MOSI, SS busy
251
    digitalWrite(MAX_SS,LOW);
252
    Max.regWr( 0x55, 0x55 );
253
//    Spi.transfer( 0x55 );
254
    digitalWrite(MAX_SS,HIGH);
255
  }
256
}
257
/* given a PROGMEM string, use Serial.print() to send it out */
258
/* Some non-intuitive casting necessary:                           */
259
/* printProgStr(PSTR("Func.Mode:\t0x"));                           */
260
/* printProgStr((char*)pgm_read_word(&mtpopNames[(op & 0xFF)]));   */
261
void printProgStr(const char* str )
262
{
263
  if(!str) {
264
    return;
265
  }
266
  char c;
267
  while((c = pgm_read_byte(str++))) {
268
    Serial.print(c,BYTE);
269
  }
270
}
271
/* prints hex numbers with leading zeroes */
272
// copyright, Peter H Anderson, Baltimore, MD, Nov, '07
273
// source: http://www.phanderson.com/arduino/arduino_display.html
274
void print_hex(int v, int num_places)
275
{
276
  int mask=0, n, num_nibbles, digit;
277
 
278
  for (n=1; n<=num_places; n++)
279
  {
280
    mask = (mask << 1) | 0x0001;
281
  }
282
  v = v & mask; // truncate v to specified number of places
283
 
284
  num_nibbles = num_places / 4;
285
  if ((num_places % 4) != 0)
286
  {
287
    ++num_nibbles;
288
  }
289
 
290
  do
291
  {
292
    digit = ((v >> (num_nibbles-1) * 4)) & 0x0f;
293
    Serial.print(digit, HEX);
294
  }
295
  while(--num_nibbles);
296
}