vr1.1

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of CDMS_RTOS_v1_1 by Team Fox

Files at this revision

API Documentation at this revision

Comitter:
cholletisaik777
Date:
Sat Jul 04 08:56:34 2015 +0000
Parent:
7:c270a9e37290
Child:
9:7ff6d75cc09e
Commit message:
cdms_rtos_v1_0_3

Changed in this revision

Flags.h Show annotated file Show diff for this revision Revisions of this file
Flash.cpp Show annotated file Show diff for this revision Revisions of this file
Flash.h Show annotated file Show diff for this revision Revisions of this file
HK_CDMS.cpp Show annotated file Show diff for this revision Revisions of this file
HK_CDMS.h Show annotated file Show diff for this revision Revisions of this file
HK_MAIN.cpp Show annotated file Show diff for this revision Revisions of this file
HK_MAIN.h Show annotated file Show diff for this revision Revisions of this file
PL.cpp Show annotated file Show diff for this revision Revisions of this file
PL.h Show annotated file Show diff for this revision Revisions of this file
RAM.cpp Show annotated file Show diff for this revision Revisions of this file
RAM.h Show annotated file Show diff for this revision Revisions of this file
RTC.cpp Show annotated file Show diff for this revision Revisions of this file
RTC.h Show annotated file Show diff for this revision Revisions of this file
SDCard.cpp Show annotated file Show diff for this revision Revisions of this file
SDCard.h Show annotated file Show diff for this revision Revisions of this file
TC.cpp Show annotated file Show diff for this revision Revisions of this file
TM.cpp Show annotated file Show diff for this revision Revisions of this file
all_funcs.h Show annotated file Show diff for this revision Revisions of this file
func_head.h Show diff for this revision Revisions of this file
hk_cdms.cpp Show diff for this revision Revisions of this file
hk_cdms.h Show diff for this revision Revisions of this file
i2c.cpp 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
main.h Show annotated file Show diff for this revision Revisions of this file
power.cpp Show annotated file Show diff for this revision Revisions of this file
power.h Show annotated file Show diff for this revision Revisions of this file
science.cpp Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flags.h	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,6 @@
+/*HK*/
+#define HK_INIT_STATUS 0X01
+#define HK_MAIN_STATUS 0X02
+#define HK_BAE_DISBLED 0X04
+#define HK_BAE_ERR-I2C 0X08
+/*End*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HK_CDMS.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,107 @@
+#include "mbed.h"
+#include "HK_CDMS.h"
+#include "pin_config.h"
+#include "all_funcs.h"
+ 
+ 
+Serial hk_cdms(USBTX, USBRX);
+//DigitalOut Select_Lines_C[]={PIN85,PIN84,PIN39,PIN38};  //to mux on IF board,from LSB(Select_Lines_C[3]) to MSB(Select_Lines_C[0])
+DigitalOut Select_Lines_C[]={D7,D6,D5,D4};
+AnalogIn Temperature_voltage_Input(PIN53);  //output from IF mux
+//AnalogIn Sensor_Input(PIN53);    //output from temperature sensor on CDMS
+char CDMS_HK_DATA[8];
+ 
+void FCTN_CDMS_HK_INIT()
+{
+    Select_Lines_C[0] = Select_Lines_C[1] = Select_Lines_C[2] = Select_Lines_C[3] = 0;  //initialisation of variables
+}
+ 
+int quantiz(float l_start,float l_step,float l_x)       // accepts min and measured values and step->quantises on a scale 0-15..(4 bit quantisation)
+{
+    int l_y = (l_x - l_start)/l_step;
+        
+    if(l_y <= 0)
+            l_y = 0;
+            
+    if(l_y >= 15)
+            l_y = 15;
+            
+    return l_y;
+} 
+ 
+Sensor_Data Sensor; 
+Sensor_Data_Quantised Sensor_Quantised;
+ 
+char* FCTN_CDMS_HK_MAIN()
+{
+    float l_resistance_thermistor;  //declaration of variables
+    float l_voltage_thermistor;
+    float l_Payload_voltage;
+    int l_Loop_Iterator = 0;
+    int l_Select_Line_Iterator = 3;
+ 
+    for(l_Loop_Iterator = 0; l_Loop_Iterator < 16; l_Loop_Iterator++)
+    {
+ 
+        l_voltage_thermistor = Temperature_voltage_Input.read()*3.3;       //voltage across thermistor
+        l_resistance_thermistor = 24000*l_voltage_thermistor/(3.3 - l_voltage_thermistor);           //resistance of thermistor
+        l_Payload_voltage = Temperature_voltage_Input.read()*3.3;
+               
+        Sensor.l_Temperature_thermistor[(l_Loop_Iterator)] = 0.00000004*l_resistance_thermistor*l_resistance_thermistor - 0.0039*l_resistance_thermistor + 347.97;
+        Sensor.l_Temperature_sensor[l_Loop_Iterator] = (-90.7*3.3*Temperature_voltage_Input.read() + 190.1543);  
+              
+        if(l_Loop_Iterator % 2 == 0) 
+        {
+             if(l_Loop_Iterator < 4)
+             Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] = quantiz(tstart_thermistor,tstep_thermistor,Sensor.l_Temperature_thermistor[(l_Loop_Iterator)]);
+               
+             else  
+             {
+                if(4 < l_Loop_Iterator < 14)
+                Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] <<4 + quantiz(tstart,tstep,Sensor.l_Payload_voltage[l_Loop_Iterator]); 
+                
+                else
+                Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] = quantiz(tstart,tstep,Sensor.l_Temperature_sensor[l_Loop_Iterator]); 
+             }
+        }
+                                              
+        else
+        {
+            
+            if(l_Loop_Iterator < 4)
+            Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] <<4 + quantiz(tstart_thermistor,tstep_thermistor,Sensor.l_Temperature_thermistor[l_Loop_Iterator]);
+            
+            else
+            {
+                if(4 < l_Loop_Iterator < 14)
+                Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] = quantiz(tstart,tstep,Sensor.l_Payload_voltage[l_Loop_Iterator]);
+                
+                else
+                Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] <<4 + quantiz(tstart,tstep,Sensor.l_Temperature_sensor[l_Loop_Iterator]); 
+        
+            }
+        }
+    
+       
+          
+       // The following lines are used to iterate the select lines from 0 to 15
+       for(l_Select_Line_Iterator = 3;l_Select_Line_Iterator >= 0;l_Select_Line_Iterator--)
+       {
+            if(Select_Lines_C[l_Select_Line_Iterator] == 0)
+            {
+                Select_Lines_C[l_Select_Line_Iterator] = 1;
+                break;
+            }
+            else Select_Lines_C[l_Select_Line_Iterator] = 0;
+            printf("%d\n",l_Select_Line_Iterator);
+       }
+      
+        wait_us(10.0); //  A delay of 10 microseconds between each sensor output. Can be changed.
+    }
+     printf("vol %f temp %f",Temperature_voltage_Input.read()*3.3,-90.7*3.3*Temperature_voltage_Input.read() + 190.1543 );
+     printf("CDMS_HK Data is Temperature_sensor=%s, l_Temperature_thermistor=%s, Payload_voltage=%s ",Sensor_Quantised.l_Temperature_sensor,Sensor_Quantised.l_Temperature_thermistor,Sensor_Quantised.l_Payload_voltage);
+     strcpy (CDMS_HK_DATA,Sensor_Quantised.l_Temperature_sensor);
+     strcat (CDMS_HK_DATA,Sensor_Quantised.l_Temperature_thermistor);
+     strcat (CDMS_HK_DATA,Sensor_Quantised.l_Payload_voltage);
+     return(CDMS_HK_DATA);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HK_CDMS.h	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,22 @@
+#define tstart -40
+#define tstep 8
+#define tstep_thermistor 8
+#define tstart_thermistor -40
+
+ 
+typedef struct Sensor_Data
+{
+    
+    float l_Temperature_sensor[2];
+    float l_Temperature_thermistor[4]; //read by thermistors on solar panels
+    float l_Payload_voltage[9]; //for payload analog signals
+ 
+} Sensor_Data;
+ 
+typedef struct Sensor_Data_Quantised
+{
+    char l_Temperature_sensor[1];
+    char l_Temperature_thermistor[2];
+    char l_Payload_voltage[5];
+    
+} Sensor_Data_Quantised;
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HK_MAIN.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,45 @@
+#include "mbed.h"
+#include "HK_MAIN.h"
+#include "all_funcs.h"
+#include "Flags.h"
+
+extern Serial hk_main(USBTX, USBRX);
+
+void FCTN_HK_MAIN()
+{
+    hk_main.printf("\rin FCTN_CDMS_HK_MAIN()\r\n");
+    FCTN_MASTER_I2C('h' , i2c_data ); //BAE hk data is collected
+    FCTN_CDMS_RD_RTC(rtc_data);  //RTC data is read to the rtc_data.
+    for(int i=(hk_count%(HK_ITER+1))*(HK_DATA+8);i<(hk_count%(HK_ITER+1))*(HK_DATA+8)+8;i++)
+    {
+        hk_data[i] = rtc_data[i-(hk_count%(HK_ITER+1))*(HK_DATA+8)];
+    }
+    for(int i=(hk_count%(HK_ITER+1))*(HK_DATA+8)+8;i<(hk_count%(HK_ITER+1))*(HK_DATA+8)+8+HK_DATA;i++)
+    {
+        hk_data[i] = i2c_data[i-(hk_count%(HK_ITER+1))*(HK_DATA+8)+8];
+    }
+    hk_count++;
+    if(hk_count%HK_ITER==0)
+    {
+        for(int i=(hk_count%(HK_ITER+1))*(HK_DATA+8)+8+HK_DATA;i<512;i++)
+        {
+            hk_data[i] = '/0';
+        }
+        hk_count = 0;
+        FCTN_WR_SD(hk_data,hk_block_number); // HK data is written to the SD card 
+        for(int i=0;i<512;i++)
+        {
+            hk_data[i] = '/0';
+        }
+        FCTN_RD_SD(hk_data,hk_block_number);
+        hk_block_number++;
+        printf("\n\r sd card hk data\r\n");
+        for(int i=0;i<512;i++)
+        {
+            hk_main.printf("%d|",hk_data[i]);
+        }
+       // FCTN_CDMS_HK_MAIN();
+       FCTN_POWER_MODE(80);          //to check dummy power algo 
+    }
+    hk_main.printf("\n\r hk exited\n");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HK_MAIN.h	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,18 @@
+/*Hash define parameters*/
+#define HK_DATA 25 //Total Hk data that is to be stored in SD card
+#define HK_ITER 5  //The iterations after which the HK data will be stored in the SD card 
+/*End*/
+
+/*Delclaring extern objects*/
+extern I2C master;
+extern DigitalOut irpt_2_slv;
+extern SPISlave pl_spi ; // mosi, miso, sclk, ssel --> using SPI1
+/*End*/
+
+/*Declaring global variables*/
+char i2c_data[25];
+int hk_count = 0;
+uint8_t hk_data[512];
+uint8_t rtc_data[8];
+uint64_t hk_block_number = 1;
+/*End*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PL.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+#include "PL.h"
+#include "all_funcs.h"
+
+Serial sr(USBTX,USBRX);
+
+SPISlave pl_spi(PTE18, PTE19, PTE17, PTE16 ); // mosi, miso, sclk, ssel --> using SPI1
+//SPISlave pl_spi(PTD6, PTD7, PTD5, PTD4   ); // mosi, miso, sclk, ssel --> using SPI1
+int payloadBins = 4;
+
+void FCTN_PL_RCV_SC_DATA()
+{
+        sr.printf("in FCTN_PL_RCV_SC_DATA\r\n");
+        uint8_t scienceRawPacket[payloadBins];
+        for(int i=0; i<payloadBins;i++)
+        {
+            while(!pl_spi.receive()); // blocking statement --> waiting for data from Payload
+            uint8_t v = pl_spi.read();   // Read byte from master
+            scienceRawPacket[i] = v;
+        }
+        // serial feedback
+        sr.printf("Packet recieved\r\r\n");
+        for(int i=0;i<payloadBins;i++)
+        {
+            sr.printf("%0x\t",scienceRawPacket[i]);
+            scienceRawPacket[i] = 0;
+        }
+        sr.printf("\r\r\n");
+}
\ No newline at end of file
--- a/RTC.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/RTC.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
-#include "func_head.h"
+#include "RTC.h"
+#include "all_funcs.h"
 
 SPI rtc_spi(PTD6,PTD7,PTD5);      // MOSI,MISO, CLOCK microcontroller(in order)                                                       
 DigitalOut rtc_cs(PTD4);         //Slave Select pin 
--- a/SDCard.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/SDCard.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,5 +1,7 @@
+#include "mbed.h"
+#include "mbed_debug.h"
 #include "SDCard.h"
-#include "func_head.h"
+#include "all_funcs.h"
 
 SPI spi_SD(PTD6, PTD7, PTD5); // mosi, miso, sclk
 DigitalOut cs_SD(D2);
@@ -31,15 +33,14 @@
  
  // send CMD8 to determine whther it is ver 2.x
     int r = cmd8();
-    if (r == R1_IDLE_STATE) {
+    if (r == R1_IDLE_STATE) 
+    {
         printf("Entering v2 bro\r\n");
         return initialise_card_v2();
         
-    } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
-        printf("Entering v1 bro\r\n");
-        return initialise_card_v1();
-        
-    } else {
+    }   
+    else 
+    {
         debug("Not in idle state after sending CMD8 (not an SD card?)\r\n");
         return SDCARD_FAIL;
     }
--- a/SDCard.h	Fri Jul 03 08:38:12 2015 +0000
+++ b/SDCard.h	Sat Jul 04 08:56:34 2015 +0000
@@ -1,7 +1,3 @@
-#include "mbed.h"
-#include "mbed_debug.h"
- 
- 
 #define SD_COMMAND_TIMEOUT 5000
  
 #define SD_DBG             0
@@ -14,35 +10,7 @@
 #define R1_ADDRESS_ERROR        (1 << 5)
 #define R1_PARAMETER_ERROR      (1 << 6)
  
- 
-int initialise_card();
-int initialise_card_v1();
-int initialise_card_v2();
-int disk_initialize();
-int FCTN_WR_SD(const uint8_t *, uint64_t);
-int FCTN_RD_SD(uint8_t *, uint64_t);
-int cmd(int, int);
-int cmd58();
-int cmdx(int, int);
-int cmd8();
-int *FCTN_INIT_SD();
- 
-uint64_t sd_sectors();
-
- 
-int read(uint8_t*, uint32_t );
-int write(const uint8_t*, uint32_t );
-static uint32_t ext_bits(unsigned char *, int , int );
- 
-
- 
- 
 #define SDCARD_FAIL 0
 #define SDCARD_V1   1
 #define SDCARD_V2   2
-#define SDCARD_V2HC 3
- 
-
-
- 
- 
\ No newline at end of file
+#define SDCARD_V2HC 3
\ No newline at end of file
--- a/TC.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/TC.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,14 +1,12 @@
 #include "mbed.h"
 #include "TC.h"
-#include "SDCard.h"
-#include "func_head.h"
-#include "i2c.h"
+#include "all_funcs.h"
 
 Serial sc(USBTX,USBRX);
    
 void FCTN_CDMS_RLY_TC()
 {
-    sc.printf("in FCTN_CDMS_RLY_TC\r\n");
+    sc.printf("\rin FCTN_CDMS_RLY_TC\r\n");
     uint8_t read;
     sc.scanf("%d",&read); //Packet sequence count
     pac_seq_cnt = read;
--- a/TM.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/TM.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,6 +1,10 @@
 #include "mbed.h"
-Serial kc(USBTX,USBRX);
+#include "TM.h"
+#include "all_funcs.h"
+
+Serial tmy(USBTX,USBRX);
+
 void FCTN_CDMS_RLY_TM()
 {
-        kc.printf("in FCTN_CDMS_RLY_TM\r\n");
+        tmy.printf("in FCTN_CDMS_RLY_TM\r\n");
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/all_funcs.h	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,83 @@
+/*PL functions*/
+void FCTN_PL_INIT();
+void FCTN_PL_MAIN();
+void FCTN_PL_RCV_SC_DATA();
+/*End*/
+
+/*ISR functions*/
+void ISR_PL_RCV_SC_DATA();
+void ISR_CDMS_RLY_TC();
+void ISR_CDMS_RLY_TM();
+/*End*/
+
+/*CDMS functions*/
+void FCTN_CDMS_INIT();
+void FCTN_CDMS_MAIN();
+/*End*/
+
+/*Flash functions*/
+void FCTN_CDMS_RD_FLASH();
+void FCTN_CDMS_WR_FLASH();
+void FCTN_CDMS_GET_FLASH_CRC();
+/*End*/
+
+/*RAM functions*/
+void FCTN_CDMS_RD_RAM();
+void FCTN_CDMS_WR_RAM();
+/*End*/
+
+/*RTC functions*/
+void FCTN_INIT_RTC();
+void FCTN_CDMS_RD_RTC(uint8_t *);
+/*End*/
+
+/*I2C functions*/
+void FCTN_I2C_READ(char *data,int length = 1);
+void FCTN_I2C_WRITE(char *data,int length = 1);
+void FCTN_MASTER_I2C(char i2c_data_type , char* i2c_data);
+/*End*/
+
+/*SDCard functions*/
+int *FCTN_INIT_SD();
+int FCTN_WR_SD(const uint8_t *, uint64_t);
+int FCTN_RD_SD(uint8_t *, uint64_t);
+void FCTN_CDMS_MNG_SD();
+int initialise_card();
+int initialise_card_v2();
+int disk_initialize();
+int cmd(int, int);
+int cmd58();
+int cmdx(int, int);
+int cmd8();
+uint64_t sd_sectors();
+int read(uint8_t*, uint32_t );
+int write(const uint8_t*, uint32_t );
+static uint32_t ext_bits(unsigned char *, int , int );
+/*End*/
+
+/*COM functions*/
+void FCTN_COM_INIT();
+void FCTN_COM_RCV_TC();
+void FCTN_COM_MNGR_TMTC();
+void FCTN_COM_SND_TM();
+void FCTN_COM_SD_TM();
+void FCTN_COM_COMP_SC_DATA();
+/*End*/
+
+/*HK functions*/
+void FCTN_CDMS_HK_INIT();
+char* FCTN_CDMS_HK_MAIN();
+int quantiz(float l_start,float l_step,float l_x);
+void FCTN_HK_MAIN();
+/*End*/
+
+/*Power functions*/
+void FCTN_POWER_MODE(int SOC);
+void FCTN_CDMS_SWCH_PWR();
+void FCTN_CDMS_RST_PWR();
+/*End*/
+
+/*TCTM functions*/
+void FCTN_CDMS_RLY_TC();
+void FCTN_CDMS_RLY_TM();
+/*End*/
\ No newline at end of file
--- a/func_head.h	Fri Jul 03 08:38:12 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-void FCTN_CDMS_INIT();
-void FCTN_CDMS_MAIN();
-void FCTN_CDMS_RLY_TC();
-void FCTN_CDMS_RLY_TM();
-void FCTN_CDMS_RUN_PRCS();
-void FCTN_CDMS_WR_FLASH();
-void FCTN_CDMS_MNG_SD();
-void FCTN_CDMS_RD_FLASH();
-void FCTN_CDMS_RD_RAM();
-void FCTN_CDMS_SWCH_PWR();
-void FCTN_CDMS_RST_PWR();
-void FCTN_CDMS_RD_RTC(uint8_t *);
-void TSC_CDMS_HK_MAIN();
-void FCTN_CDMS_GET_FLASH_CRC();
-void FCTN_PL_INIT();
-void FCTN_PL_MAIN();
-void FCTN_PL_RCV_SC_DATA();
-void FCTN_COM_INIT();
-void FCTN_COM_RCV_TC();
-void FCTN_COM_MNGR_TMTC();
-void FCTN_COM_SND_TM();
-void FCTN_COM_SD_TM();
-void FCTN_COM_COMP_SC_DATA();
-void ISR_PL_RCV_SC_DATA();
-void ISR_CDMS_RLY_TC();
-void ISR_CDMS_RLY_TM();
-
-void FCTN_INIT_RTC();
--- a/hk_cdms.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,105 +0,0 @@
-#include "hk_cdms.h"
-#include "pin_config.h"
- 
- 
-Serial hk_cdms(USBTX, USBRX);
-//DigitalOut Select_Lines_C[]={PIN85,PIN84,PIN39,PIN38};  //to mux on IF board,from LSB(Select_Lines_C[3]) to MSB(Select_Lines_C[0])
-DigitalOut Select_Lines_C[]={D7,D6,D5,D4};
-AnalogIn Temperature_voltage_Input(PIN53);  //output from IF mux
-//AnalogIn Sensor_Input(PIN53);    //output from temperature sensor on CDMS
-char CDMS_HK_DATA[8];
- 
-void FCTN_CDMS_HK_INIT()
-{
-    Select_Lines_C[0] = Select_Lines_C[1] = Select_Lines_C[2] = Select_Lines_C[3] = 0;  //initialisation of variables
-}
- 
-int quantiz(float l_start,float l_step,float l_x)       // accepts min and measured values and step->quantises on a scale 0-15..(4 bit quantisation)
-{
-    int l_y = (l_x - l_start)/l_step;
-        
-    if(l_y <= 0)
-            l_y = 0;
-            
-    if(l_y >= 15)
-            l_y = 15;
-            
-    return l_y;
-} 
- 
-Sensor_Data Sensor; 
-Sensor_Data_Quantised Sensor_Quantised;
- 
-char* FCTN_CDMS_HK_MAIN()
-{
-    float l_resistance_thermistor;  //declaration of variables
-    float l_voltage_thermistor;
-    float l_Payload_voltage;
-    int l_Loop_Iterator = 0;
-    int l_Select_Line_Iterator = 3;
- 
-    for(l_Loop_Iterator = 0; l_Loop_Iterator < 16; l_Loop_Iterator++)
-    {
- 
-        l_voltage_thermistor = Temperature_voltage_Input.read()*3.3;       //voltage across thermistor
-        l_resistance_thermistor = 24000*l_voltage_thermistor/(3.3 - l_voltage_thermistor);           //resistance of thermistor
-        l_Payload_voltage = Temperature_voltage_Input.read()*3.3;
-               
-        Sensor.l_Temperature_thermistor[(l_Loop_Iterator)] = 0.00000004*l_resistance_thermistor*l_resistance_thermistor - 0.0039*l_resistance_thermistor + 347.97;
-        Sensor.l_Temperature_sensor[l_Loop_Iterator] = (-90.7*3.3*Temperature_voltage_Input.read() + 190.1543);  
-              
-        if(l_Loop_Iterator % 2 == 0) 
-        {
-             if(l_Loop_Iterator < 4)
-             Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] = quantiz(tstart_thermistor,tstep_thermistor,Sensor.l_Temperature_thermistor[(l_Loop_Iterator)]);
-               
-             else  
-             {
-                if(4 < l_Loop_Iterator < 14)
-                Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] <<4 + quantiz(tstart,tstep,Sensor.l_Payload_voltage[l_Loop_Iterator]); 
-                
-                else
-                Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] = quantiz(tstart,tstep,Sensor.l_Temperature_sensor[l_Loop_Iterator]); 
-             }
-        }
-                                              
-        else
-        {
-            
-            if(l_Loop_Iterator < 4)
-            Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] <<4 + quantiz(tstart_thermistor,tstep_thermistor,Sensor.l_Temperature_thermistor[l_Loop_Iterator]);
-            
-            else
-            {
-                if(4 < l_Loop_Iterator < 14)
-                Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] = quantiz(tstart,tstep,Sensor.l_Payload_voltage[l_Loop_Iterator]);
-                
-                else
-                Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] <<4 + quantiz(tstart,tstep,Sensor.l_Temperature_sensor[l_Loop_Iterator]); 
-        
-            }
-        }
-    
-       
-          
-       // The following lines are used to iterate the select lines from 0 to 15
-       for(l_Select_Line_Iterator = 3;l_Select_Line_Iterator >= 0;l_Select_Line_Iterator--)
-       {
-            if(Select_Lines_C[l_Select_Line_Iterator] == 0)
-            {
-                Select_Lines_C[l_Select_Line_Iterator] = 1;
-                break;
-            }
-            else Select_Lines_C[l_Select_Line_Iterator] = 0;
-            printf("%d\n",l_Select_Line_Iterator);
-       }
-      
-        wait_us(10.0); //  A delay of 10 microseconds between each sensor output. Can be changed.
-    }
-     printf("vol %f temp %f",Temperature_voltage_Input.read()*3.3,-90.7*3.3*Temperature_voltage_Input.read() + 190.1543 );
-     printf("CDMS_HK Data is Temperature_sensor=%s, l_Temperature_thermistor=%s, Payload_voltage=%s ",Sensor_Quantised.l_Temperature_sensor,Sensor_Quantised.l_Temperature_thermistor,Sensor_Quantised.l_Payload_voltage);
-     strcpy (CDMS_HK_DATA,Sensor_Quantised.l_Temperature_sensor);
-     strcat (CDMS_HK_DATA,Sensor_Quantised.l_Temperature_thermistor);
-     strcat (CDMS_HK_DATA,Sensor_Quantised.l_Payload_voltage);
-     return(CDMS_HK_DATA);
-}
\ No newline at end of file
--- a/hk_cdms.h	Fri Jul 03 08:38:12 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#include "mbed.h"
- 
-#define tstart -40
-#define tstep 8
-#define tstep_thermistor 8
-#define tstart_thermistor -40
- 
-typedef struct Sensor_Data
-{
-    
-    float l_Temperature_sensor[2];
-    float l_Temperature_thermistor[4]; //read by thermistors on solar panels
-    float l_Payload_voltage[9]; //for payload analog signals
- 
-} Sensor_Data;
- 
-typedef struct Sensor_Data_Quantised
-{
-    char l_Temperature_sensor[1];
-    char l_Temperature_thermistor[2];
-    char l_Payload_voltage[5];
-    
-} Sensor_Data_Quantised;
- 
-void FCTN_CDMS_HK_INIT();
-char* FCTN_CDMS_HK_MAIN();
- 
-int quantiz(float l_start,float l_step,float l_x);
--- a/i2c.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/i2c.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 //#include "pin_config.h"
 #include "i2c.h"
+#include "all_funcs.h"
 
 Serial gc(USBTX,USBRX);
 InterruptIn irpt_4m_slv(D9);                                      //I2c interrupt from CDMS
--- a/i2c.h	Fri Jul 03 08:38:12 2015 +0000
+++ b/i2c.h	Sat Jul 04 08:56:34 2015 +0000
@@ -1,3 +0,0 @@
-void FCTN_I2C_READ(char *data,int length = 1);
-void FCTN_I2C_WRITE(char *data,int length = 1);
-void FCTN_MASTER_I2C(char i2c_data_type , char* i2c_data);
\ No newline at end of file
--- a/main.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/main.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,49 +1,12 @@
 /*Header Files*/
 #include "mbed.h"
 #include "rtos.h"
-#include "func_head.h"
-#include "i2c.h"
-#include "SDCard.h"
-#include "hk_cdms.h"
-#include "power.h"
-/*End*/
-
-/*Hash define parameters*/
-#define HK_DATA 25 //Total Hk data that is to be stored in SD card
-#define HK_ITER 5  //The iterations after which the HK data will be stored in the SD card 
-/*End*/
-
-Serial pc(USBTX,USBRX);
-
-/*Configuring pins*/
-InterruptIn pl_sc_data(PTC3);           //interrupt from payload to send science data
-InterruptIn cdms_rcv_tc(PTC12);         //interrupt from comm b4 sending tc
-InterruptIn bae_tm_rcv(PTC11);          //interrupt from bae b4 sending tm
-InterruptIn pl_tm_rcv(PTC10);           ////interrupt from bae b4 sending tm
+#include "main.h"
+#include "Flags.h"
+#include "all_funcs.h"
 /*End*/
 
-/*Delclaring extern objects*/
-extern I2C master;
-extern DigitalOut irpt_2_slv;
-extern SPISlave pl_spi ; // mosi, miso, sclk, ssel --> using SPI1
-/*End*/
-
-/*Declaring global variables*/
-char i2c_data[25];
-int hk_count=0;
-uint8_t hk_data[512];
-uint8_t rtc_data[8];
-uint64_t hk_block_number = 1;
-/*End*/
-
-/*Threads used*/
-Thread *ptr_t_hk_acq;              //pointer to read state of one thread from another
-Thread *ptr_t_sc_data;
-Thread *ptr_t_tc;
-Thread *ptr_t_tm;
-Thread *ptr_t_fault;
-Thread *ptr_t_wdt;
-/*End*/
+Serial pc(USBTX, USBRX);
 
 /*All Threads*/
 void T_CDMS_RLY_TC(void const *args)
@@ -61,7 +24,7 @@
     while(1)
     {
         Thread::signal_wait(0x3);
-        pc.printf("in T_CDMS_RLY_TM\r\n");
+        pc.printf("\rin T_CDMS_RLY_TM\r\n");
         FCTN_CDMS_RLY_TM();
     }
 }
@@ -71,50 +34,24 @@
     while(1)
     {
         Thread::signal_wait(0x1);
-        pc.printf("in T_PL_RCV_SC_DATA\r\n");
+        pc.printf("\rin T_PL_RCV_SC_DATA\r\n");
         FCTN_PL_RCV_SC_DATA();
     }
 }
 /*End*/
 
 /*RTOS Scheduler*/         
-void TSC_CDMS_HK_MAIN(void const *args)
+void T_SC(void const *args)
 {
-    pc.printf("in FCTN_CDMS_HK_MAIN()\r\n");
-    FCTN_MASTER_I2C('h' , i2c_data ); //BAE hk data is collected
-    FCTN_CDMS_RD_RTC(rtc_data);  //RTC data is read to the rtc_data.
-    for(int i=(hk_count%(HK_ITER+1))*(HK_DATA+8);i<(hk_count%(HK_ITER+1))*(HK_DATA+8)+8;i++)
-    {
-        hk_data[i] = rtc_data[i-(hk_count%(HK_ITER+1))*(HK_DATA+8)];
-    }
-    for(int i=(hk_count%(HK_ITER+1))*(HK_DATA+8)+8;i<(hk_count%(HK_ITER+1))*(HK_DATA+8)+8+HK_DATA;i++)
-    {
-        hk_data[i] = i2c_data[i-(hk_count%(HK_ITER+1))*(HK_DATA+8)+8];
-    }
-    hk_count++;
-    if(hk_count%HK_ITER==0)
+    sc_timer++;
+    if(sc_timer%18==0)
     {
-        for(int i=(hk_count%(HK_ITER+1))*(HK_DATA+8)+8+HK_DATA;i<512;i++)
-        {
-            hk_data[i] = '/0';
-        }
-        hk_count = 0;
-        FCTN_WR_SD(hk_data,hk_block_number); // HK data is written to the SD card 
-        for(int i=0;i<512;i++)
-        {
-            hk_data[i] = '/0';
-        }
-        FCTN_RD_SD(hk_data,hk_block_number);
-        hk_block_number++;
-        printf("\n\r sd card hk data\n");
-        for(int i=0;i<512;i++)
-        {
-            pc.printf("%d|",hk_data[i]);
-        }
-       // FCTN_CDMS_HK_MAIN();
-       FCTN_POWER_MODE(80);          //to check dummy power algo 
+        FCTN_CDMS_HK_MAIN;
     }
-    pc.printf("\n\r hk exited\n");
+    if(sc_timer%9==0)
+    {
+        FCTN_PL_MAIN;
+    }
 }
 /*End*/
 
@@ -143,7 +80,7 @@
     ptr_t_tm = new Thread (T_CDMS_RLY_TM);
     ptr_t_sc_data = new Thread (T_PL_RCV_SC_DATA);
     master.frequency(100000);
-    RtosTimer TIMER_HK_ACQ(TSC_CDMS_HK_MAIN,osTimerPeriodic);
+    RtosTimer TIMER_HK_ACQ(T_SC,osTimerPeriodic);
     TIMER_HK_ACQ.start(10000);
     irpt_2_slv = 1;
     pl_sc_data.rise(&ISR_PL_RCV_SC_DATA);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Sat Jul 04 08:56:34 2015 +0000
@@ -0,0 +1,25 @@
+/*Configuring pins*/
+InterruptIn pl_sc_data(PTC3);           //interrupt from payload to send science data
+InterruptIn cdms_rcv_tc(PTC12);         //interrupt from comm b4 sending tc
+InterruptIn bae_tm_rcv(PTC11);          //interrupt from bae b4 sending tm
+InterruptIn pl_tm_rcv(PTC10);           ////interrupt from bae b4 sending tm
+/*End*/
+
+/*Delclaring extern objects*/
+extern I2C master;
+extern DigitalOut irpt_2_slv;
+extern SPISlave pl_spi ; // mosi, miso, sclk, ssel --> using SPI1
+/*End*/
+
+/*Declaring global variables*/
+uint64_t sc_timer = 0;
+/*End*/
+
+/*Threads used*/
+Thread *ptr_t_hk_acq;              //pointer to read state of one thread from another
+Thread *ptr_t_sc_data;
+Thread *ptr_t_tc;
+Thread *ptr_t_tm;
+Thread *ptr_t_fault;
+Thread *ptr_t_wdt;
+/*End*/
--- a/power.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ b/power.cpp	Sat Jul 04 08:56:34 2015 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "pin_config.h"
+#include "all_funcs.h"
 
 #define COMM_TX_EN_PIN PIN64                 //COMM receiver enable
 #define PYLD_EN_PIN PIN73                    // Payload switch enable
--- a/power.h	Fri Jul 03 08:38:12 2015 +0000
+++ b/power.h	Sat Jul 04 08:56:34 2015 +0000
@@ -1,1 +0,0 @@
-void FCTN_POWER_MODE(int SOC);
\ No newline at end of file
--- a/science.cpp	Fri Jul 03 08:38:12 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#include "mbed.h"
-#include "func_head.h"
-
-Serial sr(USBTX,USBRX);
-
-SPISlave pl_spi(PTE18, PTE19, PTE17, PTE16 ); // mosi, miso, sclk, ssel --> using SPI1
-//SPISlave pl_spi(PTD6, PTD7, PTD5, PTD4   ); // mosi, miso, sclk, ssel --> using SPI1
-int payloadBins = 4;
-
-void FCTN_PL_RCV_SC_DATA()
-{
-        sr.printf("in FCTN_PL_RCV_SC_DATA\r\n");
-        uint8_t scienceRawPacket[payloadBins];
-        for(int i=0; i<payloadBins;i++)
-        {
-            while(!pl_spi.receive()); // blocking statement --> waiting for data from Payload
-            uint8_t v = pl_spi.read();   // Read byte from master
-            scienceRawPacket[i] = v;
-        }
-        // serial feedback
-        sr.printf("Packet recieved\r\r\n");
-        for(int i=0;i<payloadBins;i++)
-        {
-            sr.printf("%0x\t",scienceRawPacket[i]);
-            scienceRawPacket[i] = 0;
-        }
-        sr.printf("\r\r\n");
-}
\ No newline at end of file