# sdcard2.cpp

This sketch performs block reads and writes to an SD card and measures how long it takes. For this sample output, I started with a 4GB PNY SDHC card and later switched to a 4GB SanDisk SDHC card:

      b
      Ln 23.463 Read 225 blocks at 114.97KB/sec
      Ln 25.484 Wrote 129 blocks at 65.85KB/sec
      b
      Ln 135.113 Read 250 blocks at 127.62KB/sec
      Ln 137.118 Wrote 363 blocks at 185.49KB/sec

Note that this sketch will write garbage into hundreds of blocks of your SD card, without regard for any file system that might be present! Do not run this sketch on a formatted card unless you no longer care about its contents!

    // SD Card 2: Benchmark SD card raw block reads and writes
    #include "SFBSDRaw.h"           // Load the SDRaw library

    SDRaw mySD;                     // Allocate space for a raw sdcard controller
    u8 buffer[512];                 // And a disk-block-sized buffer

    bool wantBench = false;
    void startBench(u8*) { wantBench = true; }  // Just set a flag and let loop() do it

    void setup() {
      mySD.begin(NORTH);            // Set up for 'IXM SD Cell' connectivity on North
      Body.reflex('b',startBench);  // Create reflex
    }

    void loop() {
      if (!wantBench) { ledToggle(BODY_RGB_BLUE_PIN);  delay(1000); return; }
      wantBench = false;
      if (!mySD.init()) { logNormal("Can't init\n"); return; }

      u32 startBlock, blockNumber, start, totalBlocks;
      startBlock = 0;               // Just read from disk start
      blockNumber = startBlock;
      for (start = millis(); IS_EARLIER(millis(),start+1000); ++blockNumber) {
        if (!mySD.readBlock(blockNumber,buffer)) {
          logNormal("Read failed, block %d\n",blockNumber);
          return;
        }
      }
      totalBlocks = blockNumber-startBlock;
      // 512*totalBlocks == totalBytes; totalBytes/totalMs === KBytes/second
      logNormal("Read %d blocks at %fKB/sec\n",
                totalBlocks, 512.0*totalBlocks/(millis()-start));

      delay(1000);                  // The pause that refreeshes

      startBlock = blockNumber;     // Start writing where we stopped reading
      for (start = millis(); IS_EARLIER(millis(),start+1000); ++blockNumber) {
        if (!mySD.writeBlock(blockNumber,buffer)) { // Always writing the same data!
          logNormal("Write failed, block %d\n",blockNumber);
          return;
        }
      }
      totalBlocks = blockNumber-startBlock;
      logNormal("Wrote %d blocks at %fKB/sec\n",
                totalBlocks, 512.0*totalBlocks/(millis()-start));
    }