Test program for SDHCFileSystem performance test.

Dependencies:   FatFileSystem mbed

Fork of SDHCFileSystem by Klaus Bu

SDHCFileSystem performance test.

Using SDHCFileSystem/FatFileSystem via fopen()/fwrite()/fclose() . With simple code of reading/writing (fgets()/fputs()) text file.

in performance test of fwrite()

  • File size is changeable
  • Block size is changeable
  • SPI Frequency of SDHCFileSytesm is changeable

You can change SPI Frequency by changing the SDHC_SPI_FREQUENCY in SDHCFileSystem.h

// spi frequency (Hz) 100000->100kHz
#define SDHC_SPI_FREQUENCY 2000000

performance test funcion

in main.cpp

void speedtest(char *path, int totallen, int blocklen, bool unlinkfile);
path
full path of the file
totallen
total length (byte) of test data
blocklen
length (byte) of the data writing once by fwrite()
unlinkfile
true->remove the file as ending this function

Files at this revision

API Documentation at this revision

Comitter:
hayashiisme
Date:
Thu Feb 14 02:07:38 2013 +0000
Parent:
0:90601632692f
Commit message:
Commit for publishing

Changed in this revision

FATFileSystem.lib Show diff for this revision Revisions of this file
FatFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
SDHCFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
SDHCFileSystem.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 90601632692f -r f69b56cac865 FATFileSystem.lib
--- a/FATFileSystem.lib	Sat Jul 24 19:45:29 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_unsupported/code/fatfilesystem/
\ No newline at end of file
diff -r 90601632692f -r f69b56cac865 FatFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FatFileSystem.lib	Thu Feb 14 02:07:38 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/FatFileSystem/#8c55801ce311
diff -r 90601632692f -r f69b56cac865 SDHCFileSystem.cpp
--- a/SDHCFileSystem.cpp	Sat Jul 24 19:45:29 2010 +0000
+++ b/SDHCFileSystem.cpp	Thu Feb 14 02:07:38 2013 +0000
@@ -123,7 +123,7 @@
  
 #include "SDHCFileSystem.h"
 
-#define DEBUG
+//#define DEBUG
 #define SD_COMMAND_TIMEOUT 5000
 
 
@@ -227,7 +227,7 @@
         return 1;
     }
         
-    _spi.frequency(1000000); // Set to 1MHz for data transfer
+    _spi.frequency(SDHC_SPI_FREQUENCY); // Set to 1MHz for data transfer
     return 0;
 }
 
diff -r 90601632692f -r f69b56cac865 SDHCFileSystem.h
--- a/SDHCFileSystem.h	Sat Jul 24 19:45:29 2010 +0000
+++ b/SDHCFileSystem.h	Thu Feb 14 02:07:38 2013 +0000
@@ -26,6 +26,9 @@
 #include "mbed.h"
 #include "FATFileSystem.h"
 
+// spi frequency (Hz) 100000->100kHz
+#define SDHC_SPI_FREQUENCY 2000000
+
 /* Double Words */
 typedef unsigned long long uint64_t;
 typedef long long sint64_t;
diff -r 90601632692f -r f69b56cac865 main.cpp
--- a/main.cpp	Sat Jul 24 19:45:29 2010 +0000
+++ b/main.cpp	Thu Feb 14 02:07:38 2013 +0000
@@ -1,12 +1,131 @@
- #include "mbed.h"
- #include "string"
- #include "SDHCFileSystem.h"
- 
- SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
-   
- int main() {
-       
-       FILE *fp = fopen("/sd/myfile.txt", "w");
-       fprintf(fp, "\n\rHello World!\n\r");
-       fclose(fp);
- }
\ No newline at end of file
+#include "mbed.h"
+#include "string.h"
+#include "SDHCFileSystem.h"
+
+#define PSQ 35
+
+#define TEST_FPUTSFGETS 1
+#define TEST_FWRITEFREAD 1
+#define TEST_PRINTBYTEUS 0
+
+Serial pc(USBTX, USBRX);
+
+void speedtest(char *path, int totallen, int blocklen, bool unlinkfile){
+  char *data = (char *)malloc(blocklen);
+  for(int i=0; i < blocklen; i++)
+    data[i] = 0x30 + (i % 10);
+
+  int wrotelen = 0, avgus = 0, maxus = 0, blavgus = 0, blmaxus = 0;
+  int maxi = 0, blmaxi = 0;
+  Timer t;
+  t.reset();
+  t.start();
+  int beginus = t.read_us();
+  FILE *fp = fopen(path, "w");
+  int i = 0, j = 0;
+  while(wrotelen < totallen){
+    int beforeus = t.read_us();
+    int wl = fwrite((void *)data, 1, blocklen, fp);
+    int afterus = t.read_us();
+    int blockus = afterus - beforeus;
+    int byteus = blockus / wl;
+    avgus = (avgus ? (avgus + byteus) / 2 : byteus);
+    if(maxus < byteus){
+      maxus = byteus;
+      maxi = i;
+    }
+    blavgus = (blavgus ? (blavgus + blockus) / 2 : blockus);
+    if(blmaxus < blockus){
+      blmaxus = blockus;
+      blmaxi = i;
+    }
+    wrotelen += wl;
+    i++;
+    if(TEST_PRINTBYTEUS){
+        if(byteus > 2){
+            printf("%s %08d", (j++ ? "" : "\r\n"), byteus);
+        }else{
+            j = 0;
+        }
+    }
+  }
+  if(TEST_PRINTBYTEUS) printf("\r\n");
+  fclose(fp);
+  
+  int endus = t.read_us();
+  t.stop();
+
+  printf("SPIFreq=%dHz TotalBytes=%d (Block)=%dbytes TotalTime=%dus\r\n",
+     SDHC_SPI_FREQUENCY, wrotelen, blocklen, endus-beginus);
+  printf("Time(us): max=%d(%d) avg=%d max(block)=%d(%d) avg(block)=%d\r\n",
+     maxus, maxi, avgus, blmaxus, blmaxi, blavgus);
+  printf("\r\n");
+  
+  if(unlinkfile) remove(path);
+  free(data);
+}
+
+
+int main(){
+  pc.baud(921600);
+  SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
+  printf("%s [START %04d]\r\n", __FILE__, PSQ);
+  
+  char *str = "Hello, World!\n";
+  int l = strlen(str) + 1;
+  char *path;
+  FILE *fp;
+  char buf[512];
+  
+  if(TEST_FWRITEFREAD){
+    printf("[fwrite()/fread()]\r\n");
+    printf("write data of %d bytes\r\n", l);
+    memset(buf, 0x00, sizeof(buf));
+    path = "/sd/fwrite_fread.txt";
+    
+    fp = fopen(path, "w");
+    int r = fwrite((void *)str, 1, l, fp);
+    fclose(fp);
+    printf("fwrite() not wrote whole data %d/%d\r\n", r, l);
+    fp = fopen(path, "r");
+    int s = fread((void *)buf, 1, r, fp);
+    printf("fread() not read whole data %d/%d\r\n", s, r);
+    for(int i=0; i < s; i++){
+      printf("%c", buf[i]);
+    }
+    printf("\r\n");
+    fclose(fp);
+    
+    printf("speed test...write\r\n");
+
+    speedtest("/sd/fwr_5120000_0001.txt", 5120000, 1, true);
+    speedtest("/sd/fwr_5120000_0002.txt", 5120000, 2, true);
+    speedtest("/sd/fwr_5120000_0005.txt", 5120000, 5, true);
+    speedtest("/sd/fwr_5120000_0010.txt", 5120000, 10, true);
+    speedtest("/sd/fwr_5120000_0020.txt", 5120000, 20, true);
+    speedtest("/sd/fwr_5120000_0050.txt", 5120000, 50, true);
+    speedtest("/sd/fwr_5120000_0100.txt", 5120000, 100, true);
+    speedtest("/sd/fwr_5120000_0256.txt", 5120000, 256, true);
+    speedtest("/sd/fwr_5120000_0512.txt", 5120000, 512, true);
+    speedtest("/sd/fwr_5120000_1024.txt", 5120000, 1024, true);
+    speedtest("/sd/fwr_5120000_2048.txt", 5120000, 2048, true);
+    speedtest("/sd/fwr_5120000_4096.txt", 5120000, 4096, true);
+  }
+
+  if(TEST_FPUTSFGETS){
+    printf("[fputs()/fgets()]\r\n");
+    memset(buf, 0x00, sizeof(buf));
+    path = "/sd/fputs_fgets.txt";
+    
+    fp = fopen(path, "w");
+    fputs(str, fp);
+    fclose(fp);
+    fp = fopen(path, "r");
+    if(fgets(buf, sizeof(buf), fp)){
+      printf("%s\r\n", buf);
+    }
+    fclose(fp);
+  }
+  
+  printf("%s [END %04d]\r\n", __FILE__, PSQ);
+}