kcc cansat / Mbed 2 deprecated AC4790-200

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* This is a short test program for AC4790-200 transceiver*/
00002 /* We have radio's tx rx pin collected to mbed's p10 and p9 respectively, and have radio connected to external power 3.3v - 5v*, and GND
00003     only 4 pins are used from the radio*/
00004 #include "mbed.h"
00005 #define MAC_1 57
00006 #define MAC_2 147      //             MAC for relay
00007 #define MAC_3 98
00008 
00009 #define MAC_4 57
00010 #define MAC_5 149                     //MAC for GCS
00011 #define MAC_6 98
00012 
00013 Serial laird(p9, p10); //Creates a variable for serial comunication through pin 9 and 10
00014 Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer
00015 
00016 
00017 int enter_cmd();
00018 void get_respond();
00019 int exit_cmd();
00020 int api();
00021 int write_cb();
00022 void radio_send(char *to_send);
00023 void radio_received(char *received);
00024 void relay_send(char *to_send);
00025 
00026 int main() {
00027 
00028 
00029     int i;
00030     char to_send[30] = {"ACD,100"};             //120byte max in data field, 4 byte a char??TEST, 30 chars max!!
00031     char received[30] = {'\0'};  
00032 
00033     wait(3);
00034 /***************set up the serial interface parameters**********/
00035     laird.baud(57600);                      //set up the baud rate, needs to be the same as the baud rate of the radio
00036     laird.format(8, Serial::None, 1);       //set up the format, 8 bits, none parity, 1 stop bit
00037 
00038     
00039     wait(1);
00040 /*************enter command mode *****************/
00041 
00042     while(!enter_cmd()) ;    //send hex command to enter the command mode
00043     
00044     /*the supposed hex respond after enter the command mode : cc 43 4f 4d*/
00045     pc.printf("enter cmd respond: ");
00046     for( i = 0; i < 4; i++)
00047         get_respond();     
00048     wait_ms(500);
00049     pc.printf("\n\r");
00050     
00051  /*************change channel number**************/
00052     laird.putc('\xcc');
00053     laird.putc('\x01');
00054     laird.putc('\x10');
00055     
00056     pc.printf("change channle respond: ");
00057     get_respond();
00058     get_respond();
00059     pc.printf("\r\n");   
00060     
00061 /*************configure API control ****************/
00062     while(!api());              //send hex command to configure API control
00063     
00064     pc.printf("API control respond: ");         //expected respond: cc 13
00065     for( i = 0; i <2; i++)
00066         get_respond();
00067     wait_ms(500);
00068     pc.printf("\n\r");
00069     
00070 /************** EEPROM write control 1 byte********/
00071     while(!write_cb());
00072     
00073     pc.printf("write control byte respond: ");      //expected respond: 56 01 41
00074     for( i = 0; i <3; i++)
00075         get_respond();
00076     wait_ms(500);
00077     pc.printf("\n\r");
00078     
00079 /************read destination MAC address*******/
00080     laird.putc('\xcc');
00081     laird.putc('\x11');
00082     
00083     pc.printf("Destination MAC address respond: ");
00084     for(i = 0; i< 4; i++)
00085         get_respond();
00086     wait_ms(500);
00087     pc.printf("\n\r");
00088     
00089 /**************read own MAC address************/
00090     laird.putc('\xcc');
00091     laird.putc('\xc0');
00092     laird.putc('\x80');
00093     laird.putc('\x06');
00094     
00095     pc.printf("Own MAC address: ");
00096     for(i = 0; i < 9; i++)
00097         get_respond();
00098     wait_ms(500);
00099     pc.printf("\n\r");
00100         
00101 /************write destination MAC address*********/
00102 
00103 /*
00104     laird.putc('\xcc');         //ground station mac address: 61 78 32
00105     laird.putc('\x10');
00106     laird.putc(MAC_1);
00107     laird.putc(MAC_2);
00108     laird.putc(MAC_3);
00109    
00110     
00111     pc.printf("after write MAC respond: ");
00112     for(i = 0; i < 4; i++)
00113         get_respond();
00114     wait_ms(500);
00115     pc.printf("\n\r");
00116  */   
00117 /***************read DES key**********************/
00118     laird.putc('\xcc');
00119     laird.putc('\xc0');
00120     laird.putc('\xd0');
00121     laird.putc('\x07');
00122     
00123     pc.printf("DES key: ");
00124     for(i = 0; i < 10; i++)
00125         get_respond();
00126     wait_ms(500);
00127     pc.printf("\n\r");
00128     
00129 /**************exit command mode ******************/
00130     while(!exit_cmd());         //send hex command to exit the command mode
00131     
00132     /*the expected hex respond after exit the command mode: cc 44 41 54*/
00133     pc.printf("exit cmd respond: ");
00134     for( i = 0; i < 4; i++)
00135         get_respond();
00136     wait_ms(500);
00137     pc.printf("\n\r");
00138     
00139 /****************communication ****************/
00140 /*
00141 while(1){
00142     radio_send(to_send);
00143     wait(5);
00144 //    wait(1);
00145 //    pc.printf("done sending\r\n");
00146 }   
00147 */
00148 /*
00149 while(1){
00150     relay_send(to_send);
00151     wait(5);
00152     }
00153  */   
00154 /*    radio_received(received);
00155     for(i = 0; received[i] != '\0'; i++)
00156     {    
00157         pc.printf("%x ", received[i]);
00158         received[i] = '\0';
00159         
00160      }
00161      pc.printf("\r\n");
00162      */    
00163 
00164 while(1)
00165 {
00166     radio_received(received);
00167     for(i = 0; received[i] != '\0'; i++)
00168     {    
00169         pc.printf("%x ", received[i]);
00170         received[i] = '\0';
00171         
00172      }
00173      pc.printf("\r\n");
00174      
00175      wait(5);
00176      radio_send(to_send);
00177      pc.printf("reply send\r\n");
00178 }
00179 
00180 /*    
00181   
00182 while(1)
00183  {   
00184     radio_send(to_send);
00185     wait(1);
00186     pc.printf("done sending\r\n");
00187   }  
00188   */
00189 
00190 /*    
00191     while(1)
00192     {
00193         i = 0;
00194 
00195         pc.printf("done1 ");
00196         radio_received(received);
00197         pc.printf("done2 ");
00198         while(received[i] != '\0')
00199             pc.putc( received[i++] );
00200         wait(1);
00201                 for(i = 0; i < 30; i++)
00202             received[i] = '\0';
00203     } 
00204 */
00205 }
00206 
00207 
00208 
00209 /*************funtions used with radio****************/
00210 int enter_cmd()
00211 {
00212             if (laird.writeable()) {
00213             //enter cmd mode, hex value: 0x41 0x54 0x2B 0x2B 0x2B 0x0D          
00214             laird.putc('\x41');
00215             laird.putc('\x54');
00216             laird.putc('\x2b');
00217             laird.putc('\x2b');
00218             laird.putc('\x2b');
00219             laird.putc('\x0d');
00220             return 1;
00221             }
00222             else return 0;
00223 }
00224 
00225 void get_respond()
00226 {
00227     while(laird.readable() == 0);           //wait until there is char to read
00228     pc.printf("%x ", laird.getc());         //print out the char in hex form
00229 }    
00230 
00231 int exit_cmd()
00232 {
00233            //exit cmd mode, hex value: 0xcc 0x41 0x54 0x4f 0x0d
00234             if(laird.writeable()) 
00235            {            
00236             laird.putc('\xcc');
00237             laird.putc('\x41');
00238             laird.putc('\x54');
00239             laird.putc('\x4f');
00240             laird.putc('\x0d');
00241             return 1;
00242            }
00243            
00244            else return 0;
00245     
00246 }
00247 
00248 int api()
00249 {
00250             if(laird.writeable()) 
00251            {            
00252             laird.putc('\xcc');
00253             laird.putc('\x17');
00254             laird.putc('\x13');
00255             return 1;
00256            }
00257            
00258            else return 0;
00259 }
00260 
00261 int write_cb()
00262 {
00263            if(laird.writeable()) 
00264            {            
00265             laird.putc('\xcc');
00266             laird.putc('\xc1');
00267             laird.putc('\x56');
00268             laird.putc('\x01');
00269             laird.putc('\x41');
00270             return 1;
00271            }
00272            
00273            else return 0;
00274 }
00275 
00276 //function used to send data over the radio, 
00277 void radio_send(char *to_send)
00278 {
00279     int i = 0;
00280     for( i = 0; to_send[i] != '\0'; i++);
00281     
00282     laird.putc('\x81');
00283     laird.putc(i);   
00284     laird.putc(8);
00285     laird.putc(4); 
00286     laird.putc(MAC_4);
00287     laird.putc(MAC_5);
00288     laird.putc(MAC_6);
00289     
00290     for( i = 0; to_send[i] != '\0'; i++)
00291        laird.putc(to_send[i]);
00292     pc.printf("sent");
00293 }
00294 
00295 void relay_send(char *to_send)
00296 {
00297     int i = 0;
00298     for( i = 0; to_send[i] != '\0'; i++);
00299 
00300     laird.putc('\x81');
00301     laird.putc(i + 3);
00302     laird.putc(8);
00303     laird.putc(4);
00304     laird.putc(MAC_1);
00305     laird.putc(MAC_2);
00306     laird.putc(MAC_3);
00307     laird.putc(MAC_4);
00308     laird.putc(MAC_5);
00309     laird.putc(MAC_6);
00310     for( i = 0; to_send[i] != '\0'; i++)
00311        laird.putc(to_send[i]);
00312 //    laird.putc('\0');
00313 //    pc.printf("sent");
00314 }
00315    
00316 
00317 void radio_received(char *received)
00318 {
00319     int size = 0;
00320     int i = 0;
00321     
00322     while(laird.readable() == 0);
00323 
00324     if( laird.getc() == '\x81')
00325     {
00326  
00327         size = laird.getc();           
00328         //throw away next 5 chars
00329         pc.printf("%x ", laird.getc());
00330         pc.printf("%x ", laird.getc());
00331         pc.printf("%x ", laird.getc());
00332         pc.printf("%x ", laird.getc());
00333         pc.printf("%x ", laird.getc());
00334 
00335         for( i = 0; i < size; i++)
00336         {    received[i] = laird.getc();
00337 
00338             }
00339         received[i + 1] = '\0';             //used to mark the ending of data
00340     } 
00341  
00342 }