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_adf.h Source File

snd_tm_adf.h

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