krishan prajapat
/
sd_card_making_header_sss2
v2.0 working
Diff: SD_read.h
- Revision:
- 0:8ed3ad791ca2
diff -r 000000000000 -r 8ed3ad791ca2 SD_read.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SD_read.h Thu Jul 30 10:40:17 2015 +0000 @@ -0,0 +1,250 @@ +// It takes 276us to read from sd card and write in 112 buffer + +//~ #include"SDcard.h" +#define T_frames 39 +unsigned char buffer_112_counter,sent_tm = 0; +int disk_read(uint8_t *, uint64_t),j; +bool new_session = true; +bool last_buffer = false; +bool first_block = true; +bool last_block =false; +bool repeat_T_frame = false; +unsigned int counter_in_block=0,diff_prev=0; +unsigned char EOS_counter = 0; +unsigned char enable_T_frame = false; +unsigned long long int start_block_num ; // Read from TC +unsigned long long int end_block_num ; // Read from TC +unsigned long long int Number_of_blocks_to_read ; +unsigned long long int block_counter = 0; +unsigned char buffer_512[512], buffer_112[112],counter_in_S_frame=0,counter_in_SCH=0; +unsigned char S_frame[48]={0x46,0x9D,0xFB,0xF0,0xD2,0xBA,0x89,0xE4,0x46,0x9D,0xFB,0xF0,0xD2,0xBA,0x89,0xE4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; +//unsigned char SCH[40] = {0x0A,0x3F,0x46,0xB4,0x00}; +unsigned char EoS[120]; +unsigned char make_EOS_flag =true; + +void differential_encode(unsigned char* ptr, int length){ + + for(int i=0; i<length;i++){ + + unsigned char s = ptr[i] , t; + t = s ^ (s >> 1); + (diff_prev == 0) ? t=t^0x00 : t=t^0x80 ; + diff_prev = int(s & 0x01); + ptr[i] = t; + } + + } + +void make_EOS() +{ + +for(int i = 0 ; i < 24 ; ++i){ + EoS[i] = 0x00; + } + + //~ S-FRAME [1] + //~ first two bits of S-FRAME + EoS[23] = 0x01; + + EoS[24] = 0x1a; + EoS[25] = 0x77; + EoS[26] = 0xef; + EoS[27] = 0xc3; + EoS[28] = 0x4a; + EoS[29] = 0xea; + EoS[30] = 0x27; + EoS[31] = 0x91; + EoS[32] = 0x1a; + EoS[33] = 0x77; + EoS[34] = 0xef; + EoS[35] = 0xc3; + EoS[36] = 0x4a; + EoS[37] = 0xea; + EoS[38] = 0x27; + EoS[39] = 0x90; + + for(int i = 40 ; i < 72 ; ++i){ + EoS[i] = 0x00; + } + + //~ S-FRAME [2] + //~ first two bits of S-FRAME + EoS[71] = 0x01; + + EoS[72] = 0x1a; + EoS[73] = 0x77; + EoS[74] = 0xef; + EoS[75] = 0xc3; + EoS[76] = 0x4a; + EoS[77] = 0xea; + EoS[78] = 0x27; + EoS[79] = 0x91; + EoS[80] = 0x1a; + EoS[81] = 0x77; + EoS[82] = 0xef; + EoS[83] = 0xc3; + EoS[84] = 0x4a; + EoS[85] = 0xea; + EoS[86] = 0x27; + EoS[87] = 0x90; + + for(int i = 88 ; i < 120 ; ++i){ + EoS[i] = 0x00; + } + +} + +void send_tm_from_SD_card(){ + + + if(make_EOS_flag == true){ + make_EOS(); + make_EOS_flag = false; + Number_of_blocks_to_read = end_block_num - start_block_num +1; + } + + + for(buffer_112_counter=0;buffer_112_counter<112;buffer_112_counter++){ + + + if(new_session == true){ //check if it is beginnig of session + + buffer_112[buffer_112_counter]=S_frame[counter_in_S_frame++]; // sending S frame + + if(counter_in_S_frame==48){ +// cout<<"new_session_over\n"; + counter_in_S_frame=0; + new_session = false; + enable_T_frame = true; +// enable_SCH = true; + } + } + else if(enable_T_frame == true){ // To send t frames + + if(block_counter != Number_of_blocks_to_read){ + + if(first_block){ // Read first block +// cout<<"brooooooo"<<start_block_num<<"yo"<<endl; + + disk_read(buffer_512,start_block_num ); + counter_in_block = 0; + block_counter++; + + first_block = false; +// cout<<"first block exit\n"; +// cout<<Number_of_blocks_to_read; + + //printf("\n"); +// for(int j=0;j<512;j++){ +// printf("%02X ",buffer_512[j]); +// } +// printf("\n"); +// if(block_counter == Number_of_blocks_to_read){ +// last_block = true; +// } + + + } + if((counter_in_block == 318 )&&(sent_tm<38)){ // Read next block + +// cout<<"next block exit\n"; + disk_read(buffer_512,start_block_num + block_counter ); + counter_in_block = 0; + block_counter++; + if(block_counter == Number_of_blocks_to_read){ + last_block = true; + } + } + } + + if(!last_block){ + + // cout<<int(sent_tm)<<" "; + if(sent_tm<38){ + buffer_112[buffer_112_counter] = buffer_512[counter_in_block++]; // sending T frames +// cout<<int (counter_in_block)<<" "; + if(counter_in_block == 318){ + + sent_tm = sent_tm + 2; + } + } + else if(sent_tm == (T_frames-1)){ + buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159]; + if(counter_in_block == 318){ +// cout<<"last block exit\n"; + sent_tm = T_frames; + } + } + else if(sent_tm == T_frames){ // Give eos and start new session + buffer_112[buffer_112_counter]=EoS[EOS_counter++]; + if(EOS_counter == 120){ + enable_T_frame = false; + new_session = true; + EOS_counter =0; + sent_tm=0; + repeat_T_frame = false; +// cout<<"eos exit\n"; + } + } + } + if(last_block){ +// cout<<"lb"; + if(sent_tm == (T_frames-1) && (repeat_T_frame == false)){ + buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159]; + if(counter_in_block == 318){ +// cout<<"repeat block exit\n"; + sent_tm = T_frames; + } + } + if((sent_tm == T_frames) && (repeat_T_frame == false)){ // Give eos and start new session + buffer_112[buffer_112_counter]=EoS[EOS_counter++]; + if(EOS_counter == 120){ +// cout<<"eos exit"; + enable_T_frame = false; + new_session = true; + EOS_counter =0; + sent_tm=0; + } + } + else if((sent_tm < (T_frames-1)) && (repeat_T_frame == false)){ + buffer_112[buffer_112_counter] = buffer_512[counter_in_block++]; // sending last two T frames + if(counter_in_block == 318){ + sent_tm = sent_tm + 2; + repeat_T_frame= true; + } + } + else if((sent_tm< T_frames) && (repeat_T_frame == true)){ + buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159]; + if(counter_in_block == 477){ + sent_tm = sent_tm +1; + counter_in_block = counter_in_block-159; + } + } + else if((sent_tm == T_frames ) && (repeat_T_frame == true)){ // Give eos and start new session + + buffer_112[buffer_112_counter]=EoS[EOS_counter++]; + if(EOS_counter == 120){ + enable_T_frame = false; + new_session = true; + EOS_counter =0; +// printf("yoooo %d",int(sent_tm)); + sent_tm=0; + + last_block = false; + last_buffer = true; +// printf("i m here"); + break; + } + } + + } + + } + + } + differential_encode(buffer_112, 112); + if(last_buffer ==true){ + diff_prev=0; + } + +}