From bcf7ac748378aba93c3aed21a19a716ba9e18f07 Mon Sep 17 00:00:00 2001 From: Sergey1560 <53866542+Sergey1560@users.noreply.github.com> Date: Tue, 9 Jun 2020 03:59:44 +0300 Subject: [PATCH] STM32F1: Fix SDIO read errors (#18228) --- Marlin/src/HAL/STM32F1/sdio.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Marlin/src/HAL/STM32F1/sdio.cpp b/Marlin/src/HAL/STM32F1/sdio.cpp index 5b5f8bae7a..7184608ded 100644 --- a/Marlin/src/HAL/STM32F1/sdio.cpp +++ b/Marlin/src/HAL/STM32F1/sdio.cpp @@ -101,8 +101,24 @@ bool SDIO_ReadBlock_DMA(uint32_t blockAddress, uint8_t *data) { return false; } - while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {} + while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) { /* wait */ } + + //If there were SDIO errors, do not wait DMA. + if (SDIO->STA & SDIO_STA_TRX_ERROR_FLAGS) { + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + return false; + } + + //Wait for DMA transaction to complete + while ((DMA2_BASE->ISR & (DMA_ISR_TEIF4|DMA_ISR_TCIF4)) == 0 ) { /* wait */ } + if (DMA2_BASE->ISR & DMA_ISR_TEIF4) { + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); + return false; + } + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); if (SDIO->STA & SDIO_STA_RXDAVL) { @@ -146,7 +162,7 @@ bool SDIO_WriteBlock(uint32_t blockAddress, const uint8_t *data) { sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512U, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN); - while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {} + while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) { /* wait */ } dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);