Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Teensy 4.0 RTC - setting RTC time works, but current time returns after power cycle

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    10

    Teensy 4.0 RTC - setting RTC time works, but current time returns after power cycle

    With the end of DST coming up, I was playing around with changing the time on my Teensy 4.0 and came across something that I can't explain.

    Here's the setup:

    * Teensy 4.0 with a 3V lithium battery hooked up to VBat.
    * Raspberry Pi 3B, Arduino IDE 1.8.8, TeensyduinoInstall.1.48.linuxarm
    * IDE settings:
    * Board: Teensy 4.0
    * CPU speed: 600 MHz
    * Optimize: Faster

    I upload the following sketch, which demonstrates the problem I'm seeing:

    Code:
    #include <TimeLib.h>
    
    void printDigits(int digits,char leadingZero='0');
    
    void setup()  {
      Serial.begin(115200);
      while (!Serial);  // Wait for Arduino Serial Monitor to open
      Serial.print("Initial RTC:     "); digitalClockDisplay(Teensy3Clock.get());
      // Set the RTC to Jan 1, 2020
      Teensy3Clock.set(18262*SECS_PER_DAY);
      Serial.print("RTC after set:   "); digitalClockDisplay(Teensy3Clock.get());
      delay(5100);
      Serial.print("RTC 5 sec later: "); digitalClockDisplay(Teensy3Clock.get());
    }
    
    void loop() {
    }
    
    void digitalClockDisplay(time_t x) {
      printDigits(hour(x),' ');
      Serial.print(":");
      printDigits(minute(x));
      Serial.print(":");
      printDigits(second(x));
      Serial.print(" ");
      printDigits(day(x),' ');
      Serial.print(" ");
      printDigits(month(x),' ');
      Serial.print(" ");
      Serial.println(year(x)); 
    }
    
    void printDigits(int digits,char leadingZero){
      // utility function for digital clock display: prints preceding colon and leading 0
      if(digits < 10)
        Serial.print(leadingZero);
      Serial.print(digits);
    }
    After the upload, I start up the Serial Monitor and get the following output.

    Code:
    Initial RTC:     22:46:11 29 10 2019
    RTC after set:    0:00:00  1  1 2020
    RTC 5 sec later:  0:00:05  1  1 2020
    All good. The RTC was set to the current time when the sketch was uploaded. I reset it to Jan 1st, 2020 and read it back to make sure it was reset. I then wait 5 seconds and read it back again to make sure it's counting.

    Then I close the Serial Monitor and unplug the USB cable from the Teensy. Several minutes later, I plug the Teensy back in and start up the Serial Monitor. This is where things get strange.

    Code:
    Initial RTC:     22:53:37 29 10 2019
    RTC after set:    0:00:00  1  1 2020
    RTC 5 sec later:  0:00:05  1  1 2020
    I expected the initial RTC reading to be about 0:07:00 1 1 2020. Instead, it's the current time. It's almost like I didn't reset the RTC at all.

    I'm just using TimeLib to format the raw RTC time, so it's not that I'm setting the TimeLib time but not the RTC.

    I thought it was just the upload process that set the RTC time. I'd say it looks like starting the Serial Monitor does the same thing, but I see the same behaviour even when I use a terminal program (minicom) to connect to the Teensy the second time.

    Am I missing something incredibly obvious?

  2. #2
    Junior Member
    Join Date
    Oct 2019
    Posts
    10
    Okay, I get it. The set() code in rtc.c is setting the time in the RTC but not in the SRTC. So when the Teensy 4.0 is powered up and startup.c syncs the RTC to the SRTC, it loads the value that was set during the upload process.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •