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@57:79fed71031da, 2018-03-31 (annotated)
- Committer:
- cittecla
- Date:
- Sat Mar 31 21:45:08 2018 +0000
- Revision:
- 57:79fed71031da
- Parent:
- 56:218601547d13
- Child:
- 58:cda5298c9b7f
Implemented checksum function to verify medication read.; bugfixes
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| cittecla | 51:a98ffbd41e76 | 1 | #include "sdcard.h" | 
| cittecla | 53:1c61cadbcb35 | 2 | #include <stdio.h> | 
| cittecla | 53:1c61cadbcb35 | 3 | #include <string.h> | 
| cittecla | 51:a98ffbd41e76 | 4 | |
| cittecla | 51:a98ffbd41e76 | 5 | Timer timer; | 
| cittecla | 51:a98ffbd41e76 | 6 | |
| cittecla | 51:a98ffbd41e76 | 7 | DigitalIn button(PC_13, PullUp); | 
| cittecla | 51:a98ffbd41e76 | 8 | |
| cittecla | 51:a98ffbd41e76 | 9 | SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000); | 
| cittecla | 51:a98ffbd41e76 | 10 | char buffer[4096]; | 
| cittecla | 51:a98ffbd41e76 | 11 | |
| cittecla | 51:a98ffbd41e76 | 12 | void writeTest() | 
| cittecla | 51:a98ffbd41e76 | 13 | { | 
| cittecla | 51:a98ffbd41e76 | 14 | //Test write performance by creating a 1MB file | 
| cittecla | 53:1c61cadbcb35 | 15 | printf("Testing %iB write performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 16 | FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); | 
| cittecla | 51:a98ffbd41e76 | 17 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 18 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 19 | for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { | 
| cittecla | 51:a98ffbd41e76 | 20 | if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { | 
| cittecla | 51:a98ffbd41e76 | 21 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 22 | printf("write error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 23 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 24 | return; | 
| cittecla | 51:a98ffbd41e76 | 25 | } | 
| cittecla | 51:a98ffbd41e76 | 26 | } | 
| cittecla | 51:a98ffbd41e76 | 27 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 28 | if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 29 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 30 | else | 
| cittecla | 51:a98ffbd41e76 | 31 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 32 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 33 | } else { | 
| cittecla | 51:a98ffbd41e76 | 34 | printf("failed to create file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 35 | } | 
| cittecla | 51:a98ffbd41e76 | 36 | } | 
| cittecla | 51:a98ffbd41e76 | 37 | |
| cittecla | 51:a98ffbd41e76 | 38 | void readTest() | 
| cittecla | 51:a98ffbd41e76 | 39 | { | 
| cittecla | 51:a98ffbd41e76 | 40 | //Test read performance by reading the 1MB file created by writeTest() | 
| cittecla | 51:a98ffbd41e76 | 41 | printf("Testing %iB read performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 42 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); | 
| cittecla | 51:a98ffbd41e76 | 43 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 44 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 45 | int iterations = 0; | 
| cittecla | 51:a98ffbd41e76 | 46 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) | 
| cittecla | 51:a98ffbd41e76 | 47 | iterations++; | 
| cittecla | 51:a98ffbd41e76 | 48 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 49 | if (iterations != (1048576 / sizeof(buffer))) | 
| cittecla | 51:a98ffbd41e76 | 50 | printf("read error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 51 | else if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 52 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 53 | else if (sd.remove("Test File.bin")) | 
| cittecla | 51:a98ffbd41e76 | 54 | printf("failed to delete file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 55 | else | 
| cittecla | 51:a98ffbd41e76 | 56 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 57 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 58 | } else { | 
| cittecla | 51:a98ffbd41e76 | 59 | printf("failed to open file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 60 | } | 
| cittecla | 51:a98ffbd41e76 | 61 | } | 
| cittecla | 51:a98ffbd41e76 | 62 | |
| cittecla | 51:a98ffbd41e76 | 63 | void testSd() | 
| cittecla | 51:a98ffbd41e76 | 64 | { | 
| cittecla | 51:a98ffbd41e76 | 65 | //Configure CRC, large frames, and write validation | 
| cittecla | 51:a98ffbd41e76 | 66 | sd.crc(true); | 
| cittecla | 51:a98ffbd41e76 | 67 | sd.large_frames(true); | 
| cittecla | 51:a98ffbd41e76 | 68 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 69 | |
| cittecla | 51:a98ffbd41e76 | 70 | //Fill the buffer with random data for the write test | 
| cittecla | 51:a98ffbd41e76 | 71 | srand(time(NULL)); | 
| cittecla | 51:a98ffbd41e76 | 72 | for (int i = 0; i < sizeof(buffer); i++) | 
| cittecla | 51:a98ffbd41e76 | 73 | buffer[i] = rand(); | 
| cittecla | 51:a98ffbd41e76 | 74 | |
| cittecla | 51:a98ffbd41e76 | 75 | |
| cittecla | 53:1c61cadbcb35 | 76 | //Print the start message | 
| cittecla | 53:1c61cadbcb35 | 77 | printf("\n\rStarting SD Card test application:"); | 
| cittecla | 51:a98ffbd41e76 | 78 | |
| cittecla | 51:a98ffbd41e76 | 79 | |
| cittecla | 53:1c61cadbcb35 | 80 | //Make sure a card is present | 
| cittecla | 53:1c61cadbcb35 | 81 | if (!sd.card_present()) { | 
| cittecla | 53:1c61cadbcb35 | 82 | printf("\n\rNo card present!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 83 | } | 
| cittecla | 51:a98ffbd41e76 | 84 | |
| cittecla | 53:1c61cadbcb35 | 85 | //Try to mount the SD card | 
| cittecla | 53:1c61cadbcb35 | 86 | printf("\n\rMounting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 87 | if (sd.mount() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 88 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 89 | } | 
| cittecla | 53:1c61cadbcb35 | 90 | printf("success!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 91 | |
| cittecla | 53:1c61cadbcb35 | 92 | //Display the card type | 
| cittecla | 53:1c61cadbcb35 | 93 | printf("\tCard type: "); | 
| cittecla | 53:1c61cadbcb35 | 94 | SDFileSystem::CardType cardType = sd.card_type(); | 
| cittecla | 53:1c61cadbcb35 | 95 | if (cardType == SDFileSystem::CARD_NONE) | 
| cittecla | 53:1c61cadbcb35 | 96 | printf("None\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 97 | else if (cardType == SDFileSystem::CARD_MMC) | 
| cittecla | 53:1c61cadbcb35 | 98 | printf("MMC\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 99 | else if (cardType == SDFileSystem::CARD_SD) | 
| cittecla | 53:1c61cadbcb35 | 100 | printf("SD\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 101 | else if (cardType == SDFileSystem::CARD_SDHC) | 
| cittecla | 53:1c61cadbcb35 | 102 | printf("SDHC\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 103 | else | 
| cittecla | 53:1c61cadbcb35 | 104 | printf("Unknown\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 105 | |
| cittecla | 53:1c61cadbcb35 | 106 | //Display the card capacity | 
| cittecla | 53:1c61cadbcb35 | 107 | printf("\tSectors: %u\n\r", sd.disk_sectors()); | 
| cittecla | 53:1c61cadbcb35 | 108 | printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0); | 
| cittecla | 51:a98ffbd41e76 | 109 | |
| cittecla | 53:1c61cadbcb35 | 110 | /*//Format the card | 
| cittecla | 53:1c61cadbcb35 | 111 | printf("Formatting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 112 | if (sd.format() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 113 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 114 | continue; | 
| cittecla | 53:1c61cadbcb35 | 115 | } | 
| cittecla | 53:1c61cadbcb35 | 116 | printf("success!\n\r");*/ | 
| cittecla | 51:a98ffbd41e76 | 117 | |
| cittecla | 53:1c61cadbcb35 | 118 | //Perform a read/write test | 
| cittecla | 53:1c61cadbcb35 | 119 | writeTest(); | 
| cittecla | 53:1c61cadbcb35 | 120 | readTest(); | 
| cittecla | 51:a98ffbd41e76 | 121 | |
| cittecla | 51:a98ffbd41e76 | 122 | |
| cittecla | 51:a98ffbd41e76 | 123 | |
| cittecla | 53:1c61cadbcb35 | 124 | printf("write to SD card on a txt file:\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 125 | printf("\tHello World!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 126 | |
| cittecla | 53:1c61cadbcb35 | 127 | FILE *fp = fopen("/sd/medication/sdtest.txt", "a"); | 
| cittecla | 53:1c61cadbcb35 | 128 | if(fp == NULL) { | 
| cittecla | 53:1c61cadbcb35 | 129 | printf("\tCould not open file for write\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 130 | } else { | 
| cittecla | 53:1c61cadbcb35 | 131 | printf("\tfile opened\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 132 | } | 
| cittecla | 53:1c61cadbcb35 | 133 | |
| cittecla | 53:1c61cadbcb35 | 134 | fprintf(fp, "Hello fun SD Card World!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 135 | fclose(fp); | 
| cittecla | 53:1c61cadbcb35 | 136 | |
| cittecla | 53:1c61cadbcb35 | 137 | printf("\tText written to SD card\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 138 | printf("\tGoodbye World!\r\n"); | 
| cittecla | 53:1c61cadbcb35 | 139 | |
| cittecla | 53:1c61cadbcb35 | 140 | //Unmount the SD card | 
| cittecla | 53:1c61cadbcb35 | 141 | sd.unmount(); | 
| cittecla | 53:1c61cadbcb35 | 142 | } | 
| cittecla | 53:1c61cadbcb35 | 143 | |
| cittecla | 53:1c61cadbcb35 | 144 | s_user readMedication(int user) | 
| cittecla | 53:1c61cadbcb35 | 145 | { | 
| cittecla | 53:1c61cadbcb35 | 146 | s_user userfile; | 
| cittecla | 53:1c61cadbcb35 | 147 | userfile.valid = false; | 
| cittecla | 53:1c61cadbcb35 | 148 | char filepath[] = "/sd/medication/medicationUser2.txt"; | 
| cittecla | 53:1c61cadbcb35 | 149 | if(user==0) { | 
| cittecla | 53:1c61cadbcb35 | 150 | strcpy(filepath, "/sd/medication/medicationUser1.txt"); | 
| cittecla | 53:1c61cadbcb35 | 151 | } | 
| cittecla | 53:1c61cadbcb35 | 152 | |
| cittecla | 53:1c61cadbcb35 | 153 | //Configure CRC, large frames, and write validation | 
| cittecla | 53:1c61cadbcb35 | 154 | sd.crc(true); | 
| cittecla | 53:1c61cadbcb35 | 155 | sd.large_frames(true); | 
| cittecla | 53:1c61cadbcb35 | 156 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 157 | |
| cittecla | 53:1c61cadbcb35 | 158 | //mount SD card | 
| cittecla | 53:1c61cadbcb35 | 159 | printf("\n\rMounting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 160 | if (sd.mount() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 161 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 162 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 163 | } | 
| cittecla | 53:1c61cadbcb35 | 164 | printf("success!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 165 | |
| cittecla | 53:1c61cadbcb35 | 166 | //open file for read | 
| cittecla | 53:1c61cadbcb35 | 167 | printf("open file %s...",filepath); | 
| cittecla | 53:1c61cadbcb35 | 168 | FILE *fp = fopen(filepath, "r"); | 
| cittecla | 53:1c61cadbcb35 | 169 | if(fp == NULL) { | 
| cittecla | 53:1c61cadbcb35 | 170 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 171 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 172 | } | 
| cittecla | 53:1c61cadbcb35 | 173 | printf("success!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 174 | |
| cittecla | 53:1c61cadbcb35 | 175 | char string[STR_LEN]; | 
| cittecla | 53:1c61cadbcb35 | 176 | int linecounter = 1; | 
| cittecla | 57:79fed71031da | 177 | int checksum = 0; | 
| cittecla | 57:79fed71031da | 178 | int checksum2 = 0; | 
| cittecla | 53:1c61cadbcb35 | 179 | while (fgets(string, STR_LEN, fp) != NULL) { | 
| cittecla | 53:1c61cadbcb35 | 180 | switch (linecounter) { | 
| cittecla | 53:1c61cadbcb35 | 181 | case 1: | 
| cittecla | 53:1c61cadbcb35 | 182 | printf("Reading Line 1..."); | 
| cittecla | 53:1c61cadbcb35 | 183 | userfile.side = atoi(strtok(string, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 184 | strcpy(userfile.firstName, strtok(NULL, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 185 | strcpy(userfile.secondName, strtok(NULL, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 186 | printf("done!\r\n"); | 
| cittecla | 53:1c61cadbcb35 | 187 | 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); | 
| cittecla | 53:1c61cadbcb35 | 188 | break; | 
| cittecla | 57:79fed71031da | 189 | case 30: | 
| cittecla | 57:79fed71031da | 190 | printf("reading checksum..."); | 
| cittecla | 57:79fed71031da | 191 | checksum2 = atoi(strtok(string, TOKEN)); | 
| cittecla | 57:79fed71031da | 192 | printf("done!\r\n"); | 
| cittecla | 57:79fed71031da | 193 | if(checksum == checksum2) userfile.valid = true; | 
| cittecla | 57:79fed71031da | 194 | break; | 
| cittecla | 55:bdab541f434d | 195 | default: | 
| cittecla | 55:bdab541f434d | 196 | printf("reading medication %d...",(linecounter-1)); | 
| cittecla | 56:218601547d13 | 197 | int daycounter = (linecounter-2)/4; | 
| cittecla | 56:218601547d13 | 198 | int momentcounter = (linecounter-2)%4; | 
| cittecla | 57:79fed71031da | 199 | |
| cittecla | 57:79fed71031da | 200 | userfile.medication.day[daycounter].moment[momentcounter].time.hour = atoi(strtok(string, TOKEN)); | 
| cittecla | 57:79fed71031da | 201 | userfile.medication.day[daycounter].moment[momentcounter].time.minute = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 57:79fed71031da | 202 | userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 57:79fed71031da | 203 | userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 57:79fed71031da | 204 | |
| cittecla | 57:79fed71031da | 205 | checksum += userfile.medication.day[daycounter].moment[momentcounter].time.hour; | 
| cittecla | 57:79fed71031da | 206 | checksum += userfile.medication.day[daycounter].moment[momentcounter].time.minute; | 
| cittecla | 57:79fed71031da | 207 | checksum += userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus; | 
| cittecla | 57:79fed71031da | 208 | checksum += userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus; | 
| cittecla | 57:79fed71031da | 209 | |
| cittecla | 56:218601547d13 | 210 | int medication = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 55:bdab541f434d | 211 | |
| cittecla | 56:218601547d13 | 212 | for( int ii = 0; ii<6; ii++) { | 
| cittecla | 56:218601547d13 | 213 | userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[5-ii] = medication % 10; | 
| cittecla | 56:218601547d13 | 214 | medication = medication/10; | 
| cittecla | 57:79fed71031da | 215 | checksum += userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[5-ii]; | 
| cittecla | 55:bdab541f434d | 216 | } | 
| cittecla | 53:1c61cadbcb35 | 217 | printf("done!\r\n"); | 
| cittecla | 53:1c61cadbcb35 | 218 | break; | 
| cittecla | 51:a98ffbd41e76 | 219 | } | 
| cittecla | 53:1c61cadbcb35 | 220 | linecounter++; | 
| cittecla | 53:1c61cadbcb35 | 221 | } | 
| cittecla | 55:bdab541f434d | 222 | |
| cittecla | 55:bdab541f434d | 223 | //Unmount the SD card | 
| cittecla | 55:bdab541f434d | 224 | sd.unmount(); | 
| cittecla | 55:bdab541f434d | 225 | |
| cittecla | 53:1c61cadbcb35 | 226 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 227 | } | 
