To fix the hang problem

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Revision:
206:fba4aeebf004
Parent:
188:5f75962ae7ea
Child:
207:28a07943dded
--- a/cdms_sd.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/cdms_sd.h	Thu Jun 30 14:00:33 2016 +0000
@@ -1,6 +1,4 @@
-
-
-
+#include "cdms_rtc.h"
 //SPI spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
 //DigitalOut cs_sd(PTE22);
 
@@ -34,6 +32,7 @@
 uint32_t LOG_FIRST =5001;
 uint32_t LOG_LAST=6000;
 uint32_t SD_MNG_SECT=7000;
+uint16_t SD_LIB_WRITES = 0;
 
 extern uint8_t SD_INIT_FLAGS;
 
@@ -44,7 +43,7 @@
 int disk_read(uint8_t *, uint64_t);
 int disk_erase(int,int);
 
-uint32_t FCTN_SD_MNG(uint8_t);
+void FCTN_SD_MNG();
 int INCREMENT_SD_LIB(uint8_t);
 
 
@@ -57,12 +56,12 @@
 static uint32_t ext_bits(unsigned char *, int , int );
 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
 int FCTN_CDMS_SD_INIT();
-int SD_READ(uint8_t*,uint32_t,uint8_t);
+uint8_t SD_READ(uint8_t*,uint32_t,uint8_t);
 #define SDCARD_FAIL 0
 #define SDCARD_V1   1
 #define SDCARD_V2   2
 #define SDCARD_V2HC 3
-
+DigitalOut SD_SW_EN_DS (PIN97);
 
 int cdv;
 uint64_t sd_sectors();
@@ -86,57 +85,47 @@
     return 0;
 }
 
-    
-
-uint32_t  FCTN_SD_MNGR(uint8_t sid)
+void  FCTN_SD_MNGR()
 {
     uint32_t fsc;
     uint32_t start_fsc;
     uint8_t buffer[512];
     int b=disk_read(buffer, SD_MNG_SECT);
-    if(sid==0x01)
-    {
-        fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
-        start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
-        FSC_CURRENT[1] = fsc;
-        FSC_LAST[1] = start_fsc;
-    }
-    if(sid==0x02)
-    {
-        fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
-        start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
-        FSC_CURRENT[2] = fsc;
-        FSC_LAST[2] = start_fsc;
-    }
-    if(sid==0x03)
-    {
-        fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
-        start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
-        FSC_CURRENT[3] = fsc;
-        FSC_LAST[3] = start_fsc;
-    }
-    if(sid==0x04)
-    {
-        fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27];
-        start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31];
-        FSC_CURRENT[4] = fsc;
-        FSC_LAST[4] = start_fsc;
-    }
-    if(sid==0x05)
-    {
-        fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35];
-        start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39];
-        FSC_CURRENT[5] = fsc;
-        FSC_LAST[5] = start_fsc;
-    }
-    return fsc;
+    
+    fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
+    start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
+    FSC_CURRENT[1] = fsc;
+    FSC_LAST[1] = start_fsc;
+
+    fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
+    start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
+    FSC_CURRENT[2] = fsc;
+    FSC_LAST[2] = start_fsc;
+
+    fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
+    start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
+    FSC_CURRENT[3] = fsc;
+    FSC_LAST[3] = start_fsc;
+
+    fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27];
+    start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31];
+    FSC_CURRENT[4] = fsc;
+    FSC_LAST[4] = start_fsc;
+
+    fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35];
+    start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39];
+    FSC_CURRENT[5] = fsc;
+    FSC_LAST[5] = start_fsc;
 }
 
 int INCREMENT_SD_LIB(uint8_t sid)
 {
     uint32_t fsc;
     uint32_t start_fsc;
+    int i;
     uint8_t buffer[512];
+    SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF;
+    SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
     disk_read(buffer,SD_MNG_SECT);
     if(sid==0x01)
     {
@@ -242,7 +231,10 @@
         block_number=SD_SCP_FIRST+fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
    if(sid==0x02)
@@ -251,7 +243,10 @@
         block_number= SD_SFF_AT_FIRST+fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+         {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
      if(sid==0x03)
@@ -260,7 +255,10 @@
         block_number= SD_SFF_BT_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
      if(sid==0x04)
@@ -269,7 +267,10 @@
         block_number=SD_HK_ARCH_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
      if(sid==0x05)
@@ -278,7 +279,10 @@
         block_number= LOG_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
     return 1;
@@ -286,11 +290,11 @@
 
 uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
 {
-    FCTN_SD_MNGR(sid);
+    FCTN_SD_MNGR();
+    uint32_t block_number;
+    int result;
     if(SD_SW_EN_DS == 1)
         return 0x89;
-    uint32_t block_number;
-    int result;
     if(sid==0x01)
     {
         if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])){
@@ -604,11 +608,14 @@
     if (cmd(24, block_number * cdv) != 0) {
         return 1;
     }
-
-    // send the data block
-    write(buffer, 512);
-    //printf("Written Successfully bro \n");
-    return 0;
+    
+    uint64_t temp;
+    int r = write(buffer, 512);
+    if(r == 0 ){
+        temp = FCTN_CDMS_RD_RTC();
+        TIME_LATEST_SD_RD = temp >> 7;
+    }
+    return  r;
 }
 
 int write(const uint8_t*buffer, uint32_t length)
@@ -651,6 +658,8 @@
 
     // receive the data
     read(buffer, 512);
+    uint64_t temp = FCTN_CDMS_RD_RTC();
+    TIME_LATEST_SD_RD = temp >> 7;
     return 0;
 }