A GPS disciplined clock
Dependencies: net lpc1768 crypto clock web log
gps/gps.c@84:baec7c1f5618, 2019-11-18 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Nov 18 08:51:46 2019 +0000
- Revision:
- 84:baec7c1f5618
- Parent:
- 82:95df8c52774d
- Child:
- 86:7959accd9679
Updated LPC1768 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:67724a462d86 | 1 | #include <stdint.h> |
andrewboyson | 0:67724a462d86 | 2 | #include <stdbool.h> |
andrewboyson | 0:67724a462d86 | 3 | #include <stdio.h> |
andrewboyson | 35:a535b65203a9 | 4 | #include <stdarg.h> |
andrewboyson | 0:67724a462d86 | 5 | |
andrewboyson | 0:67724a462d86 | 6 | #include "log.h" |
andrewboyson | 0:67724a462d86 | 7 | #include "nmea.h" |
andrewboyson | 4:108157115360 | 8 | #include "pps.h" |
andrewboyson | 34:d9586fc921dc | 9 | #include "clkgov.h" |
andrewboyson | 0:67724a462d86 | 10 | |
andrewboyson | 36:2983e45eeb49 | 11 | bool GpsTrace = true; |
andrewboyson | 36:2983e45eeb49 | 12 | bool GpsVerbose = false; |
andrewboyson | 35:a535b65203a9 | 13 | |
andrewboyson | 35:a535b65203a9 | 14 | void GpsLog(const char *fmt, ...) |
andrewboyson | 35:a535b65203a9 | 15 | { |
andrewboyson | 35:a535b65203a9 | 16 | if (!GpsTrace) return; |
andrewboyson | 35:a535b65203a9 | 17 | va_list argptr; |
andrewboyson | 35:a535b65203a9 | 18 | va_start(argptr, fmt); |
andrewboyson | 35:a535b65203a9 | 19 | LogTimeF("GPS %04d ", PpsMsSinceLastPulse); |
andrewboyson | 35:a535b65203a9 | 20 | LogV(fmt, argptr); |
andrewboyson | 35:a535b65203a9 | 21 | va_end(argptr); |
andrewboyson | 35:a535b65203a9 | 22 | } |
andrewboyson | 36:2983e45eeb49 | 23 | void GpsLogVerbose(const char *fmt, ...) |
andrewboyson | 36:2983e45eeb49 | 24 | { |
andrewboyson | 36:2983e45eeb49 | 25 | if (!GpsTrace || !GpsVerbose) return; |
andrewboyson | 36:2983e45eeb49 | 26 | va_list argptr; |
andrewboyson | 36:2983e45eeb49 | 27 | va_start(argptr, fmt); |
andrewboyson | 36:2983e45eeb49 | 28 | LogTimeF("GPS %04d ", PpsMsSinceLastPulse); |
andrewboyson | 36:2983e45eeb49 | 29 | LogV(fmt, argptr); |
andrewboyson | 36:2983e45eeb49 | 30 | va_end(argptr); |
andrewboyson | 36:2983e45eeb49 | 31 | } |
andrewboyson | 0:67724a462d86 | 32 | |
andrewboyson | 78:8a29cad1b131 | 33 | #define SYNC_TYPE_NONE 0 |
andrewboyson | 78:8a29cad1b131 | 34 | #define SYNC_TYPE_PPS 1 |
andrewboyson | 78:8a29cad1b131 | 35 | #define SYNC_TYPE_TIME 2 |
andrewboyson | 78:8a29cad1b131 | 36 | static int syncType = SYNC_TYPE_NONE; |
andrewboyson | 78:8a29cad1b131 | 37 | |
andrewboyson | 78:8a29cad1b131 | 38 | static void calculateSyncType() |
andrewboyson | 78:8a29cad1b131 | 39 | { |
andrewboyson | 78:8a29cad1b131 | 40 | int newSyncType = SYNC_TYPE_NONE; |
andrewboyson | 78:8a29cad1b131 | 41 | if (PpsIsStable() && NmeaFixIsStable() && NmeaModuleIsReady()) |
andrewboyson | 78:8a29cad1b131 | 42 | { |
andrewboyson | 78:8a29cad1b131 | 43 | newSyncType = SYNC_TYPE_PPS; |
andrewboyson | 78:8a29cad1b131 | 44 | if (NmeaTimeIsStable()) newSyncType = SYNC_TYPE_TIME; |
andrewboyson | 78:8a29cad1b131 | 45 | } |
andrewboyson | 78:8a29cad1b131 | 46 | if (newSyncType != syncType) |
andrewboyson | 78:8a29cad1b131 | 47 | { |
andrewboyson | 78:8a29cad1b131 | 48 | switch (newSyncType) |
andrewboyson | 78:8a29cad1b131 | 49 | { |
andrewboyson | 78:8a29cad1b131 | 50 | case SYNC_TYPE_NONE: |
andrewboyson | 78:8a29cad1b131 | 51 | GpsLog("Sync stopped\r\n"); |
andrewboyson | 78:8a29cad1b131 | 52 | break; |
andrewboyson | 78:8a29cad1b131 | 53 | case SYNC_TYPE_PPS: |
andrewboyson | 78:8a29cad1b131 | 54 | if (syncType == SYNC_TYPE_NONE) GpsLog("Sync started with PPS alone\r\n"); |
andrewboyson | 78:8a29cad1b131 | 55 | else GpsLog("Syncing with PPS alone\r\n"); |
andrewboyson | 78:8a29cad1b131 | 56 | break; |
andrewboyson | 78:8a29cad1b131 | 57 | case SYNC_TYPE_TIME: |
andrewboyson | 78:8a29cad1b131 | 58 | if (syncType == SYNC_TYPE_NONE) GpsLog("Sync started with PPS and NMEA time\r\n"); |
andrewboyson | 78:8a29cad1b131 | 59 | else GpsLog("Syncing with PPS and NMEA time\r\n"); |
andrewboyson | 78:8a29cad1b131 | 60 | break; |
andrewboyson | 78:8a29cad1b131 | 61 | } |
andrewboyson | 78:8a29cad1b131 | 62 | syncType = newSyncType; |
andrewboyson | 78:8a29cad1b131 | 63 | } |
andrewboyson | 78:8a29cad1b131 | 64 | ClkGovIsReceivingTime = syncType != SYNC_TYPE_NONE; //Tell the clock gov module if confident |
andrewboyson | 78:8a29cad1b131 | 65 | } |
andrewboyson | 78:8a29cad1b131 | 66 | |
andrewboyson | 34:d9586fc921dc | 67 | void GpsHadPps() |
andrewboyson | 0:67724a462d86 | 68 | { |
andrewboyson | 78:8a29cad1b131 | 69 | calculateSyncType(); |
andrewboyson | 78:8a29cad1b131 | 70 | switch (syncType) |
andrewboyson | 37:a4e57b7e29dc | 71 | { |
andrewboyson | 78:8a29cad1b131 | 72 | case SYNC_TYPE_PPS: |
andrewboyson | 78:8a29cad1b131 | 73 | ClkGovSyncPpsZ(); //Synchronise to nearest second of existing clock time |
andrewboyson | 78:8a29cad1b131 | 74 | break; |
andrewboyson | 78:8a29cad1b131 | 75 | case SYNC_TYPE_TIME: |
andrewboyson | 78:8a29cad1b131 | 76 | { |
andrewboyson | 78:8a29cad1b131 | 77 | //Check the last NMEA time message was not missed |
andrewboyson | 78:8a29cad1b131 | 78 | int msSinceLastNmeaTimeMsg = (int)(PpsLastPulseMs - NmeaTimeGetMs()); |
andrewboyson | 82:95df8c52774d | 79 | if (msSinceLastNmeaTimeMsg < 900 && msSinceLastNmeaTimeMsg > 100) |
andrewboyson | 78:8a29cad1b131 | 80 | { |
andrewboyson | 78:8a29cad1b131 | 81 | ClkGovSyncPpsN(NmeaTimeGetSeconds() + 1); //Synchronise with Nmea time from the previous pulse |
andrewboyson | 78:8a29cad1b131 | 82 | } |
andrewboyson | 78:8a29cad1b131 | 83 | else |
andrewboyson | 78:8a29cad1b131 | 84 | { |
andrewboyson | 78:8a29cad1b131 | 85 | ClkGovSyncPpsZ(); //Synchronise to nearest second of existing clock time |
andrewboyson | 78:8a29cad1b131 | 86 | } |
andrewboyson | 78:8a29cad1b131 | 87 | } |
andrewboyson | 77:4e3925cbb1d8 | 88 | } |
andrewboyson | 34:d9586fc921dc | 89 | } |
andrewboyson | 0:67724a462d86 | 90 | |
andrewboyson | 0:67724a462d86 | 91 | void GpsMain() |
andrewboyson | 35:a535b65203a9 | 92 | { |
andrewboyson | 35:a535b65203a9 | 93 | NmeaMain(); |
andrewboyson | 4:108157115360 | 94 | PpsMain(); |
andrewboyson | 78:8a29cad1b131 | 95 | calculateSyncType(); |
andrewboyson | 0:67724a462d86 | 96 | } |
andrewboyson | 0:67724a462d86 | 97 | void GpsInit() |
andrewboyson | 0:67724a462d86 | 98 | { |
andrewboyson | 81:a1de28caf11a | 99 | GpsLog("GPS reset\r\n"); |
andrewboyson | 35:a535b65203a9 | 100 | NmeaInit(); |
andrewboyson | 4:108157115360 | 101 | PpsInit(); |
andrewboyson | 0:67724a462d86 | 102 | } |