QRSS Rx Network receiver. A receiver to sample a segment of RF spectrum and send this data to a server for further processing and display. NXP mbed Design Challenge entry (Honorable Mention). Published in Circuit Cellar, Feb 2012

Dependencies:   NetServices mbed DNSResolver

Committer:
claytong
Date:
Wed Jan 25 20:32:53 2012 +0000
Revision:
0:82ff15078322
1.0 (initial public release)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
claytong 0:82ff15078322 1 /*---------------------------------------------------------------------------
claytong 0:82ff15078322 2
claytong 0:82ff15078322 3 QRSS Receiver Application
claytong 0:82ff15078322 4
claytong 0:82ff15078322 5 by Clayton ZL3TKA/VK1TKA
claytong 0:82ff15078322 6 clayton@isnotcrazy.com
claytong 0:82ff15078322 7
claytong 0:82ff15078322 8 Header File for Communications Module
claytong 0:82ff15078322 9
claytong 0:82ff15078322 10 ---------------------------------------------------------------------------*/
claytong 0:82ff15078322 11 #ifndef _COMMS_H
claytong 0:82ff15078322 12 #define _COMMS_H
claytong 0:82ff15078322 13
claytong 0:82ff15078322 14 #include "mbed.h"
claytong 0:82ff15078322 15 #include "global.h"
claytong 0:82ff15078322 16 #include "EthernetNetIf.h"
claytong 0:82ff15078322 17 #include "UDPSocket.h"
claytong 0:82ff15078322 18 #include "dnsresolve.h"
claytong 0:82ff15078322 19 #include "BufferSys.h"
claytong 0:82ff15078322 20 #include "gps.h"
claytong 0:82ff15078322 21
claytong 0:82ff15078322 22 // Definitions
claytong 0:82ff15078322 23
claytong 0:82ff15078322 24 // UDP output data size - includes header
claytong 0:82ff15078322 25 #define COMMS_DATABUFF_HEADER (4+4)
claytong 0:82ff15078322 26 #define COMMS_DATABUFF_SIZE ((LPF_OUTPUTS_SIZE*SAMPLE_SET_SIZE*8)+COMMS_DATABUFF_HEADER)
claytong 0:82ff15078322 27
claytong 0:82ff15078322 28 // UDP Control Buffer Size
claytong 0:82ff15078322 29 #define COMMS_CNTRLBUFF_SIZE (60)
claytong 0:82ff15078322 30
claytong 0:82ff15078322 31 // Number of seconds between each poll message
claytong 0:82ff15078322 32 #define POLL_MSG_PERIOD (6)
claytong 0:82ff15078322 33
claytong 0:82ff15078322 34 // default poll period in absence of 1PPS (mSec)
claytong 0:82ff15078322 35 #define POLL_TIMEOUT (10000)
claytong 0:82ff15078322 36
claytong 0:82ff15078322 37 // time to stop sending data if no commands are received
claytong 0:82ff15078322 38 #define RUNNING_TIMEOUT (30000)
claytong 0:82ff15078322 39
claytong 0:82ff15078322 40 //
claytong 0:82ff15078322 41 // Message Encoding
claytong 0:82ff15078322 42 //
claytong 0:82ff15078322 43 #define COMM_POLL_MSG 10
claytong 0:82ff15078322 44 #define COMM_CMND_MSG 11
claytong 0:82ff15078322 45 #define COMM_DATA_MSG 12
claytong 0:82ff15078322 46 #define COMM_MSG_VERSION 1
claytong 0:82ff15078322 47 #define COMM_MSG_SW_VERSION 1
claytong 0:82ff15078322 48 #define COMM_MSG_HW_ID 1
claytong 0:82ff15078322 49 #define COMM_MSG_REF_CLOCK 96000000
claytong 0:82ff15078322 50 #define COMM_MSG_RF_MIXER 10125000
claytong 0:82ff15078322 51 #define COMM_MSG_SAMPLE_CLOCK 13500000
claytong 0:82ff15078322 52 #define COMM_MSG_SAMPLE_DIVIDER (384*64)
claytong 0:82ff15078322 53 #define COMM_MSG_NAME "QRSS_proto"
claytong 0:82ff15078322 54 #define COMM_MSGNAME_LEN 10
claytong 0:82ff15078322 55
claytong 0:82ff15078322 56 #define COMM_CMND_MSG_LENGTH (5*4)
claytong 0:82ff15078322 57
claytong 0:82ff15078322 58 // Macros
claytong 0:82ff15078322 59
claytong 0:82ff15078322 60 //
claytong 0:82ff15078322 61 // Classes
claytong 0:82ff15078322 62 //
claytong 0:82ff15078322 63
claytong 0:82ff15078322 64 //---------------------------------------------------------------------------
claytong 0:82ff15078322 65 //
claytong 0:82ff15078322 66 // Communications Class
claytong 0:82ff15078322 67 //
claytong 0:82ff15078322 68 class TCommunications
claytong 0:82ff15078322 69 {
claytong 0:82ff15078322 70 // create/destroy
claytong 0:82ff15078322 71 public:
claytong 0:82ff15078322 72 TCommunications() :
claytong 0:82ff15078322 73 EthernetUpLED( LED2 ),
claytong 0:82ff15078322 74 RunningLED( LED4 )
claytong 0:82ff15078322 75 {}
claytong 0:82ff15078322 76 ~TCommunications()
claytong 0:82ff15078322 77 {}
claytong 0:82ff15078322 78
claytong 0:82ff15078322 79 // Local Types - state machine state
claytong 0:82ff15078322 80 typedef enum {
claytong 0:82ff15078322 81 ST_START_ETH,
claytong 0:82ff15078322 82 ST_POLL_SERVER,
claytong 0:82ff15078322 83 ST_RUNNING
claytong 0:82ff15078322 84 } TCommsStates;
claytong 0:82ff15078322 85
claytong 0:82ff15078322 86 // API
claytong 0:82ff15078322 87 public:
claytong 0:82ff15078322 88 // Initialisation
claytong 0:82ff15078322 89 void Init();
claytong 0:82ff15078322 90
claytong 0:82ff15078322 91 // Set up Server DNS name
claytong 0:82ff15078322 92 void SetServer( char *szNm )
claytong 0:82ff15078322 93 {
claytong 0:82ff15078322 94 strncpy( szServerName, szNm, sizeof(szServerName) );
claytong 0:82ff15078322 95 szServerName[sizeof(szServerName)-1] = 0; // place terminator at end incase name was too long
claytong 0:82ff15078322 96 }
claytong 0:82ff15078322 97
claytong 0:82ff15078322 98 // Processing routine
claytong 0:82ff15078322 99 void Poll();
claytong 0:82ff15078322 100
claytong 0:82ff15078322 101 // RecordPPSTimestamps Method
claytong 0:82ff15078322 102 // Records timestamps of events (1PPS or capture of LO divider)
claytong 0:82ff15078322 103 void RecordPPSTimestamps( uint32_t uiPPSCapture, uint32_t uiLOscCapture,
claytong 0:82ff15078322 104 TGPSController::TGPSData &GPSInfo );
claytong 0:82ff15078322 105
claytong 0:82ff15078322 106 // Test if comms is ready for sample data (ie in RUNNING state)
claytong 0:82ff15078322 107 bool Running()
claytong 0:82ff15078322 108 { return (eState==ST_RUNNING); }
claytong 0:82ff15078322 109
claytong 0:82ff15078322 110 // Return NCO Inc setting received
claytong 0:82ff15078322 111 uint32_t NCOPhaseInc()
claytong 0:82ff15078322 112 { return uiNCOPhaseInc; }
claytong 0:82ff15078322 113
claytong 0:82ff15078322 114 // Return Test Mode setting received
claytong 0:82ff15078322 115 uint32_t TestMode()
claytong 0:82ff15078322 116 { return uiTestModeInc; }
claytong 0:82ff15078322 117
claytong 0:82ff15078322 118 // Pass sample data for sending. Send when buffer is full
claytong 0:82ff15078322 119 void SendSamples( uint32_t uiTimestamp, TDataSample sSample );
claytong 0:82ff15078322 120
claytong 0:82ff15078322 121
claytong 0:82ff15078322 122 // Private methods
claytong 0:82ff15078322 123 private:
claytong 0:82ff15078322 124 // Callback for incoming UDP data
claytong 0:82ff15078322 125 void onUDPSocketEvent( UDPSocketEvent evnt );
claytong 0:82ff15078322 126
claytong 0:82ff15078322 127 // Send a UDP Poll Message
claytong 0:82ff15078322 128 bool SendPollMessage();
claytong 0:82ff15078322 129
claytong 0:82ff15078322 130 // Place a word into a buffer - network byte order
claytong 0:82ff15078322 131 void WriteWord( uint8_t * &pucBuff, int32_t iWord, int &iLn )
claytong 0:82ff15078322 132 {
claytong 0:82ff15078322 133 *pucBuff = (uint8_t)(iWord>>24);
claytong 0:82ff15078322 134 pucBuff++;
claytong 0:82ff15078322 135 *pucBuff = (uint8_t)(iWord>>16);
claytong 0:82ff15078322 136 pucBuff++;
claytong 0:82ff15078322 137 *pucBuff = (uint8_t)(iWord>>8);
claytong 0:82ff15078322 138 pucBuff++;
claytong 0:82ff15078322 139 *pucBuff = (uint8_t)(iWord>>0);
claytong 0:82ff15078322 140 pucBuff++;
claytong 0:82ff15078322 141 iLn += 4;
claytong 0:82ff15078322 142 }
claytong 0:82ff15078322 143
claytong 0:82ff15078322 144 // Read a word from a buffer - network byte order
claytong 0:82ff15078322 145 uint32_t ReadWord( uint8_t * pucBuff )
claytong 0:82ff15078322 146 { return (0x1000000*pucBuff[0]) | (0x10000*pucBuff[1]) | (0x100*pucBuff[2]) | (pucBuff[3]); }
claytong 0:82ff15078322 147
claytong 0:82ff15078322 148 // data
claytong 0:82ff15078322 149 private:
claytong 0:82ff15078322 150 // LEDs
claytong 0:82ff15078322 151 DigitalOut EthernetUpLED;
claytong 0:82ff15078322 152 DigitalOut RunningLED;
claytong 0:82ff15078322 153
claytong 0:82ff15078322 154 // State machine state
claytong 0:82ff15078322 155 TCommsStates eState;
claytong 0:82ff15078322 156
claytong 0:82ff15078322 157 // Ethernet Interface
claytong 0:82ff15078322 158 EthernetNetIf eth;
claytong 0:82ff15078322 159
claytong 0:82ff15078322 160 // UDP Socket for comms
claytong 0:82ff15078322 161 char szServerName[50];
claytong 0:82ff15078322 162 UDPSocket ServerSocket;
claytong 0:82ff15078322 163 Host MyPort;
claytong 0:82ff15078322 164 Host ServerPort;
claytong 0:82ff15078322 165 IpAddr ServerAddr;
claytong 0:82ff15078322 166
claytong 0:82ff15078322 167 // DNS lookup
claytong 0:82ff15078322 168 DNSResolver dnsService;
claytong 0:82ff15078322 169
claytong 0:82ff15078322 170 // Host Details
claytong 0:82ff15078322 171
claytong 0:82ff15078322 172 // Output buffers - word align this buffer
claytong 0:82ff15078322 173 uint32_t auiDataBuffer[(COMMS_DATABUFF_SIZE/4)+8]; // the buffer - defined as words to ensure alignment
claytong 0:82ff15078322 174 uint8_t *pucBuffDataPtr; // buffer pointer - as a byte pointer
claytong 0:82ff15078322 175 int iBufferCount; // byte count
claytong 0:82ff15078322 176
claytong 0:82ff15078322 177 // Incoming UDP Data
claytong 0:82ff15078322 178 uint32_t uiCommand;
claytong 0:82ff15078322 179 uint32_t uiNCO;
claytong 0:82ff15078322 180 uint32_t uiTestOsc;
claytong 0:82ff15078322 181 bool bGotCmnd;
claytong 0:82ff15078322 182
claytong 0:82ff15078322 183 // PPS Timer Event Counter
claytong 0:82ff15078322 184 int iPPSCount;
claytong 0:82ff15078322 185
claytong 0:82ff15078322 186 // Send Poll Message timer
claytong 0:82ff15078322 187 Timer PollTimer;
claytong 0:82ff15078322 188 Timer CommandTimer;
claytong 0:82ff15078322 189
claytong 0:82ff15078322 190 // Timer Capture Data - queue of 8 captures. [0] is the most recent
claytong 0:82ff15078322 191 uint32_t auiPPSCaptures[8];
claytong 0:82ff15078322 192 uint32_t auiLOscCaptures[8];
claytong 0:82ff15078322 193
claytong 0:82ff15078322 194 // Last GPS info
claytong 0:82ff15078322 195 TGPSController::TGPSData LastGPSInfo;
claytong 0:82ff15078322 196
claytong 0:82ff15078322 197 // data from the command packet
claytong 0:82ff15078322 198 uint32_t uiNCOPhaseInc;
claytong 0:82ff15078322 199 uint32_t uiTestModeInc;
claytong 0:82ff15078322 200
claytong 0:82ff15078322 201 };
claytong 0:82ff15078322 202
claytong 0:82ff15078322 203 // declare the Communications module
claytong 0:82ff15078322 204 extern TCommunications Comms;
claytong 0:82ff15078322 205
claytong 0:82ff15078322 206 #endif
claytong 0:82ff15078322 207
claytong 0:82ff15078322 208 //---------------------------------------------------------------------------
claytong 0:82ff15078322 209 // END
claytong 0:82ff15078322 210 //---------------------------------------------------------------------------
claytong 0:82ff15078322 211