samp Srinivasan / Mbed 2 deprecated CDMS_CODE_FROM13JAN2017

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Compression.h Source File

Compression.h

00001 /*-----------------------------------------to_do_compression.h---------------------------------------------------------------
00002 
00003     -> SD_Write error to be checked(refer to flowchart)
00004     -> compression algo not yet changed
00005     -> inform Nandu that in extreme data conservation mode alone, even if pzf,ezf=1 the bins are present but have junk values
00006     -> attitude tag not updated anywhere
00007     -> packet_pp to be verified
00008 -----------------------------------------------------------------------------------------------------------------------------*/
00009 
00010 //last updated on 18 Dec 2016
00011 
00012 /*#include <iostream>
00013 #include "stdio.h"
00014 #include "crc.h"
00015 #include "interleave.h"
00016 #include "Convolution.h"
00017 #include "bitset"*/
00018 //using namespace std;
00019 
00020 //reading functions 2byte, 4byte
00021 #define srpz 0
00022 #define scpz 0
00023 #define scpz_last 0
00024 #define srpz_last 0
00025 #define debug_fsc 0
00026 #define debug_time 0    //used for testing without rtc, assigns constant time to all frames
00027 #define debug_dma 0     //byte reading order in dma, used to read test_cases 4 to 16 since they're in the wrong order
00028 #define test_science 0
00029 #define tabulation 0
00030 uint8_t HK_timer_toggle = 0;
00031 
00032 Timer timer_test;
00033 
00034 #if debug_dma
00035 uint16_t read_2byte(uint8_t* ptr)
00036 {
00037     uint16_t output = (((uint16_t )(*(ptr+1)))<<8);
00038     output += (((uint16_t)(*ptr)));
00039     return output;
00040 }
00041 uint32_t read_4byte(uint8_t* ptr)
00042 {
00043     uint32_t output = (uint32_t)(*(ptr+3))<<24;
00044     output += (uint32_t)(*(ptr+2)<<16);
00045     output += (uint32_t)(*(ptr+1)<<8);
00046     output += (uint32_t)(*(ptr));
00047     return output;
00048 }
00049 #endif
00050 #if !debug_dma
00051 uint16_t read_2byte(uint8_t* ptr)
00052 {
00053     uint16_t output = (uint8_t ) *(ptr+1);
00054     output += ( (uint8_t )(*ptr) ) << 8;
00055     return output;
00056 }
00057 uint32_t read_4byte(uint8_t* ptr)
00058 {
00059     uint32_t output = (uint32_t) (*(ptr+3));
00060     output += (uint32_t)(*(ptr+2)<<8);
00061     output += (uint32_t)(*(ptr+1)<<16);
00062     output += (uint32_t)(*(ptr)<<24);
00063     return output;
00064 }
00065 #endif
00066 uint8_t adjust(uint8_t size, uint8_t data, uint8_t* ptr , uint8_t space)
00067 {
00068     space = space&0x0f;
00069     if(space == 8)  *ptr = 0;
00070     data = data&((1<<size)-1);
00071     if(space >= size){
00072         *ptr += data<<(space-size);
00073         if(space - size == 0){
00074             return 0x18;
00075         }else{
00076             return space-size;
00077         }
00078     }else{
00079         ptr[0] += data>>(size-space);
00080         ptr[1] = (data<<(8-(size-space)))&0xff ;
00081         return 0x10 + 8-(size - space);
00082     }
00083 }
00084 int compress (int data, int x, int y)   //to be compressed with scheme (msb x)*4^y ;
00085 {
00086     for(int i = 0 ; i < (1<<y) ; i++)
00087     {
00088         if(data <= ( (1<<x)-1) * (1<<(2*i)) )
00089         {
00090             return ( ((data>>i*2)<<y) + i);
00091         }
00092     }
00093     if ( data > (( (1<<x)-1) * (1<<(2*((1<<y)-1)))) )       //if value of data is more than that can be stored by compressed bits then return maximum value
00094     {
00095         //cout <<"compression exception"<<endl;
00096         return (1<<(x+y))-1;    //maximum value is nothing but a number with(x+y) bits with all 1's
00097     }
00098 }
00099 int compress2 (int data, int x, int y)  //to be compressed with scheme (msb x)*8^y;
00100 {
00101     for(int i = 0 ; i < (1<<y) ; i++)
00102     {
00103         if(data <= ( (1<<x)-1) * (1<<(3*i)) )
00104         {
00105             return ( ((data>>i*3)<<y) + i);
00106         }
00107     }
00108     if ( data > (( (1<<x)-1) * (1<<(3*((1<<y)-1)))) )       //if value of data is more than that can be stored by compressed bits then return maximum value
00109     {
00110         //cout <<"compression exception"<<endl;
00111         return (1<<(x+y))-1;    //maximum value is nothing but a number with(x+y) bits with all 1's
00112     }   
00113 }
00114 
00115 //--------------------------------------------------------beacon
00116 uint8_t beacon_array[134];
00117 uint8_t beacon_cntr = 1;
00118 uint8_t *beacon_ptr;
00119 //--------------------------------------------------------beacon
00120 
00121 //variable declarations
00122 uint8_t srp_mode , at , pzf , ezf ,sfp[71] ,scp[56],beacon_at; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
00123 uint32_t sfp_bin[52] , scp_bin[52];         //storing the bin values.
00124 uint32_t sfp_threshold_m0[52]  = {124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,4032,252,252,252,252,252,4032,8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128};
00125 uint32_t scp_threshold_m0[52]  = {4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,114688,8128,4032,4032,4032,4032,114688,245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760};
00126 uint32_t scp_threshold_m0_1[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for callibrated mode with data conservation
00127 uint32_t scp_sfp_threshold_m0_2[3] = {245760,65472,65472};
00128 uint32_t sfp_threshold_m1[52]  = {1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,8128,8128,8128};
00129 uint32_t scp_threshold_m1[52]  = {114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,245760,245760,245760,245760};
00130 uint8_t frames[3][134] = {0};
00131 uint8_t position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
00132 uint8_t id;   //sf = 0,sc-at = 1,sc-bt = 2;
00133 uint8_t TM_interleave_data[512] , TM_convoluted_data[270] = {0};
00134 uint64_t proton_scp_sum,electron_scp_sum,FSC_science;
00135 uint16_t temp_crc;
00136 uint32_t debug_cntr, size,space,length;
00137 uint8_t *ptr ,* pointer;
00138 uint64_t sci_time = 0, time_prev_scp = 0;
00139 uint32_t attitude = 0;
00140 uint32_t beacon_threshold[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472};
00141 
00142 /*----------------------------temporary counters used for testing purposes---------------------------*/
00143 uint32_t test_fsc[4]={0,0,0,0};
00144 /*---------------------------------------------------------------------------------------------------*/
00145 
00146 
00147 Convolution ConvObj;
00148 void convolution (uint8_t * ptr){
00149     ConvObj.convolutionEncode(ptr, TM_convoluted_data);
00150     ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
00151 }
00152 #if test_science
00153 char srp_calibrated[][34] = {       //names of different bins in calibrated mode, refer to excel sheet having testcases
00154                             "NA",   //packet sequnce count (not important here)
00155                             "NA",   //science data mode + compression mode (not important here)
00156                             "proton energy",
00157                             "electron energy",
00158                             "Ion dEdX response",
00159                             "Ion bulk response",
00160                             "proton angles [0,40]",
00161                             "proton angles [40,50]",
00162                             "proton angles [50,60]",
00163                             "proton angles [60,70]",
00164                             "proton angles [70,80]",
00165                             "electron angles [0,60]",
00166                             "high energy protons and electrons",
00167                             "proton energy overlap events",
00168                             "dEdX & bulk & Veto counts",
00169                             "dEdX fast counts",
00170                             "bulk fast counts",
00171                             "veto fast counts"
00172 };
00173 char srp_scattered[][33] = {    //names of different bins in scattered mode, refer to excel sheet having testcases
00174                             "NA",
00175                             "NA",
00176                             "dEdX & bulk & not veto 2d dEdX..",
00177                             "dEdX & bulk & veto counts",
00178                             "dEdX fast counts",
00179                             "bulk fast counts",
00180                             "veto fast counts"
00181 };
00182 #endif
00183 uint32_t data_srp_calibrated[] = {  // this data will be copied 60 times(except for the first two elements which are included only at the start of dma) and to fill the dma
00184                         1,      //packet sequence count
00185                         0,      //last bit for calibrated mode and next two bits for compression mode
00186                         20,     //proton energy(18 bins)
00187                         60,     //Electron energy(6 bins)
00188                         700,    //Ion dEdX response
00189                         600,    //Ion bulk response
00190                         100,    //proton angles [0,40]
00191                         200,    //proton angles [40,50]
00192                         500,    //proton angles [50,60]
00193                         600,    //proton angles [60,70]
00194                         720,    //proton angles [70,80]
00195                         246,    //electron angles [0,60]
00196                         10,     //high energy protons and electrons
00197                         250,    //proton energy overlap events
00198                         120,    //dEdX & bulk & Veto counts
00199                         8129,   //dEdX fast counts
00200                         3000,   //bulk fast counts
00201                         4000    //veto fast counts
00202 };
00203 /*uint8_t calib_sfp_at_com[] = {0,0,0,0,700,600,100,200,500,600,720,246,10,250,120,3500,3000,4000};
00204 uint8_t data_sfp_calib_at[][2] = {
00205                                 {20,300}    //64 bytes
00206                                 {300,0},    //58 bytes
00207                                 {0,300},    //43 bytes
00208                                 {0,0}       //36 bytes
00209 };   //36 bytes
00210 uint16_t packet_insert_dma(uint8_t *head,uint8_t size,uint16_t j)
00211 {
00212     if(size==64){
00213         for(count=0;count<18;count++,j+=2){
00214             head[j]   = (calib_sfp_at_com[0][0]>>8) & 0xff;
00215             head[j+1] = (calib_sfp_at_com[0][0]) & 0xff;
00216         }
00217         for(count=0;count<6;count++,j+=2){
00218             head[j]   = (calib_sfp_at_com[0][1]>>8) & 0xff;
00219             head[j+1] = (calib_sfp_at_com[0][1]) & 0xff;
00220         }
00221     }
00222     else if(size==58){
00223         for(count=0;count<18;count++,j+=2){
00224             head[j]   = (calib_sfp_at_com[1][0]>>8) & 0xff;
00225             head[j+1] = (calib_sfp_at_com[1][0]) & 0xff;
00226         }
00227         for(count=0;count<6;count++,j+=2){
00228             head[j]   = (calib_sfp_at_com[1][1]>>8) & 0xff;
00229             head[j+1] = (calib_sfp_at_com[1][1]) & 0xff;
00230         }
00231     }
00232     else if(size==43){
00233         for(count=0;count<18;count++,j+=2){
00234             head[j]   = (calib_sfp_at_com[2][0]>>8) & 0xff;
00235             head[j+1] = (calib_sfp_at_com[2][0]) & 0xff;
00236         }
00237         for(count=0;count<6;count++,j+=2){
00238             head[j]   = (calib_sfp_at_com[2][1]>>8) & 0xff;
00239             head[j+1] = (calib_sfp_at_com[2][1]) & 0xff;
00240         }
00241     }
00242     else if(size==36){
00243         for(count=0;count<18;count++,j+=2){
00244             head[j]   = (calib_sfp_at_com[3][0]>>8) & 0xff;
00245             head[j+1] = (calib_sfp_at_com[3][0]) & 0xff;
00246         }
00247         for(count=0;count<6;count++,j+=2){
00248             head[j]   = (calib_sfp_at_com[3][1]>>8) & 0xff;
00249             head[j+1] = (calib_sfp_at_com[3][1]) & 0xff;
00250         }
00251     }
00252     else{
00253         gPC.printf("\n\rError: Invalid length in inserting packet DMA");
00254         return 3;
00255     }
00256     for(count=0;count<4;count++,j+=2){
00257         head[j]   = (calib_sfp_at_com[4]>>8) & 0xff;
00258         head[j+1] = (calib_sfp_at_com[4]) & 0xff;
00259     }
00260     for(count=0;count<4;count++,j+=2){
00261         head[j]   = (calib_sfp_at_com[5]>>8) & 0xff;
00262         head[j+1] = (calib_sfp_at_com[5]) & 0xff;
00263     }
00264     for(uint8_t count2 = 0;count2<6;count2++)
00265         for(uint8_t count=0;count<2;count++,j+=2){
00266             head[j]   = (calib_sfp_at_com[6+count2]>>8) & 0xff;
00267             head[j+1] = (calib_sfp_at_com[6+count2]) & 0xff;
00268         }
00269     head[j++] = (calib_sfp_at_com[12]>>8) & 0xff;
00270     head[j++] = (calib_sfp_at_com[12]) & 0xff;
00271     for(count=0;count<3;count++,j+=2){
00272         head[j]   = (calib_sfp_at_com[13]>>8) & 0xff;
00273         head[j+1] = (calib_sfp_at_com[13]) & 0xff;
00274     }
00275     for(uint8_t count2 =0;count2<4;count2++)
00276         for(uint8_t count=0;count<1;count++,j+=4){
00277             head[j]   = (calib_sfp_at_com[14+count2]>>24) & 0xff;
00278             head[j+1] = (calib_sfp_at_com[14+count2]>>16) & 0xff;
00279             head[j+2] = (calib_sfp_at_com[14+count2]>>8) & 0xff;
00280             head[j+3] = (calib_sfp_at_com[14+count2]) & 0xff;
00281         }
00282     return j;
00283 }
00284 void generate_dma_tmid2(uint8_t *head)
00285 {
00286     uint8_t dma_index = 3;
00287     head[0] = 0;
00288     head[1] = 1;
00289     head[2] = 0;
00290     for(uint8_t count=1;count<=2;count++){
00291         dma_index = packet_insert_dma(head,64,dma_index);
00292         dma_index = packet_insert_dma(head,58,dma_index);
00293         dma_index = packet_insert_dma(head,43,dma_index);
00294         dma_index = packet_insert_dma(head,36,dma_index);
00295     }
00296 }
00297 */
00298 //uint8_t data_sfp_scat_at10[] ={};
00299 //uint8_t data_sfp_scat_at
00300 /*uint32_t data_srp_calibrated2[] = {  // this data will be copied 60 times(except for the first two elements which are included only at the start of dma) and to fill the dma
00301                         1,      //packet sequence count
00302                         0,      //last bit for calibrated mode and next two bits for compression mode
00303                         1,     //proton energy(18 bins)
00304                         60,     //Electron energy(6 bins)
00305                         700,    //Ion dEdX response
00306                         600,    //Ion bulk response
00307                         100,    //proton angles [0,40]
00308                         200,    //proton angles [40,50]
00309                         500,    //proton angles [50,60]
00310                         600,    //proton angles [60,70]
00311                         720,    //proton angles [70,80]
00312                         246,    //electron angles [0,60]
00313                         10,     //high energy protons and electrons
00314                         250,    //proton energy overlap events
00315                         120,    //dEdX & bulk & Veto counts
00316                         3500,   //dEdX fast counts
00317                         3000,   //bulk fast counts
00318                         4000    //veto fast counts
00319 };*/
00320 uint32_t data_srp_scattered[] = {// this data will be copied 60 times(except for the first two elements which are included only at the start of dma) and to fill the dma
00321                         1,      //packet sequence count
00322                         5,      //last bit for scattered plot mode and next two bits for compression mode
00323                         1980,    //dEdX & bulk & not veto 2d dEdX bulk response
00324                         10,     //dEdX & bulk & veto counts
00325                         8100,   //dEdX fast counts
00326                         1500,   //bulk fast counts
00327                         10295    //veto fast counts 
00328 };
00329 #if test_science
00330 uint8_t dma_sfp_at_count=0,dma_scp_at_count=0,calib_thres_index[16][60],scat_thres_index[5][60],calib_thres_index_scp[16],scat_thres_index_scp[5];
00331 uint8_t calib_thres_subindex[16],scat_thres_subindex[5],thres_scp_index[16],proton_scp_sum_at,electron_scp_sum_at,sfp_mode_count[2],scp_mode_count[5];
00332 #endif
00333 #if tabulation
00334 uint8_t pack_curr_det,pack_prev_det,pack_split_det= 0,pack_buf[3][70] = {0};
00335 uint8_t fsc_incomplete[3]={0},len_prev[3]={0},len_correct[3]={0},len_current;
00336 
00337 //uint8_t test_tm[3][134] = {0};
00338 uint64_t decompress(uint16_t data, uint8_t x,uint8_t y){    //x * 4^y
00339     uint64_t value = 0;
00340     value = ((data>>y)&((1<<x)-1))*(1<<(2*(data &((1<<y)-1))));
00341     return value;
00342 }
00343 uint64_t decompress2(uint16_t data, uint8_t x,uint8_t y){   //x * 8^y for proton and electron angles bins in SCP_AT(calibrated)
00344     uint64_t value = 0;
00345     value = ((data>>y)&((1<<x)-1))*(1<<(3*(data &((1<<y)-1))));
00346     return value;
00347 }
00348 uint8_t calc_length(uint8_t tmid,uint8_t srp_mode,uint8_t comp_opt,uint8_t zf){
00349     uint16_t length;
00350     if(tmid==1){
00351         if(srp_mode == 0 && (comp_opt>>1 & 0x03) == 0){
00352             if((comp_opt&0x01) == 0){
00353                 length = 228;
00354                 if(((zf>>1)&0x01) == 0)
00355                     length += 121;
00356                 if((zf&0x01) == 0)
00357                     length +=41;
00358             }else if((comp_opt&0x01) == 1){
00359                 length = 266;
00360                 if(((zf>>1)&0x01) == 0)
00361                     length += 136;
00362                 if((zf&0x01) == 0)
00363                     length += 40;
00364             }
00365         }else if(srp_mode == 0 && (comp_opt>>1 & 0x03) == 1){                         //data conservation mode
00366             if((comp_opt&0x01) == 0){
00367                 length = 94;
00368                 if(((zf>>1)&0x01) == 0)
00369                     length += 12;
00370                 if((zf&0x01) == 0)
00371                     length += 12;
00372             }else if((comp_opt&0x01) == 1){
00373                 length = 123;
00374                 if(((zf>>1)&0x01) == 0)
00375                     length += 9;
00376                 if((zf&0x01) == 0)
00377                     length += 9;
00378             }
00379         }
00380         else if( srp_mode == 1){
00381             if((comp_opt&0x01) == 0)
00382                 length = 368;
00383             else if((comp_opt&0x01) == 1)
00384                 length = 432;
00385         }
00386         else if(((comp_opt>>1 & 0x03)==2)&&(srp_mode==0))
00387         {                         
00388             if((comp_opt&0x01) == 0){
00389                 length = 31;
00390                 if(((zf>>1)&0x01) == 0)
00391                     length += 12;
00392                 if((zf&0x01) == 0)
00393                     length += 12;
00394             }else if((comp_opt&0x01) == 1){
00395                 length = 36;
00396                 if(((zf>>1)&0x01) == 0)
00397                     length += 9;
00398                 if((zf&0x01) == 0)
00399                     length += 9;
00400             }
00401         }
00402         else if(((comp_opt>>1 & 0x03)==2)&&(srp_mode==1))
00403         {
00404             length = 56;
00405         }
00406     }
00407     else if(tmid==2){    
00408         if(srp_mode==0){
00409             length = 288;
00410             if(((zf>>1)&0x01) == 0)
00411                 length += 170;
00412             if((zf&0x01) == 0)
00413                 length += 50;            
00414         }
00415         else if(srp_mode==1)
00416             length = 568;
00417     }
00418     else if(tmid==3){
00419         if(srp_mode==0){
00420             length = 241;
00421             if(((zf>>1)&0x01) == 0)
00422                 length += 87;
00423             if((zf&0x01) == 0)
00424                 length += 35;
00425         }
00426         else if(srp_mode==1)
00427             length = 384;
00428     }
00429     length = (length%8==0)?(length/8):(length/8)+1;
00430     return (uint8_t)length;
00431 }
00432 uint64_t decode(uint8_t size,uint8_t *space,uint8_t **ptr){    
00433     /*  This function accepts a double pointer, pointing to the header pointer of an array,
00434         example if TM[135] is a telemetry array, then create a new "temporary" pointer as ptr=&TM and pass &ptr to the function
00435         Given this pointer and size, this function reads the number of bits(=size) and returns the decimal value and updates (*space).
00436         "space" is nothing but how many bits have been not been read in the current byte.
00437         example: when reading an array for the first time,(*space) will be 8 coz none of the bits have been decoded, once we decode a few bits(say 5), then (*space) will be 3
00438         This function can be used to decode bins(which have varying number of bits) from telemetry 
00439     */
00440     uint64_t data = 0;
00441     while(size!=0){
00442         if((*space)>=size){
00443             data |= (((**ptr)>>((*space)-size)) & ((1<<size)-1));
00444             (*space) = (*space)-size;
00445             size = 0;
00446         }
00447         else{
00448             data |= (((**ptr) & ((1<<(*space))-1))<<(size-(*space)));
00449             size -= (*space);
00450             (*space) = 8;
00451             (*ptr)++;
00452         }
00453     }
00454     return data;
00455 }
00456 uint8_t print_packet(uint8_t tmid,uint8_t *ptr)
00457 {
00458     //prints each bin values of a packet in a row separated by tab spaces and returns the length of the packet printed
00459     uint8_t *temp,space;
00460     uint8_t len = 0;
00461     temp = ptr; space = 8;
00462     uint8_t temp_x,srp_mode;                    //temp_x = header(compression option)
00463     uint8_t pzf=0,ezf=0;
00464     if(tmid==1)                                 //SCP packet
00465     {
00466         gPC.printf("\n\r%d",decode(1,&space,&temp));
00467         gPC.printf("\t%d",decode(6,&space,&temp));
00468         gPC.printf("\t%d",decode(7,&space,&temp));        
00469         gPC.printf("\t%d",decode(4,&space,&temp));        
00470         temp_x = decode(3,&space,&temp);
00471         gPC.printf("\t%d",temp_x);
00472         srp_mode = decode(1,&space,&temp);
00473         gPC.printf("\t%d",srp_mode);
00474         if((temp_x>>1 & 0x03)!=2){
00475             gPC.printf("\t%d",decode(24,&space,&temp)); //SFP_AT starting counter not present in extreme conservation mode        
00476         }
00477         if(srp_mode==0){    //scp calibrated
00478             pzf = decode(1,&space,&temp);
00479             ezf = decode(1,&space,&temp);
00480             gPC.printf("\t%d",pzf);
00481             gPC.printf("\t%d",ezf);
00482             len = calc_length(tmid,srp_mode,temp_x,((uint8_t)((pzf<<1)|ezf)));
00483             if(temp_x==0){
00484                 for(uint8_t i=0;i<4;i++){
00485                     if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(7,&space,&temp),4,3));    if(i!=3){gPC.printf(",");}
00486                 }
00487                 for(uint8_t i=0;i<4;i++){
00488                     if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(7,&space,&temp),4,3));    if(i!=3){gPC.printf(",");}
00489                 }
00490                 for(uint8_t i=0;i<12;i++){
00491                     if(i%2==0){gPC.printf("\t");}   gPC.printf("%d",decompress(decode(5,&space,&temp),2,3));    if(i%2==0){gPC.printf(",");}
00492                 }            
00493             }
00494             if(temp_x==1){
00495                 for(uint8_t i=0;i<4;i++){
00496                     if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(7,&space,&temp),4,3));    if(i!=3){gPC.printf(",");}
00497                 }
00498                 for(uint8_t i=0;i<4;i++){
00499                     if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(7,&space,&temp),4,3));    if(i!=3){gPC.printf(",");}
00500                 }
00501                 for(uint8_t i=0;i<12;i++){
00502                     if(i%2==0){gPC.printf("\t");}   gPC.printf("%d",decompress2(decode(5,&space,&temp),2,3));    if(i%2==0){gPC.printf(",");}
00503                 }
00504             }
00505             if(temp_x==0){                                  //SCP calibrated below threshold                
00506                 gPC.printf("\t%d",decompress(decode(6,&space,&temp),3,3)); 
00507                 for(uint8_t i=0;i<3;i++){
00508                     if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(6,&space,&temp),3,3));    if(i!=2){gPC.printf(",");}
00509                 }
00510                 for(uint8_t i=0;i<4;i++){
00511                     gPC.printf("\t%d",decompress(decode(7,&space,&temp),4,3));
00512                 }
00513                 for(uint8_t i=0;i<2;i++)
00514                     gPC.printf("\t%d",decompress(decode(6,&space,&temp),3,3));
00515                 if(pzf==0){
00516                     for(uint8_t i=0;i<2;i++){
00517                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(8,&space,&temp),6,2));    if(i==0){gPC.printf(",");}
00518                     }
00519                     for(uint8_t i=0;i<15;i++){
00520                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(7,&space,&temp),5,2));    if(i!=14){gPC.printf(",");}
00521                     }
00522                 }
00523                 else{
00524                     gPC.printf("\tNA");
00525                     gPC.printf("\tNA");                       
00526                 }
00527                 if(ezf==0){
00528                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),7,2));
00529                     for(uint8_t i=0;i<4;i++){
00530                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(8,&space,&temp),7,2));    if(i!=3){gPC.printf(",");}
00531                     }
00532                 }
00533                 else{
00534                     gPC.printf("\tNA");
00535                     gPC.printf("\tNA");   
00536                 }
00537             }
00538             else if(temp_x==1){                             //SCP calibrated above threshold
00539                 gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00540                 for(uint8_t i=0;i<3;i++){
00541                     if(i==0){gPC.printf("\t");}         gPC.printf("%d",decompress(decode(9,&space,&temp),6,3));    if(i!=2){gPC.printf(",");}
00542                 }
00543                 for(uint8_t i=0;i<4;i++){
00544                     gPC.printf("\t%d",decompress(decode(12,&space,&temp),9,3));
00545                 }
00546                 for(uint8_t i=0;i<2;i++){
00547                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00548                 }
00549                 if(pzf==0){
00550                     for(uint8_t i=0;i<2;i++){
00551                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(8,&space,&temp),5,3));    if(i==0){gPC.printf(",");}
00552                     }
00553                     for(uint8_t i=0;i<15;i++){
00554                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(8,&space,&temp),5,3));    if(i!=14){gPC.printf(",");}
00555                     }
00556                 }
00557                 else{
00558                     gPC.printf("\tNA");
00559                     gPC.printf("\tNA");   
00560                 }
00561                 if(ezf==0){
00562                     gPC.printf("\t%d",decompress(decode(8,&space,&temp),5,3));
00563                     for(uint8_t i=0;i<4;i++){
00564                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(8,&space,&temp),5,3));    if(i!=3){gPC.printf(",");}
00565                     }
00566                 }
00567                 else{
00568                     gPC.printf("\tNA");
00569                     gPC.printf("\tNA");   
00570                 }
00571             }
00572             else if(temp_x ==2){
00573                 gPC.printf("\t%d",decompress(decode(6,&space,&temp),3,3));
00574                 for(uint8_t i=0;i<4;i++){
00575                     gPC.printf("\t%d",decompress(decode(7,&space,&temp),4,3));
00576                 }
00577                 gPC.printf("\t%d",decompress(decode(6,&space,&temp),3,3));
00578                 gPC.printf("\t%d",decompress(decode(6,&space,&temp),3,3));
00579                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),10,2));
00580                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),10,2));
00581             }
00582             else if(temp_x==3){
00583                 gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00584                 for(uint8_t i=0;i<4;i++){
00585                     gPC.printf("\t%d",decompress(decode(12,&space,&temp),9,3));
00586                 }
00587                 for(uint8_t i=0;i<4;i++){
00588                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00589                 }
00590             }
00591             else if(temp_x==4){
00592                 gPC.printf("\t%d",decompress(decode(7,&space,&temp),4,3));
00593                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),10,2));
00594                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),10,2));
00595             }
00596             else if(temp_x==5){
00597                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),9,3));
00598                 gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00599                 gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00600             }
00601         }
00602         else if(srp_mode==1){   //scattered mode
00603             len = calc_length(tmid,srp_mode,temp_x,0);
00604             if(temp_x==0){
00605                 for(uint8_t i=0;i<48;i++){
00606                     if(i==0){gPC.printf("\t");}         gPC.printf("%d",decompress(decode(6,&space,&temp),3,3));    if(i!=47){gPC.printf(",");}
00607                 }
00608                 for(uint8_t i=0;i<4;i++){
00609                     gPC.printf("\t%d",decompress(decode(7,&space,&temp),4,3));
00610                 }
00611             }
00612             else if(temp_x==1){
00613                 gPC.printf("\t%d",2*decompress(decode(7,&space,&temp),4,3));
00614                 for(uint8_t i=0;i<4;i++){
00615                     gPC.printf("\t%d",decompress(decode(12,&space,&temp),9,3));
00616                 }
00617             }
00618             else if(temp_x==4){
00619                 gPC.printf("\t%d",decompress(decode(7,&space,&temp),4,3));
00620                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),10,2));
00621                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),10,2));
00622             }
00623             else if(temp_x==5){
00624                 gPC.printf("\t%d",decompress(decode(12,&space,&temp),9,3));
00625                 gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00626                 gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00627             }
00628         }
00629     }
00630     else{
00631         if(tmid==2){
00632             gPC.printf("\n\r%d",decode(2,&space,&temp));  //splitting first 35 bits of time into year,month.......
00633             gPC.printf("\t%d",decode(4,&space,&temp));
00634             gPC.printf("\t%d",decode(5,&space,&temp));
00635             gPC.printf("\t%d",decode(5,&space,&temp));
00636             gPC.printf("\t%d",decode(6,&space,&temp));
00637             gPC.printf("\t%d",decode(6,&space,&temp));
00638             gPC.printf("\t%d",decode(7,&space,&temp));  //time ends here
00639         }
00640         if(tmid==3)
00641             gPC.printf("\n\r");
00642         gPC.printf("\t%d",decode(4,&space,&temp));
00643         gPC.printf("\t%d",decode(6,&space,&temp));
00644         srp_mode = decode(1,&space,&temp);
00645         gPC.printf("\t%d",srp_mode);
00646         if(srp_mode==0){        
00647             //gPC.printf("\n\rspace before pzf = %d",space);    
00648             pzf = decode(1,&space,&temp);
00649             //gPC.printf("\n\rspace after pzf = %d",space);
00650             ezf = decode(1,&space,&temp);
00651             //gPC.printf("\n\rspace after pzf = %d",space);
00652             gPC.printf("\t%d",pzf);
00653             gPC.printf("\t%d",ezf);
00654             len = calc_length(tmid,srp_mode,0,((uint8_t)((pzf<<1)|ezf)));
00655             for(uint8_t i=0;i<4;i++){
00656                 if(i==0){gPC.printf("\t");}         gPC.printf("%d",decompress(decode(9,&space,&temp),7,2));    if(i!=3){gPC.printf(",");}
00657             }
00658             for(uint8_t i=0;i<4;i++){
00659                 if(i==0){gPC.printf("\t");}         gPC.printf("%d",decompress(decode(9,&space,&temp),7,2));    if(i!=3){gPC.printf(",");}
00660             }            
00661             if(tmid==2){
00662                 for(uint8_t i=0;i<12;i++){
00663                     if(i%2==0){gPC.printf("\t");}   gPC.printf("%d",decompress(decode(6,&space,&temp),3,3));    if(i%2==0){gPC.printf(",");}
00664                 }
00665                 gPC.printf("\t%d",decompress(decode(10,&space,&temp),8,2));
00666                 for(uint8_t i=0;i<3;i++){
00667                     if(i==0){gPC.printf("\t");}         gPC.printf("%d",decompress(decode(10,&space,&temp),8,2));    if(i!=2){gPC.printf(",");}
00668                 }
00669                 for(uint8_t i=0;i<4;i++){
00670                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00671                 }
00672                 gPC.printf("\t%d",decompress(decode(10,&space,&temp),8,2));
00673                 gPC.printf("\t%d",decompress(decode(10,&space,&temp),8,2));
00674                 if(pzf==0){
00675                     for(uint8_t i=0;i<2;i++){
00676                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(10,&space,&temp),8,2));    if(i==0){gPC.printf(",");}
00677                     }
00678                     for(uint8_t i=0;i<15;i++){
00679                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(10,&space,&temp),8,2));    if(i!=14){gPC.printf(",");}
00680                     }
00681                 }
00682                 else{
00683                     gPC.printf("\tNA");
00684                     gPC.printf("\tNA");   
00685                 }
00686                 if(ezf==0){
00687                     gPC.printf("\t%d",decompress(decode(10,&space,&temp),8,2));
00688                     for(uint8_t i=0;i<4;i++){
00689                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(10,&space,&temp),8,2));    if(i!=3){gPC.printf(",");}
00690                     }
00691                 }
00692                 else{
00693                     gPC.printf("\tNA");
00694                     gPC.printf("\tNA");   
00695                 }
00696             }
00697             else if(tmid==3){
00698                 for(uint8_t i=0;i<12;i++){
00699                     if(i%2==0){gPC.printf("\t");}   gPC.printf("%d",decompress(decode(6,&space,&temp),4,2));    if(i%2==0){gPC.printf(",");}
00700                 }
00701                 gPC.printf("\t%d",decompress(decode(8,&space,&temp),6,2));
00702                 for(uint8_t i=0;i<3;i++){
00703                     if(i==0){gPC.printf("\t");}         gPC.printf("%d",decompress(decode(8,&space,&temp),6,2));    if(i!=2){gPC.printf(",");}
00704                 }
00705                 for(uint8_t i=0;i<4;i++){
00706                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),7,2));
00707                 }
00708                 gPC.printf("\t%d",decompress(decode(8,&space,&temp),6,2));
00709                 gPC.printf("\t%d",decompress(decode(8,&space,&temp),6,2));
00710                 if(pzf==0){
00711                     for(uint8_t i=0;i<2;i++){
00712                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(6,&space,&temp),5,1));    if(i==0){gPC.printf(",");}
00713                     }
00714                     for(uint8_t i=0;i<15;i++){
00715                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(5,&space,&temp),4,1));    if(i!=14){gPC.printf(",");}
00716                     }
00717                 }
00718                 else{
00719                     gPC.printf("\tNA");
00720                     gPC.printf("\tNA");   
00721                 }
00722                 if(ezf==0){
00723                     gPC.printf("\t%d",decompress(decode(7,&space,&temp),6,1));
00724                     for(uint8_t i=0;i<4;i++){
00725                         if(i==0){gPC.printf("\t");}     gPC.printf("%d",decompress(decode(7,&space,&temp),6,1));    if(i!=3){gPC.printf(",");}
00726                     }
00727                 }
00728                 else{
00729                     gPC.printf("\tNA");
00730                     gPC.printf("\tNA");   
00731                 }   
00732             }
00733         }        
00734         else if(srp_mode==1){
00735             len = calc_length(tmid,srp_mode,0,((uint8_t)((pzf<<1)|ezf)));
00736             if(tmid==2){
00737                 for(uint8_t i=0;i<48;i++){
00738                     if(i==0){gPC.printf("\t");}   gPC.printf("%d",decompress(decode(10,&space,&temp),8,2));    if(i!=47){gPC.printf(",");}
00739                 }
00740                 for(uint8_t i=0;i<4;i++){
00741                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),6,3));
00742                 }
00743             }
00744             else if(tmid==3){
00745                 for(uint8_t i=0;i<48;i++){
00746                     if(i==0){gPC.printf("\t");}   gPC.printf("%d",decompress(decode(7,&space,&temp),5,2));    if(i!=47){gPC.printf(",");}
00747                 }
00748                 for(uint8_t i=0;i<4;i++){
00749                     gPC.printf("\t%d",decompress(decode(9,&space,&temp),7,2));
00750                 }
00751             }
00752         }
00753     }
00754     return len;
00755 }
00756 #endif
00757 void print_headings(uint8_t *pack_curr_det){
00758     switch(((*pack_curr_det) & 0x3F)){
00759         case 0x10:  gPC.printf("\n\n\r----------------------------SCP_BT calibrated mode-----------------------------"); 
00760                     break;
00761         case 0x12:  gPC.printf("\n\n\r----------------------------SCP_AT calibrated mode-----------------------------"); 
00762                     break;                    
00763         case 0x14:  gPC.printf("\n\n\r-----------------SCP_BT calibrated with data conservation mode-----------------");
00764                     break;
00765         case 0x16:  gPC.printf("\n\n\r-----------------SCP_AT calibrated with data conservation mode-----------------");
00766                     break;
00767         case 0x18:  gPC.printf("\n\n\r----------------SCP_BT calibrated with extreme conservation mode---------------");
00768                     break;
00769         case 0x1A:  gPC.printf("\n\n\r----------------SCP_AT calibrated with extreme conservation mode---------------");
00770                     break;
00771         case 0x11:  gPC.printf("\n\n\r---------------------------SCP_BT scattered plot mode--------------------------");
00772                     break;
00773         case 0x13:  gPC.printf("\n\n\r---------------------------SCP_AT scattered plot mode--------------------------");
00774                     break;
00775         case 0x19:  gPC.printf("\n\n\r--------------SCP_BT scattered plot with extreme conservation mode-------------");
00776                     break;
00777         case 0x1B:  gPC.printf("\n\n\r--------------SCP_AT scattered plot with extreme conservation mode-------------");
00778                     break;
00779         case 0x20:  gPC.printf("\n\n\r---------------------------SFP_BT calibrated mode--------------------------");
00780                     break;
00781         case 0x22:  gPC.printf("\n\n\r---------------------------SFP_AT calibrated mode--------------------------");
00782                     break;
00783         case 0x30:  gPC.printf("\n\n\r---------------------------SFP_BT calibrated mode--------------------------");
00784                     break;
00785         case 0x32:  gPC.printf("\n\n\r---------------------------SFP_AT calibrated mode--------------------------");
00786                     break;
00787         case 0x21:  gPC.printf("\n\n\r---------------------------SFP_BT scattered plot mode--------------------------");
00788                     break;
00789         case 0x23:  gPC.printf("\n\n\r---------------------------SFP_AT scattered plot mode--------------------------");
00790                     break;        
00791         case 0x31:  gPC.printf("\n\n\r---------------------------SFP_BT scattered plot mode--------------------------");
00792                     break;
00793         case 0x33:  gPC.printf("\n\n\r---------------------------SFP_AT scattered plot mode--------------------------");
00794                     break;
00795         default:    gPC.printf("\n\n\rxxxxxxxxxxxxxxxxxxxx INVALID packet type xxxxxxxxxxxxxxxxxxxxxx");
00796                     break;
00797     }
00798 }
00799 #if tabulation
00800 void clear_buffer(uint8_t index){   //index = tmid-1;
00801     for(uint8_t i=0;i<70;i++)
00802     pack_buf[index][i] = 0;
00803     fsc_incomplete[index] = 0;
00804     len_prev[index] = 0;
00805     len_correct[index] = 0;
00806     pack_split_det &= (~(0x01<<(index)));      //making the corresponding tmid bit 0 to indicate buffer is empty
00807 }
00808 void tabulate_TM(uint8_t *ptr){
00809     /* This function prints the TM packets info in the form of a table so that the data can be used to verify decoding 
00810        at the Ground Station(GS)."ptr" is the pointer to the TM frame extracted from SD card before transmitting to GS
00811     */
00812     pack_curr_det = 0;
00813     uint32_t fsc_TM;        //to store the FSC of the current TM
00814     uint8_t tmid = (ptr[0]>>3)&0x0f,temp_index;
00815     pack_curr_det |= (ptr[0]<<1)& 0x30; //3rd and 4th bits(MSB) = tmid
00816     if(tmid==1||tmid==2){       //the TMID corresponds to SCP or SFP_AT(both have FSC at the same location in the frame)
00817         fsc_TM &= 0x00000000;
00818         fsc_TM |= (((uint32_t)(ptr[1]<<16))&0x00ff0000);
00819         fsc_TM |= (((uint32_t)(ptr[2]<<8)) &0x0000ff00);
00820         fsc_TM |= (((uint32_t)(ptr[3]))    &0x000000ff);
00821         gPC.printf("\n\n\rFrame sequence count(tmid=%d): %d---------------------------------------------------------",tmid,fsc_TM);
00822         if(tmid==1){
00823             gPC.printf("\n\rYear:%d\tMon:%d\tDate:%d",((ptr[5]>>4)&0x03),(ptr[5]&0x0f),((ptr[6]>>3)&0x1f));
00824             gPC.printf("\tHour:%d\tMin:%d\n\r",((ptr[6]&0x07)<<2)|((ptr[7]&0xC0)>>6),ptr[7]&0x3f);
00825         }
00826         if(ptr[4]!=(11-(3*tmid))){          //there is an incomplete packet present at the start of the current frame
00827             if(!((pack_split_det>>(tmid-1))&0x01)){
00828                 gPC.printf("\n\rError: TMID = %d frame contains split packet but buffer empty----------------------\n",tmid);
00829                 pack_split_det |= 0x08;     //4 th bit (from LSB) if equal to 1 indicates error in packet splitting
00830             }
00831             if((fsc_TM-1)!=(fsc_incomplete[tmid-1])){                                              //fsc_incomplete[]
00832                 gPC.printf("\n\rError: FSC for TMID = %d is not continous---------------------------\n",tmid);
00833                 clear_buffer(tmid-1);
00834                 pack_split_det |= 0x08;     //4 th bit (from LSB) indicates error in packet splitting
00835             }
00836             if(((pack_split_det>>3)&0x01)==0){      //no error upto now in split packet
00837                 len_current = ptr[4] - (11-(3*tmid));                                       //len_current
00838                 if((len_prev[tmid-1]+len_current)!=len_correct[tmid-1]){                           //len_correct[] and len_prev[]
00839                     gPC.printf("\n\rError: Length of the split packet for TMID = %d not correct,len_correct=%d,len_prev=%d,len_current=%d",tmid,len_correct[tmid-1],len_prev[tmid-1],len_current);
00840                     clear_buffer(tmid-1);
00841                     pack_split_det &= (~0x08);     //clear the packet splitting error bit
00842                 }
00843                 else{
00844                     temp_index = 11-(3*tmid);
00845                     for(uint8_t i=0;i<len_current;i++)
00846                         pack_buf[tmid-1][len_prev[tmid-1]+i] = ptr[temp_index+i];   //storing the remaining part of the packet in appropriate buffer
00847                     if(tmid==1){
00848                         pack_curr_det |= (pack_buf[0][2]>>2)&0x0f;//5th and 6th bits (start from MSB) bits = compression option, 7th bit = threshold bit and last bit = science data mode
00849                     }
00850                     else if(tmid==2){
00851                         pack_curr_det |= (pack_buf[1][5]>>2)&0x01;//5th and 6th (start from MSB) bits = compression option, 7th bit = threshold bit and last bit = science data mode                    
00852                         pack_curr_det |= 0x02;      //to indicate sfp is above threshold
00853                     }
00854                     //gPC.printf("\n\rpack_curr_det=0x%02X and pack_prev_det=0x%02X",pack_curr_det,pack_prev_det);
00855                     if(pack_curr_det!=pack_prev_det){
00856                         print_headings(&pack_curr_det);
00857                         pack_prev_det = pack_curr_det;                        
00858                         pack_curr_det &= (~0x0f);   //clearing last four bits to store next packet's info(but tmid remains same)
00859                     }                    
00860                     if(len_correct[tmid-1]!=print_packet(tmid,&(pack_buf[tmid-1][0]))){
00861                         gPC.printf("\n\rError: print_packet() length not equal to len_correct (tmid=%d)-------------------------\n",tmid);
00862                     }
00863                     clear_buffer(tmid-1);
00864                 }
00865             }            
00866         }
00867         temp_index = ptr[4];    //index of the first complete packet in the frame
00868         //gPC.printf("\n\rfirst header pointer = %d and temp_index = %d",ptr[4],temp_index);
00869         while(temp_index!=0 && temp_index <132){   //temp_index = 0 implies no more complete packets left to display
00870             if(tmid==1){
00871                 pack_curr_det |= (ptr[temp_index+2]>>2)&0x0f;//5th and 6th (start from MSB) 2 bits = compression option, next bit = threshold bit and last bit = science data mode
00872             }
00873             else if(tmid==2){
00874                 pack_curr_det |= (ptr[temp_index+5]>>2)&0x01;//5th and 6th (start from MSB) bits = compression option, next bit = threshold bit and last bit = science data mode
00875                 pack_curr_det |= 0x02;      //to indicate sfp is above threshold
00876                 //gPC.printf("\n\rScience data mode:%d",pack_curr_det&0x1);
00877             }
00878             //gPC.printf("\n\rpack_curr_det=0x%02X and pack_prev_det=0x%02X",pack_curr_det,pack_prev_det);
00879             if(pack_curr_det!=pack_prev_det){
00880                 print_headings(&pack_curr_det);
00881                 pack_prev_det = pack_curr_det;                    
00882                 pack_curr_det &= (~0x0f);   //clearing last four bits to store next packet's info(but tmid remains same)
00883             }                
00884             temp_index = temp_index + print_packet(tmid,&ptr[temp_index]);
00885             //gPC.printf("\n\rtemp_index = %d after one packet",temp_index);
00886             //temp_index = 69;
00887             if((132-temp_index)<(3*tmid)){
00888                 temp_index = 0;         //no more packets available, minimum number of bytes are not remaining(3 for SCP and 6 for SFP_AT)
00889             }
00890             else if((132-temp_index)<(41+(15*tmid))){   //space left in the frame is less than the maximum packet size for the corresponding tmid
00891                 if(tmid==1){
00892                     pack_split_det |= ((ptr[temp_index+2]<<2)&0xf0);
00893                     //first(MSB) 3 bits = header(compression option),next bit=science data mode,next bit=errorbit,last 3 bits = to store if incomplete packet is present in the buffer
00894                     if((pack_split_det&0xd0)==0x80)    //extreme data & calibrated mode
00895                         len_correct[tmid-1] = calc_length(tmid,(pack_split_det>>4)&0x1,(pack_split_det>>5)&0x3,ptr[temp_index+2]&0x03);
00896                     else
00897                         len_correct[tmid-1] = calc_length(tmid,(pack_split_det>>4)&0x1,(pack_split_det>>5)&0x3,ptr[temp_index+5]&0x03); 
00898                         //scattered plot mode doesn't have pzf and ezf bits but even if we send junk values but they will not be used in the function
00899                 }
00900                 else if(tmid==2){
00901                     pack_split_det |= ((ptr[temp_index+5]<<4)&0x70);
00902                     len_correct[tmid-1] = calc_length(tmid,(pack_split_det>>6)&0x1,0,(pack_split_det>>4)&0x03);
00903                     //in this case(tmid = 2), 2nd bit(MSB) is srp_mode, next bit is pzf, next is ezf and next four bits are described before                        
00904                 }
00905                 pack_split_det &= (~0xf0);      //first four bits(MSB) of pack_split_det not used anymore
00906                 if((132-temp_index)<len_correct[tmid-1]){   //incomplete packet present
00907                     len_prev[tmid-1] = 132 - temp_index;
00908                     fsc_incomplete[tmid-1] = fsc_TM;                        
00909                     for(uint8_t i=0;i<len_prev[tmid-1];i++)
00910                         pack_buf[tmid-1][i] = ptr[temp_index+i];
00911                     pack_split_det |= (1<<(tmid-1));//1st bit(LSB) indicates scp incomplete packet,2nd bit indicates sfp_at incomplete
00912                     temp_index = 0;
00913                 }
00914             }
00915         }
00916     }
00917     else if(tmid==3){   //SFP_BT
00918         fsc_TM &= 0x00000000;
00919         fsc_TM |= (((uint32_t)(ptr[1]<<24))&0xff000000);
00920         fsc_TM |= (((uint32_t)(ptr[2]<<16))&0x00ff0000);
00921         fsc_TM |= (((uint32_t)(ptr[3]<<8)) &0x0000ff00);
00922         fsc_TM |= (((uint32_t)(ptr[4]))    &0x000000ff);
00923         gPC.printf("\n\n\rFrame sequence count(tmid=%d): %d---------------------------------------------------------",tmid,fsc_TM);
00924         gPC.printf("\n\rYear:%d\tMon:%d\tDate:%d",((ptr[6]>>1)&0x03),(((ptr[7]>>5)&0x07)|((ptr[6]&0x01)<<3)),(ptr[7]&0x1f));
00925         gPC.printf("\tHour:%d\tMin:%d\tSec:%d\tCenti:%d\n\r",(ptr[8]&0xf1)>>3,(((ptr[9]&0xE0)>>5)|((ptr[8]&0x07)<<3)),((ptr[10]&0x80)|((ptr[9]&0x1f)<<1)),ptr[10]&0x7f);
00926         if(ptr[5]!=11){                         //there is an incomplete packet present at the start of the current frame
00927             if(!((pack_split_det>>(tmid-1))&0x01)){
00928                 gPC.printf("\n\rError: TMID = %d frame contains split packet but buffer empty",tmid);
00929                 pack_split_det |= 0x08;     //4 th bit (from LSB) indicates error in packet splitting
00930             }
00931             else if((fsc_TM-1)!=(fsc_incomplete[tmid-1])){  //fsc 
00932                 gPC.printf("\n\rError: FSC for TMID = %d is not continous---------------------------\n",tmid);
00933                 clear_buffer(tmid-1);
00934                 pack_split_det |= 0x08;     //4 th bit (from LSB) indicates error in packet splitting
00935             }
00936             if(((pack_split_det>>3)&0x01)==0){      //no error upto now in split packet
00937                 len_current = ptr[5] - 11;
00938                 if((len_prev[tmid-1]+len_current)!=len_correct[tmid-1]){
00939                     gPC.printf("\n\rError: Length of the split packet for TMID = %d not correct,len_correct=%d,len_prev=%d,len_current=%d",tmid,len_correct[tmid-1],len_prev[tmid-1],len_current);
00940                     clear_buffer(tmid-1);
00941                     pack_split_det &= (~0x08);     //clear the packet splitting error bit
00942                 }
00943                 else{
00944                     temp_index = 11;
00945                     for(uint8_t i=0;i<len_current;i++)
00946                         pack_buf[tmid-1][len_prev[tmid-1]+i] = ptr[temp_index+i];   //storing the remaining part of the packet in appropriate buffer
00947                     pack_curr_det |= (pack_buf[2][1]>>5)&0x01;//5th and 6th (start from MSB) bits = compression option, 7th bit = threshold bit and last bit = science data mode
00948                     if(pack_curr_det!=pack_prev_det){
00949                         print_headings(&pack_curr_det);
00950                         pack_prev_det = pack_curr_det;
00951                         pack_curr_det &= (~0x0f);//clearing last four bits to store next packet's info(but tmid remains same)
00952                     }
00953                     if(len_correct[tmid-1]!=print_packet(tmid,&(pack_buf[tmid-1][0]))){
00954                         gPC.printf("\n\rError: print_packet() length not equal to len_correct (tmid=%d)-------------------------",tmid);
00955                     }
00956                     clear_buffer(tmid-1);
00957                 }
00958             }            
00959         }
00960         temp_index = ptr[5];    //index of the first complete packet in the frame
00961         while(temp_index!=0){   //temp_index = 0 implies no more complete packets left to display
00962             pack_curr_det |= (ptr[temp_index+1]>>5)&0x01;   //calibrated or scattered mode
00963             if(pack_curr_det!=pack_prev_det){
00964                 print_headings(&pack_curr_det);
00965                 pack_prev_det = pack_curr_det;
00966                 pack_curr_det &= (~0x0f);   //clearing last four bits to store next packet's info(but tmid remains same)
00967             }
00968             temp_index = temp_index + print_packet(tmid,&ptr[temp_index]);
00969             if((132-temp_index)<2){
00970                 temp_index = 0;         //no more packets available, minimum number of bytes are not remaining(2 for SFP_BT)
00971             }
00972             else if((132-temp_index)<(46+(2*(pack_curr_det&0x01)))){   //space left in the frame is less than the maximum packet size(46 bytes for calib and 48 for scattered mode)
00973                 pack_split_det |= ((ptr[temp_index+1]<<1)&0x70);
00974                 len_correct[tmid-1] = calc_length(tmid,(pack_split_det>>6)&0x1,0,(pack_split_det>>4)&0x03);
00975                 //gPC.printf("\n\rSFP_BT srp mode:%d",(pack_split_det>>6)&0x1);
00976                 //gPC.printf("\n\rSFP_BT: len_correct = %d",len_correct[tmid-1]);
00977                 //in this case(tmid = 3), 2nd bit(MSB) is srp_mode, next bit is pzf, next is ezf and next four bits are described before
00978                 pack_split_det &= (~0xf0);      //first four bits(MSB) of pack_split_det not used anymore
00979                 if((132-temp_index)<len_correct[tmid-1]){   //incomplete packet present at the end of the frame
00980                     len_prev[tmid-1] = 132 - temp_index;
00981                     fsc_incomplete[tmid-1] = fsc_TM;                        
00982                     for(uint8_t i=0;i<len_prev[tmid-1];i++)
00983                         pack_buf[tmid-1][i] = ptr[temp_index+i];
00984                     pack_split_det |= (1<<(tmid-1));//1st bit(LSB) indicates scp incomplete packet,2nd bit indicates sfp_at incomplete
00985                     //len_correct[tmid-1] = calc_length(tmid,(pack_split_det>>6)&0x1,0,(pack_split_det>>4)&0x03);
00986                     temp_index = 0;
00987                 }
00988             }
00989         }
00990     }
00991 }
00992 #endif
00993 #if test_science
00994 void generated_dma(uint8_t *head){
00995     uint8_t temp_buf[16] = {0};
00996     gPC.printf("\n\rDisplaying generated data");
00997     gPC.printf("\n\rhead[0,1] = %d",read_2byte(&head[0]));
00998     for(uint8_t i=0,j=3;i<48;i++,j+=2)
00999         gPC.printf("\n\rhead[%d] = %d",i,read_2byte(&head[j]));
01000     for(uint8_t i=0,j=99;i<4;i++,j+=4)
01001         gPC.printf("\n\rhead[%d] = %d",i+48,read_4byte(&head[j]));
01002     for(uint16_t j=115;j<6723;j++)
01003         if(head[j]!=head[j-112]){
01004             gPC.printf("\n\rDMA data repeat error at index = %d",j);
01005             return;
01006         }
01007     gPC.printf("\n\rNo errors in generated repeated DMA data");
01008 }
01009 void generate_dma_calibrated(uint8_t *head)
01010 {
01011     head[0] = (data_srp_calibrated[0]>>8) & 0xff;
01012     head[1] = (data_srp_calibrated[0]) & 0xff;
01013     head[2] = (data_srp_calibrated[1]) & 0xff;
01014     for(int j=3,count=0;count<18;count++,j+=2){
01015         head[j]   = (data_srp_calibrated[2]>>8) & 0xff;
01016         head[j+1] = (data_srp_calibrated[2]) & 0xff;
01017     }
01018     for(int j=39,count=0;count<6;count++,j+=2){
01019         head[j]   = (data_srp_calibrated[3]>>8) & 0xff;
01020         head[j+1] = (data_srp_calibrated[3]) & 0xff;
01021     }
01022     for(int j=51,count=0;count<4;count++,j+=2){
01023         head[j]   = (data_srp_calibrated[4]>>8) & 0xff;
01024         head[j+1] = (data_srp_calibrated[4]) & 0xff;
01025     }
01026     for(int j=59,count=0;count<4;count++,j+=2){
01027         head[j]   = (data_srp_calibrated[5]>>8) & 0xff;
01028         head[j+1] = (data_srp_calibrated[5]) & 0xff;
01029     }
01030     int j=67;
01031     for(uint8_t count2 = 0;count2<6;count2++)
01032         for(uint8_t count=0;count<2;count++,j+=2){
01033             head[j]   = (data_srp_calibrated[6+count2]>>8) & 0xff;
01034             head[j+1] = (data_srp_calibrated[6+count2]) & 0xff;
01035             //gPC.printf("\n\rj = %d",j);
01036         }
01037     head[91] = (data_srp_calibrated[12]>>8) & 0xff;
01038     head[92] = (data_srp_calibrated[12]) & 0xff;
01039     for(j=93,count=0;count<3;count++,j+=2){
01040         head[j]   = (data_srp_calibrated[13]>>8) & 0xff;
01041         head[j+1] = (data_srp_calibrated[13]) & 0xff;
01042     }
01043     j=99;
01044     for(uint8_t count2 =0;count2<4;count2++)
01045         for(uint8_t count=0;count<1;count++,j+=4){
01046             head[j]   = (data_srp_calibrated[14+count2]>>24) & 0xff;
01047             head[j+1] = (data_srp_calibrated[14+count2]>>16) & 0xff;
01048             head[j+2] = (data_srp_calibrated[14+count2]>>8) & 0xff;
01049             head[j+3] = (data_srp_calibrated[14+count2]) & 0xff;
01050         }
01051     for(j=115;j<6723;j++)   //copying the same data for all other 59 packets
01052             head[j] = head[j-112];
01053     /*for(j=115;j<3363;j++)   //copying the same data for all next 29 packets
01054         head[j] = head[j-112];
01055     //next 30 packets will have following data
01056     j=3363;
01057     for(int count=0;count<18;count++,j+=2){
01058         head[j]   = (data_srp_calibrated2[2]>>8) & 0xff;
01059         head[j+1] = (data_srp_calibrated2[2]) & 0xff;
01060     }
01061     for(int j=3399,count=0;count<6;count++,j+=2){
01062         head[j]   = (data_srp_calibrated2[3]>>8) & 0xff;
01063         head[j+1] = (data_srp_calibrated2[3]) & 0xff;
01064     }
01065     for(int j=3411,count=0;count<4;count++,j+=2){
01066         head[j]   = (data_srp_calibrated2[4]>>8) & 0xff;
01067         head[j+1] = (data_srp_calibrated2[4]) & 0xff;
01068     }
01069     for(int j=3419,count=0;count<4;count++,j+=2){
01070         head[j]   = (data_srp_calibrated2[5]>>8) & 0xff;
01071         head[j+1] = (data_srp_calibrated2[5]) & 0xff;
01072     }
01073     j=3427;
01074     for(uint8_t count2 = 0;count2<6;count2++)
01075         for(uint8_t count=0;count<2;count++,j+=2){
01076             head[j]   = (data_srp_calibrated2[6+count2]>>8) & 0xff;
01077             head[j+1] = (data_srp_calibrated2[6+count2]) & 0xff;
01078             //gPC.printf("\n\rj = %d",j);
01079         }
01080     head[3451] = (data_srp_calibrated2[12]>>8) & 0xff;
01081     head[3452] = (data_srp_calibrated2[12]) & 0xff;
01082     for(j=3453,count=0;count<3;count++,j+=2){
01083         head[j]   = (data_srp_calibrated2[13]>>8) & 0xff;
01084         head[j+1] = (data_srp_calibrated2[13]) & 0xff;
01085     }
01086     j=3459;
01087     for(uint8_t count2 =0;count2<4;count2++)
01088         for(uint8_t count=0;count<1;count++,j+=4){
01089             head[j]   = (data_srp_calibrated2[14+count2]>>24) & 0xff;
01090             head[j+1] = (data_srp_calibrated2[14+count2]>>16) & 0xff;
01091             head[j+2] = (data_srp_calibrated2[14+count2]>>8) & 0xff;
01092             head[j+3] = (data_srp_calibrated2[14+count2]) & 0xff;
01093         }
01094     for(j=3475;j<6723;j++)   //copying the same data for last 29 packets
01095             head[j] = head[j-112];*/
01096 }
01097 void generate_dma_scattered(uint8_t *head)
01098 {
01099     int j=3;
01100     uint8_t count;
01101     head[0] = (data_srp_scattered[0]>>8) & 0xff;
01102     head[1] = (data_srp_scattered[0]) & 0xff;
01103     head[2] = (data_srp_scattered[1]) & 0xff;
01104     for (count=0,j=3;count<48;count++,j+=2){
01105         head[j]   = (data_srp_scattered[2]>>8) & 0xff;
01106         head[j+1] = (data_srp_scattered[2]) & 0xff;
01107     }
01108     j=99;
01109     for(uint8_t count2 =0;count2<4;count2++)
01110         for(uint8_t count=0;count<1;count++,j+=4){
01111             head[j]   = (data_srp_scattered[3+count2]>>24) & 0xff;
01112             head[j+1] = (data_srp_scattered[3+count2]>>16) & 0xff;
01113             head[j+2] = (data_srp_scattered[3+count2]>>8) & 0xff;
01114             head[j+3] = (data_srp_scattered[3+count2]) & 0xff;
01115         }
01116     for(int j=115;j<6723;j++)
01117         head[j] = head[j-112];
01118 }
01119 uint8_t map_index_bins_2_srp_index(uint8_t i,uint8_t srp_mode)
01120 {
01121     /* This function is used to map sfp_indices to data_srp_calibrated[] or data_srp_scattered[] array indices
01122         Example: indices 0 to 17 (the first 18 bins of sfp_bin[] array) correspond to index 2 of data_srp_calibrated[] array which is nothing 
01123         but proton energy. This function is used to update calib_thres_index[], scat_thresh_index[], calib_thres_index_scp[]
01124         and scat_thres_index_scp[] which stores info about which bins is causing above threshold condition(refer to comments
01125         in dma_packet_info() funtion regarding calib_thres_index[] array)
01126     */
01127     if(srp_mode==0){
01128         if(i<18)    return 2;
01129         else if(i<24)   return 3;
01130         else if(i<28)   return 4;
01131         else if(i<32)   return 5;
01132         
01133         else if(i<34)   return 6;
01134         else if(i<36)   return 7;
01135         else if(i<38)   return 8;
01136         else if(i<40)   return 9;
01137         else if(i<42)   return 10;
01138         else if(i<44)   return 11;
01139 
01140         //else if(i<44)   return 6+((i-32)/2);
01141         else if(i<45)   return 12;
01142         else if(i<48)   return 13;
01143 
01144         else if(i<49)   return 14;
01145         else if(i<50)   return 15;
01146         else if(i<51)   return 16;
01147         else if(i<52)   return 17;
01148         //else if(i<52)   return i-34;
01149     }
01150     else if(srp_mode==1){
01151         if(i<48)        return 2;
01152         else if(i<52)   return i-45;
01153     }
01154 }
01155 void verify_read_dma(uint8_t srp_mode,uint8_t counter){
01156     /* This function verifies that correct data was generated in dma by comparing sfp_bin[i] with the 
01157        data in srp_calibrated[] or srp_scattered[]. This function assumes that all the 60 srp packets in DMA have 
01158        identical data. If not change the dma_generating functions and this function appropriately
01159     */
01160     uint8_t i;
01161     if(srp_mode==0){
01162         for(i=0;i<18;i++)   //first 18 bins correspond to proton energy which is nothing but data_srp_calibrated[2]
01163             if(sfp_bin[i]!=data_srp_calibrated[2]){
01164                 gPC.printf("\n\rsfp_bin[%d]:%d\tdatasrp_calibrated[2]:%d",i,sfp_bin[i],data_srp_calibrated[2]);
01165                 gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[2],counter);    //print the name of the bin whose DMA value didn't match with value in the data_srp_calibrated[] array
01166                 return;
01167             }
01168         for(;i<24;i++)      //next 6 bins correspond to electron energy
01169             if(sfp_bin[i]!=data_srp_calibrated[3]){
01170                 gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[3],counter);
01171                 return;
01172             }
01173         for(;i<28;i++)      //refer to excel sheet with test cases
01174             if(sfp_bin[i]!=data_srp_calibrated[4]){
01175                 gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[4],counter);
01176                 return;   
01177             }
01178         for(;i<32;i++)
01179             if(sfp_bin[i]!=data_srp_calibrated[5]){
01180                 gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[5],counter);
01181                 return;   
01182             }
01183         for(i=0;i<12;i++)
01184             if(sfp_bin[32+i]!=data_srp_calibrated[6+(i/2)]){
01185                 gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[6+(i/2)],counter);
01186                 return;   
01187             }
01188         if(sfp_bin[44]!=data_srp_calibrated[12]){
01189             gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[12],counter);
01190             return;   
01191         }
01192         for(i=45;i<48;i++)
01193             if(sfp_bin[i]!=data_srp_calibrated[13]){
01194                 gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[13],counter);
01195                 return;   
01196             }
01197         if(sfp_bin[48]!=data_srp_calibrated[14]){
01198             gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[14],counter);
01199             return;   
01200         }
01201         if(sfp_bin[49]!=data_srp_calibrated[15]){
01202             gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[15],counter);
01203             return;
01204         }
01205         if(sfp_bin[50]!=data_srp_calibrated[16]){
01206             gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[16],counter);
01207             return;   
01208         }    
01209         if(sfp_bin[51]!=data_srp_calibrated[17]){
01210             gPC.printf("\n\r\"%s\" data mismatch in DMA, counter = %d",srp_calibrated[17],counter);
01211             return;   
01212         }
01213     }
01214     else if(srp_mode==1){
01215         for(i=0;i<48;i++)   //first 48 bins correspond to dEdX & bulk & not Veto.....  which is nothing but data_srp_scattered[2]
01216             if(sfp_bin[i]!=data_srp_scattered[2]){
01217                 gPC.printf("\n\r\"%s\" data mismatch in DMA",srp_scattered[2]); //  //print the name of the bin whose DMA value didn't match with value in the srp_scattered[] array
01218                 return;
01219             }
01220         if(sfp_bin[48]!=data_srp_scattered[3]){
01221             gPC.printf("\n\r\"%s\" data mismatch in DMA",srp_scattered[3]);
01222             return;   
01223         }
01224         if(sfp_bin[49]!=data_srp_scattered[4]){
01225             gPC.printf("\n\r\"%s\" data mismatch in DMA",srp_scattered[4]);
01226             return;   
01227         }
01228         if(sfp_bin[50]!=data_srp_scattered[5]){
01229             gPC.printf("\n\r\"%s\" data mismatch in DMA",srp_scattered[5]);
01230             return;   
01231         }    
01232         if(sfp_bin[51]!=data_srp_scattered[6]){
01233             gPC.printf("\n\r\"%s\" data mismatch in DMA",srp_scattered[6]);
01234             return;   
01235         }
01236     }
01237     gPC.printf("\n\rNo errors in generated data");
01238 }
01239 void dma_packets_info()
01240 {
01241     gPC.printf("\n\n\r-------------------------------------------------------------------------------------------");
01242     gPC.printf("\n\rNumber of SFP_AT: %d",dma_sfp_at_count);    // dma_sfp_at_count = number of sfp which are above threshold when dma is read every 6 seconds
01243     gPC.printf("\n\rNumber of SCP_AT: %d",dma_scp_at_count);    // dma_scp_at_count = number of scp which are above threshold when dma is read every 6 seconds
01244     if(sfp_mode_count[0]!=0){   //number of sfp packets in calibrated mode when dma is read every 6 seconds
01245         gPC.printf("\n\rNumber of SFP in calibrated mode: %d",sfp_mode_count[0]);
01246         if(dma_sfp_at_count!=0) //if there are above threshold packets then display information
01247             gPC.printf("\n\rSFP Above Threshold indices info:");
01248         for(uint8_t i=0;i<16;i++){  //for the 16 types of bins in calibrated mode 
01249             
01250             /* calib_thres_index is a 2d array with 16 rows(for 16 differnt types of bins possible in calibrated mode which are named
01251                 in srp_calibrated character array) and each "i"th row contains the packet numbers(1 to 60) which is above threshold due to the "i"th row
01252                 example:if proton bins have energy above threshold for all the 60 srp packets in dma, then calib_thres_index[0] will have numbers 
01253                 from {1,2,3...so on upto 60} which is why the column size of calib_thres_index is [16][60].the variable calib_thres_subindex[j] is 
01254                 used to update packet numbers in each row of calib_thres_index[i][j] (0<=j<60)
01255             */
01256 
01257             if(calib_thres_index[i][0]!=0){ //display only those bins which have atleast one packet in which they are above threshold
01258                 gPC.printf("\n\r%s :{ ",srp_calibrated[i+2]);
01259                 /*srp_calibrated has index (i+2) because the first two indices have "NA" and index of "proton bins"=2 whereas,
01260                   in calib_thres_index[] the index of "proton_bins" is 0 and similarly for all other 15 bins of srp_calibrated
01261                 */
01262             }
01263             //for(uint8_t j=0;j<60;j++){
01264             for(uint8_t j=0;(calib_thres_index[i][j]!=0)&&(j<60);j++){    //display only non zero elements
01265                 gPC.printf("%d ",calib_thres_index[i][j]);
01266             }
01267             if(calib_thres_index[i][0]!=0)
01268                 gPC.printf("}");
01269         }
01270     }
01271     if(sfp_mode_count[1]!=0){   //number of sfp/srp packets in scattered plot mode when dma is read every 6 seconds
01272         gPC.printf("\n\rNumber of SFP in scattered plot mode: %d",sfp_mode_count[1]);
01273         if(dma_sfp_at_count!=0) //same logic as stated above 
01274             gPC.printf("\n\rSFP Above Threshold indices info:");
01275         for(uint8_t i=0;i<5;i++){   //scat_thres_index[5][60] has only 5 rows because only 5 different bins are possible, refer to excel sheet having test cases
01276             if(scat_thres_index[i][0]!=0){  //display only those bins which contribute to above threshold
01277                 gPC.printf("\n\r%s :{ ",srp_scattered[i+2]);
01278             }
01279             //for(uint8_t j=0;scat_thres_index[i][j]!=0;j++){
01280             for(uint8_t j=0;(scat_thres_index[i][j]!=0)&&(j<60);j++){
01281                 gPC.printf("%d ",scat_thres_index[i][j]);
01282             }
01283             if(scat_thres_index[i][0]!=0)
01284                 gPC.printf("}");
01285         }
01286     }
01287     if(scp_mode_count[0]!=0){   //scp_mode_count[0] stores number of scp in calibrated mode
01288         gPC.printf("\n\rNumber of SCP in calibrated mode: %d",scp_mode_count[0]);
01289         if(dma_scp_at_count!=0)
01290             gPC.printf("\n\rSCP Above Threshold indices info:");
01291         //for(uint8_t temp_i=0;temp_i<16;temp_i++)
01292             //gPC.printf("\n\rcalib_thres_index_scp[%d]:%d",temp_i,calib_thres_index_scp[temp_i]);
01293         for(uint8_t i=0;i<16;i++){  //same logic as srp/sfp above threshold stated above
01294             if(calib_thres_index_scp[i]!=0){    
01295                 /*the array calib_thres_index_scp[16] is one dimensional because every 6 seconds only one scp packet is generated, so if a bin caused it to 
01296                   be above threshold it is possible only in one scp packet whereas in sfp, the bin could have caused any of the 60 sfp's to be 
01297                   above threshold(so we store the packet number also), we make the value of calib_thres_index_scp[i] = 1 if that bin caused above threshold in scp
01298                 */
01299                 gPC.printf("\n\r%s is above threshold",srp_calibrated[i+2]);
01300             }
01301         }
01302     }
01303     if(scp_mode_count[1]!=0){   //scp_mode_count[1] stores number of scp in calibrated mode with data conservation
01304         gPC.printf("\n\rNumber of SCP in calibrated mode with data conservation: %d",scp_mode_count[1]);
01305         if(dma_scp_at_count!=0)
01306             gPC.printf("\n\rSCP Above Threshold indices info:");
01307         for(uint8_t i=0;i<16;i++){  //same logic as above
01308             if(calib_thres_index_scp[i]!=0){
01309                 gPC.printf("\n\r%s is above threshold",srp_calibrated[i+2]);
01310             }
01311         }
01312         if(proton_scp_sum_at==1){
01313             /* here two other things apart from the bins stated in test cases can cause above threshold,
01314                namely proton_scp_sum and electron_scp_sum which are calculated as sum of proton enery bins, refer to excel sheet
01315                hence we check these two cases saparately at the end
01316             */
01317             gPC.printf("\n\rSum of proton bins is above threshold");
01318         }
01319         if(electron_scp_sum_at==1)
01320             gPC.printf("\n\rSum of electron bins is above threshold");
01321     }
01322     if(scp_mode_count[2]!=0){   //scp_mode_count[2] stores number of scp packets in calibrated with extreme data conservation mode
01323         gPC.printf("\n\rNumber of SCP in calibrated mode with Extreme data conservation: %d",scp_mode_count[2]);
01324         if(dma_scp_at_count!=0)
01325             gPC.printf("\n\rSCP Above Threshold indices info:");
01326         //for(uint8_t temp_i=0;temp_i<16;temp_i++)
01327             //gPC.printf("\n\rcalib_thres_index_scp[%d]:%d",temp_i,calib_thres_index_scp[temp_i]);
01328         if(calib_thres_index_scp[14]!=0)    //only bulk fast counts bin is present other than sum of energies bins
01329             gPC.printf("\n\rBulk fast counts is above threshold");
01330         if(proton_scp_sum_at==1)
01331             gPC.printf("\n\rSum of proton bins is above threshold");
01332         if(electron_scp_sum_at==1)
01333             gPC.printf("\n\rSum of electron bins is above threshold");
01334     }
01335     if(scp_mode_count[3]!=0){   //scp_mode_count[3] stores number of scp packets in scattered plot mode
01336         gPC.printf("\n\rNumber of SCP in scattered plot mode: %d",scp_mode_count[3]);
01337         if(dma_scp_at_count!=0)
01338             gPC.printf("\n\rSCP Above Threshold indices info:");
01339         //for(uint8_t temp_i=0;temp_i<5;temp_i++)
01340             //gPC.printf("\n\rscat_thres_index_scp[%d]:%d",temp_i,scat_thres_index_scp[temp_i]);
01341         for(uint8_t i=0;i<5;i++){
01342             if(scat_thres_index_scp[i]!=0){
01343                 gPC.printf("\n\r%s is above index",srp_scattered[i+2]);
01344             }
01345         }
01346     }
01347     if(scp_mode_count[4]!=0){   //scp_mode_count[4] stores number of scp packets in scattered plot with extreme data conservation mode
01348         gPC.printf("\n\rNumber of SCP in scattered mode with Extreme data conservation: %d",scp_mode_count[4]);
01349         if(scat_thres_index_scp[3]!=0)    //only bulk fast counts bin is present other than sum of energies bins
01350             gPC.printf("\n\rBulk fast counts is above threshold");
01351         if(proton_scp_sum_at==1)
01352             gPC.printf("\n\rSum of proton bins is above threshold");
01353         if(electron_scp_sum_at==1)
01354             gPC.printf("\n\rSum of electron bins is above threshold");    
01355     }
01356 }
01357 #endif
01358 //give the pointer of 6 second data to this function
01359 void srp(uint8_t * head)//void const *args)
01360 {
01361     //timer_test.start();
01362     uint8_t sd_stat = 0;
01363 //    SCI_LED1 =1;
01364    
01365    /****** added by samp to HK occurance *********/
01366    if (HK_timer_toggle==0)
01367    {
01368    HK_counter->stop();
01369    wait_ms(0.1);
01370    HK_counter->start(10000);
01371    }
01372    HK_timer_toggle = !HK_timer_toggle;
01373    
01374    /****** added by samp to HK occurance *********/
01375    
01376     // SCI_LED1 = !SCI_LED1;
01377     //gPC.printf("\n\rsrp");
01378     debug_cntr = 0;
01379     sci_time = FCTN_CDMS_RD_RTC();
01380     TIME_LATEST_SPI_SPEED = sci_time;
01381     #if debug_time
01382     gPC.printf("\n\rAssigning time manually");
01383         sci_time = 0x000000055555555;
01384     #endif
01385     if(time_prev_scp==0){
01386         time_prev_scp = sci_time;
01387     } 
01388  //   gPC.printf("enterdSCI_SD7");   
01389  //  gPC.printf("timer start %d::",HK_counter.read_ms());  
01390  
01391       FCTN_SD_MNGR();     
01392    
01393   //  gPC.printf("timer end %d::",HK_counter.read_ms());                                                    ///changed recently
01394    // timer_test.stop();
01395 //    gPC.printf("endSCI_SD8");
01396     #if tabulation
01397     if(head[0]==0 & head[1]==0){
01398         position_tm_frame[1] = position_tm_starting[1];
01399         position_tm_frame[2] = position_tm_starting[2];
01400         gPC.printf("\n\n\r--------------------------*************************************-------------------------------");
01401         //gPC.printf("\n\rFrame pointer reset done");
01402         gPC.printf("\n\rStart FSC for TMID=1 :%d",FSC_CURRENT[1]+1);
01403         gPC.printf("\n\rStart FSC for TMID=2 :%d",FSC_CURRENT[2]+1);
01404         gPC.printf("\n\rStart FSC for TMID=3 :%d",FSC_CURRENT[3]+1);
01405         gPC.printf("\n\r--------------------------*************************************-------------------------------");
01406     }
01407     #endif    
01408     ptr = head + 3;                                         //ptr points to proton energy bin of srp
01409     srp_mode = head[2]&0x01;
01410     //for(uint8_t temp_counter=0;temp_counter<9;temp_counter++){
01411     for (int i = 0; i < 52 ; i++)
01412     {
01413         scp_bin[i] = 0;
01414     }
01415     #if test_science
01416     gPC.printf("\n\n\rTesting science mode - initializing test indices and to zero");
01417     for(uint8_t i=0;i<5;i++)
01418         for(uint8_t j=0;j<60;j++){
01419             calib_thres_index[i][j] = 0;
01420             scat_thres_index[i][j] = 0;            
01421         }
01422     for(uint8_t i=5;i<16;i++)
01423         for(uint8_t j=0;j<60;j++){
01424             calib_thres_index[i][j] = 0;
01425             calib_thres_index_scp[i] = 0;
01426         }
01427     for(uint8_t i=0;i<16;i++){
01428         if(i<5){
01429             scat_thres_subindex[i] = 0;
01430             scat_thres_index_scp[i] = 0;
01431             scp_mode_count[i] = 0;
01432         }
01433         calib_thres_subindex[i] = 0;
01434         thres_scp_index[i] = 0;
01435         calib_thres_index_scp[i] = 0;
01436     }
01437     dma_scp_at_count = 0;
01438     dma_sfp_at_count = 0;
01439     proton_scp_sum_at = 0;
01440     electron_scp_sum_at = 0;
01441     sfp_mode_count[0] = 0;
01442     sfp_mode_count[1] = 0;
01443     /*if((head[2]&0x01)==0){
01444         gPC.printf("\n\rGenerating DMA calibrated data");
01445         generate_dma_calibrated(head);  //fill the dma with data given in data_srp_calibrated[] and repeat it 60 times to fill the entire dma        
01446     }
01447     else if((head[2]&0x1)==1){
01448         gPC.printf("\n\rGenerating DMA scattered data");
01449         generate_dma_scattered(head);   //fill the dma with data given in data_srp_scattered[] and repeat it 60 times to fill the entire dma
01450     }
01451     generated_dma(head);*/
01452     #endif
01453     for(uint8_t counter = 0 ; counter < 60 ; counter++)
01454     {       
01455         #if srpz
01456         gPC.printf("\n\n\rSRP count:%d",counter+1);
01457         #endif
01458         /*---------------------------------populating sfp_bin and scp_bin[] starts here(also tag AT or BT)-----------------------*/
01459         at = 0;     pzf = 1;    ezf = 1;
01460         if(srp_mode == 0)                                   //calibrated mode 
01461         {
01462             #if srpz
01463             gPC.printf("\n\rSRP - Calibrated Mode");
01464             #endif
01465             for(int i=0; i<48 ; i++)                        //first 48 bins or SRP are of 16 bits size
01466             {
01467                 sfp_bin[i] = read_2byte(ptr + i*2);
01468                 /*if(counter==0){
01469                     gPC.printf("\n\r sfp_bin[%d] = %d",i,sfp_bin[i]);
01470                 }*/
01471                 scp_bin[i] += sfp_bin[i];
01472                 if(sfp_bin[i]>sfp_threshold_m0[i])
01473                 {
01474                     //gPC.printf("\n\rSFP above threshold index:%d",i);
01475                     #if test_science
01476                     //gPC.printf("\n\ri:%d",i);
01477                     //gPC.printf("\n\ri:%d\tj:%d",map_index_bins_2_srp_index(i,srp_mode)-2,calib_thres_subindex[map_index_bins_2_srp_index(i,srp_mode)-2]);
01478                     calib_thres_index[map_index_bins_2_srp_index(i,srp_mode)-2][calib_thres_subindex[map_index_bins_2_srp_index(i,srp_mode)-2]] = 1;
01479                     //gPC.printf("\n\ri:%d\tj:%d",map_index_bins_2_srp_index(i,srp_mode)-2,calib_thres_subindex[map_index_bins_2_srp_index(i,srp_mode)-2]);
01480                     #endif
01481                     at = 1;
01482                 }
01483                 if(i<17)
01484                 {
01485                     if(sfp_bin[i] > 0)
01486                         pzf = 0;
01487                 }
01488                 else if (i>17 && i < 23)
01489                 {
01490                     if(sfp_bin[i]>0)
01491                         ezf = 0;
01492                 }
01493             }
01494             for(int i=0; i<4; i++)                          //last 4 bins(excluding the spare bin) are 32 bit each
01495             {
01496                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
01497                 /*if(counter==0){
01498                     gPC.printf("\n\rsfp_bin[%d] = %d",48+i,sfp_bin[i+48]);                    
01499                 }*/
01500                 scp_bin[i+48] += sfp_bin[i+48];
01501                 if(sfp_bin[i+48]>sfp_threshold_m0[i+48]){
01502                     //gPC.printf("\n\rSP above threshold index: %d",i+48);
01503                     #if test_science
01504                     //gPC.printf("\n\ri:%d\tj:%d",map_index_bins_2_srp_index(48+i,srp_mode)-2,calib_thres_subindex[map_index_bins_2_srp_index(48+i,srp_mode)-2]);
01505                     calib_thres_index[map_index_bins_2_srp_index(48+i,srp_mode)-2][calib_thres_subindex[map_index_bins_2_srp_index(48+i,srp_mode)-2]] = 1;
01506                     //gPC.printf("\n\ri:%d\tj:%d",map_index_bins_2_srp_index(48+i,srp_mode)-2,calib_thres_subindex[map_index_bins_2_srp_index(48+i,srp_mode)-2]);
01507                     #endif
01508                     at = 1;
01509                 }
01510             }
01511         }        
01512         else if(srp_mode == 1)                              //scattered mode
01513         {
01514             #if srpz
01515             gPC.printf("\n\rSRP - Scatter Plot Mode");
01516             #endif
01517             for(int i = 0; i <48; i++)                      //first 48 bins of SRP are of 16 bit size
01518             {
01519                 sfp_bin[i]  = read_2byte(ptr+2*i);
01520                 scp_bin[i] += sfp_bin[i];
01521                 /*if(counter==0){               
01522                     gPC.printf("\n\rsfp_bin[%d]= %u\t\tscp_bin[%d]= %u",i,sfp_bin[i],i,scp_bin[i]); 
01523                 }*/
01524                 if(sfp_bin[i] > sfp_threshold_m1[i]){
01525                     //gPC.printf("\n\rSP above threshold index: %d",i);
01526                     #if test_science
01527                     scat_thres_index[map_index_bins_2_srp_index(i,srp_mode)-2][scat_thres_subindex[map_index_bins_2_srp_index(i,srp_mode)-2]] = 1;
01528                     #endif
01529                     at = 1;
01530                 }
01531             }
01532             for(int i = 0; i < 4 ; i++)                     //next 4 bins are of 32 bit size
01533             {
01534                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
01535                 scp_bin[i+48] += sfp_bin[i+48];
01536                 
01537                 /*if(counter==0){
01538                     gPC.printf("\n\rsfp_bin[%d]= %u\t\tscp_bin[%d]= %u",i,sfp_bin[i],i,scp_bin[i]);
01539                 }*/
01540                 if(sfp_bin[i+48] > sfp_threshold_m1[i+48]){
01541                     //gPC.printf("\n\rSP above threshold index: %d",i+48);
01542                     #if test_science
01543                     scat_thres_index[map_index_bins_2_srp_index(48+i,srp_mode)-2][scat_thres_subindex[map_index_bins_2_srp_index(48+i,srp_mode)-2]] = 1;
01544                     #endif
01545                     at = 1;
01546                 }
01547             }
01548         }
01549         #if test_science
01550         for(uint8_t temp_i = 0;temp_i<16;temp_i++){
01551             if(calib_thres_index[temp_i][calib_thres_subindex[temp_i]]==1){
01552                 calib_thres_index[temp_i][calib_thres_subindex[temp_i]] = counter+1;
01553                 calib_thres_subindex[temp_i]++;
01554             }
01555         }
01556         for(uint8_t temp_i = 0;temp_i<5;temp_i++){
01557             if(scat_thres_index[temp_i][scat_thres_subindex[temp_i]]==1){
01558                 scat_thres_index[temp_i][scat_thres_subindex[temp_i]] = counter+1;
01559                 scat_thres_subindex[temp_i]++;
01560             }
01561         }
01562         #endif
01563         /*---------------------------populating sfp_bin[] and scp_bin[] from srp ends here(also tagging AT or BT)------------------*/
01564         #if test_science
01565         //gPC.printf("\n\n\rCounter: %d",counter);
01566         //verify_read_dma(srp_mode,counter);
01567         #endif
01568         ptr = ptr + 112;                                    //moving the pointer to next srp packet in the DMA buffer
01569         
01570         /*------------------------------------------forming a science fine packet starts here----------------------------------------
01571         First we fill packet header, then fill packet data and finally assign its length
01572         ----------------------------------------------------------------------------------------------------------------------------*/
01573         for(int i = 0; i<71; i++)
01574             sfp[i] = 0;
01575         if(srp_mode == 0)                                                                   //calibrated mode
01576         {
01577             if(at == 0)
01578             {
01579                 #if srpz
01580                 gPC.printf("\n\rSFP Below Threshold");
01581                 #endif
01582                 pointer = sfp; debug_cntr = 0;
01583                 space = adjust(4, attitude,pointer,8);      pointer += space>>4;    debug_cntr += space>>4;
01584                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
01585                 space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
01586                 space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
01587                 space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
01588                 for(uint8_t i = 0 ; i < 8 ; i++){
01589                     space = adjust(1, ((compress(sfp_bin[24+i],7,2))&0x100)>>8 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
01590                     space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01591                 }
01592                 for(uint8_t i = 0 ; i <12 ; i++){
01593                     space = adjust(6, (compress(sfp_bin[32+i],4,2)) ,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
01594                 }
01595                 for(uint8_t i = 0; i < 4; i++){
01596                     space = adjust(8, (compress(sfp_bin[44+i],6,2)) ,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
01597                 }
01598                 for(uint8_t i = 0 ; i < 4 ; i++){
01599                     space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
01600                     space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
01601                 }
01602                 space = adjust(8, ((compress(sfp_bin[17],6,2))&0xff) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
01603                 space = adjust(8, ((compress(sfp_bin[23],6,2))&0xff) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
01604                 if(pzf == 0){
01605                     for(uint8_t i = 0; i<2 ; i++){
01606                         space = adjust(6, ((compress(sfp_bin[i],5,1))&0xff) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
01607                     }
01608                     for(uint8_t i = 0; i<15 ; i++){
01609                         space = adjust(5, ((compress(sfp_bin[i+2],4,1))&0xff) ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
01610                     }
01611                 }
01612                 if(ezf == 0){
01613                     for(uint8_t i = 0; i <5 ;i++){
01614                         space = adjust(7, ((compress(sfp_bin[18+i],6,1))&0xff) ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
01615                     }
01616                 }
01617             }                                                                                           //below thershold ends here.
01618             if(at == 1){
01619                 #if srpz
01620                 gPC.printf("\n\rSFP Above Threshold");
01621                 #endif
01622                 pointer = sfp + 6; debug_cntr = 6;space = 8;
01623                 sfp[0] = (sci_time>>27)&0xff;   sfp[1] = (sci_time>>19)&0xff;   sfp[2] = (sci_time>>11)&0xff;   sfp[3] = (sci_time>>3)&0xff;
01624                 sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5);
01625                 sfp[5] = ((counter&0x1f)<<3) + (srp_mode<<2);
01626                 sfp[5] += (pzf<<1) + ezf ;
01627                 for(uint8_t i = 0 ; i < 8 ; i++){
01628                     space = adjust(1, (compress(sfp_bin[24+i],7,2))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
01629                     space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01630                 }
01631                 for(uint8_t i = 0 ; i <12 ; i++){
01632                     space = adjust(6, ((compress(sfp_bin[32+i],3,3))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01633                 }
01634                 for(uint8_t i = 0 ; i < 4 ; i++){
01635                     space = adjust(2, (compress(sfp_bin[44+i],8,2))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
01636                     space = adjust(8, compress(sfp_bin[44+i],8,2) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
01637                 }
01638                 for(uint8_t i = 0 ; i < 4 ; i++){
01639                     space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
01640                     space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
01641                 }
01642                 space = adjust(2, (compress(sfp_bin[17],8,2))>>8 ,pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
01643                 space = adjust(8, compress(sfp_bin[17],8,2) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
01644                 space = adjust(2, (compress(sfp_bin[23],8,2))>>8 ,pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
01645                 space = adjust(8, compress(sfp_bin[23],8,2) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
01646 
01647                 if(pzf == 0){
01648                     //cout<<"proton bins ";
01649                     #if srpz
01650                     gPC.printf("\n\rProton bins present in SRP");
01651                     #endif
01652                     for(uint8_t i = 0; i<17 ; i++){
01653                         space = adjust(2, ((compress(sfp_bin[i],8,2))>>8) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
01654                         space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
01655                         //gPC.printf("%02X ",compress(sfp_bin[i],8,2));
01656                     }
01657                 }
01658                 //if(pzf==1)
01659                     //gPC.printf("\n\rAll proton bins empty");
01660                 if(ezf == 0){
01661                     //cout<<"electron bins ";
01662                     #if srpz
01663                     gPC.printf("\n\rElectron bins present in SRP");
01664                     #endif
01665                     for(int i = 0; i<5 ; i++){
01666                         space = adjust(2,((compress(sfp_bin[18+i],8,2))>>8),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01667                         space = adjust(8, compress(sfp_bin[18+i],8,2),pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
01668                         //gPC.printf("%02X ",compress(sfp_bin[i],8,2));
01669                     }
01670                 }
01671                 //if(ezf==1)
01672                     //gPC.printf("\n\rAll electron bins empty");
01673             }                                                                                   //above threshold ends here.            
01674             if(at == 0)
01675             {
01676                 id = 1;     length = 241;
01677                 if(pzf == 0)
01678                     length += 87;
01679                 if(ezf == 0)
01680                     length += 35;                
01681             }
01682             else
01683             {
01684                 id = 2;     length = 288;
01685                 if(pzf == 0)
01686                     length += 170;
01687                 if(ezf == 0)
01688                     length += 50;
01689                 #if test_science
01690                 dma_sfp_at_count++;
01691                 #endif
01692             }
01693             #if test_science
01694             sfp_mode_count[0]++;
01695             #endif
01696         }
01697         else if(srp_mode == 1)                                                                 //scattered mode
01698         {
01699             if(at == 0)
01700             {
01701                 #if srpz
01702                 gPC.printf("\n\rSFP below threshold");
01703                 #endif
01704                 pointer = sfp; debug_cntr = 0;  space = 8;
01705                 space = adjust(4, attitude,pointer,8);      pointer += space>>4;    debug_cntr += space>>4;
01706                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
01707                 space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
01708                 for(uint8_t i=0; i<48; i++)
01709                 {
01710                     space = adjust(7, compress(sfp_bin[i],5,2) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
01711                 }
01712                 for(uint8_t i = 0 ; i < 4 ; i++)
01713                 {
01714                     space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01715                     space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
01716                 }
01717             }
01718             if(at == 1)
01719             {
01720                 
01721                 #if srpz                
01722                 gPC.printf("\n\rSFP above threshold");
01723                 #endif
01724                 pointer = sfp; debug_cntr = 0;  space = 8;
01725                 space = adjust(3, sci_time>>32 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
01726                 space = adjust(8, sci_time>>24 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
01727                 space = adjust(8, sci_time>>16 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
01728                 space = adjust(8, sci_time>>8 ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
01729                 space = adjust(8, sci_time ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01730                 space = adjust(4, attitude,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
01731                 space = adjust(6, counter,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
01732                 space = adjust(1, srp_mode,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
01733                 for(uint8_t i=0; i<48; i++)
01734                 {
01735                     space = adjust(2, (compress(sfp_bin[i],8,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
01736                     space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01737                 }
01738                 for(uint8_t i = 0 ; i < 4 ; i++)
01739                 {
01740                     space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
01741                     space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
01742                 }
01743             }
01744             if(at == 0)
01745             {
01746                 id = 1;     length = 384;                                       
01747             }
01748             else
01749             {
01750                 id = 2;     length = 568;
01751                 #if test_science
01752                 dma_sfp_at_count++;
01753                 #endif
01754             }
01755             #if test_science
01756             sfp_mode_count[1]++;
01757             #endif
01758         }
01759         length = (length%8==0)?(length/8):(length/8)+1;                         //converting length to mulitple of 8
01760         #if srpz
01761         gPC.printf("\n\rLength of SFP packet in bytes = %d",length);
01762         #endif
01763         //gPC.printf("\n\rLength of SFP packet in bytes = %d",length);
01764         /*----------------------------------------filling the science fine packet array ends here------------------------------------------*/
01765 
01766         /*-----------------------------Inserting the above packet(sfp) into the TM frame(s) starts here----------------------------*/
01767         if(id==1){  //below threshold
01768             if(position_tm_frame[id]>130){
01769                 #if srpz
01770                 gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame.");
01771                 #endif
01772                 while(position_tm_frame[id]<132){
01773                     frames[id][position_tm_frame[id]] = 0;
01774                     position_tm_frame[id]++;
01775                 }
01776                 //position_tm_frame[id]=position_tm_starting[id];
01777             }
01778         }
01779         else if(id==2){ //above threshold
01780             if(position_tm_frame[id]>126){
01781                 #if srpz
01782                 gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame.");
01783                 #endif
01784                 while(position_tm_frame[id]<132){
01785                     frames[id][position_tm_frame[id]] = 0;
01786                     position_tm_frame[id]++;
01787                 }
01788                 //position_tm_frame[id]=position_tm_starting[id];     //
01789             }
01790         }
01791         if(position_tm_frame[id]==position_tm_starting[id]){
01792             frames[id][6-id] = position_tm_starting[id];
01793         }
01794         for(uint16_t j=0 ; j<length ;)
01795         {            
01796             if(position_tm_frame[id]==frames[id][6-id]){
01797                 if(id==1){
01798                     #if srpz
01799                     gPC.printf("\n\rAdding Time to TM frame");
01800                     #endif
01801                     frames[id][6]  = (uint8_t)((sci_time>>32)&0x07);
01802                     frames[id][7]  = (uint8_t)((sci_time>>24)&0xff);
01803                     frames[id][8]  = (uint8_t)((sci_time>>16)&0xff);
01804                     frames[id][9]  = (uint8_t)((sci_time>>8)&0xff);
01805                     frames[id][10] = (uint8_t)((sci_time)&0xff);
01806                 }
01807             }
01808             if(position_tm_frame[id]<132){
01809                 frames[id][position_tm_frame[id]] = sfp[j];
01810                 j++;
01811                 position_tm_frame[id]++;                                            //incrementing position of write pointer in TM frame
01812             }
01813             if(position_tm_frame[id] == 132)
01814             {
01815                 /*-----------the current frame is completely filled and we fill frame header to write into SD card---------*/
01816                 #if srpz
01817                 gPC.printf("\n\rSFP_TM frame full. Length of current packet = %d",j);
01818                 gPC.printf("\n\rFirst Header pointer: %d",frames[id][6-id]);
01819                 gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]);
01820                 #endif
01821                 pointer = frames[id];
01822                 if(id == 1){    //below threshold
01823                     space = adjust(1,0,pointer,8);
01824                     space = adjust(4,3,pointer,space);
01825                     FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit 
01826 
01827                     //gPC.printf("3 = 0x%X",FSC_science);                   
01828                     #if debug_fsc
01829                     frames[id][1] = (test_fsc[3]>>24)&0xff;
01830                     frames[id][2] = (test_fsc[3]>>16)&0xff;
01831                     frames[id][3] = (test_fsc[3]>>8)&0xff;
01832                     frames[id][4] = test_fsc[3]&0xff;                    
01833                     test_fsc[3]++;
01834                     #endif                    
01835                     #if !debug_fsc
01836                     frames[id][1] = (FSC_science>>24)&0xff;
01837                     frames[id][2] = (FSC_science>>16)&0xff;
01838                     frames[id][3] = (FSC_science>>8)&0xff;
01839                     frames[id][4] = FSC_science&0xff;                    
01840                     #endif
01841                 }
01842                 else if(id == 2)    //above threshold
01843                 {
01844                     space = adjust(1,0,pointer,8);
01845                     space = adjust(4,2,pointer,space);                    
01846                     FSC_science = FSC_CURRENT[2];                                               ///to be used as this
01847                     //gPC.printf("2 = 0x%X",FSC_science);
01848                     #if debug_fsc
01849                     frames[id][1] = (test_fsc[2]>>16)&0xff;
01850                     frames[id][2] = (test_fsc[2]>>8)&0xff;
01851                     frames[id][3] = test_fsc[2]&0xff;
01852                     test_fsc[2]++;
01853                     #endif
01854                     #if !debug_fsc
01855                     frames[id][1] = (FSC_science>>16)&0xff;
01856                     frames[id][2] = (FSC_science>>8)&0xff;
01857                     frames[id][3] = FSC_science&0xff;
01858                     #endif
01859                 }
01860                 temp_crc = crc16_gen(frames[id],132);
01861                 frames[id][132] = temp_crc>>8;
01862                 frames[id][133] = temp_crc & 0xff;
01863                 
01864                 #if srpz
01865                 gPC.printf("\n\rPrinting SFP_TM\n\r{");                
01866                 //gPC.printf("\n\r{");
01867                 for(uint8_t z=0;z<134;z++){
01868                     gPC.printf("%02X",frames[id][z]);
01869                 }
01870                 gPC.printf("}\n\r");
01871                 #endif
01872                 #if tabulation
01873                 tabulate_TM(&frames[id][0]);
01874                 #endif
01875                 /*------------------current TM frame completely filled-----------------------*/
01876                 exor(frames[id]);
01877                 convolution(frames[id]);
01878                 interleave(TM_convoluted_data,TM_interleave_data);
01879                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
01880   //              gPC.printf("enterSCI_SD11");
01881                 
01882   //              gPC.printf("timer start %d::",HK_counter);
01883                 if(id == 1)
01884                 {
01885                     sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
01886                 }
01887                 else if (id == 2)
01888                 {
01889                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
01890                 }
01891                 if(sd_stat)
01892                 {
01893                     gPC.printf("\n\n\rsd write fail---------------------------------------");
01894                 }
01895                 
01896              //  gPC.printf("timer end %d::",timer_test.read_ms());                                                    ///changed recently
01897              //    timer_test.stop();
01898               
01899              
01900  //              gPC.printf("endSCI_SD12");
01901                 position_tm_frame[id] = position_tm_starting[id];
01902                 if(j!=0)
01903                     frames[id][6-id] = (length - j)+position_tm_starting[id];
01904                 else if(j==0)
01905                     frames[id][6-id] = position_tm_starting[id];
01906             }
01907         }
01908         /*------------------------------finished inserting the sfp packet into the TM frame(s)-------------------------------------*/
01909     }                                                                               // for loop bracket which runs 60 times
01910     //ptr = ptr + 112;                                    //moving the pointer to next srp packet in the DMA buffer
01911     
01912     #if test_science
01913     if(pzf==1){
01914         gPC.printf("\n\rAll protons bins empty");
01915     }
01916     if(ezf==1){
01917         gPC.printf("\n\rAll electrons bins are empty");        
01918     }
01919     #endif
01920     #if srpz_last
01921     //gPC.printf("\n\rPrinting the last SFP_TM frame");
01922     //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]);
01923     while(position_tm_frame[id]<132){
01924         frames[id][position_tm_frame[id]] = 0;
01925         position_tm_frame[id]++;
01926     }
01927     if(position_tm_frame[id]==132){
01928         //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]);
01929         pointer = frames[id];
01930         if(id == 1){    //below threshold
01931             space = adjust(1,0,pointer,8);
01932             space = adjust(4,3,pointer,space);
01933             FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
01934             #if debug_fsc
01935             frames[id][1] = (test_fsc[3]>>24)&0xff;
01936             frames[id][2] = (test_fsc[3]>>16)&0xff;
01937             frames[id][3] = (test_fsc[3]>>8)&0xff;
01938             frames[id][4] = test_fsc[3]&0xff;                    
01939             test_fsc[3]++;
01940             #endif
01941             #if !debug_fsc
01942             frames[id][1] = (FSC_science>>24)&0xff;
01943             frames[id][2] = (FSC_science>>16)&0xff;
01944             frames[id][3] = (FSC_science>>8)&0xff;
01945             frames[id][4] = FSC_science&0xff;                    
01946             #endif
01947         }
01948         else if(id == 2)    //above threshold
01949         {
01950             space = adjust(1,0,pointer,8);
01951             space = adjust(4,2,pointer,space);                    
01952             FSC_science = FSC_CURRENT[2];                                               ///to be used as this
01953             #if debug_fsc
01954             frames[id][1] = (test_fsc[2]>>16)&0xff;
01955             frames[id][2] = (test_fsc[2]>>8)&0xff;
01956             frames[id][3] = test_fsc[2]&0xff;
01957             test_fsc[2]++;
01958             #endif
01959             #if !debug_fsc
01960             frames[id][1] = (FSC_science>>16)&0xff;
01961             frames[id][2] = (FSC_science>>8)&0xff;
01962             frames[id][3] = FSC_science&0xff;
01963             #endif
01964         }
01965         temp_crc = crc16_gen(frames[id],132);
01966         frames[id][132] = temp_crc>>8;
01967         frames[id][133] = temp_crc & 0xff;        
01968         
01969         //gPC.printf("\n\rPrinting SFP_TM\n\r{");
01970         gPC.printf("\n\r{");
01971         for(uint8_t z=0;z<134;z++){
01972             gPC.printf("%02X",frames[id][z]);
01973         }
01974         gPC.printf("}\n\r");
01975         position_tm_frame[id] = position_tm_starting[id];
01976     }
01977     #endif
01978     at = 0;     pzf = 1;    ezf = 1;
01979     srp_mode = head[2]&0x1;
01980     uint8_t compression_option = (head[2]>>1)&0x3;
01981     /*-------------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT starts here-------------------------
01982     ------------------(in case compression option = 0 , then tagging beacon packet(not yet formed) as AT or BT is also done)----------*/
01983     if(srp_mode ==0)
01984     {
01985         if(compression_option == 0)
01986         {
01987             for(int i=0; i<52 ;i++)
01988             {
01989                 if(scp_bin[i] > scp_threshold_m0[i])
01990                 {
01991                     //gPC.printf("\n\rSCP above threshold index: %d",i);
01992                     #if test_science
01993                     calib_thres_index_scp[map_index_bins_2_srp_index(i,srp_mode)-2] = 1;
01994                     #endif
01995                     at = 1;
01996                     //break;
01997                 }
01998             }
01999             #if test_science
02000             scp_mode_count[0]++;
02001             #endif
02002         }
02003         else if(compression_option == 1)
02004         {
02005             if(scp_bin[44] > scp_threshold_m0_1[0]){
02006                 //gPC.printf("\n\rSCP above threshold index: 44"); 
02007                 #if test_science
02008                 //thres_scp_index[map_index_bins_2_srp_index(44,srp_mode)-2] = 1;
02009                 calib_thres_index_scp[map_index_bins_2_srp_index(44,srp_mode)-2] = 1;
02010                 #endif
02011                 at=1;
02012             }
02013             for(int i=0; i<4 ;i++)
02014             {
02015                 if(scp_bin[48+i] > scp_threshold_m0_1[i+1])
02016                 {
02017                     //gPC.printf("\n\rSCP above threshold index: %d",i+48);
02018                     #if test_science
02019                     //thres_scp_index[map_index_bins_2_srp_index(48+i,srp_mode)-2] = 1;
02020                     calib_thres_index_scp[map_index_bins_2_srp_index(48+i,srp_mode)-2] = 1;
02021                     #endif
02022                     at = 1;
02023                     //break;
02024                 }
02025             }
02026             if(scp_bin[17] > scp_threshold_m0_1[5]){ 
02027                 //gPC.printf("\n\rSP above threshold index: 17");
02028                 #if test_science
02029                 //thres_scp_index[map_index_bins_2_srp_index(17,srp_mode)-2] = 1;
02030                 calib_thres_index_scp[map_index_bins_2_srp_index(17,srp_mode)-2] = 1;
02031                 #endif
02032                 at=1;
02033             }
02034             if(scp_bin[23] > scp_threshold_m0_1[6]){ 
02035                 //gPC.printf("\n\rSP above threshold index: 23");
02036                 #if test_science
02037                 //thres_scp_index[map_index_bins_2_srp_index(23,srp_mode)-2] = 1;
02038                 calib_thres_index_scp[map_index_bins_2_srp_index(23,srp_mode)-2] = 1;
02039                 #endif
02040                 at=1;
02041             }
02042             proton_scp_sum = 0; electron_scp_sum = 0;
02043             for(int i=0;i<17;i++)
02044             {
02045                 proton_scp_sum += scp_bin[i];
02046             }
02047             for(int i=0; i<5; i++)
02048             {
02049                 electron_scp_sum += scp_bin[18+i];
02050             }
02051             if(proton_scp_sum > scp_threshold_m0_1[7]){ 
02052                 //gPC.printf("\n\rProton_scp_sum above threshold");
02053                 #if test_science
02054                 proton_scp_sum_at = 1;
02055                 #endif
02056                 at=1;
02057             }
02058             if(electron_scp_sum > scp_threshold_m0_1[8]){
02059                 //gPC.printf("\n\rElectron_scp_sum above threshold");
02060                 #if test_science
02061                 electron_scp_sum_at = 1;
02062                 #endif
02063                 at=1;
02064             }
02065             #if test_science
02066             scp_mode_count[1]++;
02067             #endif
02068         }
02069         else if(compression_option == 2)
02070         {
02071             if(scp_bin[50] > scp_sfp_threshold_m0_2[0]){
02072                 //gPC.printf("\n\rSCP above threshold index:50"); 
02073                 #if test_science
02074                 //thres_scp_index[map_index_bins_2_srp_index(50,srp_mode)-2] = 1;
02075                 calib_thres_index_scp[map_index_bins_2_srp_index(50,srp_mode)-2] = 1;
02076                 #endif
02077                 at=1;
02078             }
02079             proton_scp_sum = 0; electron_scp_sum = 0;
02080             for(int i=0;i<17;i++)
02081             {
02082                 proton_scp_sum += scp_bin[i];
02083             }
02084             for(int i=0; i<5; i++)
02085             {
02086                 electron_scp_sum += scp_bin[18+i];
02087             }
02088             if(proton_scp_sum > scp_sfp_threshold_m0_2[1]){//gPC.printf("\n\rProton_SCP_sum above threshold"); 
02089             #if test_science
02090             proton_scp_sum_at = 1;
02091             #endif
02092             at=1;}
02093             if(electron_scp_sum > scp_sfp_threshold_m0_2[2]){//gPC.printf("\n\rElectron above threshold");
02094             #if test_science
02095             electron_scp_sum_at = 1;
02096             #endif
02097              at=1;}
02098             #if test_science
02099             scp_mode_count[2]++;
02100             #endif
02101         }
02102     }
02103     else if(srp_mode ==1)
02104     {
02105         if(compression_option==0){
02106             for(int i=0; i<48; i++)
02107             {
02108                 if(scp_bin[i] > scp_threshold_m1[i]){
02109                     //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]);
02110                     #if test_science
02111                     scat_thres_index_scp[map_index_bins_2_srp_index(i,srp_mode)-2] = 1;
02112                     #endif
02113                     at = 1;
02114                     //break;
02115                 }
02116             }
02117             for(int i=48; i<52; i++)
02118             {
02119                 if(scp_bin[i] > scp_threshold_m1[i]){
02120                     //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]);
02121                     #if test_science
02122                     scat_thres_index_scp[map_index_bins_2_srp_index(i,srp_mode)-2] = 1;
02123                     #endif
02124                     at = 1;
02125                     //break;
02126                 }
02127             }
02128             #if test_science
02129             scp_mode_count[3]++;
02130             #endif
02131         }
02132         else if(compression_option==2){            
02133             if(scp_bin[50]>scp_sfp_threshold_m0_2[0]){
02134                 //gPC.printf("\nSCP above threshold index:50, Value:%",scp_bin[50]);
02135                 #if test_science
02136                 //thres_scp_index[map_index_bins_2_srp_index(50,srp_mode)-2] = 1;
02137                 scat_thres_index_scp[map_index_bins_2_srp_index(50,srp_mode)-2] = 1;
02138                 #endif
02139                 at=1;
02140             }
02141             #if test_science
02142             scp_mode_count[4]++;
02143             #endif
02144         }
02145     }
02146     /*----------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT ends here--------------------------*/
02147     /*for(uint8_t temp_i=0;temp_i<52;temp_i++){
02148         gPC.printf("\n\rscp_bin[%d] =%d",temp_i+1,scp_bin[temp_i]);
02149     }*/
02150     if(srp_mode == 0)
02151     {
02152         //determining if non zero values of proton and electron bins exist for calibrated mode (srp)
02153         for(int i=0; i<17 ;i++)
02154         {
02155             if(scp_bin[i]>0)
02156                 pzf = 0;
02157         }
02158         for(int i=18; i<23 ;i++)
02159         {
02160             if(scp_bin[i]>0)
02161                 ezf = 0;
02162         }
02163     }
02164     /*-----------------------------------------Tagging beacon packet as AT or BT starts here-------------------------------------*/
02165     beacon_at = 0;
02166     if(srp_mode==0)     //those bins which can be meaningfully compared only in srp_mode==0
02167     {
02168         if(scp_bin[44]>beacon_threshold[0])     beacon_at = 1;            
02169         if(scp_bin[17]>beacon_threshold[5])     beacon_at = 1;
02170         if(scp_bin[23]>beacon_threshold[6])     beacon_at = 1;
02171         proton_scp_sum = 0; electron_scp_sum = 0;
02172         for(int i=0;i<17;i++)
02173         {
02174             proton_scp_sum += scp_bin[i];
02175         }
02176         for(int i=0; i<5; i++)
02177         {
02178             electron_scp_sum += scp_bin[18+i];
02179         }
02180         if(proton_scp_sum > beacon_threshold[7]) beacon_at =1;
02181         if(electron_scp_sum > beacon_threshold[8]) beacon_at =1;
02182     }
02183     if(scp_bin[48]>beacon_threshold[1])     beacon_at = 1;
02184     if(scp_bin[49]>beacon_threshold[2])     beacon_at = 1;
02185     if(scp_bin[50]>beacon_threshold[3])     beacon_at = 1;
02186     if(scp_bin[51]>beacon_threshold[4])     beacon_at = 1;   
02187     /*----------------------------------------Tagging beacon packet as AT or BT ends here------------------------------------------*/
02188 
02189     /*-----------------------------------------------Forming the beacon packet starts here-----------------------------------------*/
02190     //Last 512 bits of beacon_array excluding CRC bits is used for storing the 5 most recent beacon packets
02191 
02192     if(beacon_cntr == 1)
02193     {
02194         beacon_ptr = &(beacon_array[47]);   // starting block address, stores the oldest(among the 5 most recent) beacon packet
02195     }
02196     else if(beacon_cntr>1 && beacon_cntr<6)
02197     {
02198         beacon_ptr +=17;                    // increase the block number(totally 5 blocks for 5 beacon packets)
02199     }
02200     else if(beacon_cntr == 6)
02201     {
02202         for(uint16_t i=0;i<17;i++)          // when all 5 blocks have been used shift the last 4 blocks up along the array and
02203         {                                   // overwrite the new beacon packet in the 5th block
02204             beacon_array[47+i] = beacon_array[64+i];
02205             beacon_array[64+i] = beacon_array[81+i];
02206             beacon_array[81+i] = beacon_array[98+i];
02207             beacon_array[98+i] = beacon_array[115+i];
02208         }
02209         beacon_ptr = &(beacon_array[115]);  //address of the 5th block
02210         beacon_cntr = 5;
02211     }
02212     pointer = beacon_ptr;        debug_cntr = 0;     space = 8;
02213     space = adjust(2, compression_option,pointer,space);        pointer += space>>4;    debug_cntr += space>>4; //first two bits of compression option
02214     space = adjust(1, srp_mode,pointer,space);                  pointer += space>>4;    debug_cntr += space>>4;
02215     space = adjust(3,(sci_time>>32)&0x07,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02216     space = adjust(8,(sci_time>>24)&0xff,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02217     space = adjust(8,(sci_time>>16)&0xff,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02218     space = adjust(8,(sci_time>>8)&0xff,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02219     space = adjust(8,(sci_time)&0xff,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
02220     space = adjust(4, (attitude)&0x0f,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02221 
02222     if(srp_mode==0){
02223         if(beacon_at == 0){
02224             space = adjust(1, 0,pointer,space);                                     pointer += space>>4;    debug_cntr += space>>4;
02225             space = adjust(6, compress(scp_bin[44],3,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02226             space = adjust(7, compress(scp_bin[48],4,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02227             space = adjust(7, compress(scp_bin[49],4,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02228             space = adjust(7, compress(scp_bin[50],4,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02229             space = adjust(7, compress(scp_bin[51],4,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02230             space = adjust(6, compress(scp_bin[17],3,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02231             space = adjust(6, compress(scp_bin[23],3,3),pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02232             space = adjust(4, compress(proton_scp_sum ,10,2)>>8,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02233             space = adjust(8, compress(proton_scp_sum ,10,2),pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02234             space = adjust(4, compress(electron_scp_sum ,10,2)>>8,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
02235             space = adjust(8, compress(electron_scp_sum ,10,2),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
02236             pointer = pointer + 1;
02237             for(int temp_i = 12; temp_i<14; temp_i++)
02238             {            
02239                 *pointer = 0;
02240                 pointer = pointer + 1;
02241             }
02242         }
02243         else if(beacon_at==1){
02244             space = adjust(1, 1,pointer,space);     pointer += space>>4;        debug_cntr += space>>4;
02245             space = adjust(1, compress(sfp_bin[44],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02246             space = adjust(8, compress(sfp_bin[44],6,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02247             space = adjust(4, compress(sfp_bin[48],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02248             space = adjust(8, compress(sfp_bin[48],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02249             space = adjust(4, compress(sfp_bin[49],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02250             space = adjust(8, compress(sfp_bin[49],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02251             space = adjust(4, compress(sfp_bin[50],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;                 
02252             space = adjust(8, compress(sfp_bin[50],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02253             space = adjust(4, compress(sfp_bin[51],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;                 
02254             space = adjust(8, compress(sfp_bin[51],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02255             space = adjust(1, compress(sfp_bin[17],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02256             space = adjust(8, compress(sfp_bin[17],6,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02257             space = adjust(1, compress(sfp_bin[23],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02258             space = adjust(8, compress(sfp_bin[23],6,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02259             space = adjust(1, compress(proton_scp_sum ,6,3)>>8,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;         
02260             space = adjust(8, compress(proton_scp_sum ,6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02261             space = adjust(1, compress(electron_scp_sum,6,3)>>8,pointer,space); pointer += space>>4;    debug_cntr += space>>4;     
02262             space = adjust(8, compress(electron_scp_sum ,6,3),pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
02263             //cout<<"for beacon space = "<<(space&0x0f)<<" counter = "<<debug_cntr;
02264         }
02265     }else if(srp_mode==1){
02266         if(beacon_at==0){
02267             space = adjust(1, 0,pointer,space);                             pointer += space>>4;    debug_cntr += space>>4;
02268             space = adjust(6, compress(0,3,3),pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
02269             space = adjust(7, compress(sfp_bin[48],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02270             space = adjust(7, compress(sfp_bin[49],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02271             space = adjust(7, compress(sfp_bin[50],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02272             space = adjust(7, compress(sfp_bin[51],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02273             pointer = pointer + 1;
02274             for(uint8_t temp_i = 7; temp_i<14; temp_i++)
02275             {            
02276                 *pointer = 0;
02277                 pointer = pointer + 1;
02278             }
02279         }
02280         else if(beacon_at==1){
02281             space = adjust(1, 1,pointer,space);                                 pointer += space>>4;    debug_cntr += space>>4;
02282             space = adjust(1, compress(0,6,3)>>8,pointer,space);                pointer += space>>4;    debug_cntr += space>>4;             
02283             space = adjust(8, compress(0,6,3),pointer,space);                   pointer += space>>4;    debug_cntr += space>>4;
02284             space = adjust(4, compress(sfp_bin[48],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02285             space = adjust(8, compress(sfp_bin[48],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02286             space = adjust(4, compress(sfp_bin[49],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             
02287             space = adjust(8, compress(sfp_bin[49],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02288             space = adjust(4, compress(sfp_bin[50],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;                 
02289             space = adjust(8, compress(sfp_bin[50],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02290             space = adjust(4, compress(sfp_bin[51],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;                 
02291             space = adjust(8, compress(sfp_bin[51],9,3),pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02292             pointer = pointer + 1;
02293             for(uint8_t temp_i = 10; temp_i<14; temp_i++)
02294             {            
02295                 *pointer = 0;
02296                 pointer = pointer + 1;
02297             }
02298         }
02299     }
02300     beacon_cntr++;
02301     /*--------------------------------------------------beacon packet ends here--------------------------------------------------------*/
02302 
02303     //gPC.printf("\n\n\rSCP");
02304     uint8_t packet_pp,time_diff;
02305     /*------------------------------------------Forming a science packet(scp) starts here----------------------------------------------*/
02306 
02307     pointer = scp;      debug_cntr = 0;     space = 8;
02308     time_diff = 0;    
02309     packet_pp = 1;      //value 1 indicates there is no time jump, i.e. next packet in the frame arrived within 10 seconds
02310 
02311     /*upto line 892: if the time difference between two successive frames is greater than 10 seconds then packet_pp bit is set to 1
02312       to indicate this time jump*/
02313     if(time_prev_scp!=sci_time)
02314     {
02315         if((((uint8_t)(sci_time>>7))&0x3f)<(((uint8_t)(time_prev_scp>>7))&0x3f))
02316         {
02317             time_diff = time_diff+60;
02318         }
02319         time_diff += (((uint8_t)(sci_time>>7))&0x3f);
02320         time_diff -= (((uint8_t)(time_prev_scp>>7))&0x3f);
02321         if(time_diff>10){
02322             packet_pp = 0;
02323         }
02324         else{
02325             uint8_t min[2],hour[2],day[2],month[2],year[2];
02326             min[0]   = (((uint8_t)(sci_time>>13))&0x3f);
02327             min[1]   = (((uint8_t)(time_prev_scp>>13))&0x3f);
02328             hour[0]  = (((uint8_t)(sci_time>>19))&0x1f);
02329             hour[1]  =  (((uint8_t)(time_prev_scp>>19))&0x1f);
02330             day[0]   = (((uint8_t)(sci_time>>24))&0x1f);
02331             day[1]   = (((uint8_t)(time_prev_scp>>24))&0x1f);
02332             month[0] = (((uint8_t)(sci_time>>29))&0x0f);
02333             month[1] = (((uint8_t)(time_prev_scp>>29))&0x0f);
02334             year[0]  = (((uint8_t)(sci_time>>33))&0x03);
02335             year[1]  = (((uint8_t)(time_prev_scp>>33))&0x03);
02336             if(min[0]<min[1]){
02337                 if(hour[0]<hour[1]){
02338                     if(day[0]<day[1]){
02339                         if(month[0]<month[1]){
02340                             if((year[0]-year[1])!=1)
02341                                 packet_pp = 0;
02342                         }
02343                         else if((month[0]-month[1])!=1)
02344                             packet_pp = 0;
02345                     }
02346                     else if((day[0]-day[1])!=1)
02347                         packet_pp = 0;
02348                 }
02349                 else if((hour[0]-hour[1])!=1)
02350                     packet_pp = 0;
02351             }
02352             else if((min[0]-min[1])!=1)
02353                 packet_pp = 0;
02354         }
02355     }
02356     time_prev_scp = sci_time;
02357     uint32_t sfp_at_counter;
02358     sfp_at_counter = FSC_CURRENT[2];
02359     space = adjust(1, packet_pp,pointer,space);                     pointer += space>>4;    debug_cntr += space>>4;
02360     space = adjust(5, (sci_time>>8)&0x1f,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
02361     space = adjust(8, (sci_time)&0xff,pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
02362     space = adjust(4, (attitude)&0xf,pointer,space);                pointer += space>>4;    debug_cntr += space>>4;
02363     space = adjust(2, compression_option,pointer,space);            pointer += space>>4;    debug_cntr += space>>4; //first two bits of compression option
02364     space = adjust(1, at,pointer,space);                            pointer += space>>4;    debug_cntr += space>>4; //last bit of compression option
02365     space = adjust(1, srp_mode,pointer,space);                      pointer += space>>4;    debug_cntr += space>>4;
02366     if(!((srp_mode==0 && compression_option==2)|(srp_mode==1 && compression_option==2)))
02367     {
02368         space = adjust(8, sfp_at_counter>>16,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02369         space = adjust(8, sfp_at_counter>>8,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02370         space = adjust(8, sfp_at_counter,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
02371     }
02372     if(srp_mode == 0 && compression_option == 0){       //normal callibrated mode
02373         #if scpz
02374         gPC.printf("\n\rSCP - Normal calibrated mode");
02375         #endif
02376         space = adjust(1, pzf,pointer,space);                       pointer += space>>4;    debug_cntr += space>>4;
02377         space = adjust(1, ezf,pointer,space);                       pointer += space>>4;    debug_cntr += space>>4;
02378         if(at == 0 ){
02379             #if scpz
02380             gPC.printf("\n\rBelow threshold");
02381             #endif
02382             for(int i = 0; i<8 ;i++){
02383                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02384             }
02385             for(int i = 0; i<12 ;i++){
02386                 space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02387             }
02388             for(int i = 0; i<4 ;i++){
02389                 space = adjust(6,compress(scp_bin[44+i],3,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02390             }
02391             for(int i = 0; i<4 ;i++){
02392                 space = adjust(7,compress(scp_bin[48+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02393             }
02394             space = adjust(6,compress(scp_bin[17],3,3) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
02395             space = adjust(6,compress(scp_bin[23],3,3),pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02396             if(pzf == 0){
02397                 for(int i = 0; i<2 ;i++){
02398                     space = adjust(8,compress(scp_bin[i],6,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
02399                 }
02400                 for(int i = 0; i<15 ;i++){
02401                     space = adjust(7,compress(scp_bin[i+2],5,2) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02402                 }
02403             }
02404             if(ezf == 0){
02405                 space = adjust(1,compress(scp_bin[18],7,2)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
02406                 space = adjust(8,compress(scp_bin[18],7,2) ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
02407                 for(int i=0; i<4; i++){
02408                     space = adjust(8,compress(scp_bin[19+i],6,2) ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
02409                 }
02410             }
02411         }// below threshold ends here
02412         if(at == 1){
02413             #if scpz
02414             gPC.printf("\n\rAbove threshold");
02415             #endif
02416             for(int i = 0; i<8 ;i++){
02417                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02418             }
02419             for(int i = 0; i<12 ;i++){
02420                 space = adjust(5,compress2(scp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02421             }
02422             for(int i = 0; i<4 ;i++){
02423                 space = adjust(1,compress(scp_bin[44+i],6,3)>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02424                 space = adjust(8,compress(scp_bin[44+i],6,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02425             }
02426             for(int i=0; i<4 ;i++){
02427                 space = adjust(4,compress(scp_bin[48+i],9,3)>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02428                 space = adjust(8,compress(scp_bin[48+i],9,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02429             }
02430             space = adjust(1,compress(scp_bin[17],6,3)>>8 ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
02431             space = adjust(8,compress(scp_bin[17],6,3),pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02432             space = adjust(1,compress(scp_bin[23],6,3)>>8,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02433             space = adjust(8,compress(scp_bin[23],6,3),pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02434 
02435             if(pzf == 0){
02436                 for(int i = 0; i<17 ; i++){
02437                     space = adjust(8, (compress(scp_bin[i],5,3)) ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
02438                 }
02439             }
02440             if(ezf == 0){
02441                 for(int i = 0; i<5 ; i++){
02442                     space = adjust(8, (compress(scp_bin[18+i],5,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02443                 }
02444             }
02445         }   //above thresholds ends
02446     }   //srp_mode == 0 ends
02447     if(srp_mode == 1){
02448         #if scpz
02449         gPC.printf("\n\rSCP - Scatter plot mode");
02450         #endif
02451         if(at == 0){
02452             #if scpz
02453             gPC.printf("\n\rBelow Threshold");
02454             #endif
02455             for(int i=0; i<48; i++){
02456                 space = adjust(6, ((compress(scp_bin[i],3,3))&0xff) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02457             }
02458             for(int i = 0 ; i < 4 ; i++){
02459                 space = adjust(7, (compress(scp_bin[48+i],4,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02460             }
02461         }
02462         if(at == 1){
02463             #if scpz
02464             gPC.printf("\n\rAbove threshold");
02465             #endif
02466             for(int i=0; i<48; i++){
02467                 space = adjust(7, (compress((scp_bin[i]/2),4,3))>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02468             }
02469             for(int i = 0 ; i < 4 ; i++){
02470                 space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02471                 space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02472             }
02473         }
02474     }// scp mode 1 end
02475     if( srp_mode == 0 && compression_option == 1 ){ //scp data conservation mode
02476         #if scpz
02477         gPC.printf("\n\rSCP - Calibrated data conservation mode");
02478         #endif
02479         space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02480         space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02481         if(at == 0){
02482             #if scpz
02483             gPC.printf("\n\rBelow threshold");
02484             #endif
02485             space = adjust(6, (compress(scp_bin[44],3,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02486             for(int i=0; i<4; i++){
02487                 space = adjust(7, (compress(scp_bin[48+i],4,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02488             }
02489             space = adjust(6, (compress(scp_bin[17],3,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02490             space = adjust(6, (compress(scp_bin[23],3,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02491             if(pzf==0){
02492                 space = adjust(4, (compress(proton_scp_sum,10,2))>>8 ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
02493                 space = adjust(8, ( compress(proton_scp_sum,10,2)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02494             }
02495             if(ezf==0){
02496                 space = adjust(4, (compress(electron_scp_sum,10,2))>>8 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
02497                 space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02498             }
02499         }else if(at == 1){
02500             #if scpz
02501             gPC.printf("\n\rAbove threshold");
02502             #endif
02503             space = adjust(1, (compress(scp_bin[44],6,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02504             space = adjust(8, (compress(scp_bin[44],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02505             for(int i=0; i<4; i++){
02506                 space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02507                 space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02508             }
02509             space = adjust(1, (compress(scp_bin[17],6,3)>>8) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02510             space = adjust(8, (compress(scp_bin[17],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02511             space = adjust(1, (compress(scp_bin[23],6,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02512             space = adjust(8, (compress(scp_bin[23],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02513             if(pzf==0){
02514                 space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02515                 space = adjust(8, (compress(proton_scp_sum,6,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02516             }
02517             if(ezf==0){
02518                 space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
02519                 space = adjust(8, (compress(electron_scp_sum,6,3)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02520             }
02521         }
02522     }
02523     if(compression_option == 2 )  //scp extreme data conservation mode
02524     {
02525         if(srp_mode==0)
02526         {
02527             #if scpz
02528             gPC.printf("\n\rSCP - calibrated extreme data conservation mode");
02529             #endif
02530             space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02531             space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02532             if(at==0){
02533                 #if scpz
02534                 gPC.printf("\n\rBelow threshold");
02535                 #endif
02536                 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02537                 if(pzf==0){
02538                     space = adjust(4, (compress(proton_scp_sum,10,2))>>8 ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
02539                     space = adjust(8, ( compress(proton_scp_sum,10,2)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02540                 }
02541                 if(ezf==0){
02542                     space = adjust(4, (compress(electron_scp_sum,10,2))>>8 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
02543                     space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02544                 }
02545             }else if(at==1){
02546                 #if scpz
02547                 gPC.printf("\n\rAbove threshold");
02548                 #endif
02549                 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02550                 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
02551                 if(pzf==0){
02552                     space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
02553                     space = adjust(8, (compress(proton_scp_sum,6,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02554                 }
02555                 if(ezf==0){
02556                     space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
02557                     space = adjust(8, (compress(electron_scp_sum,6,3)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
02558                 }
02559             }
02560         }
02561         else if(srp_mode==1)
02562         {
02563             #if scpz
02564             gPC.printf("\n\rSCP - Scatter plot, extreme data conservation mode");
02565             #endif
02566             pzf = 0;    ezf = 0;    //because proton energy bins are not available in scatter plot mode and hence in the packet we send dummy data(which is nothng but the maximum value which fits into that particular compression scheme)
02567             space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02568             space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02569             if(at==0){
02570                 #if scpz
02571                 gPC.printf("\n\rBelow threshold");
02572                 #endif
02573                 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
02574                 // below 4 lines - filling the proton ane electron energy bins with zeros(junk value) since they're not available in srp
02575                 space = adjust(4, (compress(0,10,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02576                 space = adjust(8, (compress(0,10,2)) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02577                 space = adjust(4, (compress(0,10,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
02578                 space = adjust(8, (compress(0,10,2)) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
02579             }
02580             else if(at==1){
02581                 #if scpz
02582                 gPC.printf("\n\rAbove threshold");
02583                 #endif
02584                 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
02585                 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
02586                 space = adjust(1, (compress(0,6,3))>>8 ,pointer,space);                 pointer += space>>4;    debug_cntr += space>>4;
02587                 space = adjust(8, (compress(0,6,3)) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
02588                 space = adjust(1, (compress(0,6,3))>>8 ,pointer,space);                 pointer += space>>4;    debug_cntr += space>>4;
02589                 space = adjust(8, (compress(0,6,3)) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
02590             }
02591         }
02592     }
02593     /*---------------------------------------------forming a science coarse packet ends here----------------------------------------------*/
02594 
02595     id = 0;
02596     if(srp_mode == 0 && compression_option == 0){
02597         if(at == 0){
02598             length = 228;
02599             if(pzf == 0)
02600                 length += 121;
02601             if(ezf == 0)
02602                 length +=41;
02603         }else if(at == 1){
02604             length = 266;
02605             if(pzf == 0)
02606                 length += 136;
02607             if(ezf == 0)
02608                 length += 40;
02609             #if test_science
02610             dma_scp_at_count++;
02611             #endif
02612         }
02613     }else if(srp_mode == 0 && compression_option == 1){                         //data conservation mode
02614         if(at == 0){
02615             length = 94;
02616             if(pzf == 0)
02617                 length += 12;
02618             if(ezf == 0)
02619                 length += 12;
02620         }else if(at == 1){
02621             length = 123;
02622             if(pzf == 0)
02623                 length += 9;
02624             if(ezf == 0)
02625                 length += 9;
02626             #if test_science
02627             dma_scp_at_count++;
02628             #endif
02629         }
02630     }
02631     else if( srp_mode == 1){
02632         if(at == 0)
02633             length = 368;
02634         else if(at == 1){
02635             length = 432;
02636             #if test_science
02637             dma_scp_at_count++;
02638             #endif
02639         }        
02640     }
02641     else if(compression_option == 2 && srp_mode==0)                                //in extreme data conservation mode, length is same for both srp_modes
02642     {                         
02643         if(at == 0){
02644             length = 31;
02645             if(pzf == 0)
02646                 length += 12;
02647             if(ezf == 0)
02648                 length += 12;
02649         }else if(at == 1){
02650             length = 36;
02651             if(pzf == 0)
02652                 length += 9;
02653             if(ezf == 0)
02654                 length += 9;
02655             #if test_science
02656             dma_scp_at_count++;
02657             #endif
02658         }
02659     }
02660     else if(compression_option == 2 && srp_mode==1){
02661         if(at==0)
02662             length = 56;
02663         else if(at==1){
02664             length = 56;
02665             #if test_science
02666             dma_scp_at_count++;
02667             #endif
02668         }
02669     }
02670     length = (length%8==0)?(length/8):(length/8)+1;
02671     #if scpz
02672     gPC.printf("\n\rLength of SCP packet in bytes = %d",length);
02673     #endif
02674     /*if(position_tm_frame[id]>129){
02675         #if scpz
02676         gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame");
02677         gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]);
02678         #endif
02679         while(position_tm_frame[id]<132){
02680             frames[id][position_tm_frame[id]] = 0;
02681             position_tm_frame[id]++;
02682         }
02683     }*/
02684     if(srp_mode==0 && compression_option!=2){
02685         if(position_tm_frame[id]>126){
02686             #if scpz
02687             gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame");
02688             gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]);
02689             #endif
02690             while(position_tm_frame[id]<132){
02691                 frames[id][position_tm_frame[id]] = 0;
02692                 position_tm_frame[id]++;
02693             }
02694         }
02695     }
02696     else if(((srp_mode==0)||(srp_mode==1)) && compression_option==2){
02697         if(position_tm_frame[id]>129){
02698             #if scpz
02699             gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame");
02700             gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]);
02701             #endif
02702             while(position_tm_frame[id]<132){
02703                 frames[id][position_tm_frame[id]] = 0;
02704                 position_tm_frame[id]++;
02705             }
02706         }
02707     }
02708     else if(srp_mode==1){
02709         if(position_tm_frame[id]>129){
02710             #if scpz
02711             gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame");
02712             gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]);
02713             #endif
02714             while(position_tm_frame[id]<132){
02715                 frames[id][position_tm_frame[id]] = 0;
02716                 position_tm_frame[id]++;
02717             }
02718         }
02719     }
02720     if(position_tm_frame[id]==position_tm_starting[id]){
02721         frames[id][4] = position_tm_starting[id];
02722     }
02723     for(int j= 0; j < length ;)
02724     {        
02725         if(position_tm_frame[id]==frames[id][4]){
02726             #if scpz
02727             gPC.printf("\n\rInserting Time into SCP_TM");
02728             #endif
02729             frames[id][5] = (uint8_t)((sci_time>>29)&0x3f);
02730             frames[id][6] = (uint8_t)((sci_time>>21)&0xff);
02731             frames[id][7] = (uint8_t)((sci_time>>13)&0x3f);
02732         }
02733         if(position_tm_frame[id]<132){
02734             frames[id][position_tm_frame[id]] = scp[j];
02735             j++;
02736             position_tm_frame[id]++;
02737         }
02738         if(position_tm_frame[id] == 132)                                                //space full in the frame bro
02739         {
02740             #if scpz
02741             gPC.printf("\n\rSCP_TM frame full. Length of current SCP packet = %d",j);
02742             #endif
02743             pointer = frames[id];
02744             space = adjust(1,0,pointer,8);
02745             space = adjust(4,1,pointer,space);
02746             FSC_science = FSC_CURRENT[1];
02747             //gPC.printf("1 = 0x%X",FSC_science);
02748             
02749             #if debug_fsc
02750             frames[id][1] = (test_fsc[1]>>16)&0xff;
02751             frames[id][2] = (test_fsc[1]>>8)&0xff;
02752             frames[id][3] = (test_fsc[1])&0xff;
02753             test_fsc[1]++;
02754             #endif
02755             #if !debug_fsc
02756             frames[id][1] = (FSC_science>>16)&0xff;
02757             frames[id][2] = (FSC_science>>8)&0xff;
02758             frames[id][3] = (FSC_science)&0xff;
02759             #endif
02760 
02761             temp_crc = crc16_gen(frames[id],132);
02762             frames[id][132] = temp_crc>>8;
02763             frames[id][133] = temp_crc & 0xff;
02764             #if scpz
02765             gPC.printf("\n\rFirst head pointer: 0x%02X",frames[id][4]);
02766             gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]);
02767             gPC.printf("\n\rPrinting SCP_TM\n\r{");
02768             for(uint8_t z=0;z<134;z++){
02769                 gPC.printf("%02X",frames[id][z]);
02770             }
02771             gPC.printf("}\n\r");
02772             #endif
02773             #if tabulation
02774             tabulate_TM(&frames[id][0]);
02775             #endif
02776             exor(frames[id]);
02777             convolution(frames[id]);
02778             interleave(TM_convoluted_data,TM_interleave_data);
02779             interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
02780             gPC.printf("endSCI_SD13");
02781            sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
02782             gPC.printf("endSCI_SD14");
02783             if(sd_stat)
02784             {
02785                 gPC.puts("sd write fail");
02786             }
02787             position_tm_frame[id] = position_tm_starting[id];
02788             if(j!=0)
02789                 frames[id][4] = (length - j)+position_tm_starting[id];
02790             else if(j==0)
02791                 frames[id][4] = position_tm_starting[id];
02792         }
02793     }
02794     #if scpz_last
02795     //gPC.printf("\n\rPrinting the last SCP_TM frame");
02796     //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]);
02797     while(position_tm_frame[id]<132){
02798         frames[id][position_tm_frame[id]] = 0;
02799         position_tm_frame[id]++;
02800     }
02801     if(position_tm_frame[id] == 132)                                                //space full in the frame bro
02802     {
02803         //gPC.printf("\n\rSCP_TM frame full. Length of current SCP packet = %d",j);
02804         pointer = frames[id];
02805         space = adjust(1,0,pointer,8);
02806         space = adjust(4,1,pointer,space);
02807         FSC_science = FSC_CURRENT[1];
02808         //gPC.printf("1 = 0x%X",FSC_science);
02809         
02810         #if debug_fsc
02811         frames[id][1] = (test_fsc[1]>>16)&0xff;
02812         frames[id][2] = (test_fsc[1]>>8)&0xff;
02813         frames[id][3] = (test_fsc[1])&0xff;
02814         test_fsc[1]++;
02815         #endif
02816         #if !debug_fsc
02817         frames[id][1] = (FSC_science>>16)&0xff;
02818         frames[id][2] = (FSC_science>>8)&0xff;
02819         frames[id][3] = (FSC_science)&0xff;
02820         #endif
02821 
02822         temp_crc = crc16_gen(frames[id],132);
02823         frames[id][132] = temp_crc>>8;
02824         frames[id][133] = temp_crc & 0xff;
02825         //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]);
02826         //gPC.printf("\n\rPrinting SCP_TM\n\r{");
02827         gPC.printf("\n\r{");
02828         for(uint8_t z=0;z<134;z++){
02829             gPC.printf("%02X",frames[id][z]);
02830         }
02831         gPC.printf("}\n\r");
02832         position_tm_frame[id] = position_tm_starting[id];
02833     }
02834     #endif   
02835     //gPC.printf("fsc = %u, %u,%u\n\r", FSC_CURRENT[1],FSC_CURRENT[2],FSC_CURRENT[3]);
02836     #if test_science
02837     dma_packets_info();
02838     #endif
02839     gPC.printf("End of SRP function\n\r"); 
02840     //SCI_LED1 = !SCI_LED1;
02841  //   SCI_LED1 = 0;
02842 }
02843 /*void test_sci_main(){
02844     gPAYLOAD_BUFFER[2] &= (~0x01);  //calibrated mode data generation
02845     //gPAYLOAD_BUFFER[2] |= (0x01);   //scattered plot mode data generation
02846     srp(gPAYLOAD_BUFFER);
02847 }*/
02848 /*void test_tabulate(){
02849     uint8_t tmid=2,temp_index,temp=13,space;
02850     pointer = &test_tm[0][0];
02851     space = adjust(1, 0,pointer,8);          pointer += space>>4;
02852     space = adjust(4, 2,pointer,space);      pointer += space>>4;
02853     space = adjust(3, 0,pointer,space);      pointer += space>>4;
02854 
02855     space = adjust(8, 0,pointer,space);      pointer += space>>4;   //fsc
02856     space = adjust(8, 0,pointer,space);      pointer += space>>4;
02857     space = adjust(8, 1,pointer,space);      pointer += space>>4;
02858 
02859     space = adjust(8, 5,pointer,space);      pointer += space>>4;   //first head pointer
02860 
02861     space = adjust(2, 2,pointer,space);      pointer += space>>4;   //time
02862     space = adjust(4, 12,pointer,space);      pointer += space>>4;
02863     space = adjust(5, 8,pointer,space);      pointer += space>>4;
02864     space = adjust(5, 10,pointer,space);      pointer += space>>4;
02865     space = adjust(6, 25,pointer,space);      pointer += space>>4;
02866     space = adjust(6, 25,pointer,space);     pointer += space>>4;   
02867     space = adjust(7, 69,pointer,space);     pointer += space>>4;
02868 
02869     space = adjust(4, 15,pointer,space);     pointer += space>>4;  //attitude
02870 
02871     space = adjust(6, 60,pointer,space);      pointer += space>>4;
02872     space = adjust(1, 1,pointer,space);      pointer += space>>4;   //science data mode    
02873 
02874     for(uint8_t i = 0 ; i < 48 ; i++){
02875         space = adjust(2, ((compress(16320,8,2))>>8) ,pointer,space); pointer += space>>4;
02876         space = adjust(8, ((compress(16320,8,2))&0xff) ,pointer,space); pointer += space>>4;
02877     }
02878     for(uint8_t i = 0 ; i < 4 ; i++){
02879         space = adjust(1, ((compress(1032192,6,3))>>8) ,pointer,space); pointer += space>>4;
02880         space = adjust(8, ((compress(1032192,6,3))&0xff) ,pointer,space); pointer += space>>4;
02881     }
02882     space = adjust(6, 0,pointer,space);      pointer += space>>4;   //spare
02883 
02884     //next packet
02885     space = adjust(2, 2,pointer,space);      pointer += space>>4;   //time
02886     space = adjust(4, 12,pointer,space);      pointer += space>>4;
02887     space = adjust(5, 8,pointer,space);      pointer += space>>4;
02888     space = adjust(5, 10,pointer,space);      pointer += space>>4;
02889     space = adjust(6, 25,pointer,space);      pointer += space>>4;
02890     space = adjust(6, 25,pointer,space);     pointer += space>>4;   
02891     space = adjust(7, 69,pointer,space);     pointer += space>>4;
02892 
02893     space = adjust(4, 15,pointer,space);     pointer += space>>4;  //attitude
02894 
02895     space = adjust(6, 61,pointer,space);      pointer += space>>4;
02896     space = adjust(1, 0,pointer,space);      pointer += space>>4;   //science data mode
02897     space = adjust(1, 0,pointer,space);      pointer += space>>4;   //pzf
02898     space = adjust(1, 0,pointer,space);      pointer += space>>4;   //ezf
02899 
02900     for(uint8_t i = 0 ; i < 8 ; i++){
02901         space = adjust(1, ((compress(8128,7,2))>>8) ,pointer,space); pointer += space>>4;
02902         space = adjust(8, ((compress(8128,7,2))&0xff) ,pointer,space); pointer += space>>4;
02903     }
02904     for(uint8_t i = 0 ; i < 12 ; i++){
02905         space = adjust(6, ((compress(114688,3,3))) ,pointer,space); pointer += space>>4;
02906     }
02907     for(uint8_t i = 0 ; i < 4 ; i++){
02908         space = adjust(2, ((compress(16320,8,2))>>8) ,pointer,space); pointer += space>>4;
02909         space = adjust(8, ((compress(16320,8,2))&0xff) ,pointer,space); pointer += space>>4;
02910     }
02911     for(uint8_t i = 0 ; i < 4 ; i++){
02912         space = adjust(1, ((compress(1032192,6,3))>>8) ,pointer,space); pointer += space>>4;
02913         space = adjust(8, ((compress(1032192,6,3))&0xff) ,pointer,space); pointer += space>>4;
02914     }
02915     for(uint8_t i = 0 ; i < 18 ; i++){
02916         space = adjust(2, ((compress(16320,8,2))>>8) ,pointer,space); pointer += space>>4;
02917         space = adjust(8, ((compress(16320,8,2))&0xff) ,pointer,space); pointer += space>>4;
02918     }    
02919     tabulate_TM(&test_tm[0][0]);
02920 
02921     space=8;    pointer = &test_tm[1][0];
02922     space = adjust(1, 0,pointer,8);          pointer += space>>4;
02923     space = adjust(4, 2,pointer,space);      pointer += space>>4;
02924     space = adjust(3, 0,pointer,space);      pointer += space>>4;
02925 
02926     space = adjust(8, 0,pointer,space);      pointer += space>>4;   //fsc
02927     space = adjust(8, 0,pointer,space);      pointer += space>>4;
02928     space = adjust(8, 2,pointer,space);      pointer += space>>4;
02929 
02930     space = adjust(8, 13,pointer,space);      pointer += space>>4;   //first head pointer
02931 
02932     for(uint8_t i = 0 ; i < 6 ; i++){
02933         space = adjust(2, ((compress(16320,8,2))>>8) ,pointer,space); pointer += space>>4;
02934         space = adjust(8, ((compress(16320,8,2))&0xff) ,pointer,space); pointer += space>>4;
02935     }
02936     space = adjust(4, 0,pointer,space);      pointer += space>>4;   //spare    
02937     tabulate_TM(&test_tm[1][0]);
02938 }*/