Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
Fork of PES4_Programme by
source/sdcard.cpp@52:701d0c2f47d7, 2018-03-29 (annotated)
- Committer:
- cittecla
- Date:
- Thu Mar 29 18:27:02 2018 +0000
- Revision:
- 52:701d0c2f47d7
- Parent:
- 51:a98ffbd41e76
- Child:
- 53:1c61cadbcb35
Added SD card test tool
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| cittecla | 51:a98ffbd41e76 | 1 | #include "sdcard.h" | 
| cittecla | 51:a98ffbd41e76 | 2 | |
| cittecla | 51:a98ffbd41e76 | 3 | |
| cittecla | 51:a98ffbd41e76 | 4 | Timer timer; | 
| cittecla | 51:a98ffbd41e76 | 5 | |
| cittecla | 51:a98ffbd41e76 | 6 | DigitalIn button(PC_13, PullUp); | 
| cittecla | 51:a98ffbd41e76 | 7 | |
| cittecla | 51:a98ffbd41e76 | 8 | SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000); | 
| cittecla | 51:a98ffbd41e76 | 9 | char buffer[4096]; | 
| cittecla | 51:a98ffbd41e76 | 10 | |
| cittecla | 51:a98ffbd41e76 | 11 | void writeTest() | 
| cittecla | 51:a98ffbd41e76 | 12 | { | 
| cittecla | 51:a98ffbd41e76 | 13 | //Test write performance by creating a 1MB file | 
| cittecla | 51:a98ffbd41e76 | 14 | printf("Testing %iB write performance...\n\r", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 15 | FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); | 
| cittecla | 51:a98ffbd41e76 | 16 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 17 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 18 | for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { | 
| cittecla | 51:a98ffbd41e76 | 19 | if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { | 
| cittecla | 51:a98ffbd41e76 | 20 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 21 | printf("write error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 22 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 23 | return; | 
| cittecla | 51:a98ffbd41e76 | 24 | } | 
| cittecla | 51:a98ffbd41e76 | 25 | } | 
| cittecla | 51:a98ffbd41e76 | 26 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 27 | if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 28 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 29 | else | 
| cittecla | 51:a98ffbd41e76 | 30 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 31 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 32 | } else { | 
| cittecla | 51:a98ffbd41e76 | 33 | printf("failed to create file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 34 | } | 
| cittecla | 51:a98ffbd41e76 | 35 | } | 
| cittecla | 51:a98ffbd41e76 | 36 | |
| cittecla | 51:a98ffbd41e76 | 37 | void readTest() | 
| cittecla | 51:a98ffbd41e76 | 38 | { | 
| cittecla | 51:a98ffbd41e76 | 39 | //Test read performance by reading the 1MB file created by writeTest() | 
| cittecla | 51:a98ffbd41e76 | 40 | printf("Testing %iB read performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 41 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); | 
| cittecla | 51:a98ffbd41e76 | 42 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 43 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 44 | int iterations = 0; | 
| cittecla | 51:a98ffbd41e76 | 45 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) | 
| cittecla | 51:a98ffbd41e76 | 46 | iterations++; | 
| cittecla | 51:a98ffbd41e76 | 47 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 48 | if (iterations != (1048576 / sizeof(buffer))) | 
| cittecla | 51:a98ffbd41e76 | 49 | printf("read error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 50 | else if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 51 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 52 | else if (sd.remove("Test File.bin")) | 
| cittecla | 51:a98ffbd41e76 | 53 | printf("failed to delete file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 54 | else | 
| cittecla | 51:a98ffbd41e76 | 55 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 56 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 57 | } else { | 
| cittecla | 51:a98ffbd41e76 | 58 | printf("failed to open file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 59 | } | 
| cittecla | 51:a98ffbd41e76 | 60 | } | 
| cittecla | 51:a98ffbd41e76 | 61 | |
| cittecla | 51:a98ffbd41e76 | 62 | void testSd() | 
| cittecla | 51:a98ffbd41e76 | 63 | { | 
| cittecla | 51:a98ffbd41e76 | 64 | //Configure CRC, large frames, and write validation | 
| cittecla | 51:a98ffbd41e76 | 65 | sd.crc(true); | 
| cittecla | 51:a98ffbd41e76 | 66 | sd.large_frames(true); | 
| cittecla | 51:a98ffbd41e76 | 67 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 68 | |
| cittecla | 51:a98ffbd41e76 | 69 | //Fill the buffer with random data for the write test | 
| cittecla | 51:a98ffbd41e76 | 70 | srand(time(NULL)); | 
| cittecla | 51:a98ffbd41e76 | 71 | for (int i = 0; i < sizeof(buffer); i++) | 
| cittecla | 51:a98ffbd41e76 | 72 | buffer[i] = rand(); | 
| cittecla | 51:a98ffbd41e76 | 73 | |
| cittecla | 51:a98ffbd41e76 | 74 | |
| cittecla | 51:a98ffbd41e76 | 75 | //Print the start message | 
| cittecla | 52:701d0c2f47d7 | 76 | printf("\n\rStarting SD Card test application:"); | 
| cittecla | 51:a98ffbd41e76 | 77 | |
| cittecla | 51:a98ffbd41e76 | 78 | |
| cittecla | 51:a98ffbd41e76 | 79 | //Make sure a card is present | 
| cittecla | 51:a98ffbd41e76 | 80 | if (!sd.card_present()) { | 
| cittecla | 51:a98ffbd41e76 | 81 | printf("\n\rNo card present!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 82 | } | 
| cittecla | 51:a98ffbd41e76 | 83 | |
| cittecla | 51:a98ffbd41e76 | 84 | //Try to mount the SD card | 
| cittecla | 51:a98ffbd41e76 | 85 | printf("\n\rMounting SD card..."); | 
| cittecla | 51:a98ffbd41e76 | 86 | if (sd.mount() != 0) { | 
| cittecla | 51:a98ffbd41e76 | 87 | printf("failed!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 88 | } | 
| cittecla | 51:a98ffbd41e76 | 89 | printf("success!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 90 | |
| cittecla | 51:a98ffbd41e76 | 91 | //Display the card type | 
| cittecla | 51:a98ffbd41e76 | 92 | printf("\tCard type: "); | 
| cittecla | 51:a98ffbd41e76 | 93 | SDFileSystem::CardType cardType = sd.card_type(); | 
| cittecla | 51:a98ffbd41e76 | 94 | if (cardType == SDFileSystem::CARD_NONE) | 
| cittecla | 51:a98ffbd41e76 | 95 | printf("None\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 96 | else if (cardType == SDFileSystem::CARD_MMC) | 
| cittecla | 51:a98ffbd41e76 | 97 | printf("MMC\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 98 | else if (cardType == SDFileSystem::CARD_SD) | 
| cittecla | 51:a98ffbd41e76 | 99 | printf("SD\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 100 | else if (cardType == SDFileSystem::CARD_SDHC) | 
| cittecla | 51:a98ffbd41e76 | 101 | printf("SDHC\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 102 | else | 
| cittecla | 51:a98ffbd41e76 | 103 | printf("Unknown\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 104 | |
| cittecla | 51:a98ffbd41e76 | 105 | //Display the card capacity | 
| cittecla | 51:a98ffbd41e76 | 106 | printf("\tSectors: %u\n\r", sd.disk_sectors()); | 
| cittecla | 51:a98ffbd41e76 | 107 | printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0); | 
| cittecla | 51:a98ffbd41e76 | 108 | |
| cittecla | 51:a98ffbd41e76 | 109 | /*//Format the card | 
| cittecla | 51:a98ffbd41e76 | 110 | printf("Formatting SD card..."); | 
| cittecla | 51:a98ffbd41e76 | 111 | if (sd.format() != 0) { | 
| cittecla | 51:a98ffbd41e76 | 112 | printf("failed!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 113 | continue; | 
| cittecla | 51:a98ffbd41e76 | 114 | } | 
| cittecla | 51:a98ffbd41e76 | 115 | printf("success!\n\r");*/ | 
| cittecla | 51:a98ffbd41e76 | 116 | |
| cittecla | 51:a98ffbd41e76 | 117 | //Perform a read/write test | 
| cittecla | 51:a98ffbd41e76 | 118 | writeTest(); | 
| cittecla | 51:a98ffbd41e76 | 119 | readTest(); | 
| cittecla | 51:a98ffbd41e76 | 120 | |
| cittecla | 51:a98ffbd41e76 | 121 | |
| cittecla | 51:a98ffbd41e76 | 122 | |
| cittecla | 51:a98ffbd41e76 | 123 | printf("write to SD card on a txt file:\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 124 | printf("\tHello World!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 125 | |
| cittecla | 51:a98ffbd41e76 | 126 | FILE *fp = fopen("/sd/medication/sdtest.txt", "a"); | 
| cittecla | 51:a98ffbd41e76 | 127 | if(fp == NULL) { | 
| cittecla | 51:a98ffbd41e76 | 128 | printf("\tCould not open file for write\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 129 | } else { | 
| cittecla | 51:a98ffbd41e76 | 130 | printf("\tfile opened\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 131 | } | 
| cittecla | 51:a98ffbd41e76 | 132 | |
| cittecla | 51:a98ffbd41e76 | 133 | fprintf(fp, "Hello fun SD Card World!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 134 | fclose(fp); | 
| cittecla | 51:a98ffbd41e76 | 135 | |
| cittecla | 51:a98ffbd41e76 | 136 | printf("\tText written to SD card\n\r"); | 
| cittecla | 52:701d0c2f47d7 | 137 | printf("\tGoodbye World!\r\n"); | 
| cittecla | 51:a98ffbd41e76 | 138 | |
| cittecla | 51:a98ffbd41e76 | 139 | //Unmount the SD card | 
| cittecla | 51:a98ffbd41e76 | 140 | sd.unmount(); | 
| cittecla | 51:a98ffbd41e76 | 141 | } | 
