To fix the hang problem

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Revision:
341:599aa20d0279
Parent:
328:2242ebc71be8
--- a/cdms_sd.h	Mon Jan 02 14:21:44 2017 +0000
+++ b/cdms_sd.h	Sat Jan 07 02:46:59 2017 +0000
@@ -1,7 +1,4 @@
-#ifndef CDMS_SD_INLCUDED
-#define CDMS_SD_INLCUDED
-
-//SPI spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)
+//SPI spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
 //DigitalOut cs_sd(PTE22);
 
 //Serial sd1(USBTX,USBRX);
@@ -9,7 +6,7 @@
 
 #define SD_COMMAND_TIMEOUT 325
 
-#define SD_DBG             1
+#define SD_DBG             0
 
 #define R1_IDLE_STATE           (1 << 0)
 #define R1_ERASE_RESET          (1 << 1)
@@ -19,9 +16,21 @@
 #define R1_ADDRESS_ERROR        (1 << 5)
 #define R1_PARAMETER_ERROR      (1 << 6)
 
-uint32_t SD_DATABLOCK_START[] = {0, 1001, 11001, 21001, 31001, 41001};
-uint32_t SD_DATABLOCK_END[] = {0, 11000, 21000, 31000, 41000, 51000};
-uint32_t SD_MNG_SECT=80000;
+
+
+#define SD_MAX_CYCLES 10000
+
+uint32_t SD_SCP_FIRST=1001;
+uint32_t SD_SCP_LAST=2000;
+uint32_t SD_SFF_AT_FIRST=2001;
+uint32_t SD_SFF_AT_LAST = 3000;
+uint32_t SD_SFF_BT_FIRST =3001;
+uint32_t SD_SFF_BT_LAST=4000;
+uint32_t SD_HK_ARCH_FIRST=4001;
+uint32_t SD_HK_ARCH_LAST= 5000;
+uint32_t LOG_FIRST =5001;
+uint32_t LOG_LAST=6000;
+uint32_t SD_MNG_SECT = 8000;
 
 extern uint8_t SD_INIT_FLAGS;
 
@@ -57,10 +66,10 @@
 uint64_t sectors;
 
 int FCTN_CDMS_SD_INIT()
-{
+{   
     int i = initialise_card();
     if( i == 4)
-        return 4;
+    return 4;
     debug_if(SD_DBG, "init card = %d\n", i);
     sectors = sd_sectors();
 
@@ -69,7 +78,7 @@
         debug("\rSet 512-byte block timed out\r\n");
         return 1;
     } else {
-        //printf("\rDisk initialization successfull\r\n");
+    //printf("\rDisk initialization successfull\r\n");
     }
     SD_STATUS = DEVICE_POWERED;
     spi.frequency(1000000); // Set to 1MHz for data transfer
@@ -79,59 +88,183 @@
 void  FCTN_SD_MNGR()
 {
     uint32_t fsc;
-    uint32_t fsc_old;
+    uint32_t start_fsc;
     uint8_t buffer[512];
     int b;
-    if(SD_STATUS == DEVICE_POWERED) {
-        b=disk_read(buffer, SD_MNG_SECT);
+    if(SD_STATUS == DEVICE_POWERED){
+    b=disk_read(buffer, SD_MNG_SECT);
+    
+    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;
 
-        for(int i = 0; i < 5; i++ ) {
-            fsc= (uint32_t)(buffer[0 + i*8]<<24)+(uint32_t)(buffer[1 + i*8]<<16)+(uint32_t)(buffer[2 + i*8]<<8)+(uint32_t)buffer[3 + i*8];
-            fsc_old= (uint32_t)(buffer[4 + i*8]<<24)+(uint32_t)(buffer[5 + i*8]<<16)+(uint32_t)(buffer[6 + i*8]<<8)+(uint32_t)buffer[7 + i*8];
-            FSC_CURRENT[1 + i] = fsc;
-            FSC_OLD[1 + i] = fsc_old;
-        }
+    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 fsc_old;
+    uint32_t start_fsc;
     int i;
     uint8_t buffer[512];
-
+    
     disk_read(buffer,SD_MNG_SECT);
     SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF;
-    if(SD_MNG_SECT != SD_LIB_BLK_CURRENT) {
+    if(SD_MNG_SECT != SD_LIB_BLK_CURRENT)
+    {
         SD_LIB_BLK_CURRENT = SD_MNG_SECT;
-        FCTN_CDMS_WR_FLASH(16,SD_LIB_BLK_CURRENT);
+        FCTN_CDMS_WR_FLASH(16,SD_LIB_BLK_CURRENT);    
     }
     SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
     disk_write(buffer,SD_MNG_SECT);
-
+ //   gPC.printf("in increment_SD_LIB = %d, %d,%d\n\r", FSC_CURRENT[1],FSC_CURRENT[2],FSC_CURRENT[3]);
+  //  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 && sid <=0x05)
+    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++;
+        buffer[0]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[1]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[2]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[3]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_SCP_LAST-SD_SCP_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[4]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[5]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[6]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[7]=(uint8_t) (start_fsc & 0xFF);
+        }
+        
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[1] = fsc;
+            FSC_LAST[1] = start_fsc;
+            return i;
+        } 
+    }
+    if(sid==0x02)
     {
-        fsc=(uint32_t)(buffer[0 + ((sid-1)*8)]<<24)+(uint32_t)(buffer[1 + ((sid-1)*8)]<<16)+(uint32_t)(buffer[2 + ((sid-1)*8)]<<8)+(uint32_t)buffer[3 + ((sid-1)*8)];
-        fsc_old=(uint32_t)(buffer[4 + ((sid-1)*8)]<<24)+(uint32_t)(buffer[5 + ((sid-1)*8)]<<16)+(uint32_t)(buffer[6 + ((sid-1)*8)]<<8)+(uint32_t)buffer[7 + ((sid-1)*8)];
+        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++;
+        buffer[8]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[9]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[10]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[11]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_SFF_AT_LAST-SD_SFF_AT_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[12]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[13]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[14]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[15]=(uint8_t) (start_fsc & 0xFF);
+        }
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[2] = fsc;
+            FSC_LAST[2] = start_fsc;
+            return i;
+        }
+    }
+    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++;
-        buffer[0 + ((sid-1)*8)]=(uint8_t) (fsc>>24 & 0xFF);
-        buffer[1 + ((sid-1)*8)]=(uint8_t) (fsc>>16 & 0xFF);
-        buffer[2 + ((sid-1)*8)]=(uint8_t) (fsc>>8 & 0xFF);
-        buffer[3 + ((sid-1)*8)]=(uint8_t) (fsc & 0xFF);
-        if(fsc > SD_DATABLOCK_END[sid]-SD_DATABLOCK_START[sid]+1) {
-            fsc_old = fsc_old+1;
-            buffer[4 + ((sid-1)*8)]=(uint8_t) (fsc_old>>24 & 0xFF);
-            buffer[5 + ((sid-1)*8)]=(uint8_t) (fsc_old>>16 & 0xFF);
-            buffer[6 + ((sid-1)*8)]=(uint8_t) (fsc_old>>8 & 0xFF);
-            buffer[7 + ((sid-1)*8)]=(uint8_t) (fsc_old & 0xFF);
+        buffer[16]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[17]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[18]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[19]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_SFF_BT_LAST-SD_SFF_BT_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[20]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[21]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[22]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[23]=(uint8_t) (start_fsc & 0xFF);
+        }
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[3] = fsc;
+            FSC_LAST[3] = start_fsc;
+            return i;
         }
-
+    }
+     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++;
+        buffer[24]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[25]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[26]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[27]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_HK_ARCH_LAST-SD_HK_ARCH_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[28]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[29]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[30]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[31]=(uint8_t) (start_fsc & 0xFF);
+        }
         i = disk_write(buffer,SD_MNG_SECT);
-        if(i == 0) {
-            FSC_CURRENT[sid] = fsc;
-            FSC_OLD[sid] = fsc_old;
+        if(i == 0)
+        {
+            FSC_CURRENT[4] = fsc;
+            FSC_LAST[4] = start_fsc;
+            return i;
+        }
+    }
+     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++;
+        buffer[32]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[33]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[34]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[35]=(uint8_t) (fsc & 0xFF);
+        if(fsc > LOG_LAST-LOG_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[36]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[37]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[38]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[39]=(uint8_t) (start_fsc & 0xFF);
+        }
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[5] = fsc;
+            FSC_LAST[5] = start_fsc;
             return i;
         }
     }
@@ -143,59 +276,69 @@
 {
     uint32_t block_number;
     int result = 10;
-    if(SD_STATUS == DEVICE_POWERED) {
-        if(sid==0x01) {
-            block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
-            //block_number=SD_DATABLOCK_START[1]+fsc;
-            result= disk_write(buffer,block_number);
-            if(result == 0) {
-                if(INCREMENT_SD_LIB(sid) == 0)
-                    SD_LIB_WRITES++;
-            }
-            return result;
+    if(SD_STATUS == DEVICE_POWERED){
+    if(sid==0x01)
+    {
+        block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1;
+        //block_number=SD_SCP_FIRST+fsc;
+        result= disk_write(buffer,block_number);
+        if(result == 0)
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
         }
-        if(sid==0x02) {
-            block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
-            //block_number= SD_DATABLOCK_START[2]+fsc;
-            result= disk_write(buffer,block_number);
-            if(result == 0) {
-                if(INCREMENT_SD_LIB(sid) == 0)
-                    SD_LIB_WRITES++;
-            }
-            return result;
+        return result;
+    }
+   if(sid==0x02)
+    {
+        block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1;
+        //block_number= SD_SFF_AT_FIRST+fsc;
+        result= disk_write(buffer,block_number);
+        if(result == 0)
+         {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
         }
-        if(sid==0x03) {
-            block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
-            //block_number= SD_DATABLOCK_START[3] +fsc;
-            result= disk_write(buffer,block_number);
-            if(result == 0) {
-                if(INCREMENT_SD_LIB(sid) == 0)
-                    SD_LIB_WRITES++;
-            }
-            return result;
+        return result;
+    }
+     if(sid==0x03)
+    {
+        block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1;
+        //block_number= SD_SFF_BT_FIRST +fsc;
+        result= disk_write(buffer,block_number);
+        if(result == 0)
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
         }
-        if(sid==0x04) {
-            block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
-            //block_number=SD_DATABLOCK_START[4] +fsc;
-            result= disk_write(buffer,block_number);
-            if(result == 0) {
-                if(INCREMENT_SD_LIB(sid) == 0)
-                    SD_LIB_WRITES++;
-            }
-            return result;
+        return result;
+    }
+     if(sid==0x04)
+    {
+        block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1;
+        //block_number=SD_HK_ARCH_FIRST +fsc;
+        result= disk_write(buffer,block_number);
+        if(result == 0)
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
         }
-        if(sid==0x05) {
-            block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
-            //block_number= SD_DATABLOCK_START[5] +fsc;
-            result= disk_write(buffer,block_number);
-            if(result == 0) {
-                if(INCREMENT_SD_LIB(sid) == 0)
-                    SD_LIB_WRITES++;
-            }
-            return result;
+        return result;
+    }
+     if(sid==0x05)
+    {
+        block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
+        //block_number= LOG_FIRST +fsc;
+        result= disk_write(buffer,block_number);
+        if(result == 0)
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
         }
+        return result;
     }
-    // return 1;
+    }
+   // return 1;
 }
 
 uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
@@ -205,37 +348,48 @@
     int result;
     //if(SD_SW_EN_DS == 1)
     //    return 0x89;
-    if(sid==0x01) {
-        if(!(FSC_OLD[1]<=fsc && fsc<=FSC_CURRENT[1])) {
+    if(sid==0x01)
+    {
+        if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])){
             return 0x86;
         }
-        block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
+        block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1;
         result= disk_read(buffer,block_number);
-    } else if(sid==0x02) {
-        if(!(FSC_OLD[2]<=fsc && fsc<=FSC_CURRENT[2])) {
+    }
+   else if(sid==0x02)
+    {
+        if(!(FSC_LAST[2]<=fsc && fsc<=FSC_CURRENT[2])){
             return 0x86;
         }
-        block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
+        block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1;
         result= disk_read(buffer,block_number);
-    } else if(sid==0x03) {
-        if(!(FSC_OLD[3]<=fsc && fsc<=FSC_CURRENT[3])) {
+    }
+    else if(sid==0x03)
+    {
+        if(!(FSC_LAST[3]<=fsc && fsc<=FSC_CURRENT[3])){
             return 0x86;
         }
-        block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
+        block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1;
         result= disk_read(buffer,block_number);
-    } else if(sid==0x04) {
-        if(!(FSC_OLD[4]<=fsc && fsc<=FSC_CURRENT[4])) {
+    }
+    else if(sid==0x04)
+    {
+        if(!(FSC_LAST[4]<=fsc && fsc<=FSC_CURRENT[4])){
             return 0x86;
         }
-        block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
+        block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1;
         result= disk_read(buffer,block_number);
-    } else if(sid==0x05) {
-        if(!(FSC_OLD[5]<=fsc && fsc<=FSC_CURRENT[5])) {
+    }
+    else if(sid==0x05)
+    {
+        if(!(FSC_LAST[5]<=fsc && fsc<=FSC_CURRENT[5])){
             return 0x86;
         }
-        block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
+        block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
         result= disk_read(buffer,block_number);
-    } else {
+    }
+    else
+    {
         return 0x02;
     }
     if(result == 0)
@@ -249,7 +403,7 @@
 int initialise_card()
 {
     // Set to 100kHz for initialisation, and clock card with cs_sd = 1
-    spi.frequency(100000);           // changed on 31 12 2015 to 1 MHz
+    spi.frequency(100000);           // changed on 31 12 2015 to 1 MHz 
     cs_sd = 1;
     for (int i = 0; i < 16; i++) {
         spi.write(0xFF);
@@ -260,8 +414,9 @@
     if (R1_response != R1_IDLE_STATE) {
         debug("No disk, or could not put SD card in to spi idle state\r\n");
         return SDCARD_FAIL;
-    } else
-        gPC.puts("SD Card is in IDLE state\n\r");
+    }
+    else
+        gPC.puts("SD Card is in IDLE state\n\r");    
 
     // send CMD8 to determine whther it is ver 2.x
     int r = cmd8();
@@ -508,10 +663,10 @@
         CDMS_WR_SD_FAULT_COUNTER++;
         return 1;
     }
-
+    
     uint64_t temp;
     int r = write(buffer, 512);
-    if(r == 0 ) {
+    if(r == 0 ){
         temp = FCTN_CDMS_RD_RTC();
         TIME_LATEST_SD_WR = temp >> 7; //corrected by samp:TIME_LATEST_SD_WRD = temp >> 7;
     }
@@ -595,19 +750,17 @@
     if (cmd(38,0) != 0) {
         return 1;
     }
-
+    
     return 0; //normal return
 }
 
 int disk_read_statusbits(uint8_t *buffer)
 {
     if (cmd(17, 0) != 0) {
-        SD_RD_ERROR = 1;
+          SD_RD_ERROR = 1;
         return -1;
     }
 
     // receive the data
     return read(buffer,64);
-}
-
-#endif
\ No newline at end of file
+}
\ No newline at end of file