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@77:c2e22d1e5d44, 2018-04-07 (annotated)
- Committer:
- itslinear
- Date:
- Sat Apr 07 17:43:22 2018 +0000
- Revision:
- 77:c2e22d1e5d44
- Parent:
- 66:0d43bd7ed179
- Child:
- 80:6e3eb8246ced
write_medProtocol; write_medInventory; write_medError; erase_medError; erase_medProtocol; are working now
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 | 58:cda5298c9b7f | 4 | #include <math.h> | 
| cittecla | 51:a98ffbd41e76 | 5 | |
| cittecla | 51:a98ffbd41e76 | 6 | Timer timer; | 
| cittecla | 51:a98ffbd41e76 | 7 | |
| cittecla | 51:a98ffbd41e76 | 8 | DigitalIn button(PC_13, PullUp); | 
| cittecla | 51:a98ffbd41e76 | 9 | |
| cittecla | 51:a98ffbd41e76 | 10 | SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000); | 
| cittecla | 51:a98ffbd41e76 | 11 | char buffer[4096]; | 
| cittecla | 51:a98ffbd41e76 | 12 | |
| cittecla | 51:a98ffbd41e76 | 13 | void writeTest() | 
| cittecla | 51:a98ffbd41e76 | 14 | { | 
| cittecla | 51:a98ffbd41e76 | 15 | //Test write performance by creating a 1MB file | 
| cittecla | 53:1c61cadbcb35 | 16 | printf("Testing %iB write performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 17 | FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); | 
| cittecla | 51:a98ffbd41e76 | 18 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 19 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 20 | for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { | 
| cittecla | 51:a98ffbd41e76 | 21 | if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { | 
| cittecla | 51:a98ffbd41e76 | 22 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 23 | printf("write error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 24 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 25 | return; | 
| cittecla | 51:a98ffbd41e76 | 26 | } | 
| cittecla | 51:a98ffbd41e76 | 27 | } | 
| cittecla | 51:a98ffbd41e76 | 28 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 29 | if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 30 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 31 | else | 
| cittecla | 51:a98ffbd41e76 | 32 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 33 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 34 | } else { | 
| cittecla | 51:a98ffbd41e76 | 35 | printf("failed to create file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 36 | } | 
| cittecla | 51:a98ffbd41e76 | 37 | } | 
| cittecla | 51:a98ffbd41e76 | 38 | |
| cittecla | 51:a98ffbd41e76 | 39 | void readTest() | 
| cittecla | 51:a98ffbd41e76 | 40 | { | 
| cittecla | 51:a98ffbd41e76 | 41 | //Test read performance by reading the 1MB file created by writeTest() | 
| cittecla | 51:a98ffbd41e76 | 42 | printf("Testing %iB read performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 43 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); | 
| cittecla | 51:a98ffbd41e76 | 44 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 45 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 46 | int iterations = 0; | 
| cittecla | 51:a98ffbd41e76 | 47 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) | 
| cittecla | 51:a98ffbd41e76 | 48 | iterations++; | 
| cittecla | 51:a98ffbd41e76 | 49 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 50 | if (iterations != (1048576 / sizeof(buffer))) | 
| cittecla | 51:a98ffbd41e76 | 51 | printf("read error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 52 | else if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 53 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 54 | else if (sd.remove("Test File.bin")) | 
| cittecla | 51:a98ffbd41e76 | 55 | printf("failed to delete file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 56 | else | 
| cittecla | 51:a98ffbd41e76 | 57 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 58 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 59 | } else { | 
| cittecla | 51:a98ffbd41e76 | 60 | printf("failed to open file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 61 | } | 
| cittecla | 51:a98ffbd41e76 | 62 | } | 
| cittecla | 51:a98ffbd41e76 | 63 | |
| cittecla | 51:a98ffbd41e76 | 64 | void testSd() | 
| cittecla | 51:a98ffbd41e76 | 65 | { | 
| cittecla | 51:a98ffbd41e76 | 66 | //Configure CRC, large frames, and write validation | 
| cittecla | 51:a98ffbd41e76 | 67 | sd.crc(true); | 
| cittecla | 51:a98ffbd41e76 | 68 | sd.large_frames(true); | 
| cittecla | 51:a98ffbd41e76 | 69 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 70 | |
| cittecla | 51:a98ffbd41e76 | 71 | //Fill the buffer with random data for the write test | 
| cittecla | 51:a98ffbd41e76 | 72 | srand(time(NULL)); | 
| cittecla | 51:a98ffbd41e76 | 73 | for (int i = 0; i < sizeof(buffer); i++) | 
| cittecla | 51:a98ffbd41e76 | 74 | buffer[i] = rand(); | 
| cittecla | 51:a98ffbd41e76 | 75 | |
| cittecla | 51:a98ffbd41e76 | 76 | |
| cittecla | 53:1c61cadbcb35 | 77 | //Print the start message | 
| cittecla | 53:1c61cadbcb35 | 78 | printf("\n\rStarting SD Card test application:"); | 
| cittecla | 51:a98ffbd41e76 | 79 | |
| cittecla | 51:a98ffbd41e76 | 80 | |
| cittecla | 53:1c61cadbcb35 | 81 | //Make sure a card is present | 
| cittecla | 53:1c61cadbcb35 | 82 | if (!sd.card_present()) { | 
| cittecla | 53:1c61cadbcb35 | 83 | printf("\n\rNo card present!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 84 | } | 
| cittecla | 51:a98ffbd41e76 | 85 | |
| cittecla | 53:1c61cadbcb35 | 86 | //Try to mount the SD card | 
| cittecla | 53:1c61cadbcb35 | 87 | printf("\n\rMounting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 88 | if (sd.mount() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 89 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 90 | } | 
| cittecla | 53:1c61cadbcb35 | 91 | printf("success!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 92 | |
| cittecla | 53:1c61cadbcb35 | 93 | //Display the card type | 
| cittecla | 53:1c61cadbcb35 | 94 | printf("\tCard type: "); | 
| cittecla | 53:1c61cadbcb35 | 95 | SDFileSystem::CardType cardType = sd.card_type(); | 
| cittecla | 53:1c61cadbcb35 | 96 | if (cardType == SDFileSystem::CARD_NONE) | 
| cittecla | 53:1c61cadbcb35 | 97 | printf("None\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 98 | else if (cardType == SDFileSystem::CARD_MMC) | 
| cittecla | 53:1c61cadbcb35 | 99 | printf("MMC\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 100 | else if (cardType == SDFileSystem::CARD_SD) | 
| cittecla | 53:1c61cadbcb35 | 101 | printf("SD\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 102 | else if (cardType == SDFileSystem::CARD_SDHC) | 
| cittecla | 53:1c61cadbcb35 | 103 | printf("SDHC\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 104 | else | 
| cittecla | 53:1c61cadbcb35 | 105 | printf("Unknown\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 106 | |
| cittecla | 53:1c61cadbcb35 | 107 | //Display the card capacity | 
| cittecla | 53:1c61cadbcb35 | 108 | printf("\tSectors: %u\n\r", sd.disk_sectors()); | 
| cittecla | 53:1c61cadbcb35 | 109 | printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0); | 
| cittecla | 51:a98ffbd41e76 | 110 | |
| cittecla | 53:1c61cadbcb35 | 111 | /*//Format the card | 
| cittecla | 53:1c61cadbcb35 | 112 | printf("Formatting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 113 | if (sd.format() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 114 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 115 | continue; | 
| cittecla | 53:1c61cadbcb35 | 116 | } | 
| cittecla | 53:1c61cadbcb35 | 117 | printf("success!\n\r");*/ | 
| cittecla | 51:a98ffbd41e76 | 118 | |
| cittecla | 53:1c61cadbcb35 | 119 | //Perform a read/write test | 
| cittecla | 53:1c61cadbcb35 | 120 | writeTest(); | 
| cittecla | 53:1c61cadbcb35 | 121 | readTest(); | 
| cittecla | 51:a98ffbd41e76 | 122 | |
| cittecla | 51:a98ffbd41e76 | 123 | |
| cittecla | 51:a98ffbd41e76 | 124 | |
| cittecla | 53:1c61cadbcb35 | 125 | printf("write to SD card on a txt file:\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 126 | printf("\tHello World!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 127 | |
| cittecla | 53:1c61cadbcb35 | 128 | FILE *fp = fopen("/sd/medication/sdtest.txt", "a"); | 
| cittecla | 53:1c61cadbcb35 | 129 | if(fp == NULL) { | 
| cittecla | 53:1c61cadbcb35 | 130 | printf("\tCould not open file for write\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 131 | } else { | 
| cittecla | 53:1c61cadbcb35 | 132 | printf("\tfile opened\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 133 | } | 
| cittecla | 53:1c61cadbcb35 | 134 | |
| cittecla | 53:1c61cadbcb35 | 135 | fprintf(fp, "Hello fun SD Card World!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 136 | fclose(fp); | 
| cittecla | 53:1c61cadbcb35 | 137 | |
| cittecla | 53:1c61cadbcb35 | 138 | printf("\tText written to SD card\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 139 | printf("\tGoodbye World!\r\n"); | 
| cittecla | 53:1c61cadbcb35 | 140 | |
| cittecla | 53:1c61cadbcb35 | 141 | //Unmount the SD card | 
| cittecla | 53:1c61cadbcb35 | 142 | sd.unmount(); | 
| cittecla | 53:1c61cadbcb35 | 143 | } | 
| cittecla | 53:1c61cadbcb35 | 144 | |
| cittecla | 53:1c61cadbcb35 | 145 | s_user readMedication(int user) | 
| cittecla | 53:1c61cadbcb35 | 146 | { | 
| cittecla | 53:1c61cadbcb35 | 147 | s_user userfile; | 
| cittecla | 53:1c61cadbcb35 | 148 | userfile.valid = false; | 
| cittecla | 53:1c61cadbcb35 | 149 | char filepath[] = "/sd/medication/medicationUser2.txt"; | 
| cittecla | 53:1c61cadbcb35 | 150 | if(user==0) { | 
| cittecla | 53:1c61cadbcb35 | 151 | strcpy(filepath, "/sd/medication/medicationUser1.txt"); | 
| cittecla | 53:1c61cadbcb35 | 152 | } | 
| cittecla | 53:1c61cadbcb35 | 153 | |
| cittecla | 53:1c61cadbcb35 | 154 | //Configure CRC, large frames, and write validation | 
| cittecla | 53:1c61cadbcb35 | 155 | sd.crc(true); | 
| cittecla | 53:1c61cadbcb35 | 156 | sd.large_frames(true); | 
| cittecla | 53:1c61cadbcb35 | 157 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 158 | |
| cittecla | 53:1c61cadbcb35 | 159 | //mount SD card | 
| cittecla | 53:1c61cadbcb35 | 160 | printf("\n\rMounting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 161 | if (sd.mount() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 162 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 163 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 164 | } | 
| cittecla | 53:1c61cadbcb35 | 165 | printf("success!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 166 | |
| cittecla | 53:1c61cadbcb35 | 167 | //open file for read | 
| cittecla | 53:1c61cadbcb35 | 168 | printf("open file %s...",filepath); | 
| cittecla | 53:1c61cadbcb35 | 169 | FILE *fp = fopen(filepath, "r"); | 
| cittecla | 53:1c61cadbcb35 | 170 | if(fp == NULL) { | 
| cittecla | 53:1c61cadbcb35 | 171 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 172 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 173 | } | 
| cittecla | 53:1c61cadbcb35 | 174 | printf("success!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 175 | |
| cittecla | 53:1c61cadbcb35 | 176 | char string[STR_LEN]; | 
| cittecla | 53:1c61cadbcb35 | 177 | int linecounter = 1; | 
| cittecla | 57:79fed71031da | 178 | int checksum = 0; | 
| cittecla | 57:79fed71031da | 179 | int checksum2 = 0; | 
| cittecla | 53:1c61cadbcb35 | 180 | while (fgets(string, STR_LEN, fp) != NULL) { | 
| cittecla | 53:1c61cadbcb35 | 181 | switch (linecounter) { | 
| cittecla | 53:1c61cadbcb35 | 182 | case 1: | 
| cittecla | 53:1c61cadbcb35 | 183 | printf("Reading Line 1..."); | 
| cittecla | 58:cda5298c9b7f | 184 | strcpy(userfile.firstName, strtok(string, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 185 | strcpy(userfile.secondName, strtok(NULL, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 186 | printf("done!\r\n"); | 
| cittecla | 58:cda5298c9b7f | 187 | printf("\tFirst Name:\t%s \n\r\tLast Name:\t%s \n\r", 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 | 58:cda5298c9b7f | 222 | //close file | 
| cittecla | 58:cda5298c9b7f | 223 | fclose(fp); | 
| cittecla | 55:bdab541f434d | 224 | //Unmount the SD card | 
| cittecla | 55:bdab541f434d | 225 | sd.unmount(); | 
| cittecla | 55:bdab541f434d | 226 | |
| cittecla | 53:1c61cadbcb35 | 227 | return userfile; | 
| cittecla | 58:cda5298c9b7f | 228 | } | 
| cittecla | 58:cda5298c9b7f | 229 | |
| cittecla | 58:cda5298c9b7f | 230 | int writeMedication(int user, s_user userfile) | 
| cittecla | 58:cda5298c9b7f | 231 | { | 
| cittecla | 58:cda5298c9b7f | 232 | |
| cittecla | 58:cda5298c9b7f | 233 | char filepath[] = "/sd/medication/medicationUser2.txt"; | 
| cittecla | 58:cda5298c9b7f | 234 | if(user==0) { | 
| cittecla | 58:cda5298c9b7f | 235 | strcpy(filepath, "/sd/medication/medicationUser1.txt"); | 
| cittecla | 58:cda5298c9b7f | 236 | } | 
| cittecla | 58:cda5298c9b7f | 237 | |
| cittecla | 58:cda5298c9b7f | 238 | //Configure CRC, large frames, and write validation | 
| cittecla | 58:cda5298c9b7f | 239 | sd.crc(true); | 
| cittecla | 58:cda5298c9b7f | 240 | sd.large_frames(true); | 
| cittecla | 58:cda5298c9b7f | 241 | sd.write_validation(true); | 
| cittecla | 58:cda5298c9b7f | 242 | |
| cittecla | 58:cda5298c9b7f | 243 | //mount SD card | 
| cittecla | 58:cda5298c9b7f | 244 | printf("\n\rMounting SD card..."); | 
| cittecla | 58:cda5298c9b7f | 245 | if (sd.mount() != 0) { | 
| cittecla | 58:cda5298c9b7f | 246 | printf("failed!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 247 | return EXIT_FAILURE; | 
| cittecla | 58:cda5298c9b7f | 248 | } | 
| cittecla | 58:cda5298c9b7f | 249 | printf("success!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 250 | |
| cittecla | 58:cda5298c9b7f | 251 | //delete file | 
| cittecla | 58:cda5298c9b7f | 252 | //sd.remove(filepath); | 
| cittecla | 58:cda5298c9b7f | 253 | |
| cittecla | 58:cda5298c9b7f | 254 | //open file for write | 
| cittecla | 58:cda5298c9b7f | 255 | printf("open file %s...",filepath); | 
| cittecla | 58:cda5298c9b7f | 256 | FILE *fp = fopen(filepath, "w"); | 
| cittecla | 58:cda5298c9b7f | 257 | if(fp == NULL) { | 
| cittecla | 58:cda5298c9b7f | 258 | printf("failed!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 259 | return EXIT_FAILURE; | 
| cittecla | 58:cda5298c9b7f | 260 | } | 
| cittecla | 58:cda5298c9b7f | 261 | printf("success!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 262 | printf("writing to SD card..."); | 
| cittecla | 58:cda5298c9b7f | 263 | fprintf(fp,"%s;%s",userfile.firstName,userfile.secondName); | 
| cittecla | 58:cda5298c9b7f | 264 | int checksum = 0; | 
| cittecla | 58:cda5298c9b7f | 265 | for(int i = 2; i< 30; i++) { | 
| cittecla | 58:cda5298c9b7f | 266 | int daycounter = (i-2)/4; | 
| cittecla | 58:cda5298c9b7f | 267 | int momentcounter = (i-2)%4; | 
| cittecla | 58:cda5298c9b7f | 268 | int hour = userfile.medication.day[daycounter].moment[momentcounter].time.hour; | 
| cittecla | 58:cda5298c9b7f | 269 | int minute = userfile.medication.day[daycounter].moment[momentcounter].time.minute; | 
| cittecla | 58:cda5298c9b7f | 270 | int offsetMinus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus; | 
| cittecla | 58:cda5298c9b7f | 271 | int offsetPlus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus; | 
| cittecla | 58:cda5298c9b7f | 272 | checksum += hour+minute+offsetMinus+offsetPlus; | 
| cittecla | 58:cda5298c9b7f | 273 | int medication = 0; | 
| cittecla | 58:cda5298c9b7f | 274 | for(int ii = 0; ii < 6; ii++) { | 
| cittecla | 58:cda5298c9b7f | 275 | int devider = pow(10.0f,ii); | 
| cittecla | 58:cda5298c9b7f | 276 | medication += 100000/devider * userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii]; | 
| cittecla | 58:cda5298c9b7f | 277 | checksum+=userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii]; | 
| cittecla | 58:cda5298c9b7f | 278 | } | 
| cittecla | 58:cda5298c9b7f | 279 | fprintf(fp,"%02d;%02d;%02d;%02d;%06d\r\n",hour, minute,offsetMinus, offsetPlus, medication); | 
| cittecla | 58:cda5298c9b7f | 280 | |
| cittecla | 58:cda5298c9b7f | 281 | } | 
| cittecla | 58:cda5298c9b7f | 282 | fprintf(fp,"%d",checksum); | 
| cittecla | 58:cda5298c9b7f | 283 | printf("done!\r\n"); | 
| cittecla | 58:cda5298c9b7f | 284 | |
| cittecla | 58:cda5298c9b7f | 285 | //close file | 
| cittecla | 58:cda5298c9b7f | 286 | printf("closing file %s...",filepath); | 
| cittecla | 58:cda5298c9b7f | 287 | fclose(fp); | 
| cittecla | 58:cda5298c9b7f | 288 | printf("done!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 289 | //Unmount the SD card | 
| cittecla | 58:cda5298c9b7f | 290 | printf("Unmounting SD card..."); | 
| cittecla | 58:cda5298c9b7f | 291 | sd.unmount(); | 
| cittecla | 58:cda5298c9b7f | 292 | printf("done!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 293 | |
| cittecla | 58:cda5298c9b7f | 294 | return EXIT_SUCCESS; | 
| itslinear | 64:ca667234c845 | 295 | } | 
| itslinear | 64:ca667234c845 | 296 | |
| itslinear | 64:ca667234c845 | 297 | |
| itslinear | 64:ca667234c845 | 298 | |
| itslinear | 64:ca667234c845 | 299 | |
| itslinear | 66:0d43bd7ed179 | 300 | /*char read_medProtocol(char user) | 
| itslinear | 64:ca667234c845 | 301 | { | 
| itslinear | 64:ca667234c845 | 302 | char buffer[200]; | 
| itslinear | 64:ca667234c845 | 303 | char filepath[] = "/sd/protocol/medProtocolUser2.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 304 | if(user==0) { | 
| itslinear | 64:ca667234c845 | 305 | strcpy(filepath, "/sd/protocol/medProtocolUser1.txt"); // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 306 | } | 
| itslinear | 64:ca667234c845 | 307 | |
| itslinear | 64:ca667234c845 | 308 | //Configure CRC, large frames, and write validation | 
| itslinear | 64:ca667234c845 | 309 | sd.crc(true); | 
| itslinear | 64:ca667234c845 | 310 | sd.large_frames(true); | 
| itslinear | 64:ca667234c845 | 311 | sd.write_validation(true); | 
| itslinear | 64:ca667234c845 | 312 | |
| itslinear | 64:ca667234c845 | 313 | //mount SD card | 
| itslinear | 64:ca667234c845 | 314 | printf("\n\rMounting SD card..."); | 
| itslinear | 64:ca667234c845 | 315 | if (sd.mount() != 0) { | 
| itslinear | 64:ca667234c845 | 316 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 317 | return buffer; | 
| itslinear | 64:ca667234c845 | 318 | } | 
| itslinear | 64:ca667234c845 | 319 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 320 | |
| itslinear | 64:ca667234c845 | 321 | //open file for read | 
| itslinear | 64:ca667234c845 | 322 | printf("open file %s...",filepath); | 
| itslinear | 64:ca667234c845 | 323 | FILE *fp = fopen(filepath, "r"); | 
| itslinear | 64:ca667234c845 | 324 | if(fp == NULL) { | 
| itslinear | 64:ca667234c845 | 325 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 326 | return buffer; | 
| itslinear | 64:ca667234c845 | 327 | } | 
| itslinear | 64:ca667234c845 | 328 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 329 | |
| itslinear | 64:ca667234c845 | 330 | |
| itslinear | 64:ca667234c845 | 331 | while (fgets(buffer,200, fp) != NULL) { // nicht fertig | 
| itslinear | 64:ca667234c845 | 332 | |
| itslinear | 64:ca667234c845 | 333 | |
| itslinear | 64:ca667234c845 | 334 | } | 
| itslinear | 64:ca667234c845 | 335 | //close file | 
| itslinear | 64:ca667234c845 | 336 | fclose(fp); | 
| itslinear | 64:ca667234c845 | 337 | //Unmount the SD card | 
| itslinear | 64:ca667234c845 | 338 | sd.unmount(); | 
| itslinear | 64:ca667234c845 | 339 | |
| itslinear | 64:ca667234c845 | 340 | return buffer; | 
| itslinear | 64:ca667234c845 | 341 | } | 
| itslinear | 66:0d43bd7ed179 | 342 | */ | 
| itslinear | 64:ca667234c845 | 343 | |
| itslinear | 64:ca667234c845 | 344 | |
| itslinear | 66:0d43bd7ed179 | 345 | int write_medProtocol(s_time medicationTime, s_time outputTime, s_medContainer medication, int success, char user) | 
| itslinear | 64:ca667234c845 | 346 | { | 
| itslinear | 64:ca667234c845 | 347 | char filepath[] = "/sd/protocol/medProtocolUser2.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 348 | if(user==0) { | 
| itslinear | 66:0d43bd7ed179 | 349 | strcpy(filepath, "/sd/protocol/medProtocolUser1.txt"); // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 350 | } | 
| itslinear | 64:ca667234c845 | 351 | |
| itslinear | 64:ca667234c845 | 352 | //Configure CRC, large frames, and write validation | 
| itslinear | 64:ca667234c845 | 353 | sd.crc(true); | 
| itslinear | 64:ca667234c845 | 354 | sd.large_frames(true); | 
| itslinear | 64:ca667234c845 | 355 | sd.write_validation(true); | 
| itslinear | 64:ca667234c845 | 356 | |
| itslinear | 64:ca667234c845 | 357 | //mount SD card | 
| itslinear | 64:ca667234c845 | 358 | printf("\n\rMounting SD card..."); | 
| itslinear | 64:ca667234c845 | 359 | if (sd.mount() != 0) { | 
| itslinear | 64:ca667234c845 | 360 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 361 | return EXIT_FAILURE; | 
| itslinear | 64:ca667234c845 | 362 | } | 
| itslinear | 64:ca667234c845 | 363 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 364 | |
| itslinear | 77:c2e22d1e5d44 | 365 | //open file for append | 
| itslinear | 64:ca667234c845 | 366 | printf("open file %s...",filepath); | 
| itslinear | 64:ca667234c845 | 367 | FILE *fp = fopen(filepath, "a"); | 
| itslinear | 64:ca667234c845 | 368 | if(fp == NULL) { | 
| itslinear | 64:ca667234c845 | 369 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 370 | return EXIT_FAILURE; | 
| itslinear | 64:ca667234c845 | 371 | } | 
| itslinear | 64:ca667234c845 | 372 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 373 | printf("writing to SD card..."); | 
| itslinear | 77:c2e22d1e5d44 | 374 | fprintf(fp,"%d;%02d;%02d;%02d;%02d;%02d;%d;%02d;%02d;",outputTime.weekday,outputTime.day,outputTime.month,outputTime.year,medicationTime.hour,medicationTime.minute,success,outputTime.hour,outputTime.minute); | 
| itslinear | 77:c2e22d1e5d44 | 375 | fprintf(fp,"%d%d%d%d%d%d\r\n", medication.container[0], medication.container[1], medication.container[2], medication.container[3], medication.container[4], medication.container[5]); | 
| itslinear | 77:c2e22d1e5d44 | 376 | |
| itslinear | 64:ca667234c845 | 377 | printf("done!\r\n"); | 
| itslinear | 64:ca667234c845 | 378 | |
| itslinear | 64:ca667234c845 | 379 | //close file | 
| itslinear | 64:ca667234c845 | 380 | printf("closing file %s...",filepath); | 
| itslinear | 64:ca667234c845 | 381 | fclose(fp); | 
| itslinear | 64:ca667234c845 | 382 | printf("done!\n\r"); | 
| itslinear | 64:ca667234c845 | 383 | //Unmount the SD card | 
| itslinear | 64:ca667234c845 | 384 | printf("Unmounting SD card..."); | 
| itslinear | 64:ca667234c845 | 385 | sd.unmount(); | 
| itslinear | 64:ca667234c845 | 386 | printf("done!\n\r"); | 
| itslinear | 64:ca667234c845 | 387 | |
| itslinear | 64:ca667234c845 | 388 | return EXIT_SUCCESS; | 
| itslinear | 64:ca667234c845 | 389 | } | 
| itslinear | 64:ca667234c845 | 390 | |
| itslinear | 77:c2e22d1e5d44 | 391 | int erase_medProtocol() | 
| itslinear | 66:0d43bd7ed179 | 392 | { | 
| itslinear | 66:0d43bd7ed179 | 393 | char filepath[] = "/sd/protocol/medProtocolUser1.txt"; | 
| itslinear | 66:0d43bd7ed179 | 394 | |
| itslinear | 66:0d43bd7ed179 | 395 | //Configure CRC, large frames, and write validation | 
| itslinear | 66:0d43bd7ed179 | 396 | sd.crc(true); | 
| itslinear | 66:0d43bd7ed179 | 397 | sd.large_frames(true); | 
| itslinear | 66:0d43bd7ed179 | 398 | sd.write_validation(true); | 
| itslinear | 66:0d43bd7ed179 | 399 | |
| itslinear | 66:0d43bd7ed179 | 400 | //mount SD card | 
| itslinear | 66:0d43bd7ed179 | 401 | printf("\n\rMounting SD card..."); | 
| itslinear | 66:0d43bd7ed179 | 402 | if (sd.mount() != 0) { | 
| itslinear | 66:0d43bd7ed179 | 403 | printf("failed!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 404 | return EXIT_FAILURE; | 
| itslinear | 66:0d43bd7ed179 | 405 | } | 
| itslinear | 66:0d43bd7ed179 | 406 | |
| itslinear | 77:c2e22d1e5d44 | 407 | //open file for write | 
| itslinear | 77:c2e22d1e5d44 | 408 | printf("\n\ropen file %s...",filepath); | 
| itslinear | 77:c2e22d1e5d44 | 409 | FILE *fp = fopen(filepath, "w"); | 
| itslinear | 77:c2e22d1e5d44 | 410 | if(fp == NULL) { | 
| itslinear | 77:c2e22d1e5d44 | 411 | printf("failed!\n\r"); | 
| itslinear | 77:c2e22d1e5d44 | 412 | return EXIT_FAILURE; | 
| itslinear | 77:c2e22d1e5d44 | 413 | } | 
| itslinear | 77:c2e22d1e5d44 | 414 | |
| itslinear | 77:c2e22d1e5d44 | 415 | //close file | 
| itslinear | 77:c2e22d1e5d44 | 416 | printf("\n\rclosing file %s...",filepath); | 
| itslinear | 77:c2e22d1e5d44 | 417 | fclose(fp); | 
| itslinear | 77:c2e22d1e5d44 | 418 | |
| itslinear | 66:0d43bd7ed179 | 419 | strcpy(filepath, "/sd/protocol/medProtocolUser2.txt"); | 
| itslinear | 77:c2e22d1e5d44 | 420 | |
| itslinear | 77:c2e22d1e5d44 | 421 | //open file for write | 
| itslinear | 77:c2e22d1e5d44 | 422 | printf("\n\ropen file %s...",filepath); | 
| itslinear | 77:c2e22d1e5d44 | 423 | fp = fopen(filepath, "w"); | 
| itslinear | 77:c2e22d1e5d44 | 424 | if(fp == NULL) { | 
| itslinear | 77:c2e22d1e5d44 | 425 | printf("failed!\n\r"); | 
| itslinear | 77:c2e22d1e5d44 | 426 | return EXIT_FAILURE; | 
| itslinear | 77:c2e22d1e5d44 | 427 | } | 
| itslinear | 77:c2e22d1e5d44 | 428 | |
| itslinear | 77:c2e22d1e5d44 | 429 | //close file | 
| itslinear | 77:c2e22d1e5d44 | 430 | printf("closing file %s...",filepath); | 
| itslinear | 77:c2e22d1e5d44 | 431 | fclose(fp); | 
| itslinear | 66:0d43bd7ed179 | 432 | |
| itslinear | 66:0d43bd7ed179 | 433 | sd.unmount(); | 
| itslinear | 66:0d43bd7ed179 | 434 | |
| itslinear | 77:c2e22d1e5d44 | 435 | printf("done"); | 
| itslinear | 77:c2e22d1e5d44 | 436 | |
| itslinear | 66:0d43bd7ed179 | 437 | return EXIT_SUCCESS; | 
| itslinear | 66:0d43bd7ed179 | 438 | } | 
| itslinear | 66:0d43bd7ed179 | 439 | |
| itslinear | 66:0d43bd7ed179 | 440 | int write_medInventory(s_medContainer medInventory, s_time currentTime, char med1[], char med2[], char med3[], char med4[], char med5[], char med6[]) | 
| itslinear | 66:0d43bd7ed179 | 441 | { | 
| itslinear | 66:0d43bd7ed179 | 442 | char filepath[] = "/sd/protocol/medInventory.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 66:0d43bd7ed179 | 443 | |
| itslinear | 66:0d43bd7ed179 | 444 | //Configure CRC, large frames, and write validation | 
| itslinear | 66:0d43bd7ed179 | 445 | sd.crc(true); | 
| itslinear | 66:0d43bd7ed179 | 446 | sd.large_frames(true); | 
| itslinear | 66:0d43bd7ed179 | 447 | sd.write_validation(true); | 
| itslinear | 66:0d43bd7ed179 | 448 | |
| itslinear | 66:0d43bd7ed179 | 449 | //mount SD card | 
| itslinear | 66:0d43bd7ed179 | 450 | printf("\n\rMounting SD card..."); | 
| itslinear | 66:0d43bd7ed179 | 451 | if (sd.mount() != 0) { | 
| itslinear | 66:0d43bd7ed179 | 452 | printf("failed!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 453 | return EXIT_FAILURE; | 
| itslinear | 66:0d43bd7ed179 | 454 | } | 
| itslinear | 66:0d43bd7ed179 | 455 | printf("success!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 456 | |
| itslinear | 66:0d43bd7ed179 | 457 | //open file for write | 
| itslinear | 66:0d43bd7ed179 | 458 | printf("open file %s...",filepath); | 
| itslinear | 66:0d43bd7ed179 | 459 | FILE *fp = fopen(filepath, "w"); | 
| itslinear | 66:0d43bd7ed179 | 460 | if(fp == NULL) { | 
| itslinear | 66:0d43bd7ed179 | 461 | printf("failed!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 462 | return EXIT_FAILURE; | 
| itslinear | 66:0d43bd7ed179 | 463 | } | 
| itslinear | 66:0d43bd7ed179 | 464 | printf("success!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 465 | printf("writing to SD card..."); | 
| itslinear | 77:c2e22d1e5d44 | 466 | fprintf(fp,"%s\r\n", med1); | 
| itslinear | 77:c2e22d1e5d44 | 467 | fprintf(fp,"%02d\r\n", medInventory.container[0]); | 
| itslinear | 77:c2e22d1e5d44 | 468 | fprintf(fp,"%s\r\n", med2); | 
| itslinear | 77:c2e22d1e5d44 | 469 | fprintf(fp,"%02d\r\n", medInventory.container[1]); | 
| itslinear | 77:c2e22d1e5d44 | 470 | fprintf(fp,"%s\r\n", med3); | 
| itslinear | 77:c2e22d1e5d44 | 471 | fprintf(fp,"%02d\r\n", medInventory.container[2]); | 
| itslinear | 77:c2e22d1e5d44 | 472 | fprintf(fp,"%s\r\n", med4); | 
| itslinear | 77:c2e22d1e5d44 | 473 | fprintf(fp,"%02d\r\n", medInventory.container[3]); | 
| itslinear | 77:c2e22d1e5d44 | 474 | fprintf(fp,"%s\r\n", med5); | 
| itslinear | 77:c2e22d1e5d44 | 475 | fprintf(fp,"%02d\r\n", medInventory.container[4]); | 
| itslinear | 77:c2e22d1e5d44 | 476 | fprintf(fp,"%s\r\n", med6); | 
| itslinear | 77:c2e22d1e5d44 | 477 | fprintf(fp,"%02d\r\n", medInventory.container[5]); | 
| itslinear | 77:c2e22d1e5d44 | 478 | fprintf(fp,"%02d;%02d;%02d;%02d;%02d;\r\n", currentTime.day, currentTime.month, currentTime.year, currentTime.hour, currentTime.minute); | 
| itslinear | 66:0d43bd7ed179 | 479 | |
| itslinear | 66:0d43bd7ed179 | 480 | printf("done!\r\n"); | 
| itslinear | 66:0d43bd7ed179 | 481 | |
| itslinear | 66:0d43bd7ed179 | 482 | //close file | 
| itslinear | 66:0d43bd7ed179 | 483 | printf("closing file %s...",filepath); | 
| itslinear | 66:0d43bd7ed179 | 484 | fclose(fp); | 
| itslinear | 66:0d43bd7ed179 | 485 | printf("done!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 486 | //Unmount the SD card | 
| itslinear | 66:0d43bd7ed179 | 487 | printf("Unmounting SD card..."); | 
| itslinear | 66:0d43bd7ed179 | 488 | sd.unmount(); | 
| itslinear | 66:0d43bd7ed179 | 489 | printf("done!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 490 | |
| itslinear | 66:0d43bd7ed179 | 491 | return EXIT_SUCCESS; | 
| itslinear | 66:0d43bd7ed179 | 492 | } | 
| itslinear | 66:0d43bd7ed179 | 493 | |
| itslinear | 66:0d43bd7ed179 | 494 | int write_medError(char error[], s_time time) | 
| itslinear | 66:0d43bd7ed179 | 495 | { | 
| itslinear | 66:0d43bd7ed179 | 496 | char filepath[] = "/sd/protocol/Error.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 66:0d43bd7ed179 | 497 | |
| itslinear | 66:0d43bd7ed179 | 498 | //Configure CRC, large frames, and write validation | 
| itslinear | 66:0d43bd7ed179 | 499 | sd.crc(true); | 
| itslinear | 66:0d43bd7ed179 | 500 | sd.large_frames(true); | 
| itslinear | 66:0d43bd7ed179 | 501 | sd.write_validation(true); | 
| itslinear | 66:0d43bd7ed179 | 502 | |
| itslinear | 66:0d43bd7ed179 | 503 | //mount SD card | 
| itslinear | 66:0d43bd7ed179 | 504 | printf("\n\rMounting SD card..."); | 
| itslinear | 66:0d43bd7ed179 | 505 | if (sd.mount() != 0) { | 
| itslinear | 66:0d43bd7ed179 | 506 | printf("failed!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 507 | return EXIT_FAILURE; | 
| itslinear | 66:0d43bd7ed179 | 508 | } | 
| itslinear | 66:0d43bd7ed179 | 509 | printf("success!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 510 | |
| itslinear | 77:c2e22d1e5d44 | 511 | //open file for append | 
| itslinear | 66:0d43bd7ed179 | 512 | printf("open file %s...",filepath); | 
| itslinear | 66:0d43bd7ed179 | 513 | FILE *fp = fopen(filepath, "a"); | 
| itslinear | 66:0d43bd7ed179 | 514 | if(fp == NULL) { | 
| itslinear | 66:0d43bd7ed179 | 515 | printf("failed!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 516 | return EXIT_FAILURE; | 
| itslinear | 66:0d43bd7ed179 | 517 | } | 
| itslinear | 66:0d43bd7ed179 | 518 | printf("success!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 519 | printf("writing to SD card..."); | 
| itslinear | 77:c2e22d1e5d44 | 520 | fprintf(fp,"%02d;%02d;%02d;%02d;%02d;%s\r\n", time.day, time.month, time.year, time.hour, time.minute, error); | 
| itslinear | 66:0d43bd7ed179 | 521 | |
| itslinear | 66:0d43bd7ed179 | 522 | printf("done!\r\n"); | 
| itslinear | 66:0d43bd7ed179 | 523 | |
| itslinear | 66:0d43bd7ed179 | 524 | //close file | 
| itslinear | 66:0d43bd7ed179 | 525 | printf("closing file %s...",filepath); | 
| itslinear | 66:0d43bd7ed179 | 526 | fclose(fp); | 
| itslinear | 66:0d43bd7ed179 | 527 | printf("done!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 528 | //Unmount the SD card | 
| itslinear | 66:0d43bd7ed179 | 529 | printf("Unmounting SD card..."); | 
| itslinear | 66:0d43bd7ed179 | 530 | sd.unmount(); | 
| itslinear | 66:0d43bd7ed179 | 531 | printf("done!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 532 | |
| itslinear | 66:0d43bd7ed179 | 533 | return EXIT_SUCCESS; | 
| itslinear | 66:0d43bd7ed179 | 534 | } | 
| itslinear | 66:0d43bd7ed179 | 535 | |
| itslinear | 77:c2e22d1e5d44 | 536 | int erase_medError() | 
| itslinear | 66:0d43bd7ed179 | 537 | { | 
| itslinear | 66:0d43bd7ed179 | 538 | char filepath[] = "/sd/protocol/Error.txt"; | 
| itslinear | 66:0d43bd7ed179 | 539 | |
| itslinear | 66:0d43bd7ed179 | 540 | //Configure CRC, large frames, and write validation | 
| itslinear | 66:0d43bd7ed179 | 541 | sd.crc(true); | 
| itslinear | 66:0d43bd7ed179 | 542 | sd.large_frames(true); | 
| itslinear | 66:0d43bd7ed179 | 543 | sd.write_validation(true); | 
| itslinear | 66:0d43bd7ed179 | 544 | |
| itslinear | 66:0d43bd7ed179 | 545 | //mount SD card | 
| itslinear | 66:0d43bd7ed179 | 546 | printf("\n\rMounting SD card..."); | 
| itslinear | 66:0d43bd7ed179 | 547 | if (sd.mount() != 0) { | 
| itslinear | 66:0d43bd7ed179 | 548 | printf("failed!\n\r"); | 
| itslinear | 66:0d43bd7ed179 | 549 | return EXIT_FAILURE; | 
| itslinear | 66:0d43bd7ed179 | 550 | } | 
| itslinear | 66:0d43bd7ed179 | 551 | |
| itslinear | 77:c2e22d1e5d44 | 552 | //open file for write | 
| itslinear | 77:c2e22d1e5d44 | 553 | printf("open file %s...",filepath); | 
| itslinear | 77:c2e22d1e5d44 | 554 | FILE *fp = fopen(filepath, "w"); | 
| itslinear | 77:c2e22d1e5d44 | 555 | if(fp == NULL) { | 
| itslinear | 77:c2e22d1e5d44 | 556 | printf("failed!\n\r"); | 
| itslinear | 77:c2e22d1e5d44 | 557 | return EXIT_FAILURE; | 
| itslinear | 77:c2e22d1e5d44 | 558 | } | 
| itslinear | 77:c2e22d1e5d44 | 559 | |
| itslinear | 77:c2e22d1e5d44 | 560 | //close file | 
| itslinear | 77:c2e22d1e5d44 | 561 | printf("closing file %s...",filepath); | 
| itslinear | 77:c2e22d1e5d44 | 562 | fclose(fp); | 
| itslinear | 66:0d43bd7ed179 | 563 | |
| itslinear | 66:0d43bd7ed179 | 564 | sd.unmount(); | 
| itslinear | 66:0d43bd7ed179 | 565 | |
| itslinear | 77:c2e22d1e5d44 | 566 | printf("done"); | 
| itslinear | 77:c2e22d1e5d44 | 567 | |
| itslinear | 66:0d43bd7ed179 | 568 | return EXIT_SUCCESS; | 
| itslinear | 66:0d43bd7ed179 | 569 | } | 
