   ///////////////////////////////////////////////////////////////////
   spiErrorCount0 = 0;
   spiAddress0 = 0;
-  spiData0 = 0xff;
-
-  for (spiAddress0=0; spiAddress0 <= SPI_MAX_ADDR; spiAddress0++) {
+  maskPhase = 0;
+
+  for (spiAddress0=0; spiAddress0 <= SPI_MAX_ADDR; spiAddress0=spiAddress0+2) {
     if ((spiAddress0 % 32768) == 0) {
-      //Serial.print("Reading ");
-      //Serial.print(spiAddress0, HEX);
-    }
-
-    //int data = mem0Read(spiAddress0);
-    int data = spiMem0.read(spiAddress0);
+//      Serial.print("Reading ");
+//      Serial.print(spiAddress0, HEX);
+    }
+
+    spiData0 = calcData(spiAddress0, loopPhase, maskPhase);
+    int data = spiMem0.read16(spiAddress0);
     if (data != spiData0) {
       spiErrorCount0++;
       Serial.println("");
       Serial.print("ERROR MEM0: (expected) (actual):");
       Serial.print(spiData0, HEX); Serial.print(":");
-      Serial.println(data, HEX);
+      Serial.print(data, HEX);
+      
       delay(100);
     }
+    maskPhase = (maskPhase+1) % 2;
 
     if ((spiAddress0 % 32768) == 0) {
-      //Serial.print(", data = ");
-      //Serial.println(data, HEX);
-    }
-
-    spiData0 = (spiData0-1) & 0xff;
+//      Serial.print(", data = ");
+//      Serial.println(data, HEX);
+//      Serial.println(String(" loopPhase: ") + loopPhase + String(" maskPhase: ") + maskPhase);
+    }
 
     // Break out of test once the error count reaches 10
     if (spiErrorCount0 > 10) { break; }

 * This demo will provide an audio passthrough, as well as exercise the
 * MIDI interface.
 * 
 * It will also perform a sweeo of the SPI MEM0 external RAM.
 * 
 */
#include <Wire.h>
#include <Audio.h>
#include <MIDI.h>

  delay(100);
  AudioMemory(24);

  Serial.println("Sketch: Enabling codec...\n");
  codecControl.enable();
  delay(100);

  Serial.println("Enabling SPI");
  spiMem0.begin();
  
}

int calcData(int spiAddress, int loopPhase, int maskPhase)
{
  int data;

  int phase = ((loopPhase << 1) + maskPhase) & 0x3;
  switch(phase)
  {
    case 0 :
    data = spiAddress ^ mask0;
    break;
    case 1:
    data = spiAddress ^ mask1;
    break;
    case 2:
    data = ~spiAddress ^ mask0;
    break;
    case 3:
    data = ~spiAddress ^ mask1;
    
  }
  return (data & 0xffff);
}

void loop() {

  //////////////////////////////////////////////////////////////////
  // Write test data to the SPI Memory 0
  //////////////////////////////////////////////////////////////////
  maskPhase = 0;
  for (spiAddress0=0; spiAddress0 <= SPI_MAX_ADDR; spiAddress0=spiAddress0+2) {
    if ((spiAddress0 % 32768) == 0) {
      //Serial.print("Writing to ");
      //Serial.println(spiAddress0, HEX);
    }

    spiData0 = calcData(spiAddress0, loopPhase, maskPhase);
    spiMem0.write16(spiAddress0, spiData0);
    maskPhase = (maskPhase+1) % 2;
  }
  Serial.println("SPI0 writing DONE!");

