diff --git a/src/shared/drivers/qspi-flash/qspi-flash.cpp b/src/shared/drivers/qspi-flash/qspi-flash.cpp index ebd613b23ce3a036e5c23e5bd1224d29e7a41590..6e7649acad6747b06292b76c5530afbc07be21bf 100644 --- a/src/shared/drivers/qspi-flash/qspi-flash.cpp +++ b/src/shared/drivers/qspi-flash/qspi-flash.cpp @@ -19,6 +19,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + +#ifdef _ARCH_CORTEXM7_STM32F7 + #include "qspi-flash.h" using namespace miosix; @@ -38,12 +41,11 @@ bool QspiFlash::abortReset() // abort possible ongoing command Qspi->CR |= QUADSPI_CR_ABORT; - // Wait while aborted - uint32_t dt = 0; // timeout + // Wait till abort is completed + uint64_t dt = miosix::getTime(); // timeout while (Qspi->CR & QUADSPI_CR_ABORT) { - dt = dt + 1; - if (dt > 100000) + if ((miosix::getTime() - dt) > 10000000) // 10ms { return false; } @@ -64,11 +66,10 @@ bool QspiFlash::abortReset() bool QspiFlash::waitBusy() { // wait till QUADSPI has completed the current communication with the flash - uint32_t dt = 0; // timeout + uint64_t dt = miosix::getTime(); // timeout while (Qspi->SR & (1 << QUADSPI_SR_BUSY_Pos)) { - dt = dt + 1; - if (dt > 20000) + if ((miosix::getTime() - dt) > 10000000) // 10ms { return false; } @@ -81,11 +82,10 @@ bool QspiFlash::waitTransfer() { // by setting data lenght register (DLR) you set how many bytes are expected // from memory. wait till all expected bytes have been tranferred. - uint32_t dt = 0; // timeout + uint64_t dt = miosix::getTime(); // timeout while (!(Qspi->SR & (1 << QUADSPI_SR_TCF_Pos))) { - dt = dt + 1; - if (dt > 20000) + if ((miosix::getTime() - dt) > 10000000) // 10ms { return false; } @@ -777,11 +777,12 @@ bool QspiFlash::byteProgram(uint8_t data, uint32_t address, bool verify) } // wait till current program operation has ended - uint32_t dt = 0; // timeout + uint64_t dt = miosix::getTime(); // timeout while (isInProgress()) { - dt = dt + 1; - if (dt >= 5000) // max program byte cycle time = 100us + // max timeout time: 5ms + // max program byte cycle time = 100us + if ((miosix::getTime() - dt) > 5000000) { softwareReset(); // device forced reset to default status return false; @@ -1027,6 +1028,7 @@ bool QspiFlash::readSector(uint8_t* vector, const size_t size, // loop until all bytes expected have been received or // there are some data bytes to read + uint64_t dt = miosix::getTime(); // timeout while (!(Qspi->SR & QUADSPI_SR_TCF) || (((Qspi->SR & QUADSPI_SR_FLEVEL) >> 8) > 0)) { @@ -1052,6 +1054,15 @@ bool QspiFlash::readSector(uint8_t* vector, const size_t size, return false; } } + else + { + // timeout time = 400ms + if ((miosix::getTime() - dt) > 400000000) + { + abortReset(); // abort transaction with flash + return false; + } + } } // reset transfer complete flag bit (TCF) @@ -1127,11 +1138,10 @@ bool QspiFlash::pageProgram(const uint8_t* vector, const size_t size, { // if FIFO is full - wait till it has at least a byte available. - uint32_t dt = 0; // timeout + uint64_t dt = miosix::getTime(); // timeout while (((Qspi->SR & QUADSPI_SR_FLEVEL) >> 8) >= 32) { - dt = dt + 1; - if (dt >= 10000) + if ((miosix::getTime() - dt) > 100000000) // timeout time: 100 ms { return false; } @@ -1283,4 +1293,6 @@ bool QspiFlash::write(const uint8_t* vector, const size_t size, } return true; -} \ No newline at end of file +} + +#endif // _ARCH_CORTEXM7_STM32F7 \ No newline at end of file diff --git a/src/shared/drivers/qspi-flash/qspi-flash.h b/src/shared/drivers/qspi-flash/qspi-flash.h index d4877542460d09b532f6fba952ae101854fa37e6..6fe25ccb519b8bce1634ad564ab6fa236c7b9522 100644 --- a/src/shared/drivers/qspi-flash/qspi-flash.h +++ b/src/shared/drivers/qspi-flash/qspi-flash.h @@ -19,6 +19,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + +#ifdef _ARCH_CORTEXM7_STM32F7 + #include <miosix.h> #include <utils/ClockUtils.h> @@ -359,4 +362,6 @@ private: // reset memory, reset enable command should be executed first RESET_MEMORY = 0x99 }; -}; \ No newline at end of file +}; + +#endif // _ARCH_CORTEXM7_STM32F7 \ No newline at end of file diff --git a/src/tests/drivers/QuadSpi-Flash/test-Qflash.cpp b/src/tests/drivers/QuadSpi-Flash/test-Qflash.cpp index bd3a259d9c9635ae3e631d9f47456d4428b00ed8..9b6d12279b59e018976326ecfa38ef367ca9e5d2 100644 --- a/src/tests/drivers/QuadSpi-Flash/test-Qflash.cpp +++ b/src/tests/drivers/QuadSpi-Flash/test-Qflash.cpp @@ -44,6 +44,8 @@ int main() if (mymemory.test()) { + printf("test(): ok\n"); + // read device id printf("\nID: %ld\n", mymemory.readID()); @@ -67,7 +69,7 @@ int main() return -1; } - printf("write operaton succeded!\n"); + printf("write operation succeded!\n"); uint8_t b[5000] = {0}; uint32_t a = 0; @@ -80,7 +82,7 @@ int main() printf("array (b): \n"); for (i = 0; i < 5000; i++) { - printf("b[%u]: %u\n", i, b[i]); + printf("b[%lu]: %u\n", i, b[i]); } return 0;