TimeLib does not work correctly on 4.1

birb

Member
So I'm trying to copy the teensy3 example for using the RTC and syncing the clock with the computer using the compile time of the hex file and from looking at other threads, it should work perfectly fine on the 4.1. However, even the raw example code sets my clock exactly 5 hours behind:
1701036610196.png


and trying to integrate it into my actual project sets the clock about 5 years back:
1701036668216.png



Now in messing around with my actual project code, 1 time it worked identically to the example, but then reverted back to 5 years behind after i tried a few other things, then reverted. Also the code crashes entirely if I include:

C++:
if (timeStatus()!= timeSet) {
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }





I'm not sure what other details to include so here is just my entire main file, (there is a lot going on, but this is handled entirely in the setup function):

C++:
#include <Arduino.h>
#include <FlexCAN_T4.h>
#include <format>
#include "arduino_freertos.h"
#include "avr/pgmspace.h"
#include "TeensyDebug.h"
#include "FS_CAN.h"
#include "interfaces/inverter.h"
#include "interfaces/bms.h"
#include "black_box.h"

#pragma GCC optimize("O0")

FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> Can0;
FS_CAN FS_CAN0(&Can0);
time_t getTeensy3Time();

void can_sniff(const CAN_message_t &msg)
{
    FS_CAN0.CAN_RX_ISR(msg);
}

static void task1(void *)
{
    uint32_t testData = 0;
    uint32_t testData2 = 0;
    FS_CAN::CAN_MSG testMsg;
    testMsg.id = 256;
    testMsg.signal_count = 1;
    FS_CAN::CAN_SIGNAL testSignal{&testData, 0, 16, false};
    FS_CAN::CAN_SIGNAL testSignal2{&testData2, 8, 8, false};
    testMsg.signals.push_back(&testSignal);
    testMsg.signals.push_back(&testSignal2);
    Serial.printf("didnt' crash yet\n");
    FS_CAN0.subscribe_to_message(&testMsg);

    while (true)
    {
        digitalWriteFast(LED_BUILTIN, LOW);
        vTaskDelay(pdMS_TO_TICKS(500));
        BlackBox::log(LOG_INFO, std::format("data1: {} data2: {}", testData, testData2).c_str());
        digitalWriteFast(LED_BUILTIN, HIGH);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

void setup_task(void *)
{
    Can0.begin();
    Can0.setBaudRate(500000);
    Can0.setMaxMB(64);
    Can0.enableFIFO();
    Can0.enableMBInterrupt(FIFO);
    Can0.enableFIFOInterrupt();
    Can0.onReceive(can_sniff);

    BlackBox::begin(100, tskIDLE_PRIORITY + 1);

    // Delay to allow the serial port to be opened and queue to be created (for some reason it needs this)
    vTaskDelay(pdMS_TO_TICKS(5000));

    xTaskCreate(task1, "task1", 5028, nullptr, tskIDLE_PRIORITY + 2, nullptr);

    vTaskDelete(nullptr);
}

void setup()
{
    setSyncProvider(getTeensy3Time);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWriteFast(LED_BUILTIN, HIGH);

    if (CrashReport)
    {
        Serial2.print(CrashReport);
        Serial2.println();
        Serial2.flush();
    }

    Serial.println(PSTR("\r\nBooting FreeRTOS kernel " tskKERNEL_VERSION_NUMBER ". Built by gcc " __VERSION__ " (newlib " _NEWLIB_VERSION ") on " __DATE__ ". ***\r\n"));

    xTaskCreate(setup_task, "setup_task", 1024, nullptr, tskIDLE_PRIORITY + 1, nullptr);

    Serial.println("setup(): starting scheduler...");
    Serial.flush();

    vTaskStartScheduler();
}

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


void loop() {}
 
Minor update, if I use the Teensy GUI uploader, It goes back to 5 hours behind from 5 years, but I'm not sure what weird set of combinations of steps gets me into that 5 years state.
 
Last edited:
This is in platformio not the arduino IDE but I get this:

1701096424845.png

when I control-click to see it's definition, I find this.
1701096450948.png
 
Back
Top