Reading Analog Ports and Saving in a micro SD CARD with the KL25z
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
Diff: main.cpp
- Revision:
- 10:ae649a596123
- Parent:
- 7:17ca3091939f
- Child:
- 11:2be49b81dc0b
--- a/main.cpp Tue Aug 12 14:59:15 2014 +0000 +++ b/main.cpp Thu Aug 14 22:28:28 2014 +0000 @@ -1,14 +1,71 @@ #include "mbed.h" #include "SDFileSystem.h" +Timer timer; DigitalIn button(p21, PullUp); -SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NO, 20000000); +SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NO, 25000000); +char buffer[4096]; + +void writeTest() +{ + //Test write performance by creating a 1MB file + printf("Testing %iB write performance...", sizeof(buffer)); + FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); + if (file != NULL) { + timer.start(); + for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { + if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { + timer.stop(); + printf("write error!\n"); + timer.reset(); + return; + } + } + timer.stop(); + if (file->close()) + printf("failed to close file!\n"); + else + printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); + timer.reset(); + } else { + printf("failed to create file!\n"); + } +} + +void readTest() +{ + //Test read performance by reading the 1MB file created by writeTest() + printf("Testing %iB read performance...", sizeof(buffer)); + FileHandle* file = sd.open("Test File.bin", O_RDONLY); + if (file != NULL) { + timer.start(); + while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)); + timer.stop(); + if (file->close()) + printf("failed to close file!\n"); + else { + if (sd.remove("Test File.bin")) + printf("failed to delete file!\n"); + else + printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0)); + } + timer.reset(); + } else { + printf("failed to open file!\n"); + } +} int main() { - //Enable large frames for performance + //Configure CRC and large frames + sd.crc(true); sd.large_frames(true); + //Fill the buffer with random data for the write test + srand(time(NULL)); + for (int i = 0; i < sizeof(buffer); i++) + buffer[i] = rand(); + while(1) { //Print the start message printf("\nPress the button to perform tests: "); @@ -31,39 +88,21 @@ printf("Sectors: %llu\n", sd.disk_sectors()); printf("Capacity: %.1fMB\n", (sd.disk_sectors() * 512) / 1048576.0); - //Format the card - /*printf("Formatting card..."); + //Mount the filesystem + sd.mount(); + + /*//Format the card + printf("Formatting card..."); if (sd.format() == 0) printf("success!\n"); else printf("failed!\n");*/ - //Perform a write test - printf("Writing to card..."); - FILE *fp = fopen("/sd/sdtest.txt", "w"); - if (fp != NULL) { - fprintf(fp, "We're writing to an SD card!"); - fclose(fp); - printf("success!\n"); - } else { - printf("failed!\n"); - } + //Perform a read/write tests + writeTest(); + readTest(); - //Perform a read test - printf("Reading from card..."); - fp = fopen("/sd/sdtest.txt", "r"); - if (fp != NULL) { - char c = fgetc(fp); - if (c == 'W') - printf("success!\n"); - else - printf("incorrect char (%c)!\n", c); - fclose(fp); - } else { - printf("failed!\n"); - } - - //Delay for 0.2 seconds for simple debouncing - wait(0.2); + //Unmount the filesystem + sd.unmount(); } }