A GPS disciplined clock

Dependencies:   net lpc1768 crypto clock web log

Committer:
andrewboyson
Date:
Wed May 06 18:40:02 2020 +0000
Revision:
97:af023452967f
Parent:
88:2c72beb1debe
Child:
98:52506c675c82
Changed PPS timeout to 8000ms (it was 1000ms)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 8:2ef0f459bc83 1 #include <stdlib.h>
andrewboyson 8:2ef0f459bc83 2 #include <stdbool.h>
andrewboyson 41:30c9ee0ea16b 3 #include <string.h>
andrewboyson 8:2ef0f459bc83 4
andrewboyson 35:a535b65203a9 5 #include "gps.h"
andrewboyson 35:a535b65203a9 6 #include "nmea.h"
andrewboyson 24:6f3e53833e62 7 #include "clkutc.h"
andrewboyson 8:2ef0f459bc83 8 #include "log.h"
andrewboyson 8:2ef0f459bc83 9 #include "net.h"
andrewboyson 8:2ef0f459bc83 10 #include "link.h"
andrewboyson 8:2ef0f459bc83 11 #include "dns.h"
andrewboyson 42:f14cb9b5a42f 12 #include "dnslabel.h"
andrewboyson 8:2ef0f459bc83 13 #include "dnsname.h"
andrewboyson 8:2ef0f459bc83 14 #include "dnsquery.h"
andrewboyson 8:2ef0f459bc83 15 #include "dnsreply.h"
andrewboyson 8:2ef0f459bc83 16 #include "dnsserver.h"
andrewboyson 8:2ef0f459bc83 17 #include "ntp.h"
andrewboyson 31:1a1d20352fca 18 #include "ntpclient.h"
andrewboyson 8:2ef0f459bc83 19 #include "dhcp.h"
andrewboyson 8:2ef0f459bc83 20 #include "ns.h"
andrewboyson 8:2ef0f459bc83 21 #include "nr4.h"
andrewboyson 8:2ef0f459bc83 22 #include "nr6.h"
andrewboyson 24:6f3e53833e62 23 #include "clkgov.h"
andrewboyson 8:2ef0f459bc83 24 #include "echo4.h"
andrewboyson 8:2ef0f459bc83 25 #include "echo6.h"
andrewboyson 8:2ef0f459bc83 26 #include "dest6.h"
andrewboyson 8:2ef0f459bc83 27 #include "ra.h"
andrewboyson 8:2ef0f459bc83 28 #include "rs.h"
andrewboyson 8:2ef0f459bc83 29 #include "ar4.h"
andrewboyson 8:2ef0f459bc83 30 #include "ar6.h"
andrewboyson 8:2ef0f459bc83 31 #include "arp.h"
andrewboyson 8:2ef0f459bc83 32 #include "ip4.h"
andrewboyson 8:2ef0f459bc83 33 #include "ip6.h"
andrewboyson 8:2ef0f459bc83 34 #include "udp.h"
andrewboyson 8:2ef0f459bc83 35 #include "tcp.h"
andrewboyson 73:9e4842daf3ae 36 #include "web.h"
andrewboyson 8:2ef0f459bc83 37 #include "tftp.h"
andrewboyson 88:2c72beb1debe 38 #include "rtc.h"
andrewboyson 8:2ef0f459bc83 39
andrewboyson 8:2ef0f459bc83 40 #define GPREG4 (*((volatile unsigned *) 0x40024054))
andrewboyson 8:2ef0f459bc83 41 #define ALMON (*((volatile unsigned *) 0x40024078))
andrewboyson 8:2ef0f459bc83 42 #define ALYEAR (*((volatile unsigned *) 0x4002407C))
andrewboyson 16:3d40d832dc70 43 #define ALDOM (*((volatile unsigned *) 0x4002406C))
andrewboyson 11:baa388c55583 44
andrewboyson 11:baa388c55583 45 /*
andrewboyson 16:3d40d832dc70 46 ALSEC 6 Alarm value for Seconds R/W 0x4002 4060 -
andrewboyson 16:3d40d832dc70 47 ALMIN 6 Alarm value for Minutes R/W 0x4002 4064 -
andrewboyson 11:baa388c55583 48 ALHOUR 5 Alarm value for Hours R/W 0x4002 4068
andrewboyson 16:3d40d832dc70 49 ALDOM 5 Alarm value for Day of Month R/W 0x4002 406C *
andrewboyson 11:baa388c55583 50 ALDOW 3 Alarm value for Day of Week R/W 0x4002 4070
andrewboyson 16:3d40d832dc70 51 ALDOY 9 Alarm value for Day of Year R/W 0x4002 4074 -
andrewboyson 16:3d40d832dc70 52 ALMON 4 Alarm value for Months R/W 0x4002 4078 *
andrewboyson 16:3d40d832dc70 53 ALYEAR 12 Alarm value for Years R/W 0x4002 407C *
andrewboyson 11:baa388c55583 54 */
andrewboyson 8:2ef0f459bc83 55
andrewboyson 8:2ef0f459bc83 56 enum { iLogUart, iNetStack, iNetNewLine, iNetVerbose, iLink, iDnsName, iDnsQuery, iDnsReply, iDnsServer,
andrewboyson 24:6f3e53833e62 57 iNtp, iDhcp, iNsRecvSol, iNsRecvAdv, iNsSendSol, iNr4, iNr6, iNtpClient, iClkGov, iEcho4, iEcho6,
andrewboyson 8:2ef0f459bc83 58 iDest6, iRa, iRs, iAr4, iAr6, iArp, iIp4, iIp6, iUdp, iTcp, iHttp, iTftp };
andrewboyson 8:2ef0f459bc83 59
andrewboyson 11:baa388c55583 60 int GetSensorHeight()
andrewboyson 11:baa388c55583 61 {
andrewboyson 16:3d40d832dc70 62 return ALDOM; //9 bits
andrewboyson 11:baa388c55583 63 }
andrewboyson 11:baa388c55583 64 void SetSensorHeight(int value)
andrewboyson 11:baa388c55583 65 {
andrewboyson 16:3d40d832dc70 66 ALDOM = value & 0x001F; //5 bits +32
andrewboyson 11:baa388c55583 67 }
andrewboyson 11:baa388c55583 68
andrewboyson 35:a535b65203a9 69 void ChgNmeaMsgTrace() { NmeaMsgTrace = !NmeaMsgTrace; }
andrewboyson 35:a535b65203a9 70 void ChgNmeaCmdTrace() { NmeaCmdTrace = !NmeaCmdTrace; }
andrewboyson 35:a535b65203a9 71 void ChgGpsTrace() { GpsTrace = !GpsTrace; }
andrewboyson 36:2983e45eeb49 72 void ChgGpsVerbose() { GpsVerbose = !GpsVerbose; }
andrewboyson 35:a535b65203a9 73
andrewboyson 8:2ef0f459bc83 74 static bool getBit(int bit)
andrewboyson 8:2ef0f459bc83 75 {
andrewboyson 8:2ef0f459bc83 76 return GPREG4 & (1U << bit);
andrewboyson 8:2ef0f459bc83 77 }
andrewboyson 8:2ef0f459bc83 78 static void setBit(int bit, bool value)
andrewboyson 8:2ef0f459bc83 79 {
andrewboyson 8:2ef0f459bc83 80 if (value) GPREG4 |= 1U << bit ;
andrewboyson 8:2ef0f459bc83 81 else GPREG4 &= ~(1U << bit);
andrewboyson 8:2ef0f459bc83 82 }
andrewboyson 8:2ef0f459bc83 83
andrewboyson 8:2ef0f459bc83 84 void SetTraceNetHost (char* text)
andrewboyson 8:2ef0f459bc83 85 {
andrewboyson 8:2ef0f459bc83 86 int value = strtol(text, NULL, 16);
andrewboyson 8:2ef0f459bc83 87 NetTraceHost[1] = value & 0xFF; //Little endian so low byte
andrewboyson 8:2ef0f459bc83 88 NetTraceHost[0] = (value >> 8) & 0xFF; //high byte
andrewboyson 8:2ef0f459bc83 89 ALMON = value >> 12; // 4 bits
andrewboyson 8:2ef0f459bc83 90 ALYEAR = value & 0x3FFF; //12 bits
andrewboyson 8:2ef0f459bc83 91 }
andrewboyson 8:2ef0f459bc83 92 void ChgLogUart () { LogUart = !LogUart; setBit(iLogUart, LogUart ); }
andrewboyson 8:2ef0f459bc83 93 void ChgTraceNetStack () { NetTraceStack = !NetTraceStack; setBit(iNetStack, NetTraceStack ); }
andrewboyson 8:2ef0f459bc83 94 void ChgTraceNetNewLine() { NetTraceNewLine = !NetTraceNewLine; setBit(iNetNewLine, NetTraceNewLine); }
andrewboyson 8:2ef0f459bc83 95 void ChgTraceNetVerbose() { NetTraceVerbose = !NetTraceVerbose; setBit(iNetVerbose, NetTraceVerbose); }
andrewboyson 8:2ef0f459bc83 96 void ChgTraceLink () { LinkTrace = !LinkTrace; setBit(iLink, LinkTrace ); }
andrewboyson 8:2ef0f459bc83 97 void ChgTraceDnsName () { DnsNameTrace = !DnsNameTrace; setBit(iDnsName, DnsNameTrace ); }
andrewboyson 8:2ef0f459bc83 98 void ChgTraceDnsQuery () { DnsQueryTrace = !DnsQueryTrace; setBit(iDnsQuery, DnsQueryTrace ); }
andrewboyson 8:2ef0f459bc83 99 void ChgTraceDnsReply () { DnsReplyTrace = !DnsReplyTrace; setBit(iDnsReply, DnsReplyTrace ); }
andrewboyson 8:2ef0f459bc83 100 void ChgTraceDnsServer () { DnsServerTrace = !DnsServerTrace; setBit(iDnsServer, DnsServerTrace ); }
andrewboyson 8:2ef0f459bc83 101 void ChgTraceNtp () { NtpTrace = !NtpTrace; setBit(iNtp, NtpTrace ); }
andrewboyson 8:2ef0f459bc83 102 void ChgTraceDhcp () { DhcpTrace = !DhcpTrace; setBit(iDhcp, DhcpTrace ); }
andrewboyson 8:2ef0f459bc83 103 void ChgTraceNsRecvSol () { NsTraceRecvSol = !NsTraceRecvSol; setBit(iNsRecvSol, NsTraceRecvSol); }
andrewboyson 8:2ef0f459bc83 104 void ChgTraceNsRecvAdv () { NsTraceRecvAdv = !NsTraceRecvAdv; setBit(iNsRecvAdv, NsTraceRecvAdv); }
andrewboyson 8:2ef0f459bc83 105 void ChgTraceNsSendSol () { NsTraceSendSol = !NsTraceSendSol; setBit(iNsSendSol, NsTraceSendSol); }
andrewboyson 8:2ef0f459bc83 106 void ChgTraceNr4 () { Nr4Trace = !Nr4Trace ; setBit(iNr4, Nr4Trace ); }
andrewboyson 8:2ef0f459bc83 107 void ChgTraceNr6 () { Nr6Trace = !Nr6Trace ; setBit(iNr6, Nr6Trace ); }
andrewboyson 8:2ef0f459bc83 108 void ChgTraceNtpClient () { NtpClientTrace = !NtpClientTrace ; setBit(iNtpClient, NtpClientTrace ); }
andrewboyson 24:6f3e53833e62 109 void ChgTraceSync () { ClkGovTrace = !ClkGovTrace ; setBit(iClkGov, ClkGovTrace ); }
andrewboyson 8:2ef0f459bc83 110 void ChgTraceEcho4 () { Echo4Trace = !Echo4Trace ; setBit(iEcho4, Echo4Trace ); }
andrewboyson 8:2ef0f459bc83 111 void ChgTraceEcho6 () { Echo6Trace = !Echo6Trace ; setBit(iEcho6, Echo6Trace ); }
andrewboyson 8:2ef0f459bc83 112 void ChgTraceDest6 () { Dest6Trace = !Dest6Trace ; setBit(iDest6, Dest6Trace ); }
andrewboyson 8:2ef0f459bc83 113 void ChgTraceRa () { RaTrace = !RaTrace ; setBit(iRa, RaTrace ); }
andrewboyson 8:2ef0f459bc83 114 void ChgTraceRs () { RsTrace = !RsTrace ; setBit(iRs, RsTrace ); }
andrewboyson 8:2ef0f459bc83 115 void ChgTraceAr4 () { Ar4Trace = !Ar4Trace ; setBit(iAr4, Ar4Trace ); }
andrewboyson 8:2ef0f459bc83 116 void ChgTraceAr6 () { Ar6Trace = !Ar6Trace ; setBit(iAr6, Ar6Trace ); }
andrewboyson 8:2ef0f459bc83 117 void ChgTraceArp () { ArpTrace = !ArpTrace ; setBit(iArp, ArpTrace ); }
andrewboyson 8:2ef0f459bc83 118 void ChgTraceIp4 () { Ip4Trace = !Ip4Trace ; setBit(iIp4, Ip4Trace ); }
andrewboyson 8:2ef0f459bc83 119 void ChgTraceIp6 () { Ip6Trace = !Ip6Trace ; setBit(iIp6, Ip6Trace ); }
andrewboyson 8:2ef0f459bc83 120 void ChgTraceUdp () { UdpTrace = !UdpTrace ; setBit(iUdp, UdpTrace ); }
andrewboyson 8:2ef0f459bc83 121 void ChgTraceTcp () { TcpTrace = !TcpTrace ; setBit(iTcp, TcpTrace ); }
andrewboyson 73:9e4842daf3ae 122 void ChgTraceHttp () { WebTrace = !WebTrace ; setBit(iHttp, WebTrace ); }
andrewboyson 8:2ef0f459bc83 123 void ChgTraceTftp () { TftpTrace = !TftpTrace ; setBit(iTftp, TftpTrace ); }
andrewboyson 8:2ef0f459bc83 124
andrewboyson 31:1a1d20352fca 125 void ChgDnsSendRequestsViaIp4() { DnsSendRequestsViaIp4 = !DnsSendRequestsViaIp4; }
andrewboyson 31:1a1d20352fca 126 void ChgNtpSendRequestsViaIp4() { NtpClientQuerySendRequestsViaIp4 = !NtpClientQuerySendRequestsViaIp4; }
andrewboyson 31:1a1d20352fca 127 void ChgTftpSendRequestsViaIp4() { TftpSendRequestsViaIp4 = !TftpSendRequestsViaIp4; }
andrewboyson 8:2ef0f459bc83 128
andrewboyson 17:1dcc58dac445 129 //Clock settings
andrewboyson 36:2983e45eeb49 130 void SetClockPpbDivisor (int value) { ClkGovFreqDivisor = value; }
andrewboyson 36:2983e45eeb49 131 void SetClockPpbChangeMax (int value) { ClkGovFreqChangeMaxPpb = value; }
andrewboyson 36:2983e45eeb49 132 void SetClockSyncedLimitPpb (int value) { ClkGovFreqSyncedLimPpb = value; }
andrewboyson 36:2983e45eeb49 133 void SetClockSyncedHysterisPpb(int value) { ClkGovFreqSyncedHysPpb = value; }
andrewboyson 36:2983e45eeb49 134 void SetClockSlewDivisor (int value) { ClkGovSlewDivisor = value; }
andrewboyson 36:2983e45eeb49 135 void SetClockSlewMaxMs (int value) { ClkGovSlewChangeMaxMs = value; }
andrewboyson 36:2983e45eeb49 136 void SetClockSyncedLimitNs (int value) { ClkGovSlewSyncedLimNs = value; }
andrewboyson 36:2983e45eeb49 137 void SetClockSyncedHysterisNs (int value) { ClkGovSlewSyncedHysNs = value; }
andrewboyson 36:2983e45eeb49 138 void SetClockMaxOffsetSecs (int value) { ClkGovSlewOffsetMaxSecs = value; }
andrewboyson 17:1dcc58dac445 139
andrewboyson 41:30c9ee0ea16b 140 //NTP settings
andrewboyson 41:30c9ee0ea16b 141
andrewboyson 42:f14cb9b5a42f 142 void SetNtpClientServerName ( char* value) { DnsLabelCopy(NtpClientQueryServerName, value); }
andrewboyson 41:30c9ee0ea16b 143 void SetNtpClientInitialInterval ( int value) { NtpClientQueryInitialInterval = value ; }
andrewboyson 41:30c9ee0ea16b 144 void SetNtpClientNormalInterval ( int value) { NtpClientQueryNormalInterval = value ; }
andrewboyson 41:30c9ee0ea16b 145 void SetNtpClientRetryInterval ( int value) { NtpClientQueryRetryInterval = value ; }
andrewboyson 41:30c9ee0ea16b 146 void SetNtpClientOffsetMs ( int value) { NtpClientReplyOffsetMs = value ; }
andrewboyson 41:30c9ee0ea16b 147 void SetNtpClientMaxDelayMs ( int value) { NtpClientReplyMaxDelayMs = value ; }
andrewboyson 41:30c9ee0ea16b 148
andrewboyson 8:2ef0f459bc83 149 int SettingsInit()
andrewboyson 8:2ef0f459bc83 150 {
andrewboyson 36:2983e45eeb49 151 ClkGovFreqDivisor = 1000;
andrewboyson 36:2983e45eeb49 152 ClkGovFreqChangeMaxPpb = 1000;
andrewboyson 36:2983e45eeb49 153 ClkGovFreqSyncedLimPpb = 100000;
andrewboyson 36:2983e45eeb49 154 ClkGovFreqSyncedHysPpb = 1000;
andrewboyson 36:2983e45eeb49 155 ClkGovSlewDivisor = 100;
andrewboyson 36:2983e45eeb49 156 ClkGovSlewChangeMaxMs = 10;
andrewboyson 36:2983e45eeb49 157 ClkGovSlewSyncedLimNs = 10000000; //10ms
andrewboyson 36:2983e45eeb49 158 ClkGovSlewSyncedHysNs = 1000000; //1ms
andrewboyson 36:2983e45eeb49 159 ClkGovSlewOffsetMaxSecs = 3;
andrewboyson 8:2ef0f459bc83 160
andrewboyson 41:30c9ee0ea16b 161 NtpClientQueryInitialInterval = 2 ;
andrewboyson 41:30c9ee0ea16b 162 NtpClientQueryNormalInterval = 600 ;
andrewboyson 41:30c9ee0ea16b 163 NtpClientQueryRetryInterval = 60 ;
andrewboyson 41:30c9ee0ea16b 164 NtpClientReplyOffsetMs = 0 ;
andrewboyson 41:30c9ee0ea16b 165 NtpClientReplyMaxDelayMs = 100 ;
andrewboyson 41:30c9ee0ea16b 166
andrewboyson 31:1a1d20352fca 167 DnsSendRequestsViaIp4 = false;
andrewboyson 31:1a1d20352fca 168 NtpClientQuerySendRequestsViaIp4 = false;
andrewboyson 31:1a1d20352fca 169 TftpSendRequestsViaIp4 = true;
andrewboyson 88:2c72beb1debe 170
andrewboyson 88:2c72beb1debe 171 if (RtcPowerLost())
andrewboyson 88:2c72beb1debe 172 {
andrewboyson 88:2c72beb1debe 173 GPREG4 = 0;
andrewboyson 88:2c72beb1debe 174 ALMON = 0;
andrewboyson 88:2c72beb1debe 175 ALYEAR = 0;
andrewboyson 88:2c72beb1debe 176 ALDOM = 0;
andrewboyson 88:2c72beb1debe 177 }
andrewboyson 8:2ef0f459bc83 178
andrewboyson 8:2ef0f459bc83 179 NetTraceHost[1] = ALYEAR & 0xFF; //Little endian so low byte
andrewboyson 8:2ef0f459bc83 180 NetTraceHost[0] = (ALMON << 4) + (ALYEAR >> 8); //high byte
andrewboyson 8:2ef0f459bc83 181
andrewboyson 8:2ef0f459bc83 182 LogUart = getBit(iLogUart);
andrewboyson 8:2ef0f459bc83 183 NetTraceStack = getBit(iNetStack);
andrewboyson 8:2ef0f459bc83 184 NetTraceNewLine = getBit(iNetNewLine);
andrewboyson 8:2ef0f459bc83 185 NetTraceVerbose = getBit(iNetVerbose);
andrewboyson 8:2ef0f459bc83 186 LinkTrace = getBit(iLink);
andrewboyson 8:2ef0f459bc83 187 DnsNameTrace = getBit(iDnsName);
andrewboyson 8:2ef0f459bc83 188 DnsQueryTrace = getBit(iDnsQuery);
andrewboyson 8:2ef0f459bc83 189 DnsReplyTrace = getBit(iDnsReply);
andrewboyson 8:2ef0f459bc83 190 DnsServerTrace = getBit(iDnsServer);
andrewboyson 8:2ef0f459bc83 191 NtpTrace = getBit(iNtp);
andrewboyson 8:2ef0f459bc83 192 DhcpTrace = getBit(iDhcp);
andrewboyson 8:2ef0f459bc83 193 NsTraceRecvSol = getBit(iNsRecvSol);
andrewboyson 8:2ef0f459bc83 194 NsTraceRecvAdv = getBit(iNsRecvAdv);
andrewboyson 8:2ef0f459bc83 195 NsTraceSendSol = getBit(iNsSendSol);
andrewboyson 8:2ef0f459bc83 196 Nr4Trace = getBit(iNr4);
andrewboyson 8:2ef0f459bc83 197 Nr6Trace = getBit(iNr6);
andrewboyson 8:2ef0f459bc83 198 NtpClientTrace = getBit(iNtpClient);
andrewboyson 24:6f3e53833e62 199 ClkGovTrace = getBit(iClkGov);
andrewboyson 8:2ef0f459bc83 200 Echo4Trace = getBit(iEcho4);
andrewboyson 8:2ef0f459bc83 201 Echo6Trace = getBit(iEcho6);
andrewboyson 8:2ef0f459bc83 202 Dest6Trace = getBit(iDest6);
andrewboyson 8:2ef0f459bc83 203 RaTrace = getBit(iRa);
andrewboyson 8:2ef0f459bc83 204 RsTrace = getBit(iRs);
andrewboyson 8:2ef0f459bc83 205 Ar4Trace = getBit(iAr4);
andrewboyson 8:2ef0f459bc83 206 Ar6Trace = getBit(iAr6);
andrewboyson 8:2ef0f459bc83 207 ArpTrace = getBit(iArp);
andrewboyson 8:2ef0f459bc83 208 Ip4Trace = getBit(iIp4);
andrewboyson 8:2ef0f459bc83 209 Ip6Trace = getBit(iIp6);
andrewboyson 8:2ef0f459bc83 210 UdpTrace = getBit(iUdp);
andrewboyson 8:2ef0f459bc83 211 TcpTrace = getBit(iTcp);
andrewboyson 73:9e4842daf3ae 212 WebTrace = getBit(iHttp);
andrewboyson 8:2ef0f459bc83 213 TftpTrace = getBit(iTftp);
andrewboyson 8:2ef0f459bc83 214
andrewboyson 8:2ef0f459bc83 215 return 0;
andrewboyson 8:2ef0f459bc83 216 }