diff --git a/src/FatLib/FatFormatter.cpp b/src/FatLib/FatFormatter.cpp index acae6950..7108b1f7 100644 --- a/src/FatLib/FatFormatter.cpp +++ b/src/FatLib/FatFormatter.cpp @@ -85,6 +85,7 @@ bool FatFormatter::initFatDir(uint8_t fatType, uint32_t sectorCount) { size_t n; memset(m_secBuf, 0, BYTES_PER_SECTOR); writeMsg("Writing FAT "); +#if 0 for (uint32_t i = 1; i < sectorCount; i++) { if (!m_dev->writeSector(m_fatStart + i, m_secBuf)) { return false; @@ -93,6 +94,11 @@ bool FatFormatter::initFatDir(uint8_t fatType, uint32_t sectorCount) { writeMsg("."); } } +#else + if (!m_dev->writeSectorsSame(m_fatStart + 1, m_secBuf, sectorCount - 1)) { + return false; + } +#endif writeMsg("\r\n"); // Allocate reserved clusters and root for FAT32. m_secBuf[0] = 0XF8; diff --git a/src/SdCard/SdSpiCard.cpp b/src/SdCard/SdSpiCard.cpp index 4c733f58..0d56e87d 100644 --- a/src/SdCard/SdSpiCard.cpp +++ b/src/SdCard/SdSpiCard.cpp @@ -811,6 +811,39 @@ bool SdSpiCard::writeSectors(uint32_t sector, const uint8_t* src, size_t ns) { return false; } //------------------------------------------------------------------------------ +bool SdSpiCard::writeSectorsSame(uint32_t sector, const uint8_t* src, size_t ns) { +#if ENABLE_DEDICATED_SPI + if (m_curState != WRITE_STATE || m_curSector != sector) { + if (!writeStart(sector)) { + goto fail; + } + m_curSector = sector; + m_curState = WRITE_STATE; + } + for (size_t i = 0; i < ns; i++) { + if (!writeData(src)) { + goto fail; + } + } + m_curSector += ns; + return m_sharedSpi ? syncDevice() : true; +#else // ENABLE_DEDICATED_SPI + if (!writeStart(sector)) { + goto fail; + } + for (size_t i = 0; i < ns; i++) { + if (!writeData(src)) { + goto fail; + } + } + return writeStop(); +#endif // ENABLE_DEDICATED_SPI + + fail: + spiStop(); + return false; +} +//------------------------------------------------------------------------------ bool SdSpiCard::writeStart(uint32_t sector) { // use address if not SDHC card if (type() != SD_CARD_TYPE_SDHC) { diff --git a/src/SdCard/SdSpiCard.h b/src/SdCard/SdSpiCard.h index b83d3367..659ea69d 100644 --- a/src/SdCard/SdSpiCard.h +++ b/src/SdCard/SdSpiCard.h @@ -237,6 +237,15 @@ class SdSpiCard { * \return true for success or false for failure. */ bool writeSectors(uint32_t sector, const uint8_t* src, size_t ns); + /** + * Write multiple sectors with the same 512 byte data to an SD card. + * + * \param[in] sector Logical sector to be written. + * \param[in] ns Number of sectors to be written. + * \param[in] src Pointer to the location of the data to be written. + * \return true for success or false for failure. + */ + bool writeSectorsSame(uint32_t sector, const uint8_t* src, size_t ns); /** Write one data sector in a multiple sector write sequence. * \param[in] src Pointer to the location of the data to be written. * \return true for success or false for failure. diff --git a/src/common/BlockDeviceInterface.h b/src/common/BlockDeviceInterface.h index fe0f7aad..acdafa6c 100644 --- a/src/common/BlockDeviceInterface.h +++ b/src/common/BlockDeviceInterface.h @@ -89,5 +89,20 @@ class BlockDeviceInterface { * \return true for success or false for failure. */ virtual bool writeSectors(uint32_t sector, const uint8_t* src, size_t ns) = 0; + + /** + * Write multiple sectors with the same data. + * + * \param[in] sector Logical sector to be written. + * \param[in] ns Number of sectors to be written. + * \param[in] src Pointer to the location of the data to be written. + * \return true for success or false for failure. + */ + virtual bool writeSectorsSame(uint32_t sector, const uint8_t* src, size_t ns) { + for (size_t i = 0; i < ns; i++) { + if (!writeSector(sector + i, src)) return false; + } + return true; + } }; #endif // BlockDeviceInterface_h