Teensy 4.0 Epoch time if RTC not Set

Status
Not open for further replies.
In my new project using a Teensy 4.0 RTC, I need to determine if the battery has died and the RTC time is incorrect. If it is, certain actions must be taken to set the correct time.

In preperation, I did cut the trace to separate VIN from VUSB. I coded a small test program to display the Epoch time for the internal RTC on a cold startup without a battery connected. After programming the Teensy, I brought up the Serial Monitor console, left it display active, unplugged the Teensy power for 30 minutes and re applied power. Upon startup, my program shows:

Epoch time = 1546300801 01/01/49(2019) 00:00:01

Is my assumption incorrect, that the Epoch time should default to 00:00:00 on 1/1/1970?

Any guidance would be most appreciated.

My sample code is:

//
// Display Teensy RTC time on Powerup with no battery
//
#include <TimeLib.h>

void setup () {
Serial.begin(115200);
while (!Serial);
time_t tt = Teensy3Clock.get();
GetRTC();
if (tt == 0) {
// if Epoch is 0, Teesny RTC lost time
Serial.println(" RTC has lost time - take action");
}
else
{
Serial.print(" RTC is running");
}
}
void loop () {
}
void GetRTC() {
time_t tt = Teensy3Clock.get();
Serial.print("Epoch time = ");Serial.print(tt);
tmElements_t tmtm;
breakTime( tt, tmtm);

Serial.print(" ");
printDigits(tmtm.Month),Serial.print("/");
printDigits(tmtm.Day),Serial.print("/");
Serial.print(tmtm.Year),Serial.print("(");
Serial.print(tmtm.Year+1970),Serial.print(") ");

printDigits(tmtm.Hour),Serial.print(":");
printDigits(tmtm.Minute),Serial.print(":");
printDigits(tmtm.Second),Serial.print(" ");
}
void printDigits(int digits){
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
 
On T4, I would suggest to add a 3V coin cell and not to rely on Vin for keeping RTC.
E.g. T4 can switch off internal 3.3V and then you lost RTC.
Coin cell will last sufficiently long to avoid problems.
 
If you take a look in startup.c, towards the end of the ResetHandler() routine, you'll find the code that's setting the SRTC to 1/1/2019.
Code:
  // initialize RTC
  if (!(SNVS_LPCR & SNVS_LPCR_SRTC_ENV)) {
    // if SRTC isn't running, start it with default Jan 1, 2019
    SNVS_LPSRTCLR = 1546300800u << 15;
    SNVS_LPSRTCMR = 1546300800u >> 17;
    SNVS_LPCR |= SNVS_LPCR_SRTC_ENV;
  }
  SNVS_HPCR |= SNVS_HPCR_RTC_EN | SNVS_HPCR_HP_TS;
So your check for "was the RTC just started" should be for Jan 1, 2019.
 
Yes, adding a coin cell will keep the TC running.
I want to be proactive and handle the situation where the coin cell is dead and power is interrupted.
This situation would present itself with a date/time that is incorrect and needs to be set.
 
Great info!!
As long as the default date in startup.c never changes, I can code a check for the default date of 01/01/2019.
What concerns me are future changes that may change the default date in startup.c
I am reluctant in making changes to startup.c as those changes may be overwritten with future updates.
 
Unless you update versions of TeensyDuino it can't change. And that date was likely chosen on a purpose as a known setpoint designed to not be changed.

@Paul: I expect that "default Jan 1, 2019" was by design expected to not change.
 
Your code isn't printing the two digit year correctly. Try this:
Code:
  Serial.print((tmtm.Year + 1970)%100),Serial.print("(");

Pete
 
Pete,

You are very observant, compliments to you!
My compiled program was only a test. I was not looking to report a 2 digit year

//Rick
 
Status
Not open for further replies.
Back
Top