diff --git a/storage/blockdevice/COMPONENT_SD/include/SD/SDBlockDevice.h b/storage/blockdevice/COMPONENT_SD/include/SD/SDBlockDevice.h index a309240..46a427e 100644 --- a/storage/blockdevice/COMPONENT_SD/include/SD/SDBlockDevice.h +++ b/storage/blockdevice/COMPONENT_SD/include/SD/SDBlockDevice.h @@ -58,12 +58,12 @@ */ class SDBlockDevice : public mbed::BlockDevice { public: - /** Creates an SDBlockDevice on a SPI bus specified by pins (using dynamic pin-map) + /** 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 * @param sclk SPI clock pin - * @param cs SPI chip select pin + * @param cs SPI chip select pin. This constructor needs a *hardware* chip select pin. * @param hz Clock speed of the SPI bus (defaults to 1MHz) * @param crc_on Enable cyclic redundancy check (defaults to disabled) */ @@ -74,13 +74,47 @@ uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY, bool crc_on = MBED_CONF_SD_CRC_ENABLED); - /** Creates an SDBlockDevice on a SPI bus specified by pins (using static pin-map) + /** Creates an SDBlockDevice on a SPI bus specified by pins (using dynamic pin-map). + * This version creates an SPI object that uses GPIO for its chip select line instead of + * a dedicated hardware CS pin. + * + * @param mosi SPI master out, slave in pin + * @param miso SPI master in, slave out pin + * @param sclk SPI clock pin + * @param cs SPI chip select pin. May be any GPIO pin. + * @param hz Clock speed of the SPI bus (defaults to 1MHz) + * @param crc_on Enable cyclic redundancy check (defaults to disabled) + */ + SDBlockDevice(mbed::use_gpio_ssel_t, + PinName mosi = MBED_CONF_SD_SPI_MOSI, + PinName miso = MBED_CONF_SD_SPI_MISO, + PinName sclk = MBED_CONF_SD_SPI_CLK, + PinName cs = MBED_CONF_SD_SPI_CS, + uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY, + bool crc_on = MBED_CONF_SD_CRC_ENABLED); + + + /** Creates an SDBlockDevice on a SPI bus specified by pins (using static pin-map). + * This version needs a pinmap containing a hardware chip select pin. * * @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, + uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY, + bool crc_on = MBED_CONF_SD_CRC_ENABLED); + + /** Creates an SDBlockDevice on a SPI bus specified by pins (using static pin-map). + * This version creates an SPI object that uses GPIO for its chip select line instead of + * a dedicated hardware CS pin. + * + * @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, + mbed::use_gpio_ssel_t, PinName cs = MBED_CONF_SD_SPI_CS, uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY, bool crc_on = MBED_CONF_SD_CRC_ENABLED); diff --git a/storage/blockdevice/COMPONENT_SD/source/SDBlockDevice.cpp b/storage/blockdevice/COMPONENT_SD/source/SDBlockDevice.cpp index ad90a4e..46ba15a 100644 --- a/storage/blockdevice/COMPONENT_SD/source/SDBlockDevice.cpp +++ b/storage/blockdevice/COMPONENT_SD/source/SDBlockDevice.cpp @@ -251,16 +251,19 @@ // Only HC block size is supported. Making this a static constant reduces code size. const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC; -#if MBED_CONF_SD_CRC_ENABLED SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on) - : _sectors(0), _spi(mosi, miso, sclk, cs, use_gpio_ssel), _is_initialized(0), + : _sectors(0), _spi(mosi, miso, sclk, cs), _is_initialized(0), +#if MBED_CONF_SD_CRC_ENABLED _init_ref_count(0), _crc_on(crc_on) #else -SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on) - : _sectors(0), _spi(mosi, miso, sclk, cs, use_gpio_ssel), _is_initialized(0), _init_ref_count(0) #endif { +#if !MBED_CONF_SD_CRC_ENABLED + // If this assert fails, this code was compiled without CRC support but you tried to use it. + MBED_ASSERT(!crc_on); +#endif + _card_type = SDCARD_NONE; // Set default to 100kHz for initialisation and 1MHz for data transfer @@ -272,16 +275,67 @@ _erase_size = BLOCK_SIZE_HC; } +SDBlockDevice::SDBlockDevice(mbed::use_gpio_ssel_t, PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on) + : _sectors(0), _spi(mosi, miso, sclk, cs, use_gpio_ssel), _is_initialized(0), #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), _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), _is_initialized(0), _init_ref_count(0) #endif { +#if !MBED_CONF_SD_CRC_ENABLED + // If this assert fails, this code was compiled without CRC support but you tried to use it. + MBED_ASSERT(!crc_on); +#endif + + _card_type = SDCARD_NONE; + + // Set default to 100kHz for initialisation and 1MHz for data transfer + 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(const spi_pinmap_t &spi_pinmap, uint64_t hz, bool crc_on) + : _sectors(0), _spi(spi_pinmap), _is_initialized(0), +#if MBED_CONF_SD_CRC_ENABLED + _init_ref_count(0), _crc_on(crc_on) +#else +_init_ref_count(0) +#endif +{ +#if !MBED_CONF_SD_CRC_ENABLED + // If this assert fails, this code was compiled without CRC support but you tried to use it. + MBED_ASSERT(!crc_on); +#endif + + _card_type = SDCARD_NONE; + + // Set default to 100kHz for initialisation and 1MHz for data transfer + 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(const spi_pinmap_t &spi_pinmap, mbed::use_gpio_ssel_t, PinName cs, uint64_t hz, bool crc_on) + : _sectors(0), _spi(spi_pinmap, cs), _is_initialized(0), +#if MBED_CONF_SD_CRC_ENABLED + _init_ref_count(0), _crc_on(crc_on) +#else + _init_ref_count(0) +#endif +{ +#if !MBED_CONF_SD_CRC_ENABLED + // If this assert fails, this code was compiled without CRC support but you tried to use it. + MBED_ASSERT(!crc_on); +#endif + _card_type = SDCARD_NONE; // Set default to 100kHz for initialisation and 1MHz for data transfer