Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
Fork of PES4_Programme by
Diff: source/sdcard.cpp
- Revision:
- 51:a98ffbd41e76
- Child:
- 52:701d0c2f47d7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/sdcard.cpp Thu Mar 29 18:17:38 2018 +0000
@@ -0,0 +1,141 @@
+#include "sdcard.h"
+
+
+Timer timer;
+
+DigitalIn button(PC_13, PullUp);
+
+SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000);
+char buffer[4096];
+
+void writeTest()
+{
+ //Test write performance by creating a 1MB file
+ printf("Testing %iB write performance...\n\r", 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\r");
+ timer.reset();
+ return;
+ }
+ }
+ timer.stop();
+ if (file->close())
+ printf("failed to close file!\n\r");
+ else
+ printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0));
+ timer.reset();
+ } else {
+ printf("failed to create file!\n\r");
+ }
+}
+
+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();
+ int iterations = 0;
+ while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
+ iterations++;
+ timer.stop();
+ if (iterations != (1048576 / sizeof(buffer)))
+ printf("read error!\n\r");
+ else if (file->close())
+ printf("failed to close file!\n\r");
+ else if (sd.remove("Test File.bin"))
+ printf("failed to delete file!\n\r");
+ else
+ printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0));
+ timer.reset();
+ } else {
+ printf("failed to open file!\n\r");
+ }
+}
+
+void testSd()
+{
+ //Configure CRC, large frames, and write validation
+ sd.crc(true);
+ sd.large_frames(true);
+ sd.write_validation(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();
+
+
+ //Print the start message
+ printf("\n\rPress the button to perform tests: ");
+
+
+ //Make sure a card is present
+ if (!sd.card_present()) {
+ printf("\n\rNo card present!\n\r");
+ }
+
+ //Try to mount the SD card
+ printf("\n\rMounting SD card...");
+ if (sd.mount() != 0) {
+ printf("failed!\n\r");
+ }
+ printf("success!\n\r");
+
+ //Display the card type
+ printf("\tCard type: ");
+ SDFileSystem::CardType cardType = sd.card_type();
+ if (cardType == SDFileSystem::CARD_NONE)
+ printf("None\n\r");
+ else if (cardType == SDFileSystem::CARD_MMC)
+ printf("MMC\n\r");
+ else if (cardType == SDFileSystem::CARD_SD)
+ printf("SD\n\r");
+ else if (cardType == SDFileSystem::CARD_SDHC)
+ printf("SDHC\n\r");
+ else
+ printf("Unknown\n\r");
+
+ //Display the card capacity
+ printf("\tSectors: %u\n\r", sd.disk_sectors());
+ printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0);
+
+ /*//Format the card
+ printf("Formatting SD card...");
+ if (sd.format() != 0) {
+ printf("failed!\n\r");
+ continue;
+ }
+ printf("success!\n\r");*/
+
+ //Perform a read/write test
+ writeTest();
+ readTest();
+
+
+
+ printf("write to SD card on a txt file:\n\r");
+ printf("\tHello World!\n\r");
+
+ FILE *fp = fopen("/sd/medication/sdtest.txt", "a");
+ if(fp == NULL) {
+ printf("\tCould not open file for write\n\r");
+ } else {
+ printf("\tfile opened\n\r");
+ }
+
+ fprintf(fp, "Hello fun SD Card World!\n\r");
+ fclose(fp);
+
+ printf("\tText written to SD card\n\r");
+ printf("\tGoodbye World!\n\r");
+
+ //Unmount the SD card
+ sd.unmount();
+}
