diff --git a/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.cpp b/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.cpp index a79f57f482..020964ec3f 100644 --- a/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.cpp +++ b/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.cpp @@ -36,14 +36,8 @@ #define DEBUG_PRINTF(...) #endif -FlashIAPBlockDevice::FlashIAPBlockDevice() - : _flash(), _base(0), _size(0), _is_initialized(false), _init_ref_count(0) -{ - DEBUG_PRINTF("FlashIAPBlockDevice: %" PRIX32 " %" PRIX32 "\r\n", address, size); -} - -FlashIAPBlockDevice::FlashIAPBlockDevice(uint32_t address, uint32_t) - : _flash(), _base(0), _size(0), _is_initialized(false), _init_ref_count(0) +FlashIAPBlockDevice::FlashIAPBlockDevice(uint32_t address, uint32_t size) + : _flash(), _base(address), _size(size), _is_initialized(false), _init_ref_count(0) { } @@ -70,11 +64,27 @@ int FlashIAPBlockDevice::init() int ret = _flash.init(); if (ret) { + core_util_atomic_decr_u32(&_init_ref_count, 1); return ret; } - _base = _flash.get_flash_start(); - _size = _flash.get_flash_size(); + if (_size + _base > _flash.get_flash_size() + _flash.get_flash_start()) { + core_util_atomic_decr_u32(&_init_ref_count, 1); + return BD_ERROR_DEVICE_ERROR; + } + + if (_base < _flash.get_flash_start()) { + core_util_atomic_decr_u32(&_init_ref_count, 1); + return BD_ERROR_DEVICE_ERROR; + } + + if (!_base) { + _base = _flash.get_flash_start(); + } + + if (!_size) { + _size = _flash.get_flash_size() - (_base - _flash.get_flash_start()); + } _is_initialized = true; return ret; diff --git a/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.h b/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.h index 13865cee90..20279658fb 100644 --- a/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.h +++ b/components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.h @@ -28,12 +28,15 @@ */ class FlashIAPBlockDevice : public BlockDevice { public: - /** Creates a FlashIAPBlockDevice **/ - FlashIAPBlockDevice(); - - MBED_DEPRECATED("Please use default constructor instead") - FlashIAPBlockDevice(uint32_t address, uint32_t size = 0); + /** Creates a FlashIAPBlockDevice + * + * @param address Physical address where the block device start + * @param size The block device size + */ + FlashIAPBlockDevice(uint32_t address = MBED_CONF_FLASHIAP_BLOCK_DEVICE_BASE_ADDRESS, + uint32_t size = MBED_CONF_FLASHIAP_BLOCK_DEVICE_SIZE); + virtual ~FlashIAPBlockDevice(); /** Initialize a block device diff --git a/components/storage/blockdevice/COMPONENT_FLASHIAP/TESTS/filesystem/fopen/fopen.cpp b/components/storage/blockdevice/COMPONENT_FLASHIAP/TESTS/filesystem/fopen/fopen.cpp index debe30b28d..9e39dbffe5 100644 --- a/components/storage/blockdevice/COMPONENT_FLASHIAP/TESTS/filesystem/fopen/fopen.cpp +++ b/components/storage/blockdevice/COMPONENT_FLASHIAP/TESTS/filesystem/fopen/fopen.cpp @@ -680,7 +680,7 @@ control_t fslittle_fopen_test_05(const size_t call_count) } -static const char fslittle_fopen_ascii_illegal_buf_g[] = "\"�'*+,./:;<=>?[\\]|"; +static const char fslittle_fopen_ascii_illegal_buf_g[] = "\"?'*+,./:;<=>?[\\]|"; /** @brief test to call fopen() with filename that in includes * illegal characters diff --git a/components/storage/blockdevice/COMPONENT_FLASHIAP/mbed_lib.json b/components/storage/blockdevice/COMPONENT_FLASHIAP/mbed_lib.json new file mode 100644 index 0000000000..49b4cdb7b9 --- /dev/null +++ b/components/storage/blockdevice/COMPONENT_FLASHIAP/mbed_lib.json @@ -0,0 +1,19 @@ +{ + "name": "flashiap-block-device", + "config": { + "base-address": { + "help": "Base address for the block device on the external flash.", + "value": "0xFFFFFFFF" + }, + "size": { + "help": "Memory allocated for block device.", + "value": "0" + } + }, + "target_overrides": { + "REALTEK_RTL8195AM": { + "base-address": "0x1C0000", + "size": "0x40000" + } + } +} diff --git a/features/storage/TESTS/filesystem/general_filesystem/main.cpp b/features/storage/TESTS/filesystem/general_filesystem/main.cpp index fb2a7da94c..06b0ab73de 100644 --- a/features/storage/TESTS/filesystem/general_filesystem/main.cpp +++ b/features/storage/TESTS/filesystem/general_filesystem/main.cpp @@ -24,6 +24,9 @@ #elif COMPONENT_SD #include "SDBlockDevice.h" #include "FATFileSystem.h" +#elif COMPONENT_FLASHIAP +#include "FlashIAPBlockDevice.h" +#include "LittleFileSystem.h" #else #error [NOT_SUPPORTED] storage test not supported on this platform #endif diff --git a/features/storage/system_storage/SystemStorage.cpp b/features/storage/system_storage/SystemStorage.cpp index 5ce4146ef7..04bec9e63f 100644 --- a/features/storage/system_storage/SystemStorage.cpp +++ b/features/storage/system_storage/SystemStorage.cpp @@ -30,8 +30,22 @@ #include "SDBlockDevice.h" #endif +#if COMPONENT_FLASHIAP +#include "FlashIAPBlockDevice.h" +#endif + using namespace mbed; +// Align a value to a specified size. +// Parameters : +// val - [IN] Value. +// size - [IN] Size. +// Return : Aligned value. +static inline uint32_t align_up(uint32_t val, uint32_t size) +{ + return (((val - 1) / size) + 1) * size; +} + MBED_WEAK BlockDevice *BlockDevice::get_default_instance() { #if COMPONENT_SPIF @@ -68,6 +82,37 @@ MBED_WEAK BlockDevice *BlockDevice::get_default_instance() return &default_bd; +#elif COMPONENT_FLASHIAP + +#if (MBED_CONF_FLASHIAP_BLOCK_DEVICE_SIZE == 0) && (MBED_CONF_FLASHIAP_BLOCK_DEVICE_BASE_ADDRESS == 0xFFFFFFFF) + + size_t flash_size; + uint32_t start_address; + uint32_t bottom_address; + FlashIAP flash; + + int ret = flash.init(); + if (ret != 0) { + return 0; + } + + //Find the start of first sector after text area + bottom_address = align_up(FLASHIAP_ROM_END, flash.get_sector_size(FLASHIAP_ROM_END)); + start_address = flash.get_flash_start(); + flash_size = flash.get_flash_size(); + + ret = flash.deinit(); + + static FlashIAPBlockDevice default_bd(bottom_address, start_address + flash_size - bottom_address); + +#else + + static FlashIAPBlockDevice default_bd; + +#endif + + return &default_bd; + #else return NULL; @@ -92,6 +137,13 @@ MBED_WEAK FileSystem *FileSystem::get_default_instance() return &sdcard; +#elif COMPONENT_FLASHIAP + + static LittleFileSystem flash("flash", BlockDevice::get_default_instance()); + flash.set_as_default(); + + return &flash; + #else return NULL; diff --git a/targets/targets.json b/targets/targets.json index c4f634091f..251c7e5d21 100755 --- a/targets/targets.json +++ b/targets/targets.json @@ -1745,6 +1745,7 @@ "device_name": "STM32L486RG" }, "MTB_ADV_WISE_1570": { + "components": ["FLASHIAP"], "inherits": ["FAMILY_STM32"], "core": "Cortex-M4F", "extra_labels_add": ["STM32L4", "STM32L486RG", "STM32L486xG", "WISE_1570"],