ntp.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <arpa/inet.h>
  8. #include <net/route.h>
  9. #include <pro/dhcp.h>
  10. #include <pro/sntp.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <io.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include "ntp.h"
  17. #define TIME_ZONE 1
  18. time_t ntp_time = 0;
  19. tm *ntp_datetime;
  20. uint32_t timeserver = 0;
  21. void NtpInit() {
  22. /* Timezone van nederland (gmt 1) */
  23. _timezone = -TIME_ZONE * 3600;
  24. GetTime();
  25. }
  26. void GetTime(){
  27. /* Ophalen van pool.ntp.org */
  28. puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  29. timeserver = inet_addr("213.154.229.24");
  30. for (;;) {
  31. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  32. break;
  33. } else {
  34. NutSleep(400);
  35. puts("Fout bij het ontvangen van de tijd");
  36. }
  37. }
  38. puts("Opgehaald.\n");
  39. ntp_datetime = localtime(&ntp_time);
  40. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  41. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  42. X12RtcSetClock(ntp_datetime);
  43. NutDelay(100);
  44. //
  45. // // This isn't working...
  46. // struct _tm* test;
  47. // X12RtcGetClock(test);
  48. //
  49. // printf("RTC time is: %02d:%02d:%02d\n", test->tm_hour, test->tm_min, test->tm_sec);
  50. // printf("RTC date is: %02d.%02d.%02d\n\n", test->tm_mday, (test->tm_mon + 1), (test->tm_year + 1900));
  51. }