Reading Analog Ports and Saving in a micro SD CARD with the KL25z
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@16:d68a6d4050f2, 2015-07-08 (annotated)
- Committer:
- neilt6
- Date:
- Wed Jul 08 16:47:02 2015 +0000
- Revision:
- 16:d68a6d4050f2
- Parent:
- 12:bd8b17cd6a7b
- Child:
- 17:e5b7469082c8
Updated libraries, and simplified card capacity math
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 | 0:8f2b6eed2a9d | 73 | //Print the start message |
neilt6 | 0:8f2b6eed2a9d | 74 | printf("\nPress the button to perform tests: "); |
neilt6 | 0:8f2b6eed2a9d | 75 | |
neilt6 | 0:8f2b6eed2a9d | 76 | //Wait for the button to be pressed |
neilt6 | 0:8f2b6eed2a9d | 77 | while(button); |
neilt6 | 0:8f2b6eed2a9d | 78 | |
neilt6 | 0:8f2b6eed2a9d | 79 | //Display the card type and capacity |
neilt6 | 0:8f2b6eed2a9d | 80 | printf("\nCard type: "); |
neilt6 | 0:8f2b6eed2a9d | 81 | if (sd.card_type() == SDFileSystem::CARD_NONE) |
neilt6 | 0:8f2b6eed2a9d | 82 | printf("None\n"); |
neilt6 | 0:8f2b6eed2a9d | 83 | else if (sd.card_type() == SDFileSystem::CARD_MMC) |
neilt6 | 0:8f2b6eed2a9d | 84 | printf("MMC\n"); |
neilt6 | 0:8f2b6eed2a9d | 85 | else if (sd.card_type() == SDFileSystem::CARD_SD) |
neilt6 | 0:8f2b6eed2a9d | 86 | printf("SD\n"); |
neilt6 | 0:8f2b6eed2a9d | 87 | else if (sd.card_type() == SDFileSystem::CARD_SDHC) |
neilt6 | 0:8f2b6eed2a9d | 88 | printf("SDHC\n"); |
neilt6 | 0:8f2b6eed2a9d | 89 | else |
neilt6 | 0:8f2b6eed2a9d | 90 | printf("Unknown\n"); |
neilt6 | 0:8f2b6eed2a9d | 91 | printf("Sectors: %llu\n", sd.disk_sectors()); |
neilt6 | 16:d68a6d4050f2 | 92 | printf("Capacity: %.1fMB\n", sd.disk_sectors() / 2048.0); |
neilt6 | 0:8f2b6eed2a9d | 93 | |
neilt6 | 10:ae649a596123 | 94 | //Mount the filesystem |
neilt6 | 10:ae649a596123 | 95 | sd.mount(); |
neilt6 | 10:ae649a596123 | 96 | |
neilt6 | 10:ae649a596123 | 97 | /*//Format the card |
neilt6 | 10:ae649a596123 | 98 | printf("Formatting card..."); |
neilt6 | 0:8f2b6eed2a9d | 99 | if (sd.format() == 0) |
neilt6 | 0:8f2b6eed2a9d | 100 | printf("success!\n"); |
neilt6 | 0:8f2b6eed2a9d | 101 | else |
neilt6 | 0:8f2b6eed2a9d | 102 | printf("failed!\n");*/ |
neilt6 | 0:8f2b6eed2a9d | 103 | |
neilt6 | 10:ae649a596123 | 104 | //Perform a read/write tests |
neilt6 | 10:ae649a596123 | 105 | writeTest(); |
neilt6 | 10:ae649a596123 | 106 | readTest(); |
neilt6 | 0:8f2b6eed2a9d | 107 | |
neilt6 | 10:ae649a596123 | 108 | //Unmount the filesystem |
neilt6 | 10:ae649a596123 | 109 | sd.unmount(); |
neilt6 | 0:8f2b6eed2a9d | 110 | } |
neilt6 | 0:8f2b6eed2a9d | 111 | } |