Hiroshi SAKAMOTO / Mbed 2 deprecated _test_SDHCFileSystem

Dependencies:   FatFileSystem mbed

Fork of SDHCFileSystem by Klaus Bu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "string.h"
00003 #include "SDHCFileSystem.h"
00004 
00005 #define PSQ 35
00006 
00007 #define TEST_FPUTSFGETS 1
00008 #define TEST_FWRITEFREAD 1
00009 #define TEST_PRINTBYTEUS 0
00010 
00011 Serial pc(USBTX, USBRX);
00012 
00013 void speedtest(char *path, int totallen, int blocklen, bool unlinkfile){
00014   char *data = (char *)malloc(blocklen);
00015   for(int i=0; i < blocklen; i++)
00016     data[i] = 0x30 + (i % 10);
00017 
00018   int wrotelen = 0, avgus = 0, maxus = 0, blavgus = 0, blmaxus = 0;
00019   int maxi = 0, blmaxi = 0;
00020   Timer t;
00021   t.reset();
00022   t.start();
00023   int beginus = t.read_us();
00024   FILE *fp = fopen(path, "w");
00025   int i = 0, j = 0;
00026   while(wrotelen < totallen){
00027     int beforeus = t.read_us();
00028     int wl = fwrite((void *)data, 1, blocklen, fp);
00029     int afterus = t.read_us();
00030     int blockus = afterus - beforeus;
00031     int byteus = blockus / wl;
00032     avgus = (avgus ? (avgus + byteus) / 2 : byteus);
00033     if(maxus < byteus){
00034       maxus = byteus;
00035       maxi = i;
00036     }
00037     blavgus = (blavgus ? (blavgus + blockus) / 2 : blockus);
00038     if(blmaxus < blockus){
00039       blmaxus = blockus;
00040       blmaxi = i;
00041     }
00042     wrotelen += wl;
00043     i++;
00044     if(TEST_PRINTBYTEUS){
00045         if(byteus > 2){
00046             printf("%s %08d", (j++ ? "" : "\r\n"), byteus);
00047         }else{
00048             j = 0;
00049         }
00050     }
00051   }
00052   if(TEST_PRINTBYTEUS) printf("\r\n");
00053   fclose(fp);
00054   
00055   int endus = t.read_us();
00056   t.stop();
00057 
00058   printf("SPIFreq=%dHz TotalBytes=%d (Block)=%dbytes TotalTime=%dus\r\n",
00059      SDHC_SPI_FREQUENCY, wrotelen, blocklen, endus-beginus);
00060   printf("Time(us): max=%d(%d) avg=%d max(block)=%d(%d) avg(block)=%d\r\n",
00061      maxus, maxi, avgus, blmaxus, blmaxi, blavgus);
00062   printf("\r\n");
00063   
00064   if(unlinkfile) remove(path);
00065   free(data);
00066 }
00067 
00068 
00069 int main(){
00070   pc.baud(921600);
00071   SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs
00072   printf("%s [START %04d]\r\n", __FILE__, PSQ);
00073   
00074   char *str = "Hello, World!\n";
00075   int l = strlen(str) + 1;
00076   char *path;
00077   FILE *fp;
00078   char buf[512];
00079   
00080   if(TEST_FWRITEFREAD){
00081     printf("[fwrite()/fread()]\r\n");
00082     printf("write data of %d bytes\r\n", l);
00083     memset(buf, 0x00, sizeof(buf));
00084     path = "/sd/fwrite_fread.txt";
00085     
00086     fp = fopen(path, "w");
00087     int r = fwrite((void *)str, 1, l, fp);
00088     fclose(fp);
00089     printf("fwrite() not wrote whole data %d/%d\r\n", r, l);
00090     fp = fopen(path, "r");
00091     int s = fread((void *)buf, 1, r, fp);
00092     printf("fread() not read whole data %d/%d\r\n", s, r);
00093     for(int i=0; i < s; i++){
00094       printf("%c", buf[i]);
00095     }
00096     printf("\r\n");
00097     fclose(fp);
00098     
00099     printf("speed test...write\r\n");
00100 
00101     speedtest("/sd/fwr_5120000_0001.txt", 5120000, 1, true);
00102     speedtest("/sd/fwr_5120000_0002.txt", 5120000, 2, true);
00103     speedtest("/sd/fwr_5120000_0005.txt", 5120000, 5, true);
00104     speedtest("/sd/fwr_5120000_0010.txt", 5120000, 10, true);
00105     speedtest("/sd/fwr_5120000_0020.txt", 5120000, 20, true);
00106     speedtest("/sd/fwr_5120000_0050.txt", 5120000, 50, true);
00107     speedtest("/sd/fwr_5120000_0100.txt", 5120000, 100, true);
00108     speedtest("/sd/fwr_5120000_0256.txt", 5120000, 256, true);
00109     speedtest("/sd/fwr_5120000_0512.txt", 5120000, 512, true);
00110     speedtest("/sd/fwr_5120000_1024.txt", 5120000, 1024, true);
00111     speedtest("/sd/fwr_5120000_2048.txt", 5120000, 2048, true);
00112     speedtest("/sd/fwr_5120000_4096.txt", 5120000, 4096, true);
00113   }
00114 
00115   if(TEST_FPUTSFGETS){
00116     printf("[fputs()/fgets()]\r\n");
00117     memset(buf, 0x00, sizeof(buf));
00118     path = "/sd/fputs_fgets.txt";
00119     
00120     fp = fopen(path, "w");
00121     fputs(str, fp);
00122     fclose(fp);
00123     fp = fopen(path, "r");
00124     if(fgets(buf, sizeof(buf), fp)){
00125       printf("%s\r\n", buf);
00126     }
00127     fclose(fp);
00128   }
00129   
00130   printf("%s [END %04d]\r\n", __FILE__, PSQ);
00131 }