   }
   // leave flash accessible by host machine
   flash_unlock();
+  Serial.println("OK");
 }
 
 uint32_t chip_size() {

        chip_end = chip_size();
        Serial.print("Program whole chip.  Size = ");
        Serial.println(chip_end * 2);  // Because we actually have double this
        program_range(0, chip_end);
        flash_unlock();
        reset();
        break;

  return (uint8_t)Serial.read();
}

// // Read a big-endian uint32 from the USB serial port
// uint32_t serial_get_uint32() {
//   uint32_t v = (uint32_t)serial_get_uint8() << 24L;
//   v |= (uint32_t)serial_get_uint8() << 16L;
//   v |= (uint32_t)serial_get_uint8() << 8L;
//   v |= (uint32_t)serial_get_uint8();
//   return v;
// }

// // Write a big-endian uint32 to the USB serial port
// void serial_put_uint32(uint32_t v) {
//   Serial.write((uint8_t)((v & 0xFF000000) >> 24));
//   Serial.write((uint8_t)((v & 0xFF0000) >> 16));
//   Serial.write((uint8_t)((v & 0xFF00) >> 8));
//   Serial.write((uint8_t)(v & 0xFF));
// }

// Read a little-endian uint32 from the USB serial port
uint32_t serial_get_uint32() {
  uint32_t v = (uint32_t)serial_get_uint8();
  v |= (uint32_t)serial_get_uint8() << 8L;
  v |= (uint32_t)serial_get_uint8() << 16L;
  v |= (uint32_t)serial_get_uint8() << 24L;
  return v;
}

// Write a big-endian uint32 to the USB serial port
void serial_put_uint32(uint32_t v) {
  Serial.write((uint8_t)(v & 0xFF));
  Serial.write((uint8_t)((v & 0xFF00) >> 8));
  Serial.write((uint8_t)((v & 0xFF0000) >> 16));
  Serial.write((uint8_t)((v & 0xFF000000) >> 24));
}

// USB chunks: read 16kB at a time
#define CHUNK_SIZE (16384 / 4)
#define BUF_SIZE (CHUNK_SIZE + 1)
// Program 512 bytes (128 words) at a time
#define PROGRAM_BLOCK_SIZE 128

// TODO move all these vars into the programming code -- we have more RAM in this chip than the atmega32u4 this was originally written for
static uint32_t read_buf[BUF_SIZE];
