Emery Premeaux / Mbed 2 deprecated Examples

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SD_Speed_Test.cpp Source File

SD_Speed_Test.cpp

00001 /* https://os.mbed.com/users/neilt6/code/SDFileSystem/
00002  *
00003  * Mounts an SD card, creates a buffer of random data, then writes and reads
00004  * back that data. Finishes with a report of the size of the buffer file
00005  * and read / write speeds.
00006  *
00007  * SDFileSystem library requires MBED Release 165, dated Feb 20, 2019. Updates will break it.
00008  * If having difficulty, import the link above as independant program.
00009  */
00010 
00011 #ifdef COMPILE_SD_Speed_Test
00012 
00013 #include "SDFileSystem.h"
00014 
00015 Timer timer;
00016 DigitalIn button(D7, PullUp);
00017 // MOSI, MISO, CLK, CS, "name", detect
00018 SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd", PA_8, SDFileSystem::SWITCH_NEG_NC, 25000000);
00019 char buffer[4096];
00020 
00021 void writeTest()
00022 {
00023     //Test write performance by creating a 1MB file
00024     printf("Testing %iB write performance...", sizeof(buffer));
00025     FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
00026     if (file != NULL) {
00027         timer.start();
00028         for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
00029             if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
00030                 timer.stop();
00031                 printf("write error!\n");
00032                 timer.reset();
00033                 return;
00034             }
00035         }
00036         timer.stop();
00037         if (file->close())
00038             printf("failed to close file!\n");
00039         else
00040             printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
00041         timer.reset();
00042     } else {
00043         printf("failed to create file!\n");
00044     }
00045 }
00046 
00047 void readTest()
00048 {
00049     //Test read performance by reading the 1MB file created by writeTest()
00050     printf("Testing %iB read performance...", sizeof(buffer));
00051     FileHandle* file = sd.open("Test File.bin", O_RDONLY);
00052     if (file != NULL) {
00053         timer.start();
00054         int iterations = 0;
00055         while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
00056             iterations++;
00057         timer.stop();
00058         if (iterations != (1048576 / sizeof(buffer)))
00059             printf("read error!\n");
00060         else if (file->close())
00061             printf("failed to close file!\n");
00062         else if (sd.remove("Test File.bin"))
00063             printf("failed to delete file!\n");
00064         else
00065             printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
00066         timer.reset();
00067     } else {
00068         printf("failed to open file!\n");
00069     }
00070 }
00071 
00072 int main()
00073 {
00074     printf("SD Card Tester...\n");
00075     //Configure CRC, large frames, and write validation
00076     sd.crc(true);
00077     sd.large_frames(true);
00078     sd.write_validation(true);
00079 
00080     printf("Filling buffer with random data for write test...\n");
00081     //Fill the buffer with random data for the write test
00082     srand(time(NULL));
00083     for (int i = 0; i < sizeof(buffer); i++)
00084         buffer[i] = rand();
00085     printf("Done. Press button to test\n");
00086 
00087     while(1) {
00088         //Simple button debouncing
00089         wait(0.5);
00090 
00091         //Print the start message
00092         printf("\nStarting Tests: ");
00093 
00094         //Wait for the button to be pressed
00095         while(button);
00096 
00097         //Make sure a card is present
00098         if (!sd.card_present()) {
00099             printf("\nNo card present!\n");
00100             continue;
00101         }
00102 
00103         //Try to mount the SD card
00104         printf("\nMounting SD card...");
00105         if (sd.mount() != 0) {
00106             printf("failed!\n");
00107             continue;
00108         }
00109         printf("success!\n");
00110 
00111         //Display the card type
00112         printf("\tCard type: ");
00113         SDFileSystem::CardType cardType = sd.card_type();
00114         if (cardType == SDFileSystem::CARD_NONE)
00115             printf("None\n");
00116         else if (cardType == SDFileSystem::CARD_MMC)
00117             printf("MMC\n");
00118         else if (cardType == SDFileSystem::CARD_SD)
00119             printf("SD\n");
00120         else if (cardType == SDFileSystem::CARD_SDHC)
00121             printf("SDHC\n");
00122         else
00123             printf("Unknown\n");
00124 
00125         //Display the card capacity
00126         printf("\tSectors: %u\n", sd.disk_sectors());
00127         printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0);
00128 
00129         /*//Format the card
00130         printf("Formatting SD card...");
00131         if (sd.format() != 0) {
00132             printf("failed!\n");
00133             continue;
00134         }
00135         printf("success!\n");*/
00136 
00137         //Perform a read/write test
00138         writeTest();
00139         readTest();
00140 
00141         //Unmount the SD card
00142         sd.unmount();
00143     }
00144 }
00145 
00146 #endif