To fix the hang problem

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Files at this revision

API Documentation at this revision

Comitter:
chaithanyarss
Date:
Fri Dec 16 08:53:12 2016 +0000
Parent:
326:e424f70b86c0
Child:
328:2242ebc71be8
Commit message:
Working on optimizing SD card code

Changed in this revision

CDMS_HK.h Show annotated file Show diff for this revision Revisions of this file
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
adf.h Show annotated file Show diff for this revision Revisions of this file
cdms_sd.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CDMS_HK.h	Sat Dec 10 16:15:40 2016 +0000
+++ b/CDMS_HK.h	Fri Dec 16 08:53:12 2016 +0000
@@ -575,23 +575,23 @@
     for(int i = 0; i<4; i++)
         CDMS_RAM[43+i] = FSC_CURRENT[1] >> (3-i)*8;
     for(int i = 0; i<4; i++)
-        CDMS_RAM[47+i] = FSC_LAST[1] >> (3-i)*8;
+        CDMS_RAM[47+i] = FSC_OLD[1] >> (3-i)*8;
     for(int i = 0; i<4; i++)
         CDMS_RAM[51+i] = FSC_CURRENT[2] >> (3-i)*8;
     for(int i = 0; i<4; i++)
-        CDMS_RAM[55+i] = FSC_LAST[2] >> (3-i)*8;
+        CDMS_RAM[55+i] = FSC_OLD[2] >> (3-i)*8;
     for(int i = 0; i<4; i++)
         CDMS_RAM[59+i] = FSC_CURRENT[3] >> (3-i)*8;
     for(int i = 0; i<4; i++)
-        CDMS_RAM[63+i] = FSC_LAST[3] >> (3-i)*8;
+        CDMS_RAM[63+i] = FSC_OLD[3] >> (3-i)*8;
     for(int i = 0; i<4; i++)
         CDMS_RAM[67+i] = FSC_CURRENT[4] >> (3-i)*8;
     for(int i = 0; i<4; i++)
-        CDMS_RAM[71+i] = FSC_LAST[4] >> (3-i)*8;
+        CDMS_RAM[71+i] = FSC_OLD[4] >> (3-i)*8;
     for(int i = 0; i<4; i++)
         CDMS_RAM[75+i] = FSC_CURRENT[5] >> (3-i)*8;
     for(int i = 0; i<4; i++)
-        CDMS_RAM[79+i] = FSC_LAST[5] >> (3-i)*8;
+        CDMS_RAM[79+i] = FSC_OLD[5] >> (3-i)*8;
     CDMS_RAM[83] = 0x00;
     gPC.printf("\n\r%d %d %d %d %d",FSC_CURRENT[1],FSC_CURRENT[2],FSC_CURRENT[3],FSC_CURRENT[4],FSC_CURRENT[5]);
 }
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Sat Dec 10 16:15:40 2016 +0000
+++ b/DefinitionsAndGlobals.h	Fri Dec 16 08:53:12 2016 +0000
@@ -314,7 +314,7 @@
 
 //SD HK
 uint32_t FSC_CURRENT[6];
-uint32_t FSC_LAST[6];
+uint32_t FSC_OLD[6];
 uint8_t SD_RD_ERROR;
 
 //Time's
--- a/adf.h	Sat Dec 10 16:15:40 2016 +0000
+++ b/adf.h	Fri Dec 16 08:53:12 2016 +0000
@@ -11,7 +11,7 @@
 bool buffer_state;
 bool finish_write_data;
 uint8_t signal = 0x00;
-unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xF4,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,/*PA Level Byte*/0x57/*PA Parameters*/,0x00,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x20,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xE0,0x00,0x10,0x04,0x00,0x00,0x00,0x00,0x00};
+unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xFB,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,/*PA Level Byte*/0x57/*PA Parameters*/,0x00,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x20,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xE0,0x00,0x10,0x04,0x00,0x00,0x00,0x00,0x00};
 bool data_irq_err=0;
 bool data_err= false;
 unsigned char temp_byte=0x00;
--- a/cdms_sd.h	Sat Dec 10 16:15:40 2016 +0000
+++ b/cdms_sd.h	Fri Dec 16 08:53:12 2016 +0000
@@ -1,4 +1,4 @@
-//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);
@@ -16,21 +16,10 @@
 #define R1_ADDRESS_ERROR        (1 << 5)
 #define R1_PARAMETER_ERROR      (1 << 6)
 
-
-
-#define SD_MAX_CYCLES 10000
+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_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;
+uint32_t SD_MNG_SECT=80000;
 
 extern uint8_t SD_INIT_FLAGS;
 
@@ -66,10 +55,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();
 
@@ -78,7 +67,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
@@ -88,183 +77,146 @@
 void  FCTN_SD_MNGR()
 {
     uint32_t fsc;
-    uint32_t start_fsc;
+    uint32_t fsc_old;
     uint8_t buffer[512];
     int b;
-    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;
+    if(SD_STATUS == DEVICE_POWERED) {
+        b=disk_read(buffer, SD_MNG_SECT);
 
-    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;
+        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;
+        }
     }
 }
 
 int INCREMENT_SD_LIB(uint8_t sid)
 {
     uint32_t fsc;
-    uint32_t start_fsc;
+    uint32_t fsc_old;
     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)
-    {
+    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_old=(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);
+        if(fsc > SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1) {
+            fsc_old = fsc_old+1;
+            buffer[4]=(uint8_t) (fsc_old>>24 & 0xFF);
+            buffer[5]=(uint8_t) (fsc_old>>16 & 0xFF);
+            buffer[6]=(uint8_t) (fsc_old>>8 & 0xFF);
+            buffer[7]=(uint8_t) (fsc_old & 0xFF);
         }
-        
+
         i = disk_write(buffer,SD_MNG_SECT);
-        if(i == 0)
-        {
+        if(i == 0) {
             FSC_CURRENT[1] = fsc;
-            FSC_LAST[1] = start_fsc;
+            FSC_OLD[1] = fsc_old;
             return i;
-        } 
+        }
     }
-    if(sid==0x02)
-    {
+    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_old=(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);
+        if(fsc > SD_DATABLOCK_END[2]-SD_DATABLOCK_START[2]+1) {
+            fsc_old = fsc_old+1;
+            buffer[12]=(uint8_t) (fsc_old>>24 & 0xFF);
+            buffer[13]=(uint8_t) (fsc_old>>16 & 0xFF);
+            buffer[14]=(uint8_t) (fsc_old>>8 & 0xFF);
+            buffer[15]=(uint8_t) (fsc_old & 0xFF);
         }
         i = disk_write(buffer,SD_MNG_SECT);
-        if(i == 0)
-        {
+        if(i == 0) {
             FSC_CURRENT[2] = fsc;
-            FSC_LAST[2] = start_fsc;
+            FSC_OLD[2] = fsc_old;
             return i;
         }
     }
-    if(sid==0x03)
-    {
+    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_old=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
         fsc++;
         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);
+        if(fsc > SD_DATABLOCK_END[3]-SD_DATABLOCK_START[3]+1) {
+            fsc_old = fsc_old+1;
+            buffer[20]=(uint8_t) (fsc_old>>24 & 0xFF);
+            buffer[21]=(uint8_t) (fsc_old>>16 & 0xFF);
+            buffer[22]=(uint8_t) (fsc_old>>8 & 0xFF);
+            buffer[23]=(uint8_t) (fsc_old & 0xFF);
         }
         i = disk_write(buffer,SD_MNG_SECT);
-        if(i == 0)
-        {
+        if(i == 0) {
             FSC_CURRENT[3] = fsc;
-            FSC_LAST[3] = start_fsc;
+            FSC_OLD[3] = fsc_old;
             return i;
         }
     }
-     if(sid==0x04)
-    {
+    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_old=(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);
+        if(fsc > SD_DATABLOCK_END[4]-SD_DATABLOCK_START[4]+1) {
+            fsc_old = fsc_old+1;
+            buffer[28]=(uint8_t) (fsc_old>>24 & 0xFF);
+            buffer[29]=(uint8_t) (fsc_old>>16 & 0xFF);
+            buffer[30]=(uint8_t) (fsc_old>>8 & 0xFF);
+            buffer[31]=(uint8_t) (fsc_old & 0xFF);
         }
         i = disk_write(buffer,SD_MNG_SECT);
-        if(i == 0)
-        {
+        if(i == 0) {
             FSC_CURRENT[4] = fsc;
-            FSC_LAST[4] = start_fsc;
+            FSC_OLD[4] = fsc_old;
             return i;
         }
     }
-     if(sid==0x05)
-    {
+    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_old=(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);
+        if(fsc > SD_DATABLOCK_END[5]-SD_DATABLOCK_START[5]+1) {
+            fsc_old = fsc_old+1;
+            buffer[36]=(uint8_t) (fsc_old>>24 & 0xFF);
+            buffer[37]=(uint8_t) (fsc_old>>16 & 0xFF);
+            buffer[38]=(uint8_t) (fsc_old>>8 & 0xFF);
+            buffer[39]=(uint8_t) (fsc_old & 0xFF);
         }
         i = disk_write(buffer,SD_MNG_SECT);
-        if(i == 0)
-        {
+        if(i == 0) {
             FSC_CURRENT[5] = fsc;
-            FSC_LAST[5] = start_fsc;
+            FSC_OLD[5] = fsc_old;
             return i;
         }
     }
@@ -276,69 +228,59 @@
 {
     uint32_t block_number;
     int result = 10;
-    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(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(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==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;
+        }
+        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;
+        }
     }
-     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++;
-        }
-        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++;
-        }
-        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)
@@ -348,48 +290,37 @@
     int result;
     //if(SD_SW_EN_DS == 1)
     //    return 0x89;
-    if(sid==0x01)
-    {
-        if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])){
+    if(sid==0x01) {
+        if(!(FSC_OLD[1]<=fsc && fsc<=FSC_CURRENT[1])) {
             return 0x86;
         }
-        block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1;
+        block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
         result= disk_read(buffer,block_number);
-    }
-   else if(sid==0x02)
-    {
-        if(!(FSC_LAST[2]<=fsc && fsc<=FSC_CURRENT[2])){
+    } else if(sid==0x02) {
+        if(!(FSC_OLD[2]<=fsc && fsc<=FSC_CURRENT[2])) {
             return 0x86;
         }
-        block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1;
+        block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
         result= disk_read(buffer,block_number);
-    }
-    else if(sid==0x03)
-    {
-        if(!(FSC_LAST[3]<=fsc && fsc<=FSC_CURRENT[3])){
+    } else if(sid==0x03) {
+        if(!(FSC_OLD[3]<=fsc && fsc<=FSC_CURRENT[3])) {
             return 0x86;
         }
-        block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1;
+        block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
         result= disk_read(buffer,block_number);
-    }
-    else if(sid==0x04)
-    {
-        if(!(FSC_LAST[4]<=fsc && fsc<=FSC_CURRENT[4])){
+    } else if(sid==0x04) {
+        if(!(FSC_OLD[4]<=fsc && fsc<=FSC_CURRENT[4])) {
             return 0x86;
         }
-        block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1;
+        block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
         result= disk_read(buffer,block_number);
-    }
-    else if(sid==0x05)
-    {
-        if(!(FSC_LAST[5]<=fsc && fsc<=FSC_CURRENT[5])){
+    } else if(sid==0x05) {
+        if(!(FSC_OLD[5]<=fsc && fsc<=FSC_CURRENT[5])) {
             return 0x86;
         }
-        block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
+        block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
         result= disk_read(buffer,block_number);
-    }
-    else
-    {
+    } else {
         return 0x02;
     }
     if(result == 0)
@@ -403,7 +334,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);
@@ -414,9 +345,8 @@
     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();
@@ -663,10 +593,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;
     }
@@ -750,14 +680,14 @@
     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;
     }
 
--- a/main.cpp	Sat Dec 10 16:15:40 2016 +0000
+++ b/main.cpp	Fri Dec 16 08:53:12 2016 +0000
@@ -88,7 +88,7 @@
     TIME_LATEST_CDSMS_RESET = FCTN_CDMS_RD_RTC() >> 7;
     FCTN_CDMS_WR_FLASH(8,TIME_LATEST_CDSMS_RESET);
     
-    SD_MNG_SECT = SD_LIB_BLK_CURRENT;
+    //SD_MNG_SECT = SD_LIB_BLK_CURRENT;
     FCTN_SD_MNGR();