pl ack in tmtc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by shubham c

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Compression.h Source File

Compression.h

00001 unsigned int read_2byte(unsigned char* ptr){
00002     unsigned int output = (unsigned int) *(ptr+1);
00003     output += ( (unsigned int)(*ptr) ) << 8;
00004     return output;
00005 }
00006     
00007 unsigned int read_4byte(unsigned char* ptr){
00008     unsigned int output = (unsigned int) *(ptr+3);
00009     output += (unsigned int)*(ptr+2)<<8;
00010     output += (unsigned int)*(ptr+1)<<16;
00011     output += (unsigned int)*(ptr)<<24;
00012     return output;
00013 }
00014 
00015 int adjust(int size, unsigned int data, unsigned char* ptr , int space){
00016     space = space&0x0f;
00017     data = data&((1<<size)-1);
00018     if(space >= size){
00019         *ptr += data<<(space-size);
00020         if(space - size == 0){
00021             return 0x18;
00022         }else{
00023             return space-size;
00024         }
00025     }else{
00026         ptr[0] += data>>(size-space);
00027         ptr[1] = (data<<(8-(size-space)))&0xff ;
00028         return 0x10 + 8-(size - space);
00029     }
00030 }
00031 
00032 
00033 int compress (int data, int x, int y){  //to be compressed with scheme (msb x)*4^y ;
00034     for(int i = 0 ; i < 1<<y ; i++){
00035         if(data <= ( (1<<x)-1) * (1<<(2*i)) ){
00036             return ( ((data>>i*2)<<y) + i);
00037         }
00038     }
00039     if ( data > ( (1<<x)-1) * (1<<(2*((1<<y)-1))) ){
00040 //      cout <<"compression exception"<<endl;
00041         return 0;
00042     }
00043 }
00044 
00045 
00046 
00047 //variable declerations
00048 unsigned char srp_mode , at , pzf , ezf ,sfp[65] ,scp[55];; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
00049 unsigned int sfp_bin[52] , scp_bin[52];         //storing the bin values.
00050 unsigned int sfp_threshold_m0[52] = {8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128,4032,4032,124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,252,252,252,252,252}; 
00051 const unsigned int scp_threshold_m0[52] = {245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760,114688,114688,4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,4032,4032,4032,4032};
00052 unsigned int scp_threshold_m0_1[9]={114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for mode 0..ie callibrated mode
00053 const unsigned int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
00054 unsigned char frames[3][134] = {0};
00055 unsigned char position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
00056 unsigned char id;   //sf = 0,sc-at = 1,sc-bt = 2;
00057 unsigned char TM_interleave_data[512] , TM_convoluted_data[270] = {0};
00058 int proton_scp_sum,electron_scp_sum,length, temp_crc,attitude = 0,FSC_science = 1,debug_cntr = 0, size,space;   
00059 unsigned char *ptr ,* pointer;
00060 long long int sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
00061 int test_cntr = 0;
00062 
00063     Convolution ConvObj;
00064     void convolution (unsigned char * ptr){
00065         ConvObj.convolutionEncode(ptr, TM_convoluted_data);
00066         ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
00067     }
00068 
00069 //give the pointer of 6 second data to this function
00070 void srp(unsigned char * head){
00071     //sci_time = FCTN_CDMS_RD_RTC();
00072     sci_time = 0;
00073     #if DEBUG
00074     gPC.puts("enter srp");
00075     #endif
00076     
00077     for (int i = 0; i < 52 ; i++){
00078         scp_bin[i] = 0;
00079     }
00080     ptr = head + 3;
00081     srp_mode = head[2]&0x1;
00082     at = 0;     //above threshold is false
00083     for(int counter = 0 ; counter < 60 ; counter++){
00084         pzf = 1;    ezf = 1;
00085         if(srp_mode == 0){      //calibrated mode
00086             for(int i=0; i<48 ; i++){
00087                 sfp_bin[i] = read_2byte(ptr + i*2);
00088                 scp_bin[i] += sfp_bin[i];
00089                 if(sfp_bin[i]>sfp_threshold_m0[i]){
00090                     at = 1;
00091                 }
00092                 if(i<17){
00093                     if(sfp_bin[i] > 0)
00094                         pzf = 0;
00095                 }
00096                 else if (i>17 && i < 23){
00097                     if(sfp_bin[i]>0)
00098                         ezf = 0;
00099                 }
00100             }
00101             for(int i=0; i<4; i++){
00102                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
00103                 scp_bin[i+48] += sfp_bin[i+48];
00104                 if(sfp_bin[i+48]>sfp_threshold_m0[i+48])
00105                     at = 1;
00106             }
00107         }
00108         else if(srp_mode == 1){ //scattered mode
00109             for(int i = 0; i <32; i++){
00110                 sfp_bin[i] = read_2byte(ptr+2*i);
00111                 scp_bin[i] += sfp_bin[i];
00112                 if(sfp_bin[i] > sfp_threshold_m1[0])
00113                     at = 1;
00114             }
00115             for(int i = 0; i < 4 ; i++){
00116                 sfp_bin[i+32] = read_4byte( (ptr+64) + 4*i );
00117                 scp_bin[i+32] += sfp_bin[i+32];
00118                 if(sfp_bin[i+32] > sfp_threshold_m1[1])
00119                     at = 1;
00120             }
00121         }
00122         ptr = ptr + 112;
00123         for(int i = 0; i<65; i++)
00124             sfp[i] = 0;
00125         if(srp_mode == 0){      //calibrated mode
00126             if(at == 0){
00127                 pointer = sfp; debug_cntr = 0;
00128                 space = adjust(4, attitude,pointer,8);  pointer += space>>4;    debug_cntr += space>>4;
00129                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
00130                 space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
00131                 space = adjust(1, pzf,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
00132                 space = adjust(1, ezf,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
00133                 for(int i = 0 ; i < 8 ; i++){
00134                     space = adjust(1, ((compress(sfp_bin[24+i],7,2))&0x100)>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00135                     space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00136                 }
00137                 for(int i = 0 ; i <12 ; i++){
00138                     space = adjust(6, (compress(sfp_bin[32+i],4,2)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00139                 }
00140                 for(int i = 0; i < 4; i++){
00141                     space = adjust(8, (compress(sfp_bin[44+i],6,2)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00142                 }
00143                 for(int i = 0 ; i < 4 ; i++){
00144                     space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00145                     space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00146                 }
00147                 space = adjust(8, ((compress(sfp_bin[17],6,2))&0xff) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00148                 space = adjust(8, ((compress(sfp_bin[23],6,2))&0xff) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00149                 if(pzf == 0){
00150                     for(int i = 0; i<2 ; i++){
00151                         space = adjust(6, ((compress(sfp_bin[i],5,1))&0xff) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00152                     }
00153                     for(int i = 0; i<15 ; i++){
00154                         space = adjust(5, ((compress(sfp_bin[i+2],4,1))&0xff) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00155                     }
00156                 }
00157                 if(ezf == 0){
00158                     for(int i = 0; i <5 ;i++){
00159                         space = adjust(7, ((compress(sfp_bin[18+i],6,1))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00160                     }
00161                 }
00162             }//below thershold ends here.
00163         
00164         //gPC.puts("below_thresh\r\n");
00165         
00166             if(at == 1){
00167                 pointer = sfp + 6; debug_cntr = 6;space = 8;
00168                 sfp[0] = (sci_time>>27)&0xff;   sfp[1] = (sci_time>>19)&0xff;   sfp[2] = (sci_time>>11)&0xff;   sfp[3] = (sci_time>>3)&0xff;
00169                 sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5);    
00170                 sfp[5] = ((counter&0x0f)<<3) + (srp_mode<<2);   
00171                 sfp[5] += (pzf<<1) + ezf ;
00172                 for(int i = 0 ; i < 8 ; i++){
00173                     space = adjust(1, (compress(sfp_bin[24+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00174                     space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00175                 }
00176                 for(int i = 0 ; i <12 ; i++){
00177                     space = adjust(6, ((compress(sfp_bin[32+i],3,3))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00178                 }
00179                 for(int i = 0 ; i < 4 ; i++){
00180                     space = adjust(2, (compress(sfp_bin[44+i],8,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00181                     space = adjust(8, compress(sfp_bin[44+i],8,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00182                 }
00183                 for(int i = 0 ; i < 4 ; i++){
00184                     space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00185                     space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00186                 }
00187                 space = adjust(2, (compress(sfp_bin[17],8,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00188                 space = adjust(8, compress(sfp_bin[17],8,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00189                 space = adjust(2, (compress(sfp_bin[23],8,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00190                 space = adjust(8, compress(sfp_bin[23],8,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00191                 
00192                 if(pzf == 0){
00193 //                  cout<<"proton bins ";
00194                     for(int i = 0; i<17 ; i++){
00195                         space = adjust(2, ((compress(sfp_bin[i],8,2))>>8) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00196                         space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00197 //                      printf("%02X ",compress(sfp_bin[i],8,2));
00198                     }
00199                 }
00200                 if(ezf == 0){
00201 //                  cout<<"electron bins ";
00202                     for(int i = 0; i<5 ; i++){
00203                         space = adjust(2, ((compress(sfp_bin[18+i],8,2))>>8) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00204                         space = adjust(8, compress(sfp_bin[18+i],8,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00205 //                      printf("%02X ",compress(sfp_bin[i],8,2));
00206                     }
00207                 }
00208 //              cout<<debug_cntr<<" "<<(space&0xf)<<endl;
00209 //              cout<<"packet ";
00210 //              for(int i = 0; i< 64; i++){
00211 //                  cout<<bitset<8>(sfp[i]);
00212 //              }
00213 //              cout<<"ends"<<endl;
00214             }//above threshold ends here.
00215         }else if(srp_mode == 1){    //scattered mode
00216             if(at == 0){
00217                 pointer = sfp; debug_cntr = 0;  space = 8;
00218                 space = adjust(4, attitude,pointer,8);  pointer += space>>4;    debug_cntr += space>>4;
00219                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
00220                 space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
00221                 for(int i=0; i<32; i++){
00222                     space = adjust(7, compress(sfp_bin[i],6,1) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00223                 }
00224                 for(int i = 0 ; i < 4 ; i++){
00225                     space = adjust(1, (compress(sfp_bin[32+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00226                     space = adjust(8, compress(sfp_bin[32+i],7,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00227                 }
00228             }
00229             if(at == 1){
00230                 pointer = sfp; debug_cntr = 0;  space = 8;
00231                 space = adjust(3, sci_time>>32 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
00232                 space = adjust(8, sci_time>>24 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
00233                 space = adjust(8, sci_time>>16 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
00234                 space = adjust(8, sci_time>>8 ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
00235                 space = adjust(8, sci_time ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
00236                 space = adjust(4, attitude,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
00237                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
00238                 space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
00239                 for(int i=0; i<32; i++){
00240                     space = adjust(2, (compress(sfp_bin[i],8,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00241                     space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00242                 }
00243                 for(int i = 0 ; i < 4 ; i++){
00244                     space = adjust(1, (compress(sfp_bin[32+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00245                     space = adjust(8, compress(sfp_bin[32+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00246                 }
00247             }
00248         }   
00249         
00250         //gPC.puts("sci fine pack\r\n");
00251         //science fine packet is complete here.
00252         //lets try to make frame now******************************************************************************************************************************
00253         if(srp_mode == 0){
00254             if(at == 0){
00255                 id = 1;     length = 241;
00256                 if(pzf == 0)
00257                     length += 87;
00258                 if(ezf == 0)
00259                     length += 35;
00260             }else{
00261                 id = 2;     length = 288;
00262                 if(pzf == 0)
00263                     length += 170;
00264                 if(ezf == 0)
00265                     length += 50;
00266             }
00267         }
00268         else if(srp_mode == 1){
00269             if(at == 0){
00270                 id = 1;     length = 272;
00271             }else{
00272                 id = 2;     length = 408;
00273             }
00274         }
00275         length = (length%8==0)?(length/8):(length/8)+1;
00276         for(int j=0 ; j<length ;j++){
00277 //          printf("%02X",sfp[j]);
00278             frames[id][position_tm_frame[id]] = sfp[j];
00279             position_tm_frame[id]++;
00280             if(position_tm_frame[id] == 132){   //space full in the frame bro
00281                 pointer = frames[id];
00282                 if(id == 1){    //below thereshold
00283                     space = adjust(1,0,pointer,8);
00284                     space = adjust(4,3,pointer,space);
00285                     FSC_science = FCTN_SD_MNGR(3);
00286                     //gPC.printf("SID = 3, FSC = %02X\r\n", FSC_science);
00287                     frames[id][1] = (FSC_science>>24)&0xff;
00288                     frames[id][2] = (FSC_science>>16)&0xff;
00289                     frames[id][3] = (FSC_science>>8)&0xff;
00290                     frames[id][4] = FSC_science&0xff;
00291                     frames[id][6] = (sci_time>>32)&0xff;
00292                     frames[id][7] = (sci_time>>24)&0xff;
00293                     frames[id][8] = (sci_time>>16)&0xff;
00294                     frames[id][9] = (sci_time>>8)&0xff;
00295                     frames[id][10] = (sci_time)&0xff;
00296                 }else if(id == 2){
00297                     space = adjust(1,0,pointer,8);
00298                     space = adjust(4,2,pointer,space);
00299 //                    FSC_science = FCTN_SD_MNGR(2);
00300                     
00301                     //gPC.printf("SID = 2, FSC = %02X\r\n", FSC_science);
00302                     frames[id][1] = (FSC_science>>16)&0xff;
00303                     frames[id][2] = (FSC_science>>8)&0xff;
00304                     frames[id][3] = FSC_science&0xff;
00305                 }
00306                 temp_crc = crc16_gen(frames[id],132);
00307                 frames[id][132] = temp_crc>>8;
00308                 frames[id][133] = temp_crc & 0xff;
00309                 if(test_cntr++ == 0){
00310                     #if DEBUG
00311                     gPC.puts("frame start\r\n");
00312                     #endif
00313                     for(int i = 0; i<134; i++){
00314                         #if DEBUG
00315                         gPC.printf("%02X",frames[id][i]);
00316                         #endif
00317                     }    
00318                     #if DEBUG
00319                     gPC.puts("frame ends\r\n");
00320                     #endif
00321                 }
00322                 exor(frames[id]);
00323                 convolution(frames[id]);
00324                 interleave(TM_convoluted_data,TM_interleave_data);
00325                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
00326                 if(id == 1)
00327                     SD_WRITE(TM_interleave_data,FSC_science,3);
00328                 else if (id == 2)
00329                     SD_WRITE(TM_interleave_data,FSC_science,2);
00330                 FSC_science++;      //remove this after testing
00331                 position_tm_frame[id] = position_tm_starting[id];
00332                 frames[id][6-id] = (length-1) - j;      // first head pointer.
00333             }
00334         }
00335         
00336     }//for loop bracket which runs 60 times
00337     
00338     //---------------below is scp --------------**************************************************************************************************
00339     at = 0;     pzf = 1;    ezf = 1;
00340     srp_mode = head[2]&0x1;
00341     unsigned char compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
00342     if(srp_mode ==0){
00343         if(compression_option == 0){
00344             for(int i=0; i<52 ;i++){
00345                 if(scp_bin[i] > scp_threshold_m0[i]){
00346                     at = 1;
00347                     break;
00348                 }
00349             }
00350         }
00351         else if(compression_option == 1){
00352             if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
00353             for(int i=0; i<4 ;i++){
00354                 if(scp_bin[48+i] > scp_threshold_m0_1[i+1]){
00355                     at = 1;
00356                     break;
00357                 }
00358             }
00359             if(scp_bin[17] > scp_threshold_m0_1[5]) at=1;
00360             if(scp_bin[23] > scp_threshold_m0_1[6]) at=1;
00361             
00362             proton_scp_sum = 0; electron_scp_sum = 0;
00363             for(int i=0;i<17;i++){
00364                 proton_scp_sum += scp_bin[i];
00365             }
00366             for(int i=0; i<5; i++){
00367                 electron_scp_sum += scp_bin[18+i];
00368             }
00369             if(proton_scp_sum > scp_threshold_m0_1[7]) at=1;
00370             if(electron_scp_sum > scp_threshold_m0_1[8]) at=1;
00371         }
00372     }else if(srp_mode ==1){
00373         for(int i=0; i<32; i++){
00374             if(scp_bin[i] > scp_threshold_m1[0]){
00375                 at = 1;
00376                 break;
00377             }
00378         }
00379         for(int i=32; i<36; i++){
00380             if(scp_bin[i] > scp_threshold_m1[1]){
00381                 at = 1;
00382                 break;
00383             }
00384         }
00385     }
00386     
00387     for(int i=0; i<17 ;i++){
00388         if(scp_bin[i]>0)
00389             pzf = 0;
00390     }
00391     for(int i=18; i<23 ;i++){
00392         if(scp_bin[i]>0)
00393             ezf = 0;
00394     }
00395     #if DEBUG
00396     gPC.puts("scp\r\n");
00397     #endif
00398     
00399     pointer = scp;      debug_cntr = 0;     space = 8;
00400     unsigned char packet_pp = 1;    //where to get this packet present bit----------------------------------------------------------------------------------
00401     int sfp_at_counter; //----------------------------------------------------------------------------------------------------------------------------------
00402     space = adjust(1, packet_pp,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00403     space = adjust(5, (sci_time>>8)&0x1f,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00404     space = adjust(8, (sci_time)&0xff,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00405     space = adjust(4, (attitude)&0xf,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00406     space = adjust(2, compression_option,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00407     space = adjust(1,srp_mode,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00408     space = adjust(1,srp_mode,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00409     space = adjust(8, sfp_at_counter>>16,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00410     space = adjust(8, sfp_at_counter>>8,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00411     space = adjust(8, sfp_at_counter,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00412     if(srp_mode == 0 && compression_option == 0){       //normal callibrated mode
00413         space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00414         space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00415         if(at == 0 ){
00416             for(int i = 0; i<8 ;i++){
00417                 space = adjust(7,compress(sfp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00418             }
00419             for(int i = 0; i<12 ;i++){
00420                 space = adjust(5,compress(sfp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00421             }
00422             for(int i = 0; i<4 ;i++){
00423                 space = adjust(6,compress(scp_bin[44+i],3,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00424             }
00425             for(int i = 0; i<4 ;i++){
00426                 space = adjust(7,compress(scp_bin[48+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00427             }
00428             space = adjust(6,compress(scp_bin[17],3,3) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00429             space = adjust(6,compress(scp_bin[23],3,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00430             if(pzf == 0){
00431                 for(int i = 0; i<2 ;i++){
00432                     space = adjust(8,compress(scp_bin[i],6,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00433                 }
00434                 for(int i = 0; i<15 ;i++){
00435                     space = adjust(7,compress(scp_bin[i+2],5,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00436                 }
00437             }
00438             if(ezf == 0){
00439                 space = adjust(1,compress(scp_bin[18],7,2)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00440                 space = adjust(8,compress(scp_bin[18],7,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00441                 for(int i=0; i<4; i++){
00442                     space = adjust(8,compress(scp_bin[19+i],6,2) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00443                 }
00444             }
00445         }// below threshold ends here
00446         if(at == 1){
00447             for(int i = 0; i<8 ;i++){
00448                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00449             }
00450             for(int i = 0; i<12 ;i++){
00451                 space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00452             }
00453             for(int i = 0; i<4 ;i++){
00454                 space = adjust(1,compress(scp_bin[44+i],6,3)>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00455                 space = adjust(8,compress(scp_bin[44+i],6,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00456             }
00457             for(int i=0; i<4 ;i++){
00458                 space = adjust(2,compress(scp_bin[48+i],6,4)>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00459                 space = adjust(8,compress(scp_bin[48+i],6,4) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00460             }
00461             space = adjust(1,compress(scp_bin[17],6,3)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00462             space = adjust(8,compress(scp_bin[17],6,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00463             space = adjust(1,compress(scp_bin[23],6,3)>>8,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00464             space = adjust(8,compress(scp_bin[23],6,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00465             if(pzf == 0){
00466                 for(int i = 0; i<17 ; i++){
00467                     space = adjust(8, (compress(scp_bin[i],5,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00468                 }
00469             }
00470             if(ezf == 0){
00471                 for(int i = 0; i<5 ; i++){
00472                     space = adjust(8, (compress(scp_bin[18+i],5,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00473                 }
00474             }
00475         }   //above thresholds ends
00476     }   //srp_mode == 0 ends
00477     if(srp_mode == 1){
00478         if(at == 0){
00479             for(int i=0; i<32; i++){
00480                 space = adjust(6, ((compress(scp_bin[i],4,2))&0xff) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00481             }
00482             for(int i = 0 ; i < 4 ; i++){
00483                 space = adjust(7, (compress(scp_bin[32+i],4,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00484             }
00485         }
00486         if(at == 1){
00487             for(int i=0; i<32; i++){
00488                 space = adjust(6, (compress(scp_bin[i],3,3))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00489             }
00490             for(int i = 0 ; i < 4 ; i++){
00491                 space = adjust(4, (compress(scp_bin[32+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00492                 space = adjust(8, (compress(scp_bin[32+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00493             }
00494         }
00495     }// scp mode 1 end
00496     if( srp_mode == 0 && compression_option == 1 ){ //scp data conservation mode
00497         space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00498         space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00499         if(at == 0){
00500             if(at == 0){
00501                 space = adjust(6, (compress(scp_bin[44],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00502                 for(int i=0; i<4; i++){
00503                     space = adjust(7, (compress(scp_bin[48+i],4,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00504                 }
00505                 space = adjust(6, (compress(scp_bin[17],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00506                 space = adjust(6, (compress(scp_bin[23],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00507                 if(pzf==0){
00508                     space = adjust(2, (compress(proton_scp_sum,10,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00509                     space = adjust(8, (compress(proton_scp_sum,10,2)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00510                 }
00511                 if(ezf==0){
00512                     space = adjust(2, (compress(electron_scp_sum,10,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00513                     space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
00514                 }
00515             }   // scp data conservaton mode 
00516         }else if(at == 1){
00517             space = adjust(1, (compress(scp_bin[44],6,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00518             space = adjust(8, (compress(scp_bin[44],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00519             for(int i=0; i<4; i++){
00520                 space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00521                 space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00522             }
00523             space = adjust(1, (compress(scp_bin[17],6,3)>>8) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00524             space = adjust(8, (compress(scp_bin[17],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00525             space = adjust(1, (compress(scp_bin[23],6,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00526             space = adjust(8, (compress(scp_bin[23],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00527             if(pzf==0){
00528                 space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
00529                 space = adjust(8, (compress(proton_scp_sum,6,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
00530             }
00531             if(ezf==0){
00532                 space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
00533                 space = adjust(8, (compress(electron_scp_sum,6,3)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
00534             }
00535         }
00536     }//-----------------------------------------------------------------------------------------------------------------------------------------------------
00537     #if DEBUG
00538     gPC.puts("sci frame starts\r\n");
00539     #endif
00540     
00541     //time to make scp frame
00542     id = 0;
00543     if(srp_mode == 0 && compression_option == 0){
00544         if(at == 0){
00545             length = 228;
00546             if(pzf == 0)
00547                 length += 121;
00548             if(ezf == 0)
00549                 length +=41;
00550         }else if(at == 1){
00551             length = 266;
00552             if(pzf == 0)
00553                 length += 136;
00554             if(ezf == 0)
00555                 length += 40;
00556         }
00557             
00558     }else if(srp_mode == 0 && compression_option == 1){     //data conservation mode
00559         if(at == 0){
00560             length = 94;
00561             if(pzf == 0)
00562                 length += 12;
00563             if(ezf == 0)
00564                 length += 12;
00565         }else if(at == 1){
00566             length = 123;
00567             if(pzf == 0)
00568                 length += 9;
00569             if(ezf == 0)
00570                 length += 9;
00571         }
00572     }else if( srp_mode == 1){
00573         if(at == 0)
00574             length = 272;
00575         else if(at == 1)
00576             length = 288;
00577     }
00578     length = (length%8==0)?(length/8):(length/8)+1;
00579     for(int j= 0; j < length ; j++){
00580         frames[id][position_tm_frame[id]] = scp[j];
00581             position_tm_frame[id]++;
00582             if(position_tm_frame[id] == 132){   //space full in the frame bro
00583                 pointer = frames[id];
00584                 space = adjust(1,0,pointer,8);
00585                 space = adjust(4,1,pointer,space);
00586                 FSC_science = FCTN_SD_MNGR(1);
00587                 //gPC.printf("SID = 1, FSC = %02X\r\n", FSC_science);
00588                 frames[id][1] = (FSC_science>>16)&0xff;
00589                 frames[id][2] = (FSC_science>>8)&0xff;
00590                 frames[id][3] = (FSC_science)&0xff;
00591                 frames[id][5] = (sci_time>>16)&0x3f;
00592                 frames[id][6] = (sci_time>>8)&0xff;
00593                 frames[id][7] = (sci_time)&0xff;
00594                 temp_crc = crc16_gen(frames[id],132);
00595                 frames[id][132] = temp_crc>>8;
00596                 frames[id][133] = temp_crc & 0xff;
00597                 exor(frames[id]);
00598                 convolution(frames[id]);
00599                 interleave(TM_convoluted_data,TM_interleave_data);
00600                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
00601                 SD_WRITE(TM_interleave_data,FSC_science,1);
00602                 position_tm_frame[id] = position_tm_starting[id];
00603                 frames[id][4] = (length-1) - j;
00604             }
00605     }
00606     #if DEBUG
00607     gPC.puts("end srp");
00608     #endif
00609 }//srp fucntion end bracket