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

Thread: Teensy 4 RTC - prevent loader from changing time

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    3

    Teensy 4 RTC - prevent loader from changing time

    I'm moving up from my trusty 3.2 to 4.0. The changes in Serial ports and in the RTC have caused me a few headaches, mostly solved. What I cannot solve is how to stop teensyduino from setting the RTC every time I upload a sketch. In my application I'm setting the time to UTC and calculating the timezone time in the part of the country I'm currently in. My understanding was that when the time is initially set, teensy loader would not change it but that is not what is happening. Every time I upload, it sets the time to the PC time. I'm testing using the example sketch TimeTeensy3.

    Thanks.

    Code:
    /*
     * TimeRTC.pde
     * example code illustrating Time library with Real Time Clock.
     * 
     */
    
    #include <TimeLib.h>
    
    void setup()  {
      // set the Time library to use Teensy 3.0's RTC to keep time
      setSyncProvider(getTeensy3Time);
    
      Serial.begin(9600);
      while (!Serial);  // Wait for Arduino Serial Monitor to open
      delay(100);
      if (timeStatus()!= timeSet) {
        Serial.println("Unable to sync with the RTC");
      } else {
        Serial.println("RTC has set the system time");
      }
    }
    
    void loop() {
      if (Serial.available()) {
        time_t t = processSyncMessage();
        if (t != 0) {
          Teensy3Clock.set(t); // set the RTC
          setTime(t);
        }
      }
      digitalClockDisplay();  
      delay(1000);
    }
    
    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(); 
    }
    
    time_t getTeensy3Time()
    {
      return Teensy3Clock.get();
    }
    
    /*  code to process time sync messages from the serial port   */
    #define TIME_HEADER  "T"   // Header tag for serial time sync message
    
    unsigned long processSyncMessage() {
      unsigned long pctime = 0L;
      const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 
    
      if(Serial.find(TIME_HEADER)) {
         pctime = Serial.parseInt();
         return pctime;
         if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
           pctime = 0L; // return 0 to indicate that the time is not valid
         }
      }
      return pctime;
    }
    
    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);
    }

  2. #2
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    121
    Have you tried using the command line version of the loader? From admittedly just a quick glance, I can't see any code in there that sets the target's time on loading. You would have to compile the loader for your platform.

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Or you can use TyCommander - it doesn't have the time set code during upload which has not yet been made public -it has a thread and github.com/Koromix/tytools

    It is interesting it sees the time as set and then still over writes it, not seen a way to disable.

    Assuming you have an RTC battery on the T_4.0? - Does it maintain the time if unplugged and plugged back in? I haven't worked with that lately - but when I checked last my onboard cell was dead so it wouldn't hold time. Tested that after a post noted it consumes more power from the cell than a T_3.2 does.

  4. #4
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    121
    Curiosity got the better of me. The command line loader definitely does not reset the time when uploading a new sketch. It does require pressing the button on the T4.1 to start the download; the soft reset option didn't want to work (the comments do say that it's only for the T3.x boards but I was hoping!).

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Didn't expect it had the more recent time fix - but it has been updated with last TD release it seems to support T_4.1 as well. But that code is public - so if it had the time set during upload it could be added to TyCommander.

    Can you link or show where you saw the "T-3.2" only text? @Paul will want to fix that AFAIK.

  6. #6
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    121
    Quote Originally Posted by defragster View Post
    Assuming you have an RTC battery on the T_4.0? - Does it maintain the time if unplugged and plugged back in? I haven't worked with that lately - but when I checked last my onboard cell was dead so it wouldn't hold time. Tested that after a post noted it consumes more power from the cell than a T_3.2 does.
    I missed that. And as I've had the same dollar store CR2032 cell in a T4.0 setup since October of last year I was a bit concerned. So I just measured its in circuit voltage and it's still reading 3V and keeping the time backed up. Maybe I just got lucky.

  7. #7
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    121
    Quote Originally Posted by defragster View Post
    Can you link or show where you saw the "T-3.2" only text? @Paul will want to fix that AFAIK.
    It was in the usage text from the program itself (source line #43 in teensy_loader_cli.c):

    Code:
    earth:~/Documents/src/teensy_loader_cli $ ./teensy_loader_cli 
    Filename must be specified
    
    Usage: teensy_loader_cli --mcu=<MCU> [-w] [-h] [-n] [-b] [-v] <file.hex>
    	-w : Wait for device to appear
    	-r : Use hard reboot if device not online
    	-s : Use soft reboot if device not online (Teensy3.x only)
    	-n : No reboot after programming
    	-b : Boot only, do not program
    	-v : Verbose output
    
    Use `teensy_loader_cli --list-mcus` to list supported MCUs.
    
    For more information, please visit:
    http://www.pjrc.com/teensy/loader_cli.html

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Quote Originally Posted by Silverlock View Post
    I missed that. And as I've had the same dollar store CR2032 cell in a T4.0 setup since October of last year I was a bit concerned. So I just measured its in circuit voltage and it's still reading 3V and keeping the time backed up. Maybe I just got lucky.
    Has it been otherwise powered? Mine was in one of the beta units and battery was empty. It had been parked more than used - and off/on as the T_4.1 went through and out of Beta and mostly since it released.

  9. #9
    Senior Member
    Join Date
    Oct 2019
    Location
    Calgary
    Posts
    121
    Quote Originally Posted by defragster View Post
    Has it been otherwise powered? Mine was in one of the beta units and battery was empty. It had been parked more than used - and off/on as the T_4.1 went through and out of Beta and mostly since it released.
    It does see main power for several hours pretty much every day. But even so it's been off for at least 12 hours a day through all that time, and that's a very conservative estimate.

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,439
    Quote Originally Posted by Silverlock View Post
    It was in the usage text from the program itself (source line #43 in teensy_loader_cli.c):

    Code:
    earth:~/Documents/src/teensy_loader_cli $ ./teensy_loader_cli 
    Filename must be specified
    
    Usage: teensy_loader_cli --mcu=<MCU> [-w] [-h] [-n] [-b] [-v] <file.hex>
    	-w : Wait for device to appear
    	-r : Use hard reboot if device not online
    	-s : Use soft reboot if device not online (Teensy3.x only)
    	-n : No reboot after programming
    	-b : Boot only, do not program
    	-v : Verbose output
    
    Use `teensy_loader_cli --list-mcus` to list supported MCUs.
    
    For more information, please visit:
    http://www.pjrc.com/teensy/loader_cli.html
    Also > At a glance that text "-s : Use soft reboot if device not online" seems like maybe it should read :: "-s : Use soft reboot if device online" - Where offline implies a soft reboot wouldn't work?

    @Paul might see that here - but might be worth a link on the TD 1.54 Beta 5 thread?

  11. #11
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    The time is kept perfectly on power down, but updated only on upload of a sketch.

    I'm using PlatformIO and Teensyduino - is there some code in teensyduino where I can disable updating the RTC?

    Or can I integrate Tycommander with PlatformIO so the uplaod is through the PlatformIO/VSCode IDE?

  12. #12
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    For anyone finding this post in a search, this is how I solved my issue:

    I installed TyTools to C:\TyTools
    Added these lines in platformio.ini:

    upload_protocol = custom
    upload_command = C:\TyTools\tycmd.exe upload -B $UPLOAD_PORT $SOURCE
    build_flags = -DUSB_SERIAL_HID -DLAYOUT_US_ENGLISH

    I found PlatformIO uses it's own copy of C:\Arduino\hardware\teensy\avr\cores and libraries at C:\Users\xxxx\.platformio\packages\framework-arduinoteensy
    This isn't updated with a new version of teensyduino

    So I created symlinks in C:\Users\xxxx\.platformio\packages\framework-arduinoteensy

    C:\Arduino\hardware\teensy\avr>mklink /J C:\Users\xxxx\.platformio\packages\framework-arduinoteensy\cores cores
    C:\Arduino\hardware\teensy\avr>mklink /J C:\Users\xxxx\.platformio\packages\framework-arduinoteensy\libraries libraries

    (My Arduino installation is in the root of C: , xxxx is your user name)

Posting Permissions

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