////////////////////////////////////////////////////////////////////////// // Homemade GPS Receiver // Copyright (C) 2013 Andrew Holme // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // http://www.aholme.co.uk/GPS/Main.htm ////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include "gps.h" #include "spi.h" #include "ephemeris.h" const int PWR_LEN = 8; const int MAX_BITS = 64; struct UPLOAD { // Embedded CPU CHANNEL structure uint16_t nav_ms; // Milliseconds 0 ... 19 uint16_t nav_bits; // Bit count uint16_t nav_glitch; // Glitch count uint16_t nav_prev; // Last data bit uint16_t nav_buf[MAX_BITS/16]; // NAV data buffer uint16_t ca_freq[4]; // Loop filter integrator uint16_t lo_freq[4]; // Loop filter integrator int16_t iq[2]; // Last I, Q samples uint16_t ca_gain[2]; // Code loop ki, kp uint16_t lo_gain[2]; // Carrier loop ki, kp }; struct CHANNEL { // Locally-held channel data UPLOAD ul; // Copy of embedded CPU channel state float pwr_tot, pwr[PWR_LEN]; // Running average of signal power int pwr_pos; // Circular counter int gain_adj; // AGC int ch, sv; // Association int probation; // Temporarily disables use if channel noisy int holding, rd_pos; // NAV data bit counters void Reset(); void Start(int sv, int t_sample, int taps, int lo_shift, int ca_shift); void SetGainAdj(int); int GetGainAdj(); void CheckPower(); float GetPower(); void Service(); void Acquisition(); void Tracking(); void SignalLost(); void UploadEmbeddedState(); int ParityCheck(char *buf, int *nbits); void Subframe(char *buf); void Status(); int RemoteBits(uint16_t wr_pos); bool GetSnapshot(uint16_t wr_pos, int *p_sv, int *p_bits, float *p_pwr); }; static CHANNEL Chans[NUM_CHANS]; static unsigned BusyFlags; /////////////////////////////////////////////////////////////////////////////////////////////// static double GetFreq(uint16_t *u) { // Convert NCO command to Hertz return ( u[0] * pow(2, -64) + u[1] * pow(2, -48) + u[2] * pow(2, -32) + u[3] * pow(2, -16)) * FS; } /////////////////////////////////////////////////////////////////////////////////////////////// const char preambleUpright [] = {1,0,0,0,1,0,1,1}; const char preambleInverse [] = {0,1,1,1,0,1,0,0}; static int parity(char *p, char *word, char D29, char D30) { char *d = word-1; for (int i=1; i<25; i++) d[i] ^= D30; p[0] = D29 ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[6] ^ d[10] ^ d[11] ^ d[12] ^ d[13] ^ d[14] ^ d[17] ^ d[18] ^ d[20] ^ d[23]; p[1] = D30 ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[7] ^ d[11] ^ d[12] ^ d[13] ^ d[14] ^ d[15] ^ d[18] ^ d[19] ^ d[21] ^ d[24]; p[2] = D29 ^ d[1] ^ d[3] ^ d[4] ^ d[5] ^ d[7] ^ d[8] ^ d[12] ^ d[13] ^ d[14] ^ d[15] ^ d[16] ^ d[19] ^ d[20] ^ d[22]; p[3] = D30 ^ d[2] ^ d[4] ^ d[5] ^ d[6] ^ d[8] ^ d[9] ^ d[13] ^ d[14] ^ d[15] ^ d[16] ^ d[17] ^ d[20] ^ d[21] ^ d[23]; p[4] = D30 ^ d[1] ^ d[3] ^ d[5] ^ d[6] ^ d[7] ^ d[9] ^ d[10] ^ d[14] ^ d[15] ^ d[16] ^ d[17] ^ d[18] ^ d[21] ^ d[22] ^ d[24]; p[5] = D29 ^ d[3] ^ d[5] ^ d[6] ^ d[8] ^ d[9] ^ d[10] ^ d[11] ^ d[13] ^ d[15] ^ d[19] ^ d[22] ^ d[23] ^ d[24]; return memcmp(d+25, p, 6); } /////////////////////////////////////////////////////////////////////////////////////////////// void CHANNEL::UploadEmbeddedState() { SPI_MISO miso; spi_get(CmdGetChan, &miso, sizeof(ul), ch); memcpy(&ul, miso.byte, sizeof(ul)); } /////////////////////////////////////////////////////////////////////////////////////////////// int CHANNEL::GetGainAdj() { return gain_adj; } void CHANNEL::SetGainAdj(int adj) { gain_adj = adj; int lo_ki = 20 + gain_adj; int lo_kp = 27 + gain_adj; spi_set(CmdSetGainLO, ch, lo_ki + ((lo_kp-lo_ki)<<16)); } /////////////////////////////////////////////////////////////////////////////////////////////// void CHANNEL::Reset() { uint32_t ca_rate = CPS/FS*powf(2,32); spi_set(CmdSetRateCA, ch, ca_rate); int ca_ki = 20-9; int ca_kp = 27-4; spi_set(CmdSetGainCA, ch, ca_ki + ((ca_kp-ca_ki)<<16)); SetGainAdj(0); memset(pwr, 0, sizeof pwr); pwr_tot=0; pwr_pos=0; probation=2; } /////////////////////////////////////////////////////////////////////////////////////////////// void CHANNEL::Start( // called on search thread to initiate acquisition int sv_, int t_sample, int taps, int lo_shift, int ca_shift) { this->sv = sv_; // Estimate Doppler from FFT bin shift double lo_dop = lo_shift*FS/FFT_LEN; double ca_dop = lo_dop/L1*CPS; // NCO rates uint32_t lo_rate = (FC + lo_dop)/FS*pow(2,32); uint32_t ca_rate = (CPS + ca_dop)/FS*pow(2,32); // Initialise code and carrier NCOs spi_set(CmdSetRateLO, ch, lo_rate); spi_set(CmdSetRateCA, ch, ca_rate); // Seconds elapsed since sample taken double secs = (Microseconds()-t_sample) / 1e6; // Code creep due to code rate Doppler ca_shift += nearbyint(ca_dop*secs*FS/CPS); // Align code generator by pausing NCO uint32_t ca_pause = (20000-ca_shift) % 10000; if (ca_pause) spi_set(CmdPause, ch, ca_pause-1); spi_set(CmdSetSV, ch, taps); // Gold Code taps // Wait 3 epochs to be sure phase errors are valid before ... TimerWait(3); // ... enabling embedded PI controllers spi_set(CmdSetMask, BusyFlags |= 1<>16) & 1; } rd_pos+=16; rd_pos&=MAX_BITS-1; } while (holding>=300) { // Enough for a subframe? int nbits; if (0==ParityCheck(buf, &nbits)) watchdog=0; memmove(buf, buf+nbits, holding-=nbits); } CheckPower(); } } /////////////////////////////////////////////////////////////////////////////////////////////// void CHANNEL::SignalLost() { // Disable embedded PI controllers BusyFlags &= ~(1<HYST_HI) SetGainAdj(-1); // half loop gain } UserStat(STAT_POWER, mean, ch); } float CHANNEL::GetPower() { return pwr_tot / PWR_LEN; } /////////////////////////////////////////////////////////////////////////////////////////////// // Debug static unsigned bin(char *s, int n) { unsigned u=*s; while(--n) u+=u+*++s; return u; } void CHANNEL::Subframe(char *buf) { char *sub = buf+49; char *tow = buf+30; int id; printf("sub %d tow %d ", id=bin(sub,3), bin(tow,17)); if (id>3) { int j, pg; for (pg=0, j=0; j<6; j++) pg += pg + buf[62+j]; printf("pg %2d", pg); } putchar('\n'); } void CHANNEL::Status() { double rssi = sqrt(GetPower()); double lo_f = GetFreq(ul.lo_freq) - FC; double ca_f = GetFreq(ul.ca_freq) - CPS; printf("chan %d PRN %2d rssi %4.0f adj %2d freq %5.0f %6.3f ", ch, sv+1, rssi, gain_adj, lo_f, ca_f); } /////////////////////////////////////////////////////////////////////////////////////////////// int CHANNEL::ParityCheck(char *buf, int *nbits) { char p[6]; // Upright or inverted preamble? Setting of parity bits resolves phase ambiguity. if (0==memcmp(buf, preambleUpright, 8)) p[4]=p[5]=0; else if (0==memcmp(buf, preambleInverse, 8)) p[4]=p[5]=1; else return *nbits=1; // Parity check up to ten 30-bit words ... for (int i=0; i<300; i+=30) { if (parity(p, buf+i, p[4], p[5])) { Status(); puts("parity"); probation=2; return *nbits=i+30; } } Status(); Subframe(buf); Ephemeris[sv].Subframe(buf); if (probation) probation--; *nbits=300; return 0; } /////////////////////////////////////////////////////////////////////////////////////////////// bool CHANNEL::GetSnapshot( uint16_t wr_pos, // in: circular nav_buf pointer int *p_sv, // out: Ephemeris[] index int *p_bits, // out: total NAV bits held locally + remotely float *p_pwr) { // out: signal power for least-squares weighting if (probation) return false; // temporarily too noisy *p_sv = sv; *p_bits = holding + RemoteBits(wr_pos); *p_pwr = GetPower(); return true; // ok to use } bool ChanSnapshot( // called on solver thread int ch, uint16_t wr_pos, int *p_sv, int *p_bits, float *p_pwr) { if (BusyFlags&(1<