pls work
Dependencies: mbed WS2813 PixelArray SDFileSystem ds3231
Fork of SDFileSystem_HelloWorld by
main.cpp@26:dcced86ba984, 2018-08-02 (annotated)
- Committer:
- deldering95
- Date:
- Thu Aug 02 14:42:56 2018 +0000
- Revision:
- 26:dcced86ba984
- Parent:
- 20:ec1c4f02d50e
- Child:
- 27:2b989633a518
Please dont;
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; |
deldering95 | 26:dcced86ba984 | 5 | //DigitalIn button(p21, PullUp); |
deldering95 | 26:dcced86ba984 | 6 | SDFileSystem sd(PC_12,PC_11,PC_10,PA_15, "sd", NC, SDFileSystem::SWITCH_NONE, 400000); |
deldering95 | 26:dcced86ba984 | 7 | RawSerial pc(PA_9,PA_10); |
neilt6 | 10:ae649a596123 | 8 | char buffer[4096]; |
neilt6 | 10:ae649a596123 | 9 | |
neilt6 | 10:ae649a596123 | 10 | void writeTest() |
neilt6 | 10:ae649a596123 | 11 | { |
neilt6 | 10:ae649a596123 | 12 | //Test write performance by creating a 1MB file |
deldering95 | 26:dcced86ba984 | 13 | pc.printf("Testing %iB write performance...", sizeof(buffer)); |
deldering95 | 26:dcced86ba984 | 14 | FileHandle* file = sd.open("Test_File.bin", O_WRONLY | O_CREAT); |
deldering95 | 26:dcced86ba984 | 15 | pc.printf("whatever u want\n"); |
neilt6 | 10:ae649a596123 | 16 | if (file != NULL) { |
neilt6 | 10:ae649a596123 | 17 | timer.start(); |
neilt6 | 10:ae649a596123 | 18 | for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { |
deldering95 | 26:dcced86ba984 | 19 | pc.printf("%d\n",i); |
neilt6 | 10:ae649a596123 | 20 | if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { |
neilt6 | 10:ae649a596123 | 21 | timer.stop(); |
deldering95 | 26:dcced86ba984 | 22 | pc.printf("write error!\n"); |
neilt6 | 10:ae649a596123 | 23 | timer.reset(); |
neilt6 | 10:ae649a596123 | 24 | return; |
neilt6 | 10:ae649a596123 | 25 | } |
neilt6 | 10:ae649a596123 | 26 | } |
neilt6 | 10:ae649a596123 | 27 | timer.stop(); |
neilt6 | 10:ae649a596123 | 28 | if (file->close()) |
deldering95 | 26:dcced86ba984 | 29 | pc.printf("failed to close file!\n"); |
neilt6 | 10:ae649a596123 | 30 | else |
deldering95 | 26:dcced86ba984 | 31 | pc.printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); |
neilt6 | 10:ae649a596123 | 32 | timer.reset(); |
neilt6 | 10:ae649a596123 | 33 | } else { |
deldering95 | 26:dcced86ba984 | 34 | pc.printf("failed to create file!\n"); |
neilt6 | 10:ae649a596123 | 35 | } |
neilt6 | 10:ae649a596123 | 36 | } |
neilt6 | 10:ae649a596123 | 37 | |
neilt6 | 10:ae649a596123 | 38 | void readTest() |
neilt6 | 10:ae649a596123 | 39 | { |
neilt6 | 10:ae649a596123 | 40 | //Test read performance by reading the 1MB file created by writeTest() |
deldering95 | 26:dcced86ba984 | 41 | pc.printf("Testing %iB read performance...", sizeof(buffer)); |
neilt6 | 10:ae649a596123 | 42 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); |
neilt6 | 10:ae649a596123 | 43 | if (file != NULL) { |
neilt6 | 10:ae649a596123 | 44 | timer.start(); |
neilt6 | 12:bd8b17cd6a7b | 45 | int iterations = 0; |
deldering95 | 26:dcced86ba984 | 46 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)){ |
neilt6 | 12:bd8b17cd6a7b | 47 | iterations++; |
deldering95 | 26:dcced86ba984 | 48 | pc.printf("%d\n",iterations);} |
neilt6 | 10:ae649a596123 | 49 | timer.stop(); |
neilt6 | 12:bd8b17cd6a7b | 50 | if (iterations != (1048576 / sizeof(buffer))) |
deldering95 | 26:dcced86ba984 | 51 | pc.printf("read error!\n"); |
neilt6 | 12:bd8b17cd6a7b | 52 | else if (file->close()) |
deldering95 | 26:dcced86ba984 | 53 | pc.printf("failed to close file!\n"); |
neilt6 | 12:bd8b17cd6a7b | 54 | else if (sd.remove("Test File.bin")) |
deldering95 | 26:dcced86ba984 | 55 | pc.printf("failed to delete file!\n"); |
neilt6 | 12:bd8b17cd6a7b | 56 | else |
deldering95 | 26:dcced86ba984 | 57 | pc.printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); |
neilt6 | 10:ae649a596123 | 58 | timer.reset(); |
neilt6 | 10:ae649a596123 | 59 | } else { |
deldering95 | 26:dcced86ba984 | 60 | pc.printf("failed to open file!\n"); |
neilt6 | 10:ae649a596123 | 61 | } |
neilt6 | 10:ae649a596123 | 62 | } |
neilt6 | 0:8f2b6eed2a9d | 63 | |
neilt6 | 0:8f2b6eed2a9d | 64 | int main() |
neilt6 | 0:8f2b6eed2a9d | 65 | { |
neilt6 | 12:bd8b17cd6a7b | 66 | //Configure CRC, large frames, and write validation |
neilt6 | 10:ae649a596123 | 67 | sd.crc(true); |
neilt6 | 7:17ca3091939f | 68 | sd.large_frames(true); |
neilt6 | 12:bd8b17cd6a7b | 69 | sd.write_validation(true); |
neilt6 | 7:17ca3091939f | 70 | |
deldering95 | 26:dcced86ba984 | 71 | pc.baud(115200); |
deldering95 | 26:dcced86ba984 | 72 | pc.format(8, SerialBase::None, 1); |
deldering95 | 26:dcced86ba984 | 73 | pc.printf("pls work"); |
neilt6 | 10:ae649a596123 | 74 | //Fill the buffer with random data for the write test |
deldering95 | 26:dcced86ba984 | 75 | //srand(time(NULL)); |
deldering95 | 26:dcced86ba984 | 76 | for (int i = 0; i < sizeof(buffer); i++){ |
neilt6 | 10:ae649a596123 | 77 | buffer[i] = rand(); |
deldering95 | 26:dcced86ba984 | 78 | pc.printf("pls work %d\n",i);} |
neilt6 | 10:ae649a596123 | 79 | |
neilt6 | 0:8f2b6eed2a9d | 80 | while(1) { |
neilt6 | 17:e5b7469082c8 | 81 | //Simple button debouncing |
neilt6 | 17:e5b7469082c8 | 82 | wait(0.5); |
neilt6 | 17:e5b7469082c8 | 83 | |
neilt6 | 0:8f2b6eed2a9d | 84 | //Print the start message |
deldering95 | 26:dcced86ba984 | 85 | pc.printf("\nPress the button to perform tests: "); |
neilt6 | 0:8f2b6eed2a9d | 86 | |
neilt6 | 0:8f2b6eed2a9d | 87 | //Wait for the button to be pressed |
deldering95 | 26:dcced86ba984 | 88 | //while(button); |
neilt6 | 0:8f2b6eed2a9d | 89 | |
neilt6 | 19:ae979143c796 | 90 | //Make sure a card is present |
neilt6 | 19:ae979143c796 | 91 | if (!sd.card_present()) { |
deldering95 | 26:dcced86ba984 | 92 | pc.printf("\nNo card present!\n"); |
neilt6 | 19:ae979143c796 | 93 | continue; |
neilt6 | 19:ae979143c796 | 94 | } |
neilt6 | 19:ae979143c796 | 95 | |
neilt6 | 19:ae979143c796 | 96 | //Try to mount the SD card |
deldering95 | 26:dcced86ba984 | 97 | pc.printf("\nMounting SD card..."); |
neilt6 | 19:ae979143c796 | 98 | if (sd.mount() != 0) { |
deldering95 | 26:dcced86ba984 | 99 | pc.printf("failed!\n"); |
neilt6 | 19:ae979143c796 | 100 | continue; |
neilt6 | 19:ae979143c796 | 101 | } |
deldering95 | 26:dcced86ba984 | 102 | pc.printf("success!\n"); |
neilt6 | 19:ae979143c796 | 103 | |
neilt6 | 17:e5b7469082c8 | 104 | //Display the card type |
deldering95 | 26:dcced86ba984 | 105 | pc.printf("\tCard type: "); |
neilt6 | 17:e5b7469082c8 | 106 | SDFileSystem::CardType cardType = sd.card_type(); |
neilt6 | 17:e5b7469082c8 | 107 | if (cardType == SDFileSystem::CARD_NONE) |
deldering95 | 26:dcced86ba984 | 108 | pc.printf("None\n"); |
neilt6 | 17:e5b7469082c8 | 109 | else if (cardType == SDFileSystem::CARD_MMC) |
deldering95 | 26:dcced86ba984 | 110 | pc.printf("MMC\n"); |
neilt6 | 17:e5b7469082c8 | 111 | else if (cardType == SDFileSystem::CARD_SD) |
deldering95 | 26:dcced86ba984 | 112 | pc.printf("SD\n"); |
neilt6 | 17:e5b7469082c8 | 113 | else if (cardType == SDFileSystem::CARD_SDHC) |
deldering95 | 26:dcced86ba984 | 114 | pc.printf("SDHC\n"); |
neilt6 | 0:8f2b6eed2a9d | 115 | else |
deldering95 | 26:dcced86ba984 | 116 | pc.printf("Unknown\n"); |
neilt6 | 17:e5b7469082c8 | 117 | |
neilt6 | 17:e5b7469082c8 | 118 | //Display the card capacity |
deldering95 | 26:dcced86ba984 | 119 | pc.printf("\tSectors: %u\n", sd.disk_sectors()); |
deldering95 | 26:dcced86ba984 | 120 | pc.printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0); |
neilt6 | 0:8f2b6eed2a9d | 121 | |
neilt6 | 17:e5b7469082c8 | 122 | /*//Format the card |
deldering95 | 26:dcced86ba984 | 123 | pc.printf("Formatting SD card..."); |
neilt6 | 17:e5b7469082c8 | 124 | if (sd.format() != 0) { |
deldering95 | 26:dcced86ba984 | 125 | pc.printf("failed!\n"); |
neilt6 | 17:e5b7469082c8 | 126 | continue; |
neilt6 | 17:e5b7469082c8 | 127 | } |
deldering95 | 26:dcced86ba984 | 128 | pc.printf("success!\n");*/ |
neilt6 | 10:ae649a596123 | 129 | |
neilt6 | 17:e5b7469082c8 | 130 | //Perform a read/write test |
neilt6 | 10:ae649a596123 | 131 | writeTest(); |
neilt6 | 10:ae649a596123 | 132 | readTest(); |
neilt6 | 0:8f2b6eed2a9d | 133 | |
neilt6 | 17:e5b7469082c8 | 134 | //Unmount the SD card |
neilt6 | 10:ae649a596123 | 135 | sd.unmount(); |
neilt6 | 0:8f2b6eed2a9d | 136 | } |
neilt6 | 0:8f2b6eed2a9d | 137 | } |