Repo for COM_MNG_TMTC_SIMPLE for integration with adf

Dependencies:   mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Committer:
shreeshas95
Date:
Mon Dec 14 12:04:01 2015 +0000
Revision:
1:a0055b3280c8
Child:
8:d1951bbdcdd8
Simple working version code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shreeshas95 1:a0055b3280c8 1 // It takes 276us to read from sd card and write in 112 buffer
shreeshas95 1:a0055b3280c8 2
shreeshas95 1:a0055b3280c8 3 //~ #include"SDcard.h"
shreeshas95 1:a0055b3280c8 4 #define T_frames 30
shreeshas95 1:a0055b3280c8 5 unsigned char buffer_112_counter,sent_tm = 0;
shreeshas95 1:a0055b3280c8 6 int disk_read(uint8_t *, uint64_t),j;
shreeshas95 1:a0055b3280c8 7 bool new_session = true;
shreeshas95 1:a0055b3280c8 8 bool last_buffer = false;
shreeshas95 1:a0055b3280c8 9 bool first_block = true;
shreeshas95 1:a0055b3280c8 10 bool last_block =false;
shreeshas95 1:a0055b3280c8 11 bool repeat_T_frame = false;
shreeshas95 1:a0055b3280c8 12 unsigned int counter_in_block=0,diff_prev=0;
shreeshas95 1:a0055b3280c8 13 unsigned char EOS_counter = 0;
shreeshas95 1:a0055b3280c8 14 unsigned char enable_T_frame = false;
shreeshas95 1:a0055b3280c8 15 uint64_t start_block_num ; // Read from TC
shreeshas95 1:a0055b3280c8 16 uint64_t end_block_num ; // Read from TC
shreeshas95 1:a0055b3280c8 17 uint64_t Number_of_blocks_to_read ;
shreeshas95 1:a0055b3280c8 18 uint64_t block_counter = 0;
shreeshas95 1:a0055b3280c8 19 unsigned char buffer_512[512], buffer_112[112],counter_in_S_frame=0,counter_in_SCH=0;
shreeshas95 1:a0055b3280c8 20 unsigned char make_EOS_flag = true;
shreeshas95 1:a0055b3280c8 21 bool reset = false;
shreeshas95 1:a0055b3280c8 22
shreeshas95 1:a0055b3280c8 23 void differential_encode(unsigned char* ptr, int length){
shreeshas95 1:a0055b3280c8 24
shreeshas95 1:a0055b3280c8 25 for(int i=0; i<length;i++){
shreeshas95 1:a0055b3280c8 26
shreeshas95 1:a0055b3280c8 27 unsigned char s = ptr[i] , t;
shreeshas95 1:a0055b3280c8 28 t = s ^ (s >> 1);
shreeshas95 1:a0055b3280c8 29 (diff_prev == 0) ? t=t^0x00 : t=t^0x80 ;
shreeshas95 1:a0055b3280c8 30 diff_prev = int(s & 0x01);
shreeshas95 1:a0055b3280c8 31 ptr[i] = t;
shreeshas95 1:a0055b3280c8 32 }
shreeshas95 1:a0055b3280c8 33
shreeshas95 1:a0055b3280c8 34 }
shreeshas95 1:a0055b3280c8 35
shreeshas95 1:a0055b3280c8 36 void send_tm_from_SD_card(){
shreeshas95 1:a0055b3280c8 37
shreeshas95 1:a0055b3280c8 38 if(make_EOS_flag == true){
shreeshas95 1:a0055b3280c8 39 make_EOS_flag = false;
shreeshas95 1:a0055b3280c8 40 Number_of_blocks_to_read = end_block_num - start_block_num +1;
shreeshas95 1:a0055b3280c8 41 }
shreeshas95 1:a0055b3280c8 42
shreeshas95 1:a0055b3280c8 43
shreeshas95 1:a0055b3280c8 44 for(buffer_112_counter=0;buffer_112_counter<112;buffer_112_counter++){
shreeshas95 1:a0055b3280c8 45
shreeshas95 1:a0055b3280c8 46
shreeshas95 1:a0055b3280c8 47 if(new_session == true){ //check if it is beginnig of session
shreeshas95 1:a0055b3280c8 48
shreeshas95 1:a0055b3280c8 49 buffer_112[buffer_112_counter]=S_frame[counter_in_S_frame++]; // sending S frame
shreeshas95 1:a0055b3280c8 50
shreeshas95 1:a0055b3280c8 51 if(counter_in_S_frame==48){
shreeshas95 1:a0055b3280c8 52 // cout<<"new_session_over\n";
shreeshas95 1:a0055b3280c8 53 counter_in_S_frame=0;
shreeshas95 1:a0055b3280c8 54 new_session = false;
shreeshas95 1:a0055b3280c8 55 enable_T_frame = true;
shreeshas95 1:a0055b3280c8 56 // enable_SCH = true;
shreeshas95 1:a0055b3280c8 57 }
shreeshas95 1:a0055b3280c8 58 }
shreeshas95 1:a0055b3280c8 59 else if(enable_T_frame == true){ // To send t frames
shreeshas95 1:a0055b3280c8 60
shreeshas95 1:a0055b3280c8 61 if(block_counter != Number_of_blocks_to_read){
shreeshas95 1:a0055b3280c8 62
shreeshas95 1:a0055b3280c8 63 if(first_block){ // Read first block
shreeshas95 1:a0055b3280c8 64 // cout<<"brooooooo"<<start_block_num<<"yo"<<endl;
shreeshas95 1:a0055b3280c8 65 SPI_mutex.lock();
shreeshas95 1:a0055b3280c8 66 disk_read(buffer_512,start_block_num );
shreeshas95 1:a0055b3280c8 67 SPI_mutex.unlock();
shreeshas95 1:a0055b3280c8 68 counter_in_block = 0;
shreeshas95 1:a0055b3280c8 69 block_counter++;
shreeshas95 1:a0055b3280c8 70
shreeshas95 1:a0055b3280c8 71 first_block = false;
shreeshas95 1:a0055b3280c8 72 // cout<<"first block exit\n";
shreeshas95 1:a0055b3280c8 73 // cout<<Number_of_blocks_to_read;
shreeshas95 1:a0055b3280c8 74
shreeshas95 1:a0055b3280c8 75 //printf("\n");
shreeshas95 1:a0055b3280c8 76 // for(int j=0;j<512;j++){
shreeshas95 1:a0055b3280c8 77 // printf("%02X ",buffer_512[j]);
shreeshas95 1:a0055b3280c8 78 // }
shreeshas95 1:a0055b3280c8 79 // printf("\n");
shreeshas95 1:a0055b3280c8 80 // if(block_counter == Number_of_blocks_to_read){
shreeshas95 1:a0055b3280c8 81 // last_block = true;
shreeshas95 1:a0055b3280c8 82 // }
shreeshas95 1:a0055b3280c8 83
shreeshas95 1:a0055b3280c8 84
shreeshas95 1:a0055b3280c8 85 }
shreeshas95 1:a0055b3280c8 86 if((counter_in_block == 318 )&&(sent_tm<38)){ // Read next block
shreeshas95 1:a0055b3280c8 87
shreeshas95 1:a0055b3280c8 88 // cout<<"next block exit\n";
shreeshas95 1:a0055b3280c8 89 SPI_mutex.lock();
shreeshas95 1:a0055b3280c8 90 disk_read(buffer_512,start_block_num + block_counter );
shreeshas95 1:a0055b3280c8 91 SPI_mutex.unlock();
shreeshas95 1:a0055b3280c8 92 counter_in_block = 0;
shreeshas95 1:a0055b3280c8 93 block_counter++;
shreeshas95 1:a0055b3280c8 94 if(block_counter == Number_of_blocks_to_read){
shreeshas95 1:a0055b3280c8 95 last_block = true;
shreeshas95 1:a0055b3280c8 96 }
shreeshas95 1:a0055b3280c8 97 }
shreeshas95 1:a0055b3280c8 98 }
shreeshas95 1:a0055b3280c8 99
shreeshas95 1:a0055b3280c8 100 if(!last_block){
shreeshas95 1:a0055b3280c8 101
shreeshas95 1:a0055b3280c8 102 // cout<<int(sent_tm)<<" ";
shreeshas95 1:a0055b3280c8 103 if(sent_tm<38){
shreeshas95 1:a0055b3280c8 104 buffer_112[buffer_112_counter] = buffer_512[counter_in_block++]; // sending T frames
shreeshas95 1:a0055b3280c8 105 // cout<<int (counter_in_block)<<" ";
shreeshas95 1:a0055b3280c8 106 if(counter_in_block == 318){
shreeshas95 1:a0055b3280c8 107
shreeshas95 1:a0055b3280c8 108 sent_tm = sent_tm + 2;
shreeshas95 1:a0055b3280c8 109 }
shreeshas95 1:a0055b3280c8 110 }
shreeshas95 1:a0055b3280c8 111 else if(sent_tm == (T_frames-1)){
shreeshas95 1:a0055b3280c8 112 buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159];
shreeshas95 1:a0055b3280c8 113 if(counter_in_block == 318){
shreeshas95 1:a0055b3280c8 114 // cout<<"last block exit\n";
shreeshas95 1:a0055b3280c8 115 sent_tm = T_frames;
shreeshas95 1:a0055b3280c8 116 }
shreeshas95 1:a0055b3280c8 117 }
shreeshas95 1:a0055b3280c8 118 else if(sent_tm == T_frames){ // Give eos and start new session
shreeshas95 1:a0055b3280c8 119 buffer_112[buffer_112_counter]=EoS[EOS_counter++];
shreeshas95 1:a0055b3280c8 120 if(EOS_counter == 120){
shreeshas95 1:a0055b3280c8 121 enable_T_frame = false;
shreeshas95 1:a0055b3280c8 122 new_session = true;
shreeshas95 1:a0055b3280c8 123 EOS_counter =0;
shreeshas95 1:a0055b3280c8 124 sent_tm=0;
shreeshas95 1:a0055b3280c8 125 repeat_T_frame = false;
shreeshas95 1:a0055b3280c8 126 // cout<<"eos exit\n";
shreeshas95 1:a0055b3280c8 127 }
shreeshas95 1:a0055b3280c8 128 }
shreeshas95 1:a0055b3280c8 129 }
shreeshas95 1:a0055b3280c8 130 if(last_block){
shreeshas95 1:a0055b3280c8 131 // cout<<"lb";
shreeshas95 1:a0055b3280c8 132 if(sent_tm == (T_frames-1) && (repeat_T_frame == false)){
shreeshas95 1:a0055b3280c8 133 buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159];
shreeshas95 1:a0055b3280c8 134 if(counter_in_block == 318){
shreeshas95 1:a0055b3280c8 135 // cout<<"repeat block exit\n";
shreeshas95 1:a0055b3280c8 136 sent_tm = T_frames;
shreeshas95 1:a0055b3280c8 137 }
shreeshas95 1:a0055b3280c8 138 }
shreeshas95 1:a0055b3280c8 139 if((sent_tm == T_frames) && (repeat_T_frame == false)){ // Give eos and start new session
shreeshas95 1:a0055b3280c8 140 buffer_112[buffer_112_counter]=EoS[EOS_counter++];
shreeshas95 1:a0055b3280c8 141 if(EOS_counter == 120){
shreeshas95 1:a0055b3280c8 142 // cout<<"eos exit";
shreeshas95 1:a0055b3280c8 143 enable_T_frame = false;
shreeshas95 1:a0055b3280c8 144 new_session = true;
shreeshas95 1:a0055b3280c8 145 EOS_counter =0;
shreeshas95 1:a0055b3280c8 146 sent_tm=0;
shreeshas95 1:a0055b3280c8 147 }
shreeshas95 1:a0055b3280c8 148 }
shreeshas95 1:a0055b3280c8 149 else if((sent_tm < (T_frames-1)) && (repeat_T_frame == false)){
shreeshas95 1:a0055b3280c8 150 buffer_112[buffer_112_counter] = buffer_512[counter_in_block++]; // sending last two T frames
shreeshas95 1:a0055b3280c8 151 if(counter_in_block == 318){
shreeshas95 1:a0055b3280c8 152 sent_tm = sent_tm + 2;
shreeshas95 1:a0055b3280c8 153 repeat_T_frame= true;
shreeshas95 1:a0055b3280c8 154 }
shreeshas95 1:a0055b3280c8 155 }
shreeshas95 1:a0055b3280c8 156 else if((sent_tm< T_frames) && (repeat_T_frame == true)){
shreeshas95 1:a0055b3280c8 157 buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159];
shreeshas95 1:a0055b3280c8 158 if(counter_in_block == 477){
shreeshas95 1:a0055b3280c8 159 sent_tm = sent_tm +1;
shreeshas95 1:a0055b3280c8 160 counter_in_block = counter_in_block-159;
shreeshas95 1:a0055b3280c8 161 }
shreeshas95 1:a0055b3280c8 162 }
shreeshas95 1:a0055b3280c8 163 else if((sent_tm == T_frames ) && (repeat_T_frame == true)){ // Give eos and start new session
shreeshas95 1:a0055b3280c8 164
shreeshas95 1:a0055b3280c8 165 buffer_112[buffer_112_counter]=EoS[EOS_counter++];
shreeshas95 1:a0055b3280c8 166 if(EOS_counter == 120){
shreeshas95 1:a0055b3280c8 167 enable_T_frame = false;
shreeshas95 1:a0055b3280c8 168 new_session = true;
shreeshas95 1:a0055b3280c8 169 EOS_counter =0;
shreeshas95 1:a0055b3280c8 170 // printf("yoooo %d",int(sent_tm));
shreeshas95 1:a0055b3280c8 171 sent_tm=0;
shreeshas95 1:a0055b3280c8 172
shreeshas95 1:a0055b3280c8 173 last_block = false;
shreeshas95 1:a0055b3280c8 174 last_buffer = true;
shreeshas95 1:a0055b3280c8 175 printf("?\r\n");
shreeshas95 1:a0055b3280c8 176 break;
shreeshas95 1:a0055b3280c8 177 }
shreeshas95 1:a0055b3280c8 178 }
shreeshas95 1:a0055b3280c8 179
shreeshas95 1:a0055b3280c8 180 }
shreeshas95 1:a0055b3280c8 181
shreeshas95 1:a0055b3280c8 182 }
shreeshas95 1:a0055b3280c8 183
shreeshas95 1:a0055b3280c8 184 }
shreeshas95 1:a0055b3280c8 185 differential_encode(buffer_112, 112);
shreeshas95 1:a0055b3280c8 186 if(last_buffer ==true){
shreeshas95 1:a0055b3280c8 187 diff_prev=0;
shreeshas95 1:a0055b3280c8 188 }
shreeshas95 1:a0055b3280c8 189
shreeshas95 1:a0055b3280c8 190 }