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

Dependents:   PrV_SDCard

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDCard.cpp Source File

SDCard.cpp

00001 #include "SDCard.h"
00002 
00003 SDCard::SDCard(PinName miso, PinName mosi, PinName sck, PinName cs) : __blockDevice(miso, mosi, sck, cs), __fileSystem("fs")
00004 {
00005     __cs = cs;
00006 }
00007 
00008 int SDCard::mounting(){
00009     // Try to mount the filesystem
00010     printf("Mounting the filesystem... \r\n");
00011     fflush(stdout);
00012 
00013     __err = __fileSystem.mount(&__blockDevice);
00014     printf("%s\r\n", (__err ? "Fail :(" : "OK"));
00015     if (__err) {
00016         // Reformat if we can't mount the filesystem
00017         // this should only happen on the first boot
00018         printf("No filesystem found... \r\n");
00019         fflush(stdout);
00020         /*
00021         err = fileSystem.reformat(&blockDevice);
00022         my_pc.printf("%s\r\n", (err ? "Fail :(" : "OK"));
00023         if (err) {
00024             error("error: %s (%d)\r\n", strerror(-err), err);
00025         }
00026         */
00027         return -1;
00028     }
00029     return 1;
00030 } 
00031 
00032 
00033 int SDCard::opening_file(char *path, char* filename){
00034     char filetoopen[100];
00035     // Open the numbers file
00036     strcpy(filetoopen, path); 
00037     strcat(filetoopen, filename);
00038     printf("Opening FILE %s ... \r\n", filetoopen);
00039     fflush(stdout);
00040     __f = fopen(filetoopen, "r+");
00041     printf("%s\r\n", (!__f ? "Fail :(" : "OK"));
00042     if (!__f) {
00043         // Create the numbers file if it doesn't exist
00044         printf("No file found... \r\n");
00045         fflush(stdout);
00046         return -1;
00047     }
00048     return 1;
00049 }
00050 
00051 int SDCard::closing_file(void){
00052     // Close the file which also flushes any cached writes
00053     printf("Closing FILE ... \r\n");
00054     fflush(stdout);
00055     __err = fclose(__f);
00056     printf("%s\r\n", (__err < 0 ? "Fail :(" : "OK"));
00057     if (__err < 0) {
00058         error("error: %s (%d)\r\n", strerror(errno), -errno);
00059         return -1;
00060     }
00061     return 1;
00062 }
00063 
00064 int SDCard::unmounting(){
00065     printf("Unmounting... \r\n");
00066     fflush(stdout);
00067     __err = __fileSystem.unmount();
00068     printf("%s\r\n", (__err < 0 ? "Fail :(" : "OK"));
00069     if (__err < 0) {
00070         error("error: %s (%d)\r\n", strerror(-__err), __err);
00071         return -1;
00072     }
00073     printf("Deinitializing the block device... \r\n");
00074     fflush(stdout);
00075     __err = __blockDevice.deinit();
00076     printf("%s\r\n", (__err ? "Fail :(" : "OK"));
00077     if (__err) {
00078         error("error: %s (%d)\r\n", strerror(-__err), __err);
00079         return -1;
00080     }
00081     return 1;
00082 }
00083 
00084 FILE * SDCard::getFile(void){
00085     return __f;
00086 }