From ee5953ad7415714d9dba88394bfd9cb78fd9d0c2 Mon Sep 17 00:00:00 2001 From: Przemyslaw Stekiel Date: Mon, 9 Dec 2019 11:40:44 +0100 Subject: [PATCH] Add static pinmap support: SDBlockDevice, kvstore, system storage This is done in order to enable static pin-map for Mbed Cloud Client Example. This should give extra ROM savings, ~1KB. --- .../COMPONENT_SD/SDBlockDevice.cpp | 22 +++++++++++++++++++ .../blockdevice/COMPONENT_SD/SDBlockDevice.h | 12 +++++++++- features/storage/kvstore/conf/kv_config.cpp | 11 ++++++++++ .../storage/system_storage/SystemStorage.cpp | 11 ++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp b/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp index dd7e7da675..35ac80b5e9 100644 --- a/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp +++ b/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp @@ -271,6 +271,28 @@ SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName c _erase_size = BLOCK_SIZE_HC; } +#if MBED_CONF_SD_CRC_ENABLED +SDBlockDevice::SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on) + : _sectors(0), _spi(spi_pinmap), _cs(cs), _is_initialized(0), + _init_ref_count(0), _crc_on(crc_on) +#else +SDBlockDevice::SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on) + : _sectors(0), _spi(spi_pinmap), _cs(cs), _is_initialized(0), + _init_ref_count(0) +#endif +{ + _cs = 1; + _card_type = SDCARD_NONE; + + // Set default to 100kHz for initialisation and 1MHz for data transfer + MBED_STATIC_ASSERT(((MBED_CONF_SD_INIT_FREQUENCY >= 100000) && (MBED_CONF_SD_INIT_FREQUENCY <= 400000)), + "Initialization frequency should be between 100KHz to 400KHz"); + _init_sck = MBED_CONF_SD_INIT_FREQUENCY; + _transfer_sck = hz; + + _erase_size = BLOCK_SIZE_HC; +} + SDBlockDevice::~SDBlockDevice() { if (_is_initialized) { diff --git a/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h b/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h index ebf1048486..4439f883c1 100644 --- a/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h +++ b/components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h @@ -27,6 +27,7 @@ #include "drivers/DigitalOut.h" #include "platform/platform.h" #include "platform/PlatformMutex.h" +#include "hal/static_pinmap.h" /** SDBlockDevice class * @@ -34,7 +35,7 @@ */ class SDBlockDevice : public mbed::BlockDevice { public: - /** Creates an SDBlockDevice on a SPI bus specified by pins + /** Creates an SDBlockDevice on a SPI bus specified by pins (using dynamic pin-map) * * @param mosi SPI master out, slave in pin * @param miso SPI master in, slave out pin @@ -44,6 +45,15 @@ public: * @param crc_on Enable cyclic redundancy check (defaults to disabled) */ SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz = 1000000, bool crc_on = 0); + + /** Creates an SDBlockDevice on a SPI bus specified by pins (using static pin-map) + * + * @param spi_pinmap Static SPI pin-map + * @param hz Clock speed of the SPI bus (defaults to 1MHz) + * @param crc_on Enable cyclic redundancy check (defaults to disabled) + */ + SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz = 1000000, bool crc_on = 0); + virtual ~SDBlockDevice(); /** Initialize a block device diff --git a/features/storage/kvstore/conf/kv_config.cpp b/features/storage/kvstore/conf/kv_config.cpp index 84e21e6985..4f68173a75 100644 --- a/features/storage/kvstore/conf/kv_config.cpp +++ b/features/storage/kvstore/conf/kv_config.cpp @@ -49,6 +49,10 @@ #if COMPONENT_SD #include "components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h" + +#if (STATIC_PINMAP_READY) +constexpr spi_pinmap_t static_spi_pinmap = get_spi_pinmap(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, NC); +#endif #endif /** @@ -564,12 +568,19 @@ BlockDevice *_get_blockdevice_SD(bd_addr_t start_address, bd_size_t size) bd_addr_t aligned_end_address; bd_addr_t aligned_start_address; +#if (STATIC_PINMAP_READY) + static SDBlockDevice bd( + static_spi_pinmap, + MBED_CONF_SD_SPI_CS + ); +#else static SDBlockDevice bd( MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS ); +#endif if (bd.init() != MBED_SUCCESS) { tr_error("KV Config: SDBlockDevice init fail"); diff --git a/features/storage/system_storage/SystemStorage.cpp b/features/storage/system_storage/SystemStorage.cpp index 5ea167a149..8ab9a73ecd 100644 --- a/features/storage/system_storage/SystemStorage.cpp +++ b/features/storage/system_storage/SystemStorage.cpp @@ -38,6 +38,10 @@ #if COMPONENT_SD #include "components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h" + +#if (STATIC_PINMAP_READY) +constexpr spi_pinmap_t static_spi_pinmap = get_spi_pinmap(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, NC); +#endif #endif #if COMPONENT_FLASHIAP @@ -110,12 +114,19 @@ MBED_WEAK BlockDevice *BlockDevice::get_default_instance() #elif COMPONENT_SD +#if (STATIC_PINMAP_READY) + static SDBlockDevice default_bd( + static_spi_pinmap, + MBED_CONF_SD_SPI_CS + ); +#else static SDBlockDevice default_bd( MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS ); +#endif return &default_bd;