 	digitalWrite(TCK_PIN, LOW);
 
   // Set up pullups for flash pins
-  pinMode(FLASH_NRESET_PIN, OUTPUT);
-  digitalWrite(FLASH_NRESET_PIN, LOW);
-  digitalWrite(FLASH_NRESET_PIN, HIGH);
-  pinMode(FLASH_NRESET_PIN, INPUT_PULLUP);
+  flash_reset();
   pinMode(FLASH_NREADY_PIN, INPUT_PULLUP);
 
-  // Set up SPI comms on SERCOM2 with CPLD: MOSI=PA08, SCK=PA09, SS=PA10, MISO=PA11
-  // pinMode(CPLD_MOSI_PIN, OUTPUT);
-  // pinMode(CPLD_SCK_PIN, OUTPUT);
-  // pinMode(CPLD_MISO_PIN, INPUT);
+  // Set up fast SPI comms on SERCOM2 with CPLD
   pinMode(CPLD_SS_PIN, OUTPUT);
   digitalWrite(CPLD_SS_PIN, HIGH);
   cpld_spi.begin();

      }
      case 'I': {
        Serial.println("IDENTIFY FLASH");
        flash_write(0x55, 0x00980098L);
        Serial.println(" <-- write");
        for (uint32_t cfi_addr = 0x10; cfi_addr <= 0x50; ++cfi_addr) {
          Serial.println(flash_read(cfi_addr), HEX);
        }
        flash_unlock();
        break;
      }
      case 'Z': {


// PA28 - cpld_clock_from_mcu


// Uncomment this to show every byte sent and received over SPI
// #define SHOW_ALL_SPI_TRANSFERS

// Disable USB serial commands (CPLD programming etc) by commenting this out:
#define ENABLE_USB_SERIAL


// We have the S29GL064S70DHI010 (S29GL064S 70-ns D-LAE064-BGA-pkg H-halogen/lead-free I-industrial 01-uniform sector 0-tray)
// 2 x 64 (4Mwordx16) megabit flash chips, so addresses are 22 bits long
#define CHIP_SIZE (4 * 1048576L)
// Sectors are 32k word / 64kB long
#define SECTOR_SIZE (32768L)
#define SECTOR_MASK ((CHIP_SIZE * 4 - 1) & ~(SECTOR_SIZE - 1))


// libxsvf (xsvftool-arduino) entry point
extern void arduino_play_svf(int tms_pin, int tdi_pin, int tdo_pin, int tck_pin, int trst_pin);

// SPI on SERCOM2 to talk to the CPLD at 24MHz
SPIClass cpld_spi(&sercom2, CPLD_MISO_PIN, CPLD_SCK_PIN, CPLD_MOSI_PIN, SPI_PAD_0_SCK_1, SERCOM_RX_PAD_3);


// Send/receive a byte over SPI, optionally dumping details to the serial port
uint8_t spi_transfer(uint8_t b) {
  uint8_t r = cpld_spi.transfer(b);
#ifdef SHOW_ALL_SPI_TRANSFERS
  Serial.print("[");
  Serial.print(b, HEX);
  Serial.print(" -> ");
  Serial.print(r, HEX);
  Serial.print("]");
#endif
  return r;
}

// Write a 32-bit word to the flash chips
void flash_write(uint32_t A, uint32_t D) {
  // Write a 32-bit word to the flash, leaving allowing_arm_access == 0
  digitalWrite(CPLD_SS_PIN, LOW);
