A collection of examples organized from basics to advanced.

Dependencies:   mbed SDFileSystem

Mbed online compiler has no facility to easily manage a lot of programs or organized them in to related folders. This makes creating an examples and sample pack difficult.

This repository contains a single main.cpp file (which does very little), and a BuildOptions.h file. Simply uncomment the example you would like to compile from the build options. Each example is wrapped in a compiler directive.

If the directive does not include a description comment, it likely does not exist yet. If you would like to contribute to the Examples project, please contact me or fork and issue a pull request.

Committer:
epremeaux
Date:
Tue Jul 09 02:23:18 2019 +0000
Revision:
2:17a5c34b3a79
Added SD card examples. Had to roll back the MBED library to maintain SDFileSystem compatability

Who changed what in which revision?

UserRevisionLine numberNew contents of line
epremeaux 2:17a5c34b3a79 1 /* https://os.mbed.com/users/neilt6/code/SDFileSystem/
epremeaux 2:17a5c34b3a79 2 *
epremeaux 2:17a5c34b3a79 3 * Mounts an SD card, creates a buffer of random data, then writes and reads
epremeaux 2:17a5c34b3a79 4 * back that data. Finishes with a report of the size of the buffer file
epremeaux 2:17a5c34b3a79 5 * and read / write speeds.
epremeaux 2:17a5c34b3a79 6 *
epremeaux 2:17a5c34b3a79 7 * SDFileSystem library requires MBED Release 165, dated Feb 20, 2019. Updates will break it.
epremeaux 2:17a5c34b3a79 8 * If having difficulty, import the link above as independant program.
epremeaux 2:17a5c34b3a79 9 */
epremeaux 2:17a5c34b3a79 10
epremeaux 2:17a5c34b3a79 11 #ifdef COMPILE_SD_Speed_Test
epremeaux 2:17a5c34b3a79 12
epremeaux 2:17a5c34b3a79 13 #include "SDFileSystem.h"
epremeaux 2:17a5c34b3a79 14
epremeaux 2:17a5c34b3a79 15 Timer timer;
epremeaux 2:17a5c34b3a79 16 DigitalIn button(D7, PullUp);
epremeaux 2:17a5c34b3a79 17 // MOSI, MISO, CLK, CS, "name", detect
epremeaux 2:17a5c34b3a79 18 SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd", PA_8, SDFileSystem::SWITCH_NEG_NC, 25000000);
epremeaux 2:17a5c34b3a79 19 char buffer[4096];
epremeaux 2:17a5c34b3a79 20
epremeaux 2:17a5c34b3a79 21 void writeTest()
epremeaux 2:17a5c34b3a79 22 {
epremeaux 2:17a5c34b3a79 23 //Test write performance by creating a 1MB file
epremeaux 2:17a5c34b3a79 24 printf("Testing %iB write performance...", sizeof(buffer));
epremeaux 2:17a5c34b3a79 25 FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
epremeaux 2:17a5c34b3a79 26 if (file != NULL) {
epremeaux 2:17a5c34b3a79 27 timer.start();
epremeaux 2:17a5c34b3a79 28 for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
epremeaux 2:17a5c34b3a79 29 if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
epremeaux 2:17a5c34b3a79 30 timer.stop();
epremeaux 2:17a5c34b3a79 31 printf("write error!\n");
epremeaux 2:17a5c34b3a79 32 timer.reset();
epremeaux 2:17a5c34b3a79 33 return;
epremeaux 2:17a5c34b3a79 34 }
epremeaux 2:17a5c34b3a79 35 }
epremeaux 2:17a5c34b3a79 36 timer.stop();
epremeaux 2:17a5c34b3a79 37 if (file->close())
epremeaux 2:17a5c34b3a79 38 printf("failed to close file!\n");
epremeaux 2:17a5c34b3a79 39 else
epremeaux 2:17a5c34b3a79 40 printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
epremeaux 2:17a5c34b3a79 41 timer.reset();
epremeaux 2:17a5c34b3a79 42 } else {
epremeaux 2:17a5c34b3a79 43 printf("failed to create file!\n");
epremeaux 2:17a5c34b3a79 44 }
epremeaux 2:17a5c34b3a79 45 }
epremeaux 2:17a5c34b3a79 46
epremeaux 2:17a5c34b3a79 47 void readTest()
epremeaux 2:17a5c34b3a79 48 {
epremeaux 2:17a5c34b3a79 49 //Test read performance by reading the 1MB file created by writeTest()
epremeaux 2:17a5c34b3a79 50 printf("Testing %iB read performance...", sizeof(buffer));
epremeaux 2:17a5c34b3a79 51 FileHandle* file = sd.open("Test File.bin", O_RDONLY);
epremeaux 2:17a5c34b3a79 52 if (file != NULL) {
epremeaux 2:17a5c34b3a79 53 timer.start();
epremeaux 2:17a5c34b3a79 54 int iterations = 0;
epremeaux 2:17a5c34b3a79 55 while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
epremeaux 2:17a5c34b3a79 56 iterations++;
epremeaux 2:17a5c34b3a79 57 timer.stop();
epremeaux 2:17a5c34b3a79 58 if (iterations != (1048576 / sizeof(buffer)))
epremeaux 2:17a5c34b3a79 59 printf("read error!\n");
epremeaux 2:17a5c34b3a79 60 else if (file->close())
epremeaux 2:17a5c34b3a79 61 printf("failed to close file!\n");
epremeaux 2:17a5c34b3a79 62 else if (sd.remove("Test File.bin"))
epremeaux 2:17a5c34b3a79 63 printf("failed to delete file!\n");
epremeaux 2:17a5c34b3a79 64 else
epremeaux 2:17a5c34b3a79 65 printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
epremeaux 2:17a5c34b3a79 66 timer.reset();
epremeaux 2:17a5c34b3a79 67 } else {
epremeaux 2:17a5c34b3a79 68 printf("failed to open file!\n");
epremeaux 2:17a5c34b3a79 69 }
epremeaux 2:17a5c34b3a79 70 }
epremeaux 2:17a5c34b3a79 71
epremeaux 2:17a5c34b3a79 72 int main()
epremeaux 2:17a5c34b3a79 73 {
epremeaux 2:17a5c34b3a79 74 printf("SD Card Tester...\n");
epremeaux 2:17a5c34b3a79 75 //Configure CRC, large frames, and write validation
epremeaux 2:17a5c34b3a79 76 sd.crc(true);
epremeaux 2:17a5c34b3a79 77 sd.large_frames(true);
epremeaux 2:17a5c34b3a79 78 sd.write_validation(true);
epremeaux 2:17a5c34b3a79 79
epremeaux 2:17a5c34b3a79 80 printf("Filling buffer with random data for write test...\n");
epremeaux 2:17a5c34b3a79 81 //Fill the buffer with random data for the write test
epremeaux 2:17a5c34b3a79 82 srand(time(NULL));
epremeaux 2:17a5c34b3a79 83 for (int i = 0; i < sizeof(buffer); i++)
epremeaux 2:17a5c34b3a79 84 buffer[i] = rand();
epremeaux 2:17a5c34b3a79 85 printf("Done. Press button to test\n");
epremeaux 2:17a5c34b3a79 86
epremeaux 2:17a5c34b3a79 87 while(1) {
epremeaux 2:17a5c34b3a79 88 //Simple button debouncing
epremeaux 2:17a5c34b3a79 89 wait(0.5);
epremeaux 2:17a5c34b3a79 90
epremeaux 2:17a5c34b3a79 91 //Print the start message
epremeaux 2:17a5c34b3a79 92 printf("\nStarting Tests: ");
epremeaux 2:17a5c34b3a79 93
epremeaux 2:17a5c34b3a79 94 //Wait for the button to be pressed
epremeaux 2:17a5c34b3a79 95 while(button);
epremeaux 2:17a5c34b3a79 96
epremeaux 2:17a5c34b3a79 97 //Make sure a card is present
epremeaux 2:17a5c34b3a79 98 if (!sd.card_present()) {
epremeaux 2:17a5c34b3a79 99 printf("\nNo card present!\n");
epremeaux 2:17a5c34b3a79 100 continue;
epremeaux 2:17a5c34b3a79 101 }
epremeaux 2:17a5c34b3a79 102
epremeaux 2:17a5c34b3a79 103 //Try to mount the SD card
epremeaux 2:17a5c34b3a79 104 printf("\nMounting SD card...");
epremeaux 2:17a5c34b3a79 105 if (sd.mount() != 0) {
epremeaux 2:17a5c34b3a79 106 printf("failed!\n");
epremeaux 2:17a5c34b3a79 107 continue;
epremeaux 2:17a5c34b3a79 108 }
epremeaux 2:17a5c34b3a79 109 printf("success!\n");
epremeaux 2:17a5c34b3a79 110
epremeaux 2:17a5c34b3a79 111 //Display the card type
epremeaux 2:17a5c34b3a79 112 printf("\tCard type: ");
epremeaux 2:17a5c34b3a79 113 SDFileSystem::CardType cardType = sd.card_type();
epremeaux 2:17a5c34b3a79 114 if (cardType == SDFileSystem::CARD_NONE)
epremeaux 2:17a5c34b3a79 115 printf("None\n");
epremeaux 2:17a5c34b3a79 116 else if (cardType == SDFileSystem::CARD_MMC)
epremeaux 2:17a5c34b3a79 117 printf("MMC\n");
epremeaux 2:17a5c34b3a79 118 else if (cardType == SDFileSystem::CARD_SD)
epremeaux 2:17a5c34b3a79 119 printf("SD\n");
epremeaux 2:17a5c34b3a79 120 else if (cardType == SDFileSystem::CARD_SDHC)
epremeaux 2:17a5c34b3a79 121 printf("SDHC\n");
epremeaux 2:17a5c34b3a79 122 else
epremeaux 2:17a5c34b3a79 123 printf("Unknown\n");
epremeaux 2:17a5c34b3a79 124
epremeaux 2:17a5c34b3a79 125 //Display the card capacity
epremeaux 2:17a5c34b3a79 126 printf("\tSectors: %u\n", sd.disk_sectors());
epremeaux 2:17a5c34b3a79 127 printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0);
epremeaux 2:17a5c34b3a79 128
epremeaux 2:17a5c34b3a79 129 /*//Format the card
epremeaux 2:17a5c34b3a79 130 printf("Formatting SD card...");
epremeaux 2:17a5c34b3a79 131 if (sd.format() != 0) {
epremeaux 2:17a5c34b3a79 132 printf("failed!\n");
epremeaux 2:17a5c34b3a79 133 continue;
epremeaux 2:17a5c34b3a79 134 }
epremeaux 2:17a5c34b3a79 135 printf("success!\n");*/
epremeaux 2:17a5c34b3a79 136
epremeaux 2:17a5c34b3a79 137 //Perform a read/write test
epremeaux 2:17a5c34b3a79 138 writeTest();
epremeaux 2:17a5c34b3a79 139 readTest();
epremeaux 2:17a5c34b3a79 140
epremeaux 2:17a5c34b3a79 141 //Unmount the SD card
epremeaux 2:17a5c34b3a79 142 sd.unmount();
epremeaux 2:17a5c34b3a79 143 }
epremeaux 2:17a5c34b3a79 144 }
epremeaux 2:17a5c34b3a79 145
epremeaux 2:17a5c34b3a79 146 #endif