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.
Fork of Wio_3G-example-sd-driver by
main.cpp
00001 #include "mbed.h" 00002 #include "FATFileSystem.h" 00003 #include "SDBlockDevice.h" 00004 #include <stdio.h> 00005 #include <errno.h> 00006 /* mbed_retarget.h is included after errno.h so symbols are mapped to 00007 * consistent values for all toolchains */ 00008 #include "platform/mbed_retarget.h" 00009 00010 #if defined(TARGET_WIO_3G) 00011 DigitalOut SD_POWER(PA_15, 1); 00012 #endif 00013 00014 // #define DO_FORMAT 00015 00016 SDBlockDevice sd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS); 00017 FATFileSystem fs("sd", &sd); 00018 Serial pc(USBTX, USBRX, 115200); 00019 00020 void return_error(int ret_val) 00021 { 00022 if (ret_val) 00023 pc.printf("Failure. %d\n", ret_val); 00024 else 00025 pc.printf("done.\n"); 00026 } 00027 00028 void errno_error(void* ret_val) 00029 { 00030 if (ret_val == NULL) 00031 pc.printf(" Failure. %d \n", errno); 00032 else 00033 pc.printf(" done.\n"); 00034 } 00035 00036 int main() 00037 { 00038 int error = 0; 00039 00040 #if defined(DO_FORMAT) 00041 pc.printf("Welcome to the filesystem example.\r\n" 00042 "Formatting... "); 00043 error = FATFileSystem::format(&sd); 00044 return_error(error); 00045 #else 00046 pc.printf("Welcome to the filesystem example.\r\n"); 00047 #endif 00048 00049 pc.printf("Opening a new file, numbers.txt."); 00050 FILE* fd = fopen("/sd/numbers.txt", "w+"); 00051 errno_error(fd); 00052 00053 for (int i = 0; i < 20; i++) { 00054 pc.printf("Writing decimal numbers to a file (%d/20)\r", i); 00055 fprintf(fd, "%d\n", i); 00056 } 00057 pc.printf("Writing decimal numbers to a file (20/20) done.\n"); 00058 00059 pc.printf("Closing file."); 00060 fclose(fd); 00061 pc.printf(" done.\n"); 00062 00063 pc.printf("Re-opening file read-only."); 00064 fd = fopen("/sd/numbers.txt", "r"); 00065 errno_error(fd); 00066 00067 pc.printf("Dumping file to screen.\n"); 00068 char buff[16] = {0}; 00069 while (!feof(fd)) { 00070 int size = fread(&buff[0], 1, 15, fd); 00071 fwrite(&buff[0], 1, size, stdout); 00072 } 00073 pc.printf("EOF.\n"); 00074 00075 pc.printf("Closing file."); 00076 fclose(fd); 00077 pc.printf(" done.\n"); 00078 00079 pc.printf("Opening root directory."); 00080 DIR* dir = opendir("/sd/"); 00081 errno_error(fd); 00082 00083 struct dirent* de; 00084 pc.printf("Printing all filenames:\n"); 00085 while((de = readdir(dir)) != NULL) { 00086 pc.printf(" %s\n", &(de->d_name)[0]); 00087 } 00088 00089 pc.printf("Closeing root directory. "); 00090 error = closedir(dir); 00091 return_error(error); 00092 pc.printf("Filesystem Demo complete.\n"); 00093 00094 while (true) {} 00095 }
Generated on Thu Jul 21 2022 12:40:42 by
1.7.2
