![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test for sd card
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@20:ec1c4f02d50e, 2015-12-11 (annotated)
- Committer:
- neilt6
- Date:
- Fri Dec 11 16:34:13 2015 +0000
- Revision:
- 20:ec1c4f02d50e
- Parent:
- 19:ae979143c796
- Child:
- 26:280dfa8b03ac
Updated SDFileSystem
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:8f2b6eed2a9d | 1 | #include "mbed.h" |
neilt6 | 0:8f2b6eed2a9d | 2 | #include "SDFileSystem.h" |
neilt6 | 0:8f2b6eed2a9d | 3 | |
neilt6 | 10:ae649a596123 | 4 | Timer timer; |
neilt6 | 0:8f2b6eed2a9d | 5 | DigitalIn button(p21, PullUp); |
neilt6 | 11:2be49b81dc0b | 6 | SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NEG_NO, 25000000); |
neilt6 | 10:ae649a596123 | 7 | char buffer[4096]; |
neilt6 | 10:ae649a596123 | 8 | |
neilt6 | 10:ae649a596123 | 9 | void writeTest() |
neilt6 | 10:ae649a596123 | 10 | { |
neilt6 | 10:ae649a596123 | 11 | //Test write performance by creating a 1MB file |
neilt6 | 10:ae649a596123 | 12 | printf("Testing %iB write performance...", sizeof(buffer)); |
neilt6 | 10:ae649a596123 | 13 | FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); |
neilt6 | 10:ae649a596123 | 14 | if (file != NULL) { |
neilt6 | 10:ae649a596123 | 15 | timer.start(); |
neilt6 | 10:ae649a596123 | 16 | for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { |
neilt6 | 10:ae649a596123 | 17 | if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { |
neilt6 | 10:ae649a596123 | 18 | timer.stop(); |
neilt6 | 10:ae649a596123 | 19 | printf("write error!\n"); |
neilt6 | 10:ae649a596123 | 20 | timer.reset(); |
neilt6 | 10:ae649a596123 | 21 | return; |
neilt6 | 10:ae649a596123 | 22 | } |
neilt6 | 10:ae649a596123 | 23 | } |
neilt6 | 10:ae649a596123 | 24 | timer.stop(); |
neilt6 | 10:ae649a596123 | 25 | if (file->close()) |
neilt6 | 10:ae649a596123 | 26 | printf("failed to close file!\n"); |
neilt6 | 10:ae649a596123 | 27 | else |
neilt6 | 10:ae649a596123 | 28 | printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); |
neilt6 | 10:ae649a596123 | 29 | timer.reset(); |
neilt6 | 10:ae649a596123 | 30 | } else { |
neilt6 | 10:ae649a596123 | 31 | printf("failed to create file!\n"); |
neilt6 | 10:ae649a596123 | 32 | } |
neilt6 | 10:ae649a596123 | 33 | } |
neilt6 | 10:ae649a596123 | 34 | |
neilt6 | 10:ae649a596123 | 35 | void readTest() |
neilt6 | 10:ae649a596123 | 36 | { |
neilt6 | 10:ae649a596123 | 37 | //Test read performance by reading the 1MB file created by writeTest() |
neilt6 | 10:ae649a596123 | 38 | printf("Testing %iB read performance...", sizeof(buffer)); |
neilt6 | 10:ae649a596123 | 39 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); |
neilt6 | 10:ae649a596123 | 40 | if (file != NULL) { |
neilt6 | 10:ae649a596123 | 41 | timer.start(); |
neilt6 | 12:bd8b17cd6a7b | 42 | int iterations = 0; |
neilt6 | 12:bd8b17cd6a7b | 43 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) |
neilt6 | 12:bd8b17cd6a7b | 44 | iterations++; |
neilt6 | 10:ae649a596123 | 45 | timer.stop(); |
neilt6 | 12:bd8b17cd6a7b | 46 | if (iterations != (1048576 / sizeof(buffer))) |
neilt6 | 12:bd8b17cd6a7b | 47 | printf("read error!\n"); |
neilt6 | 12:bd8b17cd6a7b | 48 | else if (file->close()) |
neilt6 | 10:ae649a596123 | 49 | printf("failed to close file!\n"); |
neilt6 | 12:bd8b17cd6a7b | 50 | else if (sd.remove("Test File.bin")) |
neilt6 | 12:bd8b17cd6a7b | 51 | printf("failed to delete file!\n"); |
neilt6 | 12:bd8b17cd6a7b | 52 | else |
neilt6 | 12:bd8b17cd6a7b | 53 | printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); |
neilt6 | 10:ae649a596123 | 54 | timer.reset(); |
neilt6 | 10:ae649a596123 | 55 | } else { |
neilt6 | 10:ae649a596123 | 56 | printf("failed to open file!\n"); |
neilt6 | 10:ae649a596123 | 57 | } |
neilt6 | 10:ae649a596123 | 58 | } |
neilt6 | 0:8f2b6eed2a9d | 59 | |
neilt6 | 0:8f2b6eed2a9d | 60 | int main() |
neilt6 | 0:8f2b6eed2a9d | 61 | { |
neilt6 | 12:bd8b17cd6a7b | 62 | //Configure CRC, large frames, and write validation |
neilt6 | 10:ae649a596123 | 63 | sd.crc(true); |
neilt6 | 7:17ca3091939f | 64 | sd.large_frames(true); |
neilt6 | 12:bd8b17cd6a7b | 65 | sd.write_validation(true); |
neilt6 | 7:17ca3091939f | 66 | |
neilt6 | 10:ae649a596123 | 67 | //Fill the buffer with random data for the write test |
neilt6 | 10:ae649a596123 | 68 | srand(time(NULL)); |
neilt6 | 10:ae649a596123 | 69 | for (int i = 0; i < sizeof(buffer); i++) |
neilt6 | 10:ae649a596123 | 70 | buffer[i] = rand(); |
neilt6 | 10:ae649a596123 | 71 | |
neilt6 | 0:8f2b6eed2a9d | 72 | while(1) { |
neilt6 | 17:e5b7469082c8 | 73 | //Simple button debouncing |
neilt6 | 17:e5b7469082c8 | 74 | wait(0.5); |
neilt6 | 17:e5b7469082c8 | 75 | |
neilt6 | 0:8f2b6eed2a9d | 76 | //Print the start message |
neilt6 | 0:8f2b6eed2a9d | 77 | printf("\nPress the button to perform tests: "); |
neilt6 | 0:8f2b6eed2a9d | 78 | |
neilt6 | 0:8f2b6eed2a9d | 79 | //Wait for the button to be pressed |
neilt6 | 0:8f2b6eed2a9d | 80 | while(button); |
neilt6 | 0:8f2b6eed2a9d | 81 | |
neilt6 | 19:ae979143c796 | 82 | //Make sure a card is present |
neilt6 | 19:ae979143c796 | 83 | if (!sd.card_present()) { |
neilt6 | 19:ae979143c796 | 84 | printf("\nNo card present!\n"); |
neilt6 | 19:ae979143c796 | 85 | continue; |
neilt6 | 19:ae979143c796 | 86 | } |
neilt6 | 19:ae979143c796 | 87 | |
neilt6 | 19:ae979143c796 | 88 | //Try to mount the SD card |
neilt6 | 19:ae979143c796 | 89 | printf("\nMounting SD card..."); |
neilt6 | 19:ae979143c796 | 90 | if (sd.mount() != 0) { |
neilt6 | 19:ae979143c796 | 91 | printf("failed!\n"); |
neilt6 | 19:ae979143c796 | 92 | continue; |
neilt6 | 19:ae979143c796 | 93 | } |
neilt6 | 19:ae979143c796 | 94 | printf("success!\n"); |
neilt6 | 19:ae979143c796 | 95 | |
neilt6 | 17:e5b7469082c8 | 96 | //Display the card type |
neilt6 | 19:ae979143c796 | 97 | printf("\tCard type: "); |
neilt6 | 17:e5b7469082c8 | 98 | SDFileSystem::CardType cardType = sd.card_type(); |
neilt6 | 17:e5b7469082c8 | 99 | if (cardType == SDFileSystem::CARD_NONE) |
neilt6 | 0:8f2b6eed2a9d | 100 | printf("None\n"); |
neilt6 | 17:e5b7469082c8 | 101 | else if (cardType == SDFileSystem::CARD_MMC) |
neilt6 | 0:8f2b6eed2a9d | 102 | printf("MMC\n"); |
neilt6 | 17:e5b7469082c8 | 103 | else if (cardType == SDFileSystem::CARD_SD) |
neilt6 | 0:8f2b6eed2a9d | 104 | printf("SD\n"); |
neilt6 | 17:e5b7469082c8 | 105 | else if (cardType == SDFileSystem::CARD_SDHC) |
neilt6 | 0:8f2b6eed2a9d | 106 | printf("SDHC\n"); |
neilt6 | 0:8f2b6eed2a9d | 107 | else |
neilt6 | 0:8f2b6eed2a9d | 108 | printf("Unknown\n"); |
neilt6 | 17:e5b7469082c8 | 109 | |
neilt6 | 17:e5b7469082c8 | 110 | //Display the card capacity |
neilt6 | 20:ec1c4f02d50e | 111 | printf("\tSectors: %u\n", sd.disk_sectors()); |
neilt6 | 19:ae979143c796 | 112 | printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0); |
neilt6 | 0:8f2b6eed2a9d | 113 | |
neilt6 | 17:e5b7469082c8 | 114 | /*//Format the card |
neilt6 | 17:e5b7469082c8 | 115 | printf("Formatting SD card..."); |
neilt6 | 17:e5b7469082c8 | 116 | if (sd.format() != 0) { |
neilt6 | 17:e5b7469082c8 | 117 | printf("failed!\n"); |
neilt6 | 17:e5b7469082c8 | 118 | continue; |
neilt6 | 17:e5b7469082c8 | 119 | } |
neilt6 | 17:e5b7469082c8 | 120 | printf("success!\n");*/ |
neilt6 | 10:ae649a596123 | 121 | |
neilt6 | 17:e5b7469082c8 | 122 | //Perform a read/write test |
neilt6 | 10:ae649a596123 | 123 | writeTest(); |
neilt6 | 10:ae649a596123 | 124 | readTest(); |
neilt6 | 0:8f2b6eed2a9d | 125 | |
neilt6 | 17:e5b7469082c8 | 126 | //Unmount the SD card |
neilt6 | 10:ae649a596123 | 127 | sd.unmount(); |
neilt6 | 0:8f2b6eed2a9d | 128 | } |
neilt6 | 0:8f2b6eed2a9d | 129 | } |