Reading Analog Ports and Saving in a micro SD CARD with the KL25z
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@10:ae649a596123, 2014-08-14 (annotated)
- Committer:
- neilt6
- Date:
- Thu Aug 14 22:28:28 2014 +0000
- Revision:
- 10:ae649a596123
- Parent:
- 7:17ca3091939f
- Child:
- 11:2be49b81dc0b
Changed simple read/write test to performance test
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 | 10:ae649a596123 | 6 | SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_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 | 10:ae649a596123 | 42 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)); |
neilt6 | 10:ae649a596123 | 43 | timer.stop(); |
neilt6 | 10:ae649a596123 | 44 | if (file->close()) |
neilt6 | 10:ae649a596123 | 45 | printf("failed to close file!\n"); |
neilt6 | 10:ae649a596123 | 46 | else { |
neilt6 | 10:ae649a596123 | 47 | if (sd.remove("Test File.bin")) |
neilt6 | 10:ae649a596123 | 48 | printf("failed to delete file!\n"); |
neilt6 | 10:ae649a596123 | 49 | else |
neilt6 | 10:ae649a596123 | 50 | printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); |
neilt6 | 10:ae649a596123 | 51 | } |
neilt6 | 10:ae649a596123 | 52 | timer.reset(); |
neilt6 | 10:ae649a596123 | 53 | } else { |
neilt6 | 10:ae649a596123 | 54 | printf("failed to open file!\n"); |
neilt6 | 10:ae649a596123 | 55 | } |
neilt6 | 10:ae649a596123 | 56 | } |
neilt6 | 0:8f2b6eed2a9d | 57 | |
neilt6 | 0:8f2b6eed2a9d | 58 | int main() |
neilt6 | 0:8f2b6eed2a9d | 59 | { |
neilt6 | 10:ae649a596123 | 60 | //Configure CRC and large frames |
neilt6 | 10:ae649a596123 | 61 | sd.crc(true); |
neilt6 | 7:17ca3091939f | 62 | sd.large_frames(true); |
neilt6 | 7:17ca3091939f | 63 | |
neilt6 | 10:ae649a596123 | 64 | //Fill the buffer with random data for the write test |
neilt6 | 10:ae649a596123 | 65 | srand(time(NULL)); |
neilt6 | 10:ae649a596123 | 66 | for (int i = 0; i < sizeof(buffer); i++) |
neilt6 | 10:ae649a596123 | 67 | buffer[i] = rand(); |
neilt6 | 10:ae649a596123 | 68 | |
neilt6 | 0:8f2b6eed2a9d | 69 | while(1) { |
neilt6 | 0:8f2b6eed2a9d | 70 | //Print the start message |
neilt6 | 0:8f2b6eed2a9d | 71 | printf("\nPress the button to perform tests: "); |
neilt6 | 0:8f2b6eed2a9d | 72 | |
neilt6 | 0:8f2b6eed2a9d | 73 | //Wait for the button to be pressed |
neilt6 | 0:8f2b6eed2a9d | 74 | while(button); |
neilt6 | 0:8f2b6eed2a9d | 75 | |
neilt6 | 0:8f2b6eed2a9d | 76 | //Display the card type and capacity |
neilt6 | 0:8f2b6eed2a9d | 77 | printf("\nCard type: "); |
neilt6 | 0:8f2b6eed2a9d | 78 | if (sd.card_type() == SDFileSystem::CARD_NONE) |
neilt6 | 0:8f2b6eed2a9d | 79 | printf("None\n"); |
neilt6 | 0:8f2b6eed2a9d | 80 | else if (sd.card_type() == SDFileSystem::CARD_MMC) |
neilt6 | 0:8f2b6eed2a9d | 81 | printf("MMC\n"); |
neilt6 | 0:8f2b6eed2a9d | 82 | else if (sd.card_type() == SDFileSystem::CARD_SD) |
neilt6 | 0:8f2b6eed2a9d | 83 | printf("SD\n"); |
neilt6 | 0:8f2b6eed2a9d | 84 | else if (sd.card_type() == SDFileSystem::CARD_SDHC) |
neilt6 | 0:8f2b6eed2a9d | 85 | printf("SDHC\n"); |
neilt6 | 0:8f2b6eed2a9d | 86 | else |
neilt6 | 0:8f2b6eed2a9d | 87 | printf("Unknown\n"); |
neilt6 | 0:8f2b6eed2a9d | 88 | printf("Sectors: %llu\n", sd.disk_sectors()); |
neilt6 | 0:8f2b6eed2a9d | 89 | printf("Capacity: %.1fMB\n", (sd.disk_sectors() * 512) / 1048576.0); |
neilt6 | 0:8f2b6eed2a9d | 90 | |
neilt6 | 10:ae649a596123 | 91 | //Mount the filesystem |
neilt6 | 10:ae649a596123 | 92 | sd.mount(); |
neilt6 | 10:ae649a596123 | 93 | |
neilt6 | 10:ae649a596123 | 94 | /*//Format the card |
neilt6 | 10:ae649a596123 | 95 | printf("Formatting card..."); |
neilt6 | 0:8f2b6eed2a9d | 96 | if (sd.format() == 0) |
neilt6 | 0:8f2b6eed2a9d | 97 | printf("success!\n"); |
neilt6 | 0:8f2b6eed2a9d | 98 | else |
neilt6 | 0:8f2b6eed2a9d | 99 | printf("failed!\n");*/ |
neilt6 | 0:8f2b6eed2a9d | 100 | |
neilt6 | 10:ae649a596123 | 101 | //Perform a read/write tests |
neilt6 | 10:ae649a596123 | 102 | writeTest(); |
neilt6 | 10:ae649a596123 | 103 | readTest(); |
neilt6 | 0:8f2b6eed2a9d | 104 | |
neilt6 | 10:ae649a596123 | 105 | //Unmount the filesystem |
neilt6 | 10:ae649a596123 | 106 | sd.unmount(); |
neilt6 | 0:8f2b6eed2a9d | 107 | } |
neilt6 | 0:8f2b6eed2a9d | 108 | } |