Central Heating controller using the real time clock, PHY module for internet, 1-wire interface for temperature sensors, a system log and a configuration file

Dependencies:   net 1-wire lpc1768 crypto clock web fram log

/media/uploads/andrewboyson/heating.sch

/media/uploads/andrewboyson/heating.brd

/media/uploads/andrewboyson/eagle.epf

Revision:
28:bb55def47737
Parent:
21:0c9673027ff9
Child:
31:ee99e468f4b9
--- a/settings/settings.c	Sun Jan 20 20:34:25 2019 +0000
+++ b/settings/settings.c	Fri Jan 25 17:42:55 2019 +0000
@@ -1,5 +1,6 @@
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include     "clkgov.h"
 #include       "fram.h"
@@ -15,7 +16,7 @@
 #include         "ns.h"
 #include        "nr4.h"
 #include        "nr6.h"
-#include "ntp-client.h"
+#include  "ntpclient.h"
 #include     "1-wire.h"
 #include      "echo4.h"
 #include      "echo6.h"
@@ -32,6 +33,7 @@
 #include       "http.h"
 #include       "tftp.h"
 #include        "dns.h"
+#include        "led.h"
 
 static int iClkGovSlewDivisor      ;
 static int iClkGovSlewMaxMs        ;
@@ -99,9 +101,9 @@
 void ChgTraceOneWire          () {   OneWireTrace        =   !OneWireTrace       ; FramWrite(iOneWire,    1,   &OneWireTrace       ); }
 
 //Net settings
-void ChgDnsSendRequestsViaIp4    () {  DnsSendRequestsViaIp4 =  !DnsSendRequestsViaIp4; FramWrite( iDnsSendRequestsViaIp4,    1,  &DnsSendRequestsViaIp4); }
-void ChgNtpSendRequestsViaIp4    () {  NtpSendRequestsViaIp4 =  !NtpSendRequestsViaIp4; FramWrite( iNtpSendRequestsViaIp4,    1,  &NtpSendRequestsViaIp4); }
-void ChgTftpSendRequestsViaIp4   () { TftpSendRequestsViaIp4 = !TftpSendRequestsViaIp4; FramWrite(iTftpSendRequestsViaIp4,    1, &TftpSendRequestsViaIp4); }
+void ChgDnsSendRequestsViaIp4    () {            DnsSendRequestsViaIp4 =             !DnsSendRequestsViaIp4; FramWrite( iDnsSendRequestsViaIp4,    1,             &DnsSendRequestsViaIp4); }
+void ChgNtpSendRequestsViaIp4    () { NtpClientQuerySendRequestsViaIp4 =  !NtpClientQuerySendRequestsViaIp4; FramWrite( iNtpSendRequestsViaIp4,    1,  &NtpClientQuerySendRequestsViaIp4); }
+void ChgTftpSendRequestsViaIp4   () {           TftpSendRequestsViaIp4 =            !TftpSendRequestsViaIp4; FramWrite(iTftpSendRequestsViaIp4,    1,            &TftpSendRequestsViaIp4); }
 
 void SetTraceNetHost (char* text)
 {
@@ -142,6 +144,39 @@
 void ChgTraceHttp      () {      HttpTrace        =      !HttpTrace       ; FramWrite(iHttp,       1,      &HttpTrace       ); }
 void ChgTraceTftp      () {      TftpTrace        =      !TftpTrace       ; FramWrite(iTftp,       1,      &TftpTrace       ); }
 
+
+
+static int iServerName;
+static int iInitialInterval;
+static int iNormalInterval;
+static int iRetryInterval;
+static int iOffsetMs;
+static int iMaxDelayMs;
+
+static void lblcpy(char* dst, char* src) { strncpy(dst, src, DNS_MAX_LABEL_LENGTH); dst[DNS_MAX_LABEL_LENGTH] = 0; }
+
+void SetNtpClientServerName      ( char* value) { lblcpy(NtpClientQueryServerName,            value); FramWrite(iServerName,      DNS_MAX_LABEL_LENGTH,  NtpClientQueryServerName      ); }
+void SetNtpClientInitialInterval ( int   value) { NtpClientQueryInitialInterval    = (int32_t)value ; FramWrite(iInitialInterval,                    4, &NtpClientQueryInitialInterval ); }
+void SetNtpClientNormalInterval  ( int   value) { NtpClientQueryNormalInterval     = (int32_t)value ; FramWrite(iNormalInterval,                     4, &NtpClientQueryNormalInterval  ); }
+void SetNtpClientRetryInterval   ( int   value) { NtpClientQueryRetryInterval      = (int32_t)value ; FramWrite(iRetryInterval,                      4, &NtpClientQueryRetryInterval   ); }
+void SetNtpClientOffsetMs        ( int   value) { NtpClientReplyOffsetMs           = (int32_t)value ; FramWrite(iOffsetMs,                           4, &NtpClientReplyOffsetMs        ); }
+void SetNtpClientMaxDelayMs      ( int   value) { NtpClientReplyMaxDelayMs         = (int32_t)value ; FramWrite(iMaxDelayMs,                         4, &NtpClientReplyMaxDelayMs      ); }
+
+int SettingsNtpInit()
+{
+    int address;
+    int32_t def4;
+    
+    def4 =    0; address = FramLoad( DNS_MAX_LABEL_LENGTH+1,  NtpClientQueryServerName,      NULL ); if (address < 0) return -1; iServerName      = address;
+    def4 =    1; address = FramLoad(                      4, &NtpClientQueryInitialInterval, &def4); if (address < 0) return -1; iInitialInterval = address;
+    def4 =  600; address = FramLoad(                      4, &NtpClientQueryNormalInterval,  &def4); if (address < 0) return -1; iNormalInterval  = address;
+    def4 =   60; address = FramLoad(                      4, &NtpClientQueryRetryInterval,   &def4); if (address < 0) return -1; iRetryInterval   = address; 
+    def4 =    0; address = FramLoad(                      4, &NtpClientReplyOffsetMs,        &def4); if (address < 0) return -1; iOffsetMs        = address; 
+    def4 =   50; address = FramLoad(                      4, &NtpClientReplyMaxDelayMs,      &def4); if (address < 0) return -1; iMaxDelayMs      = address; 
+    
+    return 0;
+}
+
 int SettingsInit()
 {
     int address;
@@ -159,9 +194,9 @@
     def4 =        3; address = FramLoad( 4, &ClkGovMaxOffsetSecs,     &def4);    if (address < 0) return -1; iClkGovMaxOffsetSecs     = address;
     address = FramLoad( 1, &b, NULL);   ClkGovTrace          = b; if (address < 0) return -1; iClkGov      = address;
     address = FramLoad( 1, &b, NULL);  OneWireTrace          = b; if (address < 0) return -1; iOneWire     = address;
-    address = FramLoad( 1, &b, NULL);  DnsSendRequestsViaIp4 = b; if (address < 0) return -1; iDnsSendRequestsViaIp4  = address;
-    address = FramLoad( 1, &b, NULL);  NtpSendRequestsViaIp4 = b; if (address < 0) return -1; iNtpSendRequestsViaIp4  = address;
-    address = FramLoad( 1, &b, NULL); TftpSendRequestsViaIp4 = b; if (address < 0) return -1; iTftpSendRequestsViaIp4 = address;
+    address = FramLoad( 1, &b, NULL);             DnsSendRequestsViaIp4 = b; if (address < 0) return -1; iDnsSendRequestsViaIp4  = address;
+    address = FramLoad( 1, &b, NULL);  NtpClientQuerySendRequestsViaIp4 = b; if (address < 0) return -1; iNtpSendRequestsViaIp4  = address;
+    address = FramLoad( 1, &b, NULL);            TftpSendRequestsViaIp4 = b; if (address < 0) return -1; iTftpSendRequestsViaIp4 = address;
     address = FramLoad( 1, &b, NULL);       LogUart          = b; if (address < 0) return -1; iLogUart     = address;
     address = FramLoad( 2,                  NetTraceHost, NULL) ; if (address < 0) return -1; iNetHost     = address;
     address = FramLoad( 1, &b, NULL);       NetTraceStack    = b; if (address < 0) return -1; iNetStack    = address;