grand final code bro include all the headers in the same order as it is used in main and remove mai and just call snd_tm.head_point( TM_list *);

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers snd_tm_editing_Mpi.h Source File

snd_tm_editing_Mpi.h

00001 #define START_ADDRESS 0x020;
00002 #define MISO_PIN PTE3
00003 
00004 /**************Defining Counter Limits**************/
00005 
00006 #define THRS 20
00007 #define STATE_ERR_THRS 20
00008 #define PHY_OFF_EXEC_TIME 300
00009 #define PHY_ON_EXEC_TIME 300
00010 #define PHY_TX_EXEC_TIME 600
00011 /******DEFINING COMMANDS*********/
00012 
00013 #define CMD_HW_RESET 0xC8
00014 #define CMD_PHY_ON 0xB1
00015 #define CMD_PHY_OFF 0xB0
00016 #define CMD_PHY_TX 0xB5
00017 #define CMD_CONFIG_DEV 0xBB
00018 
00019 /*****FUNCTION Declaration********************/
00020 bool assrt_phy_off( int, int, int);
00021 bool assrt_phy_on( int,int,int);
00022 bool assrt_phy_tx(int,int,int);
00023 /****************************/
00024 SPI adf(PTE1, PTE3, PTE2); //MOSI, MISO, CLOCK
00025 //SPI adf(PTA16, PTA17, PTA15); //MOSI, MISO, CLOCK
00026 //DigitalOut CS(D10);
00027 DigitalOut CS(PTA15);
00028 DigitalOut led2(LED_RED);
00029 //InterruptIn IRQ(D8);
00030 InterruptIn IRQ(PTA14);
00031 Ticker ticker;
00032 unsigned char d = 0,c=1; 
00033 unsigned char k = 0;
00034 unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xF4,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,0x7F,0x00,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x10,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00};
00035 
00036 unsigned char status =0;
00037 unsigned int cmd_err_cnt=0;
00038 unsigned int state_err_cnt=0;
00039 unsigned int miso_err_cnt=0;
00040 unsigned int hw_reset_err_cnt=0;
00041 
00042 bool bcn_flag=0;
00043 bool bbram_flag=0;
00044 
00045 /*******ADDITIONAL STUFF****************/    
00046 unsigned char check_status(void){
00047     unsigned char stat=0;
00048     CS=0;
00049     adf.write(0xFF);
00050     stat = adf.write(0xFF);
00051     CS=1;
00052     return stat;
00053     }
00054 /*****************************************/
00055 bool assrt_phy_off(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
00056     for(int i = 0; i<40 ;i++){
00057         status=check_status();
00058         if(status==0xB1){
00059             return 0;
00060         }
00061         else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
00062             return 1;//You need to Reset the hardware
00063         }
00064         else if(state_err_cnt>STATE_ERR_THRS){
00065             return 1;//Again reset the hardware
00066         }
00067         else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
00068             CS=0;
00069             adf.write(CMD_PHY_OFF);        //CMD_PHY_OFF=0xB0
00070             CS=1;
00071             wait_us(PHY_OFF_EXEC_TIME);// Typical = 24us  We are giving 300us 
00072             state_err_cnt++;
00073 //            return assrt_phy_off(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
00074         }
00075         else if(status&0x80==0x00){
00076             wait_ms(5);
00077             //Error: SPI=0 Not ready CMD= Dont care
00078             spi_err_cnt++;
00079 //            return assrt_phy_off(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
00080         }
00081         else {//if(status&0xA0==0x80){
00082             wait_ms(1);
00083             //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
00084             cmd_err_cnt++;
00085 //            return assrt_phy_off(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
00086         }
00087     }
00088 }
00089 
00090 bool assrt_phy_on(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
00091         
00092     for(int i = 0 ; i < 40 ; i++){
00093         status=check_status();
00094         if((status&0x1F)==0x12){
00095             return 0;
00096         }
00097         else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
00098             return 1;//You need to Reset the hardware
00099         }
00100         else if(state_err_cnt>STATE_ERR_THRS){
00101             return 1;//Again reset the hardware
00102         }
00103         else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
00104             CS=0;
00105             adf.write(0xB1);        //CMD_PHY_OFF
00106             CS=1;
00107             wait_us(PHY_ON_EXEC_TIME);// Typical = 24us  We are giving 300us 
00108             state_err_cnt++;
00109         }
00110         else if(status&0x80==0x00){
00111             wait_ms(5);
00112             spi_err_cnt++;
00113         }
00114         else if(status&0xA0==0x80){
00115             wait_ms(1);
00116             cmd_err_cnt++;
00117         } 
00118     }
00119         
00120 }
00121 
00122 
00123  /*****************************/
00124  bool assrt_phy_tx(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
00125     for(int i = 0 ; i <40; i++){
00126         status=check_status();
00127         if((status & 0x1F) == 0x14){
00128             return 0;
00129         }
00130         else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
00131             return 1;//You need to Reset the hardware
00132         }
00133         else if(state_err_cnt>STATE_ERR_THRS){
00134             return 1;//Again reset the hardware
00135         }
00136         else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
00137             CS=0;
00138             adf.write(0xB1);        //CMD_PHY_OFF
00139             CS=1;
00140             wait_us(PHY_TX_EXEC_TIME);// Typical = 24us  We are giving 300us 
00141             state_err_cnt++;
00142         }
00143         else if(status&0x80==0x00){
00144             wait_ms(1);
00145             //Error: SPI=0 Not ready CMD= Dont care
00146             spi_err_cnt++;
00147         }
00148         else if(status&0xA0==0x80){
00149             wait_us(50);
00150             //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
00151             cmd_err_cnt++;
00152         }
00153     }
00154 }
00155 
00156 /***************************/
00157 bool hardware_reset(int bcn_call){
00158     if (bcn_call>20){//Worst Case 20seconds will be lost !
00159         return 1;
00160     }
00161     int count=0;
00162     CS=0;
00163     adf.write(CMD_HW_RESET);
00164     CS=1;
00165     wait_ms(2);// Typically 1 ms
00166     while(count<10 && miso_err_cnt<10){      
00167         if(MISO_PIN){
00168             
00169             if(!assrt_phy_off(0,0,0)){
00170                 break;
00171             }
00172             count++;
00173         }
00174         else{
00175             wait_us(50);
00176             miso_err_cnt++;
00177         }
00178     }
00179     if(count==10 ||miso_err_cnt==10){
00180         return hardware_reset(bcn_call+1);  
00181     }
00182     else
00183         return 0;
00184     
00185 }
00186 /**********END OF STATUS CHECKING FUNCTIONS******************/
00187 
00188 void bbram_write()
00189 {   
00190     CS=0;
00191     adf.write(0xB0);//PHY_OFF
00192     wait_us(300);
00193     CS=1;
00194     
00195     // Write bbram
00196     CS=0;
00197      for(int i=0;i<66;i++){
00198         adf.write(bbram_buffer[i]);    
00199     }
00200     CS=1;
00201 //    wait(1);
00202     
00203 }
00204 
00205 
00206 
00207 
00208 /******************************************************************************************/
00209 
00210 void initiate(void){
00211     
00212     CS=0;
00213     adf.write(0xFF);
00214     adf.write(0xFF);
00215     CS=1;
00216 //    wait_ms(2);
00217 
00218     CS=0;
00219     adf.write(0x08);    // TRANSMIT_DATA LENGTH
00220     adf.write(0x14);
00221     adf.write(0xFF);
00222     CS=1;
00223 //    wait_ms(2);
00224     CS=0;
00225     adf.write(0x08);        // TRANSMIT_DATA LENGTH
00226     adf.write(0x15);
00227     adf.write(0xFF);
00228     CS=1;
00229 //    wait_ms(2);
00230     
00231     CS=0;
00232     adf.write(0x09);
00233     adf.write(0x24);   // TX_BASE ADDRESS   0x20(starting Tx Byte)
00234     adf.write(0x20);
00235     CS=1;
00236 //    wait_ms(2);
00237     
00238     CS=0;
00239     adf.write(0x09);
00240     adf.write(0x37);// BUFFER SIZE 0xE0=224 Bytes 0x137 is adress of buffer size
00241     adf.write(0xE0);
00242     CS=1;
00243 //    wait_ms(2);
00244     
00245     CS=0;
00246     adf.write(0x09);
00247     adf.write(0x36);//BB_Tx_Buffer Signal when Buffer is half filled
00248     adf.write(0x70);//0x70 = 112 >> When Half buffer interrupt is given
00249     CS=1;
00250 //    wait_ms(2);
00251     
00252     
00253     CS=0;
00254     adf.write(0x09);
00255     adf.write(0x39);//BB_Tx_Buffer Signal when Buffer is half filled
00256     adf.write(0x10);//0x70 = 112 >> When Half buffer interrupt is given
00257     CS=1;
00258 //    wait_ms(2);    
00259     }
00260 
00261  
00262 
00263 void write_data(void){
00264    
00265 //    wait_ms(0);
00266     CS=0;
00267     adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF
00268     adf.write(0x36);//Address : 0x336
00269     adf.write(0xFF);//Put Low
00270     CS=1;
00271 //    wait_us(80);
00272     CS=0;
00273     static bool first_2_time = true;
00274     k=0;
00275  
00276     if(!first_2_time){
00277                if(d==1 && snd_tm.last_buffer==false){
00278 //                cout<<"3"<<ENDL;
00279                 adf.write(0x18);
00280                 adf.write(0x20);
00281                 while(k<112)
00282                 {
00283                 adf.write(snd_tm.transmit[k]);  
00284                 k++;
00285                 }
00286                 d=2;
00287                 }
00288                    
00289     
00290             else  if(d==2 && snd_tm.last_buffer==false){     
00291 //                    cout<<"4"<<ENDL;
00292                     adf.write(0x18);
00293                     adf.write(0x90);
00294                     while(k<112)
00295                     {
00296                         adf.write(snd_tm.transmit[k]);  
00297                         k++;
00298                     }
00299                     d=1;   
00300          
00301               }   
00302      
00303         
00304     }
00305  
00306     if(first_2_time){
00307         switch(c){
00308         case 1:  
00309 //           cout<<"1"<<ENDL;
00310                     adf.write(0x18);
00311                     adf.write(0x20);
00312                     while(k<112)
00313                     {
00314                     adf.write(snd_tm.transmit[k]);  
00315                     k++;
00316                     }
00317                     c++;
00318                     break;   
00319         
00320         case 2:    
00321 //         cout<<"2"<<ENDL;
00322                         adf.write(0x18);
00323                         adf.write(0x90);
00324                         while(k<112)
00325                         {
00326                             adf.write(snd_tm.transmit[k]);  
00327                             k++;
00328                         }
00329                         c++;
00330                         d=1;
00331                         first_2_time = false;
00332                      break;   
00333                      }   
00334             
00335      }
00336                  
00337      
00338             
00339       if(snd_tm.last_buffer){
00340                 CS=0;
00341                 adf.write(0x18);
00342                 adf.write(0x20);
00343                
00344                 while(k<112){
00345                     adf.write(snd_tm.transmit[k]);  
00346                     k++;
00347                 }
00348                 CS=1;
00349 //            //        
00350                 snd_tm.transmission_done=true;
00351                 //ticker.detach();
00352 //                wait_ms(60);
00353 //                CS=0;
00354 //                 adf.write(0xB1);
00355 //                CS=1;
00356 //                snd_tm.transmission_done=true;
00357         }
00358                           
00359      CS=1;
00360 //     wait_us(1);
00361     }
00362     
00363 
00364 
00365 void check()
00366     {   
00367      
00368      if(IRQ){
00369          if(snd_tm.transmission_done==false){
00370              write_data();
00371              snd_tm.transmit_data();
00372          }
00373          else{
00374                 ticker.detach();
00375                 CS=0;
00376                  adf.write(0xB1);
00377                 CS=1;
00378 //                snd_tm.transmission_done=true;
00379              }
00380          }
00381      }
00382 
00383 void send_data(void){
00384         
00385     CS=0;
00386     adf.write(0xBB);
00387     CS=1;
00388 //    wait_us(300); 
00389     
00390     CS=0;
00391     adf.write(0xFF);
00392     adf.write(0xFF);
00393     CS=1;
00394 //    wait_ms(2); 
00395     snd_tm.transmit_data();
00396 //    wait_ms(1);
00397     write_data();
00398 //    wait_ms(1);
00399     snd_tm.transmit_data();
00400 //    wait_ms(1);
00401     write_data();
00402     snd_tm.transmit_data();
00403 //    snd_tm.transmit_data();
00404 //    write_data();
00405 //  wait_ms(2);  
00406     CS=0;
00407     adf.write(0xB1);
00408     CS=1;
00409     wait_us(300); 
00410     
00411     CS=0;
00412     adf.write(0xFF);
00413     adf.write(0xFF);
00414     CS=1;
00415 //    wait_ms(2);  
00416     
00417     CS=0;
00418     adf.write(0xB5);
00419     CS=1;
00420     printf("b5 gien\n");
00421     wait_us(300);  
00422 //    int b5=t.read_us();
00423     CS=0;
00424     adf.write(0xFF);
00425     adf.write(0xFF);
00426     CS=1;
00427 //    wait_ms(2);
00428     
00429     ticker.attach_us(&check,32000);
00430     }
00431     
00432     void adf_callme(){
00433         adf.format(8,0);
00434         adf.frequency(1000000);
00435         printf("inside callme");
00436         bool state_0=0;
00437         adf.write(CMD_PHY_OFF); //0xB0
00438         while(hw_reset_err_cnt<2){
00439         
00440             if(!assrt_phy_off(0,0,0)){  //assrt_phy_off () returns 0 if state is PHY_OFF , returns 1 if couldn't go to PHY_OFF
00441                 bbram_write();
00442                 bbram_flag=1;
00443                 break;
00444             }
00445             else{
00446                 hardware_reset(0);  // Asserts Hardware for 20sec(20times). PHY_OFF for 20,000 times
00447                 hw_reset_err_cnt++;
00448             }
00449         }
00450         state_0=assrt_phy_off(0,0,0);// We actually do not need this but make sure "we do not need this"
00451         if(!bbram_flag){
00452         //Switch to beacon
00453             bcn_flag=1;
00454          }
00455     
00456 //        bbram_write();
00457         initiate();
00458         send_data();
00459     
00460         while(!snd_tm.transmission_done)
00461         {
00462                 led2=!led2;
00463                 wait(.1);
00464         }
00465     }