Kev Mann / mbed-dev-OS5_10_4
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SystemStorage.cpp Source File

SystemStorage.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * 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, WITHOUT
00012  * 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 "BlockDevice.h"
00017 #include "FileSystem.h"
00018 #include "FATFileSystem.h"
00019 #include "LittleFileSystem.h"
00020 
00021 #if COMPONENT_SPIF
00022 #include "SPIFBlockDevice.h"
00023 #endif
00024 
00025 #if COMPONENT_DATAFLASH
00026 #include "DataFlashBlockDevice.h"
00027 #endif
00028 
00029 #if COMPONENT_SD
00030 #include "SDBlockDevice.h"
00031 #endif
00032 
00033 #if COMPONENT_FLASHIAP
00034 #include "FlashIAPBlockDevice.h"
00035 #endif
00036 
00037 using namespace mbed;
00038 
00039 // Align a value to a specified size.
00040 // Parameters :
00041 // val           - [IN]   Value.
00042 // size          - [IN]   Size.
00043 // Return        : Aligned value.
00044 static inline uint32_t align_up(uint32_t val, uint32_t size)
00045 {
00046     return (((val - 1) / size) + 1) * size;
00047 }
00048 
00049 MBED_WEAK BlockDevice *BlockDevice::get_default_instance()
00050 {
00051 #if COMPONENT_SPIF
00052 
00053     static SPIFBlockDevice default_bd(
00054         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00055         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00056         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00057         MBED_CONF_SPIF_DRIVER_SPI_CS,
00058         MBED_CONF_SPIF_DRIVER_SPI_FREQ
00059     );
00060 
00061     return &default_bd;
00062 
00063 #elif COMPONENT_DATAFLASH
00064 
00065     static DataFlashBlockDevice default_bd(
00066         MBED_CONF_DATAFLASH_SPI_MOSI,
00067         MBED_CONF_DATAFLASH_SPI_MISO,
00068         MBED_CONF_DATAFLASH_SPI_CLK,
00069         MBED_CONF_DATAFLASH_SPI_CS
00070     );
00071 
00072     return &default_bd;
00073 
00074 #elif COMPONENT_SD
00075 
00076     static SDBlockDevice default_bd(
00077         MBED_CONF_SD_SPI_MOSI,
00078         MBED_CONF_SD_SPI_MISO,
00079         MBED_CONF_SD_SPI_CLK,
00080         MBED_CONF_SD_SPI_CS
00081     );
00082 
00083     return &default_bd;
00084 
00085 #elif COMPONENT_FLASHIAP
00086 
00087 #if (MBED_CONF_FLASHIAP_BLOCK_DEVICE_SIZE == 0) && (MBED_CONF_FLASHIAP_BLOCK_DEVICE_BASE_ADDRESS == 0xFFFFFFFF)
00088 
00089     size_t flash_size;
00090     uint32_t start_address;
00091     uint32_t bottom_address;
00092     FlashIAP flash;
00093 
00094     int ret = flash.init();
00095     if (ret != 0) {
00096         return 0;
00097     }
00098 
00099     //Find the start of first sector after text area
00100     bottom_address = align_up(FLASHIAP_ROM_END, flash.get_sector_size(FLASHIAP_ROM_END));
00101     start_address = flash.get_flash_start();
00102     flash_size = flash.get_flash_size();
00103 
00104     ret = flash.deinit();
00105 
00106     static FlashIAPBlockDevice default_bd(bottom_address, start_address + flash_size - bottom_address);
00107 
00108 #else
00109 
00110     static FlashIAPBlockDevice default_bd;
00111 
00112 #endif
00113 
00114     return &default_bd;
00115     
00116 #else
00117 
00118     return NULL;
00119 
00120 #endif
00121 
00122 }
00123 
00124 MBED_WEAK FileSystem *FileSystem::get_default_instance()
00125 {
00126 #if COMPONENT_SPIF || COMPONENT_DATAFLASH
00127 
00128     static LittleFileSystem flash("flash", BlockDevice::get_default_instance());
00129     flash.set_as_default();
00130 
00131     return &flash;
00132 
00133 #elif COMPONENT_SD
00134 
00135     static FATFileSystem sdcard("sd", BlockDevice::get_default_instance());
00136     sdcard.set_as_default();
00137 
00138     return &sdcard;
00139 
00140 #elif COMPONENT_FLASHIAP
00141 
00142     static LittleFileSystem flash("flash", BlockDevice::get_default_instance());
00143     flash.set_as_default();
00144 
00145     return &flash;
00146 
00147 #else
00148 
00149     return NULL;
00150 
00151 #endif
00152 
00153 }