Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
FileSystemInit.cpp
00001 /******************************************************************************* 00002 * Copyright 2016, 2017 ARM Ltd. 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 //uncomment this to use littleFS instead of fatFS 00017 //#define PAL_EXAMPLE_USE_LITTLE_FS 00018 00019 #include "pal.h" 00020 #include "mbed.h" 00021 #include "BlockDevice.h" 00022 #include "MBRBlockDevice.h" 00023 #include "storage-selector/storage-selector.h" 00024 00025 bool FileSystemInit = false; 00026 00027 #ifndef PRIMARY_PARTITION_NUMBER 00028 #define PRIMARY_PARTITION_NUMBER 1 00029 #endif 00030 00031 #ifndef PRIMARY_PARTITION_START 00032 #define PRIMARY_PARTITION_START 0 00033 #endif 00034 00035 #ifndef PRIMARY_PARTITION_SIZE 00036 #define PRIMARY_PARTITION_SIZE 512*1024 00037 #endif 00038 00039 #ifndef SECONDARY_PARTITION_NUMBER 00040 #define SECONDARY_PARTITION_NUMBER 2 00041 #endif 00042 00043 #ifndef SECONDARY_PARTITION_START 00044 #define SECONDARY_PARTITION_START PRIMARY_PARTITION_SIZE 00045 #endif 00046 00047 #ifndef SECONDARY_PARTITION_SIZE 00048 #define SECONDARY_PARTITION_SIZE PRIMARY_PARTITION_SIZE 00049 #endif 00050 00051 //Uncomment this to create the partitions 00052 #define PAL_EXAMPLE_GENERATE_PARTITION 00053 00054 //Uncomment this to format partitions if fs->mount() fails 00055 #define PAL_EXAMPLE_FORMAT_PARTITION 00056 00057 #define PAL_PARTITION_TYPE 0x83 00058 // 00059 // See the mbed_lib.json in the sd-driver library for the definitions. 00060 // See the sd-driver library README.md for details with CI-shield etc. 00061 // Add also new boards/exceptions there rather than in code directly 00062 // OR 00063 // alternatively overload via your mbed_app.json (MBED_CONF_APP...) 00064 // 00065 00066 static BlockDevice *bd = storage_selector(); 00067 00068 static MBRBlockDevice part1(bd, 1); 00069 static FileSystem *fs1; 00070 static MBRBlockDevice part2(bd, 2); 00071 static FileSystem *fs2; 00072 00073 00074 static int ReFormatPartition(BlockDevice* part, FileSystem* filesystem) 00075 { 00076 int err = 0; 00077 printf("re-format partition\r\n"); 00078 err = filesystem->reformat(part); 00079 return err; 00080 } 00081 00082 static int initFileSystem(BlockDevice* part, FileSystem* filesystem, bool reformat) 00083 { 00084 int err = 0; 00085 if (reformat) 00086 { 00087 err = filesystem->reformat(part); 00088 } 00089 err = filesystem->unmount(); // filesystem_selector func do mount but doesnt return value , for checking if mount function return error we need first to unmount and then try to mount again. 00090 if (err < 0) { 00091 printf("failed to unmount %d\r\n", err); 00092 } 00093 err = filesystem->mount(part); 00094 if (err < 0) { 00095 printf("failed to mount %d\r\n", err); 00096 err = ReFormatPartition(part, filesystem); 00097 } 00098 if (err == 0) { 00099 err = filesystem->mkdir("bsp_test", 0600); // FATFS miss magic field. mkdir to check FS correctness. 00100 if (err != 0) { 00101 printf("failed to mkdir - reformat \r\n"); 00102 err = ReFormatPartition(part, filesystem); 00103 } 00104 filesystem->remove("bsp_test"); // delete in any case even after format 00105 } 00106 return err; 00107 } 00108 00109 int initSDcardAndFileSystem(bool reformat) 00110 { 00111 int err = 0; 00112 printf("Initializing the file system\r\n"); 00113 #if (MBED_CONF_STORAGE_SELECTOR_FILESYSTEM_INSTANCES > 0) 00114 err = part1.init(); 00115 if (err < 0) 00116 { 00117 printf("failed to init primary partition cause %d\r\n", err); 00118 err = MBRBlockDevice::partition(bd, PRIMARY_PARTITION_NUMBER, PAL_PARTITION_TYPE, PRIMARY_PARTITION_START, PRIMARY_PARTITION_START + PRIMARY_PARTITION_SIZE); 00119 if (err < 0) { 00120 printf("Failed to initialize primary partition\r\n"); 00121 } 00122 } 00123 if (!err) 00124 { 00125 fs1 = filesystem_selector(((char*)PAL_FS_MOUNT_POINT_PRIMARY + 1), &part1, 1); 00126 err = initFileSystem(&part1, fs1, reformat); 00127 } 00128 #if (MBED_CONF_STORAGE_SELECTOR_FILESYSTEM_INSTANCES == 2) 00129 if (!err) { 00130 err = part2.init(); 00131 if (err < 0) { 00132 printf("failed to init secondary partition cause %d\r\n", err); 00133 err = MBRBlockDevice::partition(bd, SECONDARY_PARTITION_NUMBER, PAL_PARTITION_TYPE, SECONDARY_PARTITION_START, SECONDARY_PARTITION_START + SECONDARY_PARTITION_SIZE); 00134 if (err < 0) { 00135 printf("Failed to initialize secondary partition\r\n"); 00136 } 00137 } 00138 if (!err) { 00139 fs2 = filesystem_selector(((char*)PAL_FS_MOUNT_POINT_SECONDARY + 1), &part2, 2); 00140 err = initFileSystem(&part2, fs2, reformat); 00141 } 00142 } 00143 #endif 00144 #endif 00145 if (!err) 00146 { 00147 printf("Succeed to initialize the file system\r\n"); 00148 FileSystemInit = true; 00149 } 00150 00151 return err; 00152 }
Generated on Tue Jul 12 2022 19:12:12 by 1.7.2