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.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "mbed.h" 00017 #include <stdio.h> 00018 #include <errno.h> 00019 00020 // Block devices 00021 //#include "SPIFBlockDevice.h" 00022 //#include "DataFlashBlockDevice.h" 00023 #include "SDBlockDevice.h" 00024 //#include "HeapBlockDevice.h" 00025 00026 // File systems 00027 //#include "LittleFileSystem.h" 00028 #include "FATFileSystem.h" 00029 00030 //----------------------------------------------------------------------- 00031 // RHOMB.IO CARRIER BOARDS 00032 // INDICATE THE CORRECT "CHIP SELECT" PIN TO BE USED: 00033 // SPI_A_CS0 if Rhombio uSD Holder module is assembled on Slave_socket_1 00034 // SPI_A_CS1 if Rhombio uSD Holder module is assembled on Slave_socket_2 00035 // uncommnt here the proper one: 00036 //#define SD_CS SPI_A_CS0 00037 #define SD_CS SPI_A_CS1 00038 //----------------------------------------------------------------------- 00039 00040 // Physical block device, can be any device that supports the BlockDevice API 00041 SDBlockDevice blockDevice(SPI_MOSI, SPI_MISO, SPI_SCK, SD_CS); // mosi, miso, sck, cs 00042 00043 // File system declaration 00044 FATFileSystem fileSystem("fs"); 00045 00046 // Entry point for the example 00047 int main() 00048 { 00049 printf("--- Mbed OS filesystem example ---\n"); 00050 00051 // Try to mount the filesystem 00052 printf("Mounting the filesystem... "); 00053 fflush(stdout); 00054 00055 int err = fileSystem.mount(&blockDevice); 00056 printf("%s\n", (err ? "Fail :(" : "OK")); 00057 if (err) { 00058 // Reformat if we can't mount the filesystem 00059 // this should only happen on the first boot 00060 printf("No filesystem found, formatting... "); 00061 fflush(stdout); 00062 err = fileSystem.reformat(&blockDevice); 00063 printf("%s\n", (err ? "Fail :(" : "OK")); 00064 if (err) { 00065 error("error: %s (%d)\n", strerror(-err), err); 00066 } 00067 } 00068 00069 // Open the numbers file 00070 printf("Opening \"/fs/numbers.txt\"... "); 00071 fflush(stdout); 00072 00073 FILE* f = fopen("/fs/numbers.txt", "r+"); 00074 printf("%s\n", (!f ? "Fail :(" : "OK")); 00075 if (!f) { 00076 // Create the numbers file if it doesn't exist 00077 printf("No file found, creating a new file... "); 00078 fflush(stdout); 00079 f = fopen("/fs/numbers.txt", "w+"); 00080 printf("%s\n", (!f ? "Fail :(" : "OK")); 00081 if (!f) { 00082 error("error: %s (%d)\n", strerror(errno), -errno); 00083 } 00084 00085 for (int i = 0; i < 10; i++) { 00086 printf("\rWriting numbers (%d/%d)... ", i, 10); 00087 fflush(stdout); 00088 err = fprintf(f, " %d\n", i); 00089 if (err < 0) { 00090 printf("Fail :(\n"); 00091 error("error: %s (%d)\n", strerror(errno), -errno); 00092 } 00093 } 00094 00095 printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); 00096 00097 printf("Seeking file... "); 00098 fflush(stdout); 00099 err = fseek(f, 0, SEEK_SET); 00100 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00101 if (err < 0) { 00102 error("error: %s (%d)\n", strerror(errno), -errno); 00103 } 00104 } 00105 00106 // Go through and increment the numbers 00107 for (int i = 0; i < 10; i++) { 00108 printf("\rIncrementing numbers (%d/%d)... ", i, 10); 00109 fflush(stdout); 00110 00111 // Get current stream position 00112 long pos = ftell(f); 00113 00114 // Parse out the number and increment 00115 int32_t number; 00116 fscanf(f, "%ld", &number); 00117 number += 1; 00118 00119 // Seek to beginning of number 00120 fseek(f, pos, SEEK_SET); 00121 00122 // Store number 00123 fprintf(f, " %ld\n", number); 00124 00125 // Flush between write and read on same file 00126 fflush(f); 00127 } 00128 00129 printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10); 00130 00131 // Close the file which also flushes any cached writes 00132 printf("Closing \"/fs/numbers.txt\"... "); 00133 fflush(stdout); 00134 err = fclose(f); 00135 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00136 if (err < 0) { 00137 error("error: %s (%d)\n", strerror(errno), -errno); 00138 } 00139 00140 // Display the root directory 00141 printf("Opening the root directory... "); 00142 fflush(stdout); 00143 00144 DIR* d = opendir("/fs/"); 00145 printf("%s\n", (!d ? "Fail :(" : "OK")); 00146 if (!d) { 00147 error("error: %s (%d)\n", strerror(errno), -errno); 00148 } 00149 00150 printf("root directory:\n"); 00151 while (true) { 00152 struct dirent* e = readdir(d); 00153 if (!e) { 00154 break; 00155 } 00156 00157 printf(" %s\n", e->d_name); 00158 } 00159 00160 printf("Closing the root directory... "); 00161 fflush(stdout); 00162 err = closedir(d); 00163 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00164 if (err < 0) { 00165 error("error: %s (%d)\n", strerror(errno), -errno); 00166 } 00167 00168 // Display the numbers file 00169 printf("Opening \"/fs/numbers.txt\"... "); 00170 fflush(stdout); 00171 f = fopen("/fs/numbers.txt", "r"); 00172 printf("%s\n", (!f ? "Fail :(" : "OK")); 00173 if (!f) { 00174 error("error: %s (%d)\n", strerror(errno), -errno); 00175 } 00176 00177 printf("numbers:\n"); 00178 while (!feof(f)) { 00179 int c = fgetc(f); 00180 printf("%c", c); 00181 } 00182 00183 printf("\rClosing \"/fs/numbers.txt\"... "); 00184 fflush(stdout); 00185 err = fclose(f); 00186 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00187 if (err < 0) { 00188 error("error: %s (%d)\n", strerror(errno), -errno); 00189 } 00190 00191 // Tidy up 00192 printf("Unmounting... "); 00193 fflush(stdout); 00194 err = fileSystem.unmount(); 00195 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00196 if (err < 0) { 00197 error("error: %s (%d)\n", strerror(-err), err); 00198 } 00199 00200 printf("Initializing the block device... "); 00201 fflush(stdout); 00202 00203 err = blockDevice.init(); 00204 printf("%s\n", (err ? "Fail :(" : "OK")); 00205 if (err) { 00206 error("error: %s (%d)\n", strerror(-err), err); 00207 } 00208 00209 printf("Erasing the block device... "); 00210 fflush(stdout); 00211 err = blockDevice.erase(0, blockDevice.size()); 00212 printf("%s\n", (err ? "Fail :(" : "OK")); 00213 if (err) { 00214 error("error: %s (%d)\n", strerror(-err), err); 00215 } 00216 00217 printf("Deinitializing the block device... "); 00218 fflush(stdout); 00219 err = blockDevice.deinit(); 00220 printf("%s\n", (err ? "Fail :(" : "OK")); 00221 if (err) { 00222 error("error: %s (%d)\n", strerror(-err), err); 00223 } 00224 00225 printf("\r\n"); 00226 00227 printf("Mbed OS filesystem example done!\n"); 00228 }
Generated on Fri Jul 15 2022 14:17:18 by
1.7.2
Rhomb.io uSD Card Holder module