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:
samp1234
Date:
Fri Jan 13 08:00:33 2017 +0000
Parent:
342:77b0d59897f7
Child:
344:2517c9f6bb4f
Commit message:
HK SD write moved to Science, Timer restart in science thread, SD_write of compressed data commented, PL_GPIO1_Status (pin71) used for signalling PL for DMA data transfer.

Changed in this revision

CDMS_HK.h Show annotated file Show diff for this revision Revisions of this file
Compression.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
ThreadsAndFunctions.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
i2c.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	Thu Jan 05 17:38:21 2017 +0000
+++ b/CDMS_HK.h	Fri Jan 13 08:00:33 2017 +0000
@@ -52,11 +52,14 @@
         gHK_THREAD->signal_wait(HK_SIGNAL);
         gPC.printf("checking mutex in HK\n\r");
         gMutex.lock();
+      SCI_LED1 =1;
         gPC.printf("locked mutex, entered in HK \n\r");
         if(hk_count == 1 || hk_count == 2) {
             FCTN_CDMS_PL_MAIN((void const *)NULL);
             hk_count--;
             gPC.printf("releasing mutex for HK_PL \n\r");
+           SCI_LED1 =0;
+           PL_GPIO_1_STATUS=1;
             gMutex.unlock();
             continue;
         } else if(hk_count == 0) {
@@ -70,7 +73,9 @@
         CDMS_HK_MAIN_STATUS = 0x01;
         CDMS_HK_MAIN_COUNTER++;
 
+       
         FCTN_CDMS_HK();         //collects temperatures
+       
         if(!COM_RSSI_SET)
         {
             RSSI_VOLTAGE = COMRX_RSSI_VOLTAGE.read() * 3.3;//to be checked
@@ -126,8 +131,13 @@
 
         gPC.printf("\t\t%d/%d/%d  ",((time & 0x003E0000)>>17),((time & 0x03C00000)>>22),((time & 0x0C000000)>>26)+2016);
         gPC.printf("%d:%d:%d\n",((time & 0x0001F000)>>12),((time & 0x00000FC0)>>6),(time & 0x0000003F));
-
-        FCTN_SD_MNGR();                                 //Adding FSC & TMID to TM frame
+       if(CDMS_HK_SD_DATA_READY==1||BAE_HK_SD_DATA_READY==1) 
+        {
+        FCTN_SD_MNGR();
+        gPC.printf("endHK_SD");  
+        }
+  //      gPC.printf("endHK_SD");                                 //Adding FSC & TMID to TM frame
+       
         CDMS_HK_FRAME[0] = 0x20;
         CDMS_HK_FRAME[1] = (FSC_CURRENT[4]+1) >> 16;
         CDMS_HK_FRAME[2] = (FSC_CURRENT[4]+1) >> 8;
@@ -149,21 +159,31 @@
         interleave(convoluted_CDMS_HK ,  interleave_CDMS_HK);
         interleave(convoluted_CDMS_HK +135, interleave_CDMS_HK + 144);
         //gPC.printf("\n\r reached here");
+     //   gPC.printf("enterdHK_SD");
+        
+        if(CDMS_HK_SD_DATA_READY==1)
+        {
         for(int i=0; i<288; i++)
             CDMS_HEALTH_FINAL[i] = interleave_CDMS_HK[i];
         //gPC.printf("\n\r reached here");
         sd_stat = SD_WRITE(CDMS_HEALTH_FINAL,FSC_CURRENT[4]+1,4);
+        
         if(sd_stat) {
             gPC.puts("sd write failure $*&^@!~!");
-            // break;
+        }
         }
+//        }
+        CDMS_HK_SD_DATA_READY=1;    
+        
         // gPC.printf("Completed CDMS HK\t");
 
         /*---------------------------------- BAE HK --------------------------------------------*/
-
+        
+    //    gPC.printf("endHK_SD2");
         BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134);
-        //  gPC.printf("Entering BAE HK\t");
+        //  gPC.printf("\nEntering BAE HK\t");
         if(BAE_HK_I2C == 0) {
+     //  gPC.printf("BAE_HK_");
             crc = crc16_gen((unsigned char *)BAE_HK,132);
             if(crc == ((uint16_t)BAE_HK[132] << 8) | (uint16_t)BAE_HK[133]) {
                 //gPC.printf("BAE HK data recieved through I2C\t");
@@ -186,13 +206,22 @@
                 BAE_HEALTH.convolutionEncode(BAE_HK_FRAME + 67, convoluted_BAE_HK + 135);
                 interleave(convoluted_BAE_HK ,  interleave_BAE_HK);
                 interleave(convoluted_BAE_HK +135, interleave_BAE_HK + 144);
+               
+     //           gPC.printf("enterHK_SD3");
+                
+                if(BAE_HK_SD_DATA_READY==1)
+                {
+        
                 for(int i=0; i<288; i++)
                     BAE_HEALTH_FINAL[i] = interleave_BAE_HK[i];
                 sd_stat = SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5);
                 if(sd_stat) {
-                    gPC.puts("sd write failure");
+                    gPC.puts("BAE_HK sd write fail");
                     //break;
                 }
+                }
+                BAE_HK_SD_DATA_READY = 1;
+    //            gPC.printf("endHK_SD4");
             }
 
         } else {
@@ -249,7 +278,8 @@
             gPC.printf("long Bcn not sent\r\n");
         //gPC.printf("\rCompleted Beacon\n");
         gPC.printf("\rreleasing mutex for HK_MAIN \n\r");
-        gMutex.unlock();
+      SCI_LED1 =0;
+       gMutex.unlock();
     }
 }
 
--- a/Compression.h	Thu Jan 05 17:38:21 2017 +0000
+++ b/Compression.h	Fri Jan 13 08:00:33 2017 +0000
@@ -27,6 +27,9 @@
 #define debug_dma 0     //byte reading order in dma, used to read test_cases 4 to 16 since they're in the wrong order
 #define test_science 0
 #define tabulation 0
+uint8_t HK_timer_toggle = 0;
+
+Timer timer_test;
 
 #if debug_dma
 uint16_t read_2byte(uint8_t* ptr)
@@ -1355,8 +1358,21 @@
 //give the pointer of 6 second data to this function
 void srp(uint8_t * head)//void const *args)
 {
+    //timer_test.start();
     uint8_t sd_stat = 0;
-    SCI_LED1 =1;
+//    SCI_LED1 =1;
+   
+   /****** added by samp to HK occurance *********/
+   if (HK_timer_toggle==0)
+   {
+   HK_counter->stop();
+   wait_ms(0.1);
+   HK_counter->start(10000);
+   }
+   HK_timer_toggle = !HK_timer_toggle;
+   
+   /****** added by samp to HK occurance *********/
+   
     // SCI_LED1 = !SCI_LED1;
     //gPC.printf("\n\rsrp");
     debug_cntr = 0;
@@ -1368,8 +1384,15 @@
     #endif
     if(time_prev_scp==0){
         time_prev_scp = sci_time;
-    }    
-    FCTN_SD_MNGR();                                                             ///changed recently
+    } 
+ //   gPC.printf("enterdSCI_SD7");   
+ //  gPC.printf("timer start %d::",HK_counter.read_ms());  
+ 
+      FCTN_SD_MNGR();     
+   
+  //  gPC.printf("timer end %d::",HK_counter.read_ms());                                                    ///changed recently
+   // timer_test.stop();
+//    gPC.printf("endSCI_SD8");
     #if tabulation
     if(head[0]==0 & head[1]==0){
         position_tm_frame[1] = position_tm_starting[1];
@@ -1854,18 +1877,27 @@
                 convolution(frames[id]);
                 interleave(TM_convoluted_data,TM_interleave_data);
                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
+  //              gPC.printf("enterSCI_SD11");
+                
+  //              gPC.printf("timer start %d::",HK_counter);
                 if(id == 1)
                 {
-                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
+             //       sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
                 }
                 else if (id == 2)
                 {
-                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
+          //          sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
                 }
                 if(sd_stat)
                 {
                     gPC.printf("\n\n\rsd write fail---------------------------------------");
                 }
+                
+             //  gPC.printf("timer end %d::",timer_test.read_ms());                                                    ///changed recently
+             //    timer_test.stop();
+              
+             
+ //              gPC.printf("endSCI_SD12");
                 position_tm_frame[id] = position_tm_starting[id];
                 if(j!=0)
                     frames[id][6-id] = (length - j)+position_tm_starting[id];
@@ -2745,8 +2777,9 @@
             convolution(frames[id]);
             interleave(TM_convoluted_data,TM_interleave_data);
             interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
-
-            sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
+            gPC.printf("endSCI_SD13");
+      //      sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
+            gPC.printf("endSCI_SD14");
             if(sd_stat)
             {
                 gPC.puts("sd write fail");
@@ -2805,7 +2838,7 @@
     #endif
     gPC.printf("End of SRP function\n\r"); 
     //SCI_LED1 = !SCI_LED1;
-    SCI_LED1 = 0;
+ //   SCI_LED1 = 0;
 }
 /*void test_sci_main(){
     gPAYLOAD_BUFFER[2] &= (~0x01);  //calibrated mode data generation
--- a/DefinitionsAndGlobals.h	Thu Jan 05 17:38:21 2017 +0000
+++ b/DefinitionsAndGlobals.h	Fri Jan 13 08:00:33 2017 +0000
@@ -268,6 +268,8 @@
 float RSSI_VOLTAGE;
 uint8_t COM_ADF_TMP = 0;
 uint8_t COM_PA_IC_TMP = 0;
+uint8_t BAE_HK_SD_DATA_READY = 1;
+uint8_t CDMS_HK_SD_DATA_READY = 1;
 
 CDMS_HK_actual actual_data;
 CDMS_HK_quant quant_data;
@@ -286,7 +288,7 @@
 DigitalIn COMRX_OC_FAULT (PIN68);
 //DigitalIn COMTX_OC_FAULT (PIN69);
 DigitalIn BAE_OC_FAULT (PIN92);
-DigitalOut PL_GPIO_1_STATUS (PIN71); // PYLD EPS CNTRL Signal1 / PYLD_EPS_CNTRL_SIG1
+DigitalOut PL_GPIO_1_STATUS (PIN71,0); // PYLD EPS CNTRL Signal1 / PYLD_EPS_CNTRL_SIG1
 DigitalOut PL_GPIO_2_STATUS (PIN81);//PYLD EPS CNTRL 2 / PYLD_EPS_CNTRL_2
 DigitalOut PL_GPIO_3_STATUS (PIN80);// PYLD EPS CNTRL 3 / PYLD_EPS_CNTRL_3
 DigitalIn PL_BEE_SW_OC_FAULT (PIN91);
--- a/ThreadsAndFunctions.h	Thu Jan 05 17:38:21 2017 +0000
+++ b/ThreadsAndFunctions.h	Fri Jan 13 08:00:33 2017 +0000
@@ -3,13 +3,13 @@
 
 #include "COM_MNG_TMTC.h"
 #include "adf.h"
-
 #include "dmaSPIslave.h"
 
 dmaSPISlave *gPAY_SPI;
 
 void payload_isr_fun(){
     gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
+  
 }
 
 void payload_isr_fun_dma(void const *args ){
@@ -28,9 +28,13 @@
     while(true){
         gPC.printf("waiting for Sci sig\n\r");
         gSCIENCE_THREAD->signal_wait(SCIENCE_SIGNAL);
+         
         gPC.printf("checking mutex in Sci\n\r");
         gMutex.lock();
+        
         gPC.printf("locked mutex,entered in Sci thread\n\r"); 
+  
+        DMA_LED1 = 1;
         PL_RCV_SC_DATA_STATUS = 1;
         //gPC.puts("sig_set\r\n");
         //for(int i =0; i<PAYLOAD_BUFFER_LENGTH; i++)
@@ -41,10 +45,10 @@
         //wait(2);/*timeout to be decided*/
         if(dma)
         {
-            TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;   
+     //       TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;   
             PL_RCV_SC_DATA_COUNTER++;
             gPAY_SPI->bulkRead_start();
-            DMA_LED1 = !DMA_LED1; // added by samp
+         //     DMA_LED1 = !DMA_LED1; // added by samp
             //gPAY_SPI->bulkRead_pause();
             //gPAY_SPI->bulkRead_resume(&payload_isr_fun);
         }
@@ -52,9 +56,35 @@
         
         srp(gPAYLOAD_BUFFER);//skip one apcket when cdms resets
     
+      uint8_t sd_stat1 = 0;
+        if(BAE_HK_SD_DATA_READY==1)
+                {
+                for(int i=0; i<288; i++)
+                    BAE_HEALTH_FINAL[i] = interleave_BAE_HK[i];
+                sd_stat1 = SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5);
+                if(sd_stat1) {
+                    gPC.puts("BAE_HK sd write fail");
+                    //break;
+                }
+                
+                }
+                BAE_HK_SD_DATA_READY = 0;
+        if(CDMS_HK_SD_DATA_READY==1)
+        {
+        for(int i=0; i<288; i++)
+            CDMS_HEALTH_FINAL[i] = interleave_CDMS_HK[i];
+        //gPC.printf("\n\r reached here");
+        sd_stat1 = SD_WRITE(CDMS_HEALTH_FINAL,FSC_CURRENT[4]+1,4);
+        
+        if(sd_stat1) {
+            gPC.puts("sd write failure $*&^@!~!");
+        }
+        }
+        CDMS_HK_SD_DATA_READY= 0;
         /*else
         gPC.printf("\n\rDMA error");*/
         gPC.printf("\r\nreleasing mutex for Sci\n\r");
+        DMA_LED1 =0;
         gMutex.unlock(); 
     }
 }
@@ -62,7 +92,9 @@
 //HK Thread(Contains HK_main() and PL_main())
 //RTOS timer calllback func
 void hk_isr(void const *args){
+    //gPC.printf("\r\n HK isr \n\r");
     gHK_THREAD->signal_set(HK_SIGNAL);
+    // gPC.printf("\r\n HK 1 \n\r");
 }
 
 
@@ -162,6 +194,7 @@
                 // DISABLE THREADS
                 HK_counter->stop();
                 gPAY_SPI->bulkRead_pause();
+                 PL_GPIO_1_STATUS=0;
                 gFLAGS = gFLAGS | COM_SESSION_FLAG;
                 gSESSION_TIMEOUT.attach(&after_session, COM_SESSION_TIMEOUT);
                 gFLAGS = gFLAGS | COM_RX_FLAG;
--- a/cdms_sd.h	Thu Jan 05 17:38:21 2017 +0000
+++ b/cdms_sd.h	Fri Jan 13 08:00:33 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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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_OLD[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
--- a/i2c.h	Thu Jan 05 17:38:21 2017 +0000
+++ b/i2c.h	Fri Jan 13 08:00:33 2017 +0000
@@ -58,10 +58,12 @@
         CDMS_I2C_ERR_SPEED_COUNTER++;
         I2C_busreset();
     }
+    
     PL_I2C_GPIO = 0;
     i2c_count = 0;
     return read_ack;
     
+    
 }
 bool FCTN_I2C_WRITE_PL(char *data2,uint8_t tc_len2)       // Returns 0 for success
 {
@@ -97,6 +99,7 @@
 }
 bool FCTN_I2C_READ(char *data,int length)       // Returns 0 for success
 {   
+ //   gPC.printf("i2C_rd\r\n");
     CDMS_I2C_GPIO = 1;
     read_ack = master.read(addr_bae|1,data,length);
     Thread::wait(1);        //as per tests Thread::wait not required on master side. But its safe to give 1ms 
@@ -126,6 +129,7 @@
         CDMS_I2C_ERR_BAE_COUNTER++;
         
     }
+ //  gPC.printf("end_12c_RD_\r\n");
     CDMS_I2C_GPIO = 0;
     i2c_count = 0;
     return read_ack;
@@ -133,6 +137,7 @@
 
 bool FCTN_I2C_WRITE(char *data,int tc_len2)       // Returns 0 for success
 {    
+   //  gPC.printf("i2C_wr\r\n");
     CDMS_I2C_GPIO = 1;
     write_ack = master.write(addr_bae|0x00,data,tc_len2);    
     Thread::wait(1);    //As per the tests Thread::wait is not required on master side but its safe to give 1ms
@@ -161,6 +166,7 @@
         I2C_busreset();
         CDMS_I2C_ERR_BAE_COUNTER++;
     }
+  //   gPC.printf("i2C_wr_end\r\n");
     CDMS_I2C_GPIO = 0;
     i2c_count = 0;
     return write_ack;
--- a/main.cpp	Thu Jan 05 17:38:21 2017 +0000
+++ b/main.cpp	Fri Jan 13 08:00:33 2017 +0000
@@ -113,7 +113,8 @@
     gCOM_MNG_TMTC_THREAD->set_priority(osPriorityRealtime);
     
     gHK_THREAD = new Thread(FCTN_CDMS_HK_MAIN);
-    gHK_THREAD->set_priority(osPriorityAboveNormal);
+   // gHK_THREAD->set_priority(osPriorityHigh);
+     gHK_THREAD->set_priority(osPriorityNormal);
     
     gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
     gSCIENCE_THREAD->set_priority(osPriorityHigh);
@@ -126,7 +127,7 @@
     
     HK_counter = new RtosTimer(hk_isr, osTimerPeriodic,(void * )NULL);
     gHK_THREAD->signal_set(HK_SIGNAL);
-    HK_counter->start(10000);
+   HK_counter->start(10000);
     
     sys_reset_cdms_timer = new RtosTimer(sys_pwr_reset, osTimerPeriodic, (void * )NULL);
     sys_reset_cdms_timer->start(cdms_reset_timeout);