From ddef0654939ee7f0b7eda454f5447351592b512f Mon Sep 17 00:00:00 2001
From: Deomid Ryabkov <rojer@cesanta.com>
Date: Tue, 2 May 2017 21:49:07 +0100
Subject: [PATCH] Enable SNTP on CC3200

PUBLISHED_FROM=cea0fc4db21f5b68fd3e779334989fef84a1637a
---
 mongoose.c | 29 ++++++++++++++++-------------
 mongoose.h |  1 +
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 95fd8e5f3..a0e2637d8 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -12388,21 +12388,24 @@ time_t HOSTtime() {
 #endif /* __TI_COMPILER_VERSION__ */
 
 #ifndef __TI_COMPILER_VERSION__
-int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) {
+int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tz) {
 #else
-int gettimeofday(struct timeval *tp, void *tzp) {
+int gettimeofday(struct timeval *tp, void *tz) {
 #endif
-  unsigned long long r1 = 0, r2;
-  /* Achieve two consecutive reads of the same value. */
-  do {
-    r2 = r1;
-    r1 = PRCMSlowClkCtrFastGet();
-  } while (r1 != r2);
-  /* This is a 32768 Hz counter. */
-  tp->tv_sec = (r1 >> 15);
-  /* 1/32768-th of a second is 30.517578125 microseconds, approx. 31,
-   * but we round down so it doesn't overflow at 32767 */
-  tp->tv_usec = (r1 & 0x7FFF) * 30;
+  unsigned long sec;
+  unsigned short msec;
+  MAP_PRCMRTCGet(&sec, &msec);
+  tp->tv_sec = sec;
+  tp->tv_usec = ((unsigned long) msec) * 1000;
+  return 0;
+}
+
+#ifndef __TI_COMPILER_VERSION__
+int settimeofday(const struct timeval *tv, const struct timezone *tz) {
+#else
+int settimeofday(const struct timeval *tv, const void *tz) {
+#endif
+  MAP_PRCMRTCSet(tv->tv_sec, tv->tv_usec / 1000);
   return 0;
 }
 
diff --git a/mongoose.h b/mongoose.h
index 766fc1ff2..b9247747a 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -676,6 +676,7 @@ extern "C" {
 struct SlTimeval_t;
 #define timeval SlTimeval_t
 int gettimeofday(struct timeval *t, void *tz);
+int settimeofday(const struct timeval *tv, const void *tz);
 
 int asprintf(char **strp, const char *fmt, ...);
 
-- 
GitLab