Advertisement
redsees

Untitled

Mar 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. /*
  2.  *  AX.25 protocol frame structure:
  3.  *  -------------------------------
  4.  *
  5.  *  Flag Field: 8 bits (1 byte)
  6.  *  Header Field: 24 bits (3 bytes)
  7.  *      {
  8.  *    Destination Address: 8 bits (1 bytes) *
  9.  *    Source      Address: 8 bits (1 bytes) *
  10.  *    Control Bits       : 8  bits (1 byte ) * Control Bits will be  (length of data-1) .
  11.  *  }
  12.  *  Data Field: 32-2048 bits (4-256 bytes)
  13.  *  Frame Checksum Field: 16 bits (2 bytes)
  14.  *  Flag Field: 8 bits (1 byte)
  15.  *
  16.  */
  17.  
  18. #include <Crc16.h>
  19. Crc16 crc;
  20.  
  21. unsigned char AX25_Flag = 0x7E;    // flag = 0b01111110;
  22. unsigned char AX25_Header[3]  = {0xAA,0xBB,0}; //0xAA is the GND Station Adress - 0xBB is the Sourse Address - the third byte will be data length-1
  23. // Data is 7 bytes long, with 2 authentication bytes
  24. unsigned char AX25_Data[9]   = {0};
  25. unsigned char AX25_CheckSum[2] = {0};
  26.  
  27. // Authentication Key
  28. unsigned char Auth_Key[2] = {0xAB, 0xCD};
  29.  
  30. // LUK Counter
  31. unsigned char LUK = 0;
  32.  
  33. int inByte = 0;
  34. int command= 0;
  35. unsigned short value = 0;
  36. int serav = 0;
  37.  
  38. void setup()
  39. {
  40.   // Serial1 : TX pin 18, RX pin 19
  41.   // Serial2 : TX pin 16, RX pin 17
  42.   // Serial1 Ground Station Interface
  43.   // Serial2 On Board Computer interface
  44.   // Serial3 Peripheral Interface
  45.  
  46.   Serial1.begin(9600);
  47.   Serial2.begin(9600);
  48.   Serial3.begin(9600);
  49.  
  50. }
  51.  
  52. void loop()
  53. {
  54.  
  55.   // Wait until Command is received on Serial2 port
  56.   while ( !(Serial1.available()) );
  57.  
  58.   // Read received data
  59.   Serial1.readBytes(AX25_Flag,1);
  60.   Serial1.readBytes(AX25_Header,3);
  61.   Serial1.readBytes(AX25_Data,9);
  62.   Serial1.readBytes(AX25_CheckSum,2);
  63.  
  64.   // CRC Check on the received command without the two authentication bytes
  65.   crc.clearCrc();
  66.   crc.updateCrc((int)(AX25_Data >> 16));
  67.   value = crc.getCrc();
  68.   if ( value != AX25_CheckSum )
  69.   {
  70.       // Send Ground station 'fail' flag
  71.       AX25_Data = {'F','A','I','L',0,0,0,0,0}
  72.       Serial1.write(AX25_Flag);
  73.       Serial1.write(AX25_Header,3);
  74.       Serial1.write(AX25_Data,9);
  75.       Serial1.write(AX25_CheckSum,2);
  76.      
  77.       // Freeze afterward
  78.       while(1);
  79.   }
  80.  
  81.   // Authentication Check on the received data
  82.   if ( (AX25_Data & 0xFFFF) ^ Auth_Key < LUK++ )
  83.   {
  84.       // Send Ground station 'fail' flag
  85.       AX25_Data = {'F','A','I','L',0,0,0,0,0}
  86.       Serial1.write(AX25_Flag);
  87.       Serial1.write(AX25_Header,3);
  88.       Serial1.write(AX25_Data,9);
  89.       Serial1.write(AX25_CheckSum,2);
  90.      
  91.       // Freeze afterward
  92.       while(1);
  93.   }
  94.  
  95.   // Clear the two authentication bytes
  96.   AX25_Data[8] = 0x00;
  97.   AX25_Data[7] = 0x00;
  98.  
  99.   // Send data to OBC
  100.   Serial1.write(AX25_Flag);
  101.   Serial1.write(AX25_Header,3);
  102.   Serial1.write(AX25_Data,9);
  103.   Serial1.write(AX25_CheckSum,2);
  104.  
  105.   // Wait until either Serial2 or Serial3 is available
  106.  
  107.   while (1)
  108.   {
  109.       if ( Serial2.available() )
  110.       {
  111.           serav = 2;
  112.           break;
  113.       }
  114.       if ( Serial3.available() )
  115.       {
  116.           serav = 3;
  117.           break;
  118.       }
  119.   }
  120.  
  121.   // Receive data from the opened one
  122.   if (serav == 2)
  123.   {
  124.       Serial2.readBytes(AX25_Flag,1);
  125.       Serial2.readBytes(AX25_Header,3);
  126.       Serial2.readBytes(AX25_Data,9);
  127.       Serial2.readBytes(AX25_CheckSum,2);
  128.   }
  129.   else
  130.   {
  131.       Serial3.readBytes(AX25_Flag,1);
  132.       Serial3.readBytes(AX25_Header,3);
  133.       Serial3.readBytes(AX25_Data,9);
  134.       Serial3.readBytes(AX25_CheckSum,2);
  135.   }
  136.  
  137.   // CRC check on data
  138.   crc.clearCrc();
  139.   crc.updateCrc((int)(AX25_Data >> 16));
  140.   value = crc.getCrc();
  141.   if ( value != AX25_CheckSum )
  142.   {
  143.       // Send Ground station 'fail' flag
  144.       AX25_Data = {'F','A','I','L',0,0,0,0,0}
  145.       Serial1.write(AX25_Flag);
  146.       Serial1.write(AX25_Header,3);
  147.       Serial1.write(AX25_Data,9);
  148.       Serial1.write(AX25_CheckSum,2);
  149.      
  150.       // Freeze afterward
  151.       while(1);
  152.   }
  153.  
  154.   // Send to Ground station
  155.   Serial1.write(AX25_Flag);
  156.   Serial1.write(AX25_Header,3);
  157.   Serial1.write(AX25_Data,9);
  158.   Serial1.write(AX25_CheckSum,2);
  159.  
  160.   // Wait until data is received on Serial2 port
  161.   delay(2000);
  162. }
  163.  
  164. //Check routine taken from
  165. //http://web.mit.edu/6.115/www/miscfiles/amulet/amulet-help/xmodem.htm
  166. int calcrc(char *ptr, int count)
  167. {
  168.     int  crc;
  169.     char i;
  170.     crc = 0;
  171.     while (--count >= 0)
  172.     {
  173.         crc = crc ^ (int) *ptr++ << 8;
  174.         i = 8;
  175.         do
  176.         {
  177.             if (crc & 0x8000)
  178.                 crc = crc << 1 ^ 0x1021;
  179.             else
  180.                 crc = crc << 1;
  181.         } while(--i);
  182.     }
  183.     return (crc);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement