Skip to content

Commit

Permalink
Add writeSectorsSame for faster FAT32 format with SHARED_SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Oct 9, 2021
1 parent eebca9f commit 76b0804
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/FatLib/FatFormatter.cpp
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/SdCard/SdSpiCard.cpp
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/SdCard/SdSpiCard.h
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions src/common/BlockDeviceInterface.h
Expand Up @@ -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

0 comments on commit 76b0804

Please sign in to comment.