Reading Analog Ports and Saving in a micro SD CARD with the KL25z
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@17:e5b7469082c8, 2015-08-27 (annotated)
- Committer:
- neilt6
- Date:
- Thu Aug 27 21:51:58 2015 +0000
- Revision:
- 17:e5b7469082c8
- Parent:
- 16:d68a6d4050f2
- Child:
- 19:ae979143c796
Updated libraries and implemented minor improvements
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 | 17:e5b7469082c8 | 82 | //Display the card type |
neilt6 | 0:8f2b6eed2a9d | 83 | printf("\nCard type: "); |
neilt6 | 17:e5b7469082c8 | 84 | SDFileSystem::CardType cardType = sd.card_type(); |
neilt6 | 17:e5b7469082c8 | 85 | if (cardType == SDFileSystem::CARD_NONE) |
neilt6 | 0:8f2b6eed2a9d | 86 | printf("None\n"); |
neilt6 | 17:e5b7469082c8 | 87 | else if (cardType == SDFileSystem::CARD_MMC) |
neilt6 | 0:8f2b6eed2a9d | 88 | printf("MMC\n"); |
neilt6 | 17:e5b7469082c8 | 89 | else if (cardType == SDFileSystem::CARD_SD) |
neilt6 | 0:8f2b6eed2a9d | 90 | printf("SD\n"); |
neilt6 | 17:e5b7469082c8 | 91 | else if (cardType == SDFileSystem::CARD_SDHC) |
neilt6 | 0:8f2b6eed2a9d | 92 | printf("SDHC\n"); |
neilt6 | 0:8f2b6eed2a9d | 93 | else |
neilt6 | 0:8f2b6eed2a9d | 94 | printf("Unknown\n"); |
neilt6 | 17:e5b7469082c8 | 95 | |
neilt6 | 17:e5b7469082c8 | 96 | //Try to mount the SD card |
neilt6 | 17:e5b7469082c8 | 97 | printf("Mounting SD card..."); |
neilt6 | 17:e5b7469082c8 | 98 | if (sd.mount() != 0) { |
neilt6 | 17:e5b7469082c8 | 99 | printf("failed!\n"); |
neilt6 | 17:e5b7469082c8 | 100 | continue; |
neilt6 | 17:e5b7469082c8 | 101 | } |
neilt6 | 17:e5b7469082c8 | 102 | printf("success!\n"); |
neilt6 | 17:e5b7469082c8 | 103 | |
neilt6 | 17:e5b7469082c8 | 104 | //Display the card capacity |
neilt6 | 0:8f2b6eed2a9d | 105 | printf("Sectors: %llu\n", sd.disk_sectors()); |
neilt6 | 16:d68a6d4050f2 | 106 | printf("Capacity: %.1fMB\n", sd.disk_sectors() / 2048.0); |
neilt6 | 0:8f2b6eed2a9d | 107 | |
neilt6 | 17:e5b7469082c8 | 108 | /*//Format the card |
neilt6 | 17:e5b7469082c8 | 109 | printf("Formatting SD card..."); |
neilt6 | 17:e5b7469082c8 | 110 | if (sd.format() != 0) { |
neilt6 | 17:e5b7469082c8 | 111 | printf("failed!\n"); |
neilt6 | 17:e5b7469082c8 | 112 | continue; |
neilt6 | 17:e5b7469082c8 | 113 | } |
neilt6 | 17:e5b7469082c8 | 114 | printf("success!\n");*/ |
neilt6 | 10:ae649a596123 | 115 | |
neilt6 | 17:e5b7469082c8 | 116 | //Perform a read/write test |
neilt6 | 10:ae649a596123 | 117 | writeTest(); |
neilt6 | 10:ae649a596123 | 118 | readTest(); |
neilt6 | 0:8f2b6eed2a9d | 119 | |
neilt6 | 17:e5b7469082c8 | 120 | //Unmount the SD card |
neilt6 | 10:ae649a596123 | 121 | sd.unmount(); |
neilt6 | 0:8f2b6eed2a9d | 122 | } |
neilt6 | 0:8f2b6eed2a9d | 123 | } |