SDCard is a library to access SDCard files mounted as FAT filesystem

Dependents:   PrV_SDCard

Committer:
villemejane
Date:
Tue Mar 10 14:22:02 2020 +0000
Revision:
0:1477b4630c35
SDCard is a library to access SDCard files mounted as FAT filesystem

Who changed what in which revision?

UserRevisionLine numberNew contents of line
villemejane 0:1477b4630c35 1 #include "SDCard.h"
villemejane 0:1477b4630c35 2
villemejane 0:1477b4630c35 3 SDCard::SDCard(PinName miso, PinName mosi, PinName sck, PinName cs) : __blockDevice(miso, mosi, sck, cs), __fileSystem("fs")
villemejane 0:1477b4630c35 4 {
villemejane 0:1477b4630c35 5 __cs = cs;
villemejane 0:1477b4630c35 6 }
villemejane 0:1477b4630c35 7
villemejane 0:1477b4630c35 8 int SDCard::mounting(){
villemejane 0:1477b4630c35 9 // Try to mount the filesystem
villemejane 0:1477b4630c35 10 printf("Mounting the filesystem... \r\n");
villemejane 0:1477b4630c35 11 fflush(stdout);
villemejane 0:1477b4630c35 12
villemejane 0:1477b4630c35 13 __err = __fileSystem.mount(&__blockDevice);
villemejane 0:1477b4630c35 14 printf("%s\r\n", (__err ? "Fail :(" : "OK"));
villemejane 0:1477b4630c35 15 if (__err) {
villemejane 0:1477b4630c35 16 // Reformat if we can't mount the filesystem
villemejane 0:1477b4630c35 17 // this should only happen on the first boot
villemejane 0:1477b4630c35 18 printf("No filesystem found... \r\n");
villemejane 0:1477b4630c35 19 fflush(stdout);
villemejane 0:1477b4630c35 20 /*
villemejane 0:1477b4630c35 21 err = fileSystem.reformat(&blockDevice);
villemejane 0:1477b4630c35 22 my_pc.printf("%s\r\n", (err ? "Fail :(" : "OK"));
villemejane 0:1477b4630c35 23 if (err) {
villemejane 0:1477b4630c35 24 error("error: %s (%d)\r\n", strerror(-err), err);
villemejane 0:1477b4630c35 25 }
villemejane 0:1477b4630c35 26 */
villemejane 0:1477b4630c35 27 return -1;
villemejane 0:1477b4630c35 28 }
villemejane 0:1477b4630c35 29 return 1;
villemejane 0:1477b4630c35 30 }
villemejane 0:1477b4630c35 31
villemejane 0:1477b4630c35 32
villemejane 0:1477b4630c35 33 int SDCard::opening_file(char *path, char* filename){
villemejane 0:1477b4630c35 34 char filetoopen[100];
villemejane 0:1477b4630c35 35 // Open the numbers file
villemejane 0:1477b4630c35 36 strcpy(filetoopen, path);
villemejane 0:1477b4630c35 37 strcat(filetoopen, filename);
villemejane 0:1477b4630c35 38 printf("Opening FILE %s ... \r\n", filetoopen);
villemejane 0:1477b4630c35 39 fflush(stdout);
villemejane 0:1477b4630c35 40 __f = fopen(filetoopen, "r+");
villemejane 0:1477b4630c35 41 printf("%s\r\n", (!__f ? "Fail :(" : "OK"));
villemejane 0:1477b4630c35 42 if (!__f) {
villemejane 0:1477b4630c35 43 // Create the numbers file if it doesn't exist
villemejane 0:1477b4630c35 44 printf("No file found... \r\n");
villemejane 0:1477b4630c35 45 fflush(stdout);
villemejane 0:1477b4630c35 46 return -1;
villemejane 0:1477b4630c35 47 }
villemejane 0:1477b4630c35 48 return 1;
villemejane 0:1477b4630c35 49 }
villemejane 0:1477b4630c35 50
villemejane 0:1477b4630c35 51 int SDCard::closing_file(void){
villemejane 0:1477b4630c35 52 // Close the file which also flushes any cached writes
villemejane 0:1477b4630c35 53 printf("Closing FILE ... \r\n");
villemejane 0:1477b4630c35 54 fflush(stdout);
villemejane 0:1477b4630c35 55 __err = fclose(__f);
villemejane 0:1477b4630c35 56 printf("%s\r\n", (__err < 0 ? "Fail :(" : "OK"));
villemejane 0:1477b4630c35 57 if (__err < 0) {
villemejane 0:1477b4630c35 58 error("error: %s (%d)\r\n", strerror(errno), -errno);
villemejane 0:1477b4630c35 59 return -1;
villemejane 0:1477b4630c35 60 }
villemejane 0:1477b4630c35 61 return 1;
villemejane 0:1477b4630c35 62 }
villemejane 0:1477b4630c35 63
villemejane 0:1477b4630c35 64 int SDCard::unmounting(){
villemejane 0:1477b4630c35 65 printf("Unmounting... \r\n");
villemejane 0:1477b4630c35 66 fflush(stdout);
villemejane 0:1477b4630c35 67 __err = __fileSystem.unmount();
villemejane 0:1477b4630c35 68 printf("%s\r\n", (__err < 0 ? "Fail :(" : "OK"));
villemejane 0:1477b4630c35 69 if (__err < 0) {
villemejane 0:1477b4630c35 70 error("error: %s (%d)\r\n", strerror(-__err), __err);
villemejane 0:1477b4630c35 71 return -1;
villemejane 0:1477b4630c35 72 }
villemejane 0:1477b4630c35 73 printf("Deinitializing the block device... \r\n");
villemejane 0:1477b4630c35 74 fflush(stdout);
villemejane 0:1477b4630c35 75 __err = __blockDevice.deinit();
villemejane 0:1477b4630c35 76 printf("%s\r\n", (__err ? "Fail :(" : "OK"));
villemejane 0:1477b4630c35 77 if (__err) {
villemejane 0:1477b4630c35 78 error("error: %s (%d)\r\n", strerror(-__err), __err);
villemejane 0:1477b4630c35 79 return -1;
villemejane 0:1477b4630c35 80 }
villemejane 0:1477b4630c35 81 return 1;
villemejane 0:1477b4630c35 82 }
villemejane 0:1477b4630c35 83
villemejane 0:1477b4630c35 84 FILE * SDCard::getFile(void){
villemejane 0:1477b4630c35 85 return __f;
villemejane 0:1477b4630c35 86 }