Team Fox / Mbed 2 deprecated CDMS_QM_03MAR2017_Flash_with_obsrs

Dependencies:   mbed mbed-rtos SimpleDMA FreescaleIAP eeprom

Fork of CDMS_CODE_FM_28JAN2017 by samp Srinivasan

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