浏览代码

Added ntp time server sync

jancoow 9 年之前
父节点
当前提交
95ca846f1c
共有 3 个文件被更改,包括 70 次插入8 次删除
  1. 11 8
      main.c
  2. 49 0
      ntp.c
  3. 10 0
      ntp.h

+ 11 - 8
main.c

@@ -46,6 +46,7 @@
 
 #include <time.h>
 #include "rtc.h"
+#include "ntp.h"
 
 
 /*-------------------------------------------------------------------------*/
@@ -79,7 +80,7 @@ static void SysControlMainBeat(u_char);
 /*-------------------------------------------------------------------------*/
 
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  *
@@ -90,7 +91,7 @@ static void SysControlMainBeat(u_char);
  *
  * \param *p not used (might be used to pass parms from the ISR)
  */
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 static void SysMainBeatInterrupt(void *p)
 {
 
@@ -102,7 +103,7 @@ static void SysMainBeatInterrupt(void *p)
 }
 
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief Initialise Digital IO
  *  init inputs to '0', outputs to '1' (DDRxn='0' or '1')
@@ -110,7 +111,7 @@ static void SysMainBeatInterrupt(void *p)
  *  Pull-ups are enabled when the pin is set to input (DDRxn='0') and then a '1'
  *  is written to the pin (PORTxn='1')
  */
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 void SysInitIO(void)
 {
     /*
@@ -167,12 +168,12 @@ void SysInitIO(void)
     outp(0x18, DDRG);
 }
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief Starts or stops the 4.44 msec mainbeat of the system
  * \param OnOff indicates if the mainbeat needs to start or to stop
  */
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 static void SysControlMainBeat(u_char OnOff)
 {
     int nError = 0;
@@ -206,7 +207,7 @@ int checkOffPressed(){
 	}
 }
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief Main entry of the SIR firmware
  *
@@ -217,7 +218,7 @@ int checkOffPressed(){
  *
  * \return \b never returns
  */
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 int main(void)
 {
 	time_t start;
@@ -254,6 +255,8 @@ int main(void)
     CardInit();
 
     NetworkInit();
+
+    NtpInit();
 	/*
 	 * Kroeske: sources in rtc.c en rtc.h
 	 */

+ 49 - 0
ntp.c

@@ -0,0 +1,49 @@
+//
+// Created by janco on 25-2-16.
+//
+#include <dev/board.h>
+#include <dev/debug.h>
+
+#include <sys/timer.h>
+
+#include <arpa/inet.h>
+#include <net/route.h>
+#include <pro/dhcp.h>
+#include <pro/sntp.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <io.h>
+#include <string.h>
+#include <time.h>
+
+#include "ntp.h"
+
+time_t ntp_time = 0;
+tm *ntp_datetime;
+uint32_t timeserver = 0;
+
+void NtpInit() {
+    /* Timezone van nederland (gmt 1) */
+    _timezone = -2 * 60 * 60;
+    GetTime();
+}
+
+void GetTime(){
+    /* Ophalen van pool.ntp.org */
+    puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
+    timeserver = inet_addr("213.154.229.24");
+
+    for (;;) {
+        if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
+            break;
+        } else {
+            NutSleep(400);
+            puts("Fout bij het ontvangen van de tijd");
+        }
+    }
+    puts("Opgehaald.\n");
+    ntp_datetime = localtime(&ntp_time);
+    printf("NTP tijd is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
+    //return ntp_datetime;
+}

+ 10 - 0
ntp.h

@@ -0,0 +1,10 @@
+//
+// Created by janco on 25-2-16.
+//
+
+#ifndef _Network_H
+#define _Network_H
+
+extern void NtpInit(void);
+extern void GetTime(void);
+#endif /* _Network_H */