pl ack in tmtc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by shubham c

COM_SND_TM_functions.h

Committer:
pradeepvk2208
Date:
2016-04-02
Revision:
140:4feeb1163bb6
Parent:
1:a0055b3280c8

File content as of revision 140:4feeb1163bb6:

// ***************************************** CONVOLUTION *****************************************
class Convolution{
    
private:
    typedef struct ConvNode{
        //next state
        //format : 8 bits mapped as [1]xxxx [0]xxxx
        unsigned char nextState;
        
        //output
        // format : 8 bits mapped as 
        // **** xxxx{2} [1]-[0]   {1} [1]-[0]      i.e.   ****xxxx
        // x = data, * = junk
        unsigned char output;
    }ConvNode;

    ConvNode convStateList[16]; 
    
public:
    /*
    @brief:     Constructor : Initialise all the variables
    @param:     none
    @return:    none
    */
    Convolution(){
        
        convStateList[0].nextState = 0x10;
        convStateList[1].nextState = 0x32;
        convStateList[2].nextState = 0x54;
        convStateList[3].nextState = 0x76;
        convStateList[4].nextState = 0x98;
        convStateList[5].nextState = 0xBA;
        convStateList[6].nextState = 0xDC;
        convStateList[7].nextState = 0xFE;
        convStateList[8].nextState = 0x10;
        convStateList[9].nextState = 0x32;
        convStateList[10].nextState = 0x54;
        convStateList[11].nextState = 0x76;
        convStateList[12].nextState = 0x98;
        convStateList[13].nextState = 0xBA;
        convStateList[14].nextState = 0xDC;
        convStateList[15].nextState = 0xFE;
        
        convStateList[0].output = 0x0A;
        convStateList[1].output = 0x06;
        convStateList[2].output = 0x06;
        convStateList[3].output = 0x0A;
        convStateList[4].output = 0x09;
        convStateList[5].output = 0x05;
        convStateList[6].output = 0x05;
        convStateList[7].output = 0x09;
        convStateList[8].output = 0x05;
        convStateList[9].output = 0x09;
        convStateList[10].output = 0x09;
        convStateList[11].output = 0x05;
        convStateList[12].output = 0x06;
        convStateList[13].output = 0x0A;
        convStateList[14].output = 0x0A;
        convStateList[15].output = 0x06;
    }
    
        
    /*
    @brief:     convolution encode the input bytes : in sync with =>   (1-TM-Frame -> 2-T-Frame)
    @param:     inPtr : pointer to input unsigned char array
                inLength : length in bytes of input array
                outPtr : pointer to the output bytes, define it outside the function
                outLength : length of output array in bytes [call by reference, edited inside the function]
    @return:    none
    */
    void convolutionEncode(unsigned char *input, unsigned char *output){
        
//      cout << "inside convolution" << endl;
        
        unsigned int convState = 0;
        
        int inBit = 7;
        unsigned int inByte = 0;
        
        unsigned int outState = 0;
        unsigned int outByte = 0;
        
        for(unsigned int j = 0 ; j < 536 ; ++j){
//          printf("j = %u, inByte = %u\n", j, inByte);
            // read a new bit from input stream
//          cout << "inByte " <<inByte;
            unsigned char tempBit = (input[inByte] >> inBit) & 1;
            --inBit;
            
            // convolute and write output
            switch(outState){
                case 0:
                    outState = 2;
                    output[outByte] = ( (convStateList[convState].output >> tempBit) & 1 ) << 7;
                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 ) << 6;
                    break;
                case 2:
                    outState = 4;
                    output[outByte] += ( (convStateList[convState].output >> tempBit) & 1 ) << 5;
                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 ) << 4;
                    break;
                case 4:
                    outState = 6;
                    output[outByte] += ( (convStateList[convState].output >> tempBit) & 1 ) << 3;
                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 ) << 2;
                    break;
                case 6:
                    outState = 0;
                    output[outByte] += ( (convStateList[convState].output >> tempBit) & 1 ) << 1;
                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 );
                    ++outByte;
                    output[outByte] = 0x00;
                    break;
            }
            
            // next state transition
            switch(tempBit){
                case 0:
                    convState = (convStateList[convState].nextState) & 0xF;
                    break;
                case 1:
                    convState = (convStateList[convState].nextState >> 4) & 0xF;
                    break;
            }
            
            if(inBit == -1){
//              printf("in byte = %u\n", inByte);
                ++inByte;
                inBit = 7;
            }
            
        }
        
//      printf("normal iteration complete, outByte = %u\n", outByte);

        for(unsigned int j = 0 ; j < 4 ; ++j){
//          printf("j = %u\n, outByte = %u\n", j, outByte);
            // append zero at the end
            switch(outState){
                case 0:
                    outState = 2;
                    output[outByte] = ( (convStateList[convState].output) & 1 ) << 7;
                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 ) << 6;
                    break;
                case 2:
                    outState = 4;
                    output[outByte] += ( (convStateList[convState].output) & 1 ) << 5;
                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 ) << 4;
                    break;
                case 4:
                    outState = 6;
                    output[outByte] += ( (convStateList[convState].output ) & 1 ) << 3;
                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 ) << 2;
                    break;
                case 6:
                    outState = 0;
                    output[outByte] += ( (convStateList[convState].output ) & 1 ) << 1;
                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 );
                    ++outByte;
//                  printf("outByte @ end of switch = %u\n", outByte);
                    break;
            }
            // next state transition
            convState = (convStateList[convState].nextState) & 0xF;
        }
//      printf("complete\n");
    }
    
};
 

// ******************************************** INTERLEAVE **************************************
    void interleave( unsigned char *input, unsigned char *output ){
    
    unsigned int outState = 0;
    unsigned int outByte = 0;
    
    for( unsigned int i = 0 ; i < 36 ; ++i ){
        for(unsigned int j = 0 ; j < 30 ; ++j){
            unsigned int x = j*36+i;
            unsigned char tempBit = ((input[x >> 3]) >> (7-(x % 8))) & 1;
            switch(outState){
                case 0:
                    outState = 1;
                    output[outByte] = tempBit << 7;
                    break;
                case 1:
                    outState = 2;
                    output[outByte] += tempBit << 6;
                    break;
                case 2:
                    outState = 3;
                    output[outByte] += tempBit << 5;
                    break;
                case 3:
                    outState = 4;
                    output[outByte] += tempBit << 4;
                    break;
                case 4:
                    outState = 5;
                    output[outByte] += tempBit << 3;
                    break;
                case 5:
                    outState = 6;
                    output[outByte] += tempBit << 2;
                    break;
                case 6:
                    outState = 7;
                    output[outByte] += tempBit << 1;
                    break;
                case 7:
                    outState = 0;
                    output[outByte] += tempBit;
                    ++outByte;
                    break;
            }
        }
        for(unsigned int j = 0 ; j < 2 ; ++j){
            switch(outState){
                case 0:
                    output[outByte] = 0;
                    outState = 1;
                    break;
                case 1:
                    outState = 2;
                    break;
                case 2:
                    outState = 3;
                    break;
                case 3:
                    outState = 4;
                    break;
                case 4:
                    outState = 5;
                    break;
                case 5:
                    outState = 6;
                    break;
                case 6:
                    outState = 7;
                    break;
                case 7:
                    outState = 0;
                    ++outByte;
                    break;
            }
        }
    }
}