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
source/sdcard.cpp
- Committer:
- cittecla
- Date:
- 2018-03-30
- Revision:
- 53:1c61cadbcb35
- Parent:
- 52:701d0c2f47d7
- Child:
- 55:bdab541f434d
File content as of revision 53:1c61cadbcb35:
#include "sdcard.h"
#include <stdio.h>
#include <string.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...", 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\rStarting SD Card test application:");
//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!\r\n");
//Unmount the SD card
sd.unmount();
}
s_user readMedication(int user)
{
s_user userfile;
userfile.valid = false;
char filepath[] = "/sd/medication/medicationUser2.txt";
if(user==0) {
strcpy(filepath, "/sd/medication/medicationUser1.txt");
}
//Configure CRC, large frames, and write validation
sd.crc(true);
sd.large_frames(true);
sd.write_validation(true);
//mount SD card
printf("\n\rMounting SD card...");
if (sd.mount() != 0) {
printf("failed!\n\r");
return userfile;
}
printf("success!\n\r");
//open file for read
printf("open file %s...",filepath);
FILE *fp = fopen(filepath, "r");
if(fp == NULL) {
printf("failed!\n\r");
return userfile;
}
printf("success!\n\r");
char string[STR_LEN];
int linecounter = 1;
while (fgets(string, STR_LEN, fp) != NULL) {
switch (linecounter) {
case 1:
printf("Reading Line 1...");
userfile.side = atoi(strtok(string, TOKEN));
strcpy(userfile.firstName, strtok(NULL, TOKEN));
strcpy(userfile.secondName, strtok(NULL, TOKEN));
printf("done!\r\n");
printf("\tside:\t\t%d \n\r\tFirst Name:\t%s \n\r\tLast Name:\t%s \n\r",userfile.side, userfile.firstName,userfile.secondName);
break;
case 2:
printf("Reading Line 2...");
int mo = atoi(strtok(string, TOKEN));
int lu = atoi(strtok(NULL, TOKEN));
int di = atoi(strtok(NULL, TOKEN));
int ev = atoi(strtok(NULL, TOKEN));
printf("done!\r\n");
printf("\tMedication for Monday:\r\n\tmorning:\t%06d\r\n\tlunch:\t\t%06d\r\n\tdinner:\t\t%06d\r\n\tevening:\t%06d\r\n",mo,lu,di,ev);
break;
default:
break;
}
linecounter++;
}
userfile.valid = true;
return userfile;
}
