Browse Source

Merge pull request #3 from jancoow/network_realiseren

Network realiseren
Janco Kock 9 years ago
parent
commit
078f1146e4
6 changed files with 156 additions and 8 deletions
  1. 14 8
      main.c
  2. 26 0
      network.c
  3. 10 0
      network.h
  4. 63 0
      ntp.c
  5. 10 0
      ntp.h
  6. 33 0
      uart0driver.c

+ 14 - 8
main.c

@@ -41,10 +41,12 @@
 #include "watchdog.h"
 #include "flash.h"
 #include "spidrv.h"
+#include "network.h"
 
 
 #include <time.h>
 #include "rtc.h"
+#include "ntp.h"
 
 
 /*-------------------------------------------------------------------------*/
@@ -78,7 +80,7 @@ static void SysControlMainBeat(u_char);
 /*-------------------------------------------------------------------------*/
 
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief ISR MainBeat Timer Interrupt (Timer 2 for Mega128, Timer 0 for Mega256).
  *
@@ -89,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)
 {
 
@@ -101,7 +103,7 @@ static void SysMainBeatInterrupt(void *p)
 }
 
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief Initialise Digital IO
  *  init inputs to '0', outputs to '1' (DDRxn='0' or '1')
@@ -109,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)
 {
     /*
@@ -166,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;
@@ -221,7 +223,7 @@ int checkOffPressed(){
 	}
 }
 
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 /*!
  * \brief Main entry of the SIR firmware
  *
@@ -232,7 +234,7 @@ int checkOffPressed(){
  *
  * \return \b never returns
  */
-/* ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ */
+/* ����������������������������������������������������������������������� */
 int main(void)
 {
 	time_t start;
@@ -269,6 +271,9 @@ int main(void)
 
     CardInit();
 
+    NetworkInit();
+
+    //NtpInit();
 	/*
 	 * Kroeske: sources in rtc.c en rtc.h
 	 */
@@ -284,6 +289,7 @@ int main(void)
     }
 
 
+
     RcInit();
     
 	KbInit();

+ 26 - 0
network.c

@@ -0,0 +1,26 @@
+//
+// Created by janco on 25-2-16.
+//
+#include <dev/board.h>
+#include <sys/timer.h>
+#include <sys/confnet.h>
+
+#include <dev/nicrtl.h>
+
+#include <stdio.h>
+#include <io.h>
+#include <arpa/inet.h>
+#include <pro/dhcp.h>
+#include "network.h"
+
+void NetworkInit() {
+    /* Register de internet controller. */
+    if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
+        printf("Registering  failed.");
+    }/* Netwerk configureren op dhcp */
+    else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
+        /* Done. */
+    }else {
+        printf("Ik heb een internet connectie. Ip is: %s", inet_ntoa(confnet.cdn_ip_addr));
+    }
+}

+ 10 - 0
network.h

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

+ 63 - 0
ntp.c

@@ -0,0 +1,63 @@
+//
+// 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 time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
+    printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
+
+    NutRtcSetTime(ntp_datetime);
+
+
+    // This isn't working...
+    tm *test;
+
+    X12RtcGetClock(test);
+
+    printf("RTC time is: %02d:%02d:%02d\n", test->tm_hour, test->tm_min, test->tm_sec);
+    printf("RTC date is: %02d.%02d.%02d\n\n", test->tm_mday, (test->tm_mon + 1), (test->tm_year + 1900));
+}
+

+ 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 */

+ 33 - 0
uart0driver.c

@@ -56,8 +56,10 @@
 
 //#pragma text:appcode
 
+#include "log.h"
 #include "system.h"
 #include "uart0driver.h"
+#include "watchdog.h"
 
 
 //----------------------------------------------------------
@@ -84,6 +86,37 @@ static FILE *stream=NULL;
     // }
 // }
 
+THREAD(Uart0KeyEvents, arg)
+{
+    //LogMsg_P(LOG_INFO, PSTR("UART-Thread Start"));
+
+    NutThreadSetPriority(200);  // low prio
+    for (;;)
+    {
+        char *result = "";
+        char rst[] = "reset\n";
+
+        if (stream==NULL)
+        {
+            //LogMsg_P(LOG_INFO, PSTR("Stream is NULL!"));
+            NutSleep(2000);           //Mhe
+            continue;
+        }
+
+        result = fgets(result, 16, stream);
+        short _reset_received = strcmp(rst, result);
+
+        *rst = "abcde\n";
+        *result = "";
+
+        if (_reset_received == 0){
+            LogMsg_P(LOG_INFO, PSTR(">>>>>> RESET COMMAND RECEIVED"));
+            //RESET SIR!
+            WatchDogStart(0);
+        }
+    }
+}
+
 //----------------------------------------------------------
 
 /*!