SDFileSystem for STM32F746NG DISCOVERY with 4bit SDMMC interface on fixed pins

Dependencies:   FATFileSystem

Dependents:   DISCO-F746NG_SDFileSystem uzairkhan DISCO-F746NG_Scope_copy

Fork of SDFileSystem by Neil Thiessen

Files at this revision

API Documentation at this revision

Comitter:
DieterGraef
Date:
Tue Apr 12 13:47:47 2016 +0000
Parent:
23:c03ef1abef0e
Child:
25:391eade4ef85
Commit message:
added a lock mechanism to make it usable in RTOS environment.

Changed in this revision

SDFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
SD_Helper.c Show annotated file Show diff for this revision Revisions of this file
SD_Helper.h Show annotated file Show diff for this revision Revisions of this file
--- a/SDFileSystem.cpp	Thu Mar 31 17:39:48 2016 +0000
+++ b/SDFileSystem.cpp	Tue Apr 12 13:47:47 2016 +0000
@@ -40,6 +40,7 @@
     NVIC_SetVector(DMA2_Stream6_IRQn,(uint32_t)h);
     h=(void*)&SDFileSystem::SDMMC1_IRQHandler;
     NVIC_SetVector(SDMMC1_IRQn,(uint32_t)h);
+    BSP_SD_Clear_Busy();
     initstat=BSP_SD_Init();
    if (initstat!=MSD_OK)
    {
@@ -196,19 +197,22 @@
     //Make sure the card is initialized before proceeding
     if (m_Status & STA_NOINIT)
         return RES_NOTRDY;
-
+    while(BSP_SD_Get_Busy()==1){;}
+    BSP_SD_Set_Busy();
     //Read a single block, or multiple blocks
     if (count > 1) {
         BSP_SD_Set_RX_Busy();
         retval=BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512),512, count);
-        while(BSP_SD_Get_RX_busy()==1){;}
+        while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;}
         CPU_CACHE_Flush((uint32_t *)buffer,(512*count));
+        BSP_SD_Clear_Busy();
         return (retval ? RES_ERROR : RES_OK);
     } else {
         BSP_SD_Set_RX_Busy();
         retval= BSP_SD_ReadBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1);
-        while(BSP_SD_Get_RX_busy()==1){;}
+        while((BSP_SD_Get_RX_Busy()==1)&&(retval==MSD_OK)){;}
         CPU_CACHE_Flush((uint32_t *)buffer,(512));
+        BSP_SD_Clear_Busy();
         return (retval ? RES_ERROR : RES_OK);
     }
 }
@@ -219,23 +223,28 @@
     //Make sure the card is initialized before proceeding
     if (m_Status & STA_NOINIT)
         return RES_NOTRDY;
-
+    while(BSP_SD_Get_Busy()==1){;}
+    BSP_SD_Set_Busy();
     //Make sure the card isn't write protected before proceeding
     if (m_Status & STA_PROTECT)
-        return RES_WRPRT;
-
+    {
+    BSP_SD_Clear_Busy();
+    return RES_WRPRT;
+    }
     //Write a single block, or multiple blocks
     if (count > 1) {
         CPU_CACHE_Flush((uint32_t *)buffer,(512*count));
         BSP_SD_Set_TX_Busy();
         retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, count);
-         while(BSP_SD_Get_TX_busy()==1){;}
+         while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;}
+         BSP_SD_Clear_Busy();
          return (retval? RES_ERROR : RES_OK);
     } else {
         CPU_CACHE_Flush((uint32_t *)buffer,(512));
         BSP_SD_Set_TX_Busy();
         retval= BSP_SD_WriteBlocks_DMA((uint32_t *)buffer, (uint64_t) (sector * 512), 512, 1);
-         while(BSP_SD_Get_TX_busy()==1){;}
+         while((BSP_SD_Get_TX_Busy()==1)&&(retval==MSD_OK)){;}
+         BSP_SD_Clear_Busy();
          return (retval? RES_ERROR : RES_OK);
 
     }
@@ -244,11 +253,15 @@
 int SDFileSystem::disk_sync()
 {
     //Select the card so we're forced to wait for the end of any internal write processes
+    while(BSP_SD_Get_Busy()==1){;}
+    BSP_SD_Set_Busy();
     while(BSP_SD_GetStatus()==SD_TRANSFER_BUSY){;}
     if(BSP_SD_GetStatus()==SD_TRANSFER_OK)
     {
+        BSP_SD_Clear_Busy();
         return RES_OK;
     } else {
+        BSP_SD_Clear_Busy();
         return RES_ERROR;
     }
 }
@@ -259,8 +272,11 @@
     //Make sure the card is initialized before proceeding
     if (m_Status & STA_NOINIT)
         return 0;
-   BSP_SD_GetCardInfo(&m_CardInfo);
-   sectors=m_CardInfo.CardCapacity>>9;
+    while(BSP_SD_Get_Busy()==1){;}
+    BSP_SD_Set_Busy();
+    BSP_SD_GetCardInfo(&m_CardInfo);
+    sectors=m_CardInfo.CardCapacity>>9;
+    BSP_SD_Clear_Busy();
     return sectors;
 }
 
@@ -323,6 +339,3 @@
 {
   BSP_SD_IRQHandler();
 }
-
-
-
--- a/SD_Helper.c	Thu Mar 31 17:39:48 2016 +0000
+++ b/SD_Helper.c	Tue Apr 12 13:47:47 2016 +0000
@@ -6,7 +6,7 @@
 static SD_HandleTypeDef uSdHandle;
 static int RX_Busy;
 static int TX_Busy;
-
+static int SD_Busy;
 
 
 
@@ -98,6 +98,18 @@
   SDMMC_SendCommand(uSdHandle.Instance, &sdmmc_cmdinitstructure);
 }
 
+void BSP_SD_Set_Busy(void)
+{
+    SD_Busy=1;
+}
+void BSP_SD_Clear_Busy(void)
+{
+    SD_Busy=0;
+}
+int BSP_SD_Get_Busy(void)
+{
+    return SD_Busy;
+}
 
 void BSP_SD_Set_RX_Busy(void)
 {
@@ -107,7 +119,7 @@
 {
     RX_Busy=0;
 }
-int BSP_SD_Get_RX_busy(void)
+int BSP_SD_Get_RX_Busy(void)
 {
     return RX_Busy;
 }
@@ -120,7 +132,7 @@
 {
     TX_Busy=0;
 }
-int BSP_SD_Get_TX_busy(void)
+int BSP_SD_Get_TX_Busy(void)
 {
     return TX_Busy;
 }
--- a/SD_Helper.h	Thu Mar 31 17:39:48 2016 +0000
+++ b/SD_Helper.h	Tue Apr 12 13:47:47 2016 +0000
@@ -12,12 +12,15 @@
 
 void    BSP_SD_CommandTransaction(char cmd, unsigned int arg);
 
+void BSP_SD_Set_Busy(void);
+void BSP_SD_Clear_Busy(void);
+int BSP_SD_Get_Busy(void);
 void BSP_SD_Set_RX_Busy(void);
 void BSP_SD_Clear_RX_Busy(void);
-int BSP_SD_Get_RX_busy(void);
+int BSP_SD_Get_RX_Busy(void);
 void BSP_SD_Set_TX_Busy(void);
 void BSP_SD_Clear_TX_Busy(void);
-int BSP_SD_Get_TX_busy(void);
+int BSP_SD_Get_TX_Busy(void);
 
 #ifdef __cplusplus
 }