You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2022/09/12 20:12:09 UTC

[incubator-nuttx] branch master updated: reinit spi sd when sd status is wrong.

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6a01099c59 reinit spi sd when sd status is wrong.
6a01099c59 is described below

commit 6a01099c596ae6d78028f4c20b875bfbbdc25fe9
Author: licheng <ch...@bestechnic.com>
AuthorDate: Tue Sep 6 18:58:00 2022 +0800

    reinit spi sd when sd status is wrong.
    
    Signed-off-by: licheng <ch...@bestechnic.com>
---
 drivers/mmcsd/Kconfig     |  6 ++++++
 drivers/mmcsd/mmcsd_spi.c | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/drivers/mmcsd/Kconfig b/drivers/mmcsd/Kconfig
old mode 100644
new mode 100755
index 912b5d2e30..bf6ac955bc
--- a/drivers/mmcsd/Kconfig
+++ b/drivers/mmcsd/Kconfig
@@ -98,6 +98,12 @@ config MMCSD_IDMODE_CLOCK
 		SPI clock identify MMC/SD card.
 		Should be 400KHz or less.
 
+config MMCSD_SPIRETRY_COUNT
+	int "MMC/SD read/write fail retry max count"
+	default 0
+	---help---
+		Try to recovery MMCSD read/write fail.
+
 endif
 
 config SDIO_DMA
diff --git a/drivers/mmcsd/mmcsd_spi.c b/drivers/mmcsd/mmcsd_spi.c
old mode 100644
new mode 100755
index fa76911260..095bd9a6a6
--- a/drivers/mmcsd/mmcsd_spi.c
+++ b/drivers/mmcsd/mmcsd_spi.c
@@ -1161,6 +1161,8 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
 {
   FAR struct mmcsd_slot_s *slot;
   FAR struct spi_dev_s *spi;
+  FAR unsigned char *restore = buffer;
+  int retry_count = 0;
   size_t nbytes;
   off_t  offset;
   uint8_t response;
@@ -1235,6 +1237,7 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
       return (ssize_t)ret;
     }
 
+retry:
   SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
 
   /* Single or multiple block read? */
@@ -1307,6 +1310,21 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
 
 errout_with_eio:
   SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
+  if (retry_count++ < CONFIG_MMCSD_SPIRETRY_COUNT)
+    {
+      buffer = restore;
+      ret = mmcsd_mediainitialize(slot);
+      if (ret < 0)
+        {
+          ferr("ERROR: Failed to reinitialize card\n");
+        }
+      else
+        {
+          fwarn("ERROR: retry %d\n", retry_count);
+          goto retry;
+        }
+    }
+
   mmcsd_semgive(slot);
   return -EIO;
 }
@@ -1326,6 +1344,8 @@ static ssize_t mmcsd_write(FAR struct inode *inode,
 {
   FAR struct mmcsd_slot_s *slot;
   FAR struct spi_dev_s *spi;
+  FAR const unsigned char *restore = buffer;
+  int retry_count = 0;
   size_t nbytes;
   off_t  offset;
   uint8_t response;
@@ -1410,6 +1430,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode,
       return (ssize_t)ret;
     }
 
+retry:
   SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
 
   /* Single or multiple block transfer? */
@@ -1502,6 +1523,21 @@ static ssize_t mmcsd_write(FAR struct inode *inode,
 
 errout_with_sem:
   SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
+  if (retry_count++ < CONFIG_MMCSD_SPIRETRY_COUNT)
+    {
+      buffer = restore;
+      ret = mmcsd_mediainitialize(slot);
+      if (ret < 0)
+        {
+          ferr("ERROR: Failed to reinitialize card\n");
+        }
+      else
+        {
+          fwarn("ERROR: retry %d\n", retry_count);
+          goto retry;
+        }
+    }
+
   mmcsd_semgive(slot);
   return -EIO;
 }