/////////////////////////////////////////////////////////////////////////////////////////////// // Homemade GPS Receiver // Copyright (c) Andrew Holme 2011 // http://www.holmea.demon.co.uk/GPS/Main.htm /////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include "fftw3.h" /////////////////////////////////////////////////////////////////////////////////////////////// enum Commands { CmdMonitor, CmdSample, CmdSetMask, CmdSetRateCA, CmdSetRateLO, CmdSetGainCA, CmdSetGainLO, CmdSetSV, CmdPause, CmdSetVCO, CmdGetSamples, CmdGetChanData, CmdGetClocks, CmdLog, CmdGetLog }; /////////////////////////////////////////////////////////////////////////////////////////////// const int MAX_CHANS = 8; const int MAX_BITS = 64; struct CHANNEL { short ch_NAV_MS; // Milliseconds 0 ... 19 short ch_NAV_BITS; // Bit count short ch_NAV_PREV; // Last data bit short ch_NAV_BUF[MAX_BITS/16]; // NAV data buffer short ch_CA_FREQ[4]; // Loop filter integrator short ch_LO_FREQ[4]; // Loop filter integrator short ch_IQ[2]; short ch_CA_GAIN[2]; short ch_LO_GAIN[2]; }; /////////////////////////////////////////////////////////////////////////////////////////////// typedef struct { unsigned bits:10, ms: 5, g1:10, ca_phase:7; } REPLICA_OUT; typedef struct { unsigned ms:16, bits:16, g1:10, ca_phase:6; } REPLICA_IN; /////////////////////////////////////////////////////////////////////////////////////////////// #define BYPASS 0x3FFF #define USER1 0x3FC2 #define USER2 0x3FC3 #pragma comment(lib, "libCseJtag.lib") enum shiftMode { CSEJTAG_SHIFT_READ = 1, CSEJTAG_SHIFT_WRITE = 2, CSEJTAG_SHIFT_READWRITE = 3 }; enum exitState {CSEJTAG_RUN_TEST_IDLE = 1}; extern "C" { int CseJtag_session_create(void *messageRouterFn, int, int, int *); int CseJtag_session_destroy(int); int CseJtag_target_open(int, char *, void *, int, char **, char *); int CseJtag_target_close(int); int CseJtag_target_cleanLocks(int); int CseJtag_target_flush(int); int CseJtag_target_lock(int, int, int *); int CseJtag_target_unlock(int); int CseJtag_tap_shiftChainIR(int handle, int shiftMode, int exitState, void (*msgRouter)(void), int bitCount, void *in, void *out, int=0, int=0); int CseJtag_tap_shiftChainDR(int handle, int shiftMode, int exitState, void (*msgRouter)(void), int bitCount, void *in, void *out, int=0, int=0); } /////////////////////////////////////////////////////////////////////////////////////////////// int jt_handle, jt_err; char jt_scratch[16384]; // size >= 11268 void PASCAL silentInfo(int, int, int) {} void jt_open() { char *argv[] = {"port=USB2", "frequency=3000000"}; int argc=2, status; jt_err |= CseJtag_session_create(silentInfo, 0, 0, &jt_handle); jt_err |= CseJtag_target_cleanLocks(jt_handle); jt_err |= CseJtag_target_open(jt_handle, "xilinx_platformusb", 0, argc, argv, jt_scratch); jt_err |= CseJtag_target_lock(jt_handle, -1, &status); } void jt_close() { jt_err |= CseJtag_target_unlock(jt_handle); jt_err |= CseJtag_target_close(jt_handle); jt_err |= CseJtag_session_destroy(jt_handle); } void jt_scan_ir(int ir) { jt_err |= CseJtag_tap_shiftChainIR(jt_handle, CSEJTAG_SHIFT_READWRITE, CSEJTAG_RUN_TEST_IDLE, 0, 14, &ir, &ir); jt_err |= CseJtag_target_flush(jt_handle); } void jt_scan_dr(char *dr, int len) { jt_err |= CseJtag_tap_shiftChainDR(jt_handle, CSEJTAG_SHIFT_READWRITE, CSEJTAG_RUN_TEST_IDLE, 0, len, dr, dr); jt_err |= CseJtag_target_flush(jt_handle); int st = dr[0] & 0x3F; if (st!=0x1) printf("st = 0x%02x\n", st); } /////////////////////////////////////////////////////////////////////////////////////////////// char jt_dr[2049]; static union { char *jt_byte; short *jt_word; long *jt_long; CHANNEL *jt_chan; REPLICA_IN *jt_repl; }; void jt_set(short cmd, short wparam=0, long lparam=0) { jt_byte = jt_dr; *jt_word++ = cmd; *jt_word++ = wparam; *jt_long = lparam; jt_scan_dr(jt_dr, 1+16+16+32); } void jt_get(short cmd, int len) { jt_byte = jt_dr; *jt_word++ = cmd; jt_scan_dr(jt_dr, 1+8+len); jt_byte = jt_dr + 1; } /////////////////////////////////////////////////////////////////////////////////////////////// struct SVDATA { int PRN, Navstar, T1, T2; } SV[] = { 1, 63, 2, 6, 2, 56, 3, 7, 3, 37, 4, 8, 4, 35, 5, 9, 5, 64, 1, 9, 6, 36, 2, 10, 7, 62, 1, 8, 8, 44, 2, 9, 9, 33, 3, 10, 10, 38, 2, 3, 11, 46, 3, 4, 12, 59, 5, 6, 13, 43, 6, 7, 14, 49, 7, 8, 15, 60, 8, 9, 16, 51, 9, 10, 17, 57, 1, 4, 18, 50, 2, 5, 19, 54, 3, 6, 20, 47, 4, 7, 21, 52, 5, 8, 22, 53, 6, 9, 23, 55, 1, 3, 24, 23, 4, 6, 25, 24, 5, 7, 26, 26, 6, 8, 27, 27, 7, 9, 28, 48, 8, 10, 29, 61, 1, 6, 30, 39, 2, 7, 31, 58, 3, 8, 32, 22, 4, 9, }; struct CACODE { // GPS coarse acquisition (C/A) gold code generator. Sequence length = 1023. char g1[11], g2[11], *tap[2]; CACODE(int t0, int t1) { tap[0] = g2+t0; tap[1] = g2+t1; memset(g1+1, 1, 10); memset(g2+1, 1, 10); } int Chip() { return g1[10] ^ *tap[0] ^ *tap[1]; } void Clock() { g1[0] = g1[3] ^ g1[10]; g2[0] = g2[2] ^ g2[3] ^ g2[6] ^ g2[8] ^ g2[9] ^ g2[10]; memmove(g1+1, g1, 10); memmove(g2+1, g2, 10); } }; /////////////////////////////////////////////////////////////////////////////////////////////// const char preambleUpright [] = {1,0,0,0,1,0,1,1}; const char preambleInverse [] = {0,1,1,1,0,1,0,0}; 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); } /////////////////////////////////////////////////////////////////////////////////////////////// const int fc = 2600000; const int fs = 10000000; const int Len = 40000; fftw_complex code[Len], data[Len], prod[Len]; /////////////////////////////////////////////////////////////////////////////////////////////// void Sample(int chan) { static char x[Len]; int i, j, k; jt_set(CmdSetRateCA, chan, int(1.023/10*pow(2,32))); jt_set(CmdSample, chan); Sleep(20); jt_set(CmdGetSamples); for (i=j=0; i>=1) x[i++] = dr&1; } /////////////////////////////////////////////////////////////////////////////////////////////// const int lo_sin[] = {1,1,0,0}; const int lo_cos[] = {1,0,0,1}; double lo_freq = fc, lo_phase = 0, lo_rate = lo_freq/fs*4; for (i=0; i= 4) lo_phase -= 4; } fftw_plan p = fftw_plan_dft_1d(Len, data, data, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(p); fftw_destroy_plan(p); } /////////////////////////////////////////////////////////////////////////////////////////////// void Bargraph(SVDATA& sv, double& max_snr, int& max_snr_dop, int& max_snr_i) { printf("%-2d %4d %7.0f %8.1f %7.1f ", sv.PRN, sv.Navstar, max_snr_dop*double(fs)/Len, (max_snr_i*1023.0)/(fs/1000), max_snr); for (int i=int(max_snr)/10; i--; ) putchar('*'); putchar('\n'); } /////////////////////////////////////////////////////////////////////////////////////////////// // Cross correlation function void CrossCorrelate(SVDATA& sv, double& max_snr, int& max_snr_dop, int& max_snr_i) { CACODE ca(sv.T1, sv.T2); double ca_freq = 1023000, ca_phase = 0, ca_rate = ca_freq/fs; for (int i=0; i= 1.0) { ca_phase -= 1.0; ca.Clock(); if (ca_phase > 0) { double nxt = ca.Chip() ? -1.0 : 1.0; chip *= 1.0 - ca_phase; chip += nxt * ca_phase; } } code[i][0] = chip; code[i][1] = 0; } fftw_plan p = fftw_plan_dft_1d(Len, code, code, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(p); fftw_destroy_plan(p); /////////////////////////////////////////////////////////////////////////////////////////////// p = fftw_plan_dft_1d(Len, prod, prod, FFTW_BACKWARD, FFTW_ESTIMATE); for (int dop=-5000*Len/fs; dop<=5000*Len/fs; dop++) { double max_pwr=0, tot_pwr=0; int max_pwr_i; /////////////////////////////////////////////////////////////////////////////////////////////// // (a-ib)(x+iy) = (ax+by) + i(ay-bx) for (i=0; imax_pwr) max_pwr=pwr, max_pwr_i=i; tot_pwr += pwr; } double ave_pwr = tot_pwr/i; double snr = max_pwr/ave_pwr; if (snr>max_snr) max_snr=snr, max_snr_dop=dop, max_snr_i=max_pwr_i; } fftw_destroy_plan(p); } /////////////////////////////////////////////////////////////////////////////////////////////// void Search() { Sample(0); for (int sv=0; sv='0' && ch<='7') debug_chan = ch - '0'; // Viewed on 'scope & SA else if (ch=='f' || ch=='F') mask = -1; // Loop forever else if (ch=='m' || ch=='M') mask |= ((1<ch_NAV_BITS - pos[chan]) & ~0xF; while (avail) { int dr = jt_chan->ch_NAV_BUF[pos[chan]/16]; for (int j=0; j<16; j++) { dr<<=1; buf[chan][holding[chan]++] = (dr>>16) & 1; } avail-=16; pos[chan]+=16; pos[chan]&=MAX_BITS-1; } rssi[chan] = unsigned(sqrt(pow(jt_chan->ch_IQ[0],2) + pow(jt_chan->ch_IQ[1],2))); lo_f[chan] = nco_freq(jt_chan->ch_LO_FREQ) - 2.600e6; ca_f[chan] = nco_freq(jt_chan->ch_CA_FREQ) - 1.023e6; jt_chan++; while (holding[chan]>=300) { // Enough for a subframe? // Parity check up to ten 30-bit words ... for (int i=0;; i+=30) { if (i==0) { // Upright or inverted preamble? Setting of parity bits resolves phase ambiguity. if (0==memcmp(buf[chan], preambleUpright, 8)) p[4]=p[5]=0; else if (0==memcmp(buf[chan], preambleInverse, 8)) p[4]=p[5]=1; else { i=1; break; } } if (i==300) { // Valid subframe found for (int j=0; j<300; j++) buf[chan][j] += '0'; char sub[4], tow[18]; strncpy(sub, buf[chan]+49, 3)[ 3]=0; strncpy(tow, buf[chan]+30, 17)[17]=0; printf( "chan %d PRN %2d rssi %4d freq %5.0f %6.3f subframe %d tow %d ", chan, atoi(PRN[chan]), rssi[chan], lo_f[chan], ca_f[chan], bin(sub), bin(tow)); fprintf(fp, "chan %d PRN %2d subframe %s tow %s ", chan, atoi(PRN[chan]), sub, tow); int id = ((sub[0]<<2)+(sub[1]<<1)+sub[2]) & 7; if (id>3) { for (int sv=0, j=0; j<6; j++) sv += sv + buf[chan][62+j] - '0'; printf("sv %2d", sv); if (id==4 && sv==56) tally |= 1 << (chan + 3*MAX_CHANS); // Page 18 of subframe 4 contains ionospheric model } else tally |= 1 << (id-1+3*chan); fwrite(buf[chan], 300, 1, fp); fputc(putchar('\n'), fp); break; } if (parity(p, buf[chan]+i, p[4], p[5])) { i+=30; printf("chan %d PRN %2d rssi %4d freq %5.0f %6.3f parity\n", chan, atoi(PRN[chan]), rssi[chan], lo_f[chan], ca_f[chan]); break; } } memmove(buf[chan], buf[chan]+i, holding[chan]-=i); } } } jt_set(CmdGetClocks); for (int i=0; i<500; i++) { jt_get(CmdGetClocks, 16 + 8 * MAX_CHANS * sizeof REPLICA_IN); int srq = *jt_word++; union { REPLICA_OUT t_sv[MAX_CHANS]; unsigned char dr[1]; }; for (chan=0; chanms + (srq&1); t_sv[ chan].bits = ((MAX_BITS-1) & (jt_repl->bits - pos[chan])) + holding[chan]; t_sv[MAX_CHANS-1-chan].g1 = jt_repl->g1; t_sv[MAX_CHANS-1-chan].ca_phase = jt_repl->ca_phase << 1; jt_word+=3; srq>>=1; } fprintf(fp, "%d ", chans); for (int j=32; j--; ) fprintf(fp, "%02X", dr[j]); GetSystemTime(&st); double UTC = ((st.wDayOfWeek*24+st.wHour)*60+st.wMinute)*60+st.wSecond+st.wMilliseconds/1000.0; _ftime(&timebuffer); timeline = ctime(&timebuffer.time); fprintf(fp, " %0.3f %.19s.%03u %s", 16.0 + UTC, // GPS time = UTC + 16s timeline, timebuffer.millitm, &timeline[20]); } fclose(fp); fp = fopen("log.csv", "w"); jt_set(CmdGetLog); for (i=0; i<4; i++) { jt_get(CmdGetLog, 4096); for (int j=0; j<128; j+=chans) { for (int k=0; k