Repository for CDMS code

Dependencies:   SimpleDMA mbed-rtos mbed eeprom

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Revision:
332:7d2431ce979f
Parent:
330:6ac9661f2e10
Child:
333:cddef120cae3
diff -r 1943be3fd178 -r 7d2431ce979f CDMS_HK.h
--- a/CDMS_HK.h	Thu Dec 22 12:55:32 2016 +0000
+++ b/CDMS_HK.h	Wed Dec 28 04:25:18 2016 +0000
@@ -47,200 +47,194 @@
 {
     uint8_t sd_stat = 0;
     uint8_t hk_count=0;
-    while(1)
-    {
-    gHK_THREAD->signal_wait(HK_SIGNAL);
-    gMutex.lock();
-    if(hk_count == 1 || hk_count == 2)
-    {
-        FCTN_CDMS_PL_MAIN((void const *)NULL);
-        hk_count--;
-        gMutex.unlock();
-        continue;       
-    }
-    else if(hk_count == 0)
-    {
-        FCTN_CDMS_PL_MAIN((void const *)NULL);
-        hk_count = 2;
-    }
-  //  gPC.printf("\n\rEntering HK thread\n");
-    //gPC.printf("\n\r%d\n",CDMS_WR_SD_FAULT_COUNTER);
-    if(EN_CDMS_HK == 0x00)
-    continue;
-    CDMS_HK_MAIN_STATUS = 0x01;
-    CDMS_HK_MAIN_COUNTER++;
+    while(1) {
+        gHK_THREAD->signal_wait(HK_SIGNAL);
+        gMutex.lock();
+        if(hk_count == 1 || hk_count == 2) {
+            FCTN_CDMS_PL_MAIN((void const *)NULL);
+            hk_count--;
+            gMutex.unlock();
+            continue;
+        } else if(hk_count == 0) {
+            FCTN_CDMS_PL_MAIN((void const *)NULL);
+            hk_count = 2;
+        }
+        //  gPC.printf("\n\rEntering HK thread\n");
+        //gPC.printf("\n\r%d\n",CDMS_WR_SD_FAULT_COUNTER);
+        if(EN_CDMS_HK == 0x00)
+            continue;
+        CDMS_HK_MAIN_STATUS = 0x01;
+        CDMS_HK_MAIN_COUNTER++;
 
-    FCTN_CDMS_HK();         //collects temperatures
-    RSSI_volatge = COMRX_RSSI_volatge.read() * 3.3;//to be checked
-    //VERIFY_COMRX();
-    VERIFY_RTC();
-    HANDLE_HW_FAULTS();
-    FUNC_CDMS_GPIO_STATUS();
+        FCTN_CDMS_HK();         //collects temperatures
+        RSSI_volatge = COMRX_RSSI_volatge.read() * 3.3;//to be checked
+        //VERIFY_COMRX();
+        VERIFY_RTC();
+        HANDLE_HW_FAULTS();
+        FUNC_CDMS_GPIO_STATUS();
 
-    uint8_t CDMS_quant[20];
-    CDMS_quant[0]= COM_ADF_TMP+40;
-    CDMS_quant[1]= (uint8_t)quant_data.CDMS_temp_quant;
-    CDMS_quant[2]= (uint8_t)RSSI_volatge;
-    CDMS_quant[3]= (uint8_t)(EPS_BTRY_VOLT*33*(62.0/11));
-    for(int i=0; i<16; i++) {
-        CDMS_quant[i+4]= (uint8_t)quant_data.temp_quant[i];
-    }
-    minMaxHkData();
+        uint8_t CDMS_quant[20];
+        CDMS_quant[0]= COM_ADF_TMP+40;
+        CDMS_quant[1]= (uint8_t)quant_data.CDMS_temp_quant;
+        CDMS_quant[2]= (uint8_t)RSSI_volatge;
+        CDMS_quant[3]= (uint8_t)(EPS_BTRY_VOLT*33*(62.0/11));
+        for(int i=0; i<16; i++) {
+            CDMS_quant[i+4]= (uint8_t)quant_data.temp_quant[i];
+        }
+        minMaxHkData();
 
-    CDMS_HEALTH_DATA[1] = GPIO_STATUS;            //Reading GPIO Pins
-    CDMS_HEALTH_DATA[0] = GPIO_STATUS >> 8;
-    COLLECT_CDMS_RAM();
-    for(int i = 0;i<84;i++)
-        CDMS_HEALTH_DATA[2+i] = CDMS_RAM[i];                  //Reading RAM parameters
-    for(int i = 0;i<20;i++)                                   //Collecting Data from Temp sensors
-        CDMS_HEALTH_DATA[86+i] = CDMS_quant[i];
+        CDMS_HEALTH_DATA[1] = GPIO_STATUS;            //Reading GPIO Pins
+        CDMS_HEALTH_DATA[0] = GPIO_STATUS >> 8;
+        COLLECT_CDMS_RAM();
+        for(int i = 0; i<84; i++)
+            CDMS_HEALTH_DATA[2+i] = CDMS_RAM[i];                  //Reading RAM parameters
+        for(int i = 0; i<20; i++)                                 //Collecting Data from Temp sensors
+            CDMS_HEALTH_DATA[86+i] = CDMS_quant[i];
 
-    // Here: Have to FIT flash data.
-    CDMS_HEALTH_DATA[106] = (EPS_V_A_EN_STATUS<<7) | ((BAE_STATUS<<5)&0x60) | ((SD_STATUS<<3)&0x18) | ((PL_STATUS<<1)&0x06) | (PL_EPS_LATCH_SW_EN & 0x01);
-    CDMS_HEALTH_DATA[107] = (RTC_INIT_STATUS<<6) | ((CDMS_RTC_DISABLE<<5)&0x20);
-    CDMS_HEALTH_DATA[108] = CDMS_RESET_COUNTER >>8;
-    CDMS_HEALTH_DATA[109] = CDMS_RESET_COUNTER;
-    CDMS_HEALTH_DATA[110] = TIME_LATEST_CDSMS_RESET >>24;
-    CDMS_HEALTH_DATA[111] = TIME_LATEST_CDSMS_RESET >>16;
-    CDMS_HEALTH_DATA[112] = TIME_LATEST_CDSMS_RESET >>8;
-    CDMS_HEALTH_DATA[113] = TIME_LATEST_CDSMS_RESET;
-    CDMS_HEALTH_DATA[114] = COM_TC_BYTES_LIMIT>>8;
-    CDMS_HEALTH_DATA[115] = COM_TC_BYTES_LIMIT;
-    CDMS_HEALTH_DATA[116] = COM_RX_CURRENT_MAX;
-    CDMS_HEALTH_DATA[117] = COM_RX_DISABLE_TIMEOUT;
-    CDMS_HEALTH_DATA[118] = COM_PA_TMP_HIGH;
-    CDMS_HEALTH_DATA[119] = COM_PA_RECOVERY_TIMEOUT;
-    CDMS_HEALTH_DATA[120] = COM_SESSION_TIMEOUT;
-    CDMS_HEALTH_DATA[121] = COM_RSSI_MIN;
-    CDMS_HEALTH_DATA[122] = SD_LIB_BLK_CURRENT>>8;
-    CDMS_HEALTH_DATA[122] = SD_LIB_BLK_CURRENT;
-    
-    uint32_t time = FCTN_CDMS_RD_RTC() >> 7;             //Reading Time from RTC
-    for(int i = 124; i<128; i++)
-        CDMS_HEALTH_DATA[i] = time >> (127-i)*8;
+        // Here: Have to FIT flash data.
+        CDMS_HEALTH_DATA[106] = (EPS_V_A_EN_STATUS<<7) | ((BAE_STATUS<<5)&0x60) | ((SD_STATUS<<3)&0x18) | ((PL_STATUS<<1)&0x06) | (PL_EPS_LATCH_SW_EN & 0x01);
+        CDMS_HEALTH_DATA[107] = (RTC_INIT_STATUS<<6) | ((CDMS_RTC_DISABLE<<5)&0x20);
+        CDMS_HEALTH_DATA[108] = CDMS_RESET_COUNTER >>8;
+        CDMS_HEALTH_DATA[109] = CDMS_RESET_COUNTER;
+        CDMS_HEALTH_DATA[110] = TIME_LATEST_CDSMS_RESET >>24;
+        CDMS_HEALTH_DATA[111] = TIME_LATEST_CDSMS_RESET >>16;
+        CDMS_HEALTH_DATA[112] = TIME_LATEST_CDSMS_RESET >>8;
+        CDMS_HEALTH_DATA[113] = TIME_LATEST_CDSMS_RESET;
+        COM_TC_BYTES_LIMIT = BYTE_OVERFLOW_CONSTANT;
+        CDMS_HEALTH_DATA[114] = COM_TC_BYTES_LIMIT>>8;
+        CDMS_HEALTH_DATA[115] = COM_TC_BYTES_LIMIT;
+        CDMS_HEALTH_DATA[116] = COM_RX_CURRENT_MAX;
+        CDMS_HEALTH_DATA[117] = COM_RX_DISABLE_TIMEOUT;
+        CDMS_HEALTH_DATA[118] = COM_PA_TMP_HIGH;
+        CDMS_HEALTH_DATA[119] = COM_PA_RECOVERY_TIMEOUT;
+        CDMS_HEALTH_DATA[120] = COM_SESSION_TIMEOUT/60;
+        CDMS_HEALTH_DATA[121] = COM_RSSI_MIN;
+        CDMS_HEALTH_DATA[122] = SD_LIB_BLK_CURRENT>>8;
+        CDMS_HEALTH_DATA[122] = SD_LIB_BLK_CURRENT;
 
-    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
-    CDMS_HK_FRAME[0] = 0x20;
-    CDMS_HK_FRAME[1] = (FSC_CURRENT[4]+1) >> 16;
-    CDMS_HK_FRAME[2] = (FSC_CURRENT[4]+1) >> 8;
-    CDMS_HK_FRAME[3] = FSC_CURRENT[4]+1;
-   // gPC.printf("\n");
-    for(int i = 0; i<128; i++){                       /*Adding actual CDMS Health data to TM frame*/
-        CDMS_HK_FRAME[i+4] = CDMS_HEALTH_DATA[i];
-        //gPC.printf("%02x",CDMS_HEALTH_DATA[i]);
-    }
- //   gPC.printf("\n");
-    uint16_t crc = crc16_gen(CDMS_HK_FRAME,132);      /*Adding CRC to TM frame*/
-    CDMS_HK_FRAME[132] = crc >> 8;
-    CDMS_HK_FRAME[133] = crc;
+        uint32_t time = FCTN_CDMS_RD_RTC() >> 7;             //Reading Time from RTC
+        for(int i = 124; i<128; i++)
+            CDMS_HEALTH_DATA[i] = time >> (127-i)*8;
+
+        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
+        CDMS_HK_FRAME[0] = 0x20;
+        CDMS_HK_FRAME[1] = (FSC_CURRENT[4]+1) >> 16;
+        CDMS_HK_FRAME[2] = (FSC_CURRENT[4]+1) >> 8;
+        CDMS_HK_FRAME[3] = FSC_CURRENT[4]+1;
+        // gPC.printf("\n");
+        for(int i = 0; i<128; i++) {                      /*Adding actual CDMS Health data to TM frame*/
+            CDMS_HK_FRAME[i+4] = CDMS_HEALTH_DATA[i];
+            //gPC.printf("%02x",CDMS_HEALTH_DATA[i]);
+        }
+//   gPC.printf("\n");
+        uint16_t crc = crc16_gen(CDMS_HK_FRAME,132);      /*Adding CRC to TM frame*/
+        CDMS_HK_FRAME[132] = crc >> 8;
+        CDMS_HK_FRAME[133] = crc;
 
-    exor(CDMS_HK_FRAME);
-    CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME , convoluted_CDMS_HK);
-    CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME + 67, convoluted_CDMS_HK + 135);
-    //gPC.printf("\n\r reached here");
-    interleave(convoluted_CDMS_HK ,  interleave_CDMS_HK);
-    interleave(convoluted_CDMS_HK +135, interleave_CDMS_HK + 144);
-    //gPC.printf("\n\r reached here");
-    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;
+        exor(CDMS_HK_FRAME);
+        CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME , convoluted_CDMS_HK);
+        CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME + 67, convoluted_CDMS_HK + 135);
+        //gPC.printf("\n\r reached here");
+        interleave(convoluted_CDMS_HK ,  interleave_CDMS_HK);
+        interleave(convoluted_CDMS_HK +135, interleave_CDMS_HK + 144);
+        //gPC.printf("\n\r reached here");
+        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;
         }
-   // gPC.printf("Completed CDMS HK\t");
+        // gPC.printf("Completed CDMS HK\t");
+
+        /*---------------------------------- BAE HK --------------------------------------------*/
 
-    /*---------------------------------- BAE HK --------------------------------------------*/
+        BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134);
+        //  gPC.printf("Entering BAE HK\t");
+        if(BAE_HK_I2C == 0) {
+            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");
+                TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
+                /*for(int i = 0; i<15; i++)
+                    gPC.printf("\r 0x%02X\n",BAE_HK[i]);*/
+                for(int i = 0; i<4; i++)
+                    BAE_HK[i] = time >> (3-i)*8;
+                BAE_HK_FRAME[0] = 0x28;
+                BAE_HK_FRAME[1] = (FSC_CURRENT[5]+1) >> 16;
+                BAE_HK_FRAME[2] = (FSC_CURRENT[5]+1) >> 8;
+                BAE_HK_FRAME[3] = FSC_CURRENT[5]+1;
+                for(int i = 0; i<128; i++)                       /*Adding actual CDMS Health data to TM frame*/
+                    BAE_HK_FRAME[4+i] = BAE_HK[i];
+                crc = crc16_gen(BAE_HK_FRAME,132);               /*Adding CRC to TM frame*/
+                BAE_HK_FRAME[132] = crc >> 8;
+                BAE_HK_FRAME[133] = crc;
+                exor(BAE_HK_FRAME);
+                BAE_HEALTH.convolutionEncode(BAE_HK_FRAME , convoluted_BAE_HK);
+                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);
+                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");
+                    //break;
+                }
+            }
 
-    BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134);
-  //  gPC.printf("Entering BAE HK\t");
-    if(BAE_HK_I2C == 0) {
-        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");
+        } else {
+            gPC.printf("BAE HK data not recieved through I2C\t");
+            for(int i = 0; i<134; i++)
+                BAE_HK[i] = 0;
+        }
+        // gPC.printf("Completed BAE HK\n");
+
+        /*----------------------------------Beacon message--------------------------------------*/
+
+
+        // Add HK bits
+        beacon_array[0] = 0x00;
+        beacon_array[1] = time >> 32;
+        beacon_array[2] = time >> 24;
+        beacon_array[3] = time >> 16;
+        beacon_array[4] = time >> 8;
+        beacon_array[5] = time;
+        beacon_array[6] = SD_FAULTCOUNT >> 8;
+        beacon_array[7] = SD_FAULTCOUNT;
+        beacon_array[8] = RTC_FAULTCOUNT >> 8;
+        beacon_array[9] = RTC_FAULTCOUNT;
+        beacon_array[10] = (((SD_STATUS == DEVICE_DISABLED || SD_STATUS == DEVICE_OC_FAULT)?1:0)<<7)|(RTC_STATUS <<6)|(COM_RX_STATUS<<3)|(0<<2)|(COMRX_OC_FAULT<<1)|(COM_TX_OC_FAULT);
+        beacon_array[11] = (COM_RX_CNTRL <<7)|(COM_TX_CNTRL);
+        beacon_array[12] = CDMS_HK_MAIN_COUNTER >>8;
+        beacon_array[13] = CDMS_HK_MAIN_COUNTER;
+        beacon_array[14] = PL_MAIN_COUNTER >>8;
+        beacon_array[15] = PL_MAIN_COUNTER;
+        beacon_array[16] = PL_RCV_SC_DATA_COUNTER >>8;
+        beacon_array[17] = PL_RCV_SC_DATA_COUNTER;
+        beacon_array[18] = TIME_LATEST_SPI_SPEED >>24;
+        beacon_array[19] = TIME_LATEST_SPI_SPEED >>16;
+        beacon_array[20] = TIME_LATEST_SPI_SPEED >>8;
+        beacon_array[21] = TIME_LATEST_SPI_SPEED;
+        beacon_array[22] = (uint8_t)RSSI_volatge;
+
+        // Add SC bits
+        crc = crc16_gen(beacon_array,132);
+        beacon_array[132] = crc;
+        beacon_array[133] = crc >> 8;
+        bool y;
+        y = FCTN_I2C_WRITE((char *)beacon_array,134);
+        if(y == 0) {
             TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
-            /*for(int i = 0; i<15; i++)
-                gPC.printf("\r 0x%02X\n",BAE_HK[i]);*/
-            for(int i = 0; i<4; i++)
-            BAE_HK[i] = time >> (3-i)*8;
-            BAE_HK_FRAME[0] = 0x28;
-            BAE_HK_FRAME[1] = (FSC_CURRENT[5]+1) >> 16;
-            BAE_HK_FRAME[2] = (FSC_CURRENT[5]+1) >> 8;
-            BAE_HK_FRAME[3] = FSC_CURRENT[5]+1;
-            for(int i = 0; i<128; i++)                       /*Adding actual CDMS Health data to TM frame*/
-                BAE_HK_FRAME[4+i] = BAE_HK[i];
-            crc = crc16_gen(BAE_HK_FRAME,132);               /*Adding CRC to TM frame*/
-            BAE_HK_FRAME[132] = crc >> 8;
-            BAE_HK_FRAME[133] = crc;
-            exor(BAE_HK_FRAME);
-            BAE_HEALTH.convolutionEncode(BAE_HK_FRAME , convoluted_BAE_HK);
-            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);
-            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");
-        //break;
-        }
-        }
-        
-    } else {
-        gPC.printf("BAE HK data not recieved through I2C\t");
-        for(int i = 0; i<134; i++)
-            BAE_HK[i] = 0;
-    }
-   // gPC.printf("Completed BAE HK\n");
-
-    /*----------------------------------Beacon message--------------------------------------*/
-    
-    
-    // Add HK bits
-    beacon_array[0] = 0x00;
-    beacon_array[1] = time >> 32;
-    beacon_array[2] = time >> 24;
-    beacon_array[3] = time >> 16;
-    beacon_array[4] = time >> 8;
-    beacon_array[5] = time;
-    beacon_array[6] = SD_FAULTCOUNT >> 8;
-    beacon_array[7] = SD_FAULTCOUNT;
-    beacon_array[8] = RTC_FAULTCOUNT >> 8;
-    beacon_array[9] = RTC_FAULTCOUNT;
-    beacon_array[10] = (((SD_STATUS == DEVICE_DISABLED || SD_STATUS == DEVICE_OC_FAULT)?1:0)<<7)|(RTC_STATUS <<6)|(COM_RX_STATUS<<3)|(0<<2)|(COMRX_OC_FAULT<<1)|(COM_TX_OC_FAULT);
-    beacon_array[11] = (COM_RX_CNTRL <<7)|(COM_TX_CNTRL);
-    beacon_array[12] = CDMS_HK_MAIN_COUNTER >>8;
-    beacon_array[13] = CDMS_HK_MAIN_COUNTER;
-    beacon_array[14] = PL_MAIN_COUNTER >>8;
-    beacon_array[15] = PL_MAIN_COUNTER;
-    beacon_array[16] = PL_RCV_SC_DATA_COUNTER >>8;
-    beacon_array[17] = PL_RCV_SC_DATA_COUNTER;
-    beacon_array[18] = TIME_LATEST_SPI_SPEED >>24;
-    beacon_array[19] = TIME_LATEST_SPI_SPEED >>16;
-    beacon_array[20] = TIME_LATEST_SPI_SPEED >>8;
-    beacon_array[21] = TIME_LATEST_SPI_SPEED;
-    beacon_array[22] = (uint8_t)RSSI_volatge;
-    
-    // Add SC bits
-    crc = crc16_gen(beacon_array,132);
-    beacon_array[132] = crc;
-    beacon_array[133] = crc >> 8;
-    bool y;
-    y = FCTN_I2C_WRITE((char *)beacon_array,134);
-    if(y == 0){
-       TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
-       //gPC.printf("long Bcn sent\n\r");
-    }
-        else
-        gPC.printf("long Bcn not sent\r\n");
-    //gPC.printf("\rCompleted Beacon\n");
-    gMutex.unlock();
+            //gPC.printf("long Bcn sent\n\r");
+        } else
+            gPC.printf("long Bcn not sent\r\n");
+        //gPC.printf("\rCompleted Beacon\n");
+        gMutex.unlock();
     }
 }
 
@@ -309,48 +303,42 @@
             SelectLinec2=!(SelectLinec2);
         if(Iteration%8==7)
             SelectLinec3=!(SelectLinec3);
-            wait_ms(1);
-     //  gPC.printf("\r%d %d %d %d\n",SelectLinec3.read(),SelectLinec2.read(),SelectLinec1.read(),SelectLinec0.read());
+        wait_ms(1);
+        //  gPC.printf("\r%d %d %d %d\n",SelectLinec3.read(),SelectLinec2.read(),SelectLinec1.read(),SelectLinec0.read());
     }
 
     actual_data.CDMS_temp_actual=(-90.7*3.3*CDMS_temp_sensor.read())+190.1543;
-    
+
 
-    
+
     for(Iteration=0; Iteration<16; Iteration++) {
 
-        if(Iteration<4)
-        {
+        if(Iteration<4) {
             actual_data.temp_actual[Iteration]=actual_data.temp_actual[Iteration]*3.3*2*10;
-        }
-        else if(Iteration<14)
-        {
+        } else if(Iteration<14) {
             resistance=22000*actual_data.temp_actual[Iteration]*3.3/(3.3-actual_data.temp_actual[Iteration]*3.3);
-           
+
             if(actual_data.temp_actual[Iteration]*3.3<1.47)      //Document says 1.378 .Pls Check
-                
-                actual_data.temp_actual[Iteration]=(3694/log(24.032242*resistance))-273;  
+
+                actual_data.temp_actual[Iteration]=(3694/log(24.032242*resistance))-273;
             else
-                
+
                 actual_data.temp_actual[Iteration]=(3365.4/log(7.60573*resistance))-273;
-                 
-        }
-        else
+
+        } else
             actual_data.temp_actual[Iteration]=(-90.7*3.3*actual_data.temp_actual[Iteration])+190.1543;
     }
     for(Iteration=0; Iteration<16; Iteration++) {
 
-        if(Iteration<4)
-        {
+        if(Iteration<4) {
             quant_data.temp_quant[Iteration]=actual_data.temp_actual[Iteration];
             wait(0.001);
-        }
-        else if(Iteration<14)
+        } else if(Iteration<14)
             quant_data.temp_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.temp_actual[Iteration]);
-          // quant_data.temp_quant[Iteration]=quantiz(0,1,actual_data.temp_actual[Iteration]);
+        // quant_data.temp_quant[Iteration]=quantiz(0,1,actual_data.temp_actual[Iteration]);
         else
-           // quant_data.temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.temp_actual[Iteration]);
-           quant_data.temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.temp_actual[Iteration]);
+            // quant_data.temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.temp_actual[Iteration]);
+            quant_data.temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.temp_actual[Iteration]);
     }
     quant_data.CDMS_temp_quant=quantiz(tstart,tstep,actual_data.CDMS_temp_actual);
 
@@ -369,7 +357,7 @@
     //V_C_PGOOD //3V3CPGOOD //$
     GPIO_STATUS=(V_C_PGOOD)?(GPIO_STATUS)|((uint16_t)(0x1<<12)):(GPIO_STATUS)&(~((uint16_t)(0x1<<12)));
     */
-    
+
     //COMRX_OC_FAULT //$
     GPIO_STATUS=(COMRX_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<11)):(GPIO_STATUS)&(~((uint16_t)(0x1<<11)));
     // COMTX_OC_FAULT //$
@@ -378,7 +366,7 @@
     GPIO_STATUS=(SD_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<9)):(GPIO_STATUS)&(~((uint16_t)(0x1<<9)));
     //BAE_OC_FAULT //$
     GPIO_STATUS=(BAE_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<8)):(GPIO_STATUS)&(~((uint16_t)(0x1<<8)));
-    
+
     /*
     //PL_GPIO_1_STATUS //$
     GPIO_STATUS=(PL_GPIO_1_STATUS)?(GPIO_STATUS)|((uint16_t)(0x1<<8)):(GPIO_STATUS)&(~((uint16_t)(0x1<<8)));
@@ -387,7 +375,7 @@
     //PL_GPIO_3_STATUS //$
     GPIO_STATUS=(PL_GPIO_3_STATUS)?(GPIO_STATUS)|((uint16_t)(0x1<<6)):(GPIO_STATUS)&(~((uint16_t)(0x1<<6)));
     */
-    
+
     //PL_BEE_SW_OC_FAULT //to be verified
     GPIO_STATUS=(PL_BEE_SW_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<4)):(GPIO_STATUS)&(~((uint16_t)(0x1<<4)));
     //PL_EPS_LATCH_SW_OC_FAULT // to be verified
@@ -396,8 +384,8 @@
     GPIO_STATUS=(COM_RX_CNTRL)?(GPIO_STATUS)|((uint16_t)(0x1<<2)):(GPIO_STATUS)&(~((uint16_t)(0x1<<2)));
     //EPS_V_D_EN_STATUS
     GPIO_STATUS=(COM_TX_CNTRL)?(GPIO_STATUS)|((uint16_t)(0x1<<1)):(GPIO_STATUS)&(~((uint16_t)(0x1<<1)));
-  //  gPC.printf("%04x\n",GPIO_STATUS);
-   gPC.printf("\n\rBAE_OC STATE = %04x",GPIO_STATUS);
+    //  gPC.printf("%04x\n",GPIO_STATUS);
+    gPC.printf("\n\rBAE_OC STATE = %04x",GPIO_STATUS);
 }
 
 void VERIFY_COMRX()
@@ -417,20 +405,20 @@
 
 void VERIFY_RTC()
 {
-        uint8_t response;
-        if(EN_RTC == 0x00)
+    uint8_t response;
+    if(EN_RTC == 0x00)
         return;
-        gCS_RTC=1;
-        gCS_RTC=0;
-        spi.write(0x0F);
-        response = spi.write(0x00);
-        CDMS_RTC_BL = (response & 0x10) >>4;
-        if(response & 0x04 == 0x04) {
-            //RESET_RTC();
-            RTC_STATUS = 0x01;
-            RTC_FAULTCOUNT++;
-        }
-        gCS_RTC=1;
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x0F);
+    response = spi.write(0x00);
+    CDMS_RTC_BL = (response & 0x10) >>4;
+    if(response & 0x04 == 0x04) {
+        //RESET_RTC();
+        RTC_STATUS = 0x01;
+        RTC_FAULTCOUNT++;
+    }
+    gCS_RTC=1;
 }
 
 void HANDLE_HW_FAULTS()
@@ -443,19 +431,19 @@
 void HANDLE_HW_FAULT_SD()
 {
     if(SD_STATUS != DEVICE_DISABLED) {
-        if(SD_STATUS == DEVICE_OC_FAULT){
+        if(SD_STATUS == DEVICE_OC_FAULT) {
             gPC.printf("Switching on SD card");
             SD_SW_EN_DS = 1; //powering on SD
             wait_ms(10);
         }
-            
+
         if(SD_OC_FAULT == 0) {
             gPC.printf("Switching off SD card");
             SD_SW_EN_DS = 0; //switching off SD card
             SD_FAULTCOUNT++;
             SD_STATUS = (SD_FAULTCOUNT == 3) ? DEVICE_DISABLED :DEVICE_OC_FAULT;
-            if(SD_FAULTCOUNT == 3){
-                FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);    
+            if(SD_FAULTCOUNT == 3) {
+                FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
                 gPC.printf("Declaring SD card permanantly Disabled");
             }
         } else {
@@ -470,25 +458,25 @@
 void HANDLE_HW_FAULT_BAE()
 {
     if(BAE_STATUS != DEVICE_DISABLED) {
-        if(BAE_STATUS == DEVICE_OC_FAULT){
+        if(BAE_STATUS == DEVICE_OC_FAULT) {
             gPC.printf("Switching on BAE");
             BAE_SW_EN_DS = 1; //Power ON BAE
             wait_ms(10);
         }
-        
+
         if(BAE_OC_FAULT == 0) {
             gPC.printf("Switching off BAE");
             BAE_SW_EN_DS = 0; //Switch OFF BAE
             BAE_FAULTCOUNT++;
             BAE_STATUS = (BAE_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
-            if(BAE_FAULTCOUNT == 3){
+            if(BAE_FAULTCOUNT == 3) {
                 FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
                 gPC.printf("Declaring BAE permanantly Disabled");
             }
         } else {
             BAE_STATUS = DEVICE_POWERED;
             if(SD_STATUS != DEVICE_POWERED);
-                FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+            FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
             BAE_FAULTCOUNT = 0;
         }
     }
@@ -497,7 +485,7 @@
 void HANDLE_HW_FAULT_PL()
 {
     if(PL_STATUS != DEVICE_DISABLED) {
-        if(PL_STATUS == DEVICE_OC_FAULT){
+        if(PL_STATUS == DEVICE_OC_FAULT) {
             gPC.printf("Switching on PL_BEE");
             PYLD_DFF_CLK = 0;
             PYLD_DFF = 1;           // Switching ON PL
@@ -505,11 +493,11 @@
             PYLD_DFF_CLK = 1;
             wait_us(1);
             PYLD_DFF_CLK = 0;
-            wait_us(1);         
+            wait_us(1);
         }
         if(PL_BEE_SW_OC_FAULT == 0) { // if OC Fault
             gPC.printf("Switching off PL_BEE");
-            PYLD_DFF_CLK = 0;     
+            PYLD_DFF_CLK = 0;
             PYLD_DFF = 0;             //Switching OFF PL
             wait_us(1);
             PYLD_DFF_CLK = 1;
@@ -518,14 +506,14 @@
             wait_us(1);
             PL_FAULTCOUNT++;
             PL_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
-            if(PL_FAULTCOUNT == 3){
+            if(PL_FAULTCOUNT == 3) {
                 FCTN_CDMS_WR_FLASH(3,DEVICE_DISABLED);
                 gPC.printf("Declaring PL_BEE permanantly Disabled");
             }
         } else {
-            if(PL_STATUS == DEVICE_OC_FAULT){
+            if(PL_STATUS == DEVICE_OC_FAULT) {
                 gPC.printf("Switching off PL_BEE");
-                PYLD_DFF_CLK = 0;     
+                PYLD_DFF_CLK = 0;
                 PYLD_DFF = 0;             //Switching OFF PL
                 wait_us(1);
                 PYLD_DFF_CLK = 1;
@@ -544,11 +532,21 @@
 void COLLECT_CDMS_RAM()
 {
     CDMS_RAM[0] = ((PL_INIT_STATUS<<7)&0x80)|((PL_MAIN_status<<6)&0x40)|((PL_LOW_power<<5)&0x20)|((PL_STATE<<3)&0x18)|(PL_STATUS&0x07);
-    //gPC.printf("\n\rPL_STATUS : %d",PL_STATUS);
-    //gPC.printf("\n\rPL_STATE : %d",PL_STATE);
-    //gPC.printf("\n\rpl bits = %02x\n",CDMS_RAM[0]);
-    CDMS_RAM[1] = ((PL_RCV_SC_DATA_STATUS<<7)&0x80)|((COM_SESSION<<6)&0x40)|((COM_RX<<5)&0x20)|((RF_SW_STATUS<<4)&0x10)|((COM_TX<<3)&0x08)|((COM_TX_STATUS<<2)&0x04)|((COM_MNG_TMTC<<1)&0x02)|(EN_CDMS_HK&0x01);
-    CDMS_RAM[2] = ((EN_PL<<7)&0x80)|((EN_RCV_SC<<6)&0x40)|((CDMS_INIT_STATUS<<5)&0x20)|((CDMS_HK_MAIN_STATUS<<4)&0x10)|((CDMS_HK_STATUS<<2)&0x0C)|((COM_RX_STATUS<<1)&0x02)|(CDMS_RTC_BL&0x01);
+
+    CDMS_RAM[1] = ( (PL_RCV_SC_DATA_STATUS<<7) & 0x80 )|
+                  ( ((gFLAGS & COM_SESSION_FLAG)<<3) & 0x40 )|
+                  ( ((gFLAGS & COM_RX_FLAG)<<1) & 0x20 )|
+                  ( ((gFLAGS & RF_SW_STATUS_FLAG)>>8)&0x10 )|
+                  ( ((gFLAGS & COM_TX_FLAG)>>5)&0x08 )|
+                  ( (COM_TX_STATUS<<2)&0x04 )|
+                  ( ((gFLAGS & COM_MNG_TMTC_RUNNING_FLAG)>>3) & 0x02 )|
+                  (EN_CDMS_HK&0x01);
+    CDMS_RAM[2] = ((EN_PL<<7)&0x80)|((EN_RCV_SC<<6)&0x40)|
+                  ((CDMS_INIT_STATUS<<5)&0x20)|
+                  ((CDMS_HK_MAIN_STATUS<<4)&0x10)|
+                  ((CDMS_HK_STATUS<<2)&0x0C)|
+                  ((COM_RX_STATUS<<1)&0x02)|
+                  (CDMS_RTC_BL&0x01);
     CDMS_RAM[3] = CDMS_I2C_ERR_SPEED_COUNTER >> 8;
     CDMS_RAM[4] = CDMS_I2C_ERR_SPEED_COUNTER;
     CDMS_RAM[5] = CDMS_I2C_ERR_BAE_COUNTER >> 8;
@@ -567,7 +565,7 @@
     CDMS_RAM[18] = SD_LIB_WRITES;
     TIME_LATEST_RTC= FCTN_CDMS_RD_RTC() >> 7; // added by samp
     for(int i = 0; i<4; i++)
-        CDMS_RAM[19+i] = TIME_LATEST_RTC >> (3-i)*8;   
+        CDMS_RAM[19+i] = TIME_LATEST_RTC >> (3-i)*8;
     for(int i = 0; i<4; i++)
         CDMS_RAM[23+i] = TIME_LATEST_I2C_BAE >> (3-i)*8;
     for(int i = 0; i<4; i++)