diff --git a/os/hal/src/hal_mmc_spi.c b/os/hal/src/hal_mmc_spi.c index 163adde74..65dcf76cd 100644 --- a/os/hal/src/hal_mmc_spi.c +++ b/os/hal/src/hal_mmc_spi.c @@ -312,14 +312,14 @@ static uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len) { * * @notapi */ -static void wait(MMCDriver *mmcp) { +static bool mmc_wait_idle(MMCDriver *mmcp) { int i; uint8_t buf[4]; for (i = 0; i < 16; i++) { spiReceiveSmall(mmcp->config->spip, 1, buf); - if (buf[0] == 0xFFU) { - return; + if (buf[0] == 0xFFU) { + return HAL_SUCCESS; } } #if MMC_NICE_WAITING == TRUE @@ -330,7 +330,7 @@ static void wait(MMCDriver *mmcp) { while (true) { spiReceiveSmall(mmcp->config->spip, 1, buf); if (buf[0] == 0xFFU) { - break; + return HAL_SUCCESS; } #if MMC_NICE_WAITING == TRUE /* Trying to be nice with the other threads.*/ @@ -341,6 +341,8 @@ static void wait(MMCDriver *mmcp) { } #endif } + + return HAL_FAILED; } /** @@ -356,7 +358,7 @@ static void send_hdr(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) { uint8_t buf[6]; /* Wait for the bus to become idle if a write operation was in progress.*/ - wait(mmcp); + mmc_wait_idle(mmcp); buf[0] = (uint8_t)0x40U | cmd; buf[1] = (uint8_t)(arg >> 24U); @@ -503,37 +505,6 @@ static bool read_CxD(MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) { return HAL_FAILED; } -/** - * @brief Waits that the card reaches an idle state. - * - * @param[in] mmcp pointer to the @p MMCDriver object - * - * @notapi - */ -static void sync(MMCDriver *mmcp) { - uint8_t buf[1]; - - spiSelect(mmcp->config->spip); -#if MMC_NICE_WAITING == TRUE - int waitCounter = 0; -#endif - while (true) { - spiReceiveSmall(mmcp->config->spip, 1, buf); - if (buf[0] == 0xFFU) { - break; - } -#if MMC_NICE_WAITING == TRUE - /* Trying to be nice with the other threads.*/ - osalThreadSleepMilliseconds(1); - if (++waitCounter == MMC_WAIT_RETRY) { - // it's time to give up, this MMC card is not working property - break; - } -#endif - } - spiUnselect(mmcp->config->spip); -} - /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ @@ -740,10 +711,12 @@ failed: * @api */ bool mmcDisconnect(MMCDriver *mmcp) { + bool result; osalDbgCheck(mmcp != NULL); osalSysLock(); + osalDbgAssert((mmcp->state == BLK_ACTIVE) || (mmcp->state == BLK_READY), "invalid state"); if (mmcp->state == BLK_ACTIVE) { @@ -755,11 +728,16 @@ bool mmcDisconnect(MMCDriver *mmcp) { /* Wait for the pending write operations to complete.*/ spiStart(mmcp->config->spip, mmcp->config->hscfg); - sync(mmcp); + spiSelect(mmcp->config->spip); + result = mmc_wait_idle(mmcp); + + spiUnselect(mmcp->config->spip); spiStop(mmcp->config->spip); + mmcp->state = BLK_ACTIVE; - return HAL_SUCCESS; + + return result; } /** @@ -992,7 +970,7 @@ bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer) { spiIgnoreSmall(mmcp->config->spip, 2); /* CRC ignored. */ spiReceiveSmall(mmcp->config->spip, 1, b); if ((b[0] & 0x1FU) == 0x05U) { - wait(mmcp); + mmc_wait_idle(mmcp); return HAL_SUCCESS; } @@ -1043,6 +1021,7 @@ bool mmcStopSequentialWrite(MMCDriver *mmcp) { * @api */ bool mmcSync(MMCDriver *mmcp) { + bool result; osalDbgCheck(mmcp != NULL); @@ -1054,11 +1033,16 @@ bool mmcSync(MMCDriver *mmcp) { mmcp->state = BLK_SYNCING; spiStart(mmcp->config->spip, mmcp->config->hscfg); - sync(mmcp); + spiSelect(mmcp->config->spip); + + result = mmc_wait_idle(mmcp); + + spiUnselect(mmcp->config->spip); /* Synchronization operation finished.*/ mmcp->state = BLK_READY; - return HAL_SUCCESS; + + return result; } /**