From 433322b582957fbfc5888c11b303fa816f28a8f9 Mon Sep 17 00:00:00 2001 From: Dawid Wojtas Date: Wed, 12 Apr 2023 10:22:49 +0200 Subject: [PATCH] [BH-1671] Reinit eMMC driver Reinit eMMC driver if write/read transmission failed. --- harmony_changelog.md | 6 +- module-bsp/board/rt1051/bsp/eMMC/fsl_mmc.c | 81 +++++++++++++++++++--- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/harmony_changelog.md b/harmony_changelog.md index ba5f9582af836dce67a9b93110a028407a000471..18c34faf215074f7113219f70f38ca13319b70f7 100644 --- a/harmony_changelog.md +++ b/harmony_changelog.md @@ -50,15 +50,13 @@ #### General: * Fixed displayed device name when connected to Windows +* Minor firmware fixes/optimizations. +* Reinitialization of eMMC driver after failure #### UI/UX: * Added missing translations in French. -#### General: - -* Minor firmware fixes/optimizations. - ## [1.8.2 2023-04-03] ### Added diff --git a/module-bsp/board/rt1051/bsp/eMMC/fsl_mmc.c b/module-bsp/board/rt1051/bsp/eMMC/fsl_mmc.c index 5e0dd06b5236170ea50f0472582d102adc94f5fc..4fe53561d0e75d2044529a69584ca35d076a110f 100644 --- a/module-bsp/board/rt1051/bsp/eMMC/fsl_mmc.c +++ b/module-bsp/board/rt1051/bsp/eMMC/fsl_mmc.c @@ -35,6 +35,8 @@ #include #include "fsl_mmc.h" +#include + /******************************************************************************* * Definitons ******************************************************************************/ @@ -2146,11 +2148,14 @@ status_t MMC_ReadBlocks(mmc_card_t *card, uint8_t *buffer, uint32_t startBlock, assert(buffer); assert(blockCount); + const uint8_t maxAttempts = 5; uint32_t blockCountOneTime; /* The block count can be erased in one time sending READ_BLOCKS command. */ uint32_t blockDone; /* The blocks has been read. */ uint32_t blockLeft; /* Left blocks to be read. */ uint8_t *nextBuffer; bool dataAddrAlign = true; + bool errorOccured = false; + bool isWriteOk = false; blockLeft = blockCount; blockDone = 0U; @@ -2177,11 +2182,37 @@ status_t MMC_ReadBlocks(mmc_card_t *card, uint8_t *buffer, uint32_t startBlock, } } - if (kStatus_Success != MMC_Read(card, - dataAddrAlign ? nextBuffer : (uint8_t *)g_sdmmc, - (startBlock + blockDone), - FSL_SDMMC_DEFAULT_BLOCK_SIZE, - blockCountOneTime)) { + for (uint8_t i = 0; i < maxAttempts; i++) + { + if (errorOccured) + { + if (MMC_Init(card) == kStatus_Success) + { + LOG_ERROR("MMC reinit OK\n"); + errorOccured = false; + } + else + { + LOG_ERROR("MMC reinit error\n"); + continue; + } + } + + if (kStatus_Success != MMC_Read(card, dataAddrAlign ? nextBuffer : (uint8_t *)g_sdmmc, (startBlock + blockDone), + FSL_SDMMC_DEFAULT_BLOCK_SIZE, blockCountOneTime)) + { + LOG_ERROR("MMC_Read transfer failed\n"); + errorOccured = true; + } + else + { + isWriteOk = true; + break; + } + } + + if (!isWriteOk) + { return kStatus_SDMMC_TransferFailed; } @@ -2201,11 +2232,14 @@ status_t MMC_WriteBlocks(mmc_card_t *card, const uint8_t *buffer, uint32_t start assert(buffer); assert(blockCount); + const uint8_t maxAttempts = 5; uint32_t blockCountOneTime; uint32_t blockLeft; uint32_t blockDone; const uint8_t *nextBuffer; bool dataAddrAlign = true; + bool errorOccured = false; + bool isWriteOk = false; blockLeft = blockCount; blockDone = 0U; @@ -2233,14 +2267,41 @@ status_t MMC_WriteBlocks(mmc_card_t *card, const uint8_t *buffer, uint32_t start } } - if (kStatus_Success != MMC_Write(card, - dataAddrAlign ? nextBuffer : (uint8_t *)g_sdmmc, - (startBlock + blockDone), - FSL_SDMMC_DEFAULT_BLOCK_SIZE, - blockCountOneTime)) { + for (uint8_t i = 0; i < maxAttempts; i++) + { + if (errorOccured) + { + if (MMC_Init(card) == kStatus_Success) + { + LOG_ERROR("MMC reinit OK\n"); + errorOccured = false; + } + else + { + LOG_ERROR("MMC reinit error\n"); + continue; + } + } + + if (kStatus_Success != MMC_Write(card, dataAddrAlign ? nextBuffer : (uint8_t *)g_sdmmc, + (startBlock + blockDone), FSL_SDMMC_DEFAULT_BLOCK_SIZE, blockCountOneTime)) + { + LOG_ERROR("MMC_Write transfer failed\n"); + errorOccured = true; + } + else + { + isWriteOk = true; + break; + } + } + + if (!isWriteOk) + { return kStatus_SDMMC_TransferFailed; } + blockDone += blockCountOneTime; if (!card->noInteralAlign) { memset(g_sdmmc, 0U, FSL_SDMMC_DEFAULT_BLOCK_SIZE);