Time lib on T3.6 using internal RTC falsely(?) reports time is set

Status
Not open for further replies.

Ben

Well-known member
Here's a sketch to recreate my issue. My Win7 machine needs the delay(), sorry:
Arduino 1.8.1, Teensyduino 1.35 on Windows 7 SP1.No errors or warnings during compile/upload (verbose output enabled).
Using library Time at version 1.5 in folder: C:\[...]\arduino\arduino-1.8.1+teensyduino1.35\hardware\teensy\avr\libraries\Time

Code:
#include <TimeLib.h>
void setup()
{
  Serial.begin(9600);
  delay(2000);
  setSyncProvider(getTeensy3Time);
  if (timeStatus() == timeSet) {
    Serial.println("Time is set.");
  } else {
    Serial.println("Time is not set.");
  }
  digitalClockDisplay();
}

void loop()
{
}

time_t getTeensy3Time() {
  return Teensy3Clock.get();
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

It seems the internal RTC on the T3.6 gets initialized with the compile time, which only works if compile time == start time. Power cycling the teensy resets the RTC to compile time and timeStatus() still returns "timeSet". What can I do to make the Time library report "timeSet" only after I called setTime()?

Ben
 
Here's a sketch to recreate my issue. My Win7 machine needs the delay(), sorry:
Arduino 1.8.1, Teensyduino 1.35 on Windows 7 SP1.No errors or warnings during compile/upload (verbose output enabled).
Using library Time at version 1.5 in folder: C:\[...]\arduino\arduino-1.8.1+teensyduino1.35\hardware\teensy\avr\libraries\Time

Code:
#include <TimeLib.h>
void setup()
{
  Serial.begin(9600);
  delay(2000);
  setSyncProvider(getTeensy3Time);
  if (timeStatus() == timeSet) {
    Serial.println("Time is set.");
  } else {
    Serial.println("Time is not set.");
  }
  digitalClockDisplay();
}

void loop()
{
}

time_t getTeensy3Time() {
  return Teensy3Clock.get();
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

It seems the internal RTC on the T3.6 gets initialized with the compile time, which only works if compile time == start time. Power cycling the teensy resets the RTC to compile time and timeStatus() still returns "timeSet". What can I do to make the Time library report "timeSet" only after I called setTime()?

Ben

adding a battery to Vbat so it keeps RTC running during power cycle ?
 
I thought about my problem a bit more and I think I'm using the Time lib and the RTC wrong. The library reports "timeSet" because it has synced successfully to the internal RTC. So even if I used setTime() it would revert to what the RTC reports after the next sync of the library with the RTC. Instead, I need to use Teensy3Clock.set(t) to adjust the RTC clock, right?

However that still leaves me with the issue that I can not know whether the time reported by the RTC is correct (because a backup battery was attached during power cycling) or incorrect (because the backup battery failed or was not attached).
 
I thought about my problem a bit more and I think I'm using the Time lib and the RTC wrong. The library reports "timeSet" because it has synced successfully to the internal RTC. So even if I used setTime() it would revert to what the RTC reports after the next sync of the library with the RTC. Instead, I need to use Teensy3Clock.set(t) to adjust the RTC clock, right?

However that still leaves me with the issue that I can not know whether the time reported by the RTC is correct (because a backup battery was attached during power cycling) or incorrect (because the backup battery failed or was not attached).

I assume you know the code lines 1099 ff in mk20dx128.c where the RTC is handled on restart?
 
Thanks for your help! No I didn't know about these lines. That's excellent, I can just test register 0x4003E01C for that "magic number" to see if the RTC has been reset to compile time. Great, thanks for that hint!
Is it okay if I reset that register to 0x00 in my sketch after I called Teensy3Clock.set(t) to indicate the RTC time is good again?

Ben
 
Is it okay if I reset that register to 0x00 in my sketch after I called Teensy3Clock.set(t) to indicate the RTC time is good again?

Sure, you can write a zero there if you like. Doing so will prevent the RTC from being set after an upload.

But if you've uploaded even once since cold powerup (no VBAT power), it should already be zero and remain zero as long as the RTC has power.
 
Status
Not open for further replies.
Back
Top