pl ack in tmtc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by shubham c

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers adf.h Source File

adf.h

00001 //without reset feature , with state checks.
00002 InterruptIn IRQ(ADF_IRQ);
00003 //Ticker ticker;
00004  
00005 bool loop_on;
00006 bool ADF_off;
00007 bool buffer_state;
00008 bool finish_write_data;
00009 uint8_t signal = 0x00;
00010 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};
00011  
00012 //int initialise_card();
00013 //int disk_initialize();
00014  
00015 #define bbram_write {\
00016      SPI_mutex.lock();\
00017     gCS_ADF=0;\
00018     spi.write(0xB0);\
00019     wait_us(300);\
00020     gCS_ADF=1;\
00021     gCS_ADF=0;\
00022     for(int i=0;i<66;i++){\
00023         spi.write(bbram_buffer[i]);\
00024     }\
00025     gCS_ADF=1;\
00026     SPI_mutex.unlock();\
00027 }
00028 //------------------------------------------------------------------------
00029 // state checking functions
00030 //bool assrt_phy_off( int, int, int);
00031 //bool assrt_phy_on( int,int,int);
00032 //bool assrt_phy_tx(int,int,int);
00033  
00034 #define START_ADDRESS 0x020;
00035 #define MISO_PIN PTE3
00036 /**************Defining Counter Limits**************/
00037 #define THRS 20
00038 #define STATE_ERR_THRS 20
00039 #define PHY_OFF_EXEC_TIME 300
00040 #define PHY_ON_EXEC_TIME 300
00041 #define PHY_TX_EXEC_TIME 600
00042 /******DEFINING COMMANDS*********/
00043 #define CMD_HW_RESET 0xC8
00044 #define CMD_PHY_ON 0xB1
00045 #define CMD_PHY_OFF 0xB0
00046 #define CMD_PHY_TX 0xB5
00047 #define CMD_CONFIG_DEV 0xBB
00048  
00049 #define check_status {\
00050     unsigned char stat=0;\
00051     gCS_ADF=0;\
00052     spi.write(0xFF);\
00053     stat = spi.write(0xFF);\
00054     gCS_ADF=1;\
00055     status = stat;\
00056 }
00057  
00058 // all three arguments are int
00059 #define assrt_phy_off(return_this) {\
00060     int cmd_err_cnt = 0;\
00061     int spi_err_cnt = 0;\
00062     int state_err_cnt = 0;\
00063     for(int i = 0 ; i < 40 ;i++){\
00064         check_status;\
00065         if(status == 0xB1){\
00066             return_this = 0;\
00067             break;\
00068         }\
00069         else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){\
00070             return_this = 1;\
00071             break;\
00072         }\
00073         else if(state_err_cnt>STATE_ERR_THRS){\
00074             return_this = 1;\
00075             break;\
00076         }\
00077         else if( (status & 0xA0) == 0xA0 ){\
00078             gCS_ADF=0;\
00079             spi.write(CMD_PHY_OFF);\
00080             gCS_ADF=1;\
00081             wait_us(PHY_OFF_EXEC_TIME);\
00082             state_err_cnt++;\
00083         }\
00084         else if(status&0x80==0x00){\
00085             wait_ms(5);\
00086             spi_err_cnt++;\
00087         }\
00088         else {\
00089             wait_ms(1);\
00090             cmd_err_cnt++;\
00091         }\
00092     }\
00093 }
00094  
00095  
00096 #define initial_adf_check {\
00097     spi.write(CMD_PHY_OFF);\
00098     int tempReturn = 0;\
00099     bool flag = false;\
00100     while( hw_reset_err_cnt < 2 ){\
00101         assrt_phy_off( tempReturn);\
00102         if( !tempReturn ){\
00103             bbram_write;\
00104             bbram_flag=1;\
00105             flag = true;\
00106             break;\
00107         }\
00108         else{\
00109             hardware_reset(0);\
00110             hw_reset_err_cnt++;\
00111             /*gPC.puts("Resetting hardware\r\n");*/\
00112         }\
00113     }\
00114     if( flag == false ){\
00115         /*gPC.puts("Seems to be SPI problem\r\n");*/\
00116     }\
00117     assrt_phy_off(tempReturn);\
00118     if(!bbram_flag){\
00119         bcn_flag=1;\
00120      }\
00121 }
00122  
00123 unsigned char status =0;
00124 unsigned int cmd_err_cnt=0;
00125 unsigned int state_err_cnt=0;
00126 unsigned int miso_err_cnt=0;
00127 unsigned int hw_reset_err_cnt=0;
00128 bool bcn_flag=0;
00129 bool bbram_flag=0;
00130  
00131 bool hardware_reset(int bcn_call){
00132     for(int i= 0; i < 20 ; i++){
00133         gCS_ADF=0;
00134         spi.write(CMD_HW_RESET);
00135         gCS_ADF=1;
00136         wait_ms(2);// Typically 1 ms
00137         int count=0;
00138         int temp_return = 0;
00139         while(count<10 && miso_err_cnt<10){      
00140             if(MISO_PIN){
00141                 assrt_phy_off(temp_return);
00142                 if(!temp_return){
00143                     return 0;
00144                 }
00145                 count++;
00146             }
00147             else{
00148                 wait_us(50);
00149                 miso_err_cnt++;
00150             }
00151         }
00152     }
00153     return 1;
00154 }
00155  
00156 //for reseting the transmission call assert function after b5 and b1. after b1 assert_phi_on and after b5 assert_phi_tx. 
00157 //----------------------------------------------------------------------------
00158  
00159 # define initiate {\
00160     SPI_mutex.lock();\
00161     gCS_ADF=0;\
00162     spi.write(0xFF);\
00163     spi.write(0xFF);\
00164     gCS_ADF=1;\
00165     gCS_ADF=0;\
00166     spi.write(0x08);\
00167     spi.write(0x14);\
00168     spi.write(0xFF);\
00169     gCS_ADF=1;\
00170     gCS_ADF=0;\
00171     spi.write(0x08);\
00172     spi.write(0x15);\
00173     spi.write(0xFF);\
00174     gCS_ADF=1;\
00175     gCS_ADF=0;\
00176     spi.write(0x09);\
00177     spi.write(0x24);\
00178     spi.write(0x20);\
00179     gCS_ADF=1;\
00180     gCS_ADF=0;\
00181     spi.write(0x09);\
00182     spi.write(0x37);\
00183     spi.write(0xE0);\
00184     gCS_ADF=1;\
00185     gCS_ADF=0;\
00186     spi.write(0x09);\
00187     spi.write(0x36);\
00188     spi.write(0x70);\
00189     gCS_ADF=1;\
00190     gCS_ADF=0;\
00191     spi.write(0x09);\
00192     spi.write(0x39);\
00193     spi.write(0x10);\
00194     gCS_ADF=1;\
00195     gCS_ADF=0;\
00196     spi.write(0xBB);\
00197     gCS_ADF=1;\
00198     gCS_ADF=0;\
00199     spi.write(0xFF);\
00200     spi.write(0xFF);\
00201     gCS_ADF=1;\
00202     SPI_mutex.unlock();\
00203 }
00204  
00205 #define write_data {\
00206     SPI_mutex.lock();\
00207     gCS_ADF=0;\
00208     spi.write(0x0B);\
00209     spi.write(0x36);\
00210     spi.write(0xFF);\
00211     gCS_ADF=1;\
00212     gCS_ADF=0;\
00213     if(buffer_state){\
00214         spi.write(0x18);\
00215         spi.write(0x20);\
00216         for(unsigned char i=0; i<112;i++){\
00217             spi.write(buffer_112[i]);\
00218         }\
00219     }\
00220     else{\
00221         spi.write(0x18);\
00222         spi.write(0x90);\
00223         for(unsigned char i=0; i<112;i++){\
00224             spi.write(buffer_112[i]);\
00225         }\
00226     }\
00227     gCS_ADF=1;\
00228     SPI_mutex.unlock();\
00229     buffer_state = !buffer_state;\
00230     if(last_buffer){\
00231         finish_write_data = true;\
00232         /*gPC.puts("adf_off\r\n");*/\
00233     }\
00234 }
00235  
00236 /* 
00237 void check(){   
00238     if(IRQ){
00239         gCOM_MNG_TMTC_THREAD->signal_set(signal);
00240     }
00241 }*/
00242   
00243  
00244 #define send_data {\
00245 if(sent_tmfrom_SDcard){\
00246         send_tm_from_SD_card_fun();\
00247     }else{\
00248         snd_tm.transmit_data(buffer_112,&last_buffer);\
00249     }\
00250     write_data;\
00251     if(sent_tmfrom_SDcard){\
00252         send_tm_from_SD_card_fun();\
00253     }else{\
00254         snd_tm.transmit_data(buffer_112,&last_buffer);\
00255     }\
00256     write_data;\
00257     if(sent_tmfrom_SDcard){\
00258         send_tm_from_SD_card_fun();\
00259     }else{\
00260         snd_tm.transmit_data(buffer_112,&last_buffer);\
00261     }\
00262     SPI_mutex.lock();\
00263     gCS_ADF=0;\
00264     spi.write(0xB1);\
00265     gCS_ADF=1;\
00266     wait_us(300);\
00267     gCS_ADF=0;\
00268     spi.write(0xFF);\
00269     spi.write(0xFF);\
00270     gCS_ADF=1;\
00271     gCS_ADF=0;\
00272     spi.write(0xB5);\
00273     gCS_ADF=1;\
00274     wait_us(300);\
00275     gCS_ADF=0;\
00276     spi.write(0xFF);\
00277     spi.write(0xFF);\
00278     gCS_ADF=1;\
00279     SPI_mutex.unlock();\
00280     /*ticker.attach_us(&check,32000);*/\
00281 }
00282  
00283 #define configure_adf {\
00284     finish_write_data = false;\
00285     buffer_state = true;\
00286     last_buffer = false;\
00287     loop_on = true;\
00288     ADF_off = false;\
00289     initial_adf_check;\
00290     /*gPC.puts("initial adf check\r\n");*/\
00291     initiate;\
00292     /*gPC.puts("adf configured\r\n");*/\
00293     /*gLEDR = !gLEDR;*/\
00294 }
00295  
00296 #define transmit_adf {\
00297     configure_adf;\
00298     if(sent_tmfrom_SDcard)\
00299     signal = COM_MNG_TMTC_SIGNAL_ADF_SD;\
00300     else signal = COM_MNG_TMTC_SIGNAL_ADF_NSD;\
00301     send_data;\
00302     while(loop_on){\
00303         wait_ms(COM_TX_TICKER_LIMIT);\
00304         if(IRQ || bypass_adf){\
00305             if(finish_write_data){\
00306                 if(ADF_off){\
00307                     SPI_mutex.lock();\ 
00308                     gCS_ADF=0;\
00309                     spi.write(0xB1);\
00310                     gCS_ADF=1;\
00311                     SPI_mutex.unlock();\
00312                     loop_on = false;\
00313                 }\
00314                 else{\
00315                     ADF_off = true;\
00316                 }\
00317             }else{\ 
00318                 gLEDG = !gLEDG;\  
00319                 write_data;\
00320                 if(sent_tmfrom_SDcard)\
00321                 send_tm_from_SD_card_fun();\
00322                 else snd_tm.transmit_data(buffer_112,&last_buffer);\ 
00323             }\
00324         }\
00325     }\
00326     /*gPC.puts("after while loop\r\n");*/\
00327 }
00328