sd 32 update

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers COM_RCV_TC.h Source File

COM_RCV_TC.h

00001 #define flushData_appendTC(bytes, outState) {\
00002     uint16_t tc_type = 2;\
00003     if( (bytes == TC_SHORT_SIZE) && (outState == 7) ){\
00004         tc_type = SHORT_TC_CODE;\
00005     }\
00006     else if( (bytes == TC_LONG_SIZE) && (outState == 7) ){\
00007         tc_type = LONG_TC_CODE;\
00008     }\
00009     else{\
00010         /*gPC.puts("incorrect size\r\n");*/\
00011         /*gPC.printf("%u, %u\r\n", bytes, outState);*/\
00012         ++gTOTAL_INCORRECT_SIZE_TC;\
00013     }\
00014     if( tc_type == SHORT_TC_CODE ){\
00015         uint16_t crc_checksum = crc16_gen(rx_tempString, TC_SHORT_SIZE-2);\
00016         if( ( (crc_checksum & 0xFF) == rx_tempString[TC_SHORT_SIZE-1]) && ( ((crc_checksum >> 8) & 0xFF) == rx_tempString[TC_SHORT_SIZE-2] ) ){\
00017             uint16_t temp16;\
00018             Base_tc *temp_ptr = gHEAD_NODE_TCL;\
00019             bool repeat_flag = false;\
00020             uint8_t testPSC = rx_tempString[0];\
00021             while(temp_ptr != NULL){\
00022                 if( GETpacket_seq_count(temp_ptr) == testPSC ){\
00023                     repeat_flag = true;\
00024                     break;\
00025                 }\
00026                 temp_ptr = temp_ptr->next_TC;\
00027             }\
00028             if( repeat_flag == false ){\
00029                 if( gTOTAL_VALID_TC == 0 ){\
00030                     gHEAD_NODE_TCL = new Short_tc;\
00031                     gHEAD_NODE_TCL->next_TC = NULL;\
00032                     gLAST_NODE_TCL = gHEAD_NODE_TCL;\
00033                 }\
00034                 else{\
00035                     gLAST_NODE_TCL->next_TC = new Short_tc;\
00036                     gLAST_NODE_TCL = gLAST_NODE_TCL->next_TC;\
00037                     gLAST_NODE_TCL->next_TC = NULL;\
00038                 }\
00039                 /*gPC.puts("printing tx string\r\n");*/\
00040                 for(int i = 0 ; i < TC_SHORT_SIZE ; ++i){\
00041                     gLAST_NODE_TCL->TC_string[i] = rx_tempString[i];\
00042                 }\
00043                 PUTshort_or_long(gLAST_NODE_TCL, tc_type);\
00044                 temp16 = 1;\
00045                 PUTcrc_pass(gLAST_NODE_TCL, temp16);\
00046                 temp16 = 0;\
00047                 PUTexec_status(gLAST_NODE_TCL, temp16);\
00048                 ++gTOTAL_VALID_TC;\
00049             }\
00050             else{\
00051                 /*gPC.puts("repeated psc short\r\n");*/\
00052                 ++gTOTAL_REPEATED_TC;\
00053             }\
00054         }\
00055         else{\
00056             /*gPC.puts("crc fail tc short\r\n");*/\
00057             ++gTOTAL_CRC_FAIL_TC;\
00058         }\
00059     }\
00060     else if( tc_type == LONG_TC_CODE ){\
00061         uint16_t crc_checksum = crc16_gen(rx_tempString, TC_LONG_SIZE-2);\
00062         if( ( (crc_checksum & 0xFF) == rx_tempString[TC_LONG_SIZE-1]) && ( ((crc_checksum >> 8) & 0xFF) == rx_tempString[TC_LONG_SIZE-2] ) ){\
00063             uint16_t temp16;\
00064             Base_tc *temp_ptr = gHEAD_NODE_TCL;\
00065             bool repeat_flag = false;\
00066             uint8_t testPSC = rx_tempString[0];\
00067             while(temp_ptr != NULL){\
00068                 if( GETpacket_seq_count(temp_ptr) == testPSC ){\
00069                     repeat_flag = true;\
00070                     break;\
00071                 }\
00072                 temp_ptr = temp_ptr->next_TC;\
00073             }\
00074             if( repeat_flag == false ){\
00075                 if( gTOTAL_VALID_TC == 0 ){\
00076                     gHEAD_NODE_TCL = new Long_tc;\
00077                     gHEAD_NODE_TCL->next_TC = NULL;\
00078                     gLAST_NODE_TCL = gHEAD_NODE_TCL;\
00079                 }\
00080                 else{\
00081                     gLAST_NODE_TCL->next_TC = new Long_tc;\
00082                     gLAST_NODE_TCL = gLAST_NODE_TCL->next_TC;\
00083                     gLAST_NODE_TCL->next_TC = NULL;\
00084                 }\
00085                 /*gPC.puts("printing tx string\r\n");*/\
00086                 for(int i = 0 ; i < TC_LONG_SIZE ; ++i){\
00087                     gLAST_NODE_TCL->TC_string[i] = rx_tempString[i];\
00088                 }\
00089                 PUTshort_or_long(gLAST_NODE_TCL, tc_type);\
00090                 temp16 = 1;\
00091                 PUTcrc_pass(gLAST_NODE_TCL, temp16);\
00092                 temp16 = 0;\
00093                 PUTexec_status(gLAST_NODE_TCL, temp16);\
00094                 ++gTOTAL_VALID_TC;\
00095             }\
00096             else{\
00097                 /*gPC.puts("repeated psc long\r\n");*/\
00098                 ++gTOTAL_REPEATED_TC;\
00099             }\
00100         }\
00101         else{\
00102             /*gPC.puts("crc fail tc long\r\n");*/\
00103             ++gTOTAL_CRC_FAIL_TC;\
00104         }\
00105     }\
00106 }
00107 
00108 unsigned char rx_tempString[TC_LONG_SIZE+1];
00109 
00110 void raw_data_to_tc(void){
00111     /*gPC.puts("inside raw_data_to_tc\r\n")*/;
00112     bool frame_started = false;
00113     bool chain_started = false;
00114     bool flush_called = false;
00115 
00116     uint32_t bytes_read = 0;
00117     uint8_t state7e = 0;
00118     uint8_t outState = 0;
00119     uint32_t outByte = 0;
00120     uint32_t byteCount = 0;
00121     
00122     COM_RX_DATA_NODE *data_node = gRX_HEAD_DATA_NODE;
00123     // read byte by byte
00124     while( data_node != NULL ){
00125         for(int iBuf = 0 ; iBuf < RX_BUFFER_LENGTH ; ++iBuf ){
00126             uint8_t test_this = data_node->values[iBuf];
00127             ++bytes_read;
00128     
00129             // read bit by bit
00130             for(int i = 7 ; i >= 0 ; --i){
00131                 unsigned char tempBit = (test_this >> i) & 1;
00132                 bool skipIteration = false;
00133     
00134                 if( tempBit == 1 ){
00135                     switch( state7e ){
00136                         case 0:
00137                             state7e = 0;
00138                             break;
00139                         case 1:
00140                             state7e = 2;
00141                             break;
00142                         case 2:
00143                             state7e = 3;
00144                             break;
00145                         case 3:
00146                             state7e = 4;
00147                             break;
00148                         case 4:
00149                             state7e = 5;
00150                             break;
00151                         case 5:
00152                             state7e = 6;
00153                             break;
00154                         case 6:
00155                             state7e = 7;
00156                             break;
00157                         case 7:
00158                             // error reset
00159                             state7e = 0;
00160                             chain_started = false;
00161                             frame_started = false;
00162                             byteCount = 0;
00163                             outByte = 0;
00164                             outState = 0;
00165                             skipIteration = true;
00166                             break;
00167                     }
00168                 }
00169                 else{
00170                     switch( state7e ){
00171                         case 0:
00172                         case 1:
00173                         case 2:
00174                         case 3:
00175                         case 4:
00176                         case 5:
00177                             state7e = 1;
00178                             break;
00179                         case 6:
00180                             state7e = 1;
00181                             skipIteration = true;
00182                             break;
00183                         case 7:
00184                             state7e = 0;
00185                             // detected 7e
00186                             if( !chain_started ){
00187                                 chain_started = true;
00188                                 frame_started = true;
00189                                 byteCount = 0;
00190                                 outByte = 0;
00191                                 outState = 0;
00192                                 skipIteration = true;
00193                             }
00194                             else{
00195                                 flush_called = true;
00196                                 flushData_appendTC(byteCount, outState);
00197                                 byteCount = 0;
00198                                 outState = 0;
00199                                 outByte = 0;
00200                                 skipIteration = true;
00201                             }
00202                             break;
00203                     }
00204                 }
00205                 if( (!skipIteration) && (frame_started) ){
00206                     // write bit to output
00207                     switch( outState ){
00208                         case 0:
00209                             outState = 1;
00210                             rx_tempString[outByte] = tempBit << 7;
00211                             break;
00212                         case 1:
00213                             outState = 2;
00214                             rx_tempString[outByte] += tempBit << 6;
00215                             break;
00216                         case 2:
00217                             outState = 3;
00218                             rx_tempString[outByte] += tempBit << 5;
00219                             break;
00220                         case 3:
00221                             outState = 4;
00222                             rx_tempString[outByte] += tempBit << 4;
00223                             break;
00224                         case 4:
00225                             outState = 5;
00226                             rx_tempString[outByte] += tempBit << 3;
00227                             break;
00228                         case 5:
00229                             outState = 6;
00230                             rx_tempString[outByte] += tempBit << 2;
00231                             break;
00232                         case 6:
00233                             outState = 7;
00234                             rx_tempString[outByte] += tempBit << 1;
00235                             break;
00236                         case 7:
00237                             outState = 0;
00238                             rx_tempString[outByte] += tempBit;
00239                             ++outByte;
00240                             // exceeded tc length discard
00241                             if(outByte > 135){
00242                                 ++gTOTAL_INCORRECT_SIZE_TC;
00243                                 outByte = 0;
00244                             }
00245                             ++byteCount;
00246                             break;
00247                     }
00248                 }
00249             }
00250         }
00251         COM_RX_DATA_NODE *temp = data_node->next_node;
00252         delete data_node;
00253         data_node = temp;
00254     }
00255     if( (!flush_called) && (gTOTAL_INCORRECT_SIZE_TC == 0) ){
00256         ++gTOTAL_INCORRECT_SIZE_TC;
00257     }
00258     gRX_HEAD_DATA_NODE = new COM_RX_DATA_NODE;
00259     gRX_HEAD_DATA_NODE->next_node = NULL;
00260     gRX_CURRENT_DATA_NODE = gRX_HEAD_DATA_NODE;
00261     gRX_COUNT = 0;
00262     
00263     /*gPC.puts("Completed conversion of raw_data\r\n")*/;
00264     // PENDING: SORT THE LINKED LIST ACCORDING TO PSC VALUE
00265 }