A GPS disciplined clock

Dependencies:   net lpc1768 crypto clock web log

gps/gps.c

Committer:
andrewboyson
Date:
2019-10-21
Revision:
77:4e3925cbb1d8
Parent:
76:bea8fd22bccf
Child:
78:8a29cad1b131

File content as of revision 77:4e3925cbb1d8:

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>

#include "log.h"
#include "nmea.h"
#include "pps.h"
#include "clkgov.h"

bool GpsTrace   = true;
bool GpsVerbose = false;

void GpsLog(const char *fmt, ...)
{
    if (!GpsTrace) return;
    va_list argptr;
    va_start(argptr, fmt);
    LogTimeF("GPS %04d ", PpsMsSinceLastPulse);
    LogV(fmt, argptr);
    va_end(argptr);
}
void GpsLogVerbose(const char *fmt, ...)
{
    if (!GpsTrace || !GpsVerbose) return;
    va_list argptr;
    va_start(argptr, fmt);
    LogTimeF("GPS %04d ", PpsMsSinceLastPulse);
    LogV(fmt, argptr);
    va_end(argptr);
}

void GpsHadPps()
{
    if (!PpsIsStable()      ) return;
    if (!NmeaFixIsStable()  ) return;
    if (!NmeaModuleIsReady()) return;

    //Check NMEA is stable and that the last NMEA time message was not missed
    int msSinceLastNmeaTimeMsg = (int)(PpsLastPulseMs - NmeaTimeGetMs());
    if (NmeaTimeIsStable() && msSinceLastNmeaTimeMsg < 900)
    {
        ClkGovSyncPpsN(NmeaTimeGetSeconds() + 1); //Synchronise with Nmea time from the previous pulse
    }
    else
    {
        ClkGovSyncPpsZ();                         //Synchronise to nearest second of existing clock time
    }
}

static void mainConfidence()
{
    ClkGovIsReceivingTime = PpsIsStable() && NmeaFixIsStable() && NmeaModuleIsReady(); //Tell the clock gov module if confident
    static bool wasStable = false;
    if (ClkGovIsReceivingTime && !wasStable) GpsLog("PPS is stable and NMEA has fix - sync enabled\r\n");
    wasStable = ClkGovIsReceivingTime;
}

void GpsMain()
{
    NmeaMain();
    PpsMain();
    mainConfidence();
}
void GpsInit()
{
    NmeaInit();
    PpsInit();
}