Clayton G / Mbed 2 deprecated QRSS_Rx

Dependencies:   NetServices mbed DNSResolver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers comms.h Source File

comms.h

00001 /*---------------------------------------------------------------------------
00002 
00003     QRSS Receiver Application
00004         
00005     by Clayton ZL3TKA/VK1TKA
00006     clayton@isnotcrazy.com
00007 
00008     Header File for Communications Module
00009 
00010 ---------------------------------------------------------------------------*/
00011 #ifndef _COMMS_H
00012 #define _COMMS_H
00013 
00014 #include "mbed.h"
00015 #include "global.h"
00016 #include "EthernetNetIf.h"
00017 #include "UDPSocket.h"
00018 #include "dnsresolve.h"
00019 #include "BufferSys.h"
00020 #include "gps.h"
00021 
00022 // Definitions
00023 
00024 // UDP output data size - includes header
00025 #define COMMS_DATABUFF_HEADER   (4+4)
00026 #define COMMS_DATABUFF_SIZE     ((LPF_OUTPUTS_SIZE*SAMPLE_SET_SIZE*8)+COMMS_DATABUFF_HEADER)
00027 
00028 // UDP Control Buffer Size
00029 #define COMMS_CNTRLBUFF_SIZE    (60)
00030 
00031 // Number of seconds between each poll message
00032 #define POLL_MSG_PERIOD         (6)
00033 
00034 // default poll period in absence of 1PPS (mSec)
00035 #define POLL_TIMEOUT            (10000)
00036 
00037 // time to stop sending data if no commands are received
00038 #define RUNNING_TIMEOUT         (30000)
00039 
00040 //
00041 //  Message Encoding
00042 //
00043 #define COMM_POLL_MSG               10
00044 #define COMM_CMND_MSG               11
00045 #define COMM_DATA_MSG               12
00046 #define COMM_MSG_VERSION            1
00047 #define COMM_MSG_SW_VERSION         1
00048 #define COMM_MSG_HW_ID              1
00049 #define COMM_MSG_REF_CLOCK          96000000
00050 #define COMM_MSG_RF_MIXER           10125000
00051 #define COMM_MSG_SAMPLE_CLOCK       13500000
00052 #define COMM_MSG_SAMPLE_DIVIDER     (384*64)
00053 #define COMM_MSG_NAME               "QRSS_proto"
00054 #define COMM_MSGNAME_LEN            10
00055 
00056 #define COMM_CMND_MSG_LENGTH        (5*4)
00057 
00058 // Macros
00059 
00060 //
00061 // Classes
00062 //
00063 
00064 //---------------------------------------------------------------------------
00065 //
00066 //  Communications Class
00067 //
00068 class TCommunications
00069 {
00070     // create/destroy
00071     public:
00072         TCommunications() :
00073             EthernetUpLED( LED2 ),
00074             RunningLED( LED4 )
00075             {}
00076         ~TCommunications() 
00077             {}
00078 
00079     // Local Types - state machine state
00080     typedef enum {
00081         ST_START_ETH,
00082         ST_POLL_SERVER,
00083         ST_RUNNING
00084         } TCommsStates;
00085 
00086     // API
00087     public:
00088         // Initialisation
00089         void Init();
00090 
00091         // Set up Server DNS name
00092         void SetServer( char *szNm )
00093             { 
00094                 strncpy( szServerName, szNm, sizeof(szServerName) ); 
00095                 szServerName[sizeof(szServerName)-1] = 0;   // place terminator at end incase name was too long
00096             }
00097 
00098         // Processing routine
00099         void Poll();
00100 
00101         //  RecordPPSTimestamps Method
00102         //      Records timestamps of events (1PPS or capture of LO divider)
00103         void RecordPPSTimestamps( uint32_t uiPPSCapture, uint32_t uiLOscCapture, 
00104                                   TGPSController::TGPSData &GPSInfo );
00105 
00106         // Test if comms is ready for sample data (ie in RUNNING state)
00107         bool Running()
00108             { return (eState==ST_RUNNING); }
00109 
00110         // Return NCO Inc setting received
00111         uint32_t NCOPhaseInc()
00112             { return uiNCOPhaseInc; }
00113 
00114         // Return Test Mode setting received
00115         uint32_t TestMode()
00116             { return uiTestModeInc; }
00117 
00118         //  Pass sample data for sending. Send when buffer is full
00119         void SendSamples( uint32_t uiTimestamp, TDataSample sSample );
00120 
00121 
00122     // Private methods
00123     private:
00124         // Callback for incoming UDP data
00125         void onUDPSocketEvent( UDPSocketEvent evnt );
00126 
00127         // Send a UDP Poll Message
00128         bool SendPollMessage();
00129 
00130         // Place a word into a buffer - network byte order
00131        void WriteWord( uint8_t * &pucBuff, int32_t iWord, int &iLn )
00132             {
00133                 *pucBuff = (uint8_t)(iWord>>24);
00134                 pucBuff++;
00135                 *pucBuff = (uint8_t)(iWord>>16);
00136                 pucBuff++;
00137                 *pucBuff = (uint8_t)(iWord>>8);
00138                 pucBuff++;
00139                 *pucBuff = (uint8_t)(iWord>>0);
00140                 pucBuff++;
00141                 iLn += 4;
00142             }
00143 
00144         // Read a word from a buffer - network byte order
00145        uint32_t ReadWord( uint8_t * pucBuff )
00146             { return (0x1000000*pucBuff[0]) | (0x10000*pucBuff[1]) | (0x100*pucBuff[2]) | (pucBuff[3]); }
00147 
00148     // data
00149     private:
00150         // LEDs
00151         DigitalOut EthernetUpLED;
00152         DigitalOut RunningLED;
00153 
00154         // State machine state
00155         TCommsStates    eState;
00156 
00157         // Ethernet Interface
00158         EthernetNetIf   eth;
00159         
00160         // UDP Socket for comms
00161         char            szServerName[50];
00162         UDPSocket       ServerSocket;
00163         Host            MyPort;
00164         Host            ServerPort;
00165         IpAddr          ServerAddr;
00166         
00167         // DNS lookup
00168         DNSResolver     dnsService;
00169         
00170         // Host Details
00171         
00172         // Output buffers - word align this buffer
00173         uint32_t        auiDataBuffer[(COMMS_DATABUFF_SIZE/4)+8];   // the buffer - defined as words to ensure alignment
00174         uint8_t        *pucBuffDataPtr;     // buffer pointer - as a byte pointer
00175         int             iBufferCount;       // byte count
00176 
00177         // Incoming UDP Data
00178         uint32_t        uiCommand;
00179         uint32_t        uiNCO;
00180         uint32_t        uiTestOsc;
00181         bool            bGotCmnd;
00182 
00183         // PPS Timer Event Counter
00184         int             iPPSCount;
00185     
00186         // Send Poll Message timer
00187         Timer           PollTimer;
00188         Timer           CommandTimer;
00189         
00190         // Timer Capture Data - queue of 8 captures. [0] is the most recent
00191         uint32_t        auiPPSCaptures[8];
00192         uint32_t        auiLOscCaptures[8];
00193 
00194         // Last GPS info        
00195         TGPSController::TGPSData    LastGPSInfo;
00196         
00197         // data from the command packet
00198         uint32_t        uiNCOPhaseInc;
00199         uint32_t        uiTestModeInc;
00200 
00201 };
00202 
00203 // declare the Communications module
00204 extern TCommunications Comms;
00205 
00206 #endif
00207 
00208 //---------------------------------------------------------------------------
00209 //  END
00210 //---------------------------------------------------------------------------
00211